//------------VALIDAR EMAIL-------------------

//-----------VALIDAÇÃO TIPO-------------------

/*
    <script type="text/javascript" src="validarFormularios.js">
      function validarForm(formName) { 
	var msg = "";
	if(!checkEmail(formName.email,""))msg += "E-mail errado!\n";
	if(!checkPhone(formName.tlf,"",9))msg += "Telefone errado - mínimo 9 dígitos!\n";
	.
	.
	.
	if(msg != "")alert(msg);
      }
    </script>
*/

//---------VALIDAR EMAIL-----------
//verificar se a input text está vazia ou nula
// obj = document.formName.inputName
// msgError = "Aviso, há um erro..."
//---------------------------------------------

//verificar se o input é nulo ou vazio  
  function checkEmail(obj,msgError){
	var invalidChars = " /:,;";
	var ev=obj.value; var et=ev.length;
	if (ev == "" || ev == null) {
		if(msgError != null && msgError != "" && msgError != " " && msgError.length > 2){
			alert(msgError);
		}
		obj.focus();obj.style.backgroundColor = "red";
                    	return false;
            }
//verificar se o input foi feito com caracteres não permitidos
            for (i=0; i<invalidChars.length; i++) {
                var badChar = invalidChars.charAt(i)
                if (ev.indexOf(badChar,0) > -1) {
		if(msgError != null && msgError != "" && msgError != " " && msgError.length > 2){
			alert(msgError);
		}
		obj.focus();obj.style.backgroundColor = "red";
                    	return false;
                }
            }
//verificar se o 1º caracter é um @
	var atPo = ev.charAt(0).indexOf("@");
            if (atPo > -1) {
		if(msgError != null && msgError != "" && msgError != " " && msgError.length > 2){
			alert(msgError);
		}
		obj.focus();obj.style.backgroundColor = "red";
                    	return false;
            }
//verificar se existe um @
            var atPos = ev.indexOf("@",1);
            if (atPos == -1) {
		if(msgError != null && msgError != "" && msgError != " " && msgError.length > 2){
			alert(msgError);
		}
		obj.focus();obj.style.backgroundColor = "red";
                    	return false;
            }
//verificar se existe + que um @
            if (ev.indexOf("@",atPos+1) > -1) {
		if(msgError != null && msgError != "" && msgError != " " && msgError.length > 2){
			alert(msgError);
		}
		obj.focus();obj.style.backgroundColor = "red";
                    	return false;
            }
//verificar se existe um "."
            var periodPos = ev.indexOf(".",atPos)
            if (periodPos == -1) {
		if(msgError != null && msgError != "" && msgError != " " && msgError.length > 2){
			alert(msgError);
		}
		obj.focus();obj.style.backgroundColor = "red";
                    	return false;
            }
//verificar se não existe + nenhum "."
            if (periodPos+3 > et)    {
		if(msgError != null && msgError != "" && msgError != " " && msgError.length > 2){
			alert(msgError);
		}
		obj.focus();obj.style.backgroundColor = "red";
                    	return false;
            }obj.style.backgroundColor = "#fff";
	return true;
  }
//---------VALIDAR TELEFONE/TELEMÓVEL-----------
// obj = document.formName.inputName
// msgError = "9 números"
// minDig = número mínimo de dígitos
//--------------------------------------------

//verificar se o input é nulo, vazio ou se não tem o número mínimo de dígitos
	function checkPhone(obj,msgError,minDig){
		var tfv = obj.value; tft = tfv.length;
		if(tfv=="" || tfv==null ||tfv==" " || tft<minDig){
			if(msgError != null && msgError != "" && msgError != " " && msgError.length > 2){
				alert(msgError);
			}
			obj.focus();obj.style.backgroundColor = "red";
	                    	return false;
		}
//verificar se foram digitados caracteres não permitidos
		var invalidCar = "»?=)(/&%$#!|\`*POIUYTREWQqwertyuiop+´";
		invalidCar += "^ªÇLKJHGFDSAasdfghjklçº~";
		invalidCar += "_:;MNBVCXZ><zxcvbnm,.-";
	            for (i = 0; i < invalidCar.length; i++) {
	                var maucar = invalidCar.charAt(i)
		   if (tfv.indexOf(maucar,0) > -1) {
			if(msgError != null && msgError != "" && msgError != " " && msgError.length > 2){
				alert(msgError);
			}
			obj.focus();obj.style.backgroundColor = "red";
	                    	return false;
		   }
	            }obj.style.backgroundColor = "#fff";
		return true;
	}
  

