
var anioDesde = "1900";
var anioHasta = "2010";
var numeros="0123456789";
var separadorfecha="/";
var separadorhora=":";
var puntodecimal=".";
var signos="+-";
telefonos="0123456789-";

error= new creaerror();  
errores= new Array();

  
  
errores[1]="El campo está vacío.";
// fecha
errores[2]="Caracter ilegal en la fecha";
errores[3]="Faltan separadores de fecha";
errores[4]="Año incorrecto en una fecha. Debe ser mayor a " + anioDesde + " y menor que " + anioHasta;
errores[5]="Mes incorrecto en una fecha";
errores[6]="Día incorrecto en una fecha";
//hora
errores[7]="Caracter ilegal en la hora";
errores[8]="Faltan separadores de hora";
errores[9]="Horas incorrectas en una hora. Deben ser 'HH'";
errores[10]="Minutos incorrectos en una hora. Deben ser 'MM'";
errores[11]="Segundos incorrectos en una hora. Deben ser 'SS'";

// numeros
errores[12]="Caracter ilegal en un número";
errores[13]="Caracter ilegal (Debe ser +, - o un número)";
errores[14]="Solamente ingresó un símbolo";
errores[15]="Falta parte decimal";

//telefonos
errores[16]="Caracter Ilegal en un número telefónico. Solamente pueden ser números (0-9) o guiones (-)";
errores[17]="Por favor use un guión por bloque de números (999-999-9999)";
errores[18]="Un número telefónico no puede empezar con un guión";
errores[19]="Un número telefónico no puede terminar con un guión";

//email
errores[20]="Caracter ilegal en un email";

errores[21] = "La máxima longitud permitida para este campo es "
errores[22] = "La mínima longitud permitida para este campo es "

errores[100] = "Debe seleccionar una Opción";
errores[101] = "La Fecha debe ser posterior a Hoy";

// Crea un objeto que guarda un indice a la posición y al mensaje de error
function creaerror()
{
this.valor=0;
this.posicion=0;
return this
}
  
// Determina si un caracter es un número
function numero(car)
{
	return (numeros.indexOf(car)>=0)
}

// Determina si un año es bisiesto
function bisiesto(anio) 
{
	if (((anio % 4 == 0) && anio % 100 != 0) || anio % 400 == 0) 
		return true;
	return false;
}

// Comprueba si una fecha es correcta
function compruebafecha(contenido,error,desde,hasta)
{
   
	if ((contenido.length==0))
	{
		error.valor=1;
		error.posicion=1; 
		return false;
	} 
	var nsep=0;
	// Comprobación de la sintáxis de una fecha 
	for (var i=0; i<contenido.length; ++i)
	{
		var car=contenido.charAt(i);
		if (!numero(car)&&car!=separadorfecha)
		{
			error.valor=2;
			error.posicion=i+1; 
			return false;
		} 
		if (car==separadorfecha)
			nsep++ 
	}
	if (nsep!=2)
	{
		error.valor=3;
		error.posicion=i+1; 
		return false;
	}
	// Comprobación de la semántica de una fecha
	var pos1=contenido.indexOf(separadorfecha);
	var dia=contenido.substring(0,pos1);
	var pos2=contenido.indexOf(separadorfecha,pos1+1);
	var mes=contenido.substring(pos1+1,pos2); 
	var anio=contenido.substring(pos2+1,10);
	if (anio<desde||anio>hasta)
	{
		error.valor=4;
		error.posicion=6; 
		return false;
	} 
	if (mes<1||mes>12)
	{
		error.valor=5;
		error.posicion=2;
		return false;
	}
	if ((dia<1 || dia>31)||(mes==4&&dia>30)||(mes==6&&dia>30)
	||(mes==9&&dia>30)||(mes==11&&dia>30)
	||(mes==2&&bisiesto(anio)&&dia>29)
	||(mes==2&&!bisiesto(anio)&&dia>28))
	{
		error.valor=6;
		error.posicion=4;
		return false;
	}
	var mayor = comparaDosFechasCastellano(hoy,contenido)
	if (mayor == 1)
	{
		error.valor = 101;
		return false;
	}
	
	return true 
}
function compruebafechacombo(dia, mes, anio, error)
{
	if ((dia<1 || dia>31)||(mes==4&&dia>30)||(mes==6&&dia>30)
	||(mes==9&&dia>30)||(mes==11&&dia>30)
	||(mes==2&&bisiesto(anio)&&dia>29)
	||(mes==2&&!bisiesto(anio)&&dia>28))
	{
		error.valor=6;
		error.posicion=2;
		return false;
	} 
	return true 

}
 
