// JavaScript Document
// Funcoes de utilizadas na validacao de campos de formularios
function validaCampoObrigatorio(nomeCampo, nomeCorrecto, nomeDefault){
  if (nomeCorrecto == undefined) nomeCorrecto = nomeCampo;
  divResposta = document.getElementById("divResp"); 
  objCampo = document.getElementById(nomeCampo);
  if( (objCampo.value == "") || (objCampo.value == nomeDefault) ){
	divResposta.innerHTML = nomeCorrecto + " obrigatório";	
	return false;
  }
  return true;
}

function validaCampoObrigatorioTelefone(nomeCampo, nomeCorrecto){
  if (nomeCorrecto == undefined) nomeCorrecto = nomeCampo;
  objCampo = document.getElementById(nomeCampo);
  if(objCampo.value == ""){
	return false;
  }
  return true;
}

function validaCampoObrigatorioRadio(nomeCampoRadio, numOpcoes, msg, nomeForm){
  divResposta = document.getElementById("divResp");
  for (i=0; i<numOpcoes; i++){
  	radio = eval("document."+nomeForm+"."+ nomeCampoRadio);
  	objCampo = radio[i];
  	if(objCampo.checked) return true;
  }
  divResposta.innerHTML = msg + " obrigatório";	
  return false;
}



function validaCodigoPostal(codigo){
	if (codigo.substring(0,4) == "0000") return false;
	
	var codPostalRegxp = /^([0-9]{4})(-([0-9]{3}))*$/;
	if (codPostalRegxp.test(codigo) != true) return false;
	
	return true;
}



function validaTelemovel(numero){
	
	var telnoRegxp = /^([0-9]{9})$/;
	if (telnoRegxp.test(numero) != true) return false;
	
	return true;
}


function validaEnderecoEmail(endereco){
	divResposta = document.getElementById("divResp");
	var emailRegxp = /^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/;

	if (emailRegxp.test(endereco) != true) return false;
	
	return true;

}


function validaCampoEmail(nomeCampo, nomeCorrecto){
	divResposta = document.getElementById("divResp"); 
	objCampo = document.getElementById(nomeCampo);
  	if(!validaEnderecoEmail(objCampo.value)){
		divResposta.innerHTML = nomeCorrecto + " inválido";	
		return false;
  }
  return true;
}


function validaData(thisData){

var objRegExp = /^\d{1,2}-\d{1,2}-\d{4}$/

  //check to see if in correct format
  if(!objRegExp.test(thisData))
    return false; //doesn't match pattern, bad date
  else{
    var strSeparator = thisData.substring(2,3) //find date separator
    var arrayDate = thisData.split(strSeparator); //split date into month, day, year
    //create a lookup for months not equal to Feb.
    var arrayLookup = { '01' : 31,'03' : 31, '04' : 30,'05' : 31,'06' : 30,'07' : 31,
                        '08' : 31,'09' : 30,'10' : 31,'11' : 30,'12' : 31}
    var intDay = parseInt(arrayDate[0],10);

    //check if month value and day value agree
    if(arrayLookup[arrayDate[1]] != null) {
      if(intDay <= arrayLookup[arrayDate[1]] && intDay != 0)
        return true; //found in lookup table, good date
    }

    //check for February (bugfix 20050322)
    var intMonth = parseInt(arrayDate[1],10);
    if (intMonth == 2) { 
       var intYear = parseInt(arrayDate[2],10);
       if( ((intYear % 4 == 0 && intDay <= 29) || (intYear % 4 != 0 && intDay <=28)) && intDay !=0)
          return true; //Feb. had valid number of days
       }
  }
  divResposta = document.getElementById("divResp"); 
  divResposta.innerHTML = ".";	
  return false; //any other values, bad date
}




function validaCampoTelemovel(nomeCampo){
	divResposta = document.getElementById("divResp"); 
	objCampo = document.getElementById(nomeCampo);
  	if(!validaTelemovel(objCampo.value)){
		divResposta.innerHTML = nomeCampo + " inválido";	
		return false;
  }
  return true;
}


function validaCampoCodigoPostal(nomeCampo){
	divResposta = document.getElementById("divResp"); 
	objCampo = document.getElementById(nomeCampo);
  	if(!validaCodigoPostal(objCampo.value)){
		divResposta.innerHTML = "Campo Código Postal inválido.";	
		return false;
  }
  return true;
}


function validaCampoDataNasc(nomeCampo){
	divResposta = document.getElementById("divResp"); 
	objCampo = document.getElementById(nomeCampo);
  	if(!validaData(objCampo.value)){
		divResposta.innerHTML = "Campo Data nascimento inválido.";	
		return false;
  }
  return true;
}

function validaConfirmacaoPassword(pwd, pwdConf){
	divResposta = document.getElementById("divResp"); 
	objPwd = document.getElementById(pwd);
	objPwdConf = document.getElementById(pwdConf);
	
  	if(objPwd.value != objPwdConf.value){
		divResposta.innerHTML = "confirmação de password inválida";	
		return false;
  }
  return true;
}

function stripCharsInBag(s, bag)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}


function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function validaNumeroSemLimite(numero, campo){
	objCampo = document.getElementById(numero);
	
	// Declaring required variables
	var digits = "0123456789";
	s=objCampo.value;
	retorno = (isInteger(s));
	if(!retorno){
		divResposta = document.getElementById("divResp"); 
		divResposta.innerHTML = campo +" inválido";
	}
	return retorno;
}

function validaNumero(numero, campo, limite){
	objCampo = document.getElementById(numero);
	
	// Declaring required variables
	var digits = "0123456789";
	// non-digit characters which are allowed in phone numbers
	var phoneNumberDelimiters = "()- ";
	// characters which are allowed in international phone numbers
	// (a leading + is OK)
	var validWorldPhoneChars = phoneNumberDelimiters + "+";
	// Minimum no of digits in an international phone no.
	var minDigitsInIPhoneNumber = limite;
	
	if( (numero == "dia") && (objCampo.value > 31)){
		divResposta = document.getElementById("divResp"); 
		divResposta.innerHTML = campo +" inválido";
		return false;
	}
	
	if( (numero == "mes") && (objCampo.value > 12)){
		divResposta = document.getElementById("divResp"); 
		divResposta.innerHTML = campo +" inválido";
		return false;
	}
	
	if( (numero == "ano") && (objCampo.value < 1900)){
		divResposta = document.getElementById("divResp"); 
		divResposta.innerHTML = campo +" inválido";
		return false;
	}

	
	s=stripCharsInBag(objCampo.value,validWorldPhoneChars);
	if(s.length != minDigitsInIPhoneNumber){
		divResposta = document.getElementById("divResp"); 
		divResposta.innerHTML = campo +" inválido";
		return false;
	}
	
	retorno = (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
	if(!retorno){
		divResposta = document.getElementById("divResp"); 
		divResposta.innerHTML = campo +" inválido";
	}
	return retorno;
}

function validaUserName(user) { 
	var pattern = /[\w@.\-]+/;
	objCampo = document.getElementById(user);
	username = objCampo.value;
 if(pattern.exec(username) != username){
 	divResposta = document.getElementById("divResp"); 
	divResposta.innerHTML = "username inválido";
	return false; 
 } else {
 	return true;
 }
}



