﻿//AjaxHttpRequest.js
function MM_reloadPage(init) //reloads the window if Nav4 resized
{
    if (init == true) with (navigator) {
        if ((appName == "Netscape") && (parseInt(appVersion) == 4)) {
            document.MM_pgW = innerWidth;
            document.MM_pgH = innerHeight;
            onresize = MM_reloadPage;
        }
    }
    else if (innerWidth != document.MM_pgW || innerHeight != document.MM_pgH) location.reload();
}
MM_reloadPage(true);

function createRequestObject() {
    var objRequest;
    var browser = navigator.appName;
    if (browser == "Microsoft Internet Explorer") {
        objRequest = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else {
        objRequest = new XMLHttpRequest();
    }
    return objRequest;
}

var http = createRequestObject();

function handleResponse() {
    if (http.readyState == 4) {
        var response = http.responseText;
        document.getElementById("dvStateChange").style.display = "none";
        //document.getElementById("dvProductImage").style.display="block";
        //document.getElementById("dvColor").style.display="block";
        //document.getElementById("dvProdDescription").style.display="block";
        //document.getElementById("dvPrice").style.display="block";
        document.getElementById("dvMain").style.display = "block";
        //alert(http.responseText);
        var update = new Array();
        if (response.indexOf('|' != -1)) {
            update = response.split("|");
            if (update[0].toString != "1") {
                if (update[1].toString != "Color") {
                    document.getElementById("dvProdId").innerHTML = update[2];
                    document.getElementById("dvColor").innerHTML = update[3];
                    document.getElementById("dvProductImage").innerHTML = update[4];
                    document.getElementById("dvProdDescription").innerHTML = update[5];
                    //document.getElementById("ProductPrice").innerHTML ="Rs. " + update[2] + " / " + CurrecyUnit[1] + " " +  (collDivs[i].title/CurrecyUnit[2]).toFixed(2);
                    document.getElementById("dvPrice").title = update[6];
                }
            }
            else if (update[0].toString != "0") {
                document.getElementById("lblError").innerHTML = "Error!";
            }
            else if (update[0].toString != "0") {
                document.getElementById("lblError").innerHTML = "No data found...Try again...";
            }
        }

    }
    else {
        document.getElementById("dvStateChange").style.display = "block";
        //document.getElementById("dvProductImage").style.display="none";
        //document.getElementById("dvColor").style.display="none";
        //document.getElementById("dvProdDescription").style.display="none";
        //document.getElementById("dvPrice").style.display="none";
        document.getElementById("dvMain").style.display = "none";

    }

}

///////////////// Send to function on change of the color dropdown
function CheckForColorData(theOpt) {
    //alert("Ajax_BigProduct.aspx?ProdId=" + theOpt + "&Mode=Color&sId=" + Math.random());
    http.open("get", "http://www.giftstoindia24x7.com/Ajax_BigProduct.aspx?ProdId=" + theOpt + "&Mode=Color&sId=" + Math.random());
    http.onreadystatechange = handleResponse;
    http.send(null);
}


function fetchProdColor(ddlObj, categoryId) {
    var objErrLabel = document.getElementById("lblError");
    var objMainDiv = document.getElementById("productDetail");
    //alert(objErrLabel + " : " + objMainDiv);
    var varProdId = document.getElementById("prodId");
    var varCatId = document.getElementById("catId");
    if (objErrLabel != null && objMainDiv != null && varProdId != null && varCatId != null) {
        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/ajaxCommonFunction.aspx", true);
        self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        self.xmlHttpReq.onreadystatechange = function () {
            if (self.xmlHttpReq.readyState == 4) {
                ddlObj.disabled = false;
                document.getElementById("dvAjaxPic").style.display = "none";
                //objMainDiv.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();
                        //objMainDiv.innerHTML="";  
                    }
                    else {
                        varProdId.value = GetValue[1].toString();
                        varCatId.value = categoryId;
                        document.getElementById("dvColor").innerHTML = GetValue[2].toString();
                        document.getElementById("dvProductImage").innerHTML = GetValue[3].toString();
                        document.getElementById("dvProdDescription").innerHTML = GetValue[4].toString();
                        document.getElementById("dvPrice").innerHTML = GetValue[5].toString();

                        objErrLabel.innerHTML = "";
                    }
                }
                else {
                    alert("Ajax return not found...");
                }
            }
            else {
                ddlObj.disabled = true;
                document.getElementById("dvAjaxPic").style.display = "block";
                //objMainDiv.style.display="none";
            }
        }
        self.xmlHttpReq.send("mode=2&prodId=" + ddlObj.value + "&catId=" + categoryId);
    }
    else {
        alert("Objects not found. Try again.");
    }
}
function IsNumeric(e) {
    // Calling procedure
    // onKeyPress=\"javascript:return IsNumeric(event);\" 
    //
    //alert(e);
    var KeyID = (window.event) ? event.keyCode : e.which;
    if ((KeyID >= 66 && KeyID <= 90) || (KeyID >= 97 && KeyID <= 122) || (KeyID >= 33 && KeyID <= 47) ||
	   (KeyID >= 58 && KeyID <= 64) || (KeyID >= 91 && KeyID <= 96) || (KeyID >= 123 && KeyID <= 126)) {
        return false;
    }
    return true;
}

