/* JavaScript Document */
/* IMPORTANTE: este archivo js debe incluirse en el documento que crear objetos de este tipo */
/* Los objetos se deben crear despues de que el documento termine de cargarse */
function setValidation(obj, value){
	obj.type = "noValidar"
}

function createTitle(id){
	var idTitle = id + "_:";
	var title = document.getElementById(idTitle).innerHTML;
	title = title.replace("*","");
	title = title.replace(":","");
	title = title.substring(0, title.length);
	return title;
}

function validObject(obj){
	if(type != "noValidar"){
		var id = obj.id;
		var type = obj.type;
		var typeValidation = obj.typeValidation;
		obj.title = createTitle(id);
		var title = obj.title;
		var value = getValue(id);	
	}
	switch(type){
		case "rut":
		if(nullText(id, title, value)){
			return false;
		}else{
			return validRut(id, title, value);
		}
		break;
		
		case "email":
		if(nullText(id, title, value)){
			return false;
		}else{
			return validEmail(id, title, value);
		}
		break;
		
		case "fecha":
		if(nullText(id, title, value)){
			return false;
		}else{
			return validDate(id, title, value, typeValidation);
		}
		break;
		
		case "hora":
		if(nullText(id, title, value)){
			return false;
		}else{
			return validTime(id, title, value);
		}
		break;
		
		case "telefono":
		if(nullText(id, title, value)){
			return false;
		}else{
			return validPhone(id, title, value);
		}
		break;
		
		case "texto":
		if(nullText(id, title, value)){
			return false;
		}else{
			return validText(id, title, value);
		}
		break;
		
		case "numerico":
		if(nullText(id, title, value)){
			return false;
		}else{
			return validNumber(id, title, value);
		}
		break;
		
		case "alfanumerico":
		if(nullText(id, title, value)){
			return false;
		}else{
			return true;
		}
		break;

		case "radio":
		if(radioChecked(id, title, value)){
			return false;
		}else{
			return true;
		}
		break;

		case "noValidar":
		return true;
		break;
	}		
}

function validRut(id, title, value){
	var rut = value;
	var patern = /^([0-9]{1,2}.?[0-9]{3}.?[0-9]{3}\-?)([0-9,k,K]{1})$/;
	var flag = value.search(patern);
	if(flag != 0){
		errorMessage(id, title, value);
		return false;
	}else{
		while(rut.indexOf(".", 0) != -1){
			rut = rut.replace(".", "");
		}
		while(rut.indexOf("-", 0) != -1){
			rut = rut.replace("-", "");
		}
		var rutConFormato = formatRut(rut);
		var dvRut = rut.substring(rut.length-1, rut.length);
		if(dvRut == "k" || dvRut == "K"){
			dvRut = 1;
		}
		rut = rut.substring(0, rut.length-1);
		var multiplicador = 2;
		var tamano = rut.length;
		var suma = 0;
		var resto = 0;
		while(tamano > 0){
			if(multiplicador == 8){
				multiplicador = 2;
			}
			suma = suma + (multiplicador * rut.substring(tamano - 1, tamano));
			rut = rut.substring(0, tamano -1);
			tamano--;
			multiplicador++;
		}
		resto = suma % 11;
		dv = 11 - resto;
		if(dv == 11){
			dv = 0;
		}
		if(dv == 10){
			dv = 1;
		}
		if(dv == dvRut){
			document.getElementById(id).value = rutConFormato;
			return true;
		}else{
			document.getElementById(id).value = rutConFormato;
			errorMessage(id, title, value);
			return false;
		}
	}
}

function formatRut(rut){
	var formatRut = "";
	var tamano = rut.length;
	formatRut = "-" + rut.substring(tamano - 1, tamano);
	if(tamano > 4){
		formatRut = "." + rut.substring(tamano - 4, tamano - 1) + formatRut;
	}
	if(tamano > 7){
		formatRut = "." + rut.substring(tamano - 7, tamano - 4) + formatRut;
		formatRut = rut.substring(0, tamano - 7) + formatRut;
	}
	return formatRut;
}

function validEmail(id, title, value){
	var patern = /^[A-Za-z0-9_-]+([.]{1}[A-Za-z0-9_-]+)*@[A-Za-z0-9_-]+([.]{1}[A-Za-z0-9_-]+)*[.]{1}[A-Za-z]{2,3}$/;
	var flag = value.search(patern);
	if(flag != 0){
		errorMessage(id, title, value);
		return false;
	}else{
		return true;
	}
}

