/* The following function creates an XMLHttpRequest object... */
function createDeleteRequestObject(){
	var delete_request_o; 
	var browser = navigator.appName;
	if(browser == "Microsoft Internet Explorer"){
		delete_request_o = new ActiveXObject("Microsoft.XMLHTTP");
	}else{
		delete_request_o = new XMLHttpRequest();
	}
	return delete_request_o; 
}
var http = createDeleteRequestObject(); 

function setDeleteCart(s_id,attributes_id,action,formactionValue)
{
if(confirm('Are you sure delete this item ?'))
{
http.open('get', 'process/get_delete_card.php?s_id='+ s_id+'&attributes_id='+ attributes_id+ '&action='+ action+'&formactionValue='+ formactionValue);
http.onreadystatechange=stateChanged 
http.send(null)
timeout_init();
}
}



function setDeleteCustomCart(category,subcategory,productno,pid,cpi,type,s_id,attributes_id,action,formactionValue)
{
http.open('get', 'process/get_delete_card_custom.php?category='+category+'&subcategory='+subcategory+'&productno='+productno+'&pid='+pid+'&cpi='+cpi+'&type='+type+'&s_id='+ s_id+'&attributes_id='+ attributes_id+ '&action='+ action+'&formactionValue='+ formactionValue);
http.onreadystatechange=stateCustomChanged; 
http.send(null);
//location.href="addtocart.php?category="+category+"&subcategory="+subcategory+"&productno="+productno+"&pid="+pid+"&cpi="+cpi+"&type="+type;
}

function stateCustomChanged() 
{ 
var url;
if (http.readyState==4)
 { 
 var response_delete = http.responseText;
// alert(http.responseText);
 url=jQuery.trim(http.responseText);
 location.href="addtocart.php?"+url;
 } 

}



var timeout;
function timeout_trigger() {
    document.getElementById('loading_text').innerHTML = '';
}

function timeout_init() {
    timeout = setTimeout('timeout_trigger()', 500);
    document.getElementById('loading_text').innerHTML = '<img style="vertical-align:middle" src="images/loading.gif">Please wait...';
}

function stateChanged() 
{ 
if (http.readyState==4)
 { 
 var response_delete = http.responseText;
// document.getElementById("txtHint1").innerHTML=http.responseText 
 var doLoading = '<img style="vertical-align:middle" src="images/loading.gif">Please wait...';
location.href="shoppingcart.php?action=delete";
 } 
}




function setUpdateCart(s_id,attributes_id,qtyField,action,formactionValue)
{
if(qtyField=="0" || qtyField=="")
{
alert('please enter quantity');
}
else
{
http.open('get', 'process/get_update_card.php?s_id='+ s_id+'&attributes_id='+ attributes_id+ '&action='+ action+'&qtyField='+ qtyField+'&formactionValue='+ formactionValue);
http.onreadystatechange=updateCardChanged 
http.send(null)
timeout_init();
}
}



function updateCardChanged() 
{ 
if(http.readyState==4 || http.readyState=="complete")
 { 
// alert(http.responseText);
 var response_update;
 var countitem='';
 var strmatch='';
 var doLoading = '<img style="vertical-align:middle" src="images/loading.gif">Please wait...';
 countitem=http.responseText;
 minqty=jQuery.trim(countitem);
 strmatch=minqty.length;
// alert(strmatch);
 if(strmatch=="2")
	{
	alert("Min quantity item must be : "+minqty+"\n");
	}
	else
	{
	 location.href="shoppingcart.php?action=update";
	}
 } 
}

function setDeleteAllCart()
{

if(confirm('Are you sure delete all items ?')) { 
http.open('get', 'process/get_delete_all_card.php');
http.onreadystatechange=deleteAllCardChanged 
http.send(null)
timeout_init();
}

}


function deleteAllCardChanged() 
{ 
if(http.readyState==4)
 { 
 var response_update = http.responseText;
 var doLoading = '<img style="vertical-align:middle" src="images/loading.gif">Please wait...';
// document.getElementById("txtHint3").innerHTML=http.responseText 
location.href="shoppingcart.php?action=all";
 } 
}

function chkqty(obj){
if(obj.value=="")
{
//alert('please enter quantity ')
}
else if(obj.value=="0")
{
//alert('please enter valid quantity')
}
else
{
obj.value = obj.value.replace(/[^0-9]/g,'') // numbers only
}
}


function setChangeShipMethod(shipping_method,shipping_rate)
{
document.cart.shipping_method.value=shipping_method;
document.cart.shipping_rate.value=shipping_rate;
http.open('get', 'process/get_shipping_rate.php?shipping_method='+ document.cart.shipping_method.value+'&shipping_rate='+ document.cart.shipping_rate.value);
http.onreadystatechange=updateShipMethodChanged 
http.send(null)
}


function updateShipMethodChanged() 
{ 
if(http.readyState==4)
 { 
 var response_update = http.responseText;
// alert(response_update);
document.getElementById("txttotal").innerHTML='$'+response_update;
document.getElementById("shipping_rate").innerHTML='$'+document.cart.shipping_rate.value;
document.getElementById("shipping_rate_hide").innerHTML='';
document.getElementById("totalcarthide").innerHTML='';
// document.getElementById("txtHint3").innerHTML=http.responseText 
 } 
}

function getGoogleCheckout()
{
//	document.cart.action="https://checkout.google.com/api/checkout/v2/checkoutForm/Merchant/752007410140373";
	document.cart.action="https://sandbox.google.com/checkout/api/checkout/v2/checkoutForm/Merchant/944399811928510";

	document.cart.method ="post";
	document.cart.submit();
}


