function messages() {
	
	var form = document.cofinanciamientoInfonavit;
	var ini = parseFloat(form.ini.value);
	if (ini == 1){
		alert("Antes de iniciar la cotizaci\u00F3n, debes leer los siguientes 4 mensajes:");
		alert("Para iniciar el tr\u00E1mite de un cr\u00E9dito en cofinanciamiento debes contar con la precalificaci\u00F3n de Infonavit en Cofinanciamiento con una antiguedad m\u00E1xima de un mes, accesa http://www.infonavit.org.mx o solicitala a tu cliente.");	
		alert("El monto m\u00E1ximo a financiar ser\u00E1 del 85% del valor aval\u00FAo y considerando gastos de originaci\u00F3n hasta el 95% en func\u00EDon de la capacidad de pago y del cr\u00E9dito Infonavit.");	
		alert("Los gastos de originaci\u00F3n que pueden financiarse son: gastos de titulaci\u00F3n y registro de Infonavit, comisi\u00F3n de apertura, impuestos, derechos, honorarios y gestorias. Se financiar\u00E1 el 10% o el real, el menor.");		
		alert("El ejercicio que vas a realizar es una simulaci\u00F3n, para verificar el monto real a financiar debes realizar la evaluaci\u00F3n en el sistema DIANA en COFINANCIAMIENTO y contar con el aval\u00FAo del inmueble.");	
		ini = 0;
		form.ini.value = ini;
	}		
}


