//****************
// all-scripts.js
// covers as many sanbrook scripts as possible
//****************
	
var xmlhttp;
var currNodeList;

function updateOrder(obj)
{
	var productid = obj.name.split("_")[1];
	var quantity = obj.value.rightTrim().leftTrim();

	if(quantity =='' || isNaN(quantity) || quantity.indexOf(".") > -1)
	{
		quantity = 0;
		
	}
	
	if (quantity == 0) obj.value = '';

	//document.getElementById("debugbox").value += "\n/ajax/update-order.php?p="+escape(productid)+"&q="+quantity+"&pr="+obj.price;
	
	// code for Mozilla, etc.
	if (window.XMLHttpRequest)
	{
		xmlhttp=new XMLHttpRequest();
		xmlhttp.onreadystatechange=xmlhttpChange;
		xmlhttp.open("GET","/ajax/update-order.php?p="+escape(productid)+"&q="+quantity+"&pr="+obj.getAttribute("price"),true);
		xmlhttp.send(null);
	}
	// code for IE
	else if (window.ActiveXObject)
  	{
  		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
    	if (xmlhttp)
    	{
		    xmlhttp.onreadystatechange=xmlhttpChange;
		    l_strQuery = "/ajax/update-order.php?p="+escape(productid)+"&q="+quantity+"&pr="+obj.price;
		    xmlhttp.open("GET",l_strQuery,true);
		    xmlhttp.send();
    	}
  	}
}

function xmlhttpChange()
{
	// if xmlhttp shows "loaded"
	if (xmlhttp.readyState==4)
  	{
  		// if "OK"
  		if (xmlhttp.status==200)
    	{
	   		itemObj = xmlhttp.responseXML.documentElement;
			
    		itemcount = itemObj.getElementsByTagName("totalitems")[0].firstChild.nodeValue;
    		totalcost = itemObj.getElementsByTagName("totalcost")[0].firstChild.nodeValue;
    		
    		document.getElementById("order_summary").innerHTML = "You have <strong>" + itemcount + "</strong> items totalling <strong>$" + formatCurrency(totalcost) + "</strong> (excluding GST)";
    		
			
    	}
  		else
    	{
    		alert("Problem retrieving XML data")
    	}
  	}
}

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 rightTrim() { 
    return this.replace(/\s+$/gi, ""); 


} 

function leftTrim() { 
    return this.replace(/^\s*/gi, ""); 


} 

String.prototype.rightTrim = rightTrim; 
String.prototype.leftTrim = leftTrim;



