// JavaScript Document
/* VALIDAÇÃO DE FORMULÁRIOS */
String.prototype.trim = function()
{
  return this.replace(/^\s*/, "").replace(/\s*$/, "");
}

var DOM = YAHOO.util.Dom;
var EVT = YAHOO.util.Event;


function validate_form(_obj,lg){
  
  var errors = false;
  
  if(lg=='pt'){
    var aux_txt = "Preenchimento obrigatório.";
    var aux_txt_ema = "O email introduzido não é válido.";
    var aux_txt_pass = "As palavras-chave introduzidas não são iguais.";
    var txt_menos6 = "Palavra chave inválida";
  }
  if(lg=='gb'){
    var aux_txt = "Required field.";
    var aux_txt_ema = "The email entered is not valid.";
    var aux_txt_pass = "The passwords entered do not match.";
    var txt_menos6 = "Invalid passwords";
  }
  if(lg=='fr'){
    var aux_txt = "Champ obligatoire.";
    var aux_txt_ema = "Le e-mail saisies n'est pas valide.";
    var aux_txt_pass = "Les mots de passe saisis ne correspondent pas.";
    var txt_menos6 = "Mots-clés Blancs";
  }
  if(lg=='sp'){
    var aux_txt = "Campo obligatorio.";
    var aux_txt_ema = "El correo electrónico introducido no es válido.";
    var aux_txt_pass = "Las palabras clave no son iguales.";
    var txt_menos6 = "Palabras clave no válido";
  }
  
  
  
	DOM.getElementsByClassName(
		'required',
		null,
		_obj,
		function(e){
		    
		    
  			var _tag  = e.tagName;
  			var _type = e.getAttribute('type');
  			var _emai = e.getAttribute('email');
  			var _fich = e.getAttribute('fich');
        var _sel  = e.getAttribute('selecta');
        
        
  			if(_tag == 'TEXTAREA' || _type == 'text' || _type == 'password'){
  			
  				if ((e.value.trim() == '') ){
  				
            var _varid = e.getAttribute('id');
            
            e.style.border = '1px solid #DC2619';
            
            
           	
  					if(errors == false){
              errors = true;
              e.focus();
            }              
  					EVT.addListener(e, "focus", function(){ e.style.border = '1px solid #d6d6d6'; });
  				
  					
  			 }
  			 
  				
  				if(_emai == 1){
  				
  					if(verify_email(e.value) == false){
  						var _varid = e.getAttribute('id');
  						
              e.style.border = '1px solid #DC2619';
              
  						EVT.addListener(e, "focus", function(){  e.style.border = '1px solid #d6d6d6'; });
  						
    						if(errors == false){
                   errors = true;
                   aux_txt = aux_txt_ema;
                    e.focus();
                }
  						
  					}
  					
  				}
  				
  				
  			} else if(_fich == 1){
  			  if(e.value == ''){
            var _varid = e.getAttribute('id');
            e.style.border = '1px solid #DC2619';
  				
  					//EVT.addListener(e, "focus", function(){ e.value = ''; });
  					if(errors == false) {
              errors = true;
            }
          } else {
  					var _varid = e.getAttribute('id');
  				  e.style.border = '1px solid #676767';
  				
  				}
  			} else if(_sel == 1) {
          if ((e.value=="") || (e.value==0) || (e.value=="Seleccione uma Opção")){
            var _varid = e.getAttribute('id');
            e.style.border = '1px solid #DC2619';
  		
						EVT.addListener(e, "focus", function(){ e.style.color = '#d6d6d6'; });
						if(errors == false){
               errors = true;
               e.focus();
            }
          } else {
            var _varid = e.getAttribute('id');
  				  e.style.border = '1px solid #d6d6d6';
  				
          }
  			} else if(_type == 'checkbox') {
  				if(e.checked == false){
  					//alert('Tem de dizer se aceita os termos e condições de funcionamento do site.');
  					if(errors == false) errors = true;
  				}
  			}
		}
	);
	
	/*Valida CP1*/
	if(document.getElementById('cp1')){
    var cp1_f = document.getElementById('cp1');
    var cp2_f = document.getElementById('cp2');
    if(cp1_f.value!=""){
      if ( isNaN(cp1_f.value) || cp1_f.value == '0000' || cp1_f.value.length<4 ){
        
         
          cp1_f.style.border='1px solid #DC2619';
       
          
          EVT.addListener(cp1_f,'focus',function mknome() {
            	cp1_f.style.border='1px solid #d6d6d6';
          });
          
      
    		 if(errors == false){
           aux_txt = "Código postal inválido.";
           cp1_f.focus();
           errors = true;
         }
      }
    }
    
    /*Valida CP2*/
    if(cp2_f.value!=""){
      if ( isNaN(cp2_f.value) || (cp2_f.value == '000') || cp2_f.value.length<3 ){
        
          cp2_f.style.border='1px solid #DC2619';
          EVT.addListener(cp2_f,'focus',function mknome() {
            	cp2_f.style.border='1px solid #d6d6d6';
          });
          
    		  if(errors == false){
           aux_txt = "Código postal inválido";
           cp2_f.focus();
           errors = true;
         }
      }
    }
  }
  
  if(document.getElementById('pwd2')){
  
      var pw1 = document.getElementById('pwd');
      var pw2 = document.getElementById('pwd2');
      
      if(pw1.value=="" || pw2.value=="" || pw1.value!=pw2.value ||  pw1.value.length < 6 ){
          if(errors==false) {
              errors = true;
              aux_txt = aux_txt_pass;
              pw1.focus();
              if(pw1.value.length < 6) aux_txt = txt_menos6
          }
          
          
          pw1.style.border='1px solid #DC2619';
          pw2.style.border='1px solid #DC2619';
      }

    
  }
	
	
	
	if(errors == true){
	 
    if(document.getElementById('p_error')){
        document.getElementById('p_error').style.display='block';
    }else{
      alert(aux_txt);
    }
		return false;
	} else {
		return true;		
	}
	
	
	
}

