// This creates a XMLRequest (ActiveXObject) to be sent to the server
var XmlSearchReq;
// This page returns the XML Response for the selected choice
var AjaxServerSearchPageName = "AjaxSearchServer.aspx";

function CreateXmlSearchReq() {
    try {
        XmlSearchReq = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e) {
        try {
            XmlSearchReq = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch (oc) {
            XmlSearchReq = null;
        }
    }
    if (!XmlSearchReq && typeof XMLHttpRequest != "undefined") {
        XmlSearchReq = new XMLHttpRequest();
    }
}

function GetConditionDataSearch(obj) {

    var cbl = document.getElementById(obj);

    var len = cbl.getElementsByTagName('input').length;
   
    var lbl = ''
    for (i = 0; i < len; i++) {
        var opt = document.getElementById(cbl.id + "_" + i);
       // alert(opt);
        if (opt.checked == true) {
            //alert(opt.checked);
            lbl = lbl + opt.parentNode.childNodes[1].innerHTML + ",";
            
        }
    }
   // alert(lbl);
    return lbl;
}
//This fucntion is to send the choice into the AJAX Server page for processing
function FetchDataCondSearch() {

    var lblShape = GetConditionDataSearch('ChkShape')
    var lblPty = GetConditionDataSearch('ChkPty')
    var lblClr = GetConditionDataSearch('ChkClr')
    var lblFlour = GetConditionDataSearch('ChkFlorecent')
    var lblLab = GetConditionDataSearch('ChkLab')
    var lblPol = GetConditionDataSearch('ChkListPol')
    var lblSym = GetConditionDataSearch('ChkListSym')
    var lblCut = GetConditionDataSearch('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 = AjaxServerSearchPageName + "?Choice=" + encodeURIComponent(CrtLft) + '-' + encodeURIComponent(CrtRht) + '-' +
                                                       encodeURIComponent(lblShape) + '-' + encodeURIComponent(lblPty) + '-' +
                                                       encodeURIComponent(lblClr) + '-' + encodeURIComponent(lblFlour) + '-' +
                                                       encodeURIComponent(lblLab) + '-' + encodeURIComponent(lblPol) + '-' +
                                                       encodeURIComponent(lblSym) + '-' + encodeURIComponent(lblCut);

    CreateXmlSearchReq();

    if (XmlSearchReq) {
        XmlSearchReq.onreadystatechange = HandleSearchResponse;
        XmlSearchReq.open("GET", requestUrl, true);
        XmlSearchReq.send();
    }
}



function HandleSearchResponse() {
    if (XmlSearchReq.readyState == 4) {
        if (XmlSearchReq.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>' + XmlSearchReq.responseText + '</span>';
            // alert(XmlReq.responseText);
           // 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.");
        }
    }
}


