var saved_last_pos = '';
function createLocationBoxBus(content)
{
	if (!document.getElementById('get_location_box'))
	{	
 	 var parent_div = document.getElementById('imgDiv');
	 var box = document.createElement("div");
	 box.align = 'left';
	 box.id = 'get_location_box';	 
	 box.innerHTML = content;
	 parent_div.appendChild(box);
	}
}

function getLocationBoxBus()
{
	if (document.getElementById) {
	var x = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
	}
	if (x)
		{		
			x.onreadystatechange = function()
			{ 
				if (x.readyState == 4 && x.status == 200)
				{			
					closeLoadingLocationBox();
					createLocationBoxBus(x.responseText);
				} else
				{
					showLoadingLocationBox();
				}
			};								
			var last_pos = getLastPos();
			var original_pos = getOriginalPos();			
			var str_same = '';
			if (last_pos.x == original_pos.x && last_pos.y == original_pos.y) str_same ='&same=1';
			saved_last_pos = last_pos;
			last_pos = last_pos.x+","+last_pos.y;
			var level = MapAPI.mapClient.levelIndex;
			var url = "http://"+location.hostname+"/travel/get_location_code.php?last_pos="+last_pos+"&level="+level;
			x.open("GET", url, true);
			x.send(null);					
		}			
	return false;
}

function showLoadingLocationBox()
{
	if (document.getElementById('loading_box'))
	{
		document.getElementById('loading_box').style.display = 'block';
	} else
	{
		var loading_box = document.createElement('div');
		loading_box.id = 'loading_box';
		loading_box.style.cssText = "position:absolute; top:0px; right:0px; width:375px; z-index:1000; height:80px; overflow:hidden;  background-color:#FFFFFF; border:1px solid #CCCCCC;";
		var img = document.createElement('img');
		img.src = 'http://'+location.hostname+'/img/map/loading.gif';
		img.style.cssText = "position:absolute; top:-120px; left:10px;";
		loading_box.appendChild(img);
		
		var close_btn = document.createElement('div');
		close_btn.style.cssText = "position:absolute; top:5px; right:-350px; width:375px; z-index:1000; height:80px;";
		close_btn.innerHTML = '<a href="javascript:void(0);" onclick="javascript:closeLoadingLocationBox();">[x]</a>';
		loading_box.appendChild(close_btn);
		
		var parent_div = document.getElementById('free_widget');
		parent_div.appendChild(loading_box);
	}
}

function closeLoadingLocationBox()
{
	if (document.getElementById('loading_box'))
	{
		document.getElementById('loading_box').style.display = 'none';
	}
}

function getOriginalPos()
{
	return (document.getElementById('original_pos').innerHTML).replace(" ","");
}

function openLocationBoxBus()
{
	if (saved_last_pos == getLastPos())
	{	
		showLocationBoxBus();		
	} else
	{
		if (document.getElementById('get_location_box')) removeElement('get_location_box');
		getLocationBoxBus();		
	}
}

function removeElement(id)	{
	var Node = document.getElementById(id);	
	Node.parentNode.removeChild(Node);
}

function closeLocationBox()
{
	if (document.getElementById('get_location_box'))
	{
		document.getElementById('get_location_box').style.display = 'none';
	}
}

function showLocationBoxBus()
{
	if (document.getElementById('get_location_box'))
	{
		document.getElementById('get_location_box').style.display = 'block';
	}
}

function getLastPos()
{
	var vertex = document.getElementById("last_pos").innerHTML;
	vertex = vertex.split(",");
	return new Vertex(vertex[0],vertex[1]);
}

function showTitle(status)
	{
		if (status == 1)
		{
			document.getElementById('address_info').style.display = 'block';
			document.getElementById('show_indicator').style.display = 'none';
			document.getElementById('hide_indicator').style.display = 'block';
		} else
		{
			document.getElementById('address_info').style.display = 'none';
			document.getElementById('show_indicator').style.display = 'block';
			document.getElementById('hide_indicator').style.display = 'none';
		}
	}
	
function showFare(status)
	{
		if (status == 1)
		{
			document.getElementById('fare_info').style.display = 'block';
			document.getElementById('show_fare').style.display = 'none';
			document.getElementById('hide_fare').style.display = 'block';
		} else
		{
			document.getElementById('fare_info').style.display = 'none';
			document.getElementById('show_fare').style.display = 'block';
			document.getElementById('hide_fare').style.display = 'none';
		}
	}
	

function selectThis(obj)
{
	obj.style.backgroundColor='#3366CC';
	obj.style.color= '#FFFFFF';
}