var isNav4 = false, isNav5 = false, isIE4 = false;
if(navigator.appName == "Netscape") 
{
	if (navigator.appVersion < "5") 
	{
		isNav4 = true;
		isNav5 = false;
	}
	else
		if (navigator.appVersion > "4") 
		{
			isNav4 = false;
			isNav5 = true;
		}
}
else 
{
	isIE4 = true;
}

function compruebaemail(contenido, error)
{
	var correcto = sintaxisemail(contenido);
	if (!correcto)
	{
		error.valor=20;
		return false;
	}
	return true;
}

function sintaxisemail(str){
	supported = 0;
	if (window.RegExp) 
	{
		var tempStr = "a";
		var tempReg = new RegExp(tempStr);
		if (tempReg.test(tempStr)) supported = 1;
	}
	if (!supported) return (str.indexOf(".") > 2) && (str.indexOf("@") >0);
	var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
	var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
	return (!r1.test(str) && r2.test(str));
}


// Comprueba si una hora es correcta
function compruebahoraminutosegundo(contenido,error)
{
	if ((contenido.length==0))
	{
		error.valor=1;
		error.posicion=1; 
		return false;
	} 
	var nsep=0; 
	// Comprobación de la sintáxis de una hora 
	for (var i=0; i<contenido.length; ++i)
	{
		var car=contenido.charAt(i);
		if (!numero(car) && car!=separadorhora)
		{
			error.valor=7;
			error.posicion=i+1; 
			return false;
		} 
		if (car==separadorhora)
			nsep++ 
	}
	if (nsep!=2)
	{
		error.valor=8;
		error.posicion=i+1; 
		return false;
	}
	// Comprobación de la semántica de una hora 
	var pos1=contenido.indexOf(separadorhora);
	var horas=contenido.substring(0,pos1);
	var pos2=contenido.indexOf(separadorhora,pos1+1);
	var minutos=contenido.substring(pos1+1,pos2); 
	var segundos=contenido.substring(pos2+1,10);
	if (horas>12 || horas.length!=2)
	{
		error.valor=9;
		error.posicion=2; 
		return false;
	} 
	if (minutos>59 || minutos.length!=2)
	{
		error.valor=10;
		error.posicion=4;
		return false;
	}
	if (segundos>59 ||segundos.length!=2)
	{
		error.valor=11;
		error.posicion=6;
		return false;
	} 
		return true 
}
function compruebahoraminuto(contenido,error)
{
	if ((contenido.length==0))
	{
		error.valor=1;
		error.posicion=1; 
		return false;
	} 
	var nsep=0; 
	// Comprobación de la sintáxis de una hora 
	for (var i=0; i<contenido.length; ++i)
	{
		var car=contenido.charAt(i);
		if (!numero(car) && car!=separadorhora)
		{
			error.valor=7;
			error.posicion=i+1; 
			return false;
		} 
		if (car==separadorhora)
			nsep++ 
	}
	if (nsep!=1)
	{
		error.valor=8;
		error.posicion=i+1; 
		return false;
	}
	// Comprobación de la semántica de una hora 
	var pos1=contenido.indexOf(separadorhora);
	var horas=contenido.substring(0,pos1);
	//var pos2=contenido.indexOf(separadorhora,pos1+1);
	var minutos=contenido.substring(pos1+1,6); 
	//var segundos=contenido.substring(pos2+1,10);
	if (horas>12 || horas.length!=2)
	{
		error.valor=9;
		error.posicion=2; 
		return false;
	} 
	if (minutos>59 || minutos.length!=2)
	{
		error.valor=10;
		error.posicion=4;
		return false;
	}
		return true 
}
	  