//------------VALIDAR CHECKBOXES--------------
//verificar se p/ - 1 checkbox foi clicada
// obj = document.formName.checkBoxName
// msgError = "Tem de escolher\npelo menos n opção(ões)"
// n = escolhas que é obrigado a fazer (1, 2, etc.)
//--------------------------------------------
	function checkCheckBox(obj,msgError,n){
		var cbx=0;
		for(var i=0; i < obj.length; i++){
			if(obj[i].checked){cbx++;}
		}
		if(cbx < n){
			if(msgError != null && msgError != "" && msgError != " " && msgError.length > 2){
				alert(msgError);
			}
			return false;
		}
		return true;
	}


//-----------VALIDAR RADIOBUTTONS------------
//verificar se um radio foi clicado
// obj = document.formName.radioName
// msgError = "Tem de escolher SIM ou NÃO.\nObrigado."
//--------------------------------------------
	function checkRadio(obj,msgError){
		var rs=false;
		for(var i=0; i < obj.length; i++){
			if(obj[i].checked){rs=true;} 
		}
		if(!rs){
			if(msgError != null && msgError != "" && msgError != " " && msgError.length > 2){
				alert(msgError);
			}
			return false;
		}
		return true;		
	}

//------------VALIDAR SELECTBOX-------------
//verificar se foi feita uma escolha
// obj = document.formName.selectName
// msgError   = "Faça a sua escolha p. f.\nObrigado."
//--------------------------------------------
	function checkSelect(obj,msgError){
		if(obj.selectedIndex <= 0){// obj.selectedIndex || obj.value == "" || obj.value == null || obj.value == ' ' || obj.value == 'undefined'
			if(msgError != null && msgError != "" && msgError != " " && msgError.length > 2){
				alert(msgError);
			}
			obj.focus();obj.style.backgroundColor = "red";
	                    	return false;
		}
		obj.style.backgroundColor = "#fff";
		return true;
	}

//------------VALIDAR INPUTBOX SÓ TEXTO
//verificar se foi feita escrito algo ex:pedro
// obj = document.formName.inputName
// txtError   = "O seu nome p. f.\nObrigado."
//--------------------------------------------

//verificar se o input não é nulo ou vazio
	function checkNome(obj,msgError){
		var nv=obj.value; var nt=nv.length;
		if(nv=="" || nv==null || nv==" " || nt<2){
			if(msgError != null && msgError != "" && msgError != " " && msgError.length > 2){
				alert(msgError);
			}
			obj.focus();
			return false;
		}
//verificar se há algum espaço no nome
/*		var esp=nv.indexOf(" ",0);
		if(esp > -1){
			if(msgError != null && msgError != "" && msgError != " " && msgError.length > 2){
				alert(msgError);
			}
			obj.focus();
			return false;
		}*/
//verificar se foram digitados caracteres não permitidos
		var invalidChars = "/:,;[]()*+?«»&%$#!|\£@§{}<>0123456789";	
	            for (i=0; i<invalidChars.length; i++) {
	                var badChar = invalidChars.charAt(i)
	                if (nv.indexOf(badChar,0) > -1) {
			if(msgError != null && msgError != "" && msgError != " " && msgError.length > 2){
				alert(msgError);
			}
			obj.focus();
	                    	return false;
	                }
            		}
		return true;
	}

//------------VALIDAR INPUTBOX (+ QUE UM NOME) SÓ TEXTO
//verificar se foi feita escrito algo
// obj = document.formName.inputName
// txtError   = "O seu nome p. f.\nObrigado."
//assume-se um padrão mínimo: Zé Sá (5 caracteres)
//--------------------------------------------

