// Ajax Utilities

function createRequest() {
	try {
		request = new XMLHttpRequest();
	} catch (tryMS) {
		try {
			request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (otherMS) {
			try {
				request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (failed) {
				request = null;
			}
		}
	}
	return request;
}

function getDetails(url,dDiv) {
	request = createRequest();
	if (request==null) {
		alert("Unable to create request");
		return;
	}
	request.open("GET",url,true);
	request.onreadystatechange = function() { displayDetails(dDiv); }
	request.send(null);
}

function displayDetails(dDiv) {
	if (request.readyState == 4) {
		if (request.status == 200) {
			detailDiv = document.getElementById(dDiv);
			detailDiv.innerHTML = request.responseText;
		}
	}
}

function getWeather(options,title) { 
	var request = createRequest();
	if (request==null) {
		alert("Unable to create request");
		return;
	}
	request.open("GET","weatherWidget.php",true);
	request.onreadystatechange = function() {
		if (request.readyState == 4) {
			if (request.status == 200) {
				// Parse CSV string from PHP widget
				var wData = request.responseText;
				wData = wData.split(",");
				
				// Output HTML (check for specified options, output all if no options set)
				var wHTML = "";
				if (!options || options.indexOf("t") > -1) {
					// title bar
					if (!title) {
						var wTitle = "Current Weather";
					} else { var wTitle = title; }
					wHTML += '<div id="weatherTitle">' + wTitle + '</div>'
				}				
				wHTML += '<div id="weatherBox">';
				if (!options || options.indexOf("i") > -1) {
					// image
					wHTML += '<img id="weatherImage" src="' + wData[0] + '"/>';
				}
				
				if (!options || options.indexOf("c") > -1) {
					// current conditions
					wHTML += '<span id="weatherCond">' + wData[1] + '</span><br />';
					wHTML += '<span id="weatherTemp">' + wData[2] + '</span><br />';
				}
				if (!options || options.indexOf("w") > -1) {
					// wind
					wHTML += '<span id="weatherWind">Wind: ' + wData[3] + ' ' + wData[4] + 'mph</span>';
				}
				if (options && options.indexOf("1") > -1 && options.indexOf("2") == -1) {
					// 1 day forecast (only if no '2' in options)
					wHTML += '<div id="weatherFore"><h4>Forecast:</h4>';
					wHTML += '<span class="forecastDay">' + wData[5] + '</span><br />';
					wHTML += '<span class="forecastCond">' + wData[6] + '</span><br />';
					wHTML += '<span class="forecastHigh">High:' + wData[7] + '</span><br />';
					wHTML += '<span class="forecastLow">Low:' + wData[8] + '</span><br />';
					wHTML += '</div>';

				}
				if (!options || options.indexOf("2") > -1) {
					// 2 day forecast
					wHTML += '<div id="weatherFore"><h4>Forecast:</h4>';
					wHTML += '<span class="forecastDay">' + wData[5] + '</span><br />';
					wHTML += '<span class="forecastCond">' + wData[6] + '</span><br />';
					wHTML += '<span class="forecastHigh">High:' + wData[7] + '</span><br />';
					wHTML += '<span class="forecastLow">Low:' + wData[8] + '</span><br />';
					wHTML += '<span class="forecastDay">' + wData[9] + '</span><br />';
					wHTML += '<span class="forecastCond">' + wData[10] + '</span><br />';
					wHTML += '<span class="forecastHigh">High:' + wData[11] + '</span><br />';
					wHTML += '<span class="forecastLow">Low:' + wData[12] + '</span><br />';
					wHTML += '</div>';
				}
				wHTML += '</div>';
				document.getElementById("weatherDiv").innerHTML = wHTML;
			}
		}
	}
	request.send(null);

}