function verify_email(_mail){
	
  var status = false;     
	var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
	
  if (_mail.search(emailRegEx) == -1) {
		status = false;
	} else {
		status = true;
	}
	return status;
}

function valida_email(email,lg){
  if(lg!='')
    lg='pt';
  var e = email.value;
  var erro = 0;
  if (e!=''){
    if (verify_email(e) == false){
      if(lg=='pt'){
        alert("Email inválido.");
      }
      if(lg=='gb'){
        alert("Invalid Email.");
      }
      if(lg=='fr'){
        alert("Email Blancs.");
      }
      if(lg=='sp'){
        alert("No válida de correo electrónico.");
      }
      
      erro=1;
    }
  } else {
    if(lg=='pt'){
      alert("Tem que introduzir um email válido.");
    }
    if(lg=='gb'){
      alert("You must enter a valid email.");
    }
    if(lg=='fr'){
      alert("Vous devez entrer une adresse email valide.");
    }
    if(lg=='sp'){
      alert("Debe ingresar un email válido.");
    }
    
    erro=1;
  }
  if(erro==1){  
    return false;
  } else {
    return true;
  }
}

function verify_number(field,lg){
  if(lg!='')
    lg='pt';
    
  if (isNaN(field.value)) 
  {
    if(lg=='pt'){
      alert("Só pode introduzir caracteres numéricos.");
    }
    if(lg=='gb'){
      alert("You can only enter numeric characters.");
    }
    if(lg=='fr'){
      alert("Vous ne pouvez introduire des caractères numériques.");
    }
    if(lg=='sp'){
      alert("Sólo se pueden introducir caracteres numéricos.");
    }
    field.value='';
    field.focus();
  }
}


/* VALIDAÇÃO DE FORMULÁRIOS */

/* Picker calendário */
function escolhe_data_entrada(type,args,obj) {
  //alert(type+' - '+args+' - '+obj)
  var dates = args[0];   
  var date = dates[0];   
  var ano = date[0], mes = date[1], dia = date[2];
  obj = obj.split(','); 
  if(dia<10)
    dia = "0"+dia;
  if(mes<10)
    mes = "0"+mes;
  
  document.getElementById(obj[0]).value=dia+'-'+mes+'-'+ano;
    
  cal2.hide();
}




