<!--

// This file contains the data validation JavaScript functions.
// It is included in the HTML pages with forms that need these
// data validation routines.

/****************************************************************/
/* 
	IsEmpty:
	Checks if the string s is empty.
*/

function IsEmpty(s)
{   return ((s == null) || (s.length == 0))
}

/****************************************************************/

/* 
	Required:
	Checks if the string s is composed only by whitespaces. 
	Use it to to verify required fields.
*/

function Required (objField,strFieldName)
{   
	var strTest = new String(objField.value);
	var bResult = true;
	var cant = 0;

	if (!objField.disabled) {
		// Is s empty?
		if (IsEmpty(strTest)) 
			bResult = false;

		// 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.

		var i;
		for (i = 0; i < strTest.length; i++)
			if (strTest.charAt(i) == " ") 
				cant = cant + 1
				
		if (strTest.length == cant)
			bResult = false;
		else
			bResult = true;

		if (!bResult)
		{
			alert("Please complete the field " + strFieldName + ".")
			objField.focus();
			objField.select();
		}
	}
	
	return bResult;
}


/****************************************************************/

/* 
	RequiredCombo:
	Checks if the string s is composed only by whitespaces. 
	Use it to to verify required fields.
*/

function RequiredCombo (objField,strFieldName)
{   
	var strTest = new String(objField.value);
	var bResult = true;
	var cant = 0;

	if (!objField.disabled) {
		// Is s empty?
		if (IsEmpty(strTest)) 
			bResult = false;

		// 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.

		var i;
		for (i = 0; i < strTest.length; i++)
			if (strTest.charAt(i) == " ") 
				cant = cant + 1
				
		if (strTest.length == cant)
			bResult = false;
		else
			bResult = true;

		if (!bResult)
		{
			alert("Please complete the field " + strFieldName + ".")
			objField.focus();
		}
	}
	
	return bResult;
}

/****************************************************************/

/* 
	RequiredCheked:
	Checks if the object is not selected. 
	Use it to to verify required fields.
*/

function RequiredChecked (objField,strFieldName)
{   
	var bResult = true;
	
	if (!objField.disabled) {
		if (objField.checked != true) {
			bResult = false
		}
				
		if (!bResult)
		{
			alert(strFieldName + ".")
			objField.focus();
		}
	}
	return bResult;
}

/****************************************************************/

/*
	IsText:
	Checks if the field has only text characters.
	Then, verifies the text length.	
*/

function IsText(objField,iLength,strFieldName)
{
	// Todas las letras del español
	var checkOK = "ABCDEFGHIJKLMNñÑOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzÁÉÍÓÚáéóúüñÑ0123456789-/#$%&?.,()=@áíóúé ";
	var bResult = true;
	
	if (!objField.disabled) {
	
		var strTest = objField.value;
		var strAlert = "Please type only letters and / or spaces in the field " + strFieldName + ".";
	
	
		for (i = 0;  i < strTest.length;  i++)
		{
			ch = strTest.charAt(i);
			for (j = 0;  j < checkOK.length;  j++)
			if (ch == checkOK.charAt(j))
				break;
			if (j == checkOK.length)
			{
				bResult = false;
				break;
			}
		}

		if (strTest.length > iLength) 
		{
			strAlert = "The field " + strFieldName + " cant have more than  " + iLength + " characters.";
			bResult = false;
		}

		if (!bResult)
		{
			alert(strAlert);
			objField.focus();
			objField.select();
		}
	}

	return bResult;
}

/****************************************************************/

/* 
	IsNumber:
	Returns true if the string is a number into the range specified.
	If the string is nonnumeric or out of range, false is passed back.  
	If iNumDec > 0, verifies the position of the decimal dot.
	If objField is empty, uses strFieldName as the string to verify.
*/

