// JScript File
var request = null;
var response = null;
var buttonClicked = null;

function doZipcodeSearch(form) {
    this.form = form;
    var req = this;
    if (window.XMLHttpRequest) {
        // If IE7, Mozilla, Safari, etc: Use native object
        this.xmlHttp = new XMLHttpRequest();
    }
    else if (window.ActiveXObject) {
        // ...otherwise, use the ActiveX control for IE5.x and IE6
        this.xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else alert("XMLHttpRequest failed!");
    this.xmlHttp.onreadystatechange = function () {
        if (req.xmlHttp.readyState == 4) {
            window.request = null;
            if (req.xmlHttp.status == 200) {
            	if ( req.xmlHttp.responseText )
            		document.getElementById("zipcodeResult").innerHTML = req.xmlHttp.responseText;
            	else
            	{
            		var test = createAjaxRequest(doZipcodeSearch, req.form);
            		test.send();
            	}
            }
            else {
                alert('There was a problem with the request.');
            }
            //parent.hideWaitMessage();
       }
    };
    this.send = function () {
        try
        {
            var poststr = "";
            for (i = 0; i < this.form.elements.length; i++) {
                var e = this.form.elements[i];
                if ( (e.type == "button") && (this.buttonClicked != e) ) continue;
                if ( e.name && e.value && (e.type != "radio" || e.checked) && (e.type != "checkbox" || e.checked) ) {
                    if (poststr) {
                        poststr = poststr + "&" + e.name + "=" + encodeURIComponent( e.value );
                    }
                    else {
                        poststr = e.name + "=" + encodeURIComponent( e.value );
                    }
                }
            }
            this.xmlHttp.open("POST", this.form.action, true);
            this.xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            this.xmlHttp.setRequestHeader("Content-length", poststr.length);
            this.xmlHttp.setRequestHeader("Connection", "close");
            this.xmlHttp.send(poststr);
        }
        catch (error)
        {
            window.request = null;
            alert(error.toString());
        }
    }
    document.getElementById("zipcodeResult").innerHTML = "Please wait...";
}

function createAjaxRequest(reqObj, srcObj) {
    if ( window.request ) {
        alert('The previous request is still in progress.  Please wait for it.');
        return false;
    }
    return window.request = new reqObj(srcObj);
}
