function chasm_set_cookie(name, value, days)
{
    var today = new Date();
    var exp   = new Date();

    if (!days) days = 1;
    exp.setTime(today.getTime() + 3600000 * 24 * days);
    document.cookie = name + "=" + escape(value) + ";expired=" + exp.toGMTString();
}

function chasm_show_hide(obj)
{
    if (document.getElementById(obj).style.display == 'none')
        document.getElementById(obj).style.display = 'block';
    else
        document.getElementById(obj).style.display = 'none';
}

function chasm_show_hide_row(obj)
{
    if (document.getElementById(obj).style.display == 'none')
        document.getElementById(obj).style.display = 'table-row';
    else
        document.getElementById(obj).style.display = 'none';
}

function chasm_new_window(width, height, url, name)
{
    var top  = (screen.height / 2) - (height / 2);
    var left = (screen.width  / 2) - (width  / 2);

    window.open(url, name, 'location=no,scrollbars=yes,top=' + top + ',left=' + left + ',width=' + width + ',height=' + height, false);
}

function chasm_trim(inputString)
{
    if (typeof inputString != "string") return inputString;

    var retValue = inputString;
    var ch = retValue.substring(0, 1);

    while (ch == " ")
    {
        retValue = retValue.substring(1, retValue.length);
        ch = retValue.substring(0, 1);
    }

    ch = retValue.substring(retValue.length - 1, retValue.length);
    
    while (ch == " ")
    {
        retValue = retValue.substring(0, retValue.length - 1);
        ch = retValue.substring(retValue.length - 1, retValue.length);
    }
    
    while (retValue.indexOf("  ") != -1)
    {
        retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length);
    }

    return retValue;
}

function chasm_format_currency(num)
{
    num = String(num);
    decimal = -1;

    for (i = 0; i < num.length; i++)
    {
        if (num.charAt(i) == ".")
        {
            decimal = i;
            break;
        }
    }

    if (decimal == -1) return num + ".00";

    if (num.charAt(num.length - 2) == '.')
    {
        return num + "0";
    }

    num = Math.round(num * 100) / 100;
    num = String(num);

    for (i = 0; i < num.length; i++)
    {
        if (num.charAt(i) == ".")
        {
            decimal = i;
            break;
        }
    }

    if (decimal == -1) return num + ".00";

    if (num.charAt(num.length - 2) == '.')
    {
        return num + "0";
    }

    return num;

}

function chasm_format_currency_display(num)
{
    if (isNaN(num))
        num = "0";

    sign  = (num == (num = Math.abs(num)));
    num   = Math.floor(num * 100 + 0.50000000001);
    cents = num % 100;
    num   = Math.floor(num / 100).toString();

    if (cents < 10)
      cents = "0" + cents;

    for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
      num = num.substring(0, num.length - (4 * i + 3)) + ',' + num.substring(num.length - (4 * i + 3));

    return (((sign) ? '' : '-') + num + '.' + cents);
}

function chasm_valid_email(email)
{
    var at_sym   = email.indexOf('@');
    var period   = email.lastIndexOf('.');
    var space    = email.indexOf(' ');
    var length   = email.length - 1;

    return (at_sym < 1 || period <= at_sym + 1 || period == length || space != -1) ? false : true;
}

function chasm_copy_text(control)
{
    r = control.createTextRange();
    r.select();
    r.execCommand('copy');
}

function chasm_input_event_char(event)
{
    if (event.which)
    {
        return event.which;
    }
    else
    {
        return event.keyCode;
    }
}

function chasm_input_numeric_only(event)
{
    if (event.charCode === 0 && event.keyCode)
        return true;

    code = chasm_input_event_char(event);
    if (code < 48 || code > 57) return false;
    return true;
}

function chasm_input_domain_only(event)
{
    if (event.charCode === 0 && event.keyCode)
        return true;

    code = chasm_input_event_char(event);

    if (code == 42)                   return true;
    if (code >= 48 && code <= 57)  return true;
    if (code >= 65 && code <= 90)  return true;
    if (code >= 97 && code <= 122) return true;
    if (code == 46 || code == 45)  return true;
    return false;
}

function chasm_input_nickname_only(event)
{
    if (event.charCode === 0 && event.keyCode)
        return true;

    code = chasm_input_event_char(event);

    if (code >= 48 && code <= 57)  return true;
    if (code >= 65 && code <= 90)  return true;
    if (code >= 97 && code <= 122) return true;
    if (code == 45 || code == 95)  return true;
    return false;
}

function chasm_input_conform_domain(ctrl)
{
    ctrl.value = ctrl.value.replace(/[^a-zA-Z0-9\-\.\*]+/g, "");
    ctrl.value = ctrl.value.replace(/[\-\.]{2,}/g, ".");
    ctrl.value = ctrl.value.replace(/[\.]{2,}/g,   ".");
    ctrl.value = ctrl.value.replace(/[\-]{2,}/g,   "-");
    ctrl.value = ctrl.value.replace(/[\*]{2,}/g,   "*");

    first = ctrl.value.charAt(0);
    last  = ctrl.value.charAt(ctrl.value.length - 1);

    if (first == "." || first == "-")
    {
        ctrl.value = ctrl.value.substr(1);
    }
    if (last == "." || last == "-")
    {
        ctrl.value = ctrl.value.substr(0, ctrl.value.length - 1);
    }
}

function chasm_search_reboot_bar(server_id) {
  $.ajax({
    url: "./index.php?mode=popup&tile=search_reboot_bar&server_id=" + server_id,
    success: function(data) {
      if(data == 1) {
        $("[name='edit_port_allow_client_control']").filter("[value=1]").attr("checked","checked");
      } else {
        $("[name='edit_port_allow_client_control']").filter("[value=0]").attr("checked","checked");
      }
    }
  });
}
