var total_price_xml = false;
var quantity_xml = false;


var loading_html = '<table width="100%">'
	+'<tr>'
	+'<td align="left"></td>'	
	+'<td align="right"><input type="submit" name="submit" onClick="HideContent(\'curtain\'); HideContent(\'popup\');" value="Close"></input></td>'
	+'</tr>'
	+'<tr>'
	+'<td align="center" colspan="2"><br><br><center>Loading...</center><br><br></td>'	
	+'</tr>'
	+'</table>';

// ------------ Div Functions ---------------

function HideContent(d) {
	if(d.length < 1) { return; }
	document.getElementById(d).style.visibility = "hidden";
	document.getElementById(d).style.display = "none";
}
function ShowContent(d) {
	if(d.length < 1) { return; }
	document.getElementById(d).style.visibility = "visible";
	document.getElementById(d).style.display = "";
}
function ReverseContentDisplay(d) {
	if(d.length < 1) { return; }
	if(document.getElementById(d).style.display == "hidden") { document.getElementById(d).style.display = "visible"; }
	else { document.getElementById(d).style.display = "hidden"; }
}

// ------------ innerHTML Page loader ---------------

function retrieveURL(url, element){
	if (window.XMLHttpRequest) { // Non-IE browsers
 		req = new XMLHttpRequest();
 		elem = element;
 		req.onreadystatechange = processStateChange;
 		try { req.open("GET", url, true); }
 		catch (e) { alert(e); }
 		req.send(null);
	}
	else if (window.ActiveXObject) { // IE
 		req = new ActiveXObject("Microsoft.XMLHTTP");
 		if (req) {
			elem = element;
 			req.onreadystatechange = processStateChange;
 			req.open("GET", url, true);
 			req.send();
		}
	}
}
function processStateChange() {
	if (req.readyState == 4) { // Complete
 		if (req.status == 200) { // OK response
 			document.getElementById(elem).innerHTML = req.responseText;
		}
		else { alert("Problem: " + req.statusText); }
 	}
}

// ------------ Global Interface Functions ---------

function gup( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

function getTotalPrice(){
	total_price_xml = makeXMLRequest('/cart/total_price/', '', updateTotalPrice);
}	

function getQuantity(){
	quantity_xml = makeXMLRequest('/cart/item_quantity/', '', updateQuantity);
}

function updateTotalPrice(){
	if (total_price_xml.readyState != 4) return false;
	if (total_price_xml.status != 200) return false;
	var text = total_price_xml.responseText;
	total_price_xml = false;
	if (!text){document.getElementById('total_price').innerHTML = ""; return false;}
	document.getElementById('total_price').innerHTML = text;
}

function updateQuantity(){
	if (quantity_xml.readyState != 4) return false;
	if (quantity_xml.status != 200) return false;
	var text = quantity_xml.responseText;
	quantity_xml = false;
	if (!text){document.getElementById('quantity').innerHTML = ""; return false;}
	var arr  = text.split(",");
	var total = 0;
	for (var h in arr)
		if ((arr[h] != null) && (arr[h] != '')) total += parseInt(arr[h]);	
	document.getElementById('quantity').innerHTML = '( '+total+' )';
}