//======================================Fuction for Related Product on may 2011 by sanjib ========================================//

function FetchRelatedProduct(ddlObj, categoryId) {

    var objErrLabel = document.getElementById("lblError");
    var objMainDiv = document.getElementById("productDetail");
    //alert(objErrLabel + " : " + objMainDiv);
    var varProdId = document.getElementById("prodId");
    var varCatId = document.getElementById("catId");
    if (objErrLabel != null && objMainDiv != null && varProdId != null && varCatId != null) {
        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', "ajaxCommonFunction.aspx", true);
        self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        self.xmlHttpReq.onreadystatechange = function () {
            if (self.xmlHttpReq.readyState == 4) {
                ddlObj.disabled = false;
                document.getElementById("dvAjaxPic").style.display = "none";
                //objMainDiv.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();
                        //window.location.href = "#proid=" + GetValue[1].toString() + "&CatId=" + categoryId;
                    }
                    else {
                        varProdId.value = GetValue[1].toString();
                        varCatId.value = categoryId;
                        //document.getElementById("dvRelatedPrdct").innerHTML = GetValue[2].toString();
                        document.getElementById("dvProductImage").innerHTML = GetValue[3].toString();
                        document.getElementById("dvProdDescription").innerHTML = GetValue[4].toString();
                        document.getElementById("dvPrice").innerHTML = GetValue[5].toString();
                        document.getElementById("spnCartName").innerHTML = GetValue[6].toString();
                        objErrLabel.innerHTML = "";
                        //                        var url = window.location.href;
                        //                        var newURL = url.split("?")[0] + "?" + "proid=" + GetValue[1].toString() + "&CatId=" + categoryId;
                        //                        window.history.pushState("OBJ", "TITLE", newURL);
                        window.location.href = "#proid=" + GetValue[1].toString() + "&CatId=" + categoryId;

                    }
                }
                else {
                    alert("Ajax return not found...");
                }
            }
            else {
                ddlObj.disabled = true;
                document.getElementById("dvAjaxPic").style.display = "block";
                //objMainDiv.style.display="none";
            }
        }
        self.xmlHttpReq.send("mode=3&prodId=" + ddlObj.value + "&catId=" + categoryId);
    }
    else {
        alert("Objects not found. Try again.");
    }


}

