// JavaScript Document
var xmlhttp
function trim(s)
{
	var l=0; var r=s.length -1;
	while(l < s.length && s[l] == ' ')
	{	l++; }
	while(r > l && s[r] == ' ')
	{	r-=1;	}
	return s.substring(l, r+1);
}

function showLoginBox() {
	document.getElementById("loginBox").style.visibility="visible";
}

function hideLoginBox() {
	document.getElementById("loginBox").style.visibility="hidden";
}

function procGetWeather(lang) {
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null)
	{
		  alert ("Your browser does not support AJAX!");
		  return;
	}
	var url="/inc/getWeather.php";
	url=url+"?lang="+lang;
	url=url+"&sid="+Math.random();
	xmlhttp.onreadystatechange=getWeatherStateChanged;
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}

function getWeatherStateChanged()
{
	if (xmlhttp.readyState==4)
	{
		document.getElementById("menuWeather").innerHTML=xmlhttp.responseText;
	}
}

function procLogout() {
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null)
	{
		  alert ("Your browser does not support AJAX!");
		  return;
	}
	var url="/inc/logout.php";
	url=url+"?sid="+Math.random();
	xmlhttp.onreadystatechange=function() {
	if (xmlhttp.readyState==4)
		{
			document.getElementById("menuLoginMsg").innerHTML=xmlhttp.responseText;
			window.location="/en/";
		}
	};
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
	
}

function procLogin()
{	
	xmlhttp=GetXmlHttpObject();
	login=document.getElementById("login").value;
	password=document.getElementById("password").value;
	if (xmlhttp==null)
	{
		  alert ("Your browser does not support AJAX!");
		  return;
	}
	var url="/inc/login.php";
	var parm="login="+login;
	parm=parm+"&password="+password;
	parm=parm+"&sid="+Math.random();
	//url=url+"?login="+login;
	//url=url+"&password="+password;
	//url=url+"&sid="+Math.random();
	
	
	
	
	/*xmlhttp.onreadystatechange=function() {
	if (xmlhttp.readyState==4)
		{	
			if (xmlhttp.responseText=="error") {
				alert ('Login failure: Either email address or password is invalid');
			} else {
				document.getElementById("menuLoginMsg").innerHTML=xmlhttp.responseText;
				window.location.reload();
			}
		}
	};
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
	*/
	
	xmlhttp.open("POST",url,true);
	xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
	xmlhttp.setRequestHeader("Content-length", parm.length);
	xmlhttp.setRequestHeader("Connection", "close");
	
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4) {
			if (xmlhttp.responseText=="error") {
				alert ('Login failure: Either email address or password is invalid');
			} else {
				document.getElementById("menuLoginMsg").innerHTML=xmlhttp.responseText;
				window.location.reload();
			}
		}
	};
	
	xmlhttp.send(parm);
	hideLoginBox();
}

function procResetPW() {
	login=document.getElementById("login").value;
	if (login=="") {
		alert ("Please enter the email");
	} else {
		document.body.style.cursor="wait";
		document.getElementById("ResetPWBtn").disabled=true;
		resetPWhttp=GetXmlHttpObject();
		
		if (resetPWhttp==null) {
			  alert ("Your browser does not support AJAX!");
			  return;
		}
		
		var url="/eng/resetPassword.php";
		url=url+"?id="+login;
		url=url+"&sid="+Math.random();
		
		resetPWhttp.onreadystatechange=function() {
			if (resetPWhttp.readyState==4)
			{
				document.body.style.cursor="default";
				document.getElementById("ResetPWBtn").disabled=false;
				alert (resetPWhttp.responseText);
			}
		};
		resetPWhttp.open("GET",url,true);
		resetPWhttp.send(null);
	}
}

function GetXmlHttpObject()
{
	if (window.XMLHttpRequest)
	{
	  // code for IE7+, Firefox, Chrome, Opera, Safari
	  return new XMLHttpRequest();
	}
	if (window.ActiveXObject)
	{
	  // code for IE6, IE5
	  return new ActiveXObject("Microsoft.XMLHTTP");
	}
return null;
}

function changeLanguage(f) {
	var a=window.top.location.href;
	if (f=="en") {
		a=a.replace("/tc/","/en/");
	} else {
		a=a.replace("/en/", "/tc/");
	}
	window.top.location.href=a;
}

