﻿var http = false;

if (navigator.appName == "Microsoft Internet Explorer") {
    http = new ActiveXObject("Microsoft.XMLHTTP");
}
else {
    http = new XMLHttpRequest();

    if (http == null)
        alert("no XMLHttpRequest() support");
}



function AjaxCall(url) {
    document.getElementById("progrss").style.display = "block";
    //alert(url);
    //throw "simon error";
    //URL = "sajax.asp?ajax=" + id;
    http.open("GET", url, true);

    http.onreadystatechange = onHttpReturn;

    http.send(null);
}

function onHttpReturn() {
    //alert(http.readyState);
    if (http.readyState == 4) {
        if (http.status == 200) {
            docXML = http.responseText;
            //alert(docXML);            
            getDisp().innerHTML = docXML;
            AdjustCover();
            document.getElementById("progrss").style.display = "none";
        }
    }
}

function showProgress(yes) {
    if (yes == true)
        document.getElementById("progrss").style.display = "block";
    else
        document.getElementById("progrss").style.display = "none";
}

function AjaxPost(url, data) {
    //alert("loading " + url + ", " + data);
    document.getElementById("progrss").style.display = "block";
    http.open("POST", url, true);

    http.onreadystatechange = onHttpReturn;
    var header;
    header = 'Content-Type:application/x-www-form-urlencoded; charset=UTF-8';
    http.setRequestHeader(header.split(':')[0], header.split(':')[1]);
    //alert(data);
    http.send(data);

}

function getDisp() {
    return document.getElementById("listings");
}

function showLocation(obj) {
    alert("window's location: x=" + obj.offsetLeft + "; y=" + obj.offsetTop);
    alert("height=" + obj.offsetHeight + "; width=" + obj.offsetWidth);
    alert("style width=" + obj.style.width + "; style height=" + obj.style.height);
}

function show() {
    showLocation(getDisp());
}

function hideSearchBox() {
    //alert("rlookup: " + document.getElementById("rlookup").style.display);
    document.getElementById("rlookup").style.display = "none";
    document.getElementById("cover").style.zIndex = -70;
}

function showSearchBox() {
    /*alert("showbox");
    if (document.getElementById("rlookup"))
    alert("found");
    else
    alert("not found");
    */
    document.getElementById("rlookup").style.display = "block";
    document.getElementById("cover").style.zIndex = 70;
}

function isZipCode(value) {
    var RegExp = /^\d{5}$/;
    return (RegExp.test(value));
}

function onZipSearch() {
    //show();
    if (isZipCode(document.getElementById("zip").value)) {

        AjaxCall("GetRest.aspx?pgn=gcp&PageSize=3&zip=" + document.getElementById("zip").value);
        //AjaxCall("GetRest.php?pgn=gcp&PageSize=3&zip=" + document.getElementById("zip").value);
        //AjaxPost("http://gift.restaurant.com/GetRest.asp?pgn=gcp", "PageSize=5&zip=" + document.getElementById("zip").value);
        //show(); 
        //alert(document.getElementById("zip").value);
        hideSearchBox();
    }
    else
        alert("Please enter a 5-digit zip code.");
}

function onStateSearch() {
    //alert("OK again");
    //AjaxCall("GetState.asp?");
    if (document.getElementById("StateID").selectedIndex == 0)
        alert("Please select a state.");
    else {
        var url;
        //url = "GetState.php?pgn=gcp";
        url = "GetState.aspx?pgn=gcp";
        var data;
        //alert(document.getElementById("StateID").selectedIndex);

        data = "VicinitySearchLevel=state&StateID=" + document.getElementById("StateID").value;
        //alert(url+"&"+data);
        AjaxCall(url + "&" + data);
        hideSearchBox();
    }
}

function AdjustCover() {

    document.getElementById("cover").style.height = document.getElementById("gc-box-right").clientHeight;
    //alert(document.getElementById("gc-box-right").clientHeight);
    //alert(document.getElementById("gc-box-right").getAttribute('height'));
}
