﻿//======================================
//======================================
//============DO NOT REMOVE=============
//======================================
// Created by Rajib Ahmed
// From ProgTalk.com
//======================================
//======================================
//======================================
var req;
var CurrentDIV;
var searchddl;
var searchdiv;
searchdiv = 'panel1';
//Set up to use javascript to call pages
//You do not need to change anything here
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, key1, key2, MyDiv, url, ddlsearch) {
    var searchkeyword;
    searchkeyword = '';
    var searchkeyword1;
    var searchkeyword2;
    var searchkeyword3;
    var strchkflg;
    strchkflg = false
    searchkeyword1 = document.getElementById(key).value;
    searchkeyword2 = document.getElementById(key1).value;
    searchkeyword3 = document.getElementById(key2).value;
    if (searchkeyword1 != null && searchkeyword1.length > 0 && searchkeyword1 != 'Restaurant') {
        strchkflg = true;
        searchkeyword = searchkeyword1;
    }
    if (searchkeyword2 != null && searchkeyword2.length > 0 && searchkeyword2 != 'City') {
        strchkflg = true;
        searchkeyword = searchkeyword + ',' + searchkeyword2;
    }
    else { searchkeyword = searchkeyword + ','; }
    if (searchkeyword3 != null && searchkeyword3.length > 0 && searchkeyword3 != 'State') {
        strchkflg = true;
        searchkeyword = searchkeyword + ',' + searchkeyword3;
    }
    else { searchkeyword = searchkeyword + ','; }
    if (strchkflg == false) {
        document.getElementById(MyDiv).innerHTML = "";
        HideDiv(MyDiv);
        HideDiv(searchdiv);
        document.getElementById(ddlsearch).style.display = '';
        return;
    }
    searchddl = ddlsearch
    CurrentDIV = MyDiv;    
    Initialize();
    var url = url + "?searchterms=" + escape(searchkeyword) + "&MyDiv=" + MyDiv;
    
    if (req != null) {
        req.onreadystatechange = Process;
        req.open("GET", url, true);
        req.send(null);

    }
    
}

//checks is status was good
function Process() {

    document.getElementById("contentmsg").innerHTML = "<img height='22' src='" + document.getElementById('servername').value + "images/loadingsearch.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("contentmsg").offsetHeight) / 2);
    //document.getElementById("contentmsg").style.top = (IpopTop + h) + 'px';
    //document.getElementById("contentmsg").style.left = ((document.body.clientWidth / 2)) + 'px';
    document.getElementById("contentmsg").style.display = "block";
    

    //////////////////////////////////////////////
    if (req.readyState == 4) {
        document.getElementById("contentmsg").style.display = "none";
        if (req.status == 200) {
            if (req.responseText == "") {
                HideDiv(CurrentDIV);
                document.getElementById(searchddl).style.display = '';              
            }
            else {
                document.getElementById(searchddl).style.display = 'none';
                ShowDiv(searchdiv);
                ShowDiv(CurrentDIV);

                document.getElementById(CurrentDIV).innerHTML = req.responseText;

                document.getElementById("contentmsg").innerHTML = "";
                document.getElementById("contentmsg").style.display = "none";
            }
        }
        else {
            document.getElementById(searchddl).style.display = '';              
            document.getElementById(CurrentDIV).innerHTML = "There was a problem retrieving data:<br>" + req.statusText;
        }
    }    
}

function ShowDiv(divid) {
    if (document.layers) document.layers[divid].display = "block";
    else document.getElementById(divid).style.display = "block";
}

function HideDiv(divid) {
    //alert(document.layers[divid].display);
    if (document.layers) document.layers[divid].display = "none";
    else document.getElementById(divid).style.display = "none";
}

function SetTextbox(TextboxID, data, MyDiv) {
    document.getElementById(TextboxID).value = data;
    if (MyDiv.length > 0) {
        HideDiv(MyDiv);

    }
         
}

function setfocusoffhide() {   
    //document.getElementById(CurrentDIV).style.display = "none";
    document.getElementById(searchdiv).style.display = 'none';
}

function hidetext(txtbox) {
    document.getElementById(txtbox).value = "";
}

