function getQueryVariable(variable) {
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable) {
      return pair[1];
    }
  } 
  return "NOT_FOUND";
}

function toggleMenu(objID) {
	if (!document.getElementById) return;
	var ob = document.getElementById(objID).style;
	ob.display = (ob.display == 'block')?'none': 'block';
	if(ob.display=='none'){
		setCookie("activemenu",objID,1);
	}else{
		setCookie("activemenu","",1);
	}
	//alert(getCookie("activemenu"));
}

function setCookie(c_name,value,expiredays){
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}
function getCookie(c_name){
	if(document.cookie.length>0){
  		c_start=document.cookie.indexOf(c_name + "=");
  		if(c_start!=-1){ 
    		c_start=c_start + c_name.length+1; 
    		c_end=document.cookie.indexOf(";",c_start);
    		if(c_end==-1) c_end=document.cookie.length;
    		return unescape(document.cookie.substring(c_start,c_end));
    	} 
  	}
	return "";
}

function showProductDesc(ProductHTMLDesc){
	var inlinewin=dhtmlwindow.open("ProductDescription"+Math.random(), "inline", ProductHTMLDesc, "Product Description", "width=400px,height=220px,left=350px,top=500px,resize=1,scrolling=1");
	inlinewin.moveTo('middle', 'middle');
}
function init(){
	showBasket();
	initTip();
}
function xupdateBasket(ProductID){
	eval("document.getElementById('p"+ProductID+"').innerHTML=document.frmBasket.txt" + ProductID+".value * document.getElementById('c"+ProductID+"').innerHTML");
	//alert(a);
	//document.getElementById(ProductID).innerHTML=eval("document.all.frmBasket.txt" + ProductID+".value");
}
function updateBasket(){
  
  var numrows = document.getElementById('basket').rows.length - 1;  // don't count the header row!
  var i, qty, price, cost, totalcost = 0.00, productwt, totalwt=0;
  for (i = 1; i < numrows; i++) {
  
      // Compute total for each row
  
    qty = parseInt(document.getElementById('txt' + i).value);
    price = parseFloat(document.getElementById('cost' + i).innerHTML);
    unitwt = parseFloat(document.getElementById('unitwt' + i).innerHTML);
    if (!qty || !price || !unitwt){
      cost = 0.00;
      productwt=0;
      
    }else{
      cost = qty * price;
      productwt = qty * unitwt;
    }
    var total = document.getElementById('total' + i);
    total.innerHTML =  + cost;
    totalcost = totalcost + cost;  // Keep running grand total
    
    var totalprowt = document.getElementById('productwt' + i);
    totalprowt.innerHTML =  + productwt;
    totalwt = totalwt + productwt;  // Keep running grand total

  }
  var total = document.getElementById('total');
  total.innerHTML =  + totalcost;
  
  document.getElementById('totalwt').innerHTML= totalwt;
}
function updateCookie(){
	setCookie('basketHTML',document.getElementById('shopping_cart').innerHTML,1);
	/* setCookie('shopping_cart_div',shopping_cart_div); */
	setCookie('flyingDiv',flyingDiv);
	setCookie('currentProductDiv',currentProductDiv);
	setCookie('shopping_cart_x',shopping_cart_x);
	setCookie('shopping_cart_y',shopping_cart_y);
	setCookie('slide_xFactor',slide_xFactor);
	setCookie('slide_yFactor',slide_yFactor);
	setCookie('diffX',diffX);
	setCookie('diffY',diffY);
	setCookie('currentXPos',currentXPos);
	setCookie('currentYPos',currentYPos);
	setCookie('ajaxObjects',ajaxObjects);
}

function getXMLHTTPObject(){
		  	var xmlHttp;
  			try{
    			// Firefox, Opera 8.0+, Safari
    			xmlHttp=new XMLHttpRequest();
    		}catch (e){
    			// Internet Explorer
    			try{
      			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
     			 }catch (e){
      			try{
        				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        			}catch (e){
        				alert("Your browser does not support AJAX!");
        				return false;
        			}
      			}
    		}
    		return xmlHttp;
}		
function addToBasket(ProductID){	
	var xmlHttp = getXMLHTTPObject();
	xmlHttp.onreadystatechange=function(){
     		if(xmlHttp.readyState==4){
     			//alert("Hi"+xmlHttp.responseText);
			document.getElementById('shopping_cart').innerHTML=xmlHttp.responseText;						
		}
      	}
	xmlHttp.open("POST","addProduct.php",true);
    	var data;
    	data="productId=" + ProductID;
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", data.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(data);
}

