// JavaScript Document
function $(values) {
	return document.getElementById(values);
}

function show_element(id)
{
	if ($(id)) $(id).style.display = 'block';
}

function hide_element(id)
{
	if ($(id)) $(id).style.display = 'none';
}

function inNumOnly(e) {
	var code = e.charCode? e.charCode : e.keyCode;
	// allow backspace, delete, home, end, and return char
	return ( (code>=48 && code<=57) || code==36 || code==35 || code==46 || code==13 || code==8)
}

function strTrim(AStr) {
	return AStr.replace(/^\s+/,"").replace(/\s+$/,"");
}

function GoFavorite(lnk,link2) {
	if(lnk!="") {
		arr = lnk.split("-");
		if(arr[0] != "00") {
			location = arr[1]+"&abddr_id="+arr[0]+link2;
		} else {
			location = arr[1]+link2;
		}
	}
}

function ShowMap() {
	if(document.selFav.value != "") {
		var id = document.selFav.value;
		var ptrn = /^\d+$/;
	    if(ptrn.test(id)) {
			location = "/addressbook/getaddress.php?id="+id;
		} else {
			location = id;
		}
	}
}

function ShowAddress(sdGloBaseUrl,id) {
	if(id != ""){
		location = sdGloBaseUrl + "/addressbook/newaddressbook.php?abaddr_id="+id+"&abedit=1";
	} else {
		alert("Choose Address First");
	}
}

var countryInterval = "";

//For Login Box
function showDropMenu1() {
	show_element('dropMenu1');
	show_element('tblDropMenu1');
}

//For Property Post New Ads
//For Login Box
function showPropAds() {
	show_element('dropMenuPropAds');
	show_element('tblDropMenuPropAds');
}

//For Related Keyword New Design
function show_help(){
	if($('box_search_more_up').style.display=='none'){
		hide_element('box_search_more');
		show_element('box_search_more_up');
		show_element('show_help');
	} else{
		show_element('box_search_more');
		hide_element('box_search_more_up');
		hide_element('show_help');
	}
}

//For Hover SearchBox
function searchbox_hover(element){
	if(element == 1){
		$('box_search_left').style.backgroundPosition = '-7px -543px';
		$('box_search_repeat').style.backgroundPosition = '0px -35px';
		$('box_search_right').style.backgroundPosition = '-7px -581px';
	} else if(mainsearch_focus == false) {
		$('box_search_left').style.backgroundPosition = '0px -543px';
		$('box_search_repeat').style.backgroundPosition = '0px 0px';
		$('box_search_right').style.backgroundPosition = '0px -581px';
	}
}
//For related Keyword
function relatedPlace(id,page) {
	if($('hideRelated').style.display=="none")
	{
		show_element('hideRelated');
		hide_element('showRelated');
		//if ( global['city'] == undefined ) $('moreEg').src='/img/map/dropdown-arrow.gif';
		if ( global['city'] == undefined && (page == undefined || page == 'businessfinder') )  moreEgOver('close',page);
		if(global['fullscreen']){
			$('fs_bg').style.height = '107px';
			$('photo_satelite_map').style.top = '109px';
			$('closefs').style.top = '109px';
		}
	}
	else
	{
		hide_element('hideRelated');
		show_element('showRelated');
		//if ( global['city'] == undefined ) $('moreEg').src='/img/map/dropdown-arrow-clicked.gif';
		if ( global['city'] == undefined  && (page == undefined || page == 'businessfinder') ) moreEgOver('open',page)

		if(global['fullscreen']){
			$('fs_bg').style.height = '144px';
			$('photo_satelite_map').style.top = '146px';
			$('closefs').style.top = '146px';
		}
	}
}

function moreEgOver(type, page)
{
	/*
	if($('moreEg').src.indexOf('dropdown-arrow.gif', 0) > 0 && type=="over")
		$('moreEg').src='/img/map/dropdown-arrow-over.gif';
	else if($('moreEg').src.indexOf('dropdown-arrow-over.gif', 0) > 0 && type=="out")
		$('moreEg').src='/img/map/dropdown-arrow.gif';
	*/
	if( type=="over"){
		if($('moreEg').style.backgroundPosition == "-241px -88px") $('moreEg').style.backgroundPosition = '-253px -81px';
		else if (page=='businessfinder' && $('moreEg').style.backgroundPosition == "-262px -7px") $('moreEg').style.backgroundPosition = '-273px 0px';
	}
	else if(type=="out"){
		if($('moreEg').style.backgroundPosition == "-253px -81px") $('moreEg').style.backgroundPosition = '-241px -88px';
		else if (page=='businessfinder' && $('moreEg').style.backgroundPosition == "-273px 0px") $('moreEg').style.backgroundPosition = '-262px -7px';
	}
	else if(type=="close"){
		if (page=='businessfinder')  $('moreEg').style.backgroundPosition='-241px -88px';
		else $('moreEg').style.backgroundPosition='-241px -88px';
	}
	else if(type=="open"){
		if (page=='businessfinder')  $('moreEg').style.backgroundPosition='-241px -81px';
		else $('moreEg').style.backgroundPosition='-241px -81px';
	}

}

