/*******************************************************************
********************************************************************
************************** Script written by Xavier BIRGY **********
********************************************************************
*******************************************************************/

//BASE FUNCTIONS ---------------------------------------
function GetId(id){
	return document.getElementById(id);
}


function getById(a_id){
	if (document.getElementById){
	  return document.getElementById(a_id);
	}
	else if (document.all){
	  return document.all[a_id];
	}
	else if (document.layers){
	  return document.layers[a_id];
	}
	return null;
}


function HideDiv(Div){
	document.getElementById(Div).style.display = 'none';
}


function inner(Div,content){
	document.getElementById(Div).innerHTML = content;
}

function closediv(div){
	document.getElementById(div).style.display = 'none'; 
}
//-----------------------------------------------------------------




//InfoBuble ----------------------------
var i=false;
function montre(text) {
  if(i==false) {
	  GetId("curseur").style.visibility="visible";
	  GetId("curseur").innerHTML = text;
	  i=true;
  }

}

function cache() {
	if(i==true) {
		GetId("curseur").style.visibility="hidden"; 
		i=false;
	}
}

function move (e) {
	if (i) {
		if(navigator.appName!="Microsoft Internet Explorer"){ 
			GetId("curseur").style.left=e.pageX + 5+"px";
			GetId("curseur").style.top=e.pageY + 10+"px";
		}else{ 
			if(document.documentElement.clientWidth>0){
				GetId("curseur").style.left=20+event.x+document.documentElement.scrollLeft+"px";
				GetId("curseur").style.top=10+event.y+document.documentElement.scrollTop+"px";
			}else{
				GetId("curseur").style.left=20+event.x+document.body.scrollLeft+"px";
				GetId("curseur").style.top=10+event.y+document.body.scrollTop+"px";
			}
		}
	}
}

document.onmousemove=move;
//-----------------------------------------------------




//AJAX ///////////////////////////////////////////////////////////////////////

var message = '<img src="http://www.yellow.co.th/img/loading.gif" align="absmiddle">';

function AJAX(a_sURL, a_sDiv, message) {
	
    if (a_sURL != undefined){
      this.m_sURL = a_sURL;
    }
    
    if (a_sDiv != undefined){
      this.m_sDiv = a_sDiv;
    }
    
    if (this.m_Request != undefined){
      this.m_Request.abort();
      delete this.m_Request;
    }
    
    this.m_Request = this.createReqestObject();
    
    var m_This = this;
    
    this.m_Request.onreadystatechange = function() {m_This.handleResponse()};
    this.m_Request.open("GET", this.m_sURL, true);
    this.m_Request.send(null);
}

AJAX.prototype.m_sURL = undefined;
AJAX.prototype.m_sDiv = undefined;
AJAX.prototype.m_Request = undefined;

