var xmlHttp;
window.onerror = unAvail;
function unAvail(){
	str="<img src=\"images/weather/3200.gif\" alt=\"Currently not available\"/>";
	document.getElementById("weatherdiv").innerHTML=str;
	return true;
}
function showHint()
{
	dt = new Date();
   	xmlHttp=GetXmlHttpObject();   	
	if (xmlHttp==null)
	{		
		return;
	} 
	var mon = dt.getMonth();
	if(parseInt(mon)<=9)mon="0"+mon;
	var dat = dt.getDate();
	if(parseInt(dat)<=9)dat="0"+dat;
	
	var url="weatherxml.php?sid="+dt.getFullYear()+mon+dat+dt.getHours();
	//alert(url);
	xmlHttp.onreadystatechange=stateChanged 
	xmlHttp.open("GET",url,true)	
	xmlHttp.send(null)
} 

function stateChanged() 
{ 
	var prefix = "yweather:";
	var isIE = (document.all);
	if (xmlHttp.readyState==4 )
	{ 
	    var xmlDoc=xmlHttp.responseXML;	    
	    //alert(xmlDoc) ;
	     var humidity;
	    var sunrise;
	    var sunset;
	    var direction;
	    var speed;
	    var visibility;
	    var unitspeed;
	    var unitdistance;
	    var text,temp, lastdate,condCode;
	    if(isIE){
	    	humidity=xmlDoc.getElementsByTagName(prefix+'atmosphere')[0].getAttribute('humidity');
		    sunrise=xmlDoc.getElementsByTagName(prefix+'astronomy')[0].getAttribute('sunrise');
		    sunset=xmlDoc.getElementsByTagName(prefix+'astronomy')[0].getAttribute('sunset');
		    direction=xmlDoc.getElementsByTagName(prefix+'wind')[0].getAttribute('direction');
		    speed=xmlDoc.getElementsByTagName(prefix+'wind')[0].getAttribute('speed');
		    visibility=xmlDoc.getElementsByTagName(prefix+'atmosphere')[0].getAttribute('visibility')/100;
		    unitspeed=xmlDoc.getElementsByTagName(prefix+'units')[0].getAttribute('speed');
		    unitdistance=xmlDoc.getElementsByTagName(prefix+'units')[0].getAttribute('distance');
		    text=xmlDoc.getElementsByTagName(prefix+'condition')[0].getAttribute('text');
		    temp = xmlDoc.getElementsByTagName(prefix+'wind')[0].getAttribute('chill');
		    lastdate = 	xmlDoc.getElementsByTagName(prefix+'condition')[0].getAttribute('date');
			condCode = xmlDoc.getElementsByTagName(prefix+'condition')[0].getAttribute('code');
	    }else{	    
		    humidity=xmlDoc.getElementsByTagName('atmosphere')[0].getAttribute('humidity');
		    sunrise=xmlDoc.getElementsByTagName('astronomy')[0].getAttribute('sunrise');
		    sunset=xmlDoc.getElementsByTagName('astronomy')[0].getAttribute('sunset');
		    direction=xmlDoc.getElementsByTagName('wind')[0].getAttribute('direction');
		    speed=xmlDoc.getElementsByTagName('wind')[0].getAttribute('speed');
		    visibility=xmlDoc.getElementsByTagName('atmosphere')[0].getAttribute('visibility')/100;
		    unitspeed=xmlDoc.getElementsByTagName('units')[0].getAttribute('speed');
		    unitdistance=xmlDoc.getElementsByTagName('units')[0].getAttribute('distance');
		    text=xmlDoc.getElementsByTagName('condition')[0].getAttribute('text');
		    temp = xmlDoc.getElementsByTagName('wind')[0].getAttribute('chill');
		    lastdate = 	xmlDoc.getElementsByTagName('condition')[0].getAttribute('date');
			condCode = 	xmlDoc.getElementsByTagName('condition')[0].getAttribute('code');
	    }
	    
	  	var newdate = lastdate.substring(lastdate.indexOf(dt.getFullYear())+5);
	    var dir
	    if(direction<=23)
	         dir="N";
	    else if(direction>23 && direction<=68)
	         dir="NE";
	    else if(direction>68 && direction<=113)
	         dir="E";
	   else if(direction>113 && direction<=158)
	         dir="SE";
	    else if(direction>158 && direction<=203)
	         dir="S";
	   else if(direction>203 && direction<=248)
	         dir="SW";
	    else if(direction>248 && direction<=293)
	         dir="W";
	   else if(direction>293 && direction<=338)
	         dir="NW";
	   else if(direction>338 && direction<=360)
	         dir="N"
	   var humidity=humidity+"%";
       var wind=dir+ " "+speed+" "+unitspeed;
	   visibility=visibility+" "+unitdistance;
	   str ="<table width=\"280\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\" class=\"txt02\">";
	   str +="<tr><td align=\"left\" colspan=2><span class=\"txt03\">Conditions at :</span> "+newdate+"  </td></tr>";
	   str +="<tr><td align=\"left\"><span class=\"txt03\">Temp:</span> "+temp+" &deg;C </td><td align=\"left\"><span class=\"txt03\">Humidity:</span> "+humidity+" </td></tr>";	   
	   str +="<tr><td align=\"left\"><span class=\"txt03\">Visibility:</span> "+visibility+"</td><td align=\"left\" rowspan=4><img src=\"images/weather/"+condCode+".gif\" alt=\""+text+"\"/></td></tr>";
	    str +="<tr><td align=\"left\" ><span class=\"txt03\">Wind:</span> "+wind+"</td></tr>";
	    str +="<tr><td align=\"left\" ><span class=\"txt03\">Sunrise:</span> "+sunrise+"</td></tr>";
	   str +="<tr><td align=\"left\" ><span class=\"txt03\">Sunset:</span> "+sunset+"</td></tr>";
	   str +="</table>";
	   document.getElementById("weatherdiv").innerHTML=str;	   		    
    } 
} 

function GetXmlHttpObject()
{ 
	var objXMLHttp=null
	if (window.XMLHttpRequest)
	{
		objXMLHttp=new XMLHttpRequest()
	}
	else if (window.ActiveXObject)
	{
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
	}
	return objXMLHttp
} 