function showBasket(){	
	var shoppingCartHTML = document.getElementById('shopping_cart');
	var xmlHttp = getXMLHTTPObject();
  	xmlHttp.onreadystatechange=function(){
     		if(xmlHttp.readyState==4){
			document.getElementById('shopping_cart').innerHTML=xmlHttp.responseText;						
		}
      	}
	xmlHttp.open("GET","gencart.php",true);
	xmlHttp.send(null);
}

function removeFromBasket(ProductID){	
				
	var shoppingCartHTML = document.getElementById('shopping_cart');
	var xmlHttp = getXMLHTTPObject();
  	xmlHttp.onreadystatechange=function(){
     		if(xmlHttp.readyState==4){
     			//alert("Hi"+xmlHttp.responseText);
			document.getElementById('shopping_cart').innerHTML=xmlHttp.responseText;						
		}
      	}
	xmlHttp.open("POST","removeProduct.php",true);
    	var data;
    	data="productId=" + ProductID;
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", data.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(data);
}

function createUser(){
	//---Data Validation Begin---
	if(document.getElementById('txtemail').value==""){
		alert("Username cannot be empty");
		document.getElementById('txtemail').focus();
		return false;
	}else if(document.getElementById('txtname').value==""){
		alert("Name cannot be empty");
		document.getElementById('txtname').focus();
		return false;
	}else if(document.getElementById('txtadd1').value+document.getElementById('txtadd2').value+document.getElementById('txtadd3').value==""){
		alert("Address cannot be empty");
		document.getElementById('txtadd1').focus();
		return false;
	}else if(document.getElementById('txtcity').value==""){
		alert("City cannot be empty");
		document.getElementById('txtcity').focus();
		return false;
	}else if(document.getElementById('txtstate').value==""){
		alert("State cannot be empty");
		document.getElementById('txtstate').focus();
		return false;
	}else if(document.getElementById('txtcountry').value==""){
		alert("Country cannot be empty");
		document.getElementById('txtcountry').focus();
		return false;
	}else if(document.getElementById('txtpin').value==""){
		alert("Pin cannot be empty");
		document.getElementById('txtpin').focus();
		return false;
	}else if(document.getElementById('txtphoneno').value==""){
		alert("Phone No cannot be empty");
		document.getElementById('txtphoneno').focus();
		return false;
	}
	//---Data Validation End---
		
	
	var xmlHttp = getXMLHTTPObject();
  	xmlHttp.onreadystatechange=function(){
   	if(xmlHttp.readyState==4){
   		var response=xmlHttp.responseText;
   		alert(response);
	}
   }
   xmlHttp.open("POST","regusr.php",true);
   var data;
   data="email=" + document.getElementById('txtemail').value;
   data+="&name="+ document.getElementById('txtname').value;
   data+="&add1="+ document.getElementById('txtadd1').value;
   data+="&add2="+ document.getElementById('txtadd2').value;
   data+="&add3="+ document.getElementById('txtadd3').value;
   data+="&city="+ document.getElementById('txtcity').value;
   data+="&pin="+ document.getElementById('txtpin').value;
   data+="&state="+ document.getElementById('txtstate').value;
   data+="&country="+ document.getElementById('txtcountry').value;
   data+="&phoneno="+ document.getElementById('txtphoneno').value;

   xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   xmlHttp.setRequestHeader("Content-length", data.length);
   xmlHttp.setRequestHeader("Connection", "close");
   xmlHttp.send(data);
}

