// JavaScript Document
function validaCampoObrigatorio(nomeCampo, nomeCorrecto, nomeDiv){
if (nomeCorrecto == undefined) nomeCorrecto = nomeCampo;
  if (nomeDiv == undefined) nomeDiv = "divResp";
  //alert(nomeCampo);
  divResposta = document.getElementById(nomeDiv); 
  objCampo = document.getElementById(nomeCampo);
  
  if(objCampo == null){
	divResposta.innerHTML = "O campo " + nomeCorrecto + " é de preenchimento obrigatório!";	
	
	return false;
  }
  if(objCampo.value == ""){
	divResposta.innerHTML = "O campo " + nomeCorrecto + " é de preenchimento obrigatório!";	
	return false;
  }
  strCampo = document.getElementById(nomeCampo).value;
  arrayStr = strCampo.split("http://");
  if(arrayStr.length > 1){
	divResposta.innerHTML = "O campo " + nomeCorrecto + " é inválido!";	
	return false;
  }
  strCampo = document.getElementById(nomeCampo).value;
  arrayStr = strCampo.split("<");
  if(arrayStr.length > 1){
	divResposta.innerHTML = "O campo " + nomeCorrecto + " é inválido!";	
	return false;
  }
  strCampo = document.getElementById(nomeCampo).value;
  arrayStr = strCampo.split(">");
  if(arrayStr.length > 1){
	divResposta.innerHTML = "O campo " + nomeCorrecto + " é inválido!";	
	return false;
  }
  strCampo = document.getElementById(nomeCampo).value;
  arrayStr = strCampo.split('"');
  if(arrayStr.length > 1){
	divResposta.innerHTML = "O campo " + nomeCorrecto + " é inválido!";	
	return false;
  }
  strCampo = document.getElementById(nomeCampo).value;
  arrayStr = strCampo.split("'");
  if(arrayStr.length > 1){
	divResposta.innerHTML = "O campo " + nomeCorrecto + " é inválido!";	
	return false;
  }
  strCampo = document.getElementById(nomeCampo).value;
  arrayStr = strCampo.split("&");
  if(arrayStr.length > 1){
	divResposta.innerHTML = "O campo " + nomeCorrecto + " é inválido!";	
	return false;
  }
  strCampo = document.getElementById(nomeCampo).value;
  arrayStr = strCampo.split(";");
  if(arrayStr.length > 1){
	divResposta.innerHTML = "O campo " + nomeCorrecto + " é inválido!";	
	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){
  divResposta = document.getElementById("divResp");
  for (i=0; i<numOpcoes; i++){
  	radio = eval("document.frmReg."+ nomeCampoRadio);
  	objCampo = radio[i];
  	if(objCampo.checked) return true;
  }
  divResposta.innerHTML = "O campo "  + msg + " é de preenchimento 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, nomeDiv){
	if (nomeDiv == undefined) nomeDiv = "divResp";
	divResposta = document.getElementById(nomeDiv); 
	objCampo = document.getElementById(nomeCampo);
  	if(!validaEnderecoEmail(objCampo.value)){
		divResposta.innerHTML = "O campo E-mail é 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 validaCampoHTTP(nomeCampo, nomeCorrecto){
	divResposta = document.getElementById("divResp"); 
	objCampo = document.getElementById(nomeCampo);
  
    if(objCampo == null){
	  return true;
    }
    if(objCampo.value == ""){
	  return true;
    }
	strCampo = objCampo.value;
	arrayStr = strCampo.split("http://");
	
	if(arrayStr.length > 1){
		divResposta.innerHTML = "O campo " + nomeCorrecto + " é inválido!";	
		return false;	
	}
	strCampo = document.getElementById(nomeCampo).value;
	arrayStr = strCampo.split("<");
	if(arrayStr.length > 1){
		divResposta.innerHTML = "O campo " + nomeCorrecto + " é inválido!";	
		return false;
  	}
  	strCampo = document.getElementById(nomeCampo).value;
  	arrayStr = strCampo.split(">");
  	if(arrayStr.length > 1){
		divResposta.innerHTML = "O campo " + nomeCorrecto + " é inválido!";	
		return false;
  	}
	strCampo = document.getElementById(nomeCampo).value;
  	arrayStr = strCampo.split('"');
  	if(arrayStr.length > 1){
		divResposta.innerHTML = "O campo " + nomeCorrecto + " é inválido!";	
		return false;
  	}
  	strCampo = document.getElementById(nomeCampo).value;
  	arrayStr = strCampo.split("'");
  	if(arrayStr.length > 1){
		divResposta.innerHTML = "O campo " + nomeCorrecto + " é inválido!";	
		return false;
  	}
  	strCampo = document.getElementById(nomeCampo).value;
  	arrayStr = strCampo.split("&");
  	if(arrayStr.length > 1){
		divResposta.innerHTML = "O campo " + nomeCorrecto + " é inválido!";	
		return false;
  	}
  	strCampo = document.getElementById(nomeCampo).value;
  	arrayStr = strCampo.split(";");
  	if(arrayStr.length > 1){
		divResposta.innerHTML = "O campo " + nomeCorrecto + " é inválido!";	
		return false;
  	}
	return true
}

function validaCampoPesquisa(nomeCampo){
	objCampo = document.getElementById(nomeCampo);
  
    if(objCampo == null){
	  return true;
    }
    if(objCampo.value == ""){
	  return true;
    }
	strCampo = objCampo.value;
	arrayStr = strCampo.split("http://");
	
	if(arrayStr.length > 1){
		return false;	
	}
	strCampo = document.getElementById(nomeCampo).value;
	arrayStr = strCampo.split("<");
	if(arrayStr.length > 1){
		return false;
  	}
  	strCampo = document.getElementById(nomeCampo).value;
  	arrayStr = strCampo.split(">");
  	if(arrayStr.length > 1){
		return false;
  	}
	strCampo = document.getElementById(nomeCampo).value;
  	arrayStr = strCampo.split('"');
  	if(arrayStr.length > 1){
		divResposta.innerHTML = "O campo " + nomeCorrecto + " é inválido!";	
		return false;
  	}
  	strCampo = document.getElementById(nomeCampo).value;
  	arrayStr = strCampo.split("'");
  	if(arrayStr.length > 1){
		divResposta.innerHTML = "O campo " + nomeCorrecto + " é inválido!";	
		return false;
  	}
  	strCampo = document.getElementById(nomeCampo).value;
  	arrayStr = strCampo.split("&");
  	if(arrayStr.length > 1){
		divResposta.innerHTML = "O campo " + nomeCorrecto + " é inválido!";	
		return false;
  	}
  	strCampo = document.getElementById(nomeCampo).value;
  	arrayStr = strCampo.split(";");
  	if(arrayStr.length > 1){	
		return false;
  	}
	return true
}


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


function validaCampoCodigoPostal(nomeCampo){
	divResposta = document.getElementById("divResp"); 
	objCampo = document.getElementById(nomeCampo);
  	if(!validaCodigoPostal(objCampo.value)){
		divResposta.innerHTML = "O 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 = "O 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 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 = "O campo " + campo + " é inválido!";
		return false;
	}
	
	if( (numero == "mes") && (objCampo.value > 12)){
		divResposta = document.getElementById("divResp"); 
		divResposta.innerHTML = "O campo " + campo + " é inválido!";
		return false;
	}
	
	if( (numero == "ano") && (objCampo.value < 1900)){
		divResposta = document.getElementById("divResp"); 
		divResposta.innerHTML = "O campo " + campo + " é inválido!";
		return false;
	}

	
	s=stripCharsInBag(objCampo.value,validWorldPhoneChars);
	if(s.length != minDigitsInIPhoneNumber){
		divResposta = document.getElementById("divResp"); 
		divResposta.innerHTML = "O campo " + campo + " é inválido!";
		return false;
	}
	
	retorno = (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
	if(!retorno){
		divResposta = document.getElementById("divResp"); 
		divResposta.innerHTML = "O campo " + 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 = "O campo Username é inválido.";
	return false; 
 } else {
 	return true;
 }
}

/**********************************************************************************************************************/
/* Description: Valida se a data thisData está bem formatada */
/* Params IN: thisData:string */
/* Params OUT: boolean */
/* Returns: true se a data for composta por 2 digitos para dia, 2 digitos para mes e 4 para ano */
function validaDataComposta(thisData){

var objRegExp = /^\d{2}-\d{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
       }
  }
  return false; //any other values, bad date
  
}


function compareDates (value1, value2) {
   var date1, date2;
   var month1, month2;
   var year1, year2;

   month1 = value1.substring (0, value1.indexOf ("-"));
   date1 = value1.substring (value1.indexOf ("-")+1, value1.lastIndexOf ("-"));
   year1 = value1.substring (value1.lastIndexOf ("-")+1, value1.length);

   month2 = value2.substring (0, value2.indexOf ("-"));
   date2 = value2.substring (value2.indexOf ("-")+1, value2.lastIndexOf ("-"));
   year2 = value2.substring (value2.lastIndexOf ("-")+1, value2.length);

   if (year1 > year2) return 1;
   else if (year1 < year2) return -1;
   else if (month1 > month2) return 1;
   else if (month1 < month2) return -1;
   else if (date1 > date2) return 1;
   else if (date1 < date2) return -1;
   else return 0;
} 
