//recalculate price when maintenance checkbox is clicked
function recalc_price(box) {
//alert('Hi');
var vlcost, vmcost, vtcost, numlic, mapcount, vpcost, discount, discountp, server_cost, scost, appcount, appcost, damount, subamount, jpcount, jpcost, jpmaint_cost;
numlic = document.calculator.liccount.value;
//discount = document.calculator.discount.value;
//discountp = discount / 100;
if (numlic>499) {
  if (numlic<2100) {
discount = (Math.floor((numlic / 100) + 5))/100;
//alert('disc is ' + discount);
  } else {
  discount = 0.25;
  }
} else {
discount = 0;
}
vlcost = numlic * 5;
if (numlic<1) {
server_cost = 0;
} else {
    server_cost = 500;
}
if (document.calculator.maint.checked == true)
  {
    vmcost = numlic * 2;
	}
	else {
	vmcost = 0;
	}
  //format vars
if (box==1)
{  
 // alert('value is ' + box);
  mapcount = numlic;
  appcount = numlic;
  jpcount = numlic;
  document.calculator.mapcount.value = mapcount;
  document.calculator.appcount.value = appcount;
  document.calculator.jpcount.value = jpcount;
} else {
//alert('value is ' + box);
    //all this is to make sure people buy the same number of AppUse licenses as LabStats licenses
	appcount = document.calculator.appcount.value;
	if (numlic>0&&numlic!=appcount&&appcount!=0) 
	{		
		  alert('Any AppUse client licenses you purchase must match your LabStats client licenses.');
          appcount = numlic;
	      //sets the AppUse license number box to match LabStats licenses
          document.calculator.appcount.value = appcount;
	} 
  jpcount = document.calculator.jpcount.value;
  mapcount = document.calculator.mapcount.value;
}
//have to run this again after jpcount is defined, to figure jp maint
if (document.calculator.maint.checked == true)
  {
	jpmaint_cost = jpcount * 1;
	}
	else {
	jpmaint_cost = 0;
	}
vpcost = mapcount * 1;
appcost = appcount * 2;
jpcost = jpcount * 4;
  vtcost = vmcost + vlcost + server_cost + vpcost + appcost + jpcost + jpmaint_cost;
  subamount = vtcost;
if (discount > 0){
damount = vtcost * discount;
vtcost = vtcost - damount;
} else {
damount = 0;
}
  //alert('vmcost = ' + vmcost);
  //alert('vlcost = ' + vlcost);
  //alert('vtcost = ' + vtcost);
  //discountamount = formatCurrency(discountamount);
  server_cost = formatCurrency(server_cost);
  appcost = formatCurrency(appcost);
  jpcost = formatCurrency(jpcost);
  jpmaint_cost = formatCurrency(jpmaint_cost);
  subamount = formatCurrency(subamount);
  damount = formatCurrency(damount);
  vpcost = formatCurrency(vpcost);
  vlcost = formatCurrency(vlcost);
  vmcost = formatCurrency(vmcost);
  vtcost = formatCurrency(vtcost);
  //alert('vmcost = ' + vmcost);
  //alert('vlcost = ' + vlcost);
  //alert('vtcost = ' + vtcost);
  //place vars
  //document.calculator.tdiscount.value = discountamount;
  document.calculator.scost.value = server_cost;
  document.calculator.pcost.value = vpcost;
  document.calculator.tdiscount.value = damount;
  document.calculator.tsubtotal.value = subamount;
  document.calculator.lcost.value = vlcost;
  document.calculator.mcost.value = vmcost;
  document.calculator.acost.value = appcost;
  document.calculator.jcost.value = jpcost;
  document.calculator.jmcost.value = jpmaint_cost;
  document.calculator.tcost.value = vtcost;
  discp = discount * 100;
  discp = Math.floor(discp);
  discd = "&nbsp;&nbsp;- " + discp + "%";
  display(discd);
}
 //format as currency
function formatCurrency(num) {
num = num.toString().replace(/\$|\,/g,'');
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);
}
//display the discount percentage
function display(str) {
    document.getElementById('percent').innerHTML = str;
}