//
// Gets the browser specific XmlHttpRequest Object 
//
function getHttpRequest () {

//
// Internet Explore 7 and all other compatable browsers (Mozilla, Safari, etc)
//
if (window.XMLHttpRequest) {
    try {
        return new XMLHttpRequest ();
        }
    catch (e) {
	}
    } 
else if (window.ActiveXObject) {
    //
    // This is for Internet Explore 6
    //
    var msxmls = new Array ("Microsoft.XMLHTTP",
                            "Msxml2.XMLHTTP.5.0",
                            "Msxml2.XMLHTTP.4.0",
                            "Msxml2.XMLHTTP.3.0",
                            "Msxml2.XMLHTTP");
    for (var i = 0; msxmls.length; ++i) {
	try {
	    return new ActiveXObject (msxmls[i]);
	    }
	catch (e) {
	    }
	}
    }

//
// Throw an error if we couldn't create an object
//
throw new Error ("Could not instantiate XMLHttpRequest");

}

//
//
//
function AjaxXmlResponse (HttpReq, HttpRtn) {

if (HttpReq.readyState == 4) { 
    if (HttpReq.status == 200 || HttpReq.status == 304) {
	if (HttpRtn.value != "undefined") {
	    var data = HttpReq.responseXML;
	    HttpRtn (data);
	    }
	} 
    else {
	alert ('An unexpected status of ' + HttpReq.status + ' was encountered');
	}
    }

}

//
//
//
function AjaxTxtResponse (HttpReq, HttpRtn) {

if (HttpReq.readyState == 4) { 
    if (HttpReq.status == 200 || HttpReq.status == 304) {
	if (HttpRtn.value != "undefined") {
	    var data = HttpReq.responseText;
	    HttpRtn (data);
	    }
	} 
    else {
	alert ('An unexpected status of ' + HttpReq.status + ' was encountered');
	}
    }

}

//
//
//
function AjaxXmlReq (HttpXml, HttpRtn, HttpSync) {

var HttpReq = getHttpRequest ();
if (HttpReq) {
    if (HttpSync == null) {
        HttpSync = true;
        }
    HttpReq.onreadystatechange = function() {AjaxXmlResponse (HttpReq, HttpRtn); };
    HttpReq.open ("GET", HttpXml, HttpSync);
    HttpReq.send (null);
    }

}

//
//
//
function AjaxTxtReq (HttpTxt, HttpRtn, HttpSync) {

var HttpReq = getHttpRequest ();
if (HttpReq) {
    if (HttpSync == null) {
        HttpSync = true;
        }
    HttpReq.onreadystatechange = function() {AjaxTxtResponse (HttpReq, HttpRtn); };
    HttpReq.open ("GET", HttpTxt, HttpSync);
    HttpReq.send (null);
    }

}

var ImageLinks = new Array(0);
var ImageNames = new Array(0);
var ImageLocal = new Array(0);

var ImageIdx = 0;

//
// Parse Images
//
function ParseImages (XmlDoc) {

var ImagesObj = XmlDoc.getElementsByTagName('images')[0];
var ImageInt = 3;

for (var i = 0; i < ImagesObj.childNodes.length; i++) {
    var NodeName = ImagesObj.childNodes[i].nodeName;
    NodeName = NodeName.toLowerCase();
    if (NodeName == "image") {
    ImageLinks.push(ImagesObj.childNodes[i].getAttribute('url'));
    ImageNames.push(ImagesObj.childNodes[i].getAttribute('name'));
	ImageLocal.push(ImagesObj.childNodes[i].getAttribute('location'));
    }
    if (NodeName == "interval") {
    ImageInt = ImagesObj.childNodes[i].firstChild.nodeValue;
    }
    }

if (ImageLinks.length > 0) {
    document.getElementById("Image").innerHTML = '<img width="155px" height="204px" src="' + ImageLinks[ImageIdx] + '" alt="" border="0" /><p>'+ImageNames[ImageIdx]+'<br />'+ImageLocal[ImageIdx]+'</p>';
    ImageIdx++;
    setInterval ("RotateImages()", ImageInt * 1000);
    }

}

//
//
//
function RotateImages() {

if (ImageIdx >= ImageLinks.length - 1) {
    ImageIdx = 0;
    }
else {
    ImageIdx++;
    }
//document.getElementById("Image").innerHTML = '<img width="213px" height="285px" src="' + ImageLinks[ImageIdx] + '" alt="' + ImageNames[ImageIdx] + '" border="0"  />';
document.getElementById("Image").innerHTML = '<img width="155px" height="204px" src="' + ImageLinks[ImageIdx] + '" alt="" border="0" /><p>'+ImageNames[ImageIdx]+'<br />'+ImageLocal[ImageIdx]+'</p>';

}


//
// Check the zip code
//
function CheckZip(_ZipCode) {

var ZipCode = _ZipCode.value;

var ZipCodeLength = ZipCode.length;

if (ZipCodeLength == 0) {
    alert ("Please enter zipcode.");
    return false;
    }
if (ZipCodeLength != 5 || isNaN(ZipCode)) {
    alert ("Please enter a valid 5 digit zipcode.");
    return false;
    }
for (var j = 0; j < String(ZipCode).length; j++) {
    if ((String(ZipCode).charAt(j) < "0") || (String(ZipCode).charAt(j) > "9")) {
        alert ("Please enter a valid zipcode.");
        return false;
        }
    }

document.frmEnterZip.submit ();
}

//
// Check the state code
//
function CheckState(_StateCode) {

var StateCode = _StateCode.value;
var StateCodeLength = String(StateCode).length;

if (StateCodeLength == 0) {
    alert ("Please enter a state.");
    return false;
    }
if (StateCodeLength != 2) {
    alert ("Please enter a valid state.");
    return false;
    }

document.frmEnterState.submit ();

}
uniqid=Math.floor((Math.random()+1000000)*1000000)


//
//
//


function OnLoad() {

AjaxXmlReq ("/content/images.xml?id="+uniqid, ParseImages);
zipSearch();
}
window.onload = function() {
	document.onload = OnLoad();	
}