function deSelectThis(obj)
{
	obj.style.backgroundColor='';
	obj.style.color= '#000000';
}

function submit_route(type)
{
	var f = document.search_form;
	var from = f.new_link_id_1.value;	
	var str_from = f.dd_from.value;	
	var to =  f.new_link_id_2.value;
	var str_to = f.dd_to.value;
	
	if (from == 0 && str_from != '' || to == 0 && str_to != '')
	{
		if(str_from==""){
			alert('Please input start address');	
			f.dd_from.focus();
		}else if(str_to==""){
			alert('Please input end address');
			f.dd_to.focus();
		}else{
			checkValidAddress(str_from,str_to,from,to);
		}		
		
	} else	{
		if ( str_from == '')
		{
			alert('Please input start address');	
			f.dd_from.focus();
		} else if (str_to== '')
		{
			alert('Please input end address');
			f.dd_to.focus();
		}else if (from == to)
		{
			alert('Start address and input address can not be same');
			f.dd_from.focus();
		} else 
		{
			var pref="",type,method_drive = f.method_drive,by="",rbus="",rmrt="",rbmrt="";
			for(var i=0;i<=2;i++)
			{
				if (method_drive[i].checked == true)
				{
					if (method_drive[1].checked == true) 
					{
						by = "MRT";
						rmrt = "best";
						type = "mrt_guide";
					}
					else if (method_drive[2].checked == true) 
					{
						by = "Bus/MRT";
						rbmrt = "best";
						type = "busmrt_guide";
					}
					else 
					{
						by = "Bus";
						type = "bus_guide";
						rbus = method_drive[i].value;
						switch(method_drive[i].value)
						{
							case "min": pref = "minimum_walking_distance"; break;
							case "direct": pref = "direct_service"; break;
							case "custom": pref = "custom"; break;
							default: pref = "minimum_walking_distance"; break;
						}
					}
				}
			}
			var url = "http://"+location.hostname+"/travel/"+type+"/process.php?param="+from+"_to_"+to+"&rbus="+rbus+"&rmrt="+rmrt+"&rbmrt="+rbmrt+"&by="+by+"&link1="+from+"&link2="+to;
			//alert(url);
			//var url = "http://"+location.hostname+"/travel/"+type+"/direction_result/"+from+"_to_"+to+"/";
			//if(pref) url += pref.toLowerCase()+"/"
			location.href = url;
		}
		
		
	}

}

function checkValidAddress(address,address2,link_id_from,link_id_to)
{
	var f = document.search_form;
	if (document.getElementById) {
	var x = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
	}
	if (x)
		{		
			x.onreadystatechange = function()
			{ 
				if (x.readyState == 4 && x.status == 200)
				{	
				
					var pref="",type,method_drive = f.method_drive,by="",rbus="",rmrt="",rbmrt="";
					for(var i=0;i<=2;i++)
					{
						if (method_drive[i].checked == true)
						{
							if (method_drive[1].checked == true) 
							{
								by = "MRT";
								rmrt = "best";
								type = "mrt_guide";
							}
							else if (method_drive[2].checked == true) 
							{
								by = "Bus/MRT";
								rbmrt = "best";
								type = "busmrt_guide";
							}
							else 
							{
								by = "Bus";
								type = "bus_guide";
								rbus = method_drive[i].value;
								switch(method_drive[i].value)
								{
									case "min": pref = "minimum_walking_distance"; break;
									case "direct": pref = "direct_service"; break;
									case "custom": pref = "custom"; break;
									default: pref = "minimum_walking_distance"; break;
								}
							}
						}
					}
					
					result = x.responseText;	
					arr_result = result.split("_");	
					from = arr_result[0];
					to = arr_result[1];
					if(result!=""){
						if (pref == "certain"){
							alert('Please select your preferred expressway.');
						}else{
							if(from>0 && to>0)	{
								var url = "http://"+location.hostname+"/travel/"+type+"/process.php?param="+from+"_to_"+to+"&rbus="+rbus+"&rmrt="+rmrt+"&rbmrt="+rbmrt+"&by="+by+"&link1="+from+"&link2="+to;
								//var url = "http://"+location.hostname+"/routing/automobile_guide/"+from+"_to_"+to+"/"+pref.toLowerCase()+"/";
								location.href = url;
							}else{
								if(from==""){
									alert('Sorry, your start address can not be found.');
								}else{
									alert('Sorry, your end address can not be found.');
								}
							}
						}
					}else{
						alert('Please enter your address correctly.');
					}
			
					
					return true;
				} else
				{
					//showLoadingLocationBox(type);
				}
			};						
			address = encodeURIComponent(address); 
			address = address.replace(/%20/gi,"+");
			
			address2 = encodeURIComponent(address2); 
			address2 = address2.replace(/%20/gi,"+");
			var url = "http://"+location.hostname+"/routing/get_valid_address.php?address="+address+"&address2="+address2+"&link_id_from="+link_id_from+"&link_id_to="+link_id_to;
			x.open("GET", url, true);
			x.send(null);					
		}			
	return false;
}