function validate_ns(_obj,lg,estr){
  var errors = false;

  if(lg=='pt'){
    var aux_txt = "O email introduzido não é válido.";
  }
  if(lg=='gb'){
    var aux_txt = "The email entered is not valid.";
  }
  
  if(lg=='sp'){
    
    var aux_txt = "El correo electrónico introducido no es válido.";
  }
  
	DOM.getElementsByClassName(
		'required',
		null,
		_obj,
		function(e){
  			var _tag  = e.tagName;
  			var _type = e.getAttribute('type');
  			var _emai = e.getAttribute('email');
  			var _fich = e.getAttribute('fich');
        var _sel  = e.getAttribute('selecta');
        if(_emai == 1){
            
            if(verify_email(e.value) == estr){
  						var _varid = e.getAttribute('id');
    					
  						if(errors == false) errors = true;
  						
  					} else {
  					  var _varid = e.getAttribute('id');
      	
  					}
        
  					if(verify_email(e.value) == false){
  						var _varid = e.getAttribute('id');
             
    					
  						if(errors == false) errors = true;
  						
  					} else {
  					  var _varid = e.getAttribute('id');
      	
  					}
  				}
  		  }
  	);
	
  	if(errors == true){
  	   alert(aux_txt);
  		return false;
  	} else {  	
  		return true;
  	}
}


var contador = 0;
function validate_pesq(_obj,lg){
  var errors = false;
 
  if(lg=='pt'){
    var estr = "A palavra não é válida ou contém menos de três caracteres.";
  }
  
  if(lg=='gb'){
    var estr = "That word is not valid or contains less than three characters.";
  }
  
	DOM.getElementsByClassName(
		'required',
		null,
		_obj,
		function(e){
  			var _tag  = e.tagName;
  			var _type = e.getAttribute('type');
  			var _emai = e.getAttribute('email');
  			var _fich = e.getAttribute('fich');
        var _sel  = e.getAttribute('selecta');

        if(contador == 1){
  
          if(e.value.length<3){
            //alert(estr);
            errors = true;
          }
        }else if(contador == 0){
            errors = true;
        }
         
  		}
  	);

  	if(errors == true){
  	   alert(estr);
  		return false;
  	} else {  	
  		return true;
  	}
  	
}


function validate_pesqA(_obj,lg){
  var errors = false;

    if(lg=='pt'){
    var aux_txt = "A palavra não é válida ou contém menos de três caracteres.";
  }
  if(lg=='gb'){
    var aux_txt = "That word is not valid or contains less than three characters.";
  }


	DOM.getElementsByClassName(
		'required',
		null,
		_obj,
		function(e){
  			var _tag  = e.tagName;
  			var _type = e.getAttribute('type');
  			var _emai = e.getAttribute('email');
  			var _fich = e.getAttribute('fich');
        var _sel  = e.getAttribute('selecta');

         if(e.value == 'Termo' || e.value == 'Word' || e.value.length<3 )  e.value = "";
 
         if(e.value == '' ){
              e.style.border='1px solid red';
              errors = true;
         }
  		}
  	);

  	if(errors == true){
  	   alert(aux_txt);
  		return false;
  	} else {  	
  		return true;
  	}
}

function validate_form2(_obj,lg){
  var errors = false;

  if(lg=='pt'){
    var aux_txt = "Preenchimento obrigatório.";
  }
  if(lg=='gb'){
    var aux_txt = "Required field.";
  }
  var paraimp = document.getElementById('para');
  if(paraimp.value==""){
      document.getElementById('fakeNomes').style.border="1px solid red";
      errors = true;
  }
  
  var msgimp = document.getElementById('msg');
  if(msgimp.value==""){
      msgimp.style.border="1px solid red";
      errors = true;
  }
  
	if(errors == true){
	   alert(aux_txt);
		 return false;
	} else {  	
		 return true;
	}
  
}



function getXmlHttpRequest() {
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP");
	}
}




