﻿/*
  File         : Validations.js
  Company      : InterDev S.A.
  Creation Date: 2008-05-06
  Developer(s) : Nelson Rodriguez Caliz
  Summay       :
  History      : 1.0.0 Initial Release.
  
*/


//
function isInteger(s){
	var i;
	var val = s.value;
	val = val.replace('.', '');
	val = val.replace(',', '');
	var lng = val.length;
	for (i = 0; i < lng; i++){   
		// Verifica si cada caracter es numerico.
		var c = val.charAt(i);
		if (((c < "0") || (c > "9"))) return false;
	}
	// Todos los caracteres son numericos.
	return true;
}

//
function daysInFebruary(year){
	// Febrero tiene 29 dias en cualquier año disivible entre 4.
	// Excepto para siglos los cuales no son divisibles entre 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(Mes, Dia, Ano){
	var daysInMonth = DaysArray(12);		// Determina la cantidad maxima de dias en el mes.
	//
	var strMonth=Mes;	// Recoge el mes.
	var strDay=Dia;		// Recoge el dia.
	var strYear=Ano;	// Recoge el año.
	//
	month=parseInt(strMonth);
	day=parseInt(strDay);
	year=parseInt(strYear);
	//
	if (strMonth.length<1 || month<1 || month>12 || isNaN(month)){
		alert("Mes inválido.");
		return false;
	}
	if (strDay.length<1 || isNaN(day) || day<1 || day>daysInMonth[month] || (month==2 && day>daysInFebruary(year))){
		alert("Día inválido");
		return false;
	}
	if (strYear.length != 4 || isNaN(year) || year==0){
		alert("Año inválido.");
		return false;
	}
	return true;
}


//