function validDate(id, title, value, typeValidation){
	var patern = /^([0-9]{1,2}(\/|-)?[0-9]{1,2}(\/|-)?[0-9]{4})$/;
	var flag = value.search(patern);
	var response;
	var typeMessage = 0;
	if(flag != 0){
		response = false;
	}else{
		document.getElementById(id).value = formatearFecha(value);
		if(!esDiaValido(value)){
			response = false;
		}else{
			switch(typeValidation){
				case "anterior":
				response = esFechaAnterior(value);
				typeMessage = 1;
				break;
				
				case "posterior":
				response = esFechaPosterior(value);
				typeMessage = 2;
				break;
				
				default:
				return false;
				break;
			}
		}
	}
	if(!response){
		switch(typeMessage){			
			case 1:
			errorMessageDatePrev(id, title, value);
			break;
			
			case 2:
			errorMessageDatePost(id, title, value);
			break;
			
			default:
			errorMessage(id, title, value);
			break;
		}
		return false;
	}else{
		return true;
	}
}

function validTime(id, title, value){
	var patern = /^([0-9]{1,2}:?[0-9]{2})$/;
	var flag = value.search(patern);
	var time = "";
	if(flag != 0){
		errorMessage(id, title, value);
		return false;
	}else{
		var tamano = value.length;
		switch(tamano){
			case 3:
			var minutos = value.substring(1,3);
			var horas = value.substring(0,1);
			time = "0" + horas + ":" + minutos;
			break;
			
			case 4:
			if(value.substring(1,2) == ":"){
				var minutos = value.substring(2,4);
				var horas = value.substring(0,1);
				time = "0" + value;
			}else{
				var minutos = value.substring(2,4);
				var horas = value.substring(0,2);
				time = horas + ":" + minutos;
			}
			break;
			
			case 5:
			var minutos = value.substring(3,5);
			var horas = value.substring(0,2);
			time = value;
			break;
		}
		document.getElementById(id).value = time;
		if(parseInt(horas) >= 0 && parseInt(horas) <= 23 && parseInt(minutos) >= 0 && parseInt(minutos) <= 59){
			return true;
		}else{
			errorMessage(id, title, value);
			return false;
		}
	}
}

function validPhone(id, title, value){
	var patern = /^([0-9]{1,2}-{0,1}[0-9]{6,7}|[0-9]{6,7})$/; 
	var flag = value.search(patern);
	if(flag != 0){
		errorMessage(id, title, value);
		return false;
	}else{
		return true;
	}
}

function validText(id, title, value){
	var patern = /^[a-zA-Z]$/;
	var flag = value.search(patern);
	if(flag != 0){
		errorMessage(id, title, value);
		return false;
	}else{
		return true;
	}
}

function validNumber(id, title, value){
	var patern = /^[0-9]*$/;
	var flag = value.search(patern);
	if(flag != 0){
		errorMessage(id, title, value);
		return false;
	}else{
		return true;
	}
}

function nullMessage(id, title){
	alert("Debe ingresar \""+title+"\"");
	document.getElementById(id).focus();
}

function errorMessage(id, title, value){
	alert(title+" \""+value+"\" no es válido.");
	alert("Ingrese nuevamente \""+title+"\"");
	document.getElementById(id).focus();
	document.getElementById(id).select();
}

function errorMessageDatePrev(id, title, value){
	alert("La "+title+" ingresada no es válida. Debe ingresar una fecha anterior a la actual.");
	document.getElementById(id).focus();
	document.getElementById(id).select();
}

function errorMessageDatePost(id, title, value){
	alert("La "+title+" ingresada no es válida. Debe ingresar una fecha posterior a la actual.");
	document.getElementById(id).focus();
	document.getElementById(id).select();
}

function nullText(id, title, value){//campo en blanco
	if(value == ""){
		nullMessage(id, title);
		return true;
	}else{
		return false;
	}
}

function radioChecked(id, title, value){//objeto no chequeado
	var size = eval("document.forms[0]."+id+".length");
	var flag = false;
	for(i=0;i<size;i++){
		var aux = eval("document.forms[0]."+id+"["+i+"].checked");
		if(aux){
			flag = true;
			break;
		}
	}
	if(!flag){
		nullMessage(id, title);
		return true;
	}else{
		return false;
	}
}

function getValue(id){
	return document.getElementById(id).value;
}

function objeto(id, type, typeValidation, title){// tipos: rut, email, fecha, hora, telefono, texto, numerico, alfanumerico, radio
//	alert("id:" + id + " type:" + type + " typeValidation:" + typeValidation + " title:" + title);
	this.id = id;
	this.type = type;
	this.typeValidation = typeValidation;
//	this.title = createTitle(this.id);
}

