//////////////----------OnKeyPress Event code to restrinct Kayboard input starts----------//////////

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') { // Decimal point allowed

                        if (keychar == '.') {

                                    if (this.value.indexOf('.') == -1) { // Can only have a single decimal point

                                                return true;
                                    }
                        }
            } 
            return false;

}

//////////////----------OnKeyPress Event code to restrinct Kayboard input ends----------//////////

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";
			//alert ("please enter numeric only.");						
		}
	}
	
	return IsNumber;   
}//number validation

var xmlHttp;
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
	    }
	
	    var url="empty_cartdetails.php";
	    //url=url+"?movid="+str;
			
	    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")
		{
			//document.getElementById("empty").innerHTML="<div class='data'>There are no items in your shopping cart.</div>";
			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=="")
	{
			//document.getElementById("alt_qtnty"+id2).innerHTML="<div><span>Enter quantity.</span></div>";
			alert("Enter quantity.");
			document.getElementById("qtnty"+id2).select();
			return false;
	}
	if (document.getElementById("qtnty"+id2).value=="00" || document.getElementById("qtnty"+id2).value=="0")
	{
			//document.getElementById("alt_qtnty"+id2).innerHTML="<div><span>Enter Proper quantity.</span></div>";
			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")
		{	
			//document.getElementById("alt_qtnty"+id2).innerHTML="<div><span>Product quantity accepts numbers only.</span></div>";
			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
    }

    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;
	//alert(url);	
    xmlHttp1.onreadystatechange=stateChangedallupdqty
    xmlHttp1.open("GET",url,true)
    xmlHttp1.send(null)
}

function stateChangedallupdqty()
{
    if (xmlHttp1.readyState==4 || xmlHttp1.readyState=="complete")
    {
		str=xmlHttp1.responseText;			
		//alert(str);
		if(str=="")
		{
			location.href="../index.php";
		}
		
		var rmov=str.split('^');	
		pass_prod_info=rmov[1];
		details=rmov[0];
		//alert(details);
		var rmov1=details.split('~');	
		spprice=rmov1[0];		
		pid=parseInt(rmov1[1]);
		ival=parseInt(pid);		
		tempqty=rmov1[2];		
		totprice=rmov1[3];		
		tqty=rmov1[4];
		//tottransvalue=parseInt(rmov1[4]);		
		//tot_ship_price=rmov1[5];
		tot_ship_amt=rmov1[5];
		//alert(tot_ship_amt);
		//totalqty=parseInt(rmov[4]);
			
		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=tottransvalue;
		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";
		}
		
		
		//=============update total quantity in ccavenue hidden field==============
		if(document.cfrm)
		{
			document.cfrm.Amount.value=tot_ship_amt;
			document.cfrm.Merchant_Param.value=pass_prod_info;
		}
		//=============update total quantity in ccavenue hidden field==============
	}
}

var xmlHttp3;	
function updateqty_cart(id,id1,id2)
{
	var i;
	qty=document.getElementById("qtnty"+id2).value;	
	if (document.getElementById("qtnty"+id2).value=="")
	{
			//document.getElementById("alt_qtnty"+id2).innerHTML="<div><span>Enter quantity.</span></div>";
			alert("Enter quantity.");
			document.getElementById("qtnty"+id2).select();
			return false;
	}
	if (document.getElementById("qtnty"+id2).value=="00" || document.getElementById("qtnty"+id2).value=="0")
	{
			//document.getElementById("alt_qtnty"+id2).innerHTML="<div><span>Enter Proper quantity.</span></div>";
			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")
		{	
			//document.getElementById("alt_qtnty"+id2).innerHTML="<div><span>Product quantity accepts numbers only.</span></div>";
			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
    }

    var url="update_cartqty_viewcart.php";
    url=url+"?tempid="+id;
	url=url+"&qty="+qty;
	url=url+"&id2="+id2;
	//alert(url);	
    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;
		}
		
		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";
		}
		
		//=============update total quantity in ccavenue hidden field==============
		if(document.cfrm)
		{
			document.cfrm.Amount.value=tottransvalue;
			document.cfrm.Merchant_Param.value=pass_prod_info;
		}
		//=============update total quantity in ccavenue hidden field==============
	}
}


var xmlHttp2;	
function delprod(id)
{	
	var con=confirm("Are you sure you want to delete the product?");
	if(con)
	{
		xmlHttp2=GetXmlHttpObject()
	    if (xmlHttp2==null)
	    {
	        alert ("Browser does not support HTTP Request");
	        return
	    }
	
	    var url="dellistprod.php";
	    url=url+"?tempid="+id;
			
	    xmlHttp2.onreadystatechange=deletlistprod
	    xmlHttp2.open("GET",url,true)
	    xmlHttp2.send(null)
	}
}

function deletlistprod() 
{
    if (xmlHttp2.readyState==4 || xmlHttp2.readyState=="complete")
    { 
		//document.getElementById("empty").innerHTML=xmlHttp2.responseText;\
		location.href="view_cart.php";
	}
}

function GetXmlHttpObject()
{ 
     var objXMLHttp=null
     if (window.XMLHttpRequest)
     {
          objXMLHttp=new XMLHttpRequest()
     }
     else if (window.ActiveXObject)
     {
          objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
     }
     return objXMLHttp
}