// Determina si un carácter es un signo positivo o negativo
function signo(car)
{
	return (signos.indexOf(car)>=0)
}

    
// Comprueba si el contenido es un número es natural incluido el 0
function compruebanatural(contenido,error)
{
	if (contenido.length == 0)
	{
		error.valor=1;
		error.posicion=1; 
		return false;
	}  
	for (var i=0; i<contenido.length;i++)
	{
		if (!numero(contenido.charAt(i)))
		{
			error.valor=12;
			error.posicion=i+1;
			return false;
		} 
	}
	return true;
}

// Comprueba si un signo se encuetra en la posición correcta 
function signocorrecto(contenido,error)
{
	if (contenido.length == 0)
	{
		error.valor=1;
		error.posicion=1; 
		return false;
	}
	else 
		if (!numero(contenido.charAt(0))&&!signo(contenido.charAt(0)))
		{
			error.valor=13;
			error.posicion=1; 
			return false;
		}
		else
			return true;
}

// Comprueba si el contenido es un número es entero
function compruebaentero(contenido,error)
{ 
	if (!signocorrecto(contenido,error))    
		return false;    
	else 
		if (numero(contenido.charAt(0)))
			var aux=compruebanatural(contenido,error);
		else
		{ 
			var aux=compruebanatural(contenido.substring(1,contenido.length),error);
			if (!aux)
				error.posicion++; 
			if (error.valor==1)
			{
				error.valor=14;
				error.posicion=1;
			}
		}
	return aux;   
}

// Comprueba si el contenido es un número es real
function compruebareal(contenido,error)
{ 
	var aux=compruebaentero(contenido,error);
	var posicionpunto=error.posicion-1;
	if (!aux && error.valor==12 && puntodecimal.indexOf(contenido.charAt(posicionpunto))>=0) 
	{
		var aux=compruebanatural(contenido.substring(error.posicion,contenido.length),error); 
		if (!aux && error.valor==1)
		{
			error.valor=15;
		}
		if (!aux)
			error.posicion+=posicionpunto+1;
	}
	return aux;
}

// Comprueba si contenido está vacio
function compruebavacio(contenido,error)
{
	if (contenido.length == 0)
	{
		error.valor=1;
		error.posicion=1; 
		return false;
	}
	for (var i=0; i<contenido.length; i++)
	{
		if (contenido.charAt(i)!=' ' && contenido.charAt(i)!='\t')
		{
			return true;
		} 
	}
	error.valor=1;
	error.posicion=1; 
	return false;

}

function telefono(car)
{
	return (telefonos.indexOf(car)>=0)
}
function compruebalongitudcampo(contenido,error,longitud)
{
	if (contenido.length > longitud)
	{
		error.valor=21;
		error.posicion=longitud;
		return false;
	}
	return true;
}
function compruebaminimalongitudcampo(contenido,error,longitud)
{
	if (contenido.length < longitud)
	{
		error.valor=22;
		error.posicion=longitud;
		errores[22] = "La mínima longitud permitida para este campo es " + error.posicion
		return false;
	}
	return true;
}
function compruebatelefono(contenido,error)
{
	var comprobar;
	if (contenido.length==0)
	{
		error.valor=1;
		error.posicion=1; 
		return false;
	} 

	guiones = "-"
	for (var i=0; i<contenido.length;i++)
	{
		guiones += "-";
		if (!telefono(contenido.charAt(i)))
		{
			error.valor=16;
			error.posicion=i+1;
			return false;
		}
		// Comprueba que no haya 2 o más guiones seguidos
		if (contenido.indexOf(guiones)>=0)
		{
			error.valor=17;
			error.posicion=contenido.indexOf(guiones)+1;
			return false;
		} 
	}
	if (contenido.charAt(0) == "-")
	{
		error.valor=18;
		error.posicion=1;
		return false;
	}
	if (contenido.charAt(contenido.length-1) == "-")
	{
		error.valor=19;
		error.posicion=contenido.length;
		return false;
	}
	
	comprobar = compruebaminimalongitudcampo(contenido,error,7)
	if (!comprobar)
	{
		return false
	}
	
	return true;
}

