// JavaScript Document

// JavaScript Document
// VARS GLOBAIS
var dir = "/";
// FIM VARS GLOBAIS

/* ------------ */
function me2home(){setHomePage('http://www.hoteis.pt');return false;}
function me2favo(){ var url = window.location;var title = document.title;if (window.sidebar) window.sidebar.addPanel(title, url,"");else if(window.opera && window.print){var mbm = document.createElement('a'); mbm.setAttribute('rel','sidebar');mbm.setAttribute('href',url); mbm.setAttribute('title',title);mbm.click();}else if(document.all){window.external.AddFavorite(url, title);}else{alert("Adcionar aos favoritos manualmente!")}}
/* ------------ */

function changeLanguage( LG ){
	$j("input#lng").val(LG);
	document.getElementById('changeLang').submit();
}

function lastMinit(){
	$j.PeriodicalUpdater({
		url : dir + 'template/pub/pub.php',
		method: 'post',
		maxTimeout: 15000,
		minTimeout: 10000
	},
	function(data){
		 $j("span#lastMinit").html(data);	
	});		
}
function installClock(){
  $j('span.jclock').jclock({
		fontFamily: '"Trebuchet MS", Arial, Helvetica, sans-serif',
		fontSize: '11px',
		foreground: 'white'
	});
}

function tbs( tb ){
	var TABS = new ddtabcontent(tb);
	TABS.setpersist(true);
	TABS.setselectedClassTarget("link"); //"link" or "linkparent"
	TABS.init();
}

function inicio(){
	lastMinit();
	installClock();
	document.oncontextmenu = function (){return false;}
}	

$j(window).bind('load',function(){
		inicio();
});

$j(function(){

    $j(".tabMenu a").click(function(){
        if($j("a#TABMAPA").hasClass("selected")){
            $j("div#rotaMapa").css({"display":"block"});
        }else{
            $j("div#rotaMapa").css({"display":"none"});
        }
    });
    if($j("a#TABMAPA").hasClass("selected")){
            $j("div#rotaMapa").css({"display":"block"});
        }
});
function showAjaxRecomendar(id) {
	//Event.stop();
	Lightview.show({
		href: dir + 'template/componentes/ajax/recomend.php',
		rel: 'ajax',
		options: {
			menubar: false,
			autosize: true,
			topclose: true,
			keyboard: false, 
			overlayClose: false,
			ajax: {
				method: 'post',
				evalScripts: true,
				parameters:{'id':id},
				onComplete: function(){
				// once the request is complete we observe the form for a submit
				$('ajaxFormRecomend').observe('submit', submitAjaxRecomendar);
				}
			}
		}
	});
	
}
function submitAjaxRecomendar(event) {
  // block default form submit
  Event.stop(event);
  
  // if there's no text in the box, don't do anything
  var text = $('Rec_Nome').value.strip();
  if (!text) return;
  
  var text = $('Rec_amigo').value.strip();
  if (!text) return;

  var text = $('Rec_email').value.strip();
  if (!text) return;

  var text = $('Rec_mensagem').value.strip();
  if (!text) return;

  Lightview.show({
    href: dir + 'template/componentes/ajax/sendRecomendMail.php',
    rel: 'ajax',
    options: {
	  menubar: false,
	  topclose: false,
	  autosize: true,
	  overlayClose: false,
      ajax: {
        parameters: Form.serialize('ajaxFormRecomend') // the parameters from the form
      }
    }
  });
}

// SMS

function showSMS(id) {
	//Event.stop();
 	Lightview.show({
		href: dir + 'template/componentes/ajax/SMS.php',
		rel: 'ajax',
		options: {
			menubar: false,
			autosize: true,
			topclose: true,
			keyboard: false, 
			overlayClose: false,
			ajax: {
				method: 'post',
				evalScripts: false,
				parameters:{'id':id},
				onComplete: function(){
				// once the request is complete we observe the form for a submit
				$('sendSMSform').observe('submit', submitAjaxSMS);
				}
			}
		}
	});	
}


function verifica_tel()
{
	var num_caract =  document.getElementById('telemovel').value.length;
    
    if( num_caract == '9')
    {
	    return true;
    } else if(num_caract != '9'){
    	alert("Insira um número de telemóvel válido.");
        return false;
    }
}

