
var validarincluido = "sim";

// whitespace characters
var whitespace = " \t\n\r";

// Check whether string s is empty.
function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}


// Returns true if string s is empty or
// whitespace characters only.
function isWhitespace (s)
{   var i;

    // Is s empty?
    if (isEmpty(s)) return true;

    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {
		// Check that current character isn't whitespace.
		var c = s.charAt(i);

		if (whitespace.indexOf(c) == -1) return false;
    }

    // All characters are whitespace.
    return true;
}

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


// isEmail (STRING s [, BOOLEAN emptyOK])
//
// Email address must be of form a@b.c ... in other words:
// * there must be at least one character before the @
// * there must be at least one character before and after the .
// * the characters @ and . are both required
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function isEmail (mail)
{
	var er = new RegExp(/^[_+a-z0-9-]+(\.[_+a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]{1,})*\.([a-z]{2,}){1}$/);

	if(er.test(mail))
	{
		return true;
	}
	else
	{
        return false;
	}
}

function ForceEmail(objField)
{
	var strField = new String(objField.value);

	if(!isEmail (strField)){
		//endereço de e-mail invalido
		objField.focus();
		return -1;
	}
	else
		return 0;
}


function ForceLettersNumbers(objField)
{
	var strField = new String(objField.value);

	var reg = /[^a-zA-Z0-9]/ig;

	if(reg.test(strField)){
		//O campo só permite letras e números. Não são permitidos espaços
		objField.focus();
		return -1;
	}else{
		return 0;
	}
}

/*
para além dos números permite o caracter passado por paramentro
CUIDADO: COM os caracteres especiais
*/
function ForceCustomChars(objField)
{
	var strField = new String(objField.value);

	var reg = /[^a-zA-Z0-9._-]/ig;

	if(reg.test(strField)){
		//O campo só permite letras, números, - , _ e . . Não são permitidos espaços.
		objField.focus();
		return -1;
	}else{
		return 0;
	}
}

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

// Checks to see if a required field is blank.  If it is, a warning
// message is displayed...

function ForceEntry(objField)
{
	var strField = new String(objField.value);
	if (isWhitespace(strField)) {
		objField.focus();
		objField.select();
		return -1;
	}

	return 0;
}

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

// Returns true if the string passed in is a valid number
//  (no alpha characters), else it displays an error message

function ForceNumber(objField)
{
	var strField = new String(objField.value);

	if (isWhitespace(strField)) return 0;

	var i = 0;

	for (i = 0; i < strField.length; i++)
		if (strField.charAt(i) < '0' || strField.charAt(i) > '9') {
			objField.focus();
			return -1;
		}

	return 0;
}




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

// Returns true if the string passed in is a valid money
//  (no alpha characters except a decimal place),
//   else it displays an error message

function ForceMoney(objField)
{
	var strField = new String(objField.value);

	var er = new RegExp(/^[0-9.]+(\,[0-9]{1,2})?$/);

	if(er.test(strField))
	{
		return 0;
	}
	else
	{
        return -1;
	}
}



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

// This function determines if the string passed in is a valid
// Portugal zip code.  It accepts either ####-###.  If the
// string is valid, it returns true, else false.
function isZipcode(strZip)
{
	var s = new String(strZip);

	if (s.length != 8)
		// inappropriate length
		return false;

	for (var i=0; i < s.length; i++)
		if ((s.charAt(i) < '0' || s.charAt(s) > '9') && s.charAt(i) != '-')
			return false;

	return true;
}

function ForceZipcode(objField)
{
	var strField = new String(objField.value);
	if(!isZipcode (strField)){
		objField.focus();
		return -1;
	}
	else
		return 0;
}

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

// This function ensures that a field is less than or equal to the
// Length passed in.  You must call this function with the element
// name in your form (for example: "ForceLength(document.forms[0].txtElement)"
// as opposed to "ForceLength(document.forms[0].txtElement.value)"
// If the field's value is too large, an error message is displayed
// and false is returned, else true is returned.

function ForceLength(objField, nLength)
{
	var strField = new String(objField.value);

	if (strField.length > nLength) {
		objField.focus();
		objField.select();
		return -1;
	} else
		return 0;
}


////////////////////////////////////////////////////////////////////////////////////////
// DATE VALIDATION

// Declaring valid date character, minimum year and maximum year
var dtCh= "-";
var minYear=1900;
var maxYear=2100;

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 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++){
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}

function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   }
   return this
}

function isDate(objField){
	var dtStr = objField.value;
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strYear=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strDay=dtStr.substring(pos2+1)
	strYr=strYear

	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){

		objField.focus();
		objField.select();
		return -1;
	}
	if (strMonth.length<1 || month<1 || month>12){
		objField.focus();
		objField.select();
		return -2;
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		objField.focus();
		objField.select();
		return -3;
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		objField.focus();
		objField.select();
		return -4;
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){

		objField.focus();
		objField.select();
		return -5;
	}
	return 0
}

function isHour(objField)
{
	var hourStr = objField.value;
	var pos = hourStr.indexOf(":");


	if(pos != 2 || hourStr.length < 5)
	{
		objField.focus();
		objField.select();
		return -1;
	}


	if(hourStr.charAt(0) < "0" ||  hourStr.charAt(0) > "2"){
		objField.focus();
		objField.select();
		return -2;
	}

	if(hourStr.charAt(1) < "0" ||  hourStr.charAt(1) > "9"){
		objField.focus();
		objField.select();
		return -3;
	}

	if(hourStr.charAt(0) == 2 && hourStr.charAt(1) > "3")
	{
		objField.focus();
		objField.select();
		return -4;
	}

	if(hourStr.charAt(3) < "0" ||  hourStr.charAt(3) > "5"){
		objField.focus();
		objField.select();
		return -5;
	}

	if(hourStr.charAt(4) < "0" ||  hourStr.charAt(4) > "9"){
		objField.focus();
		objField.select();
		return -6;
	}

	return 0;
}



function diferenca_data(data1,data2)
{
	//-1 => data1 < data2
	//0 => data1 == data2
	//1 => data1 > data2
	var data1_temp = data1.split("-");
	var data2_temp = data2.split("-");

	if(data1_temp[0]==data2_temp[0] && data1_temp[1]==data2_temp[1] && data1_temp[2]==data2_temp[2])
	{
		return 0;
	}
	else if(data1_temp[0] > data2_temp[0]){
		return 1;
	}
	else if(data1_temp[0] == data2_temp[0] && data1_temp[1] > data2_temp[1]){
		return 1;
	}
	else if(data1_temp[0] == data2_temp[0] && data1_temp[1] == data2_temp[1] && data1_temp[2] > data2_temp[2]){
		return 1;
	}

	return -1;

}

function diferenca_hora(hora1,hora2)
{
	//-1 => hora1 < hora2
	//0 => hora1 == hora2
	//1 => hora1 > hora2
	var hora1_temp = hora1.split(":");
	var hora2_temp = hora2.split(":");

	if(hora1_temp[0]==hora2_temp[0] && hora1_temp[1]==hora2_temp[1] )
	{
		return 0;
	}
	else if(hora1_temp[0]<=hora2_temp[0] && hora1_temp[1]<hora2_temp[1])
	{
		return -1
	}
	else
	{
		return 1;
	}
}