var axCart;
var oCart;


function addProduct(id_type, id_product_attribute, qty){		
	var squery = "";
	
	if (id_type != '' && id_product_attribute != '' && qty != '') {	
		squery = "?id_type="+id_type+"&id_product_attribute="+id_product_attribute+"&qty="+qty;
		onPopupMsg('on','loading');
		
		if (window.XMLHttpRequest) axCart = new XMLHttpRequest();
		else if (window.ActiveXObject) axCart = new ActiveXObject("Microsoft.XMLHTTP");
		axCart.open("GET", 'php/c-add-shoppingcart.php'+squery, true);
		axCart.onreadystatechange = parseCartResult;
		axCart.send(null);
	}
}

function updateCart(){
	var objitems = document.getElementById("c-items");
	var objprices = document.getElementById("c-prices");
	
	if (objitems != undefined) objitems.innerHTML = oCart.items;
	if (objprices != undefined) objprices.innerHTML = oCart.prices;	
}



function parseCartResult() {
	if (axCart.readyState == 4) {
		onPopupMsg('off','');
		
		switch (axCart.status) {
			case 200:
				var xmldoc = axCart.responseXML;
				var root = xmldoc.getElementsByTagName("list")[0];
				var code = root.getAttribute("code");
				oCart = new Object();						
				
				axCart = null;
				
				if (code == "100") {				
					for (var i=0; i<root.childNodes.length; i++) {
						var nField = root.childNodes.item(i);
						
						switch (nField.nodeName) {
							case "items":
							oCart.items = nField.firstChild.nodeValue;
							break;
						case "prices":
							oCart.prices = nField.firstChild.nodeValue;
							break;
						}
					}
					updateCart();
				}else{
					onPopupMsg('on','insufficient');
				}
				break;
			case 404:
				//alert(sResultXML+" not found.");
				break;
			default: 
				alert("Error "+axCart.status + " in loading data. Please try again.");
				break;
		}
	}
}





function onPopupMsg(smode, stype){
	var obj = document.getElementById("cart-msg");
	var obj_inner = document.getElementById("cart-inner");
	var dcartbg = document.getElementById("cart-msg-bg");
	
	if (smode == 'on') {
		if (stype == 'loading') obj_inner.innerHTML = "LOADING ...<BR><font style=\"font-size:12pt;\">Please wait while processing.</font>";
		else if (stype == 'insufficient') obj_inner.innerHTML = "SORRY.<BR><font style=\"font-size:12pt;\">Insufficient stock, please try again. </font><BR><BR><input type='button' value='close now' class='button' onclick='onPopupMsg(\"off\",\"\")'>";
		 
		obj.style.display = "";
		dcartbg.style.display = "";
	
		onPopupPostion();
	} else if (smode == 'off') {
		obj_inner.innerHtml = "";
		obj.style.display = 'none';
		dcartbg.style.display = "none";
	
	}
}

var aPosition;
function onPopupPostion(){
	//adjust position
	var dcart = document.getElementById("cart-msg");
	var dcartbg = document.getElementById("cart-msg-bg");
	aPosition = getScrollXY();
	
	if (self.innerHeight) {	 
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) {  
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) {  
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	dcart.style.left = aPosition[0] + ((parseInt(windowWidth)/2)-(parseInt(dcart.style.width))/2)+'px';
	dcart.style.top = aPosition[1] + ((parseInt(windowHeight)/2)-(parseInt(dcart.style.height))/2)+'px';	
	
	dcartbg.style.width = aPosition[0]+windowWidth+'px';	
	dcartbg.style.height = xDocSize()["h"]+'px';
	//dcartbg.style.height = aPosition[1]+windowHeight+'px';	
}

function getScrollXY() {
	var scrOfX = 0, scrOfY = 0;
	if( typeof( window.pageYOffset ) == 'number' ) {
		//Netscape compliant
		scrOfY = window.pageYOffset;
		scrOfX = window.pageXOffset;
	} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		//DOM compliant
		scrOfY = document.body.scrollTop;
		scrOfX = document.body.scrollLeft;
	} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		//IE6 standards compliant mode
		scrOfY = document.documentElement.scrollTop;
		scrOfX = document.documentElement.scrollLeft;
	}
	return [ scrOfX, scrOfY ];
}


function xDocSize(){
	var b=document.body, e=document.documentElement;
	var esw=0, eow=0, bsw=0, bow=0, esh=0, eoh=0, bsh=0, boh=0;
	
	if (e) {
		esw = e.scrollWidth;
		eow = e.offsetWidth;
		esh = e.scrollHeight;
		eoh = e.offsetHeight;
	}
	
	if (b) {
		bsw = b.scrollWidth;
		bow = b.offsetWidth;
		bsh = b.scrollHeight;
		boh = b.offsetHeight;
	}
	//  alert('compatMode: ' + document.compatMode + '\n\ndocumentElement.scrollHeight: ' + esh + '\ndocumentElement.offsetHeight: ' + eoh + '\nbody.scrollHeight: ' + bsh + '\nbody.offsetHeight: ' + boh + '\n\ndocumentElement.scrollWidth: ' + esw + '\ndocumentElement.offsetWidth: ' + eow + '\nbody.scrollWidth: ' + bsw + '\nbody.offsetWidth: ' + bow);
	return {w:Math.max(esw,eow,bsw,bow),h:Math.max(esh,eoh,bsh,boh)};
}