function travelNodeMouseOver(type,id)
{
  var p = Math.ceil(id / 6);
  /* if (p != curr_dd_page)
  {
	  go_to_page(p,type,id);
	  curr_dd_page = p;
  } else
  { */
	  var elm = $('trNav' + id);
	  if (elm != null) 
	  {
		elm.bgColor = '#ffffaa';
		if ($('map_icon_'+id))
			$('map_icon_'+id).src ='/img/dd_search/digit/'+id+'_red.gif';
		$('img_num_'+id).src = '/img/dd_search/digit/'+id+'_red.gif';
	  }  
  //}
}

function travelNodeMouseOut(type,id)
{
  var elm = $('trNav' + id);
  if (elm != null) 
  {
	elm.bgColor = '#ffffff';
	if ($('map_icon_'+id))
		$('map_icon_'+id).src ='/img/dd_search/digit/'+id+'.gif';
	$('img_num_'+id).src = '/img/dd_search/digit/'+id+'.gif';
  }
}

function setCenterBus(x,y)
{
	var level = MapAPI.mapClient.levelIndex;
	if (level <= 4)
	{
		var wgs = new WGSProjection();
		var xy = wgs.utmToGeo(x, y, 48, 0);
		y = xy.x;
		x = xy.y;
	} 
	setCenter(x,y);
}

function goPhase(pid1, aid1, pid2, aid2, phase)
{
	if (document.getElementById) {
		var x = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
	}
	if (x)
	{		
		var phase_elm = $('busmrt_phase');
		var spans = phase_elm.getElementsByTagName('span');
		x.onreadystatechange = function()
		{ 
			if (x.readyState == 4 && x.status == 200)
			{
				var lhs = $('bus_mrt_div');
				lhs.innerHTML = x.responseText;
				
				var icons = MapAPI.mapDraw.panelIcon.icons;
				for (var i = 4, len = icons.length; i < len; i++)
				{
					MapAPI.mapDraw.panelIcon.removeIcon(icons[i]);
				}
				icons.splice(4, icons.length - 4);
				var new_icons = eval('(' + $('dd_checkpoints').innerHTML + ')');
				for (var i = 0, len = new_icons.length; i < len; i++)
				{
					var new_icon = new_icons[i];
					var icon = MapAPI.mapDraw.addIcon(new_icon.x, new_icon.y, 17, 17, '', '', 'http://' + location.hostname + '/img/dd_search/digit/' + new_icon.i + '.gif');
					icon.obj.id = 'map_icon_' + new_icon.i;
					VEvent.addListener(icon, 'mouseover',  function() {var tmp = this.id.split('_');travelNodeMouseOver('bus_mrt', tmp[2]);});
					VEvent.addListener(icon, 'mouseout',  function() {var tmp = this.id.split('_');travelNodeMouseOut('bus_mrt', tmp[2]);});
					delete new_icon, icon;
				}
				MapAPI.mapDraw.panelIcon.updatePosition();
				
				for (var i = 1, len = spans.length; i < len; i++)
				{
					if (i != phase)
					{
						spans[i].innerHTML = "<a href='javascript:void(0);' onclick='goPhase(" + pid1 + ", " + aid1 + ", " + pid2 + ", " + aid2 + ", " + i + ")'>" + spans[i].innerHTML + "</a>";
					}
				}
			}
		}
		var url = "http://"+location.hostname+"/travel/ajax/busmrt/busmrt_detail2.php?p1="+pid1+"&a1="+aid1+"&p2=" + pid2 + "&a2=" + aid2 + "&phase="+phase;
		x.open("GET", url, true);
		x.send(null);
		
		for (var i = 1, len = spans.length; i < len; i++)
		{
			if (i == phase)
			{
				var tmp = spans[i].innerHTML;
				tmp = tmp.replace(/<\/a>/i, '');
				spans[i].innerHTML = tmp.replace(/<a[^>]*>/i, '');
			}
		}
	}
}

function swap_location()
{
	var f = document.search_form;
	var from = f.new_link_id_1.value;	
	f.new_link_id_1.value = f.new_link_id_2.value;
	f.new_link_id_2.value = from;
	
	from = f.dd_from.value;
	f.dd_from.value = f.dd_to.value;
	f.dd_to.value = from;

}