var addingProductId = "";
var addingAttributeId = ""
var addingParentAttributeId = ""
var addingAttributeTxt = "";
var addingParentAttributeTxt = "";
function cart_showProcessing(){
	document.getElementById('div_ajaxProcessing_'+addingProductId).style.display="block";
	for (i=0;i<document.forms[0].length;i++) {		
	   document.forms[0].elements[i].disabled=true;
	}
}	
function cart_endProcessing(){	
	//if(ajaxRequest.activeRequestCount == 0){	
		document.getElementById('div_ajaxProcessing_'+addingProductId).style.display="none";	
		for (i=0;i<document.forms[0].length;i++) {
		   document.forms[0].elements[i].disabled=false;		   
		}
	//}
}

function checkShoppingCartStatus(productId,haschild){
	
	addingProductId = productId;
	
	if(haschild==0){
		attributeId = document.forms[0].slAttribute.value;
		attributeObj = document.forms[0].slAttribute;
	}else{
		attributeId = document.forms[0].elements["slAttribute_"+addingParentAttributeId].value;
		attributeObj = document.forms[0].elements["slAttribute_"+addingParentAttributeId];
		
		//hidden field
		document.forms[0].slAttribute.value = attributeId;
	}
		
	if(attributeId!=""){
		//make stock status visible
		document.getElementById('span_productstatus_'+attributeId).style.display="";
		if(document.getElementById('span_productstatus_'+addingAttributeId)){
			document.getElementById('span_productstatus_'+addingAttributeId).style.display="none";
		}
		
		//replace currentAttributeId
		addingAttributeId = attributeId;
		addingAttributeTxt = attributeObj[attributeObj.selectedIndex].text;
		
		if(document.getElementById('hd_attributestock_'+attributeId).value>0){
			var url = 'AjaxWrapper.php';
			var pars = 'ajaxURL=order/controller/aj_cartstatus.php';	
			pars = pars + '&productId='+productId+'&attributeId='+attributeId;
			
			var myAjax = new Ajax.Request(
				url, 
				{
					method: 'get', 
					parameters: pars, 
					onCreate: Ajax_showProcessing(document.getElementById('div_ajaxProcessing_'+addingProductId)),
					onComplete: checkShoppingCartStatusAction 
				});
		}else{
			document.getElementById('emtpyAttributeCart_'+addingProductId).style.display="none";
			document.getElementById('addToCart_'+addingProductId).style.display="none";
			document.getElementById('chkoutnow_'+addingProductId).style.display="none";
			document.getElementById('span_restockbtn_'+addingProductId).style.display="";
		}
		
	}else{
		document.getElementById('span_productstatus_'+addingAttributeId).style.display="none";
		document.getElementById('emtpyAttributeCart_'+addingProductId).style.display="";
		document.getElementById('addToCart_'+addingProductId).style.display="none";
		document.getElementById('chkoutnow_'+addingProductId).style.display="none";
		document.getElementById('span_restockbtn_'+addingProductId).style.display="none";
		addingAttributeId = "";
	}
}

function checkShoppingCartStatusAction(originalRequest){
	
	Ajax_endProcessing(document.getElementById('div_ajaxProcessing_'+addingProductId));
	
	var result = originalRequest.responseText;	
	result = result.replace(/^\n\r\s*|\s*$/g, "");
	result = result.replace(/\n/g, "");
	result = result.replace(/\r/g, "");
	
	if(result.match("0")){
		//stock available for order
		document.getElementById('emtpyAttributeCart_'+addingProductId).style.display="none";
		document.getElementById('span_restockbtn_'+addingProductId).style.display="none";
		document.getElementById('chkoutnow_'+addingProductId).style.display = "none";	
		document.getElementById('addToCart_'+addingProductId).style.display="";
		
										
	}else if(result.match("1")){
		//stock added into cart
		document.getElementById('emtpyAttributeCart_'+addingProductId).style.display="none";
		document.getElementById('span_restockbtn_'+addingProductId).style.display="none";
		document.getElementById('chkoutnow_'+addingProductId).style.display = "";	
		document.getElementById('addToCart_'+addingProductId).style.display="none";
		
	}
}

function displaySubAttribute(){
	attributeId = document.forms[0].slAttribute_parent.value;
	attributeTxt = document.forms[0].slAttribute_parent[document.forms[0].slAttribute_parent.selectedIndex].text;
		
	//clear the hidden value
	document.forms[0].slAttribute.value="";
	
	//visible the child attribute
	if(attributeId!=""){
		document.getElementById('childatt_'+attributeId).style.display="";
		document.forms[0].elements['slAttribute_'+attributeId].value="";		
	}
	
	if(addingAttributeId!=""){
		document.getElementById('span_productstatus_'+addingAttributeId).style.display="none";
		document.getElementById('emtpyAttributeCart_'+addingProductId).style.display="";
		document.getElementById('addToCart_'+addingProductId).style.display="none";
		document.getElementById('chkoutnow_'+addingProductId).style.display="none";
		document.getElementById('span_restockbtn_'+addingProductId).style.display="none";
	}
	addingAttributeId = "";
	
	
	//invisible the previous selected child attribute 
	
	if(document.getElementById('childatt_'+addingParentAttributeId)){
		//document.forms[0].elements["slAttribute_"+addingParentAttributeId].value="";
		document.getElementById('childatt_'+addingParentAttributeId).style.display="none";
	}
	addingParentAttributeId = attributeId;
	addingParentAttributeTxt = attributeTxt
}


function addToCart(productId,unitprice,cartQty,ispromotion){
	//alert("adding... "+productId+","+unitprice+","+cartQty);
	addingProductId = productId;
	
	//var url = 'AjaxWrapper.php';
	//var url = 'AjaxWrapper_special.php';
	//var pars = 'ajaxURL=order/controller/aj_addToCart.php';	
	var url = "aj_addToCart.php";
	var pars = 'productId='+productId+'&unitprice='+unitprice;
	//pars = pars + 'productId='+productId+'&unitprice='+unitprice;
	pars = pars + '&attributeId='+addingAttributeId;
	pars = pars + '&ispromotion='+ispromotion;
	pars = pars + '&cartQty='+cartQty;
	
	var myAjax = new Ajax.Request(
		url, 
		{
			method: 'get', 
			parameters: pars, 
			onCreate: Ajax_showProcessing(document.getElementById('div_ajaxProcessing_'+addingProductId)),
			onComplete: addedToCart 
		});
}

function addedToCart(originalRequest){
	Ajax_endProcessing(document.getElementById('div_ajaxProcessing_'+addingProductId));
	var result = originalRequest.responseText;	
	result = result.replace(/^\n\r\s*|\s*$/g, "");
	result = result.replace(/\n/g, "");
	result = result.replace(/\r/g, "");
	
	if(result.match("1")){	
		//newContent = '<img src="images/chk_on1.gif"> <span class="addToCart">'+document.form1.hdTxtAddedIntoCart.value+'</span>';		
		//document.getElementById('addToCart_'+addingProductId).innerHTML=newContent;
		document.getElementById('addToCart_'+addingProductId).style.display = "none";	
		document.getElementById('chkoutnow_'+addingProductId).style.display = "";	
		var cartsize = document.getElementById('span_cartsize').innerHTML;
		document.getElementById('span_cartsize').innerHTML = (parseInt(cartsize)+1);
		
	}else{
		alert(document.form1.hdTxtFailedToAdd.value+"..");
	}
}

