function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function popupWindow2(url) {
  window.open(url,'popupWindow','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=0,resizable=no,copyhistory=no,width=400,height=200,screenX=150,screenY=150,top=150,left=150');
}

function popupWindow3(url) {
  window.open(url,'popupWindow','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,copyhistory=no,width=600,height=600,screenX=150,screenY=150,top=150,left=150');
}
function popupWindowLogin(url)
{
    window.open(url,'popupWindowLogin','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,copyhistory=no,width=400,height=300,screenX=150,screenY=150,top=150,left=150');
}
var is_cart = false;
function showShipping()
{
    if( is_cart ){
        if(document.newcheckout.chkshipping.checked)
            document.getElementById("shipping").style.display="none";
        else
            document.getElementById("shipping").style.display="inline";
    }
}

if (window.XMLHttpRequest) 
{
    req = new XMLHttpRequest();
} 
else if (window.ActiveXObject) 
{
    req = new ActiveXObject("Microsoft.XMLHTTP");
}

if (window.XMLHttpRequest) 
{
    reqCoupon = new XMLHttpRequest();
} 
else if (window.ActiveXObject) 
{
    reqCoupon = new ActiveXObject("Microsoft.XMLHTTP");
}


var   osCsid ; //session id

function remove(productid,price)
{
    req.open("POST", "newcheckout.php?action=update_product&cart_delete="+document.getElementById("cart_delete")+"&products_id="+ productid + osCsid, true);
    req.onreadystatechange = callback;
    req.send(null); 
}

function callback()
{
    if (req.readyState == 4 && req.status == 200)
    {
        var response=(req.responseText).split("&");
        if(response[0]=="RemovedSuccessfully")
        {
            var semitotal=response[3];

            //set total after removal
            document.getElementById("semitotal").innerHTML="Semi total = "+semitotal;
            document.getElementById("total").innerHTML="Total = "+semitotal;

            // hide removalproduct record
            document.getElementById(response[1]).style.display="none";
 
           //check whether cart is empty or not
            if(response[2]=="true")
            {
               document.getElementById("userCart").style.display="none";
               document.getElementById("emptyCart").style.display="inline";
            }
       }
       checkNC();
       //coupon();
    }
}

function pr(obj){
    s = "";
    for( v in obj){
        s = s + "<br>" + v;
    }
     document.getElementById('debug').innerHTML = s ;
}

function errshow(a,b,c){
    alert("Wiantech Test:: Javascript Erro \n" + a + "\n" + b + "\n Line No:" + c);
}

var tempQuantity;
//var originalQuantity; if want to set original quantity
function changeQuantity(productid,quan)
{
  // try{ //originalQuantity=quan;
    tempQuantity="quantity"+productid;
    /*if(document.getElementById(tempQuantity).value=="" || document.getElementById(tempQuantity).value==0)
    {
        alert("Qunatity should be required");
        document.getElementById(tempQuantity).value=quan;
    }
    else
    {*/
    //if( 0 != req.status ){        req.abort();            }
    //alert( req.status  );
    req.open("POST", "newcheckout.php?action=update_product&cart_quantity="+document.getElementById(tempQuantity).value+"&products_id="+ productid + osCsid, true);
    req.onreadystatechange = setquantity;
    req.send(null);
//}catch(e){}
    //}
}

function setquantity()
{
    if (req.readyState == 4 && req.status == 200)
    {
        var response=(req.responseText).split("&");
        var tempPrice="price"+response[1];
        var tempunitPrice="unitprice"+response[1];

        if(response[0]=="ChangedSuccessfully")
        {
            var unitPrice=document.getElementById(tempunitPrice).innerHTML;
            var arrPrice=unitPrice.split("$");
            var subtotal=Math.round((document.getElementById(tempQuantity).value*arrPrice[1])*100)/100;

            var zerosNeeded = 2 -  String(subtotal).split(".")[1].length; //number of zeros to add
            var temp="";
            for (var i=1; i <= zerosNeeded; i++) {
                temp += "0";
            }
            //set price and semitotal according to quantity
            document.getElementById(tempPrice).innerHTML="$"+subtotal+temp;
            document.getElementById("semitotal").innerHTML="Semi total = "+response[2];

            //set total according to quantity
            document.getElementById("total").innerHTML="Total = "+response[2]; 

            //hide error span
            document.getElementById("error").style.display="none";
        }
        else if(response[0]=="Error")
        {
            //if want to set original quantity
            //document.getElementById(tempQuantity).value=originalQuantity;
            document.getElementById("error").style.display="inline";
            document.getElementById("error").innerHTML=response[1];
            //document.getElementById(response[2]).innerHTML="*** "+document.getElementById(response[2]).innerHTML;
        }
        checkNC();
        //coupon();
    }
}
// If zone in NC North Carolina then 7% tax of total
function checkNC()
{
    if( is_cart ){
        if(document.getElementById("zone").value!='44')
        {
            var subtotal=(document.getElementById("semitotal").innerHTML).split("=")[1];
            document.getElementById("tax").innerHTML="Tax = $0.00";
            document.getElementById("total").innerHTML="Total = "+subtotal;
        }
        coupon();
    }
}

