// JavaScript Document

function isUrl(valor,erro) {

if(valor=='') return true;
	var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
	
	var matchArray = valor.match(regexp); // is the format ok?
	
	if (matchArray == null) {
	alert(erro);
	return false;
	}  else return true;
}

function isProper(string,erro) {

   var iChars = "*|\":<>[]{}`\';()@&$#%";

   for (var i = 0; i < string.length; i++) {
      if (iChars.indexOf(string.charAt(i)) != -1)
         { alert(erro);
		 return false; }
   }
   return true;
} 
function ValidarCheck(campo,erro) {

	if((campo)==false)
	{
		alert(erro);
				return false;
	}
	else{ return true;}
}

    function checkmod(num) {
      val=0;
      for (pos=0; pos<num.length-1; ++pos) {
        val += (1 * num.charAt(pos)) * (9 - pos);
      }
      ctl = val % 11 ? (11 - val % 11) % 10 : 0;
    
      return ctl == (1 * num.charAt(pos));
    }

    function validarNIF(num,erro) {
      if(num.length != 9) { alert(erro); return false;}
      if(checkmod(num)){ return true; }else { alert(erro); return false;};

    }
    function validarNIFPT(num,num2,erro) {
		if(num2 != ''){ return true; }
		else { 
			if(num.length == 0) { alert(erro); return false;}
		  	if(num.length != 9) { alert(erro); return false;}
		  	if(checkmod(num)){ return true; }else { alert(erro); return false;};
	  } 
	  
    }


function ValidarHora(campo,erro) {
	if(campo=='') return true;
	else{
		if(campo<25) return true;
			else if(campo >=25)
			{
				alert(erro);
				return false;
			}
	}
}

function ValidarMin(campo,erro) {
	if(campo=='') return true;
	else{
		if(campo<61) return true;
			else if(campo >=61)
			{
				alert(erro);
				return false;
			}
	}
}

function ValidarData(dateStr) {

if (dateStr=='') return true;

var datePat = /^(\d{4})(\/|-)(\d{1,2})(\/|-)(\d{1,2})$/;
var matchArray = dateStr.match(datePat); // is the format ok?

if (matchArray == null) {
alert("ERROR: Date Format is dd-mm-yyyy.");
return false;
}

day = matchArray[5]; // p@rse date into variables
month = matchArray[3];
year = matchArray[1];

if (month < 1 || month > 12) { // check month range
alert("Mês entre 1 e 12.");
return false;
}

if (day < 1 || day > 31) {
alert("Dias entre 1 e 31.");
return false;
}

if ((month==4 || month==6 || month==9 || month==11) && day==31) {
alert("O mês "+month+" não tem 31 dias!")
return false;
}

if (month == 2) { // check for february 29th
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day > 29 || (day==29 && !isleap)) {
alert("Fevereiro de " + year + " não tem " + day + " dias!");
return false;
}
}
return true; // date is valid
}


function ValidarEmail(email){
    var erro = "Email inválido: " + email;
	
	if (email == '') { return true; }
	
    if( email.indexOf('@',0) <= 0  || email.indexOf(';',0) != -1
     || email.indexOf(' ',0) != -1 || email.indexOf('/',0) != -1
     || email.indexOf(';',0) != -1 || email.indexOf('<',0) != -1
     || email.indexOf('>',0) != -1 || email.indexOf('*',0) != -1
     || email.indexOf('|',0) != -1 || email.indexOf('`',0) != -1
     || email.indexOf('&',0) != -1 || email.indexOf('$',0) != -1
     || email.indexOf('!',0) != -1 || email.indexOf('"',0) != -1
     || email.indexOf(':',0) != -1 )
       { alert(erro); return false; }
    else return true;
    }

function ValidarNumero(numero,erro) {

    if (numero == "") return true;
    else
      if( isNaN(parseInt(numero)) || parseInt(numero) < 0 ) {
        alert(erro);
        return false;
        }
      else return true;
    }

function ValidarIgual(id1,id2,erro) {
    if (id1 == "") return true;
    else
      if( id1 != id2 ) {
        alert(erro);
        return false;
        }
      else return true;
    }

function ValidarCoerencia(h1,h2,m1,m2,erro) {
      if ( trim(h1) > trim(h2) ) 
	  {
        alert(erro);
        return false;
        }
      else if( trim(h1) == trim(h2) )
	  {
		  	if( m2 <= m1 )
			{
				alert(erro);
				return false;
			}
			else { return true; }
	  }
	  else return true;
}
	
function ValidarVazio(campo,erro) {
	
    if (trim(campo) == "") {
		alert(erro);
	   return false;
	   }
      else return true;
    }

function ValidarVazioOk(campo) { //devolve true se for vazio e false se nao for
	
    if (trim(campo) == "") {
	   return true;
	   }
      else return false;
    }
	
	function  Validar_inteiro(campo,erro)
{
	if(isNaN(campo))
   {
     alert(erro);
     return (false);
   }
   else return true;
}

function trim(inputString) {
   // Removes leading and trailing spaces from the passed string. Also removes
   // consecutive spaces and replaces it with one space. If something besides
   // a string is passed in (null, custom object, etc.) then return the input.
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   return retValue; // Return the trimmed string back to the user
} // Ends the "trim" function


function ValidarFileField_swf(campo, alertbox) {
      if (campo == "") return true;
	  ext=campo.substr(campo.lastIndexOf(".")).toLowerCase();
		  if (ext!=".jpeg"&&ext!=".jpg"&&ext!=".gif"&&ext!=".swf"&&ext!=".png"){
			 if (alertbox!="") {
			   alert(alertbox);
			}
			return false;
		}
		else {
		  return true;
		}
}

function ValidarFileField_SWFOnly(campo, alertbox) {
      if (campo == "") return true;
	  ext=campo.substr(campo.lastIndexOf(".")).toLowerCase();
		  if (ext!=".swf"){
			 if (alertbox!="") {
			   alert(alertbox);
			}
			return false;
		}
		else {
		  return true;
		}
}
function ValidarFileField_pngjpg(campo, alertbox) {
      if (campo == "") return true;
	  ext=campo.substr(campo.lastIndexOf(".")).toLowerCase();
		  if (ext!=".jpeg"&&ext!=".jpg"&&ext!=".png"){
			 if (alertbox!="") {
			   alert(alertbox);
			}
			return false;
		}
		else {
		  return true;
		}
}

function ValidarJPG(campo, alertbox) {
	 if (campo == "") return true;
      ext=campo.substr(campo.lastIndexOf(".")).toLowerCase();
      if (ext!=".jpeg"&&ext!=".jpg"&&ext!=".png"&&ext!=".gif"&&ext!=".bmp"){
         if (alertbox!="") {
           alert(alertbox);
        }
        return false;
    }
    else {
      return true;
    }
}

function ValidarMOV(campo, alertbox) {
	  if (campo == "") return true;
	  
      ext=campo.substr(campo.lastIndexOf(".")).toLowerCase();
      if (ext!=".mov"){
         if (alertbox!="") {
           alert(alertbox);
        }
        return false;
    }
    else {
      return true;
    }
}


function ValidarFileField(campo, alertbox) {
      ext=campo.substr(campo.lastIndexOf(".")).toLowerCase();
      if (ext!=".jpeg"&&ext!=".jpg"&&ext!=".gif"&&ext!=".bmp"&&ext!=".png"){
         if (alertbox!="") {
           alert(alertbox);
        }
        return false;
    }
    else {
      return true;
    }
}