function calcula_total_caracteres(){
	var total = 0;
 
 		if (document.getElementById('nome_').checked == true)
        	{
       			var nome = parseInt($('val_nome').value.strip());      
            }else if (document.getElementById("nome_").checked == false){
            	var nome = '';
            }
            total += +nome;
     
		if (document.getElementById('morada').checked == true)
        	{
       			var morada = parseInt($('val_morada').value.strip());      
            }else if (document.getElementById("morada").checked == false){
            	var morada = '';
            }
            total += +morada;
    
		if (document.getElementById('cod_postal').checked == true)
        	{
       			var cod_postal = parseInt($('val_cod_postal').value.strip());
            }else if (document.getElementById("cod_postal").checked == false){
            	var cod_postal = '';
            }
            total += +cod_postal;

		if (document.getElementById('localidade').checked == true)
        	{
       			var localidade = parseInt($('val_localidade').value.strip());
            }else if (document.getElementById("localidade").checked == false){
            	var localidade = '';
            }
            total += +localidade;

		if (document.getElementById('concelho').checked == true)
        	{
       			var concelho = parseInt($('val_concelho').value.strip());
            }else if (document.getElementById("concelho").checked == false){
            	var concelho = '';
            }
    		total += +concelho;        

		if (document.getElementById('distrito').checked == true)
        	{
       			var distrito = parseInt($('val_distrito').value.strip());
            }else if (document.getElementById("distrito").checked == false){
            	var distrito = '';
            }
            total += +distrito;

		if (document.getElementById('telefone_').checked == true)
        	{
       			var telefone_ = parseInt($('val_telefone').value.strip());
            }else if (document.getElementById("telefone_").checked == false){
            	var telefone_ = '';
            }
            total += +telefone_;

    	if (document.getElementById('fax_').checked == true)
        	{
       			var fax_ = parseInt($('val_fax').value.strip());
            }else if (document.getElementById("fax_").checked == false){
            	var fax_ = '';
            }
            total += +fax_;

    	if (document.getElementById('email_').checked == true)
        	{
       			var email_ = parseInt($('val_email').value.strip());
            }else if (document.getElementById("email_").checked == false){
            	var email_ = '';
            }
            total += +email_;

    	if (document.getElementById('gps_').checked == true)
        	{
       			var gps_ = parseInt($('val_gps').value.strip());
            }else if (document.getElementById("gps_").checked == false){
            	var gps_ = '';
            }
            total += +gps_;
      
     //  var total = +nome + +morada + +cod_postal + +localidade + +concelho + +distrito + +telefone_ + +email_ + +gps_ + +fax_;
		if(total>160){
        	alert("A mensagem tem mais de 160 caracteres e não pode ser enviada!");
            return false;
        }
        document.getElementById("totais").innerHTML = 'Tem ' + total + ' caracteres de 160';
}

function submitAjaxSMS(event) {
  // block default form submit
  Event.stop(event);
  
	var valida =  verifica_tel();
    if(!valida)
    return;
    
  Lightview.show({
    href: dir + 'template/componentes/ajax/sendSMS.php',
    rel: 'ajax',
    options: {
	  menubar: false,
	  topclose: false,
	  autosize: true,
	  overlayClose: false,
      ajax: {
        parameters: Form.serialize('sendSMSform') // the parameters from the form
      }
    }
  });
}


// FIM SMS


function showAjaxFormDemonstration(id) {
	//Event.stop();
	
	Lightview.show({
		href: dir + 'template/componentes/ajax/contact.php',
		rel: 'ajax',
		//title: 'Pedido de Informaçao',
		//caption: '<nobr>Enter your username and password to login</nobr>',
		options: {
			menubar: false,
			autosize: true,
			topclose: true,
			keyboard: false, 
			overlayClose: false,
			ajax: {
				method: 'post',
				evalScripts: true,
				parameters:{'id':id},
				onComplete: function(){
				// once the request is complete we observe the form for a submit
				$('ajaxForm').observe('submit', submitAjaxFormDemonstration);
				}
			}
		}
	});
	
}

function submitAjaxFormDemonstration(event) {
  // block default form submit
  Event.stop(event);
  
  // if there's no text in the box, don't do anything
  var text = $('nome').value.strip();
  if (!text) return;
  
  var text = $('email').value.strip();
  if (!text) return;

  var text = $('assunto').value.strip();
  if (!text) return;

  var text = $('mensagem').value.strip();
  if (!text) return;
	  
  Lightview.show({
    href: dir + 'template/componentes/ajax/sendContactMail.php',
    rel: 'ajax',
    options: {
	  menubar: false,
	  topclose: false,
	  autosize: true,
	  overlayClose: false,
      ajax: {
        parameters: Form.serialize('ajaxForm') // the parameters from the form
      }
    }
  });
}