function bisiesto(anio){
	var resto = anio % 4;
	if(resto == 0){
		return true;
	}else{
		return false;
	}
}

function maxDiasMes(fecha){
	var mes = getMes(fecha);
	var anio = getAnio(fecha);
	var maxDias = 0;
	switch(mes){
		case '01'://Enero
		maxDias = 31;
		break;
				
		case '02'://Febrero
		if(bisiesto(anio)){
			maxDias = 29;
		}else{
			maxDias = 28;
		}
		break;
				
		case '03'://Marzo
		maxDias = 31;
		break;
				
		case '04'://Abril
		maxDias = 30;
		break;
				
		case '05'://Mayo
		maxDias = 31;
		break;
				
		case '06'://Junio
		maxDias = 30;
		break;
				
		case '07'://Julio
		maxDias = 31;
		break;
				
		case '08'://Agosto
		maxDias = 31;
		break;
				
		case '09'://Septiembre
		maxDias = 30;
		break;
				
		case '10'://Octubre
		maxDias = 31;
		break;
				
		case '11'://Noviembre
		maxDias = 30;
		break;
				
		case '12'://Diciembre
		maxDias = 31;
		break;
	}
	return maxDias;
}
		
function esFechaPosterior(fecha){
	var fechaActual = getFechaActual();
	fecha = convertirFechaEnDigitos(fecha);
	fechaActual = convertirFechaEnDigitos(fechaActual);
	if(fecha > fechaActual){
		return true;
	}else{
		return false;
	}
}
		
function esFechaAnterior(fecha){
	var fechaActual = getFechaActual();
	fecha = convertirFechaEnDigitos(fecha);
	fechaActual = convertirFechaEnDigitos(fechaActual);
	if(fecha < fechaActual){
		return true;
	}else{
		return false;
	}		
}
		
function esFechaActual(fecha){
	var fechaActual = getFechaActual();
	if(fecha == fechaActual){
		return true;
	}else{
		return false;
	}
}
		
function getFechaActual(){
	var fecha = new Date();
	var dia = fecha.getDate();
	if(dia <= 9){
		dia = "0"+dia;
	}
	var mes = fecha.getMonth() + 1;
	if(mes < 10){
		mes = "0" + mes;
	}
	var anio = fecha.getFullYear();
	var fechaActual = dia + "/" + mes + "/" + anio;
	return fechaActual;
}

function convertirFechaEnDigitos(fecha){
	fecha = limpiarFecha(fecha);
	var anio = getAnio(fecha);
	var mes = getMes(fecha);
	var dia = getDia(fecha);
	fecha = anio+mes+dia;
	return eval(fecha);	
}

function getDia(fecha){
	return fecha.substring(0,2);
}

function getMes(fecha){
	return fecha.substring(2,4);
}

function getAnio(fecha){
	return fecha.substring(4,8);
}

function formatearFecha(fecha){
	fecha = limpiarFecha(fecha);
	fecha = getDia(fecha) + "/" + getMes(fecha) + "/" + getAnio(fecha);
	return fecha;
}

function limpiarFecha(fecha){
	var dia = "";
	var mes = "";
	var anio = "";
	while(fecha.indexOf("-", 0) != -1){
		fecha = fecha.replace("-", "");
	}
	while(fecha.indexOf("/", 0) != -1){
		fecha = fecha.replace("/", "");
	}
	var tamano = fecha.length;
	anio = fecha.substring(tamano - 4, tamano);
	switch(tamano){
		case 6:
		dia = "0" + fecha.substring(0,1);
		mes = "0" + fecha.substring(1,2);
		anio = fecha.substring(2,6);
		break;
		
		case 7:
		anio = fecha.substring(3,7);
		var aux = fecha.substring(1,3);
		if(aux >= 1 && aux <= 12){
			if(aux > 9){
				dia = "0" + fecha.substring(0,1);
				mes = aux;				
			}else{
				dia = fecha.substring(0,1) + "0";
				mes = aux;
			}
		}else{
			dia = fecha.substring(0,2);
			mes = "0" + fecha.substring(2,3);
		}
		break;
		
		case 8:
		dia = fecha.substring(0,2);
		mes = fecha.substring(2,4);
		anio = fecha.substring(4,8);
		break;
	}
	fecha = dia + mes + anio;
	return fecha;
}

function esDiaValido(fecha){
	var fecha = limpiarFecha(fecha);
	var dia = eval(getDia(fecha));
	var maxDias = eval(maxDiasMes(fecha));
	if(dia <= maxDias && dia > 0){
		return true;
	}else{
		return false;
	}
}