// funciones para validar formato de fechas y horas

function validarHoras(sHora) {
	/*
		para horas con formato  hh:mm:ss
								10:30:05
		valores de retorno:
							1 -> hora valida
							2 -> sHora = ""
							3 -> formato incorrecto
							4 -> la hora es mayor de 23
							5 -> los minutos son mayores de 59
							6 -> los segundos son mayores de 59
	*/
	if (sHora == "") {
		return(2)
	} else {
		if (sHora.length != 8) {
			return(3)
		} else {
			if (sHora.indexOf(':') != 2) {
				return(3)
			} else {
				hora = sHora.substr(0,2)							
				minuto = sHora.substr(3,2)
				segundo = sHora.substr(6,2)
				// valido la hora
				if (hora > 23) {
					return(4)
				}
				for (i = 0; i < hora.length; i++) {					
					if ((hora.charCodeAt(i) < 48) || (hora.charCodeAt(i) > 57)) {
						return(3)
					}
				}
							
				// valido los minutos
				if (minuto > 59) {
					return(5)
				}							
				for (i = 0; i < minuto.length; i++) {					
					if ((minuto.charCodeAt(i) < 48) || (minuto.charCodeAt(i) > 57)) {
						return(3)
					}
				}
							
				// valido los segundos
				if (segundo > 59) {
					return(6)
				}
				for (i = 0; i < segundo.length; i++) {					
					if ((segundo.charCodeAt(i) < 48) || (segundo.charCodeAt(i) > 57)) {
						return(6)
					}
				}
			}
		}
	}
	return(1)
}

		
//function validarFechas(sFecha) {
//	//alert (sFecha)
//	/*
//	para fechas con formato dd/mm/aaaa
//							01/01/2002
//	datos de retorno:
//		1 -> correcto
//		2 -> la fecha está vacia
//		3 -> El formato de fecha es incorrecto
//		4 -> el día no puede ser mayor de 31
//		5 -> el mes no puede ser mayor de 12
//		6 -> formato incorrecto
//	*/
//				
//	//alert (sFecha)
//				
//	if(sFecha == ""){
//		    return(2)
//	} else {
//		if (sFecha.length != 10) {
//			//alert("error 1")
//			return(3)
//		}else {
//			if (sFecha.indexOf('/') != 2) {
//				//alert("error 2")
//				return(3)
//			}else {					
//				aux = sFecha.substr(3,7)
//				if (aux.indexOf('/') != 2) {
//					//alert("error 3")
//					return(3)
//				}else {
//					dia = sFecha.substr(0,2)
//					mes = sFecha.substr(3,2)
//					ano = sFecha.substr(6,4)						
//					if (dia > 31) {
//						return(4)
//					}else {
//						if (mes > 12) {
//							return(5)
//						}
//					}
//				}
//			}
//		}
//	}
//	return(1)
//}


function validarFechas(sFecha, tipo) {
	
	//alert (sFecha)
	/*
	tipo -> 1
	para fechas con formato dd/mm/aaaa
							01/01/2002
	
	tipo -> 2
	para fechas con formato mm/aa
							01/02
							
	tipo -> 3
	para fechas con formato mm/aaaa
	
	tipo -> 4
	para fechas con formato mm/aa
	
	datos de retorno:
		1 -> correcto
		2 -> la fecha está vacia
		3 -> El formato de fecha es incorrecto
		4 -> el día no puede ser mayor de 31
		5 -> el mes no puede ser mayor de 12
		6 -> formato incorrecto
	*/
				
	//alert (sFecha)
	
	if (tipo == "1") {
		if(sFecha == ""){
			    return(2)
		} else {
			if (sFecha.length != 10) {
				//alert("error 1")
				return(3)
			}else {
				if (sFecha.indexOf('/') != 2) {
					//alert("error 2")
					return(3)
				}else {					
					aux = sFecha.substr(3,7)
					if (aux.indexOf('/') != 2) {
						//alert("error 3")
						return(3)
					}else {
						dia = sFecha.substr(0,2)
						mes = sFecha.substr(3,2)
						ano = sFecha.substr(6,4)						
						if (dia > 31) {
							return(4)
						}else {
							if (mes > 12) {
								return(5)
							}
						}
					}
				}
			}
		}
		return(1)
	}
	
	if (tipo == "2") {
		if (sFecha == "") {
			return(2)
		} else {
			if (sFecha.length != 5) {
				return(3)
			} else {
				if (sFecha.indexOf('/') != 2) {
					return (3)
				} else {
					var mes = sFecha.substr(0,2)
					//var año = sFecha.substr(3,2)
					if (mes > 12) {
						return(5)
					}
				}
			}
		}
		return(1)
	}
	
	if (tipo == "3") {
		if (sFecha == "") {
			return(2)
		} else {
			if (sFecha.length != 7) {
				return (3)
			} else {
				var mes = sFecha.substr(0,2)
				if (mes > 12) {
					return(5)
				}
			}
		}
		return(1)
	} else {
		if (tipo == "4") {






			if (sFecha == "") {
				return(2)
			} else {
				if (sFecha.length != 5) {
					return (3)
			} else {
				var mes = sFecha.substr(0,2)
				var ano = sFecha.substr(3,2)
				if (mes > 12) {
					return(5)
				}
				if (sFecha.substr(2,1) != "/"){
					return (6)
				}
			}
		}
		return(1)
		
	
/*		
		
			if (sFecha == "") {
				return(2)
			} else {
				if (sFecha.length != 5) {
					return (3)
				} else {
					var mes = sFecha.substr(0,2)
					if (mes > 12) {
						return(5)
					}
				}
			}
			return(1)
			
*/	
			
		}
	}
}




