﻿function chkInsTypes(typeID,check){
    var obj = document.getElementById("hfInsuranceTypes");
    if (check){
        obj.value = obj.value + "#" + typeID + "#";
    }else{
        obj.value = obj.value.replace("#" + typeID + "#", "");
    }

    retrieveDirectory();
}

function retrieveDirectory(){
    if (document.getElementById("divDirectory")){
        zip = document.getElementById("txtSearchZip").value;
        instypes = document.getElementById("hfInsuranceTypes").value;
        
        instypes = instypes.replace(/##/g,",");
        instypes = instypes.replace(/#/g,"");

        retrieveAgentData("divDirectory",zip,instypes);
    }
}

function retrieveZip(){
    if (document.getElementById("divZip")){
        zip = document.getElementById("txtSearchZip").value;
        
        retrieveZipData("divZip",zip);
        retrieveDirectory();
    }
}

function retrieveAgentData(returndiv,zip,instypes){
    //create object for requests
    var oXmlHTTP_dir;
    oXmlHTTP_dir = createXMLHTTP();

    //connecting to the server could be a little slow at times so these couple of lines give the user a little feedback that something is happening
    var dir = document.getElementById(returndiv);
    dir.style.visibility = "visible";
    dir.innerHTML = "Loading Data...";
    
    //listener function for when the server returns its response from the open statement in the switch code above
    oXmlHTTP_dir.onreadystatechange = function(){
        //check to make sure the response is ready and completed without any unhandled errors (200 is result code for successful http transfer)
        if (oXmlHTTP_dir.readyState == 4 && oXmlHTTP_dir.status == 200){
            dir.innerHTML = oXmlHTTP_dir.responseText;
        }
    }

    oXmlHTTP_dir.open("get","/ajax/directory.aspx?zip=" + zip + "&instypes=" + instypes);
    oXmlHTTP_dir.send(null);
}

function retrieveZipData(returndiv,zip){
    //create object for requests
    var oXmlHTTP_zip;
    oXmlHTTP_zip = createXMLHTTP();

    //connecting to the server could be a little slow at times so these couple of lines give the user a little feedback that something is happening
    var dir = document.getElementById(returndiv);
    dir.style.visibility = "visible";
    //dir.innerHTML = "Loading Data...";
    
    //listener function for when the server returns its response from the open statement in the switch code above
    oXmlHTTP_zip.onreadystatechange = function(){
        //check to make sure the response is ready and completed without any unhandled errors (200 is result code for successful http transfer)
        if (oXmlHTTP_zip.readyState == 4 && oXmlHTTP_zip.status == 200){
            dir.innerHTML = oXmlHTTP_zip.responseText;
        }
    }

    oXmlHTTP_zip.open("get","/ajax/dir_zip.aspx?zip=" + zip);
    oXmlHTTP_zip.send(null);
}
