var KEY_NULL = null;

var KEY_NONE = 0;

var KEY_BCKSPC = 8;

var KEY_TAB = 9;

var KEY_ENTER = 13;

var KEY_ESC = 27;

function numbersonly(e,e1) 
	{    
		var key;
 
		var keychar;
    if (window.event) {        key = window.event.keyCode;
    }
    else if (e) {        key = e.which;
    }
    else {        return true;
    }
    keychar = String.fromCharCode(key);
    // Control keys (no @#$% "magic numbers")
	if (        (key == KEY_NULL) ||         (key == KEY_NONE) ||         (key == KEY_BCKSPC) ||         (key == KEY_TAB) ||         (key == KEY_ENTER) ||         (key == KEY_ESC) ||         (("0123456789").indexOf(keychar) > -1)        ) {       return true;
   }
else if (e1 == 'dec') {        if (keychar == '.') {        if (this.value.indexOf('.') == -1) { return true;
            }
       }
    }
     return false;
}
function IsChar(sText){    var ValidChars = "0123456789";
    var IsNumber=true;
    var Char;
    for (i = 0;
 i < sText.length && IsNumber == true;
 i++)        {             Char = sText.charAt(i);
             if (ValidChars.indexOf(Char) == -1)             {                IsNumber = "false";
            
						            }
        }
    return IsNumber;
   }

function empty_cart(){	
	var con=confirm("Are you sure to remove this product(s) from your cart?");
    if(con)    
	{
		xmlHttp=GetXmlHttpObject()
		if(xmlHttp==null)
		{
			alert ("Browser does not support HTTP Request");
            return false;
        }
        var url="empty_cartdetails.php";
        xmlHttp.onreadystatechange=deletecart;
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
	}
}
function deletecart() {   if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")    {         del=xmlHttp.responseText;
	        if(del=="")        {            location.href="../index.php";
        }
        if(del=="true")        {
            location.href="view_cart.php";
        }
    }
}
var xmlHttp1;
	function updateqty(id,id1,id2,id3,id4){    var i;
    qty=document.getElementById("qtnty"+id2).value;
	    if (document.getElementById("qtnty"+id2).value=="")    {      
        alert("Enter quantity.");
        document.getElementById("qtnty"+id2).select();
        return false;
    }
    if (document.getElementById("qtnty"+id2).value=="00" || document.getElementById("qtnty"+id2).value=="0")    {       
        alert("Enter proper quantity.");
        document.getElementById("qtnty"+id2).select();
        return false;
		    }
else{        document.getElementById("alt_qtnty"+id2).innerHTML="";
       }
    if (document.getElementById("qtnty"+id2).value!="")    {        var mytext=document.getElementById("qtnty"+id2).value;
        var number=IsChar(mytext);
        if (number=="false"){	            
            alert("Product quantity accepts numbers only.");
				            document.frm.qtnty.focus();
            return false;
        }
    }
    xmlHttp1=GetXmlHttpObject() ;   if (xmlHttp1==null){        alert ("Browser does not support HTTP Request");
        return false;
    }
    var url="update_cartqty.php";
    url=url+"?tempid="+id;
    url=url+"&qty="+qty;
    url=url+"&id2="+id2;
    url=url+"&cartid="+id3;
    url=url+"&shipid="+id4;

	    xmlHttp1.onreadystatechange=stateChangedallupdqty;    xmlHttp1.open("GET",url,true);    xmlHttp1.send(null)}