AJAX.prototype.createReqestObject = function(){
var req;

try {// Mozilla, Firefox, Opera and the rest of the world,,.
  req = new XMLHttpRequest();
}

catch (error) {
  try { // IE
	req = new ActiveXObject("Microsoft.XMLHTTP");
  }
  catch (error) {
	try { // IE
	  req = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch (error) {
	  req = false;
	}
  }
}

return req;
}

AJAX.prototype.handleResponse = function() {
	
	if (this.m_Request.readyState == 4){
	  
	  if (this.m_Request.status == "200" || this.m_Request.status == "0"){
		var content = getById(this.m_sDiv);
		content.innerHTML = this.m_Request.responseText;
		if (this.onDraw != undefined){
		  this.onDraw();
		}
	  }else{  
		if (this.onError != undefined){
		  this.onError({status:this.m_Request.status, statusText:this.m_Request.statusText});
		}
	  }
	
	  delete this.m_Request;
	  
	}else{
		
		inner(this.m_sDiv,message);
		opacity(this.m_sDiv, 0);
		
	}
	
}
/////////////////////////////////////////////////////////////////




//SEARCH ############################################################

//What suggestions div ---------------------------------------------
function AutoInput(Kw,DivId){
	if(Kw.length > 2 && document.Search.WhatWho[0].checked == true){
		document.getElementById(DivId).style.display = 'block';
		var URL = 'http://www.yellow.co.th/Div_What.php?Kw=' + Kw;
		var ajax = new AJAX(URL, DivId, "<img src='http://www.yellow.co.th/img/ajax-loader-small.gif' align='absmiddle'> searching for categories matching '" +Kw+ "'");
	}
}
//------------------------------------------------------------------


//Where suggestions div ---------------------------------------------
function AutoInputWhere(Kw,DivId){
	if(Kw.length > 2){
		document.getElementById(DivId).style.display = 'block';
		var URL = 'http://www.yellow.co.th/Div_Where.php?Kw=' + Kw;
		var ajax = new AJAX(URL, DivId, "<img src='img/ajax-loader-small.gif' align='absmiddle'> searching for location matching '" +Kw+ "'");
	}
}
//-------------------------------------------------------------------


var whereDefaultValue = 'business location: e.g. \'province\' or  \'city\' or  \'zip\'...';
var whatDefaultValue  = 'business type: e.g. \'cars\' or  \'hotel\' or  \'insurance\'...';
var whoDefaultValue   = 'business name: e.g. \'Carrefour\' or  \'Amari\' or  \'Allianz\'...';


function WhatOrWho(foc){
	
	document.Search.what.style.background  = '#ffffff';
	
	if(document.Search.WhatWho[0].checked == true){
		inner('WhatWhoDiv','What?');
		//document.getElementById('WhatWhoDiv').innerHTML = 'What?';
		var whoWhatDefaultValue = whatDefaultValue;
	}else{
		inner('WhatWhoDiv','Who?');
		//document.getElementById('WhatWhoDiv').innerHTML = 'Who?';
		var whoWhatDefaultValue = whoDefaultValue;
	}
	
	if(foc == 'y' && (document.Search.what.value == whoDefaultValue
					  || document.Search.what.value == whatDefaultValue
					  || document.Search.what.value == '')
					  ){
		document.Search.what.value     = '';
		SwitchStyle('what','homeWhatWhereFocus');
		//document.Search.what.className = 'homeWhatWhereFocus';
	}else if(foc == 'n'){
		SwitchStyle('what','homeWhatWhereDefault');
	    //document.Search.what.className = 'homeWhatWhereDefault';
		document.Search.what.value     = whoWhatDefaultValue;
	}
	
}


function WhereDef(foc){
	
	document.Search.where.style.background  = '#ffffff';
	
	if(foc == 'y' && document.Search.where.value == whereDefaultValue){
		document.Search.where.value     = '';
		//document.Search.where.className = 'homeWhatWhereFocus';
		SwitchStyle('where','homeWhatWhereFocus');
	}else if(foc == 'n'){
		SwitchStyle('where','homeWhatWhereDefault');
	   // document.Search.where.className = 'homeWhatWhereDefault';
		document.Search.where.value     = whereDefaultValue;
	}
	
}


function fillWhat(content,id){
	document.Search.what.value   = content;
	document.Search.idCat.value  = id;
	closediv('DivwhatR');
	document.Search.GoWhat.value = '1';
}


function fillWhere(content,idProvince,idDistrict,idSubDistrict,zip){
	document.Search.where.value          = content;
	document.Search.idProvince.value     = idProvince;
	document.Search.idDistrict.value     = idDistrict;
	document.Search.idSubDistrict.value  = idSubDistrict;
	document.Search.zip.value            = zip;
	closediv('DivwhereR');
	document.Search.GoWhere.value = '1';
}

//Regions Load div ---------------------------------------------
function loadRegions(Div){
		var mess = "<img src='http://www.yellow.co.th/img/loading.gif' align='absmiddle'>";
		document.getElementById(Div).style.display = 'block';
		opacity(Div,0);
		inner(Div,mess);
		var URL = 'index_regions_load.php';
		var ajax = new AJAX(URL, Div, mess);
}
//-------------------------------------------------------------------


//SUBMIT -------------------------------------------------------------------------------------
function submitSearch(){

	if(document.Search.where.value == '' || document.Search.where.value == whereDefaultValue){
		document.Search.where.style.background  = '#ffba00';
		alert ('You must specify location to process search...');
	}else if (document.Search.what.value == ''
			  || document.Search.what.value == whatDefaultValue
			  || document.Search.what.value == whoDefaultValue){
		document.Search.what.style.background  = '#ffba00';
		alert ('You must specify business type or name before processing search...');		
	}else{
		document.Search.submit();	
	}
	
}
//--------------------------------------------------------------------------------------------

//####################################################################




//ALPHA FUNCTION --------------------------------------------
function opacity(Div, alpha){
	if (navigator.appName!="Microsoft Internet Explorer") {
		alpha = (!alpha ? 0 : alpha);
		var target = document.getElementById(Div);
		target.style.opacity = (alpha/100);
		target.style.filter = 'alpha(opacity='+alpha+')';
		if(alpha < 100){
			setTimeout('opacity("'+Div+'", '+(alpha+20)+')', 100);
		}
	}
}
//-----------------------------------------------------------


//MENU ALPHA SHOW / HIDE DIV ------------------------------------
function showHideDiv(Div){
	
	var target = document.getElementById(Div);

	
	if(target.style.display == 'block'){
		target.style.display = 'none';
	 }else{
		target.style.display = 'block';
		if (navigator.appName!="Microsoft Internet Explorer") {
		      target.style.opacity = 0;
              target.style.filter = 'alpha(opacity=0)';
		      opacity(Div, 0);
		}
	 }
	
}
//---------------------------------------------------------------


function rezet(mod){
	eval ('document.Search.Go' + mod + '.value   = \'0\';');
}





/*
var map;
var draggable = false, resizable = false;
var typecontrol = new GMapTypeControl(true);*/

function loadMap(lng, lat, name, divId, num, cc) {
	
 var div = document.getElementById("div" + divId);
 
 if(div.style.display != 'block'){
	 
	 document.getElementById('messager'+divId).style.display = 'none';
	 showHideDiv("div" + divId);
     
	 var map;
	 
	 if(GBrowserIsCompatible()) {
		 
			var baseIcon               = new GIcon(G_DEFAULT_ICON);
			baseIcon.shadow            = "http://www.yellow.co.th/img/markers/default_shadow.png";
			baseIcon.iconSize          = new GSize(20, 34);
			baseIcon.shadowSize        = new GSize(37, 34);
			baseIcon.iconAnchor        = new GPoint(9, 34);
			baseIcon.infoWindowAnchor  = new GPoint(9, 2);	
		    var Icon                   = new GIcon(baseIcon);
	  		Icon.image                 = "http://www.yellow.co.th/img/markers/"+num+".png";
			var point                  = new GLatLng(lat, lng);
			map                        = new GMap2(div);
	 		markerOptions              = { icon:Icon, title:'long/lat: ' + lng + '/' + lat };
			map.setCenter(point, 13	, G_NORMAL_MAP);
			map.addControl(new GSmallZoomControl());
			map.addControl(new GMapTypeControl(true));
			var marker                 = new GMarker(point,markerOptions);
			map.addOverlay(marker);
			marker.openInfoWindowHtml(name);
			
			initialize(lat, lng, name);
			//initialize.mapAll.setCenter(point, 11);
			//initialize.mapAll.openInfoWindowHtml(point, name);		
	 }


 }else{
	div.style.display = 'none';	 
 }
 
 
}


function hideMap(divId) {
	var div = document.getElementById("div" + divId);
	div.style.display = 'none';
	GUnload();
}




function loadMessager(id) {
	
	var divId = 'messager' + id;
	var Div   = document.getElementById(divId);
	
	if(Div.style.display != 'block'){
		hideMap(id);
		var mess = '<img src="http://www.yellow.co.th/img/loading.gif" align="absmiddle">';
		Div.style.display = 'block';
		var URL = 'http://www.yellow.co.th/mailer.php?id=' + id;
		var ajax = new AJAX(URL, divId, mess);
		 
	 
	}else{
		 Div.style.display = 'none';
	}
 
}




function sendMessage(id) {
	
	var form         = 'messageForm'+id;
	var email        = eval('document.'+form+'.email.value');
	var name         = eval('document.'+form+'.name.value');	
	var txt          = eval('escape(document.'+form+'.txt.value)');
	var secuCode     = eval('document.'+form+'.secuCode.value');
	
	var msg = "";

	if (email != "")	{
		indexAroba = email.indexOf('@');
		indexPoint = email.indexOf('.');
		if ((indexAroba < 0) || (indexPoint < 0))		{
			eval('document.'+form+'.email.style.backgroundColor = "#FFCC00"');
			msg += "This email address is not valid.. Correct it!\n";
		}else{
		    eval('document.'+form+'.email.style.backgroundColor = "#FFFFFF"');
		}
	}else{
		eval('document.'+form+'.email.style.backgroundColor = "#FFCC00"');
		msg += "You must input your email address !\n";		
	}
	
	if (name.length < 3)	{
		msg += "Please input full name !\n";
		eval('document.'+form+'.name.style.backgroundColor = "#FFCC00"');
	}else{
		eval('document.'+form+'.name.style.backgroundColor = "#FFFFFF"');
	}
	
	if (txt.length < 5)	{
		msg += "Please write a message to be sent !\n";
		eval('document.'+form+'.txt.style.backgroundColor = "#FFCC00"');
	}else{
		eval('document.'+form+'.txt.style.backgroundColor = "#FFFFFF"');
	}	
	
	if (secuCode.length != 4)	{
		msg += "Security must be 4 caracters (letters and / or digits) !\n";
		eval('document.'+form+'.secuCode.style.backgroundColor = "#FFCC00"');
	}else{
		eval('document.'+form+'.secuCode.style.backgroundColor = "#FFFFFF"');
	}		
		
	if (msg == "") {
		eval('document.'+form+'.bt.disabled = true');
		eval('document.'+form+'.bt.value    = "Please wait..."');
		var mess  = '<img src="http://www.yellow.co.th/img/loading.gif" align="absmiddle">';
		var URL   = 'http://www.yellow.co.th/mailer_send.php?id=' + id + '&name=' + name + '&email=' + email + '&txt=' + txt + '&secuCode=' + secuCode;
		var ajax  = new AJAX(URL, 'messager'+id, mess);	
	}else{
		alert(msg);
		return(false);
	}
		
}


function SwitchStyle (targ,clas){
	document.getElementById(targ).className = clas;
}