function mascaranatural(campo,valor,e)
{
	var whichCode = (window.Event) ? e.which : e.keyCode;

	var alphaCheck = "0123456789";
	var caracter=valor.charAt(valor.length-1);
	
	if (alphaCheck.indexOf(caracter) < 0) 
	{
		campo.value = campo.value.substr(0, (valor.length-1));
		return false;

	}
}

function mascaraentero(campo,valor,e)
{
	var whichCode = (window.Event) ? e.which : e.keyCode;
	var alphaCheck = "0123456789+-";
	var caracter=valor.charAt(valor.length-1);
	
	if (alphaCheck.indexOf(caracter) < 0) 
	{
		campo.value = campo.value.substr(0, (valor.length-1));
		return false;

	}
	if (whichCode == 8) //Ignore the Netscape value for backspace. IE has no value
		return false;
	else 
	{
		if ((caracter == "+" || caracter == "-") && valor.length > 1)
		{
			campo.value = campo.value.substr(0, (valor.length-1));
			return false;
		}

	}
}
function mascarareal(campo,valor,e)
{
	var whichCode = (window.Event) ? e.which : e.keyCode;
	var alphaCheck = "0123456789.+-";
	var caracter=valor.charAt(valor.length-1);
	
	if (alphaCheck.indexOf(caracter) < 0) 
	{
		campo.value = campo.value.substr(0, (valor.length-1));
		return false;

	}
	if (whichCode == 8) //Ignore the Netscape value for backspace. IE has no value
		return false;
	else 
	{
		if ((caracter == "+" || caracter == "-") && valor.length > 1)
		{
			campo.value = campo.value.substr(0, (valor.length-1));
			return false;
		}
		verificador = valor.substr(0, (valor.length-1));
		if (caracter == "." && (verificador.indexOf(".") >= 0 || valor == "." || valor == "+." || valor == "-."))
		{
			campo.value = campo.value.substr(0, (valor.length-1));
			return false;
		}
	}
}

function mascaratelefono(campo,valor,e)
{
	var whichCode = (window.Event) ? e.which : e.keyCode;
	var alphaCheck = "0123456789-";
	var caracter=valor.charAt(valor.length-1);
	
	if (alphaCheck.indexOf(caracter) < 0) 
	{
		campo.value = campo.value.substr(0, (valor.length-1));
		return false;

	}
	if (whichCode == 8) //Ignore the Netscape value for backspace. IE has no value
		return false;
	else 
	{
		if (valor.charAt(0) == "-")
		{
			campo.value = campo.value.substr(0, (valor.length-1));
			return false;
		}
		if (caracter == "-" && valor.charAt(valor.length-2) == caracter)
		{
			campo.value = campo.value.substr(0, (valor.length-1));
			return false;
		}
	}
}

