﻿// JavaScript Document
var xmlWeather;
var infile = "/tc/js/yp_weather.xml";
var url = "http://www.hko.gov.hk/contentc.htm";
var href=document.location.href;
if(href.indexOf("sc.youth.gov.hk/TuniS/")!=-1){
	if(href.indexOf("https://")==-1){
		infile="http://sc.youth.gov.hk/TuniS/www.youth.gov.hk/tc/js/yp_weather.xml"
	}else{
		infile="https://sc.youth.gov.hk/TuniS/www.youth.gov.hk/tc/js/yp_weather.xml"
	}
	url = "http://gb.weather.gov.hk/contentc.htm";
}
function loadWeather()
{
// code for IE
if (window.ActiveXObject)
  {
  xmlWeather=new ActiveXObject("Microsoft.XMLDOM");
  xmlWeather.async=false;
  xmlWeather.load(infile);
  getweatherdata();
  }
// code for Mozilla, Firefox, Opera, etc.
else if (document.implementation &&
document.implementation.createDocument)
  {
  xmlWeather=document.implementation.createDocument("","",null);
  xmlWeather.load(infile);
  xmlWeather.onload=getweatherdata;
  }
else
  {
  alert('Your browser cannot handle this script');
  }
}

function getweatherdata()
{
var x=xmlWeather.documentElement;

//document.write(xmlWeather.documentElement.childNodes.length);
//for (i=0;i<x.childNodes.length;i++)
	//{
	//abc+=xmlWeather.getElementsByTagName("title")[i].childNodes[0].nodeValue;
	var dt=xmlWeather.getElementsByTagName("LastUpdateTime")[0].childNodes[0].nodeValue;
	var wf=xmlWeather.getElementsByTagName("URL")[0].childNodes[0].nodeValue;
	
	var atext;
	
	var curDate;
	var newDate;
	var tempHumid;
	var tempTemper;
	
	if (wf == "pic50"){ atext = "陽光充沛";
	}else if (wf == "pic51"){ atext = "間有陽光";
	}else if (wf == "pic52"){ atext = "短暫陽光";
	}else if (wf == "pic53"){ atext = "間有陽光 幾陣驟雨";
	}else if (wf == "pic54"){ atext = "短暫陽光 有驟雨";
	}else if (wf == "pic60"){ atext = "多雲";
	}else if (wf == "pic61"){ atext = "密雲";
	}else if (wf == "pic62"){ atext = "微雨";
	}else if (wf == "pic63"){ atext = "雨";
	}else if (wf == "pic64"){ atext = "大雨";
	}else if (wf == "pic65"){ atext = "雷暴";
	}else if (wf == "pic70"){ atext = "天色良好";
	}else if (wf == "pic71"){ atext = "天色良好";
	}else if (wf == "pic72"){ atext = "天色良好";
	}else if (wf == "pic73"){ atext = "天色良好";
	}else if (wf == "pic74"){ atext = "天色良好";
	}else if (wf == "pic75"){ atext = "天色良好";	
	}else if (wf == "pic76"){ atext = "大致多雲";
	}else if (wf == "pic77"){ atext = "天色大致良好";	
	}else if (wf == "pic80"){ atext = "大風";
	}else if (wf == "pic81"){ atext = "乾燥";
	}else if (wf == "pic82"){ atext = "潮濕";
	}else if (wf == "pic83"){ atext = "霧";
	}else if (wf == "pic84"){ atext = "薄霧";
	}else if (wf == "pic85"){ atext = "煙霞";
	}else if (wf == "pic90"){ atext = "熱";
	}else if (wf == "pic91"){ atext = "暖";
	}else if (wf == "pic92"){ atext = "涼";
	}else if (wf == "pic93"){ atext = "冷";	
	}		
			
	curDate=dt.substring(0,10);
	newDate=curDate.substring(8,10)+"-"+curDate.substring(5,7)+"-"+curDate.substring(0,4);
	
	//document.getElementById("datentime").innerHTML=dt.substring(0,10);
	document.getElementById("datentime").innerHTML="<span class=\"weatherinfo\">" + newDate + "</span>";
	
	tempHumid = xmlWeather.getElementsByTagName("Measure")[1].childNodes[0].nodeValue;
	tempTemper = xmlWeather.getElementsByTagName("Measure")[0].childNodes[0].nodeValue;

	if (tempTemper != null) {
		document.getElementById("temperature").innerHTML= "<span class=\"weatherinfo\">" + tempTemper + "<sup>o</sup>C</span>";
	}
	if (tempHumid != null) {
		document.getElementById("humidity").innerHTML="<span class=\"weatherinfo\">濕度 " + tempHumid + "% </span>";
	}
	document.getElementById("weatherforecast").innerHTML="<a href=\"" + url + "\" target=\"_blank\"><img src=\"/tc/images/home/weather_icon/" + wf + ".png\" title=\""+ atext +"\" width=\"18\" height=\"18\" border=\"0\"/></a>";

// Weather warning icon goes here
var wi;
var wiout='';
var wn;

for (var i=1;i<12;i++){
	//wn=xmlWeather.getElementsByTagName("Name")[i].childNodes[0].nodeValue;
	try
  {
	wi=xmlWeather.getElementsByTagName("URL")[i].childNodes[0].nodeValue;
	
	if (wi=="tc10"){ atext = "十 號 颶 風 信 號 ";
	}else if (wi=="tc1"){ atext = "一 號 戒 備 信 號 ";
	}else if (wi=="tc3"){ atext = "三 號 強 風 信 號 ";
	}else if (wi=="tc8ne"){ atext = "八 號 東 北 烈 風 或 暴 風 信 號 ";
	}else if (wi=="tc8nw"){ atext = "八 號 西 北 烈 風 或 暴 風 信 號 ";
	}else if (wi=="tc8se"){ atext = "八 號 東 南 烈 風 或 暴 風 信 號 ";
	}else if (wi=="tc8sw"){ atext = "八 號 西 南 烈 風 或 暴 風 信 號 ";
	}else if (wi=="tc9"){ atext = "九 號 烈 風 或 暴 風 增 強 信 號 ";
	}else if (wi=="raina"){ atext = "黃 色 暴 雨 警 告 信 號 ";
	}else if (wi=="rainr"){ atext = "紅 色 暴 雨 警 告 信 號 ";
	}else if (wi=="rainb"){ atext = "黑 色 暴 雨 警 告 信 號 ";	
	}else if (wi=="ts"){ atext = "雷 暴 警 告 ";
	}else if (wi=="ntfl"){ atext = "新 界 北 部 水 浸 特 別 報 告";
	}else if (wi=="landslip"){ atext = "山 泥 傾 瀉 警 告";
	}else if (wi=="sms"){ atext = "強 烈 季 候 風 信 號";
	}else if (wi=="frost"){ atext = "霜 凍 警 告";
	}else if (wi=="firey"){ atext = "黃 色 火 災 危 險 警 告";
	}else if (wi=="firer"){ atext = "紅 色 火 災 危 險 警 告";	
	}else if (wi=="cold"){ atext = "寒 冷 天 氣 警 告";
	}else if (wi=="vhot"){ atext = "酷 熱 天 氣 警 告";
	}else if (wi=="tsunami-warn"){ atext = "海 嘯 警 告";
	}
	
	wiout+="<a href=\"" + url + "\" target=\"_blank\"><img src=\"/tc/images/home/weather_icon/" + wi + ".gif\" title=\""+ atext +"\" width=\"18\" height=\"18\" border=\"0\"/></a>" + "&nbsp;";
  }catch(err){
  }
}

document.getElementById("warningicon").innerHTML=wiout;
//document.getElementById("temperature").innerHTML=xmlWeather.getElementsByTagName("Measure")[i].childNodes[0].nodeValue;
  //}
}