function checkShip()
{
    if(document.getElementById("chkshipping").checked == false)
    {
        if(document.getElementById("shipfname").value=="")
        {
            alert("First name in shipping address must be at least 2 characters long");
            document.getElementById("shipfname").focus();
            return false;
        }
        if(document.getElementById("shiplname").value=="")
        {
            alert("Last name in shipping address must be at least 2 characters long");
            document.getElementById("shiplname").focus();
            return false;
        }
        if(document.getElementById("shipstreetadd").value=="")
        {
            alert("Street address in shipping address must be at least 5 characters long");
            document.getElementById("shiplname").focus();
            return false;
        }
        if(document.getElementById("shipcity").value=="")
        {
            alert("City in shipping address must be at least 3 characters long");
            document.getElementById("shipcity").focus();
            return false;
        }
        if(document.getElementById("shipzone").value=="select")
        {
            alert("Select state for shipping address");
            document.getElementById("shipzone").focus();
            return false;
        }
        if(document.getElementById("shipzip").value=="")
        {
            alert("Zip/Post code in shipping address must be at least 4 characters long");
            document.getElementById("shipzip").focus();
            return false;
        }
    }
  /*  if(document.getElementById("telephone").value!="")
    {
        if(!validateUSPhone(document.getElementById("telephone").value))
        {
            alert("Telephone no is not in US format");
            document.getElementById("telephone").focus();
            return false;
        }
    }
    if(!validateUSZip(document.getElementById("zipcode").value))
    {
        alert("Zip is not in US format");
        document.getElementById("zipcode").focus();
        return false;
    }*/
    return validateForm(document.getElementById("newcheckout"),false,false,false,true,8);
}

function validateUSPhone( strValue ) {
/************************************************
DESCRIPTION: Validates that a string contains valid
  US phone pattern.
  Ex. (999) 999-9999 or (999)999-9999

PARAMETERS:
   strValue - String to be tested for validity

RETURNS:
   True if valid, otherwise false.
*************************************************/
  var objRegExp  = /^\([1-9]\d{2}\)\s?\d{3}\-\d{4}$/;

  //check for valid us phone with or without space between
  //area code
  return objRegExp.test(strValue);
}

function validateUSZip( strValue ) {
/************************************************
DESCRIPTION: Validates that a string a United
  States zip code in 5 digit format or zip+4
  format. 99999 or 99999-9999

PARAMETERS:
   strValue - String to be tested for validity

RETURNS:
   True if valid, otherwise false.

*************************************************/
var objRegExp  = /(^\d{5}$)|(^\d{5}-\d{4}$)/;

  //check for valid US Zipcode
  return objRegExp.test(strValue);
}


/*********** This script will format a telephone number entered into a text box. Numbers can be entered as 1234567890 and will automatically be formatted as (123)456-7890.**********/

// Begin
/*var n;
var p;
var p1;
function ValidatePhone(){
p=p1.value
if(p.length==3){
    //d10=p.indexOf('(')
    pp=p;
    d4=p.indexOf('(')
    d5=p.indexOf(')')
    if(d4==-1){
        pp="("+pp;
    }
    if(d5==-1){
        pp=pp+")";
    }
    //pp="("+pp+")";
    document.frmPhone.txtphone.value="";
    document.frmPhone.txtphone.value=pp;
}
if(p.length>3){
    d1=p.indexOf('(')
    d2=p.indexOf(')')
    if (d2==-1){
        l30=p.length;
        p30=p.substring(0,4);
        //alert(p30);
        p30=p30+")"
        p31=p.substring(4,l30);
        pp=p30+p31;
        //alert(p31);
        document.frmPhone.txtphone.value="";
        document.frmPhone.txtphone.value=pp;
    }
    }
if(p.length>5){
    p11=p.substring(d1+1,d2);
    if(p11.length>3){
    p12=p11;
    l12=p12.length;
    l15=p.length
    //l12=l12-3
    p13=p11.substring(0,3);
    p14=p11.substring(3,l12);
    p15=p.substring(d2+1,l15);
    document.frmPhone.txtphone.value="";
    pp="("+p13+")"+p14+p15;
    document.frmPhone.txtphone.value=pp;
    //obj1.value="";
    //obj1.value=pp;
    }
    l16=p.length;
    p16=p.substring(d2+1,l16);
    l17=p16.length;
    if(l17>3&&p16.indexOf('-')==-1){
        p17=p.substring(d2+1,d2+4);
        p18=p.substring(d2+4,l16);
        p19=p.substring(0,d2+1);
        //alert(p19);
    pp=p19+p17+"-"+p18;
    document.frmPhone.txtphone.value="";
    document.frmPhone.txtphone.value=pp;
    //obj1.value="";
    //obj1.value=pp;
    }
}
//}
setTimeout(ValidatePhone,100)
}
function getIt(m){
n=m.name;
//p1=document.forms[0].elements[n]
p1=m
ValidatePhone()
}
function testphone(obj1){
p=obj1.value
//alert(p)
p=p.replace("(","")
p=p.replace(")","")
p=p.replace("-","")
p=p.replace("-","")
//alert(isNaN(p))
if (isNaN(p)==true){
alert("Check phone");
return false;
}
}*/
//  End -->