function validateForm() {
	var form = document.cofinanciamientoInfonavit;
	eliminaComas(form);

	var dailyIncome = parseFloat(validaCampo(form.dailyIncome.value));

	var incomeNumber = parseInt(validaCampo(form.incomeNumber.value));
	var minValuation = parseFloat(validaCampo(form.minValuation.value));
	var maxValuation;
	var VSMMDF_INGRESO = parseFloat(form.VSMMDF_INGRESO.value);

	var day = parseInt(form.day.selectedIndex) + 1;
	var month = parseInt(form.month.selectedIndex) + 1;
	if(day > 30 && (month == 4 || month == 6 || month == 9 || month == 11)) {
		alert("Proporciona una fecha v\u00E1lida");
		return;
	}

	var bisiesto = 0;
	var year = parseInt(form.year.selectedIndex) + 1998;
	if(year % 4 == 0 && (!(year % 100 == 0) || year % 400 == 0))
		bisiesto = 1;
	if(day >28 && month == 2 && !bisiesto) {
		alert("Proporciona una fecha v\u00E1lida");
		return false;
	}
	if(day >29 && month == 2 && bisiesto) {
		alert("Proporciona una fecha v\u00E1lida");
		return false;
	}
	
	

	var nss = validaCampo(form.nss.value);
	if (nss.length < 11) {
		alert("El N\u00FAmero de Seguro Social debe contener once d\u00EDgitos");
		form.nss.focus();				
		return false;
	}
	if(nss==0) {
		alert("Proporciona el N\u00FAmero del Seguro Social");
		form.nss.focus();				
		return false;
	}	
	
	/*var income = parseFloat(validaCampo(trim(form.income.value)));
	form.income.value = income;
	var minIncome = parseFloat(validaCampo(form.minMonthlyIncome.value));
	minIncome = Math.floor(minIncome * 100) / 100;
	if(income < minIncome) {
		alert("El ingreso mensual bruto debe ser m\u00EDnimo " + VSMMDF_INGRESO + " VSMDF (" + VSMMDF_INGRESO + " * " + dailyIncome + " * 30.4) = $" + currencyFormat(minIncome));
		return;
	}*/

	//VALIDA EL INGRESO
	var income = parseFloat(validaCampo(trim(form.income.value)));
	
	form.income.value = income;
	
	var minIncome = parseFloat(validaCampo(form.minMonthlyIncome.value));
	minIncome = Math.floor(minIncome * 100) / 100;
	
	
var esquema = parseFloat(validaCampo(trim(form.esquema.value)));



	if(income < Math.floor(VSMMDF_INGRESO *dailyIncome * 30.4)) {
		if(esquema==2){
		alert("Salario fuera de rango para cofinanciamiento");

		
		}
		else{
		alert("El ingreso mensual bruto debe ser m\u00EDnimo " + VSMMDF_INGRESO + " VSMDF (" + VSMMDF_INGRESO + " * " + dailyIncome + " * 30.4) = $" + currencyFormat(minIncome));
		}	
		return false;
	}


//if

	//VALIDACION SI EL CREDITO ES CONYUGAL
	if (form.infonavitType.value=="Conyugal"){
	
		var nssConyuge = validaCampo(form.nssConyuge.value);		
		if (nssConyuge.length < 11) {
			alert("El N\u00FAmero de Seguro Social del C\u00F3nyuge debe contener once d\u00EDgitos");
			form.nss.focus();				
			return false;
		}
		
		var incomeConyuge = parseFloat(validaCampo(trim(form.incomeConyuge.value)));	
		form.incomeConyuge.value = incomeConyuge; 	

		
		if (nss == nssConyuge){
			alert("Los NSS deben ser diferentes");
			return false;
		}

		if(nssConyuge==0) {
			alert("Proporciona el N\u00FAmero del Seguro Social del C\u00F3nyuge");
			form.nssConyuge.focus();				
			return false;
		}	

		if(incomeConyuge < minIncome) {
			alert("El ingreso mensual bruto del C\u00F3nyuge debe ser m\u00EDnimo " + VSMMDF_INGRESO + " VSMDF (" + VSMMDF_INGRESO + " * " + dailyIncome + " * 30.4) = $" + currencyFormat(minIncome));
			return;
		}
	
	}

	if(form.state.value==""){
		alert("Proporciona una Ubicaci\u00F3n");
		return false;
	}

//VALIDA EL INGRESO

	var income = parseFloat(validaCampo(trim(form.income.value)));
	form.income.value = income;
	
	var minIncome = parseFloat(validaCampo(form.minMonthlyIncome.value));
	minIncome = Math.floor(minIncome * 100) / 100;
	

	if(income < Math.floor(VSMMDF_INGRESO *dailyIncome * 30.4)) {
		alert("El salario debe ser m\u00EDnimo del Titular "+ VSMMDF_INGRESO + " VSMDF ("+ VSMMDF_INGRESO + " * " + dailyIncome + " * 30.4) = $" + Math.floor(VSMMDF_INGRESO *dailyIncome * 30.4));
		return;
	}

	if(form.destination.value==0) {	
		alert("Proporciona un destino");
		return;
	}


	if(form.valuation.value == "" ) {
		alert("Proporciona el valor del aval\u00FAo aproximado");
		form.valuation.focus();				
		return false;
	}	

	var valuation = parseFloat(validaCampo(trim(form.valuation.value)));
	form.valuation.value = valuation;	
	if(valuation == 0) {
		alert("Proporciona el valor de aval\u00FAo aproximado");
		return;
	}

	minValuation = Math.floor(minValuation * 100) / 100;
	maxValuation = dailyIncome * incomeNumber * 30.4;
	maxValuation = Math.ceil(maxValuation * 100) / 100;
	form.maxValuation.value = maxValuation;
	if(valuation < minValuation) {
		if(!confirm("El valor del aval\u00FAo debe ser m\u00EDnimo $" + currencyFormat(minValuation)+ "\n\n\u00BFDesea continuar\u003f"))
                    return;
	}

	var creditPercentage = parseFloat(validaCampo(form.creditPercentage.value));
	var minCredit = parseFloat(validaCampo(form.minCredit.value));
	var maxCredit = Math.min(maxValuation * creditPercentage, valuation * creditPercentage);

	minCredit = Math.floor(minCredit * 100) / 100;
	maxCredit = Math.ceil(maxCredit * 100) / 100;
	form.maxCredit.value = maxCredit;


	var age = parseInt(validaCampo(form.age.value));
	var term = parseInt(validaCampo(form.term.value));
	if(term==0) {	
		alert("Proporciona un plazo");
		return;
	}
	term = term / 12;
	if(age + term > 75) {
		alert("La suma de la edad del cliente mas el plazo del cr\u00E9dito, debe de ser igual o menor a 75 a\u00F1os");
		var ageItem = 0;
		while(parseInt(form.age[ageItem].value) + term <= 75)
			ageItem++;
		form.age.selectedIndex = ageItem - 1;
		return;
	}

	form.serviceName.value = "cofinanciamientoInfonavitDetailAction";
//	window.resizeTo(820,595);
	form.submit();

}