function mascarafecha(campo,valor,e)
{
	var whichCode = (window.Event) ? e.which : e.keyCode;
	if (valor.length > 8 && isNav4) 
	{
		if (valor.indexOf("/") >= 1)
			return true;
	}
	
	if (valor.length == 3 && valor.charAt(valor.length-1) == "/")
	{
		return true;
	}
	if (valor.length == 6 && valor.charAt(valor.length-1) == "/")
	{
		return true;
	}

	//var alphaCheck = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ/-";
	var alphaCheck = "0123456789";
	var caracter=valor.charAt(valor.length-1);
	
	if (alphaCheck.indexOf(caracter) < 0) 
	{
		campo.value = campo.value.substr(0, (valor.length-1));
		return false;

	}
	if (whichCode == 8) //Ignore the Netscape value for backspace. IE has no value
		return false;
	else 
	{
		//Create numeric string values for 0123456789/
		//The codes provided include both keyboard and keypad values
		var strCheck = '47,48,49,50,51,52,53,54,55,56,57,58,59,95,96,97,98,99,100,101,102,103,104,105';
		if (strCheck.indexOf(whichCode) != -1) 
		{
			if (valor.length == 2) 
			{
				campo.value += "/";
			}
			if (valor.length == 5) 
			{
				campo.value += "/";
			}
			if (valor.length == 3 && valor.length != "/")
			{
				campo.value = campo.value.substr(0, (valor.length-1)) + "/" + campo.value.substr((valor.length-1), valor.length);
			}
			if (valor.length == 6 && valor.length != "/") 
			{
				campo.value = campo.value.substr(0, (valor.length-1)) + "/" + campo.value.substr((valor.length-1), valor.length);
			}
		}
	}
}
function mascarahoraminuto(campo,valor,e)
{
	var whichCode = (window.Event) ? e.which : e.keyCode;
	if (valor.length > 8 && isNav4) 
	{
		if (valor.indexOf(":") >= 1)
			return true;
	}
	
	if (valor.length == 3 && valor.charAt(valor.length-1) == ":")
	{
		return true;
	}
	if (valor.length > 5)
	{
		campo.value = campo.value.substr(0, 5);
	}
	
	var alphaCheck = "0123456789";
	var caracter=valor.charAt(valor.length-1);
	
	if (alphaCheck.indexOf(caracter) < 0) 
	{
		campo.value = campo.value.substr(0, (valor.length-1));
		return false;

	}
	if (whichCode == 8) //Ignore the Netscape value for backspace. IE has no value
		return false;
	else 
	{
		//Create numeric string values for 0123456789/
		//The codes provided include both keyboard and keypad values
		var strCheck = '47,48,49,50,51,52,53,54,55,56,57,58,59,95,96,97,98,99,100,101,102,103,104,105';
		if (strCheck.indexOf(whichCode) != -1) 
		{
			if (valor.length == 2) 
			{
				campo.value += ":";
			}
			if (valor.length == 3 && valor.length != ":")
			{
				campo.value = campo.value.substr(0, (valor.length-1)) + ":" + campo.value.substr((valor.length-1), valor.length);
			}
		}
	}
}
function mascarahoraminutosegundo(campo,valor,e)
{
	var whichCode = (window.Event) ? e.which : e.keyCode;
	if (valor.length > 8 && isNav4) 
	{
		if (valor.indexOf(":") >= 1)
			return true;
	}
	
	if (valor.length == 3 && valor.charAt(valor.length-1) == ":")
	{
		return true;
	}
	if (valor.length == 6 && valor.charAt(valor.length-1) == ":")
	{
		return true;
	}
	if (valor.length > 8)
	{
		campo.value = campo.value.substr(0, 8);
	}
	
	var alphaCheck = "0123456789";
	var caracter=valor.charAt(valor.length-1);
	
	if (alphaCheck.indexOf(caracter) < 0) 
	{
		campo.value = campo.value.substr(0, (valor.length-1));
		return false;

	}
	if (whichCode == 8) //Ignore the Netscape value for backspace. IE has no value
		return false;
	else 
	{
		//Create numeric string values for 0123456789/
		//The codes provided include both keyboard and keypad values
		var strCheck = '47,48,49,50,51,52,53,54,55,56,57,58,59,95,96,97,98,99,100,101,102,103,104,105';
		if (strCheck.indexOf(whichCode) != -1) 
		{
			if (valor.length == 2) 
			{
				campo.value += ":";
			}
			if (valor.length == 5) 
			{
				campo.value += ":";
			}
			if (valor.length == 3 && valor.length != ":")
			{
				campo.value = campo.value.substr(0, (valor.length-1)) + ":" + campo.value.substr((valor.length-1), valor.length);
			}
			if (valor.length == 6 && valor.length != ":")
			{
				campo.value = campo.value.substr(0, (valor.length-1)) + ":" + campo.value.substr((valor.length-1), valor.length);
			}
		}
	}
}

