var jsddlwidth;
var jsddlmetal;
var jsddlweight;
var jsddlfinger;
var jsddlring;
var jslblprate;
var jsselfinger;
var jsproductid;
var AjaxServerPageName = 'productpergram_detailajax.aspx';
var ns6 = document.getElementById && !document.all;
var ie4 = document.all;
var ns4 = document.layers;
var ct = 0;

var strvalue;

function getObjectByID(Objid) {
    if (ns4) {
        return eval('document.' + Objid)
    }
    if (ns6) {
        return document.getElementById(Objid);
    }
    if (ie4) {
        return document.all(Objid);
    }
    return null
}


function SetText(objid, msg, fg, bg) {
    if (!fg) fg = "#ff0000";
    if (!bg) bg = "#FFFFFF";
    var content = msg;
    if (ns4) {
        objItm = eval('document.' + objid)
        objItm.document.write(content);
        objItm.document.close();
        objItm.visibility = "visible";
    }
    if (ns6) {
        document.getElementById(objid).innerHTML = content;
    }
    if (ie4) {
        document.all(objid).innerHTML = content;
    }
}

//Global XMLHTTP Request object
var XmlHttp;
var currentCntrl = 0


//Creating and setting the instance of appropriate XMLHTTP Request object to a “XmlHttp” variable
function CreateXmlHttp() {
    firstgo = 0
    //Creating object of XMLHTTP in IE
    try {
        XmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e) {
        try {
            XmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch (oc) {
            XmlHttp = null;
        }
    }
    //Creating object of XMLHTTP in Mozilla and Safari 
    if (!XmlHttp && typeof XMLHttpRequest != "undefined") {
        XmlHttp = new XMLHttpRequest();
    }

}


//Called when response comes back from server
function HandleResponse() {
    ////////////////////////////////////////////

    document.getElementById("divloadingPRINTING").innerHTML = "<img src='images/loader.gif' alt='loading' />"
    var windowHeight = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight;
    var h = window.pageYOffset || document.body.scrollTop || document.documentElement.scrollTop;
    var windowWidth = window.innerWidth ? window.innerWidth : document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body.clientWidth;
    var IpopTop = ((windowHeight - document.getElementById("divloadingPRINTING").offsetHeight) / 2);
    document.getElementById("divloadingPRINTING").style.top = (IpopTop + h) + 'px';
    document.getElementById("divloadingPRINTING").style.left = ((document.body.clientWidth / 2)) + 'px';
    document.getElementById("divloadingPRINTING").style.display = "block";

    //////////////////////////////////////////////


    // To make sure receiving response data from server is completed
    if (XmlHttp.readyState == 4) {
        // To make sure valid response is received from the server, 200 means response received is OK
        if (XmlHttp.status == 200) {
            ClearAndSetDdlItems(XmlHttp.responseXML.documentElement);
        }
        else {
            alert("There was a problem retrieving data from the server.");
        }
    }
}

//Clears the contents of state combo box and adds the states of currently selected country
function ClearAndSetDdlItems(RowNode) {
    var textValue;
    var optionItem;
    var val;
    var ddlList;
    if (currentCntrl == 0)
        ddlList = jsddlweight;
    else if (currentCntrl == 1)
        ddlList = jsddlwidth;
    else if (currentCntrl == 2)
        ddlList = jsddlfinger;
    else if (currentCntrl == 3)
        ddlList = jsddlring;
    else if (currentCntrl == 4) {
        var RowNodes = RowNode.getElementsByTagName('item');
        jslblprate.value = GetInnerText(RowNodes[0]);
        if (jsddlfinger) {
            jsselfinger.value = jsddlfinger.value;
        }

        if (jslblprate.value == 0) {
            SetText("_ctl0_ContentPlaceHolder1_lblprice", "POA");
        }
        else {
            SetText("_ctl0_ContentPlaceHolder1_lblprice", jslblprate.value);
        }

        document.getElementById("divloadingPRINTING").innerHTML = "";
        document.getElementById("divloadingPRINTING").style.display = "none";
        return;
    }

    if (ddlList != null) {
        if (ddlList.options.length) {
            ddlList.options.length = 0;
        }
    }

    var RowNodes = RowNode.getElementsByTagName('item');
    //[document.getElementById("ddlweighdimond").selectedIndex].text
    //Add new states list to the state combo box.
    
    var mytool_array = strvalue.split(",");
    var valueindex;
    
    for (var count = 0; count < RowNodes.length; count++) {
        textValue = GetInnerText(RowNodes[count]);
        var value1 = RowNodes[count].getAttribute('value');
        optionItem = new Option(textValue, value1, false, false);
        ddlList.options[ddlList.length] = optionItem;
        if (currentCntrl == 0) 
        {
            if (mytool_array[1] == value1) 
            {
                valueindex = count;
            }
        }
        else if (currentCntrl == 1) 
        {
            if (mytool_array[2] == value1) 
            {
                valueindex = count;
            }
        }
        else if (currentCntrl == 2) 
        {
            if (mytool_array[3] == value1) 
            {
               valueindex=count;
            }
        }
        else if (currentCntrl == 3) 
        {
            if (mytool_array[4] == value1) {
                valueindex = count;
            }
        }
    }
    //alert(ddlList[ddlList.selectedIndex].text);
    if (currentCntrl == 0) {
        ddlList.selectedIndex = valueindex;
    }
    else if (currentCntrl == 1) {
        ddlList.selectedIndex = valueindex;
    }
    else if (currentCntrl == 2) {
        ddlList.selectedIndex = valueindex;
    }
    else if (currentCntrl == 3) {
        ddlList.selectedIndex = valueindex;
    }


    
    if (currentCntrl == 0) {
        if (RowNodes.length == 1) {
            if (RowNodes[0].getAttribute('value') == 0) {
                document.getElementById("_ctl0_ContentPlaceHolder1_trweight").style.display = 'none';
            }
            else {
                document.getElementById("_ctl0_ContentPlaceHolder1_trweight").style.display = 'block';
            }
        } else {
            document.getElementById("_ctl0_ContentPlaceHolder1_trweight").style.display = 'block';
        }
    }

    if (currentCntrl == 0) {
        ddlweightOnChange(jsproductid, jsddlwidth, jsddlmetal, jsddlweight, jsddlfinger, jsddlring, jsselfinger, jslblprate);
    }
    else if (currentCntrl == 1) {
        ddlwidthOnChange(jsproductid, jsddlwidth, jsddlmetal, jsddlweight, jsddlfinger, jsddlring, jsselfinger, jslblprate);
    }
    else if (currentCntrl == 2) {
        ddlfingersizeOnChange(jsproductid, jsddlwidth, jsddlmetal, jsddlweight, jsddlfinger, jsddlring, jsselfinger, jslblprate);
    }
    else if (currentCntrl == 3) {
        ddlringOnChange(jsproductid, jsddlwidth, jsddlmetal, jsddlweight, jsddlfinger, jsddlring, jsselfinger, jslblprate);
    }
}

//Returns the node text value 
function GetInnerText(node) {
    return (node.textContent || node.innerText || node.text);
}


//Gets called when state combo box selection changes
function ddlmetalOnChange(productid, inddlwidth, inddlmetal, inddlweight, inddlfinger, inddlring, infinger, inlblprate) {
    strvalue = document.getElementById("_ctl0_ContentPlaceHolder1_ddlmetal").value + ',' + document.getElementById("_ctl0_ContentPlaceHolder1_ddlsize").value + ',' + document.getElementById("_ctl0_ContentPlaceHolder1_ddlwidth").value + ',' + document.getElementById("_ctl0_ContentPlaceHolder1_ddlfingersize").value + ',' + document.getElementById("_ctl0_ContentPlaceHolder1_ddlhalfsize").value + ',' + document.getElementById("_ctl0_ContentPlaceHolder1_ddlringsize").value;

    jsproductid = productid;
    jsddlwidth = inddlwidth;
    jsddlmetal = inddlmetal;
    jsddlweight = inddlweight;
    jsddlfinger = inddlfinger;
    jsddlring = inddlring;
    jsselfinger = infinger;
    jslblprate = inlblprate;

    currentCntrl = 0
    //Getting the selected country from country combo box.
    if (jsddlmetal) {
        var ddlmetal = jsddlmetal;
        var Metalid = ddlmetal.options[ddlmetal.selectedIndex].value;
    }

    // URL to get states for a given country
    var requestUrl = AjaxServerPageName + "?proid=" + encodeURIComponent(jsproductid) + "&metalid=" + encodeURIComponent(Metalid) + "&flag=1";

    CreateXmlHttp();
    // If browser supports XMLHTTPRequest object
    if (XmlHttp) {
        //Setting the event handler for the response
        XmlHttp.onreadystatechange = HandleResponse;

        //Initializes the request object with GET (METHOD of posting), 
        //Request URL and sets the request as asynchronous.
        XmlHttp.open("GET", requestUrl, true);

        //Sends the request to server
        XmlHttp.send(null);
    }
}

function ddlweightOnChange(productid, inddlwidth, inddlmetal, inddlweight, inddlfinger, inddlring, infinger, inlblprate) {

    strvalue = document.getElementById("_ctl0_ContentPlaceHolder1_ddlmetal").value + ',' + document.getElementById("_ctl0_ContentPlaceHolder1_ddlsize").value + ',' + document.getElementById("_ctl0_ContentPlaceHolder1_ddlwidth").value + ',' + document.getElementById("_ctl0_ContentPlaceHolder1_ddlfingersize").value + ',' + document.getElementById("_ctl0_ContentPlaceHolder1_ddlhalfsize").value + ',' + document.getElementById("_ctl0_ContentPlaceHolder1_ddlringsize").value;

    jsproductid = productid;
    jsddlwidth = inddlwidth;
    jsddlmetal = inddlmetal;
    jsddlweight = inddlweight;
    jsddlfinger = inddlfinger;
    jsddlring = inddlring;
    jsselfinger = infinger;
    jslblprate = inlblprate;

    currentCntrl = 1
    if (jsddlweight) {
        var ddlweight = jsddlweight;
        //Getting the selected country from country combo box.
        var weightid = ddlweight.options[ddlweight.selectedIndex].value;
    }
    if (jsddlmetal) {
        var ddlmetal = jsddlmetal;
        var Metalid = ddlmetal.options[ddlmetal.selectedIndex].value;
    }

    // URL to get states for a given country
    var requestUrl = AjaxServerPageName + "?proid=" + encodeURIComponent(jsproductid) + "&weightid=" + encodeURIComponent(weightid) + "&metalid=" + encodeURIComponent(Metalid) + "&flag=2";

    CreateXmlHttp();

    // If browser supports XMLHTTPRequest object
    if (XmlHttp) {
        //Setting the event handler for the response		
        XmlHttp.onreadystatechange = HandleResponse;

        //Initializes the request object with GET (METHOD of posting), 
        //Request URL and sets the request as asynchronous.
        XmlHttp.open("GET", requestUrl, true);

        //Sends the request to server
        XmlHttp.send(null);
    }
}

//Gets called when country combo box selection changes
function ddlwidthOnChange(productid, inddlwidth, inddlmetal, inddlweight, inddlfinger, inddlring, infinger, inlblprate) {

    strvalue = document.getElementById("_ctl0_ContentPlaceHolder1_ddlmetal").value + ',' + document.getElementById("_ctl0_ContentPlaceHolder1_ddlsize").value + ',' + document.getElementById("_ctl0_ContentPlaceHolder1_ddlwidth").value + ',' + document.getElementById("_ctl0_ContentPlaceHolder1_ddlfingersize").value + ',' + document.getElementById("_ctl0_ContentPlaceHolder1_ddlhalfsize").value + ',' + document.getElementById("_ctl0_ContentPlaceHolder1_ddlringsize").value;
    
    jsproductid = productid;
    jsddlwidth = inddlwidth;
    jsddlmetal = inddlmetal;
    jsddlweight = inddlweight;
    jsddlfinger = inddlfinger;
    jsddlring = inddlring;
    jsselfinger = infinger;
    jslblprate = inlblprate;

    currentCntrl = 2
    if (jsddlweight) {
        var ddlweight = jsddlweight;
        //Getting the selected country from country combo box.
        var weightid = ddlweight.options[ddlweight.selectedIndex].value;
    }
    if (jsddlmetal) {
        var ddlmetal = jsddlmetal;
        var Metalid = ddlmetal.options[ddlmetal.selectedIndex].value;
    }
    if (jsddlwidth) {
        var ddlwidth = jsddlwidth;
        //Getting the selected country from country combo box.
        var Widthid = ddlwidth.options[ddlwidth.selectedIndex].value;
    }


    // URL to get states for a given country
    var requestUrl = AjaxServerPageName + "?proid=" + encodeURIComponent(jsproductid) + "&widthid=" + encodeURIComponent(Widthid) + "&weightid=" + encodeURIComponent(weightid) + "&metalid=" + encodeURIComponent(Metalid) + "&flag=3";

    CreateXmlHttp();


    // If browser supports XMLHTTPRequest object
    if (XmlHttp) {
        //Setting the event handler for the response		
        XmlHttp.onreadystatechange = HandleResponse;

        //Initializes the request object with GET (METHOD of posting), 
        //Request URL and sets the request as asynchronous.
        XmlHttp.open("GET", requestUrl, true);

        //Sends the request to server
        XmlHttp.send(null);
    }
}


function ddlfingersizeOnChange(productid, inddlwidth, inddlmetal, inddlweight, inddlfinger, inddlring, infinger, inlblprate) {

    strvalue = document.getElementById("_ctl0_ContentPlaceHolder1_ddlmetal").value + ',' + document.getElementById("_ctl0_ContentPlaceHolder1_ddlsize").value + ',' + document.getElementById("_ctl0_ContentPlaceHolder1_ddlwidth").value + ',' + document.getElementById("_ctl0_ContentPlaceHolder1_ddlfingersize").value + ',' + document.getElementById("_ctl0_ContentPlaceHolder1_ddlhalfsize").value + ',' + document.getElementById("_ctl0_ContentPlaceHolder1_ddlringsize").value;
    
    jsproductid = productid;
    jsddlwidth = inddlwidth;
    jsddlmetal = inddlmetal;
    jsddlweight = inddlweight;
    jsddlfinger = inddlfinger;
    jsddlring = inddlring;
    jsselfinger = infinger;
    jslblprate = inlblprate;

    currentCntrl = 3
    if (jsddlweight) {
        var ddlweight = jsddlweight;
        //Getting the selected country from country combo box.
        var weightid = ddlweight.options[ddlweight.selectedIndex].value;
    }
    if (jsddlmetal) {
        var ddlmetal = jsddlmetal;
        var Metalid = ddlmetal.options[ddlmetal.selectedIndex].value;
    }
    if (jsddlwidth) {
        var ddlwidth = jsddlwidth;
        //Getting the selected country from country combo box.
        var Widthid = ddlwidth.options[ddlwidth.selectedIndex].value;
    }
    if (jsddlfinger) {
        var ddlfinger = jsddlfinger;
        //Getting the selected country from country combo box.
        var fingerid = ddlfinger.options[ddlfinger.selectedIndex].value;
    }


    // URL to get states for a given country
    var requestUrl = AjaxServerPageName + "?proid=" + encodeURIComponent(jsproductid) + "&fsizeid=" + encodeURIComponent(fingerid) + "&widthid=" + encodeURIComponent(Widthid) + "&weightid=" + encodeURIComponent(weightid) + "&metalid=" + encodeURIComponent(Metalid) + "&flag=4";

    CreateXmlHttp();

    // If browser supports XMLHTTPRequest object
    if (XmlHttp) {
        //Setting the event handler for the response
        XmlHttp.onreadystatechange = HandleResponse;

        //Initializes the request object with GET (METHOD of posting),
        //Request URL and sets the request as asynchronous.
        XmlHttp.open("GET", requestUrl, true);

        //Sends the request to server
        XmlHttp.send(null);
    }
}

//code adde

function ddlringOnChange(productid, inddlwidth, inddlmetal, inddlweight, inddlfinger, inddlring, infinger, inlblprate) {

    strvalue = document.getElementById("_ctl0_ContentPlaceHolder1_ddlmetal").value + ',' + document.getElementById("_ctl0_ContentPlaceHolder1_ddlsize").value + ',' + document.getElementById("_ctl0_ContentPlaceHolder1_ddlwidth").value + ',' + document.getElementById("_ctl0_ContentPlaceHolder1_ddlfingersize").value + ',' + document.getElementById("_ctl0_ContentPlaceHolder1_ddlhalfsize").value + ',' + document.getElementById("_ctl0_ContentPlaceHolder1_ddlringsize").value;
    
    jsproductid = productid;
    jsddlwidth = inddlwidth;
    jsddlmetal = inddlmetal;
    jsddlweight = inddlweight;
    jsddlfinger = inddlfinger;
    jsddlring = inddlring;
    jsselfinger = infinger;
    jslblprate = inlblprate;

    currentCntrl = 4
    if (jsddlweight) {
        var ddlweight = jsddlweight;
        //Getting the selected country from country combo box.
        var weightid = ddlweight.options[ddlweight.selectedIndex].value;
    }
    if (jsddlmetal) {
        var ddlmetal = jsddlmetal;
        var Metalid = ddlmetal.options[ddlmetal.selectedIndex].value;
    }
    if (jsddlwidth) {
        var ddlwidth = jsddlwidth;
        //Getting the selected country from country combo box.
        var Widthid = ddlwidth.options[ddlwidth.selectedIndex].value;
    }
    if (jsddlfinger) {
        var ddlfinger = jsddlfinger;
        //Getting the selected country from country combo box.
        var fingerid = ddlfinger.options[ddlfinger.selectedIndex].value;
    }
    if (jsddlring) {
        var ddlring = jsddlring;
        //Getting the selected country from country combo box.
        var ringid = ddlring.options[ddlring.selectedIndex].value;
    }


    // URL to get states for a given country
    var requestUrl = AjaxServerPageName + "?proid=" + encodeURIComponent(jsproductid) + "&rsizeid=" + encodeURIComponent(ringid) + "&fsizeid=" + encodeURIComponent(fingerid) + "&widthid=" + encodeURIComponent(Widthid) + "&weightid=" + encodeURIComponent(weightid) + "&metalid=" + encodeURIComponent(Metalid) + "&flag=5";

    CreateXmlHttp();


    // If browser supports XMLHTTPRequest object
    if (XmlHttp) {
        //Setting the event handler for the response		
        XmlHttp.onreadystatechange = HandleResponse;

        //Initializes the request object with GET (METHOD of posting), 
        //Request URL and sets the request as asynchronous.
        XmlHttp.open("GET", requestUrl, true);

        //Sends the request to server
        XmlHttp.send(null);
    }
}