function showMenuCountry(type,interval)
{
	if(type == "show")
		show_element('divCountryBox');
	else
		hide_element('divCountryBox');
}

function showMenuId(type,box,idUp,idDown,flag)
{
	if(type == "show") {
		if(flag == 1) { show_element_ajax(box); }
		show_element(box);
		hide_element(idUp);
		show_element(idDown) ;
	} else {
		hide_element(box);
		hide_element(idDown) ;
		show_element(idUp) ;
	}
}

function show_element_ajax(box)
{
	if (box) {

		var xmlhttp;
		try {
			xmlhttp=new XMLHttpRequest();
		}
		catch (e) {
			try {
				xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
			}
			  catch (e) {
				try {
					xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
				}
				catch (e) {
					alert("Your browser does not support AJAX!");
					return false;
				}
			}
		}
		var url;
		if(global['fullscreen'])
			url= 'http://' + location.hostname + '/asia_travel/mappage/ajax/dropzone.php?fullscreen=1'+ global_clear_cache+ '&v=' + global['v_c'];
		else
			url= 'http://' + location.hostname + '/asia_travel/mappage/ajax/dropzone.php?'+ global_clear_cache+ '&v=' + global['v_c'];
		xmlhttp.onreadystatechange=function() {
			if(xmlhttp.readyState==4) {
				$(box).innerHTML=xmlhttp.responseText;
			  }
		}
		xmlhttp.open("GET",url,true);
		xmlhttp.send(null);
	}

	global['show_ajax_box'] = 1;

}

function showMenuIndonesiaCity(type,interval)
{
	if(type == "show")
		show_element('divCityBox');
	else
		hide_element('divCityBox');
}

function showMenuDistrict(type,interval)
{
	if(type == "show")
		show_element('divDistrictBox');
	else
		hide_element('divDistrictBox');
}

//lavinia
function showMenuLink(type)
{
	if(type == "show")
	{
		hide_element('hideLink');
		show_element('showLink');
	}
	else
	{
		show_element('hideLink');
		hide_element('showLink');
	}
}

//For Feedback Drop Down
function showDropSubmit() {
    obj = document.getElementById('dropSubmit');
    obj.style.display='block';
    obj = document.getElementById('tblDropSubmit');
    obj.style.display='block';
}
function hideDropSubmit() {
    obj = document.getElementById('dropSubmit');
    obj.style.display='none';
    obj = document.getElementById('tblDropSubmit');
    obj.style.display='none';
}

// ismail
function checkMouseLeave (element, evt) {
	var evt = window.event || evt;
  if (element.contains && evt.toElement) {
    return !element.contains(evt.toElement);
  }
  else if (evt.relatedTarget) {
    return !containsDOM(element, evt.relatedTarget);
  }
}

function checkMouseEnter (element, evt) {
	var evt = window.event || evt;
	if (element.contains && evt.fromElement) {
		return !element.contains(evt.fromElement);
	}
	else if (evt.relatedTarget) {
		return !containsDOM(element, evt.relatedTarget);
	}
}

// ismail
function containsDOM (container, containee) {
  var isParent = false;
  do {
    if ((isParent = container == containee))
      break;
		try
		{
			containee = containee.parentNode;
		}
		catch(e)
		{
			containee = null;
		}
  }
  while (containee != null);
  return isParent;
}