function IsNumber(objField,iMin,iMax,iNumDec,strFieldName)
{
	var strTest,strAlert;
	var bResult = true;

	if (!objField.disabled) {
	
		if (objField != "") 
			strTest = new String(objField.value);
		else
			strTest = new String(strFieldName);
		
		var i = 0;
		
		for (i = 0; i < strTest.length; i++)
		{
			c = strTest.charAt(i);
			if (bResult && c != '.' && c != '-'  && (c < '0' || c > '9' ))
			{		
				strAlert = "The field " + strFieldName + " is numeric. Dont write characters differents than numbers.";
				bResult = false;
				break;
				
			}
		}
			
		iDot = strTest.indexOf(".");
		if (iDot > 0)
		{
			if (iNumDec > 0)
			{
				if (bResult && (iDot < (strTest.length - iNumDec - 1) || iDot == strTest.length -1 ))
				{
					strAlert = "The field " + strFieldName + " can only have " + iNumDec + " decimal digits.";
					bResult = false;
				}
				else
					iTest = parseFloat(strTest);
			}
			else
			{
				strAlert = "The field " + strFieldName + " does not accept decimals.";
				bResult = false;
			}
		}
		else
			iTest=parseInt(strTest);

		if (bResult && ((iTest < iMin) || (iTest > iMax)))
		{
			strAlert = "The number in the field " + strFieldName + " must be beteewn " + iMin + " and " + iMax;
			bResult = false;
		}

		// If objField is empty, don't display message
		if (!bResult && objField != "")
		{
			alert(strAlert);
			objField.focus();
			objField.select();
		}
	}
	
	return bResult;
}

/****************************************************************/

/*	IsDate:
	Checks to see if the strDate string is a valid date.  A valid
	date is defined as any of the following: MM/DD/YYYY, M/D/YYYY.
	Uses function IsRangeNumber.
*/

function IsDate(objField,strFieldName)
{
	var strAlert = "The field format " + strFieldName + " is DD/MM/AAAA.\nWrite the date using this format.";
	var iMonth = 0,iDay = 0,iYear;
	var bResult = true;

	if (!objField.disabled) {
		var strTest = new String(objField.value);
	
		if (IsEmpty(strTest))
			return true;

		var i = 0, count = strTest.length, j = 0;

		// Day	
		while (strTest.charAt(i) != "/" && i < count)
			i++;

		if (i == count || i > 2)
			bResult = false;
	
		var addOne = false;

		if (i == 2) 
			addOne = true;

		iDay = strTest.substring(0,i);

		// Day
		j = i+1;
		i = 0;

		while (strTest.charAt(i+j) != "/" && i+j < count)
			i++;

		if (bResult && (i+j == count || i > 2))
			bResult = false;
	
		iMonth  = strTest.substring(j,i+j);
		//alert("("+IsNumber("",1,12,0,iMonth)+")")
		// Year
		j = i+3;
		i = 0;

		if (addOne) 
			j++;

		while (i+j < count)
			i++;

		if (i != 4) 
			bResult = false;

		iYear = strTest.substring(j,i+j);

		if(iDay.charAt(0)=='0' )
			iDay = iDay.substring(1,2);

		if(iMonth.charAt(0)=='0' )
			iMonth = iMonth.substring(1,2);

		if (bResult && (!IsNumber("",1,31,0,iDay) || !IsNumber("",1,12,0,iMonth) || !IsNumber("",1901,2099,0,iYear)))
		{
			strAlert = "Please check day, month and year for " + strFieldName + ",\nfor the field."
			bResult = false; 
		}

		// Verifies month and day are consistent
		if (bResult && (iMonth == 4 || iMonth == 6 || iMonth == 9 || iMonth == "04" || iMonth == "06" || iMonth == "09" || iMonth == 11) && (iDay > 30))
		{
			strAlert = "In the field " + strFieldName + ",\nthe day value (" + iDay + ") does not match with the month value (" + iMonth + ")."
			bResult = false; 
		}
	
		if (bResult && iMonth == 2)
		{
			var iMax = 28;
			
			if (iYear % 4 == 0)
				iMax = 29;
			if (iDay > iMax)
			{
				strAlert = "In the field " + strFieldName + ",\nthe day value (" + iDay + ") does not match with the month value (" + iMonth + ")."
				bResult = false; 
			}
		}
	
		if (!bResult)
		{
			alert(strAlert);
			objField.focus();
			objField.select();
		}
	}
	
	return bResult;
}