function checkFieldAjaxFrom() {
	missinginfo = '';
	mail = true;
	tel = true;

	if (document.ajaxForm.nome.value == "") {
		missinginfo = 'true';
		document.ajaxForm.nome.style.border="1px solid #FF0000";
	}else{
		document.ajaxForm.nome.style.border="1px solid #00FF00";
	}
	
	if(document.ajaxForm.email.value == ""){mail = false;}
	if(document.ajaxForm.telefone.value == ""){tel = false;}
	
	if(mail){
		if(document.ajaxForm.email.value.match(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/)==null){
			missinginfo = 'true';
			document.ajaxForm.email.style.border="1px solid #FF0000";
			mail = false;
		}else{
			mail = 'true';
			document.ajaxForm.email.style.border="1px solid #00FF00";
		}
	}
	
	if(!mail && !tel){
		missinginfo = 'true';
		document.ajaxForm.email.style.border="1px solid #FF0000";
		mail=false;
	}else{
		if(mail){document.ajaxForm.email.style.border="1px solid #00FF00";}
		if(tel){document.ajaxForm.telefone.style.border="1px solid #00FF00";}	
	}
	
	
	

	if (document.ajaxForm.assunto.value == "") {
		missinginfo = 'true';
		document.ajaxForm.assunto.style.border="1px solid #FF0000";
	}else{
		document.ajaxForm.assunto.style.border="1px solid #00FF00";
	}
	
	if (document.ajaxForm.mensagem.value == "") {
		missinginfo = 'true';
		document.ajaxForm.mensagem.style.border="1px solid #FF0000";
	}else{
		document.ajaxForm.mensagem.style.border="1px solid #00FF00";
	}

	if (missinginfo != "") {
		return false;
	}
	else {
		return true;
	}
}
$j(function() {
	$j.datepicker.setDefaults($j.datepicker.regional[ LNG ]);
	$j(".datepicker").datepicker({
		buttonImage: '/images/icons/calendar.png',
		changeMonth: true,
		changeYear: true,
		dateFormat: 'yy-mm-dd',
		showButtonPanel: true,
		showOn: 'both',
		minDate: "today",
		beforeShow: customRange
	});
	
	// Customize two date pickers to work as a date range
	function customRange(input) 
	{ 
		
		var dateMin = "today";
		var dateMax = "+5y";
		
		if (input.id == "arrival_date") 
		{
			if($j("#departure_date").datepicker("getDate") != '' && $j("#departure_date").datepicker("getDate") != null){
				dateMax = $j("#departure_date").datepicker("getDate");
				dateMax = new Date(dateMax.getFullYear(), dateMax.getMonth(), dateMax.getDate() - 1);
			}
		}
		else if (input.id == "departure_date")
		{
			if($j("#arrival_date").datepicker("getDate") != '' && $j("#arrival_date").datepicker("getDate") != null){
				dateMin = $j("#arrival_date").datepicker("getDate");
				dateMin =new Date(dateMin.getFullYear(), dateMin.getMonth(), dateMin.getDate() + 1);
			}else{
				dateMin = "+1D";
			}
		}
		return {
			minDate: dateMin,
			maxDate: dateMax
		}; 
	 
	}
});

$j(function(){

	//PHONE
    $j("span.textPhone[id]").one("click", function(){
    	var ID = $j(this).attr("id");
        $j.post(dir+"template/componentes/ajax/count.php", { id: $j(this).attr("id"), tipo:"1", object:"8", local:"2" } );
        
        $j(this).css({"display":"none"});
        $j("span#numberPhone_"+ID).css({"display":"block"});
    });
    
	//FAX
    $j("span.textFax[id]").one("click", function(){
    	var ID = $j(this).attr("id");
        $j.post(dir+"template/componentes/ajax/count.php", { id: $j(this).attr("id"), tipo:"1", object:"9", local:"2" } );
        
        $j(this).css({"display":"none"});
        $j("span#numberFax_"+ID).css({"display":"block"});
    });
	//MOBILE
    $j("span.textTelm[id]").one("click", function(){
    	var ID = $j(this).attr("id");
        $j.post(dir+"template/componentes/ajax/count.php", { id: $j(this).attr("id"), tipo:"1", object:"10", local:"2" } );
        
        $j(this).css({"display":"none"});
        $j("span#numberTelm_"+ID).css({"display":"block"});
    });

	//MAIL
	$j(".sendMail[id]").one("click", function(){$j.post(dir+"template/componentes/ajax/count.php", { id: $j(this).attr("id"), tipo:"1", object:"2", local:"2" } );});
	
	//Recomenda
	$j(".sendRecomendar[id]").one("click", function(){$j.post(dir+"template/componentes/ajax/count.php", { id: $j(this).attr("id"), tipo:"1", object:"3", local:"2" } );});
	
	//Gps
	$j(".sendGps[id]").one("click", function(){$j.post(dir+"template/componentes/ajax/count.php", { id: $j(this).attr("id"), tipo:"1", object:"4", local:"2" } );});
	
	//Url
	$j(".sendUrl[id]").one("click", function(){$j.post(dir+"template/componentes/ajax/count.php", { id: $j(this).attr("id"), tipo:"1", object:"6", local:"2" } );});
	
	//Info
	$j("#btInfo[class]").one("click", function(){$j.post(dir+"template/componentes/ajax/count.php", { id: $j(this).attr("class"), tipo:"1", object:"7", local:"2" } );});
});