//For search
function submitSearch(d)
{
	var page_now = pageIndetify();
	f = document.fSdSearch;
	if(f.q.value == "" || f.q.value == "Find Businesses, Addresses or Products & Services here" || f.q.value == "Find Places, Businesses or Products & Services here")
	{
		if($("travel_from") && $("travel_to")){
			if($("travel_from").value!="" || $("travel_to").value!=""){
				alert ('Please Select Option Below');
			}else{
				alert ('You have not entered a keyword \n\n\n Try any of these keywords:\n\n a. Products and Services e.g Reflexology, Beauty\n b. Company Name e.g Asta Spa, Charles & Keith\n c. Addresses e.g Marina Bay Sands, Orchard Rd\n d. Bus Services Number e.g Bus 14\n e. Any Landmark in Singapore e.g Merlion, Beach');
			}
		}else{
			alert ('You have not entered a keyword \n\n\n Try any of these keywords:\n\n a. Products and Services e.g Reflexology, Beauty\n b. Company Name e.g Asta Spa, Charles & Keith\n c. Addresses e.g Marina Bay Sands, Orchard Rd\n d. Bus Services Number e.g Bus 14\n e. Any Landmark in Singapore e.g Merlion, Beach');
		}
		f.q.value = '';
  		f.q.focus();
		return false;
	}
	else
	{
		//kondisi kalo kluar search result dan dienter, maka hasilnya sama dengan map view onclick
			var q = f.q.value, t,str_t = '';
			
			if (f.search_type)
			{
				t = f.search_type[0].checked ? 1 : 2;
				str_t = t ? '&t='+t : '';
			}
			var tmp_q=q;
			q = encodeURIComponent(q);
			q = q.replace(/%20/gi,"+");
			
			if($("rs_1")){
				if($("mp_rs_1")){
					var curr_page=pageIdentifySearch();
					if( ($("rs_1") && !$("rs_2") ) || $("rs_title_1").innerHTML.toLowerCase() == tmp_q.toLowerCase() ){
						if($("mp_rs_1").href != "javascript:void(0);" && $("mp_rs_1").href!="#" && curr_page!='businessfinder_preview' && curr_page!='businessfinder_company'){
							if(page_now == 'search' || location.pathname=='/'){

								var url = 'http://' + location.hostname + '/asia_travel/search/?q='+q + '' + str_t;
								location.href = url;
							}
							else{
								location.href = $("mp_rs_1").href;
								return false;
							}

						}else if ($("cat_id_1") && !window.MapAPI) {
							if(location.pathname=='/'){ 
								var cate_name=$("rs_title_1").innerHTML;
								cate_name=urlencode(cate_name).replace("%20","+");
								location.href = 'http://'+location.hostname+'/asia_travel/search/?q='+cate_name+'&d=1';
							}else if(curr_page=='businessfinder_preview' || curr_page=='businessfinder_company'){
								location.href = $("cat_id_1").href;
							}
						}else{
							if (!window.MapAPI ){
								if(page_now == 'search'){

									var url = 'http://' + location.hostname + '/asia_travel/search/?q='+q + '' + str_t;
									location.href = url;
								}
								else {
									location.href = $("mp_rs_1").href;
								}
								
							}else{
								$("mp_rs_1").onclick();
								f.q.blur();
							}
							return false;
						}
						return false;
					}
			}}
			
			
			if(f.action.indexOf("company_listing") > 0)
				var url = f.action + '?companyname='+q + '' + str_t;
			else
				var url = f.action + '?q='+q + '' + str_t;
			if (d == 1) url += '&d=1';
			location.href = url;
//		alert(url);
//		f.submit();
	}
}


var mainsearch_focus = false, mainsearch_hover = false, mainsearch_state = 0, flagHeader = 0;

function mainSearchFocus(textbox, td, default_text, keep_text,page)
{
	if (!keep_text && textbox.value == default_text) textbox.value = '';
	textbox.style.color = '#000000';
	mainsearch_focus = true;
	if(page != '')
		updateSearchBox(td,page);
	textbox.select();
}

function mainSearchBlur(textbox, td, default_text,page)
{
	textbox.style.color = '#949494';
	mainsearch_focus = false;
	if(page != '')
		updateSearchBox(td,page);
	if (textbox.value == '') textbox.value = default_text;
}
// V.S.
function updateSearchBox(elm,page)
{
	if (mainsearch_state == 0 && (mainsearch_focus || mainsearch_hover))
	{
		if(page == "map") elm.style.background = 'url(/img/search_bar.png) -92px -42px no-repeat';
		else if (page=='businessfinder') elm.style.background = 'url("/img/map/text-box-hover.gif") no-repeat scroll 0 0 transparent';
		else elm.style.background = 'url(/img/business_finder/searchbar_bizfinder.png) -92px -42px no-repeat';
		mainsearch_state = 1;
	}
	else if (mainsearch_state == 1 && (!mainsearch_focus && !mainsearch_hover))
	{
		if(page == "map") elm.style.background = '';
		else if (page=='businessfinder') elm.style.background = 'url("/img/map/text-box.gif") no-repeat scroll 0 0 transparent';
		else elm.style.background = '';
		//elm.style.background = '';
		//if(page == "map") elm.style.background = '';
		//else elm.style.background = 'url(/img/map/text-box.gif)';
		mainsearch_state = 0;
	}
}