//==========================test======================================
function FetchRelatedProductTest(ddlObj, categoryId) {
    var prodId = ddlObj.value;

    var objErrLabel = document.getElementById("lblError");
    var objMainDiv = document.getElementById("productDetail");
    //alert(objErrLabel + " : " + objMainDiv);
    var varProdId = document.getElementById("prodId");
    //var existingProdID = ddlObj.value;
    //alert(existingProdID);
    var varCatId = document.getElementById("catId");
    if (objErrLabel != null && objMainDiv != null && varProdId != null && varCatId != null) {
        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', "ajaxCommonFunction.aspx", true);
        self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        self.xmlHttpReq.onreadystatechange = function () {
            if (self.xmlHttpReq.readyState == 4) {
                ddlObj.disabled = false;
                document.getElementById("dvAjaxPic").style.display = "none";
                //objMainDiv.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();
                        //objMainDiv.innerHTML="";  
                    }
                    else {
                        varProdId.value = GetValue[1].toString();
                        varCatId.value = categoryId;
                        //document.getElementById("dvRelatedPrdct").innerHTML = GetValue[2].toString();
                        document.getElementById("dvProductImage").innerHTML = GetValue[3].toString();
                        document.getElementById("dvProdDescription").innerHTML = GetValue[4].toString();
                        document.getElementById("dvPrice").innerHTML = GetValue[5].toString();
                        document.getElementById("spnCartName").innerHTML = GetValue[6].toString();
                        objErrLabel.innerHTML = "";
                        //var url = window.location.href;
                        //var newURL = url.split("?")[0] + "?" + "proid=" + GetValue[1].toString() + "&CatId=" + categoryId;
                        //window.history.pushState("OBJ", "TITLE", newURL);
                        window.location.href = "#proid=" + GetValue[1].toString() + "&CatId=" + categoryId;
                        //$("[id$='selctRE']").val(""+indexVal+" index value");
                        //debugger:
                        //var reLIs = document.getElementsByName('liReProd');
                        var reLIs = $("li[name=liReProd]");
                        var liNos = 0;
                        var strtIndx = 0;
                        var liLen = reLIs.length;
                        for (var i = 0; i < liLen; i++) {
                            //alert(i);
                            if (reLIs[i].style.display == "none") {
                                reLIs[i].style.display = "";
                                //alert(reLIs[i].id + "  shown");
                            }

                        }

                        //document.getElementById('liReProd' + prodId).style.display = "none";
                        $('#liReProd' + prodId).hide();
                        //alert('liReProd' + prodId + " hide");

                        for (i = 0; i < liLen; i++) {
                            if (reLIs[i].style.display == "block" || reLIs[i].style.display == "")
                                strtIndx++;
                            //alert(strtIndx);
                        }
                        var j = 0;
                        if (strtIndx > 5) {
                            for (i = liLen - 1; i > 0; i--) {
                                if (reLIs[i].style.display == "" || reLIs[i].style.display == "block") {
                                    j++;
                                    reLIs[i].style.display = "none";
                                    //alert(i + "  last hide");
                                    if (j == (strtIndx - 5))
                                        break;
                                }
                            }
                        }
                    }
                }
                else {
                    alert("Ajax return not found...");
                }
            }
            else {
                ddlObj.disabled = true;
                document.getElementById("dvAjaxPic").style.display = "block";
                //objMainDiv.style.display="none";
            }
        }
        self.xmlHttpReq.send("mode=3&prodId=" + prodId + "&catId=" + categoryId);
    }
    else {
        alert("Objects not found. Try again.");
    }


}
//==========================test======================================


//====================================== Function for Related Product on may 2011 by sanjib ==========================================//

//==========================test======================================
function create_ajax() {
        if (window.XMLHttpRequest) {
            xmlhttp = new XMLHttpRequest();
        }
        else {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }

    }
    function addto_cart(prodid, catid) {
        var qntity = document.getElementById("txtQuantity").value;
        if (qntity > 0 && prodid != "" && catid != "") {
            create_ajax();
            xmlhttp.open('POST', "ajaxCommonFunction.aspx", true);
            xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            //alert("ok");
            xmlhttp.send("mode=5&prodid=" + prodid + "&catid=" + catid + "&quntity=" + qntity + "&r" + Math.random() + "=" + Math.random());
            xmlhttp.onreadystatechange = function () {
                if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                    var status = xmlhttp.responseText;
                    if (status == "1") {
                        window.location = "cart.aspx";
                    }
                    else {
                        document.getElementById("lblError").value = status;
                        document.getElementById("lblError").style.display = "block";
                    }
                }
            }
        }
        return false;
    }
//==========================test======================================


    function show_details(prodid, catid) {
        var proddetails = document.getElementById("productDetail");
        var breadcramb = document.getElementById("spnCartName");
        if (prodid != "" && catid != "") {
            create_ajax();
            //alert("Ok");
            xmlhttp.open('POST', "ajaxCommonFunction.aspx", true);
            xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            //alert("ok");
            xmlhttp.send("mode=4&prodid=" + prodid + "&catid=" + catid + "&r" + Math.random() + "=" + Math.random());
            xmlhttp.onreadystatechange = function () {
                if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                    var status = xmlhttp.responseText;
                    if (status != "") {
                        var cartname = status.split("~|~");
                        breadcramb.innerHTML = cartname[0];
                        //alert(status);
                        proddetails.innerHTML = cartname[1];
	      window.location.href = "#proid=" + prodid + "&CatId=" +catid ;
                        return false;
                    }
                }
            }
        }
        return false;
    }
