
var coupons="",cansubmit=-2,paymethod=-1,OrderNumber=-1,cashsave=0;


function countExpress(ship)
{
   
   var basket_counts=document.getElementById("Quantity").innerHTML;
   var obj=document.getElementById("shippingaddress");
   
	if(parseInt(basket_counts)==0)
	{
	  alert("Your shopping cart is empty!");
	  obj.selectedIndex=0;
	  return false;
	}

	var count=parseFloat(basket_counts)-1;
	if(count<0) count=0;
	var sqian=0;
	var handler=document.getElementById("hfSh").value.split(",");
	
	var shipbase=0;
	switch(ship)
	{
		case "1":  
		  shipbase=handler[0].split("-");
		  sqian=parseInt(shipbase[1])+count*6;
		  break;
		case "2": 
		  shipbase=handler[1].split("-");
		  sqian=parseInt(shipbase[1])+count*5;
		  break;
		case "3":  
		  shipbase=handler[2].split("-");
		  sqian=parseInt(shipbase[1])+count*5;
		  break;
		case "4":  
		  shipbase=handler[3].split("-");
		  sqian=parseInt(shipbase[1])+count*5;
		  break;
	}
	
	var sum_hws=document.getElementById("Subtotal").innerHTML.replace("$","");
	var cous = document.getElementById("Coupon").innerHTML.replace("$","");
	var sum_hw=parseFloat(sum_hws);
	var cou=parseFloat(cous);
	var totol_cash=sum_hw+sqian-cou;
	document.getElementById("Shipping1").innerHTML="$"+sqian;
	document.getElementById("FinalTotal").innerHTML="$"+totol_cash;	
	if(OrderNumber=-1) OrderNumber=createNumber(document.getElementById("hfMid").value);
	document.getElementById("Order").innerHTML=OrderNumber;	
	
}


function disabled(formId)
{
   
   var oEles = document.getElementById(formId).elements;
   for(var i=0;i<oEles.length;i++){
    if(oEles[i].type=="text"||oEles[i].type=="checkbox"||oEles[i].type=="select-one")
     oEles[i].disabled=true;
	 }
   document.getElementById("imgbutton").style.display="none";
   document.getElementById("coupons").disabled = true;
   document.getElementById("couponsbtn").disabled = true;
   
}
function payPro(pay)
{
    if(checkdata()){
        var msg="Click 'OK' to finalize your purchase and move forward to the checkout process. You will be redirected to the checkout page in a few seconds. \n\nClick 'Cancel' to continue purchasing or modify your purchase.";
	     if(pay==1)  msg="Click 'OK' to finalize your purchase and move forward to the checkout process. You will be redirected to the checkout page in a few seconds. \n\nClick 'Cancel' to continue purchasing or modify your purchase.";
	      if(paymethod==-1) {
		        if(cashsave==0){
	                if(!confirm(msg)){  
	                    return false;
	                }else{
	                alert("Your order has been successfully created. You are now being redirected to the payment page.");
	                cashsave=1;
	                return true;
	                }
		        }
	        return false; 
	    }
    }
}
function payProcess(pay)
{
	var basket_counts=document.getElementById("Quantity").innerHTML;
	var sadr=document.getElementById("shippingaddress").value;
	var sub_number=document.getElementById("Order").innerHTML;
	if(checkdata()){
	    if(parseInt(basket_counts)==0)
	    {
	      alert("Your shopping cart is empty. Please pick what you like from our website.");
    	  
	      return false;
	    }
	    if(sadr==""|| sadr=="Select your area")
        {
          alert("Please Select your area!");
    	
	      document.getElementById("shippingaddress").focus();
    	  
	      return false;
        }
	    if(sub_number=="")
	    {
	      document.getElementById("shippingaddress").options[0].selected=true;
    	  
	      alert("Please Select your area!");
	      document.getElementById("shippingaddress").focus();
    	  
	      return false;
	    }
	     var msg="Click 'OK' to finalize your purchase and move forward to the checkout process. You will be redirected to the checkout page in a few seconds. \n\nClick 'Cancel' to continue purchasing or modify your purchase.";
	     if(pay==1)  msg="Click 'OK' to finalize your purchase and move forward to the checkout process. You will be redirected to the checkout page in a few seconds. \n\nClick 'Cancel' to continue purchasing or modify your purchase.";
	     if(paymethod==-1) {
		        if(cashsave==0){
	                if(!confirm(msg))  return false;
	                alert("Your order has been successfully created. You are now being redirected to the payment page.");
	                
	                cashsave=1;
	                return true;
		        }
	        return false; 
	    }
    }else
        return false;
}

function newXMLHttpRequest() {
	var xmlreq = false;
	
	if (window.XMLHttpRequest) {
		xmlreq = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
    		
		try { 
			xmlreq = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e1) { 
			
			try {
				xmlreq = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e2) {
				
			} 
		}
 	}
   	return xmlreq;
} 