/*************************************************************************
/* Function name	:	FechaMayor
 Sypnosis		:	Dice si la fecha FechaP es mayor que FechaC
 Parameters	:	FechaP de tipo Date
                   FechaC de tipo Date

 Returns		:	true si FechaP > FechaC
                   false de los contrario
*/
function FechaMayor(objFechaP, objFechaC, strFieldName) {
    var iMonth = 0,iDay = 0,iYear;
	var iMonth1 = 0,iDay1 = 0,iYear1;
	var bResult = true;

	if (!objFechaP.disabled) {
		var strFechaP = new String(objFechaP.value);
		var strFechaC = new String(objFechaC.value);
		
		var strAlert = "The date in the field " + strFieldName + " is not bigger than " + strFechaC  + ".";
	
		var i = 0, count = strFechaP.length, j = 0;

		// Day	
		while (strFechaP.charAt(i) != "/" && i < count)
			i++;

		var addOne = false;

		if (i == 2) 
			addOne = true;

		iDay = strFechaP.substring(0,i);

		// Day
		j = i+1;
		i = 0;

		while (strFechaP.charAt(i+j) != "/" && i+j < count)
			i++;

		iMonth  = strFechaP.substring(j,i+j);
	
		// Year
		j = i+3;
		i = 0;

		if (addOne) 
			j++;

		while (i+j < count)
			i++;

		iYear = strFechaP.substring(j,i+j);

		if(iDay.charAt(0)=='0' )
			iDay = iDay.substring(1,2);

		if(iMonth.charAt(0)=='0' )
			iMonth = iMonth.substring(1,2);

		//alert(iYear + "/" + iMonth + "/" + iDay)
		var objFecha1 = new Date();
		objFecha1.setDate(parseInt(iDay));
		objFecha1.setMonth(parseInt(iMonth)-1);
		objFecha1.setYear(parseInt(iYear));
		objFecha1.setHours(0);
		objFecha1.setMinutes(0);
		objFecha1.setSeconds(0);
		
		//FechaC
		i = 0;
		count = strFechaC.length;
		j = 0;
		
		// Day	
		while (strFechaC.charAt(i) != "/" && i < count)
			i++;

		addOne = false;

		if (i == 2) 
			addOne = true;

		iDay1 = strFechaC.substring(0,i);
		
		// Day
		j = i+1;
		i = 0;

		while (strFechaC.charAt(i+j) != "/" && i+j < count)
			i++;

		iMonth1  = strFechaC.substring(j,i+j);
		
		// Year
		j = i+3;
		i = 0;

		if (addOne) 
			j++;

		while (i+j < count)
			i++;

		iYear1 = strFechaC.substring(j,i+j);
		
		if(iDay1.charAt(0)=='0' )
			iDay1 = iDay1.substring(1,2);

		if(iMonth1.charAt(0)=='0' )
			iMonth1 = iMonth1.substring(1,2);
		
		//alert(iYear1 + "/" + iMonth1 + "/" + iDay1)
		var objFecha2 = new Date();
		objFecha2.setDate(parseInt(iDay1));
		objFecha2.setMonth(parseInt(iMonth1)-1);
		objFecha2.setYear(parseInt(iYear1));
		objFecha2.setHours(0);
		objFecha2.setMinutes(0);
		objFecha2.setSeconds(0);
		
		if (objFecha1 > objFecha2)
		{
			//alert(objFecha1 + " - " + objFecha2);
			bResult = true; 
		}
		else {
			//alert(objFecha1 + " / " + objFecha2);
			bResult = false; 
		}

		if (!bResult)
		{
			alert(strAlert);
			objFechaP.focus();
			objFechaP.select();
		}
	}
	
	return bResult;
}