function eliminaComas(form) {
	form.income.value = form.income.value.replace(",", "");
	form.valuation.value = form.valuation.value.replace(",", "");

}


function currencyFormat(amount) {
	amount = amount.toString();
	amount = amount.replace(/\,/g,"");
	
	if(isNaN(amount))
		return "";
	
	while(amount.charAt(0) == "0")
		amount = amount.substring(1);
	
	var decimalLength;
	if(amount.indexOf(".") > -1) {
		decimalLength = amount.indexOf(".");
		var decimals = amount.length - decimalLength - 1;
		if(decimals < 2) {
			while(decimals < 2) {
				amount = amount + "0";
				decimals++;
			}
		} else {
			amount = amount.substring(0, amount.length - (decimals - 2));
		}
	} else {
		decimalLength = amount.length;
		amount = amount + ".00";
	}
	
	var numberOfCommas = Math.floor(decimalLength / 3);
	if(decimalLength % 3 == 0)  numberOfCommas --;
	
	while(numberOfCommas > 0) {
		amount = amount.substring(0, decimalLength - 3) + "," + amount.substring(decimalLength - 3);
		decimalLength -= 3;
		numberOfCommas --;
	}
	
	return amount;
}


function validaCampo(valor) {
	return (valor == null || valor == "" || isNaN(valor)) ? 0 : valor;
}


function validateValue(field){
	field.value = validField(trim(field.value));
}

function validField(valor) {
	if (valor == null || valor == "" || valor <=0 || isNaN(valor)){
		valor = 0;
	}else{
		valor = currencyFormat(valor);
	}
	return valor;
}

function trim(str) {
	str = str.replace(",","");
	str = str.replace(",","");		
	str = str.replace("$","");		
return str
}
	
function validateAdvancePay(field){
	field.value = validField(trim(field.value));
}

//RECALCULA LA TABLA DE AMORTIZACION DEPENDIENDO DE LOS PAGOS ADELANTADOS
function recalculatePayment(){
	var rows = parseInt(document.recalculate.rows.value);
	for (var i=1;i<=rows;i++){
		//if ((i <= 12) || (i % 12 == 0)){
			advancePayment = document.getElementById("advancePayment_" + i).value;
			advancePayment = trim(advancePayment);
			document.getElementById("advancePayment_" + i).value = advancePayment;
	//	}
	}
	
	document.recalculate.submit();
}

function loadHome() {

	var income = parseFloat(validaCampo(trim(document.cofinanciamientoInfonavit.income.value)));
	document.cofinanciamientoInfonavit.income.value = income;
	var incomeConyuge = parseFloat(validaCampo(trim(document.cofinanciamientoInfonavit.incomeConyuge.value)));
	document.cofinanciamientoInfonavit.incomeConyuge.value = incomeConyuge;
	var valuation = parseFloat(validaCampo(trim(document.cofinanciamientoInfonavit.valuation.value)));
	document.cofinanciamientoInfonavit.valuation.value = valuation;

	document.cofinanciamientoInfonavit.serviceName.value = "cofinanciamientoInfonavitHomeAction";
	document.cofinanciamientoInfonavit.submit();

}

function loadInfonavitType() {

	document.cofinanciamientoInfonavit.serviceName.value = "cofinanciamientoInfonavitHomeAction";
	document.cofinanciamientoInfonavit.submit();

}
