﻿// JScript File
var httpRequest=false;

function makeRequestObject()
{
    var httpRequest;
    if(window.XMLHttpRequest)
    {
        httpRequest=new XMLHttpRequest();
    }
    else if(window.ActiveXObject)
    {
        try
        {
            httpRequest=new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch(e)
        {
            httpRequest=new ActiveXObject("Microsoft.XMLHTTP");
        }            
    }
    
    return httpRequest;
}

httpRequest = makeRequestObject();

function processRequestShippingCharge() {
    if (httpRequest == null) httpRequest = makeRequestObject();
    if (httpRequest.readyState < 4) {
        document.getElementById("dvshipcost").innerHTML = "<img src=\"http://www.spiritvoyage.com/images/commentloader.gif\" >";
        document.getElementById("dvgrandtot").innerHTML = "<img src=\"http://www.spiritvoyage.com/images/commentloader.gif\" >";

    }
    if (httpRequest.readyState == 4) {
        if (httpRequest.status == 200) {

            var response = httpRequest.responseText;
            var update = new Array();
            if (response.indexOf('#' != -1)) {
                update = response.split("#");

                if (update[0] != "" && update[0] != "0") {
                    if (update[0] == "N.A") {
                        document.getElementById("dvshipcost").innerHTML = update[0];
                    }
                    else {
                        document.getElementById("dvshipcost").innerHTML = "$" + Number(update[0]);
                    }
                    //Math.round(original*100)/100
                    if (update[1] == "N.A") {
                        document.getElementById("dvgrandtot").innerHTML = update[1];
                    }
                    else {
                        document.getElementById("dvgrandtot").innerHTML = "$" + Number(update[1]);
                    }
                    if (update.length > 1) //array contains name of product i.e not shipped outside US or N.A for single product
                        if (update[2] != null && update[2] != "") {
                            if (update[2] != "N.A") {
                                ////                                                 
                                var arItem = new Array();
                                arItem = update[2].toString().split(',');
                                var allItems = '';
                                for (var i = 0; i < arItem.length; i++) {
                                    allItems += '-' + arItem[i] + '\n';
                                }
                                if (arItem.length > 1) {
                                    alert('The following items are not available for international shipping. You will have to remove the items from the cart to proceed ahead with checkout.\n' + allItems);
                                } else {
                                    alert('The following item is not available for international shipping. You will have to remove the item from the cart to proceed ahead with checkout.\n' + allItems);
                                }
                                ////                                              
                            }
                        }
                }
                else {
                    document.getElementById("dvshipcost").innerHTML = "$0";
                    document.getElementById("dvgrandtot").innerHTML = document.getElementById("dvsubtot").innerHTML;
                }


            }
        }
    }

}

function GetShippingCharge(Pricelevel,subtotal)
{
    
   
    if(document.getElementById("ddlShipCountry")!=null)
    {
        if(document.getElementById("ddlShipCountry").value!="")
        {
           httpRequest.open("POST","http://www.spiritvoyage.com/ajaxGetShipChargeforCart.aspx",true);
           httpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
           httpRequest.onreadystatechange=processRequestShippingCharge;
           httpRequest.send("shipcountrycode="+document.getElementById("ddlShipCountry").value+"&pricelevel="+Pricelevel+"&subtotal="+subtotal+"&m="+Math.random());
        }
        else
        {
          document.getElementById("dvshipcost").innerHTML="$0";
          document.getElementById("dvgrandtot").innerHTML=document.getElementById("dvsubtot").innerHTML;
          alert("Please select one country.");
          return false;
        }
    }
    else
    {
          document.getElementById("dvshipcost").innerHTML="$0";
          document.getElementById("dvgrandtot").innerHTML=document.getElementById("dvsubtot").innerHTML;
          alert("Please select one country.");
          return false;
    }
}
