function mOvr(src) { if (src.getElementsByTagName("A").length){ src.style.cursor = document.all?'hand':'pointer'; } src.style.backgroundColor = "#CCCCFF"; }
function mOut(src) { src.style.cursor = "default"; if (src.className=="row1") { src.style.backgroundColor = ""; } else { src.style.backgroundColor = "#F4F4F4"; } }
function mClk(src) { if(src.tagName=="TD" && src.getElementsByTagName("A").length){ location.href=src.getElementsByTagName('a')[0].href; } }

function calculate(evt) {
var form = document.Services;
maxlogins = form.maxlogins.value;
accountprice = form.accountprice.value;
bgprice = form.bgprice.value;
quotaprice = form.quotaprice.value;
if (maxlogins > 1) { logincount = form.accounts.value; } else { logincount = 1; }
service = form.service.value;
term = form.paymentplan.value;
if (service == 2) { userprice = form.userprice.value; ircdprice = form.ircdprice.value; } else { userprice=0; ircdprice=0; }
var subtotal;
var total = 0;
var background;
var diskquota;
  for (x = 1; x <= maxlogins; x++) {
    username = document.getElementById("formUsername[" + x + "]");
    if (logincount >= x) {
      username.style.display="";
      background = document.getElementById("BG[" + x + "]").value;
      diskquota = document.getElementById("DQ[" + x + "]");
      if (evt && evt.name=="BG[" + x + "]") diskquota.value = background * 30;
      if (service=="1" && diskquota.value < (background * 30)) diskquota.value=background * 30;
      if (service=="1" && diskquota.value > 100) diskquota.value=100;
      if (service=="2" && diskquota.value < 50) diskquota.value=50;
      diskquota = document.getElementById("DQ[" + x + "]").value;
      if (service==2) {
        users = document.getElementById("USERS[" + x + "]");
        if (users.value < 200) { users.value=200; }
        if (users.value > 1000) { users.value=1000; }
        users = document.getElementById("USERS[" + x + "]").value; 
        ircdprocs = document.getElementById("IRCD[" + x + "]").value; }
      else { users=0; ircdprocs=0; }
      if (service == 2 && document.getElementById("services[" + x + "]").value==1) {services = 5; } else { services = 0; }
      subtotal = ((accountprice * 1) + (bgprice * background) + (diskquota * quotaprice) + services + (users * userprice) + (ircdprocs * ircdprice));
      total = total + subtotal;
      subtotal = subtotal.toFixed(2);
      document.getElementById("subtotal[" + x + "]").value  = "$" + subtotal;
    } else {
      username.style.display="none";
    }
  }
  if (term == 1) { discount = 0;
  } else if (term == 3) { discount = 0.05;
  } else if (term == 6) { discount = 0.10;
  } else if (term == 12) { discount = 0.15; }
  if (term > 1) {
    document.getElementById("discounts").style.display="";
  } else { document.getElementById("discounts").style.display="none"; }
  grandtotal = (total * term);
  discount = grandtotal * discount;
  form.grandtotal.value = "$" + (grandtotal).toFixed(2);
  form.discount.value = "$" + (discount).toFixed(2);
  form.total.value = "$" + (grandtotal - discount).toFixed(2);
}

function ValidateForm() {var error=false;
  var form = document.Services;
  maxlogins = form.maxlogins.value;
  if (maxlogins > 1) { logincount = form.accounts.value; } else { logincount = 1; }
  for (x = 1; x <= maxlogins; x++) {
    username = document.getElementById("Username[" + x + "]");
    errormsg = document.getElementById("Error[" + x + "]");
    DQ = document.getElementById("DQ[" + x + "]");
    var login = username.value.toLowerCase();
    var regexp = new RegExp(/^[a-z0-9_][a-z0-9_-]*$/);
    var _test = regexp.test(login);
    var error_msg = 0
    if(login.length>16) error_msg = document.createTextNode("Username is longer than 16 characters.");
    else if(!_test && login!="") error_msg = document.createTextNode("Illegal username. (" + login + ")");
    if (error_msg) username.value="";
    else error_msg = document.createTextNode("");
    errormsg.replaceChild(error_msg, errormsg.lastChild);
    if (logincount >= x && username.value=="") { username.style.backgroundColor = '#FFE8E8'; error=true; } else { username.style.backgroundColor = '#FFFFFF'; }
    if (logincount >= x && DQ.value=="") { DQ.style.backgroundColor = '#FFE8E8'; error=true; } else { DQ.style.backgroundColor = '#FFFFFF'; }
  }
  if (error==true) {
    if (form.reset.length) form.reset.disabled=false;
    if (form.submit.length) form.submit.disabled=false;
    if (form.action) form.action.value="";
    return false;
  } else {
    if (form.action) form.action.value="submit";
    return true;
  }
}

function numberonly(e) {
var key;
var keychar;
if (window.event)
  key = window.event.keyCode;
else if (e)
  key = e.which;
else
  return true;
keychar = String.fromCharCode(key);
keychar = keychar.toLowerCase();
if ((key==null) || (key==0) || (key==8) || (key==9) || (key==13) || (key==27) )
  return true;
else if ((("0123456789").indexOf(keychar) > -1))
  return true;
else
  return false;
}

function check_uncheck(form) {
  with(form) {
    for(i=0;i<elements.length;i++) {
      thiselm = elements[i];
      if(thiselm.name.substring(0,6) == 'checks') thiselm.checked = !thiselm.checked
    }
  }
}

function dcheck_uncheck(field) {
  for (i = 0; i < field.length; i++) {
    if (field[i].checked == true) { field[i].checked = false; }
    else { field[i].checked = true; }
  }
}
