/* Javascript Helper functions for pricing calculations
 *
 * $Id: pricing_hl.js,v 1.2 2008-04-07 13:15:59 mbayreut Exp $
 *
 * $Log: pricing_hl.js,v $
 * Revision 1.2  2008-04-07 13:15:59  mbayreut
 * Put new javascript structure into CSS and CSZ
 *
 * Revision 1.1  2008-04-07 13:01:14  mbayreut
 * Fix some bugs in CSS (missing line endings).
 * Restructure JS and pricing.php to make it more portable.
 *
 * ------------------------------------------------------------------ */

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 GetPrice(url, js_Gametype) {
	XmlHttp = false;
    // branch for native XMLHttpXmlHttpuest object
    if(window.XMLHttpRequest) {
    	try {
			XmlHttp = new XMLHttpRequest();
        } catch(e) {
			XmlHttp = false;
        }
    // branch for IE/Windows ActiveX version
    } else if(window.ActiveXObject) {
       	try {
        	XmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
      	} catch(e) {
        	try {
          		XmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        	} catch(e) {
          		XmlHttp = false;
        	}
		}
    }
    if(XmlHttp) {
	XmlHttp.open('GET', url, true);
	XmlHttp.onreadystatechange = function() {
	    if (XmlHttp.readyState != 4) 
	        return;
	    if (XmlHttp.status != 200) {
	        alert("There was a problem retrieving the XML data:\n" + XmlHttp.statusText);
	        return;
	    }
	    var curdate = new Date();
	    var year = curdate.getYear();
	    var month = curdate.getMonth();
	    var Location;
	    month++;
	    if (year < 1900) { year = year + 1900; }
	    document.getElementById("price").innerHTML = formatCurrency(XmlHttp.responseText);
	    // alert('Response: ' + XmlHttp.responseText);
	    document.getElementById("a3").value = formatCurrency(XmlHttp.responseText);
	    Location = document.getElementById("Location").value;
	    document.getElementById("item_number").value = js_Gametype + '-' + XmlHttp.responseText + '-' + year + '-' + month + '-' + Location + '-HLTV' + document.getElementById("HLTV").options[document.getElementById("HLTV").selectedIndex].value + '-PBS' + document.getElementById("PingBoost").options[document.getElementById("PingBoost").selectedIndex].value;
	}
	XmlHttp.send('');
    }
}

function GetGameType(js_Gametype) {
	 var url = '';
	 url = 'pricing.php?Game=' + js_Gametype +
	       '&Slots=' + document.getElementById("SlotCount").options[document.getElementById("SlotCount").selectedIndex].value +
	       '&GameType=' + document.getElementById("GameType").options[document.getElementById("GameType").selectedIndex].value +
	       '&HLTV=' + document.getElementById("HLTV").options[document.getElementById("HLTV").selectedIndex].value +
	       '&PingBoost=' + document.getElementById("PingBoost").options[document.getElementById("PingBoost").selectedIndex].value;
	 // alert('GetGameType URL: <' + url + '>');
	 document.getElementById("TS").innerHTML = document.getElementById("SlotCount").options[document.getElementById("SlotCount").selectedIndex].value + ' Man Teamspeak Server Free';
	 document.getElementById("item_name").value = js_Gametype + ' ' + document.getElementById("SlotCount").options[document.getElementById("SlotCount").selectedIndex].value + ' Slots';
         GetPrice(url, js_Gametype);

}

