// This creates a XMLRequest (ActiveXObject) to be sent to the server
var XmlReq;
// This page returns the XML Response for the selected choice
var AjaxServerPageName = "AjaxServer.aspx";

function CreateXmlReq() {
    try {
        XmlReq = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e) {
        try {
            XmlReq = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch (oc) {
            XmlReq = null;
        }
    }
    if (!XmlReq && typeof XMLHttpRequest != "undefined") {
        XmlReq = new XMLHttpRequest();
    }
}

function GetConditionData(obj) {

    var cbl = document.getElementById(obj);
   // alert(cbl);
    var len = cbl.getElementsByTagName('input').length;
    var lbl = ''
    for (i = 0; i < len; i++) {
        var opt = document.getElementById(cbl.id + "_" + i);

        if (opt.checked == true) {

            lbl = lbl + opt.parentNode.childNodes[1].innerHTML + ",";
           
        }
    }
    return lbl;
}
//This fucntion is to send the choice into the AJAX Server page for processing
function FetchDataCond() {

    var lblShape = GetConditionData('ChkShape')
    var lblPty = GetConditionData('ChkPty')
    var lblClr = GetConditionData('ChkClr')
    var lblFlour = GetConditionData('ChkFlorecent')
    var lblLab = GetConditionData('ChkLab')
    var lblPol = GetConditionData('ChkListPol')
    var lblSym = GetConditionData('ChkListSym')
    var lblCut = GetConditionData('ChkListCut')
    lblShape = lblShape.slice(0, -1);
    lblPty = lblPty.slice(0, -1);
    lblClr = lblClr.slice(0, -1);
    lblFlour = lblFlour.slice(0, -1);
    lblLab = lblLab.slice(0, -1);
    lblPol = lblPol.slice(0, -1);
    lblSym = lblSym.slice(0, -1);
    lblCut = lblCut.slice(0, -1);
    var CrtLft = parseFloat(document.getElementById('txtminweight').value);
    var CrtRht = parseFloat(document.getElementById('txtmaxweight').value);
    document.getElementById('imgtbl').style.visibility = 'visible';
    var requestUrl = AjaxServerPageName + "?Choice=" + encodeURIComponent(CrtLft) + '-' + encodeURIComponent(CrtRht) + '-' +
                                                       encodeURIComponent(lblShape) + '-' + encodeURIComponent(lblPty) + '-' +
                                                       encodeURIComponent(lblClr) + '-' + encodeURIComponent(lblFlour) + '-' +
                                                       encodeURIComponent(lblLab) + '-' + encodeURIComponent(lblPol) + '-' +
                                                       encodeURIComponent(lblSym) + '-' + encodeURIComponent(lblCut);

    CreateXmlReq();

    if (XmlReq) {
        XmlReq.onreadystatechange = HandleResponse;
        XmlReq.open("GET", requestUrl, true);
        XmlReq.send();
    }
}



function HandleResponse() {
    if (XmlReq.readyState == 4) {
        if (XmlReq.status == 200) {
            // Before filling new contents into the DataGrid, clear the cotents by calling this function
            //ClearTable();
            // Fill the cleared Datagrid with new XML Reponse
            //alert(XmlReq.responseText);
            document.getElementById('txtRes').innerHTML = '<span>' + XmlReq.responseText + '</span>';
           // document.getElementById('lblTotalS').innerHTML = "Found Total Stones :" +  XmlReq.responseText;
            //FillTable(XmlReq.responseXML);
            // Hides the Process Image Table after displaying the contents
            document.getElementById('imgtbl').style.visibility = 'hidden';
        }
        else {
            alert("There was a problem retrieving data from the server.");
        }
    }
}

