﻿// Load billing states on selecting the country dropdown.
function loadBillingState(ddBillCountry)
{   
    //var objErrLabel=document.getElementById("lblError");
    var objBilling=document.getElementById("dvBillingState");
    var hdnBillCountryName=document.getElementById("hdnBillCountryName");
    if(ddBillCountry!=null && objBilling!=null && hdnBillCountryName!=null)
    {    
        var val=ddBillCountry.value;
        var ddIndex=ddBillCountry.selectedIndex;
        hdnBillCountryName.value=ddBillCountry.options[ddIndex].text;
        if(val!=null && val!="0")
        {            
            var xmlHttpReq = false;
            var self = this;
            if (window.XMLHttpRequest) 
            {
                self.xmlHttpReq = new XMLHttpRequest();        
            }
            else if (window.ActiveXObject) 
            {
                self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");         
            }
            self.xmlHttpReq.open('POST', "ajaxSubmitForm_New.aspx", true);             
            self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            self.xmlHttpReq.onreadystatechange = function() 
            {
                if (self.xmlHttpReq.readyState == 4) 
                {          
                    //document.getElementById("dvAjaxPic").style.display="none";
                    ddBillCountry.disabled=false;
                    objBilling.style.display="block";
                    var GetValue=new Array();
                    if(self.xmlHttpReq.responseText.indexOf('~' != -1))
                    {
                        GetValue=self.xmlHttpReq.responseText.split("~");
                    }
                    if(GetValue[0].toString()!="")
                    {
                        if(GetValue[0].toString()=="0")
                        {
                           // objErrLabel.innerHTML=GetValue[1].toString();                         
                            objBilling.innerHTML="";  
                        }
                        else
                        {
                            objBilling.innerHTML=GetValue[1].toString();
                           // objErrLabel.innerHTML=""; 
                        }
                    }
                    else
                    {
                        alert("Sorry! Cannot show the states now. Please refresh the page and try again.");
                    }
                }
                else
                {
                    ddBillCountry.disabled=true;
                    //document.getElementById("dvAjaxPic").style.display="block";
                    objBilling.style.display="none";
                }
            }                
            self.xmlHttpReq.send("mode=1&countryId="+val); 
        }
        else if(val=="0")
        {            
            objBilling.innerHTML="";
          //  objErrLabel.innerHTML="";
            alert("Select proper country.");
        }
        else
        {            
            objBilling.innerHTML="";
           // objErrLabel.innerHTML="";
            alert("Invalid parameter. Try again.");
        }
    }
    else
    {
        alert("Objects not found. Try again.");
    }
}

// Load the shipping city from selected state dropdown
function loadShippingCity(ddShipState)
{   
    
   // var objErrLabel=document.getElementById("lblError");
    var objShipping=document.getElementById("divShippingCity");
    var hdnShipStateName=document.getElementById("hdnShipStateName");
    if(ddShipState!=null && objShipping!=null && hdnShipStateName!=null)
    {    
        var val=ddShipState.value;
        var ddIndex=ddShipState.selectedIndex;
        hdnShipStateName.value=ddShipState.options[ddIndex].text;
        if(val!=null && val!="0")
        {
            var xmlHttpReq = false;
            var self = this;
            if (window.XMLHttpRequest) 
            {
                self.xmlHttpReq = new XMLHttpRequest();        
            }
            else if (window.ActiveXObject) 
            {
                self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");         
            }
            self.xmlHttpReq.open('POST', "ajaxSubmitForm_New.aspx", true);             
            self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            self.xmlHttpReq.onreadystatechange = function() 
            {
                if (self.xmlHttpReq.readyState == 4) 
                {          
                    //document.getElementById("dvAjaxPic").style.display="none";
                    ddShipState.disabled=false;
                    objShipping.style.display="block";
                    var GetValue=new Array();
                    if(self.xmlHttpReq.responseText.indexOf('~' != -1))
                    {
                        GetValue=self.xmlHttpReq.responseText.split("~");
                    }
                    if(GetValue[0].toString()!="")
                    {
                        if(GetValue[0].toString()=="0")
                        {
                           // objErrLabel.innerHTML=GetValue[1].toString();                         
                            objShipping.innerHTML="";  
                        }
                        else
                        {
                            objShipping.innerHTML=GetValue[1].toString();
                           // objErrLabel.innerHTML=""; 
                        }
                    }
                    else
                    {
                        alert("Sorry! Cannot show the cities now. Please refresh the page and try again.");
                    }
                }
                else
                {
                    ddShipState.disabled=true;
                    //document.getElementById("dvAjaxPic").style.display="block";
                    objShipping.style.display="none";
                }
            }                
            self.xmlHttpReq.send("mode=2&stateId="+val); 
        }
        else if(val=="0")
        {            
            objShipping.innerHTML="";
            //objErrLabel.innerHTML="";
            alert("Select proper state.");
        }
        else
        {            
            objShipping.innerHTML="";
            //objErrLabel.innerHTML="";
            alert("Invalid parameter. Try again.");
        }
    }
    else
    {
        alert("Objects not found. Try again.");
    }
}


