﻿
var httpRequest1 = false;
function makeobject() {

    var httpRequest1;
    if (window.XMLHttpRequest) {

        httpRequest1 = new XMLHttpRequest();
    }
    else if (window.ActiveXObject) {

        try {
            httpRequest1 = new ActiveXObject("Msxml2.XMLHTTP");

        }
        catch (e) {

            httpRequest1 = new ActiveXObject("Microsoft.XMLHTTP");
        }

    }
    return httpRequest1;

}

httpRequest1 = makeobject();

function processRequest() {

    if (httpRequest1 == null) httpRequest1 = makeobject();
    if (httpRequest1.readyState == 4) {

        if (httpRequest1.status == 200) {
            
            var response = httpRequest1.responseText;
            var update = new Array();
            if (response.indexOf('|') != -1) {
                update = response.split('|');

                if (update[0] == "fetchServerTime")
               {
                    if(update[1] != "NA") {
                        document.getElementById("srvrDt").value = update[1];
						var date1 = new Array();
                        date1 = update[1].split('/');
                        var year = date1[0];
                        var month = parseInt(date1[1]) - 1;
                        var dt = date1[2];
                        var selecteddate;
                        var nd1 = new Date();
						nd1.setFullYear(year, month, dt);
                        //nd1.setFullYear(2012,02,20);
                        //alert(nd1);
                        var cal1 = new Calendar({

                            disabled: function (date) {
                                //fetchservertime();
                                //alert(document.getElementById("srvrDt").value);
                                // var indiatime = document.getElementById("srvrDt").value;
                                //selecteddate = Calender.dateToInt(nd1);
                                var difference;
                                var day;
                                difference = nd1 - date;
                                day = Math.round(difference / (1000 * 60 * 60 * 24));
                               // if (nd1.getHours() < 16) {
                                    if (day <= 0) {
                                        return false;
                                    }
                                    else {
                                        return true;
                                    }
                               // }
                              /*  else {
                                    if (day <= 0) {
                                        return false;
                                    }
                                    else {
                                        return true;
                                    }
                                }*/
                            },
                            date:nd1,
                            inputField: "shipDate",
                            dateFormat: "%d %B %Y",
                            trigger: "btnPopCal",
                            bottomBar: false,
                            onSelect: function () {
                                fetchservertime();
                                var indiatime = document.getElementById("srvrDt").value;
                                var date1 = new Array();
                                date1 = indiatime.split('/');
                                var year = date1[0];
                                var month = parseInt(date1[1]) - 1;
                                var dt = date1[2];
                                var nd = new Date();
                                nd.setFullYear(year, month, dt);
                                //alert(d);
                                var date = Calendar.intToDate(this.selection.get());
                                showserenadeDate(date);
                                var days;
                                var diff1;
                                //                                                                                var diff2;
                                diff1 = date - nd;
                                days = Math.round(diff1 / (1000 * 60 * 60 * 24));
                                if (days == 0) {

                                   /* if (nd.getHours() >= 16) {
                                        alert("We are no longer taking orders for delivery on " + nd.toLocaleDateString() + ".  You are requested to select another date of delivery.");
                                        $("#shipDate").val('');
                                    }*/
                                }
                                this.hide();
								
                            },
                            onChange: function () {
                                //alert('changed');
                            }
                        });

                        //cal1.moveTo(nd1,true);   
                                                                
                    }
               }
            }
        }
    }

}


function showserenadeDate(ordDate) {
    //alert("hit :: " + ordDate.toDateString());
    $.ajax({
        url: "ajaxSubmitForm_New.aspx/showSerenades",
        type: "POST",
        contentType: "application/json; charset=UTF-8",
        data: "{ordDate:'" + ordDate.toDateString() + "'}",
        success: function (data) {
            if (data.d != "cancel")
                $.fancybox(data.d);
        }

    });
}
function closeFancy() {
    $.fancybox.close();
}