function convierteFechaIngles (contenido)
{
	var pos1=contenido.indexOf(separadorfecha);
	var mes=contenido.substring(0,pos1);
	var pos2=contenido.indexOf(separadorfecha,pos1+1);
	var dia=contenido.substring(pos1+1,pos2); 
	var anio=contenido.substring(pos2+1,10);
	if (dia < 10)
	{
		if (dia.length == 1)
		{
			dia = "0" + dia; 
		}
	}
	if (mes < 10) 
	{
		if (mes.length == 1)
		{
			mes = "0" + mes; 
		}
	}
	var cont = anio + mes + dia;
	return cont;
}
function convierteFechaCastellano (contenido)
{
	var pos1=contenido.indexOf(separadorfecha);
	var dia=contenido.substring(0,pos1);
	var pos2=contenido.indexOf(separadorfecha,pos1+1);
	var mes=contenido.substring(pos1+1,pos2); 
	var anio=contenido.substring(pos2+1,10);
	if (dia < 10)
	{
		if (dia.length == 1)
		{
			dia = "0" + dia; 
		}
	}
	if (mes < 10) 
	{
		if (mes.length == 1)
		{
			mes = "0" + mes; 
		}
	}
	var cont = anio + mes + dia;
	return cont;
}
function convierteFechaCastellanoAIngles (contenido)
{
	var pos1=contenido.indexOf(separadorfecha);
	var dia=contenido.substring(0,pos1);
	var pos2=contenido.indexOf(separadorfecha,pos1+1);
	var mes=contenido.substring(pos1+1,pos2); 
	var anio=contenido.substring(pos2+1,10);
	if (dia < 10)
	{
		if (dia.length == 1)
		{
			dia = "0" + dia; 
		}
	}
	if (mes < 10) 
	{
		if (mes.length == 1)
		{
			mes = "0" + mes; 
		}
	}
	var cont = mes + "/" + dia + "/" + anio;
	return cont;
}
function convierteHora (contenido)
{
	var pos1=contenido.indexOf(separadorhora);
	var horas=contenido.substring(0,pos1);
	var minutos=contenido.substring(pos1+1,5); 
	if (horas < 10)
	{
		if (horas.length == 1)
		{
			horas = "0" + horas; 
		}
	}
	if (minutos < 10) 
	{
		if (minutos.length == 1)
		{
			minutos = "0" + minutos; 
		}
	}
	var cont = horas + minutos;
	return cont;
}
function comparaDosFechasIngles(fecha1,fecha2)
{
// Devuelve 0 si son las dos iguales, 1 si fecha1 es mayor y 2 si fecha2 es mayor
	var fechaStart = convierteFechaIngles(fecha1);
	var fechaEnd = convierteFechaIngles(fecha2);
	if (fechaStart > fechaEnd)
	{
		return 1;
	}
	if (fechaStart < fechaEnd)
	{
		return 2;
	}
	if (fechaStart == fechaEnd)
	{
		return 0;
	}
}
function comparaDosFechasCastellano(fecha1,fecha2)
{
// Devuelve 0 si son las dos iguales, 1 si fecha1 es mayor y 2 si fecha2 es mayor
	var fechaStart = convierteFechaCastellano(fecha1);
	var fechaEnd = convierteFechaCastellano(fecha2);
	if (fechaStart > fechaEnd)
	{
		return 1;
	}
	if (fechaStart < fechaEnd)
	{
		return 2;
	}
	if (fechaStart == fechaEnd)
	{
		return 0;
	}
}
function comparaDosHoras(hora1,hora2)
{
	var timeStart = convierteHora(hora1);
	var timeEnd = convierteHora(hora2);
	if (timeStart > timeEnd)
	{
		return 1;
	}
	if (timeStart < timeEnd)
	{
		return 2;
	}
	if (timeStart == timeEnd)
	{
		return 0;
	}
}
function abrirVentana(url,pagina,w,h)
{
	win = window.open(url,pagina, "top=10,left=10,width=" + w + ",height=" + h + ",toolbar=no,menubar=no,location=no,directories=no,resizable=yes,scrollbars=yes,status=no")
	//win = window.open(url,pagina, "top=10,left=10,width=640,height=500,toolbar=yes,menubar=yes,location=yes,directories=yes,resizable=yes,scrollbars=yes,status=yes")
}
function maximizarPagina()
{
	if (window.screen)
	{
		aw = screen.availWidth;
		ah = screen.availHeight;
		window.moveTo(0,0);
		window.resizeTo(aw,ah);
	}
}