/* Javascript Helper functions for pricing calculations
 *
 * $Id: pricing_ts.js,v 1.2 2008-04-22 08:44:31 mbayreut Exp $
 *
 * $Log: pricing_ts.js,v $
 * Revision 1.2  2008-04-22 08:44:31  mbayreut
 * Variable fixes in JS file. Linkage fixes and some bug fixes in ts_order
 *
 * ------------------------------------------------------------------ */

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);
}

function CheckForm() {
    var Size;
    var curdate = new Date();
    var year = curdate.getYear();
    var month = curdate.getMonth();
    var location;
    var PriceIng;

    month++;

    if (year < 1900) { year = year + 1900; }

    if (document.getElementById("location").options[document.getElementById("location").selectedIndex].value == "NA") {
	alert("You must select a location");
	return false;
    } else {
	location = document.getElementById("location").options[document.getElementById("location").selectedIndex].value;
	Size = document.getElementById("slots").value;
	
	if (Size < 5)
	{
	  alert("Slot count must be greater than 4");
	  return false;
	}
	
	if (Size < 16) { 
	    PriceIng = (Size * '.40'); 
	} 

	if ((Size >= 16) && (Size <= 30)) { 
	    PriceIng = (Size * '.35'); 
	}

	if ((Size >= 31) && (Size < 50)) { 
	    PriceIng = (Size * '.32'); 
	}

	if (Size >= 50) { 
	    PriceIng = (Size * '.28'); 
	}

	PriceIng = formatCurrency(PriceIng);
	//alert(PriceIng);
	document.getElementById("a3").value = PriceIng;
	document.getElementById("item_number").value = 'TS-' + Size + '-' + year + '-' + month + '-' + location;
	document.getElementById("item_name").value = 'Teamspeak ' + Size + ' Slots';
	//alert('TS-' + Size + '-' + year + '-' + month + '-' + location);			 
	return true;
    }
}

function GetPrice(slotcount) {
  var PriceIng;
  if (slotcount < 16) { PriceIng = slotcount * .40; }
  if ((slotcount >= 16) && (slotcount <= 30)) { PriceIng = slotcount * .35; }
  if ((slotcount >= 31) && (slotcount <= 50)) { PriceIng = slotcount * .32; }
  if (slotcount > 50) { PriceIng = slotcount * .28; }
  PriceIng = formatCurrency(PriceIng);
 document.getElementById("price").innerHTML = '<b><span class="achGreen">' + PriceIng + '</span></b>';
}

