
var thelng;
var gburl = "gb.kmb.hk/TuniS/"; 
var d = document;

function init()
{
	setFieldFocus();
}

function setFieldFocus()
{
	var input = d.getElementsByTagName('input');
	
	for (var i=0; i<input.length; i++)
	{
		if (input[i].type != 'text')			continue;
		//if (input[i].className == 'routeField')	continue;
		
		input[i].onfocus = function()
		{
			if (this.value == this.defaultValue)	this.value = '';
		}
		
		input[i].onblur = function()
		{
			if (this.value == '')	this.value = this.defaultValue;
		}
	}
}

function GetParam(name)
{
	var start=location.search.indexOf("?"+name+"=");
	if (start<0) start=location.search.indexOf("&"+name+"=");
 	if (start<0) return '';
 	start += name.length+2;
 	var end=location.search.indexOf("&",start)-1;
 	if (end<0) end=location.search.length;
 	var result=location.search.substring(start,end);
 	var result='';
 	for(var i=start;i<=end;i++)
 	{
 		var c=location.search.charAt(i);
 		result=result+(c=='+'?' ':c);
 	}
 	//alert(unescape(result));
 	return unescape(result);
}

function checkURL(array)
{
	var url = window.top.location.href;
	var id;
	var va = [["/", "/"], ["=", ""]];
	
	for (var item=0; item<va.length; item++)
	{
		for (var i=0; i<array.length; i++)
		{
			if (url.indexOf((va[item][0] + array[i] + va[item][1])) >= 0)
			{
				id = array[i];
				break;
			}
		}
	}
	
	return id;
}

/*Change language*/
function changeLanguage(target)
{
	var array = new Array("en", "tc", "gb");
	var nowL = checkURL(array);
	var va = [["/", "/"], ["=", ""]];
	var url = window.top.location.href;
	if (target == "en") url = url.replace(gburl, "");
	
	for (var item=0; item<va.length; item++)
	{
		if (url.indexOf((va[item][0] + nowL + va[item][1])) >= 0)
		{
			url = url.replace((va[item][0] + nowL + va[item][1]), (va[item][0] + target + va[item][1]));
			break;
		}
	}
	if (url.substring((url.length-1), url.length) == '#')	url = url.substring(0, (url.length-1))

	
	window.top.location.href = url;
}

/*Submit Search*/
function gosearch(lng){
	var theForm = document.form0;
	
	if (theForm.busno.value!=''){
		if (theForm.busno.value=='' || theForm.busno.value=="輸入路線號碼"){
			alert('請輸入巴士路線號碼。');
		}else{
			theForm.action = "/"+lng+"/result.html";
			theForm.submit();
		}
	}
}	

function p2psearch(go){
	var PForm = document.form1;
	
	var codelist = PForm.fr_dest.value + "|" + PForm.fr_area.value + "|" + PForm.to_dest.value  + "|" + PForm.to_area.value;
	var codearr = codelist.split('|');
	
	
	if (go){
		if (PForm.to_area.value == ''){
			alert('你並未完成選擇終點，請再選擇。');
			PForm.to_area.focus();
		}else if (PForm.fr_area.value == PForm.to_area.value){
			alert('起點不能與終點相同，請再選擇。');
		}else{
			window.location = "/"+thelng+"/result.html?"+codelist;
			//PForm.action = "/"+thelng+"/result.html?"+codelist;
			//PForm.submit();
		}
	}else{
		window.location = "/"+thelng+"/index.html?"+codelist;
		//PForm.action = "/"+thelng+"/index.html?"+codelist;
		//PForm.submit();
	}	
}

function ChiToogle(){
	var cururl = document.location.href;
	var where_is = cururl.indexOf(gburl);
		
	cururl = cururl.replace("/en", "/tc");	
	var newurl = "";
	if (where_is<0){
		cururl = cururl.replace("http://", "");
		newurl = "http://" + gburl + cururl;
	}else{
		cururl = cururl.replace(gburl, "");
		newurl = cururl;
	}	
	document.location = newurl;
}