function coupon()
{
    if((document.getElementById("gv_redeem_code").value!="") || (document.getElementById("zone").value=="44"))
    {
        try
        {
            if( 0 != reqCoupon.status )
            {
                reqCoupon.abort();
            }
        }catch(e){}
        reqCoupon.open("POST", "newcheckout.php?coupon="+document.getElementById("gv_redeem_code").value+"&zone="+document.getElementById("zone").value+ osCsid, true);
        reqCoupon.onreadystatechange = couponResponse;
        reqCoupon.send(null); 
    }
    else
    {
        document.getElementById("discount").style.display="none";
        document.getElementById("coupon").innerHTML="";
    }
}
function couponResponse()
{
    if (reqCoupon.readyState == 4 && reqCoupon.status == 200)
    {
        var splitcoupon=(reqCoupon.responseText).split("=");
        if(splitcoupon[0]=="Error")
        {
            document.getElementById("coupon").innerHTML=splitcoupon[1];
            document.getElementById("discount").style.display="none";
            if(document.getElementById("zone").value=='44')
            {
                taxtotal=(document.getElementById("semitotal").innerHTML).split("$")[1];
                var tax=Math.round(taxtotal*0.07*100)/100;
                var zerosNeeded = 2 -  String(tax).split(".")[1].length; //number of zeros to add
                    var temp="";
                    for (var i=1; i <= zerosNeeded; i++) {
                        temp += "0";
                    }
                    document.getElementById("tax").innerHTML="Tax = $"+tax+temp;
                    document.getElementById("total").innerHTML="Total = $"+Math.round((taxtotal-tax)*100)/100;
            }
        }
        else
        {
            document.getElementById("coupon").innerHTML="";
            //get discount details
            var discount=(reqCoupon.responseText).split("Discount=");
            if(discount.length>1)
            {
                //alert(discount[1]);
                var arrdis=(discount[1]).split(":");
                if(arrdis.length>4)
                {
                    document.getElementById("discount").style.display="inline";
                    document.getElementById("discount").innerHTML="Discount = "+arrdis[1];

                    if(arrdis[4]>0)
                    {
                        var tax=Math.round(arrdis[4]*100)/100;
                        var zerosNeeded = 2 -  String(tax).split(".")[1].length; //number of zeros to add
                        var temp="";
                        for (var i=1; i <= zerosNeeded; i++) {
                            temp += "0";
                        }
                        document.getElementById("tax").innerHTML="Tax = $"+tax+temp;
                    }
                    else
                        document.getElementById("tax").innerHTML="Tax = $0.00";

                    //set total
                    document.getElementById("total").innerHTML="Total = "+arrdis[2];
                    //shipping cost
                    if(arrdis[5]==0)
                        document.getElementById("shipping_cost").innerHTML="Shipping = $0.00";
                    else
                        document.getElementById("shipping_cost").innerHTML="Shipping = "+arrdis[5];
                }
                else
                {
                    var tax=Math.round(arrdis[1]*100)/100;
                    var zerosNeeded = 2 -  String(tax).split(".")[1].length; //number of zeros to add
                    var temp="";
                    for (var i=1; i <= zerosNeeded; i++) {
                        temp += "0";
                    }
                    document.getElementById("tax").innerHTML="Tax = $"+tax+temp;
                    document.getElementById("total").innerHTML="Total = $"+Math.round(arrdis[3]*100)/100;
                    document.getElementById("discount").style.display="none";
                    //shipping cost
                    if(arrdis[2]==0)
                        document.getElementById("shipping_cost").innerHTML="Shipping = $0.00";
                    else
                        document.getElementById("shipping_cost").innerHTML="Shipping = "+arrdis[2];
                }
            }
        }
    }
}