// JavaScript Document
function WeatherObject() {
	this.humidity;
	this.wind;
	this.temp;
	this.imgURL;
	this.desc;
	this.high;
	this.low;
	this.day;
	this.buildCurrent=wObject_buildCurrent;
	this.buildDay=wObject_buildDay;
}

function wObject_buildCurrent() {
	var html;
	html =  "<table cellspacing=\"2\" cellpadding=\"1\">\n";
	html += "  <tr><td><font style=\"font-size: 14px; font-weight: bold;\">" + this.temp + "&#176;F</td></tr>\n";
	html += "  <tr><td>" + this.desc + "</td></tr>\n";
	html += "  <tr><td>" + this.humidity + "</td></tr>\n";
	html += "  <tr><td>" + this.wind + "</td></tr>\n";
	html += "</table>\n";
	return html;
}

function wObject_buildDay() {
	var html;
	html =  "<table cellspacing=\"2\" cellpadding=\"1\">\n";
	html += "  <tr><td align=\"center\">" + this.day + "</td></tr>\n";
	html += "  <tr><td align=\"center\"><img src=\"http://www.google.com" + this.imgURL + "\" border=\"0\" /></td></tr>\n";
	html += "  <tr><td>" + this.high + "&#176;F | " + this.low + "&#176;F</td></tr>\n";
	html += "</table>\n";
	return html;
}

function searchEquipByDecal(obj,option) {
    if (obj.value == "") alert("Please enter a valid decal in the search field!");
    else makeRequest("edSearch.php?decal=" + obj.value,option);
}

function srv_content_add(obj) {
	searchEquipByDecal(obj,document.getElementsByName("srv_equipDecal[]")[0]);
}

function makeRequest(url,option) {
    var httpRequest;

    if (window.XMLHttpRequest) { // Mozilla, Safari, ...
        httpRequest = new XMLHttpRequest();
        if (httpRequest.overrideMimeType) {
            httpRequest.overrideMimeType('text/xml');
        }
    } else if (window.ActiveXObject) { // IE
        try {
            httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) { }
        }
    }
    if (httpRequest == null) {
        return false;
    }
    httpRequest.onreadystatechange = function() {
        alertContentsDecalSeek(httpRequest,option);
    }
    httpRequest.open('GET',url,true);
    httpRequest.send(null);
}

function makeRequestWeather() {
    var httpRequest;
	
    if (window.XMLHttpRequest) { // Mozilla, Safari, ...
   	    httpRequest = new XMLHttpRequest();
       	if (httpRequest.overrideMimeType) {
           	httpRequest.overrideMimeType('text/xml');
        }
   	} else if (window.ActiveXObject) { // IE
       	try {
           	httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
   	        try {
       	        httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
           	} catch (e) { }
        }
   	}
    if (httpRequest == null) {
   	    return false;
    }
   	httpRequest.onreadystatechange = function() {
       	alertContentsWeather(httpRequest);
    }
   	httpRequest.open("GET","/admissions/visitors/Scripts/weatherReport.php",true);
    httpRequest.send("");
}

function alertContentsWeather(httpRequest) {
	if (httpRequest.readyState == 4) {
        if (httpRequest.status == 200) {
			var html;
			var wObj = new WeatherObject();
			var doc = httpRequest.responseXML;
			var resultData;
            var resultText = doc.getElementsByTagName("xml_api_reply")[0].getElementsByTagName("weather")[0];
			resultData = resultText.getElementsByTagName("current_conditions")[0].childNodes;
			html = "<table cellspacing=\"2\" cellpadding=\"1\">\n";
			html += "  <tr>\n";
			for (var i = 0; i < resultData.length; i++) {
				if (resultData[i].tagName == "condition") wObj.desc = resultData[i].getAttribute("data");
				else if (resultData[i].tagName == "temp_f") wObj.temp = resultData[i].getAttribute("data");
				else if (resultData[i].tagName == "humidity") wObj.humidity = resultData[i].getAttribute("data");
				else if (resultData[i].tagName == "icon") wObj.imgURL = resultData[i].getAttribute("data");
				else if (resultData[i].tagName == "wind_condition") wObj.wind = resultData[i].getAttribute("data");
			}
			html += "    <td>\n" + wObj.buildCurrent() + "    </td>\n";
			resultData = resultText.getElementsByTagName("forecast_conditions");
			for (var x = 0; x < resultData.length; x++) {
				var result = resultData[x].childNodes;
				for (var i = 0; i < result.length; i++) {
					if (result[i].tagName == "day_of_week") wObj.day = result[i].getAttribute("data");
					else if (result[i].tagName == "low") wObj.low = result[i].getAttribute("data");
					else if (result[i].tagName == "high") wObj.high = result[i].getAttribute("data");
					else if (result[i].tagName == "icon") wObj.imgURL = result[i].getAttribute("data");
					else if (result[i].tagName == "condition") wObj.desc = result[i].getAttribute("data");
				}
				html += "    <td>\n" + wObj.buildDay() + "    </td>\n";
			}
			html += "  </tr>\n";
			html += "</table>\n";
			if (document.getElementById("weatherData") != null) {
				document.getElementById("weatherData").innerHTML = html;
			}
		}
	}
}
//Load event
if (typeof addLoadFunction == 'function') {
	addLoadFunction(weatherBuildURL);
} else if (window.onload == null) {
	window.onload = makeRequestWeather;
} else {
	var loadFunction = window.onload();
	window.onload = function() {
		if (typeof loadFunction == 'function') {
			loadFunction();
		} makeRequestWeather();
	}
}