function processRequest_promo() {

    if (httpRequest1 == null) httpRequest1 = makeobject();
    if (httpRequest1.readyState == 4) {

        if (httpRequest1.status == 200) {

            var response = httpRequest1.responseText;
            var update = new Array();
            if (response.indexOf('|') != -1) {
                update = response.split('|');

                if (update[0] == "fetchServerTime") {
                    if (update[1] != "NA") {
                        document.getElementById("srvrDt").value = update[1];
                        var date1 = new Array();
                        date1 = update[1].split('/');
                        var year = date1[0];
                        var month = parseInt(date1[1]) - 1;
                        var dt = date1[2];
                        var selecteddate;
                        var nd1 = new Date();
                        nd1.setFullYear(year, month, dt);
                        //nd1.setFullYear(2012,02,20);
                        //alert(nd1);
                        var cal1 = new Calendar({

                            disabled: function (date) {
                                //fetchservertime();
                                //alert(document.getElementById("srvrDt").value);
                                // var indiatime = document.getElementById("srvrDt").value;
                                //selecteddate = Calender.dateToInt(nd1);
                              /*  var difference;
                                var day;
                                difference = nd1 - date;
                                day = Math.round(difference / (1000 * 60 * 60 * 24));
                                if (nd1.getHours() < 16) {
                                    if (day <= 0) {
                                        return false;
                                    }
                                    else {
                                        return true;
                                    }
                                }
                                else {
                                    if (day < 0) {
                                        return false;
                                    }
                                    else {
                                        return true;
                                    }
                                }*/
                            },
                            date: nd1,
                            inputField: "shipDate",
                            dateFormat: "%d %B %Y",
                            trigger: "btnPopCal",
                            bottomBar: false,
                            onSelect: function () {
                                //fetchservertime();
                              /*  var indiatime = document.getElementById("srvrDt").value;
                                var date1 = new Array();
                                date1 = indiatime.split('/');
                                var year = date1[0];
                                var month = parseInt(date1[1]) - 1;
                                var dt = date1[2];
                                var nd = new Date();
                                nd.setFullYear(year, month, dt);
                                //alert(d);
                                var date = Calendar.intToDate(this.selection.get());
                                var days;
                                var diff1;
                                //                                                                                var diff2;
                                diff1 = date - nd;
                                days = Math.round(diff1 / (1000 * 60 * 60 * 24));
                                if (days == 0) {

                                    if (nd.getHours() >= 16) {
                                        alert("We are no longer taking orders for delivery on " + nd.toLocaleDateString() + ".  You are requested to select another date of delivery.");
                                        $("#shipDate").val('');
                                    }
                                }*/
                                this.hide();

                            },
                            onChange: function () {
                                //alert('changed');
                            }
                        });

                        //cal1.moveTo(nd1,true);   

                    }
                }
            }
        }
    }

}
// Fetching servertime for calender purpose....
function fetchservertime() {

    httpRequest1.open("POST", "ajaxSubmitForm_New.aspx", true);
    httpRequest1.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    httpRequest1.onreadystatechange = processRequest;
    httpRequest1.send("mode=fetchServerTime");


}

function fetchservertime_promo() {

    httpRequest1.open("POST", "ajaxSubmitForm_New.aspx", true);
    httpRequest1.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    httpRequest1.onreadystatechange = processRequest_promo;
    httpRequest1.send("mode=fetchServerTime");


}

function AjaxCall(url, id) {
    var head = document.getElementsByTagName('head').item(0);
    var old = document.getElementById(id);
    if (old) {
        head.removeChild(old);
    }
    var s = document.createElement('script');
    s.src = prefix_url + url + '&random=' + Math.random();
    s.type = 'text/javascript';
    s.defer = true;
    s.id = id;
    void (head.appendChild(s));
}

// 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', "http://www.giftstoindia24x7.com/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', "http://www.giftstoindia24x7.com/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.");
    }
}