/****************************************************************/

/*  
	IsTime:
	Check to see if the string passed in is a valid time.
	A valid time is defined as a string like HH:MMAM or HH:MMPM
	Uses function IsNumber.
*/

function IsTime(objField,strFieldName)
{
	var strAlert = "The field format " + strFieldName + " is 00:00am/pm.\nWrite the date with this format.";
	var bResult = true;

	if (!objField.disabled) {
		var strAux = new String(objField.value);
		var strTest = new String (strAux.toUpperCase());
	
		if (IsEmpty(strTest))
			return true;

		// AM,PM
		if ((strTest.indexOf("PM",1) == -1) && (strTest.indexOf("AM",1) == -1))
			bResult = false;
	
		// :
		if (strTest.indexOf(":",0) == 0)
			bResult = false;
			
		// Position of :
		var nColonPlace = strTest.indexOf(":",1);
		if (((nColonPlace + 5) < strTest.length) || ((nColonPlace + 4) > (strTest.length - 1)) ||		(nColonPlace > 2))
			bResult = false;

		// Hour and minute numeric values
		iHour = parseInt(strTest.substr(0,nColonPlace));
		iMinute = parseInt(strTest.substr(nColonPlace+1,nColonPlace+3));
	
		if (bResult && (!IsNumber("",1,12,0,iHour) || !IsNumber("",0,60,0,iMinute)))
		{
			strAlert = "Please check the hour and minute in the field " + strFieldName + "."
			bResult = false;
		}
	
		if (!bResult)
		{
			alert(strAlert);
			objField.focus();
			objField.select();
		}
	}
	
	return bResult;
}

/****************************************************************/

/*
	ValidaTexto:
	Checks if the field has only text characters.
	Then, verifies the text length.	
*/

function ValidaTexto(objField,iLength,strFieldName)
{
	// Todas las letras del español
	var checkOK = "ABCDEFGHIJKLMNñÑOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzÁÉÍÓÚáéóúüñÑáíóúé ";
	var strAlert = "Please enter only letters and/or spaces in the field " + strFieldName + ".";
	var bResult = true;
	
	if (!objField.disabled) {
		var strTest = objField.value;
	
		for (i = 0;  i < strTest.length;  i++)
		{
			ch = strTest.charAt(i);
			for (j = 0;  j < checkOK.length;  j++)
			if (ch == checkOK.charAt(j))
				break;
			if (j == checkOK.length)
			{
				bResult = false;
				break;
			}
		}

		if (strTest.length > iLength) 
		{
			strAlert = "The field " + strFieldName + " can not have more than " + iLength + " characters.";
			bResult = false;
		}

		if (!bResult)
		{
			alert(strAlert);
			objField.focus();
			objField.select();
		}
	}
	return bResult;
}

//Chequear si el correo es valido.
 function CheckMail(objField)
 {
 	var txt = new String(objField.value);
 	txt = txt.toLowerCase(txt);
 	if (txt.value != '')
 	{
 		if (txt.indexOf("@") < 3)
		{	
			alert("The e-mail written is wrong. Please check the user name or include the '@' simbol ");
			objField.focus();
			objField.select();
			return false;
		}
		
		/* Comentado ya que no todos los dominios estan aqui relacionados..
		
		if ((txt.indexOf(".com") < 5) && (txt.indexOf(".org") < 5) && (txt.indexOf(".gov") < 5) && 
			(txt.indexOf(".net") < 5) && (txt.indexOf(".mil") < 5) && (txt.indexOf(".edu") < 5) && 
			(txt.indexOf(".uk") <5))
		{
			alert("The e-mail wrtitten is wrong. Please check the domain name. (You should include the .com, .edu, .net, .org, .gov, .uk o .mil suffix)");
	   		objField.focus();
	  		objField.select();
			return false;
	 	}
	 	*/
 	}
 	return true;
 }
// -->