function showFlagDrop()
{
	if(flagHeader == 0)
	{
		$('show_flag').style.display = 'block';
		flagHeader = 1;
	}
	else
	{
		$('show_flag').style.display = 'none';
		flagHeader = 0;
	}
}

function hoverFlag(type,country)
{
	if(type=="hover")
	{
		$('flag'+country).className='imgFlag flag'+country+'Small_hover';
		$('flag'+country+'text').style.color='#000';
	}
	else
	{
		$('flag'+country).className='imgFlag flag'+country+'Small';
		$('flag'+country+'text').style.color='#505050';
	}
}

function detectMapName(x,y)
{
	var x1 = new Array(103.609808585, 106.336713792, 100.183203884, 103.600675, 102.131124378, 99.63196794, 114.438986201,101.366505152,103.848996221); //1
	var y1 = new Array(1.47421033298, -5.90996588413, 5.53096134452, 1.617192, 2.34513687652, 6.48030861, -8.04351119209,3.2765636314,1.21655296465); //2
	var x2 = new Array(104.088063761, 107.315837289, 100.550669928, 104.008627, 102.346197544, 99.94706918, 115.735151469,101.849828314,104.327250213); //3
	var y2 = new Array(1.19945734487, -6.78535607902, 5.21541483438, 1.423502, 2.1495514102, 6.15515457, -8.84114745365,2.71148572587,0.597924020327); //4

	var area = new Array('sg','jkt','png','jb', 'mlk', 'lkw','bali','kl','btm');
	var i=0, n = area.length;
	for(i=0;i<n;i++)
	{
		if (x >= x1[i] && x <= x2[i] && y >= y2[i] && y <= y1[i])
		{
			return area[i];
		}
	}
	return false;
};

function showHideDiv(divShow, divHide)
{
	for(i=0;i<divHide.length;i++)
	{
		hide_element(divHide[i]);
	}
	for(i=0;i<divShow.length;i++)
	{
		show_element(divShow[i]);
	}
}

function css_sprite(css,w, h, li, ti, ld, td, altStyle, ws, hs)
{
	var par = document.createElement("div");
	ld =  (ld == undefined) ? '' : 'left:'+ld+'px;';
	td =  (td == undefined) ? '' : 'top:'+(td)+'px;';
	par.style.cssText = 'position:absolute; '+ld+td+altStyle;
	var d = document.createElement("div");
	d.style.cssText = 'width:'+w+'px; height:'+h+'px; position: relative; overflow:hidden;';
	var f = document.createElement("div");
	altStyle = (altStyle == undefined) ? '' : altStyle;
	f.className = css;
	ws = (ws == undefined)? 1000 : ws;
	hs = (hs == undefined)? 1000 : hs;
	f.style.cssText = 'width: '+ ws +'px; height: '+ hs +'px; position:absolute; left:'+(-li)+'px; top: '+(-ti)+'px;';
	d.appendChild(f);
	par.appendChild(d);

	par.setClassName = function(newClassName)
	{
		par.childNodes[0].childNodes[0].className = newClassName;
	}

	par.setBgPosition = function(left, top)
	{
		par.childNodes[0].childNodes[0].style.left = -left+'px';
		par.childNodes[0].childNodes[0].style.top = -top+'px';
	}

	par.getBgPositionLeft = function()
	{
		return par.childNodes[0].childNodes[0].style.left;
	}

	par.getBgPositionTop = function()
	{
		return par.childNodes[0].childNodes[0].style.top;
	}

	return par;
}

function bookmark(url, title){
	if (window.sidebar) // firefox
		window.sidebar.addPanel(title, url, "http:www.yourwebsiteurl.com");
	else if(window.opera && window.print){ // opera
		var elem = document.createElement('a');
		elem.setAttribute('href',url);
		elem.setAttribute('title',title);
		elem.setAttribute('rel','sidebar');
		elem.click();
	}
	else if(document.all)// ie
		window.external.AddFavorite(url, title);
}