function validarCP(codpostal) {
	/*
		Esta función valida codigos postales compureba que tenga 5
		dígitos y que sea numérico
		
		datos de retorno:
			1 -> Codigo Correcto
			2 -> El Código está vacio
			3 -> El Código no es numérico
			4 -> No tiene 5 dígitos
	*/

	if(codpostal == ""){
		return 2
	}/*
	else{
		if (codpostal.length != 5){
			return 3
		}												
		else {
			if (!parseInt(codpostal)){
				return 4
			}
		}		
	}*/
	
	return(1)
}



function validarMail(correo) {
	/*
	esta funcion valida e-mails, comprueba si tiene arraba, punto ademas de si existe algo entre
	dichos caracteres, es decir obliga a un String a tener un formato a@a.a donde a es cualquier
	cadena de caracteres
	
	datos que retorna:
		1 -> e-mail con formato correcto
		2 -> El mail está vacio
		3 -> falta el "." o la "@", por lo tanto el formato es incorrecto
		4 -> la posición del punto es mayor que la de la arroba o no hay nada entre ellos
		5 -> la dirección termina en punto
	*/

	var arroba
	var punto

	if (correo == ""){
		return "2"
	}
	else{
		OK = false;
		arroba = 0;
		punto = 0;
		for (var i = 0; i<correo.length; i++){
			 var aux = correo.substring(i, i + 1);  		
			 if (aux == "@") {
				arroba = i;
			}
		}
		for (var i = arroba; i<correo.length; i++){
			 var aux = correo.substring(i, i + 1);  		
			 if (aux == ".") {
				punto = i;
			}
		}
		if (punto == 0 || arroba == 0){
			return "3"
		}  		
		if (punto - arroba <= 1){
			return "4"
		}		
		if ((punto + 1) == correo.length){
			return "5"
		}				
	}
	return "1"
}

function contarLetra(texto, letra) {
	var aux
	var letraAux
	aux = 0
	for (i = 0; i < texto.length;i++) {
		//letraAux = mid(texto, i, 1)
		letraAux = texto.charAt(i)
		//alert(letraAux + " - " + letra)
		if (letraAux == letra) {
			aux = aux + 1
		}
	}
	//alert(aux)
	return aux
}

function AbrirVentana(url, nombre, ancho, alto) {
	MiVentana=open(url, nombre, "toolbar=no,menubar=no,width=" + ancho + ",height=" + alto);
}
function AbrirVentanaScrolls(url, nombre, ancho, alto) {
	MiVentana=open(url, nombre, "toolbar=0,location=0,directories=0,status=1,menubar=1,scrollbars=1,resizable=1,width=" + ancho + ",height=" + alto);
}

function GWValidarCaracterPassword(){
	if((window.event.keyCode < 48 || window.event.keyCode > 57) && (window.event.keyCode < 61 || window.event.keyCode > 90) && (window.event.keyCode < 97 || window.event.keyCode > 122)){
		window.event.keyCode = 0;
	}
}