//verificar se o input não é nulo ou vazio
	function checkNomesApelidos(obj,msgError){
		var nv=obj.value; var nt=nv.length;
		if(nv=="" || nv==null || nv==" " || nt<5){
			if(msgError != null && msgError != "" && msgError != " " && msgError.length > 2){
				alert(msgError);
			}
			obj.focus();obj.style.backgroundColor = "red";
			return false;
		}
//verificar se há algum espaço entre os nomes
		var esp=nv.indexOf(" ",0);
		if(esp == -1){
			if(msgError != null && msgError != "" && msgError != " " && msgError.length > 2){
				alert(msgError);
			}
			obj.focus();obj.style.backgroundColor = "red";
			return false;
		}
//verificar se foram digitados caracteres não permitidos
		var invalidChars = "/:,;[]()*+?«»&%$#!|\£@§{}<>0123456789";	
	        for (i=0; i<invalidChars.length; i++) {
	                var badChar = invalidChars.charAt(i);
	           if (nv.indexOf(badChar,0) > -1) {
			if(msgError != null && msgError != "" && msgError != " " && msgError.length > 2){
				alert(msgError);
			}
			obj.focus();obj.style.backgroundColor = "red";
	               	return false;
	           }
            	}obj.style.backgroundColor = "#fff";
		return true;
	}


//---------VALIDAR INPUT TEXT / TEXTAREA------
//verificar se a input text está vazia ou nula 
// ou se tem o número mínimo de letras
// obj = document.formName.inputName
// msgError = "Aviso, há um erro..."
//---------------------------------------------

 
function checkText(obj,numMin,msgError){

	var ev=obj.value; var et=ev.length;

	if(!numMin || numMin == "" || numMin == " " || numMin == null){
		numMin = 2;
	}

	if(ev == "" || ev == null || ev == " " || et < numMin) {
		if(msgError != null && msgError != "" && msgError != " " && msgError.length > 2){
			alert(msgError);
		}
		obj.focus();obj.style.backgroundColor = "red";
                		return false;
        	}
	obj.style.backgroundColor = "#fff";
	return true;
}

/*************************

        DICIONÁRIO
        --------------  
        Menssagens de erro
        para formulários

*************************/

var dicc = {
	nome:{
		pt:"Insira o s/ nome, p. f..\n---\n",
		ig:"Your name please.\n---\n",
		fr:"Son nom s'il vous pla&icirc;t.\n---\n",
		es:"Su nombre, p. f..\n---\n"
	},
	email:{
		pt:"Email incorrecto.\n---\n",
		ig:"Email incorrect.\n---\n",
		fr:"Email incorrect.\n---\n",
		es:"Email incorrecto.\n---\n"
	},
	datas:{
		pt:"Datas para reserva, p. f..\n---\n",
		ig:"Dates for book.\n---\n",
		fr:"Dates pour r&eacute;serve.\n---\n",
		es:"Fechas para reservar, p. f..\n---\n"
	},
	cDatas:{
		pt:"A data de saida tem de ser superior à de entrada.\n---\n",
		ig:"The check-out date must be bigger that of check-in.\n---\n",
		fr:"La date de sortie doit d'&ecirc;tre plus grande que ce d'entr&eacute;e.\n---\n",
		es:"La fecha de salida tiene que ser m&aacute;s grande que la de entrada.\n---\n"	
	},
	mens:{
		pt:"Mensagem, p. f..\n---\n",
		ig:"Your message please.\n---\n",
		fr:"Votre message s'il vous pla&icirc;t.\n---\n",
		es:"Su mensaje, p. f..\n---\n"
	},
	agrade:{
		pt:"Obrigado.",
		ig:"Thank you.",
		fr:"Merci beaucoup.",
		es:"Gracias."
	},
	getLang:function(){
		var pathArray = window.location.pathname.split( '/' );
		var apont = pathArray[pathArray.length-1];
		apont = apont.split('.');
		var a = apont[0].substring(apont[0].length-2);
		if(!a){return 'pt';}else{return a;}
	}
}





