// DEFINE VARIABLES

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

function isValidDate(dateStr) {
// Checks for the following valid date formats:
// MM/DD/YY	 MM/DD/YYYY	 MM-DD-YY	 MM-DD-YYYY
// Also separates date into month, day, and year variables

//var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2}|\d{4})$/;

// To require a 4 digit year entry, use this line instead:
 var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;

var matchArray = dateStr.match(datePat); // is the format ok?

if (matchArray == null) {
	//alert("Date is not in a valid format.")
	return false;
}
month = matchArray[1]; // parse date into variables
day = matchArray[3];
year = matchArray[4];

if (month < 1 || month > 12) { // check month range
	//alert("Month must be between 1 and 12.");
	return false;
}
if (day < 1 || day > 31) {
	//alert("Day must be between 1 and 31.");
	return false;
}
if ((month==4 || month==6 || month==9 || month==11) && day==31) {
	//alert("Month "+month+" doesn't have 31 days!")
	return false
}
if (month == 2) { // check for february 29th
	var g = parseInt(year / 4);
	if (day > 29 || (day == 29 && (year / 4) != g)) {
		//alert("February in "+year+" doesn't have "+day+" days!")
		return false;
	 }
}
return true;	// date is valid
}

function isEmpty(s){
// Check whether string s is empty.
	 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;

		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;
}

// E-mail Validation
function isEmail (s)
{	 if (isEmpty(s)) 
			 if (isEmail.arguments.length == 1) return defaultEmptyOK;
			 else return (isEmail.arguments[1] == true);
	 
		// is s whitespace?
		if (isWhitespace(s)) return false;
		
		// there must be >= 1 character before @, so we
		// start looking at character position 1 
		// (i.e. second character)
		var i = 1;
		var sLength = s.length;

		// look for @
		while ((i < sLength) && (s.charAt(i) != "@"))
		{ i++
		}

		if ((i >= sLength) || (s.charAt(i) != "@")) return false;
		else i += 2;

		// look for .
		while ((i < sLength) && (s.charAt(i) != "."))
		{ i++
		}

		// there must be at least one character after the .
		if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
		else return true;
}

function dateDiff(date1, date2){
var msPerDay = 24 * 60 * 60 * 1000 ; // Number of milliseconds per day

	var ddate1 = new Date(date1)
	var ddate2 = new Date(date2)
	var nDays = (ddate2.getTime() - ddate1.getTime()) / msPerDay;

	return Math.ceil(nDays);
}

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

function isNumber(objField)
{
	var strField = new String(objField.value);
	
	if (isWhitespace(strField)) return false;

	var i = 0;

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

	return true;
}

/* Valida la fecha a partir del dia y el mes */
function esValidaFecha(sDia, sMes)
{
	var dFecha = new Date();
	var nDia = parseInt(sDia, 10);
   	var nMes = parseInt(sMes, 10);
	var nDiaActual = dFecha.getDate();
	var nMesActual = dFecha.getMonth() + 1;
	var nAnoActual = dFecha.getFullYear();

	if ((nMes < nMesActual) || ((nMes == nMesActual) && (nDia <= nDiaActual))){
		++nAnoActual;
	}
	
	if(isValidDate(nMes + "/" + nDia + "/" + nAnoActual)){
		return nMes + "/" + nDia + "/" + nAnoActual;
	}
	else{
		return "";
	}
	
}

function esMayorFecha(sFecha1, sFecha2)
{
	return ! (dateDiff(sFecha1, sFecha2) < 1);
}