// holds an instance of XMLHttpRequest
var xmlHttp = createXmlHttpRequestObject()
var pages=0;
var opacitys=0;
// creates an XMLHttpRequest instance
function createXmlHttpRequestObject()
{
	// will store the reference to the XMLHttpRequest object
	var xmlHttp;
	try
	{
		xmlHttp = new XMLHttpRequest();
	}
	catch(e)
	{
		// assume IE6 or older
		var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
																		"MSXML2.XMLHTTP.5.0",
																		"MSXML2.XMLHTTP.4.0",
																		"MSXML2.XMLHTTP.3.0",
																		"MSXML2.XMLHTTP",
																		"Microsoft.XMLHTTP");
		// try every prog id until one works
		for(i=0;i<XmlHttpVersions.length && !xmlHttp;i++)
		{
			try
			{
				// try to create XMLHttpRequest object
				xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
			}
			catch(e){}
		}
	}
	// return the created object or display an error message
	if (!xmlHttp)
		alert("Error creating the XMLHttpRequest object.");
	else
		return xmlHttp;
}

// read a file from the server
function weather(page,opacity)
{
	// only continue if xmlHttp isn't void
	if(xmlHttp)
	{
		try
		{
			// initiate reading a file from the server
			if(page!=0)
			{							
				pages=page;
				opacitys=opacity;
				xmlHttp.open("GET","Weather.php?page="+page,true);
				xmlHttp.onreadystatechange = handleRequestStateChanges;
				xmlHttp.send(null);			
			}
		}
		// display the error in case of failure
		catch(e)
		{
			alert("Can't connect to server:\n" + e.toString());
		}
	}
}

function handleRequestStateChanges()
{
	// when readyState is 4, we are ready to read the server response
	if(xmlHttp.readyState == 4)
	{
		// continue only if HTTP status is "OK"
		if(xmlHttp.status == 200)
		{
			// do something with the response from the server
			handleServerResponses();
		}
		else
		{
			// display status message
			alert("There was a problem retrieving the data:\n" +
			xmlHttp.statusText);
		}
	}
}

// handles the response received from the server
function handleServerResponses()
{
	var nums=0;
	// read the message from the server
	var xmlResponse = xmlHttp.responseXML;
	// catching potential errors with IE and Opera
	if (!xmlResponse || !xmlResponse.documentElement)
	throw("Invalid XML structure:\n" + xmlHttp.responseText);
	// catching potential errors with Firefox
	var rootNodeName = xmlResponse.documentElement.nodeName;
	if (rootNodeName == "parsererror") throw("Invalid XML structure");
	// obtain the XML's document element
	xmlRoot = xmlResponse.documentElement;
	// obtain arrays with image name
	nameArray = xmlRoot.getElementsByTagName("name");
	minArray = xmlRoot.getElementsByTagName("min");
	maxArray = xmlRoot.getElementsByTagName("max");
	imageArray = xmlRoot.getElementsByTagName("image");	
	
	for(i=0;i<nameArray.length;i++)
	{
		if(i==3)
		{
				var nums=nameArray.item(i).firstChild.data;
		}
		else
		{
			j=i+1;
			divcity = document.getElementById("weathercity"+j);
			divcity.innerHTML ="<span style='opacity:" + (opacitys/100) + ";filter:alpha(opacity=" + opacitys + ")'>" + nameArray.item(i).firstChild.data + "</span>";
			divminmax = document.getElementById("weatthermaxmin"+j);
			divminmax.innerHTML ="<span style='opacity:" + (opacitys/100) + ";filter:alpha(opacity=" + opacitys + ")'>" + minArray.item(i).firstChild.data + " - " + maxArray.item(i).firstChild.data + " C</span>";
			divimage = document.getElementById("weatherimage"+j);
			divimage.innerHTML = "<img src='./images/" +imageArray.item(i).firstChild.data+ "' style='opacity:" + (opacitys/100) + ";filter:alpha(opacity=" + opacitys + ")'/>";
		}
	}	
	
	var num=pages*3;
	if(opacitys!=20)
	{
		opacitys=opacitys-10
		setTimeout('weather('+pages+','+opacitys+')', 250);
	}
	else
	{
		if(num==nums)
		{
			setTimeout('weather(1,100)', 250);
		}
		else
		{
			pages++;
			setTimeout('weather(' + pages + ',100)', 250);
		}
	}
}