function login(){
	
	//---Data Validation Begin---
	if(document.getElementById('email').value==""){
		alert("Username cannot be empty");
		document.getElementById('email').focus();
		return false;
	}else if(document.getElementById('passwd').value==""){
		alert("Password cannot be empty");
		document.getElementById('passwd').focus();
		return false;
	}
	//---Data Validation End---
	var xmlHttp = getXMLHTTPObject();
  	xmlHttp.onreadystatechange=function(){
   	if(xmlHttp.readyState==4){
   		var response=xmlHttp.responseText;
   		if(response=="+OK"){
   			//document.location="shipping.php";
   			if(getQueryVariable("jumpto")!="NOT_FOUND"){
   				document.location=getQueryVariable("jumpto");
   			}else{
   				document.location=document.referrer;
   			}
   		}else{
			alert(response);
		}
	}
   }
   xmlHttp.open("POST","login.php",true);
   var data;
   data="email=" + document.getElementById('email').value;
   data+="&passwd="+ document.getElementById('passwd').value;

   xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   xmlHttp.setRequestHeader("Content-length", data.length);
   xmlHttp.setRequestHeader("Connection", "close");
   xmlHttp.send(data);
}
function changePassword(){
	if(document.getElementById('oldpasswd').value==""){
		alert("You must provide old password.");
		document.getElementById('oldpasswd').focus();
		return false;
	}else if(document.getElementById('newpasswd').value==""){
		alert("You must provide new password.");
		document.getElementById('newpasswd').focus();
		return false;
	}else if(document.getElementById('repeatnewpasswd').value==""){
		alert("You must confirm new password.");
		document.getElementById('repeatnewpasswd').focus();
		return false;
	}else if(document.getElementById('newpasswd').value!=document.getElementById('repeatnewpasswd').value){
		alert("The new passwords provided by you are different.");
		document.getElementById('newpasswd').value="";
		document.getElementById('repeatnewpasswd').value="";
		document.getElementById('newpasswd').focus();
		return false;
	}
	
	var xmlHttp = getXMLHTTPObject();
  	xmlHttp.onreadystatechange=function(){
   	if(xmlHttp.readyState==4){
   		var response=xmlHttp.responseText;
   		if(response=="-WRONGOLDPASSWD"){
   			alert("The old password provided by you is wrong.");   			
   		}else if(response=="-NOUSER"){
   			alert("You must be logged in before you can change your password.");
   			
   		}else{
				alert(response);
			}
		}
   }
   xmlHttp.open("POST","changepasswd.php",true);
   var data;
   data="oldpasswd=" + document.getElementById('oldpasswd').value;
   data+="&newpasswd="+ document.getElementById('newpasswd').value;
   xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   xmlHttp.setRequestHeader("Content-length", data.length);
   xmlHttp.setRequestHeader("Connection", "close");
   xmlHttp.send(data);
	

}
function forgotPassword(){
	var email;
	email=prompt("Please enter your email?");

	var xmlHttp = getXMLHTTPObject();
  	xmlHttp.onreadystatechange=function(){
   	if(xmlHttp.readyState==4){
   		var response=xmlHttp.responseText;
			alert(response);
		}
   }
   xmlHttp.open("POST","forgotpasswd.php",true);
   var data;
   data="email=" + email;
 
   xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   xmlHttp.setRequestHeader("Content-length", data.length);
   xmlHttp.setRequestHeader("Connection", "close");
   xmlHttp.send(data);

}
function updateBasketOnServer(){
	var strXML = "<\?xml version='1.0' standalone='yes'\?>\n";
	strXML += "<orders>\n";
        var numrows = document.getElementById('basket').rows.length - 1;  // don't count the header row!
	for (i = 1; i < numrows; i++) {
		strXML += "\t<order>\n";
		strXML += "\t\t<productid>";
	 	strXML += document.getElementById('productid' + i).value;
	 	strXML += "</productid>\n";
		strXML += "\t\t<quantity>";
	 	strXML += document.getElementById('txt' + i).value;
	 	strXML += "</quantity>\n";


		strXML += "\t</order>\n";
	}

	strXML += "</orders>\n";
	//alert(strXML);
	var xmlHttp = getXMLHTTPObject();
  	xmlHttp.onreadystatechange=function(){
  		document.status="Updating basket...";
   	if(xmlHttp.readyState==4){
   		var response=xmlHttp.responseText;
   		document.status="Basket updated.";
   		//alert(response);

	}
   }
   xmlHttp.open("POST","updatebasket.php",true);
   var data;
   data="order=" + strXML;
   

   xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   xmlHttp.setRequestHeader("Content-length", data.length);
   xmlHttp.setRequestHeader("Connection", "close");
   xmlHttp.send(data);

}
var addressConfirmed=false;
var tariffRecieved=false;
function resetCourierInfoForm(){
	addressConfirmed=false;
	tariffRecieved=false;
	document.getElementById('courier').innerHTML="Please confirm the address.";
	document.getElementById('shippingcharges').innerHTML="Select Courier from above";
	document.getElementById('bill').disabled=true;
}
function getCourierInfo(){
	if(document.getElementById('txtadd1').value+document.getElementById('txtadd2').value+document.getElementById('txtadd3').value==""){
		alert("Address cannot be empty");
		document.getElementById('txtadd1').focus();
	}
	if(document.getElementById('txtcity').value==""){
		alert("City cannot be empty");
		document.getElementById('txtcity').focus();
		return;
	}

	if(document.getElementById('txtpin').value==""){
		alert("PIN/ZIP cannot be empty");
		document.getElementById('txtpin').focus();
		return;
	}

	if(document.getElementById('txtphone').value==""){
		alert("Phone cannot be empty");
		document.getElementById('txtphone').focus();
		return;
	}

	if(document.getElementById('txtcountry').value==""){
		alert("Country cannot be empty");
		document.getElementById('txtcountry').focus();
		return;
	}
	

	if(document.getElementById('couriers')){
		document.getElementById('couriers').selectedIndex=0;
		document.getElementById('shippingcharges').innerHTML="Select Courier from above";
		
	}
	var country=document.getElementById('txtcountry').value;
	var xmlHttp = getXMLHTTPObject();
  	xmlHttp.onreadystatechange=function(){
   	if(xmlHttp.readyState==4){
   		var response=xmlHttp.responseText;
   		if(response.substring(0,6)=="-FAIL:"){
   			alert(response.substring(6,response.length));
   		}else{
   			//alert(response);
				strCourierHTML = "<select id='couriers' onchange='getShippingCharges()'>";
				strCourierHTML = strCourierHTML+"<option value='0'> ---Select Courier--- </option>";
				strCourierHTML = strCourierHTML+response;
				strCourierHTML = strCourierHTML+"</select>";
   			document.getElementById('courier').innerHTML=response;
   			//alert(document.getElementById('courier').innerHTML)
   		}   			
   		
		}
   }
   xmlHttp.open("POST","courierinfo.php",true);
   var data;
   data="&add1="+ document.getElementById('txtadd1').value;
   data+="&add2="+ document.getElementById('txtadd2').value;
   data+="&add3="+ document.getElementById('txtadd3').value;
   data+="&city="+ document.getElementById('txtcity').value;
   data+="&pin="+ document.getElementById('txtpin').value;
   data+="&state="+ document.getElementById('txtstate').value;
   data+="&country="+ document.getElementById('txtcountry').value;
   data+="&phoneno="+ document.getElementById('txtphone').value;

   xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   xmlHttp.setRequestHeader("Content-length", data.length);
   xmlHttp.setRequestHeader("Connection", "close");
   xmlHttp.send(data);
	
}
function getShippingCharges(){
	if(document.getElementById('couriers').selectedIndex==0)return;
	var xmlHttp = getXMLHTTPObject();
  	xmlHttp.onreadystatechange=function(){
   	if(xmlHttp.readyState==4){
   		var response=xmlHttp.responseText;
   		document.getElementById('shippingcharges').innerHTML=response;
   		document.getElementById('bill').disabled=false;
		//showBill(response);
		}
   	}
   	xmlHttp.open("POST","shippingcharges.php",true);
   	var data;
   	data = "courierid=" + document.getElementById('couriers').options[document.getElementById('couriers').selectedIndex].value;
	data+= "&country="+document.getElementById('txtcountry').value;
		
	
//	alert(document.getElementById('couriers').options[document.getElementById('couriers').selectedIndex].value);   

   	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", data.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(data);

}
function showBill(shippingcharges){
	var shoppingCartHTML = document.getElementById('bill');
	var xmlHttp = getXMLHTTPObject();
  	xmlHttp.onreadystatechange=function(){
     		if(xmlHttp.readyState==4){
			document.getElementById('bill').innerHTML=xmlHttp.responseText;						
		}
      	}
	xmlHttp.open("POST","finalcart.php",true);
   	var data;
   	data = "shippingcharges=" + shippingcharges;
		
	
//	alert(document.getElementById('couriers').options[document.getElementById('couriers').selectedIndex].value);   

   	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", data.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(data);

}
function showOrder(){
	document.location="showorder.php?note="+document.getElementById('note').value;
}
function changeCurrency(){
	//alert(document.getElementById('currency').options[document.getElementById('currency').selectedIndex].value);
	var curr = document.getElementById('currency').options[document.getElementById('currency').selectedIndex].value;
	var xmlHttp = getXMLHTTPObject();
	xmlHttp.onreadystatechange=function(){
		if(xmlHttp.readyState==4){
			alert(xmlHttp.responseText);
			location.reload();
			//document.getElementById('shopping_cart').innerHTML=xmlHttp.responseText;						
		}
	}
	xmlHttp.open("POST","changedefcurr.php",true);
   	var data;
  	data="currency=" + curr;
   	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", data.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(data);
	
}
function getOrderDetails(){
	if(document.getElementById('orderid').selectedIndex==0){
		alert("Please select an order.");	
		return;
	}
	
	var xmlHttp = getXMLHTTPObject();
  	xmlHttp.onreadystatechange=function(){
   	if(xmlHttp.readyState==4){
   		var response=xmlHttp.responseText;
   		document.getElementById('orderdetails').innerHTML=xmlHttp.responseText;
   		//alert(response);
		}
   }
   xmlHttp.open("POST","orderdetails.php",true);
   var data;
   data = "orderid=" + document.getElementById('orderid').options[document.getElementById('orderid').selectedIndex].value;
	
	//alert(data);	
	
	//	alert(document.getElementById('couriers').options[document.getElementById('couriers').selectedIndex].value);   

   xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", data.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(data);

}