function verificaPws(lg){

  var errors = false;
  if(lg=='pt'){
    var aux_txt_pass = "As palavras-chave introduzidas não são iguais.";
  }
  
  if(lg=='gb'){
    var aux_txt_pass = "The passwords entered do not match.";
  }
  
  if(lg=='sp'){
    var aux_txt_pass = "Las palabras clave no son iguales.";
  }
  
  
  var pw1 = document.getElementById('pwd');
  var pw2 = document.getElementById('pwd2');
  
  if(pw1.value=="" || pw2.value=="" || pw1.value!=pw2.value){
      errors = true;
      pw1.style.border='1px solid #DC2619';
      pw2.style.border='1px solid #DC2619';
  }


	if(errors == true){
    alert(aux_txt_pass);
		return false;
	} else {
		return true;
	}


}


function validate_entrega(_obj,lg){
  var errors = false;
  
  if(lg=='pt'){
    var aux_txt = "Preenchimento obrigatório.";
    var aux_txt_ema = "O email introduzido não é válido.";
    var aux_txt_pass = "As palavras-chave introduzidas não são iguais.";
  }
  if(lg=='gb'){
    var aux_txt = "Required field.";
    var aux_txt_ema = "The email entered is not valid.";
    var aux_txt_pass = "The passwords entered do not match.";
  }
  if(lg=='fr'){
    var aux_txt = "Champ obligatoire.";
    var aux_txt_ema = "Le e-mail saisies n'est pas valide.";
    var aux_txt_pass = "Les mots de passe saisis ne correspondent pas.";
  }
  if(lg=='sp'){
    var aux_txt = "Campo obligatorio.";
    var aux_txt_ema = "El correo electrónico introducido no es válido.";
    var aux_txt_pass = "Las palabras clave no son iguales.";
  }
  var radio = document.getElementById('entralt');

  if(radio.checked){
    	DOM.getElementsByClassName(
    		'required',
    		null,
    		_obj,
    		function(e){
      			var _tag  = e.tagName;
      			var _type = e.getAttribute('type');
      			var _emai = e.getAttribute('email');
      			var _fich = e.getAttribute('fich');
            var _sel  = e.getAttribute('selecta');
            
            
      			if(_tag == 'TEXTAREA' || _type == 'text' || _type == 'password'){
      			
      				if ((e.value.trim() == '') ){
      				
                var _varid = e.getAttribute('id');
                
                e.style.border = '1px solid #B9171C';
                            
      					EVT.addListener(e, "focus", function(){ e.style.border = '1px solid #676767'; });
      					
      					if(errors == false) errors = true;
      					
      			 }
      			 
      				
      			 if(_emai == 1){
      				if(verify_email(e.value) == false){
      						var _varid = e.getAttribute('id');
      						
                  e.style.border = '1px solid #B9171C';
                  
      						EVT.addListener(e, "focus", function(){  e.style.border = '1px solid #676767'; });
      						
        						if(errors == false){
                       errors = true;
                       aux_txt = aux_txt_ema;
                    }
      						
      				}
      			}
    		  }
    		}
    	);
    	
    	/*Valida CP1*/
    	if(document.getElementById('cp1')){
        var cp1_f = document.getElementById('cp1');
        var cp2_f = document.getElementById('cp2');
        if(cp1_f.value!=""){
          if ( isNaN(cp1_f.value) || cp1_f.value == '0000' || cp1_f.value.length<4 ){
            
             
              cp1_f.style.border='1px solid #B9171C';
           
              
              EVT.addListener(cp1_f,'focus',function mknome() {
                	cp1_f.style.border='1px solid #676767';
              });
              
          
        		 if(errors == false){
               aux_txt = "Código postal inválido.";
               
               errors = true;
             }
          }
        }
        
        /*Valida CP2*/
        if(cp2_f.value!=""){
          if ( isNaN(cp2_f.value) || (cp2_f.value == '000') || cp2_f.value.length<3 ){
            
              cp2_f.style.border='1px solid #B9171C';
              EVT.addListener(cp2_f,'focus',function mknome() {
                	cp2_f.style.border='1px solid #676767';
              });
              
        		  if(errors == false){
               aux_txt = "Código postal inválido";
               errors = true;
             }
          }
        }
      }
    	
    	
    	
    	if(errors == true){
        alert(aux_txt);
    		return false;
    	} else {
    		return true;
    	}
	}
	
}