function cashSave(url){
    
	var sadr=document.getElementById("shippingaddress").value;
	
	if(sadr=="")
    {
      alert("Please Select your area!");
	
	  document.getElementById("shippingaddress").focus();
	
	  return false;
    }
	var req = newXMLHttpRequest();
	var sub_number=document.getElementById("Order").innerHTML;
	var basket_count=document.getElementById("Quantity").innerHTML;
	var sum_hw=document.getElementById("Subtotal").innerHTML.replace("$","");
	var sqian=document.getElementById("Shipping").innerHTML.replace("$","");
	var totol_cash=document.getElementById("FinalTotal").innerHTML.replace("$","");
	var shippment=document.getElementById("shippment").value;
	var coupon=document.getElementById("Coupon").innerHTML.replace("$","");

	url=url+"?sub_number="+sub_number+"&basket_count="+basket_count+"&sum_hw="+sum_hw+"&sqian="+sqian+"&totol_cash="+totol_cash+"&coupon="+coupon+"&coupons="+coupons+"&shippment="+shippment;
	
	
//	document._xclick.item_name.value=document.getElementById("Order").innerHTML;
	
	document.ship.sub_number.value=document.getElementById("Order").innerHTML;
	
	var cash=document.getElementById("FinalTotal").innerHTML.replace("$","");
	
//	document._xclick.amount.value=cash;	
	document.ship.amount.value=cash;	
	req.onreadystatechange = getHandler(req);
  	req.open("get", url, true);
   	req.send(null);
	
	
}
function getHandler(req) {
   
	return function () {
    
	if (req.readyState == 4 && req.status == 200) {
		
		
		cansubmit = req.responseText;
		
		
		
    	}
 	}
}

function createNumber(code)
{
	
	var myDate=new Date();
	var num=myDate.getFullYear();  
    num =num+""+fillString(myDate.getMonth()+1);   
    num=num+""+fillString(myDate.getDate()); 
	num=num+"-"+code+"-";
    num=num+""+fillString(myDate.getHours());   
    num=num+""+fillString(myDate.getMinutes());   
    num=num+""+fillString(myDate.getSeconds());  
	num=num+""+parseInt((Math.random()*899)+100); 
	return num;
}
function fillString(str)
{
  var newStr=str>=10?str:"0"+str;
  return newStr;
}
function checkCoupons(url){
    
	var req = newXMLHttpRequest();
	var couponId=document.getElementById("F_Num").value;
	var couponsPwd=document.getElementById("F_Pass").value;
	var cashs=document.getElementById("FinalTotal").innerHTML;
	var cashAry=cashs.split('$');
	var cash=cashAry[1];
	var quantity=document.getElementById("Quantity").innerHTML
	if(coupons.indexOf(couponId)>=0)
	{
	  alert("This coupon has been used!");
	  return;
	}
	url=url+"?F_Num="+couponId+"&F_Pass="+couponsPwd+"&cash="+cash+"&quantity="+quantity;
	
  	req.open("get", url, true);
  	
  	
  	req.send(null);
	
  	var callbackHandler = getReadyStateHandler(req);
  	req.onreadystatechange = callbackHandler;
}


function getReadyStateHandler(req) {
	return function () {
		
	if (req.readyState == 4) {
		if (req.status == 200) {
		
		var msg = req.responseText;
		
		switch(msg)
		{
			case "Not":alert("Please input your Coupon Number!");break;
			case "PwdErr":alert("Please input your Coupon Password!");break;
			case "Exceed":alert("Coupon value can`t exceed the shopping total!");break;
			case "No":alert("Sorry, you can only use as many coupons as your shopping quantity!");break;
			case "Used":alert("This coupon has been used!");break;
			case "Unactivated":alert("This coupon is unactivated!");break;
			case "Err":alert("This coupon doesn`t exist or wrong password, please do it again correctly!");break;
			default:
			if(coupons!="") coupons=coupons+",";
			coupons=coupons+"'"+document.getElementById("F_Num").value+"'";
			document.getElementById("F_Num").value="";
			document.getElementById("F_Pass").value="";
			var cashs=document.getElementById("FinalTotal").innerHTML.replace("$","");;
			var finally_cash=parseFloat(cashs)-parseFloat(msg);
			document.getElementById("FinalTotal").innerHTML="$"+finally_cash;
			var coups=document.getElementById("Coupon").innerHTML.replace("$","");
			var coupon=parseFloat(coups)+parseFloat(msg);
			document.getElementById("Coupon").innerHTML="$"+coupon;
		}
		} 
    	}
 	}
}
function showCouponsTemplate()
{
  shippingaddress=document.getElementById("shippingaddress").value;
  coupon=document.getElementById("coupons");
  if(shippingaddress=="" || shippingaddress=="Select your area")
  {
    alert("Please Select your area!");
	
	document.getElementById("shippingaddress").focus();
	coupon.checked=false;
	return;
  }
  
  if(coupon.checked){
    document.getElementById("couponsTd").style.display="";
  }
  else{
  document.getElementById("couponsTd").style.display="none";
  }
}