﻿//======================================
//======================================
//============DO NOT REMOVE=============
//======================================
// Created by Rajib Ahmed
// http://www.ProgTalk.com
//======================================
//======================================
//======================================

var req;
var CurrentDIV;

//Set up to use javascript to call pages for data lookup
//YOU DO NOT NEED TO CHANGE ANYTHING BELOW
function Initialize()
{

       try
       {
              req = new ActiveXObject("Msxml2.XMLHTTP");
       }
       catch(e)
       {
              try
              {
                     req = new ActiveXObject("Microsoft.XMLHTTP");
              }
              catch(oc)
              {
                     req = null;
              }
       } 
       if( !req && typeof XMLHttpRequest != "undefined" )
       {
              req = new XMLHttpRequest();
       }
} 
 

//sends the query to desired page, and returns to div autocomplete
//based on what the user typed
//key paramater has what the user typed.
//div paramater states, which div to stick the data back to.

function SendQuery(key, MyDiv, url, doState)
{
       if ( key == null || key.length <= 0 )
       {
              document.getElementById(MyDiv).innerHTML = "";
              HideDiv(MyDiv);
              return;
       }
       CurrentDIV = MyDiv;
       Initialize(); 
       var url = url + "&k=" + key + "&MyDiv=" + MyDiv
       
       if (doState) {
           if ( document.getElementById('ctl00_ContentPlaceHolder1_fBuildState').value != null ) {
                url = url + "&cmbState=" + document.getElementById('ctl00_ContentPlaceHolder1_fBuildState').value; 
           }
       }
       
       //alert(url);
       if( req != null)
       {
              req.onreadystatechange = Process;
              req.open("GET", url, true);
              req.send(null);
       }
}
 

//checks is status was good when calling url for data lookup

function Process()
{
       if (req.readyState == 4)
       {
              // only if "OK"
              if (req.status == 200)
              {
                     if(req.responseText=="")
                           HideDiv(CurrentDIV);
                     else
                     {
                           //alert(req.responseText);
                           ShowDiv(CurrentDIV);
                           document.getElementById(CurrentDIV).innerHTML =req.responseText;
                     }
              }
              else
              {
                     document.getElementById(CurrentDIV).innerHTML=
                           "There was a problem retrieving data:<br>"+req.statusText;
              }
       }
}
 

//Show Div Section in html
function ShowDiv(divid)
{
       if (document.layers) document.layers[divid].display="block";
       else document.getElementById(divid).style.display="block";
}
 

//Hide Div Section in html
function HideDiv(divid)
{
       if (document.layers) document.layers[divid].display = "none";
       else document.getElementById(divid).style.display="none";
}

 
//Load selected value into textbox, and hide div
function SetTextbox( TextboxID, data, MyDiv, pCode, pCodeID)
{
    document.getElementById(TextboxID).value = data;
    if ( MyDiv.length > 0 )
    {
        HideDiv( MyDiv );
        // set postcode field
        document.getElementById(pCodeID).value = pCode;
    }
    
    // if finding a builder, auto submit to get details straight away without clicking search
    if (TextboxID == "Leftmenu1_txtPostCode") {
    	document.getElementById("Leftmenu1_ibSubmit1").click();
    }
    else if (TextboxID == "Text1") {
        document.getElementById("ibSubmit").click();
    }
    
}