function stateChangedallupdqty(){    if (xmlHttp1.readyState==4 || xmlHttp1.readyState=="complete")    {        str=xmlHttp1.responseText;
			        
        if(str=="")        {            location.href="../index.php";
        }
		        var rmov=str.split('^');
	        pass_prod_info=rmov[1];
        details=rmov[0];
        
        var rmov1=details.split('~');
	        spprice=rmov1[0];
		        pid=parseInt(rmov1[1]);
        ival=parseInt(pid);
		        tempqty=rmov1[2];
		        totprice=rmov1[3];
		        tqty=rmov1[4];
                tot_ship_amt=rmov1[5];
        
			        if(document.getElementById("qty1_"+pid))        {            document.getElementById("qty1_"+pid+"").style.display="inline";
		            document.getElementById("qty2_"+pid+"").innerHTML=tempqty;
        }
		        if(document.getElementById("qty12_"+pid))        {            document.getElementById("qty12_"+pid+"").style.display="inline";
	            document.getElementById("qty121_"+pid+"").innerHTML=tempqty;
								        }
		        if(document.getElementById("totprice"+pid+""))        {            document.getElementById("totprice"+pid+"").innerHTML=totprice;
        }
				        if(document.getElementById("totalshipprice"+pid+""))        {            document.getElementById("totalshipprice"+pid+"").innerHTML=spprice;
        }
		        document.getElementById("totalqty").innerHTML=tqty;
        
        document.getElementById("totalvalue").innerHTML=tot_ship_amt;
				        if(document.getElementById("qty"+pid+""))        {			            document.getElementById("qty"+pid+"").style.display = "none";
        }
        if(document.getElementById("hidemode1"+pid+""))        {            document.getElementById("hidemode1"+pid+"").style.display = "inline";
        }
        if(document.getElementById("activemode1"+pid+""))        {            document.getElementById("activemode1"+pid+"").style.display = "none";
        }
    if(document.cfrm)        {            document.cfrm.Amount.value=tot_ship_amt;
            document.cfrm.Merchant_Param.value=pass_prod_info;
        }
   }
}
var xmlHttp3;
	function updateqty_cart(id,id1,id2){    	//alert ('inside update QTY');

    qty=document.getElementById("qtnty"+id2).value;
	    if (document.getElementById("qtnty"+id2).value==""){        alert("Enter quantity.");
        document.getElementById("qtnty"+id2).select();
        return false;
    }
    if (document.getElementById("qtnty"+id2).value=="00" || document.getElementById("qtnty"+id2).value=="0"){        alert("Enter proper quantity.");
        document.getElementById("qtnty"+id2).select();
        return false;
		    }
    else{        document.getElementById("alt_qtnty"+id2).innerHTML="";
    }
    if (document.getElementById("qtnty"+id2).value!=""){        var mytext=document.getElementById("qtnty"+id2).value;
        var number=IsChar(mytext);
        if (number=="false"){	            alert("Product quantity accepts numbers only.");
				            document.frm.qtnty.focus();
            return false;
        }
    }
    xmlHttp3=GetXmlHttpObject();
    if (xmlHttp3==null)    {        alert ("Browser does not support HTTP Request");
        return  false;
    }
    var url="update_cartqty_viewcart.php";
    url=url+"?tempid="+id;
    url=url+"&qty="+qty;
    url=url+"&id2="+id2;
 
    xmlHttp3.onreadystatechange=stateChangedallupdqty_cart;    xmlHttp3.open("GET",url,true);    xmlHttp3.send(null)}
function stateChangedallupdqty_cart(){    if (xmlHttp3.readyState==4 || xmlHttp3.readyState=="complete"){        str=xmlHttp3.responseText;
			        //	console.log('DEBUG : ' + str);
        if(str==""){            location.href="../index.php";
        }
        var rmov=str.split('~');
	        tempqty=rmov[1];
		        pid=parseInt(rmov[0]);
        ival=parseInt(pid);
		        totprice=rmov[2];
		        tqty=rmov[3];
        tottransvalue=rmov[4];
		        pass_prod_info=rmov[5];
        tot_ship_price=rmov[6];
        if(document.getElementById("qty1_"+pid)){            document.getElementById("qty1_"+pid+"").style.display="inline";
            document.getElementById("qty2_"+pid+"").innerHTML=tempqty;
        }
        GetXmlHttpObject();
        if(document.getElementById("totprice"+pid+"")){            document.getElementById("totprice"+pid+"").innerHTML=totprice;
        }
        document.getElementById("totalqty").innerHTML=tqty;
        document.getElementById("totalvalue").innerHTML=tottransvalue;
        if(document.getElementById("qty"+pid+"")){			            document.getElementById("qty"+pid+"").style.display = "none";
        }
        if(document.getElementById("hidemode1"+pid+"")){            document.getElementById("hidemode1"+pid+"").style.display = "inline";
        }
        if(document.getElementById("activemode1"+pid+"")){           document.getElementById("activemode1"+pid+"").style.display = "none";
        }
    if(document.cfrm){            document.cfrm.Amount.value=tottransvalue;
            document.cfrm.Merchant_Param.value=pass_prod_info;
        }
 }
}
function delprod(id){	    var con=confirm("Are you sure you want to delete the product?");
    var xmlHttp2;
	    if(con){        xmlHttp2=GetXmlHttpObject();
        if (xmlHttp2==null){           alert ("Browser does not support HTTP Request");
           return false;
        }
else{        var url="dellistprod.php";
        url=url+"?tempid="+id;
        xmlHttp2.onreadystatechange=deletlistprod(xmlHttp2);
        xmlHttp2.open("GET",url,true);
        xmlHttp2.send(null);
        }
        return true;
    }
}
function deletlistprod(xmlHttp2) {    if (xmlHttp2.readyState==4 || xmlHttp2.readyState=="complete"){         
        location.href="view_cart.php";
   }
}
function GetXmlHttpObject(){    var objXMLHttp=false;
    if (window.XMLHttpRequest){        objXMLHttp=new XMLHttpRequest();
    }
else if (window.ActiveXObject){        objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    return objXMLHttp;
}
