


// Se le pasa el nombre del campo de texto donde se ingresó la fecha. Devuelve true si el campo está vacío o
// contiene una fecha válida en formato dia/mes/anio. Así mismo, quita los espacios en blanco que pueda haber 
// entre medio
function isFechaValida(inputName){
	
//	var inputField = document.getElementById(inputName);
	var inputField = obtenerElemento(inputName);
	var fecha = inputField.value;
	
	//if (fecha=='') return true;
		
	var index1 = fecha.indexOf('/');
	var index2 = fecha.indexOf('/', index1+1);
	var index3 = fecha.indexOf('/', index2+1)	;


	if (index1==-1 || index1==0 || index1>2 || index2==-1 || index2==index1+1 || index3>-1 || fecha.length==index2+1)
		return false;
		

	var dia = fecha.substring(0,index1);
	var mes = fecha.substring(index1+1, index2);	
	var anio = fecha.substring(index2+1, fecha.length);	

	if (isNaN(dia))
		return false;
	if (isNaN(mes))
		return false;
	if (isNaN(anio))
		return false;
		 
    var daysInMonth = DaysArray(12)
	//if (isNaN(dia) || dia<=0 || dia>31)
	if (isNaN(dia) || dia<=0 || dia>31 || (mes==2 && dia>daysInFebruary(anio)) || dia> daysInMonth[mes])
		return false;

	if (isNaN(mes) || mes<=0 || mes>12)
		return false;
	
	if (isNaN(anio) || anio<0)
		return false;

	
	inputField.value=dia+"/"+mes+"/"+anio;

	return true;
}

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 daysInFebruary (year){
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}

function isFechaValidaConEspacio(inputName){

	var inputField = obtenerElemento(inputName);
	var fecha = inputField.value;
	
	if (fecha=='') return true;
	else
		return isFechaValida(inputName);
}


function corregirHora(hora){

	if(hora == ''){
		return '';
	}
	else{

		var indice = hora.indexOf(':'); 
		var fechaActual = new Date();
		var fecha = null;
		if(indice == '1'){
			var hh = hora.charAt(0);
			var mm = hora.charAt(2) + hora.charAt(3);
			fecha = new Date(fechaActual.getYear(),fechaActual.getMonth(),fechaActual.getDate(),hh,mm,'00');
		}
		if(indice == '2'){
			var hh = hora.charAt(0) + hora.charAt(1);
			var mm = hora.charAt(3) + hora.charAt(4);
			fecha = new Date(fechaActual.getYear(),fechaActual.getMonth(),fechaActual.getDate(),hh,mm,'00');
		}

		var H=fecha.getHours();
		var m=fecha.getMinutes();
		return LZ(H) + ":" + LZ(m);
//		return formatDate(fecha,'HH:mm');
			
	}
}

function LZ(x) {return(x<0||x>9?"":"0")+x}


function isHora(hora){
	if(hora == ''){
		return true;
	}
	else{
		var indice = hora.indexOf(':'); 
		
		if(indice == '-1'){
			return false;
		}
		else{
			var fechaActual = new Date();
			var fecha = null;
			var hh = null;
			var mm = null;
	
			if(indice == '1'){
				hh = hora.charAt(0);
				mm = hora.charAt(2) + hora.charAt(3);
				if (isNaN(hh))
					return false;
				if (isNaN(mm))
					return false;
				fecha = new Date(fechaActual.getYear(),fechaActual.getMonth(),fechaActual.getDate(),hh,mm,'00');
			}
			if(indice == '2'){
				hh = hora.charAt(0) + hora.charAt(1);
				mm = hora.charAt(3) + hora.charAt(4);
				if (isNaN(hh))
					return false;
				if (isNaN(mm))
					return false;
				fecha = new Date(fechaActual.getYear(),fechaActual.getMonth(),fechaActual.getDate(),hh,mm,'00');
			}
			
			if((hh == null) || (!((0<=parseInt(hh)) && (parseInt(hh)<25)))){
				return false;
			}

			if((mm == null) || (!((0<=parseInt(mm)) && (parseInt(mm)<=59)))){
				return false;
			}
			
			if(fecha != 'NaN'){
				return true;
			}
			else{
				return false;
			}
		}
	}
	
}

function isHoraAnt(hora){

	if(hora == ''){
		return true;
	}
	else{
	
		if(hora.charAt(2) != ':'){
			return false;
		}
		else{
			
			if((validarNumero(hora.charAt(0)) != 'NaN') &&
			(validarNumero(hora.charAt(1)) != 'NaN') &&
			(validarNumero(hora.charAt(3)) != 'NaN') &&
			(validarNumero(hora.charAt(4)) != 'NaN')){
				return true;
			}
			else{
				return false;
			}
			
		}
	
	}
}

function validarNumero(c_numero) 
{ 
   //chequeo la longitud de c_numero: 
   // Si (c_numero.length es igual a Cero) quiere decir que c_numero es una cadena Vacía. 
   // Si (c_numero.length es distinto(mayor) de Cero) podemos asegurar que c_numero contiene por lo menos una letra 
   //a la cual se le puede hacer la validación 
   if (c_numero.length == 0) 
   { 
       return "NaN"; 
   } 
   else 
   { 
       //Se recorre c_numero por todos sus caracteres chequeando que todos sean dígitos 
       //la condición >="0" y <="9" es basada en el valor ascii que tienen los números en la tabla ascii. 
       //Si alguno de los caracteres no es un número la función retornará un NaN 
       //Si no retornará el Número 
       for (i = 0; i < c_numero.length; i++) 
       { 
          if (!((c_numero.charAt(i) >= "0") && (c_numero.charAt(i) <= "9"))) 
          return "NaN"; 
       } 
       return c_numero; 
   } 
} 




// getElementById Special to handle quirky browsers
// most will use getElementById()
function obtenerElemento(id){
	var obj = null;
	if(document.getElementById(id) != null){
		// Prefer the widely supported W3C DOM method, if available
		obj = document.getElementById(id);
	}
	else {
//	else if(document.all){
		//Branch to use document.all on document.all only browsers. Requires that IDs are unique to the page
		//	and do not coincide with NAME attributes on other elements
		obj = document.forms[0].elements[id];
	}
	// If no appropriate element retrieval mechanism exists on	this browser this function always returns null
	return obj;
}

	function setMensaje(nombreVariable, strMensaje){
		
		var mensaje = document.getElementById(nombreVariable);
		
		if(document.all){//IE
			mensaje.innerText = strMensaje;
		}
		else{
			if (document.getElementById){ 
				mensaje.textContent = strMensaje;
			}
			else
				alert(Res.getString("ErrorBrowserSupport"));
		}
	}