function urlencode (str) {
    str = (str+'').toString();
    return encodeURIComponent(str).replace(/!/g, '%21').replace(/'/g, '%27').replace(/\(/g, '%28').replace(/\)/g, '%29').replace(/\*/g, '%2A').replace(/%20/g, '+');
}
/* start by anton */
function google_adsense()
{
	var ad_id = 'pub-0142502304201029';
	var ad = new Array();		
	var cache;
	var load_google_lib = false;
	
	this.get_format = function(id)
	{
		var format;
		switch(id)
		{
			case 1 : format = '200x200'; break; // SMALL_SQUARE_200x200
			case 2 : format = '468x60'; break; // BANNER_468x60
			case 3 : format = '300x250'; break; // MEDIUM_RECTANGLE_300x250
			case 4 : format = '234x60'; break; // MEDIUM_RECTANGLE_300x250
			default : format = '336x280'; break; // LARGE_RECTANGLE_336x280
		}
		return format;		
	}		   
	
	this.load = function(ad_slot,elm_id, format, hints)
	{			
		var index = ad_slot+'_'+elm_id;			
		if (!ad[index])
		{					    
			var options = {
				'format' : this.get_format(format),
				'ad_slot' : ad_slot, 
				'hints'	: hints
			};			
			ad[index] = new google.ads.Ad(ad_id, elm_id, options);
		} else
		{						
			if (this.e(elm_id).innerHTML == '') 
			{				
				ad[index].setContainer(elm_id);	
			}	
			ad[index].refresh({'hints' : hints});			
		}
	}
	
	this.load_google = function()
	{	
		if (!load_google_lib)
		{
			if (window['google'] == undefined)
			{
				url = 'http://'+location.hostname+'/asia_travel/mappage/ajax/adsense_lib.php';		
				loadScript(url);
			}			
			google.load("ads", "1");	
			load_google_lib = true;	
		}
	}	
	this.e = function(id)
	{
		return document.getElementById(id);
	}
	this.load_google();			
}

/* end by andton */
function showHelpTab(id){
	if(id == 1){
		for(i=1;i<=4;i++){
			if(i == id){
				if($('helpTab'+i)) $('helpTab'+i).style.display='block';
				if($('left'+i)) $('left'+i).className="left_selected";
				if($('menu'+i)) $('menu'+i).className="menu_selected";
				if($('right'+i)) $('right'+i).className="right_selected";
			}else{
				if($('helpTab'+i)) $('helpTab'+i).style.display='none';
				if($('left'+i)) $('left'+i).className="left_close";
				if($('menu'+i)) $('menu'+i).className="menu";
				if($('right'+i)) $('right'+i).className="right_close";
			}
		}
	} else if(id == 2){
		for(i=1;i<=4;i++){
			if(i == id){
				if($('helpTab'+i)) $('helpTab'+i).style.display='block';
				if($('left'+i)) $('left'+i).className="left_selected";
				if($('menu'+i)) $('menu'+i).className="menu_selected";
				if($('right'+i)) $('right'+i).className="right_selected";
			}else{
				if($('helpTab'+i)) $('helpTab'+i).style.display='none';
				if($('left'+i)) $('left'+i).className="left_close";
				if($('menu'+i)) $('menu'+i).className="menu";
				if($('right'+i)) $('right'+i).className="right_close";
			}
		}
	} else if(id == 3){
		for(i=1;i<=4;i++){
			if(i == id){
				$('helpTab'+i).style.display='block';
				$('left'+i).className="left_selected";
				$('menu'+i).className="menu_selected";
				$('right'+i).className="right_selected";
			}else{
				$('helpTab'+i).style.display='none';
				$('left'+i).className="left_close";
				$('menu'+i).className="menu";
				$('right'+i).className="right_close";
			}
		}
	} else if(id == 4){
		for(i=1;i<=4;i++){
			if(i == id){
				$('helpTab'+i).style.display='block';
				$('left'+i).className="left_selected";
				$('menu'+i).className="menu_selected";
				$('right'+i).className="right_selected";
			}else{
				$('helpTab'+i).style.display='none';
				$('left'+i).className="left_close";
				$('menu'+i).className="menu";
				$('right'+i).className="right_close";
			}
		}
	}
}


//aris add
function changeButton(idEl,idE2){
	hide_element(idEl);
	show_element(idE2);
}

function toggleWindow(buttonA, windowA, buttonB, windowB){
	if($(buttonB)){
		if($(buttonB+'_clicked').style.display == 'block'){
			hide_element(buttonB+'_clicked');
			show_element(buttonB);
			hide_element(windowB);
		}
	}
	show_element(buttonA+'_clicked');
	show_element(windowA);
	hide_element(buttonA);
}

function pageIndetify(){
	if(location.pathname.indexOf('/restaurants/')  != -1 || location.pathname.indexOf('/restaurant/')  != -1) return 'restaurant';
	else if(location.pathname.indexOf('/realestate/') != -1) return 'realestate';
	else if(location.pathname.indexOf('/jobs/') != -1)return 'jobs';
	else if(location.pathname.indexOf('/search/')  != -1)return 'search';
	else if(global['preview_bf']) return 'map';
	else if(location.pathname.indexOf('/businessfinder/')  != -1)return 'businessfinder';
	else return 'map';
}
