/*
Funci\u00F3n utilizada por el archivo "creditoHipotecario.jsp".
Valida la informaci\u00F3n proporcionada por el cliente en la forma "creditoHipotecario".
*/
function loadPage(){
	document.creditoHipotecario.propertyPrice.value = 0.00;
	document.creditoHipotecario.additionalPropertyPrice.value = 0.00;	
	document.creditoHipotecario.credit.value = 0.00;
	document.creditoHipotecario.monthlyPayment.value = 0.00;
	document.creditoHipotecario.monthlyIncome.value = 0.00;
	document.creditoHipotecario.serviceName.value = "creditoHipotecarioHomeAction";
	document.creditoHipotecario.submit();
}




function validateForm() {
        var     max;
        max = document.creditoHipotecario.term[document.creditoHipotecario.term.options.length-1].value;
        document.creditoHipotecario.maxTerm.value = max;
	var	date, month, year, currentYear, currentYearN, baseYearN, bisiesto,
		propertyPrice, additionalPropertyPrice, minValuation, propertyIndex, simulation, monthlyPayment,
		monthlyIncome, creditPercentage, minCredit,  valuationByCredit, maxValuationSE;

	day = document.creditoHipotecario.day.selectedIndex + 1;
	month = document.creditoHipotecario.month.selectedIndex + 1;

	if ( day > 30 && ( month == 4 || month == 6 || month == 9 || month == 11 ) ) {
		alert( "Por favor proporciona una fecha v\u00E1lida" );
		return false;
	}

	currentYear = document.creditoHipotecario.currentYear.value;
	currentYearN = new Number( currentYear );
	baseYearN = currentYearN - 4;
	year = parseInt(document.creditoHipotecario.year.selectedIndex) + baseYearN;
	bisiesto = 0;

	if ( year % 4 == 0 && ( !( year % 100 == 0 ) || year % 400 == 0 ) )
		bisiesto = 1;

	if ( day > 28 && month == 2 && !bisiesto ) {
		alert( "Por favor proporciona una fecha v\u00E1lida" );
		return false;
	}

	if ( day > 29 && month == 2 ) {
		alert( "Por favor proporciona una fecha v\u00E1lida" );
		return false;
	}

	simulationIndex = parseInt(document.creditoHipotecario.simulation.selectedIndex);
	if 	(simulationIndex <= 0 ) {
		alert( "Por favor selecciona un Tipo de Simulaci\u00F3n" );
		return false;
	}

	destinationIndex = parseInt(document.creditoHipotecario.destination.selectedIndex);
	if 	(destinationIndex <= 0 ) {
		alert( "Por favor selecciona un Destino" );	
		return false;
	}

	propertyPrice = parseFloat(validaCampo(trim(document.creditoHipotecario.propertyPrice.value)));
	document.creditoHipotecario.propertyPrice.value	 = propertyPrice;
	additionalPropertyPrice = parseFloat(validaCampo(trim(document.creditoHipotecario.additionalPropertyPrice.value)));
	document.creditoHipotecario.additionalPropertyPrice.value = additionalPropertyPrice;
	credit = parseFloat(validaCampo(trim(document.creditoHipotecario.credit.value)));	
	document.creditoHipotecario.credit.value = credit;
	monthlyPayment = parseFloat(validaCampo(trim(document.creditoHipotecario.monthlyPayment.value)));
	document.creditoHipotecario.monthlyPayment.value = monthlyPayment;
	monthlyIncome = parseFloat(validaCampo(trim(document.creditoHipotecario.monthlyIncome.value)));
	document.creditoHipotecario.monthlyIncome.value = monthlyIncome;
	
	minValuation = parseFloat(validaCampo(document.creditoHipotecario.minValuation.value));
//NUEVO AVALUO MAXIMO ACH SOCIAL/ECONOMICA
	maxValuationSE = parseFloat(validaCampo(document.creditoHipotecario.maxValuationSE.value));
	creditPercentage = parseFloat(validaCampo(document.creditoHipotecario.creditPercent.value));
	minCredit = parseFloat(validaCampo(document.creditoHipotecario.minCredit.value));
	termIndex = parseInt(document.creditoHipotecario.term.selectedIndex);

	simulation = parseInt(document.creditoHipotecario.simulation.value);

	if (propertyPrice <= 0 && simulation == 1) {
		alert( "Por favor proporciona el Valor de aval\u00FAo aproximado");
		document.creditoHipotecario.propertyPrice.value = 0;
		return false;
	}

	if (additionalPropertyPrice <= 0 && simulation == 1 && document.creditoHipotecario.destination.value =="ADQUISICION CASA HABITACION CON GARANTIA ADICIONAL" && document.creditoHipotecario.destination.value =="ADQUISICION CASA HABITACION MAS POR MENOS CON GARANTIA ADICIONAL" ) {
		alert( "Por favor proporciona el Valor de aval\u00FAo aproximado para la garant\u00EDa adicional");
		document.creditoHipotecario.additionalPropertyPrice.value = 0;
		return false;
	}

	//Valor al menos del 40% de la garant\u00EDa adicional
	if ((simulation == 1 || simulation == 2) && (document.creditoHipotecario.destination.value =="ADQUISICION CASA HABITACION CON GARANTIA ADICIONAL" || document.creditoHipotecario.destination.value =="ADQUISICION CASA HABITACION MAS POR MENOS CON GARANTIA ADICIONAL")){
		if ( additionalPropertyPrice < (propertyPrice * .40) ) {
			alert( "El valor m\u00EDnimo del aval\u00FAo de la garant\u00EDa adicional es de $"  + currencyFormat(propertyPrice * .40));
			document.creditoHipotecario.additionalPropertyPrice.value = propertyPrice * .40;
			return false;
		}
	}


	if (credit <= 0 && simulation == 2) {
		alert( "Por favor proporciona el Cr\u00E9dito solicitado");
		document.creditoHipotecario.credit.value = 0;
		return false;
	}

	if 	(termIndex <= 0) {
		alert( "Por favor selecciona el Plazo" );	
		return false;
	}
	
	if (monthlyPayment <= 0 && simulation == 3) {
		alert( "Por favor proporciona el valor de Pago Mensual");
		document.creditoHipotecario.monthlyPayment.value = 0;
		return false;
	}
	
	if ( monthlyIncome <= 0 && simulation == 4 ) {
		alert( "Por favor proporciona el valor de Ingreso Mensual");
		document.creditoHipotecario.monthlyIncome.value = 0;
		return false;
	}


        
	if ( propertyPrice < minValuation ) {
                if (simulation == 3){
			if (document.creditoHipotecario.destination.value == "ADQUISICION DE CASA HABITACION" || document.creditoHipotecario.destination.value =="ADQUISICION CASA HABITACION CON GARANTIA ADICIONAL" || document.creditoHipotecario.destination.value =="ADQUISICION CASA HABITACION SOCIAL/ECONOMICA" || document.creditoHipotecario.destination.value == "ADQUISICION CASA HABITACION CON SEGURO HIPOTECARIO" || document.creditoHipotecario.destination.value == "ADQUISICION CASA HABITACION MAS POR MENOS CON SEGURO HIPOTECARIO " 
                        || document.creditoHipotecario.destination.value == "ADQUISICION CASA HABITACION MAS POR MENOS" || document.creditoHipotecario.destination.value == "ADQUISICION CASA HABITACION MAS POR MENOS CON GARANTIA ADICIONAL")
				alert("Para estas condiciones requieres un Pago mayor");
			else
				alert("Para ese Pago mensual, por favor proporciona un plazo mayor");

		}else if (simulation == 4){
			if (document.creditoHipotecario.destination.value == "ADQUISICION DE CASA HABITACION" || document.creditoHipotecario.destination.value =="ADQUISICION CASA HABITACION CON GARANTIA ADICIONAL" || document.creditoHipotecario.destination.value =="ADQUISICION CASA HABITACION SOCIAL/ECONOMICA" || document.creditoHipotecario.destination.value == "ADQUISICION CASA HABITACION CON SEGURO HIPOTECARIO" || document.creditoHipotecario.destination.value == "ADQUISICION CASA HABITACION MAS POR MENOS CON SEGURO HIPOTECARIO " 
                        || document.creditoHipotecario.destination.value == "ADQUISICION CASA HABITACION MAS POR MENOS" || document.creditoHipotecario.destination.value == "ADQUISICION CASA HABITACION MAS POR MENOS CON GARANTIA ADICIONAL")
				alert("Para estas condiciones requieres un Ingreso mayor");
			else
				alert("Para ese Ingreso mensual, por favor proporciona un plazo mayor");
		}else {
			alert( "El valor m\u00EDnimo del aval\u00FAo es de $ "  + currencyFormat(minValuation));
			document.creditoHipotecario.propertyPrice.value = minValuation;
		}
		return false;
	}
	if (credit < minCredit) {
		if (simulation == 3){
			if (document.creditoHipotecario.destination.value == "ADQUISICION DE CASA HABITACION" || document.creditoHipotecario.destination.value =="ADQUISICION CASA HABITACION CON GARANTIA ADICIONAL" || document.creditoHipotecario.destination.value =="ADQUISICION CASA HABITACION SOCIAL/ECONOMICA" || document.creditoHipotecario.destination.value == "ADQUISICION CASA HABITACION CON SEGURO HIPOTECARIO" || document.creditoHipotecario.destination.value == "ADQUISICION CASA HABITACION MAS POR MENOS CON SEGURO HIPOTECARIO " 
                        || document.creditoHipotecario.destination.value == "ADQUISICION CASA HABITACION MAS POR MENOS" || document.creditoHipotecario.destination.value == "ADQUISICION CASA HABITACION MAS POR MENOS CON GARANTIA ADICIONAL")
				alert("Para estas condiciones requieres un Pago mayor");
			else
				alert("Para ese Pago mensual, por favor proporciona un plazo mayor");
		}else if (simulation == 4){
			if (document.creditoHipotecario.destination.value == "ADQUISICION DE CASA HABITACION" || document.creditoHipotecario.destination.value =="ADQUISICION CASA HABITACION CON GARANTIA ADICIONAL" || document.creditoHipotecario.destination.value =="ADQUISICION CASA HABITACION SOCIAL/ECONOMICA" || document.creditoHipotecario.destination.value == "ADQUISICION CASA HABITACION CON SEGURO HIPOTECARIO" || document.creditoHipotecario.destination.value == "ADQUISICION CASA HABITACION MAS POR MENOS CON SEGURO HIPOTECARIO " 
                        || document.creditoHipotecario.destination.value == "ADQUISICION CASA HABITACION MAS POR MENOS" || document.creditoHipotecario.destination.value == "ADQUISICION CASA HABITACION MAS POR MENOS CON GARANTIA ADICIONAL")
				alert("Para estas condiciones requieres un Ingreso mayor");
			else
				alert("Para ese Ingreso mensual, por favor proporciona un plazo mayor");
		}else {
			alert("El valor m\u00EDnimo del Cr\u00E9dito es de $"  + currencyFormat(minCredit));
			document.creditoHipotecario.credit.value = 0;
		}
		return false;
	}

//*****	
	propertyPriceOrig = parseFloat(validaCampo(trim(document.creditoHipotecario.propertyPriceOrig.value)));
	creditOrig = parseFloat(validaCampo(trim(document.creditoHipotecario.creditOrig.value)));
	
	creditApprMax = (trim(currencyFormat(propertyPrice * creditPercentage + .01)));
	valuationApprMin = trim(currencyFormat(credit / creditPercentage));
	if ((credit > creditApprMax) && (simulation == 1)) {
		alert("El valor m\u00E1ximo del Cr\u00E9dito debe ser de $" + currencyFormat(creditApprMax ));
		document.creditoHipotecario.credit.value = creditApprMax  ;
		return false;
	}
	if ((propertyPrice < valuationApprMin) && (simulation == 2)) {
		alert("El valor m\u00EDnimo del aval\u00FAo debe ser de $" + currencyFormat(valuationApprMin));
		document.creditoHipotecario.propertyPrice.value = valuationApprMin;
		return false;
	}

	if (propertyPrice != propertyPriceOrig && simulation == 1){
		validatePropertyPrice();
		return false;
	}
	
	if (credit != creditOrig && simulation == 2){
		validateCredit();
		return false;
	}
//******


//I:NUEVO AVALUO MAXIMO ACH SOCIAL/ECONOMICA
	if (document.creditoHipotecario.destination.value =="ADQUISICION CASA HABITACION SOCIAL/ECONOMICA" && propertyPrice > maxValuationSE) {
		if (simulation == 3)
			alert("Para estas condiciones requieres un Pago menor");
		else if (simulation == 4)
			alert("Para estas condiciones requieres un Ingreso menor");
		else {
			alert( "El valor m\341ximo del aval\372o es de $"  + currencyFormat(maxValuationSE));
			document.creditoHipotecario.propertyPrice.value = maxValuationSE;
		}
		return false;
	}
//F:NUEVO AVALUO MAXIMO ACH SOCIAL/ECONOMICA
	tiie = parseFloat(validaCampo(trim(document.creditoHipotecario.tiie.value)));
	document.creditoHipotecario.tiie.value = tiie;
	
	if(document.creditoHipotecario.rateType.value == "Variable"){
		if(!(tiie > 0 && tiie <= 12.9)) {
			alert("El valor de TIIE debe ser mayor a 0% y menor o igual a 12.90%");
			return false;
		}
	}

	/*if(document.creditoHipotecario.destination.value == "CONSTRUCCION DE CASA HABITACION" && document.creditoHipotecario.rateType.value == "Fija") {
		alert("Para el destino de Construcci\u00F3n de casa habitaci\u00F3n s\u00F3lo aplica la Tasa variable con tope");
		return false;
	}*/

	if (document.creditoHipotecario.name.value == "" ) {
		alert("Captura el nombre del cliente");
		return false;
	}
        else{
            if(!(isAlphabetic(document.creditoHipotecario.name.value))){
                    alert("Proporciona un nombre v\u00E1lido");
                    return false;
                }
        }
        
        if((document.creditoHipotecario.destination.value == "ADQUISICION CASA HABITACION CON SEGURO HIPOTECARIO" || document.creditoHipotecario.destination.value == "ADQUISICION CASA HABITACION MAS POR MENOS CON SEGURO HIPOTECARIO")){
        //  if((document.creditoHipotecario.destination.value == "ADQUISICION CASA HABITACION MAS POR MENOS CON SEGURO HIPOTECARIO")&& (simulation == 1 || simulation == 2)){
           if(credit/propertyPrice < 0.85){
                alert("El valor del Cr\u00E9dito debe ser mayor al 85% del avaluo")
                return false;
            }else
                if(credit/propertyPrice >.95){
                    alert("El valor del Cr\u00E9dito debe ser menor al 95% del avaluo")
                      document.creditoHipotecario.credit.value = propertyPrice * .95
                    return false;
                }
        }


/*if (document.creditoHipotecario.destination.value =="ADQUISICION CASA HABITACION MAS POR MENOS"  && ( credit / propertyPrice ) >.85 &&  document.creditoHipotecario.openingComisionType.value =="Financiada"  )
       {      
		alert("La comision de Apertura debe ser de contado" );
		
		return false;
	}
*/
	
	document.creditoHipotecario.simulationDesc.value = document.creditoHipotecario.simulation[document.creditoHipotecario.simulation.selectedIndex].text;
	document.creditoHipotecario.ivaDesc.value = document.creditoHipotecario.iva[document.creditoHipotecario.iva.selectedIndex].text;
	document.creditoHipotecario.rateDesc.value = document.creditoHipotecario.rateType[document.creditoHipotecario.rateType.selectedIndex].text;
	document.creditoHipotecario.tiie.disabled=false;

//	window.resizeTo(865,595);
	document.creditoHipotecario.serviceName.value = "creditoHipotecarioDetailAction";	
	document.creditoHipotecario.submit();
}

function validatePropertyPrice(){
	propertyPrice = parseFloat(validaCampo(trim(document.creditoHipotecario.propertyPrice.value)));
	document.creditoHipotecario.propertyPrice.value = propertyPrice;

//	document.creditoHipotecario.propertyPrice.value = currencyFormat(propertyPrice);

	additionalPropertyPrice = parseFloat(validaCampo(trim(document.creditoHipotecario.additionalPropertyPrice.value)));
	document.creditoHipotecario.additionalPropertyPrice.value = additionalPropertyPrice;	
	
	minValuation = parseFloat(validaCampo(document.creditoHipotecario.minValuation.value));
	credit = parseFloat(validaCampo(trim(document.creditoHipotecario.credit.value)));	
	document.creditoHipotecario.credit.value = credit;
	minCredit = parseFloat(validaCampo(document.creditoHipotecario.minCredit.value));	
	creditPercentage = parseFloat(validaCampo(document.creditoHipotecario.creditPercent.value));

	simulation = parseInt(document.creditoHipotecario.simulation.value);
//NUEVO AVALUO MAXIMO ACH SOCIAL/ECONOMICA
	maxValuationSE = parseFloat(validaCampo(document.creditoHipotecario.maxValuationSE.value));
	
	if (propertyPrice <= 0 && simulation==1) {
		alert( "Por favor proporciona el Valor de aval\u00FAo aproximado");
		return false;
	}

	destinationIndex = parseInt(document.creditoHipotecario.destination.selectedIndex);
	if 	(destinationIndex <= 0 ) {
		alert( "Por favor selecciona un Destino" );
		return false;
	}

	valorComercialMedia = parseFloat(validaCampo(document.creditoHipotecario.valorComercialMedia.value));
	valorComercialResidencial = parseFloat(validaCampo(document.creditoHipotecario.valorComercialResidencial.value));
	valorComercialResidencialPlus = parseFloat(validaCampo(document.creditoHipotecario.valorComercialResidencialPlus.value));

	//DETERMINA EL TIPO DE VALOR DE RESIDENCIA
	if (propertyPrice <= valorComercialMedia)
		document.creditoHipotecario.valuationType.value	= "VALOR_COMERCIAL_MEDIA";
	else if (propertyPrice > valorComercialMedia &&  propertyPrice <= valorComercialResidencial)
		document.creditoHipotecario.valuationType.value	= "VALOR_COMERCIAL_RESIDENCIAL";
	else if (propertyPrice > valorComercialResidencialPlus)
		document.creditoHipotecario.valuationType.value	= "VALOR_COMERCIAL_RESIDENCIAL_PLUS";

	valuationApprMin = credit / creditPercentage;



	if ((propertyPrice < valuationApprMin) && (simulation != 1)&& (simulation != 2)) {
		alert("El valor m\355nimo del aval\372o debe ser de $" + currencyFormat(valuationApprMin));
		document.creditoHipotecario.propertyPrice.value = valuationApprMin;		
		return false;
	}
      
	
	if (propertyPrice < minValuation  ) {
		alert( "El valor m\355nimo del aval\372o es de $"  + currencyFormat(minValuation));
		document.creditoHipotecario.propertyPrice.value = minValuation;
		document.creditoHipotecario.credit.value = minValuation*creditPercentage;
		return false;
	}


if ((document.creditoHipotecario.destination.value == "ADQUISICION CASA HABITACION CON SEGURO HIPOTECARIO" || document.creditoHipotecario.destination.value == "ADQUISICION CASA HABITACION MAS POR MENOS CON SEGURO HIPOTECARIO") && simulation == 1 ){            
           // var val = credit/0.95;

            if(propertyPrice >349000 &&  propertyPrice<450001   && credit == 0){
             document.creditoHipotecario.credit.value = propertyPrice * 0.90;

             document.creditoHipotecario.creditPercent.value =  0.90;

            }else{
                if(propertyPrice>450000    && credit == 0){
             document.creditoHipotecario.credit.value = propertyPrice * 0.95;
	   document.creditoHipotecario.creditPercent.value =  0.95;
            }

            }
        }

if ((document.creditoHipotecario.destination.value == "ADQUISICION CASA HABITACION CON SEGURO HIPOTECARIO" || document.creditoHipotecario.destination.value == "ADQUISICION CASA HABITACION MAS POR MENOS CON SEGURO HIPOTECARIO") && simulation == 2 ){            
           // var val = credit/0.95;

            if(propertyPrice >349000 &&  propertyPrice<450001   ){
             document.creditoHipotecario.credit.value = propertyPrice * 0.90;

             document.creditoHipotecario.creditPercent.value =  0.90;

            }else{
                if(propertyPrice>450000    ){
             document.creditoHipotecario.credit.value = propertyPrice * 0.95;
	   document.creditoHipotecario.creditPercent.value =  0.95;
            }

            }
        }

//I:NUEVO AVALUO MAXIMO ACH SOCIAL/ECONOMICA
	if (document.creditoHipotecario.destination.value =="ADQUISICION CASA HABITACION SOCIAL/ECONOMICA" && propertyPrice > maxValuationSE) {
		alert( "El valor m\341ximo del aval\372o es de $"  + currencyFormat(maxValuationSE));
		document.creditoHipotecario.propertyPrice.value = maxValuationSE;
		return false;
	}
//F:NUEVO AVALUO MAXIMO ACH SOCIAL/ECONOMICA	
	if (simulation == 1 || simulation == 2 ) {
		if (document.creditoHipotecario.destination.value =="ADQUISICION CASA HABITACION CON GARANTIA ADICIONAL" || document.creditoHipotecario.destination.value =="ADQUISICION CASA HABITACION MAS POR MENOS CON GARANTIA ADICIONAL"){
			document.creditoHipotecario.additionalPropertyPrice.value = propertyPrice * .40;
		}
	}

	if (simulation == 1) {
		document.creditoHipotecario.ivaDesc.value = document.creditoHipotecario.iva[document.creditoHipotecario.iva.selectedIndex].text;
		document.creditoHipotecario.service.value = "enable";
		document.creditoHipotecario.serviceName.value = "creditoHipotecarioHomeAction";	
		document.creditoHipotecario.submit();
	}
}

function validateCredit() {
	propertyPrice = parseFloat(validaCampo(trim(document.creditoHipotecario.propertyPrice.value)));
	document.creditoHipotecario.propertyPrice.value = propertyPrice;
	additionalPropertyPrice = parseFloat(validaCampo(trim(document.creditoHipotecario.additionalPropertyPrice.value)));
	document.creditoHipotecario.additionalPropertyPrice.value = additionalPropertyPrice;
	minValuation = parseFloat(validaCampo(document.creditoHipotecario.minValuation.value));
	credit = parseFloat(validaCampo(trim(document.creditoHipotecario.credit.value)));	
	document.creditoHipotecario.credit.value = credit;	
	minCredit = parseFloat(validaCampo(document.creditoHipotecario.minCredit.value));	
	maxCredit = parseFloat(validaCampo(document.creditoHipotecario.maxCredit.value));		
	creditPercentage = parseFloat(validaCampo(document.creditoHipotecario.creditPercent.value));	
	simulation = parseInt(document.creditoHipotecario.simulation.value);
	termIndex = parseInt(document.creditoHipotecario.term.selectedIndex);
	
	if (credit <= 0 && simulation==2) {
		alert( "Por favor proporciona el Cr\u00E9dito Solicitado");
		return false;
	}

	destinationIndex = parseInt(document.creditoHipotecario.destination.selectedIndex);
	if 	(destinationIndex <= 0 ) {
		alert( "Por favor selecciona un Destino" );
		return false;
	}

	creditApprMax = propertyPrice * creditPercentage;


if ((document.creditoHipotecario.destination.value == "ADQUISICION CASA HABITACION CON SEGURO HIPOTECARIO" || document.creditoHipotecario.destination.value == "ADQUISICION CASA HABITACION MAS POR MENOS CON SEGURO HIPOTECARIO") && simulation == 1  ){            
           // var val = credit/0.95;

            if(propertyPrice >349000 &&  propertyPrice<450001  ){
             creditApprMax = propertyPrice * 0.90;
document.creditoHipotecario.creditPercent.value =  0.90;
           

            }else{
                if(propertyPrice>450000   ){
            creditApprMax =  propertyPrice * 0.95;
document.creditoHipotecario.creditPercent.value =  0.95;
	
            }

            }
        }




if ((document.creditoHipotecario.destination.value == "ADQUISICION CASA HABITACION CON SEGURO HIPOTECARIO" || document.creditoHipotecario.destination.value == "ADQUISICION CASA HABITACION MAS POR MENOS CON SEGURO HIPOTECARIO") && simulation == 2 ){            
         

            if(credit >405000 ){
             document.creditoHipotecario.propertyPrice.value = credit / 0.95;

             document.creditoHipotecario.creditPercent.value =  0.95;
            creditApprMax= 0.95;



             if((credit / 0.95) >349000 &&  (credit / 0.95)<450001  ){
             creditApprMax = propertyPrice * 0.90;
             document.creditoHipotecario.creditPercent.value =  0.90;
               document.creditoHipotecario.propertyPrice.value = credit / 0.90;

            }else{
                
            creditApprMax =  propertyPrice * 0.95;
            document.creditoHipotecario.creditPercent.value =  0.95;
	    document.creditoHipotecario.propertyPrice.value = credit / 0.95;
                      }


            }else{
                if(credit >405000 )
            creditApprMax= 0.90;
               document.creditoHipotecario.propertyPrice.value = credit / 0.90;

             document.creditoHipotecario.creditPercent.value =  0.90;


            }
        }

	
      // if ((document.creditoHipotecario.destination.value == "ADQUISICION CASA HABITACION CON SEGURO HIPOTECARIO" || document.creditoHipotecario.destination.value == "ADQUISICION CASA HABITACION MAS POR MENOS CON SEGURO HIPOTECARIO") && simulation != 2){
       if ((document.creditoHipotecario.destination.value == "ADQUISICION CASA HABITACION MAS POR MENOS CON SEGURO HIPOTECARIO") && simulation != 2){
            minCredit = propertyPrice * 0.85;            
        }
        
        if ((document.creditoHipotecario.destination.value == "ADQUISICION CASA HABITACION CON SEGURO HIPOTECARIO" || document.creditoHipotecario.destination.value == "ADQUISICION CASA HABITACION MAS POR MENOS CON SEGURO HIPOTECARIO") && simulation == 2){            
            var val = credit/ .95;
            if(val<=350000 && propertyPrice == 0){
                alert( "Para este destino necesitas un cr\u00E9dito mayor" );
		return false;
            }else{
                if(((credit/propertyPrice)<=0.85 || (credit/propertyPrice)> creditApprMax) && propertyPrice != 0){
                    alert( "El valor del cr\u00E9dito debe ser mayor a $"+ currencyFormat(propertyPrice*0.85)+ " y menor a $"+ currencyFormat(propertyPrice *  creditApprMax) );
                    return false;
                }    
            }
        }

	if ((credit > creditApprMax) && (simulation != 2)) {
		alert("El valor m\u00E1ximo del Cr\u00E9dito debe ser de $" + currencyFormat(creditApprMax));
		document.creditoHipotecario.credit.value = creditApprMax;
		return false;
	}
       
        if (credit <= minCredit && (document.creditoHipotecario.destination.value == "ADQUISICION CASA HABITACION CON SEGURO HIPOTECARIO" || document.creditoHipotecario.destination.value == "ADQUISICION CASA HABITACION MAS POR MENOS CON SEGURO HIPOTECARIO" )) {
		alert("El valor m\u00EDnimo del cr\u00E9dito debe ser mayor a $"  + currencyFormat(minCredit));
		document.creditoHipotecario.credit.value = currencyFormat(minCredit);
                return false;
	}       
        
	if (credit < minCredit) {
		alert("El valor m\u00EDnimo del Cr\u00E9dito es de $"  + currencyFormat(minCredit));
		document.creditoHipotecario.credit.value = minCredit;
		document.creditoHipotecario.propertyPrice.value = minCredit*creditPercentage;

		return false;
	}
	
	if (simulation == 2) {
		document.creditoHipotecario.ivaDesc.value = document.creditoHipotecario.iva[document.creditoHipotecario.iva.selectedIndex].text;
		document.creditoHipotecario.service.value = "enable";
		document.creditoHipotecario.serviceName.value = "creditoHipotecarioHomeAction";	
		document.creditoHipotecario.submit();
	}
}

function validateMonthlyPayment() {
	monthlyPayment = parseFloat(validaCampo(trim(document.creditoHipotecario.monthlyPayment.value)));
	document.creditoHipotecario.monthlyPayment.value = monthlyPayment;
	minMonthlyPayment = parseFloat(validaCampo(document.creditoHipotecario.minMonthlyPayment.value));
	document.creditoHipotecario.minMonthlyPayment.value = minMonthlyPayment;
	
	if (monthlyPayment != 0 && monthlyPayment < minMonthlyPayment) {
		alert("El valor m\u00EDnimo del pago mensual es de $" + currencyFormat(minMonthlyPayment));
		document.creditoHipotecario.monthlyPayment.value = 0;
		return false;
	}

	destinationIndex = parseInt(document.creditoHipotecario.destination.selectedIndex);
	if (destinationIndex <= 0) {
		alert( "Por favor selecciona un Destino" );
		return false;
	}
	
	document.creditoHipotecario.ivaDesc.value = document.creditoHipotecario.iva[document.creditoHipotecario.iva.selectedIndex].text;
	document.creditoHipotecario.service.value = "enable";
	document.creditoHipotecario.serviceName.value = "creditoHipotecarioHomeAction";	
	document.creditoHipotecario.submit();
}

function ValidateOpenningCommission(field){
	openingTaxOrigic = document.creditoHipotecario.openingTaxOrigic.value;
	openningCommission = field.value;
	if(openningCommission > openingTaxOrigic && openningCommission != 0){
		alert(" El valor de la comision por apertura maximo es de "+(openingTaxOrigic*100).toFixed (2) +" % ");
		openningCommission = openingTaxOrigic;
	}else{
		openningCommission = openningCommission;
	}
	field.value = (openningCommission*100).toFixed(2);
	document.creditoHipotecario.openingTaxOrigic.value = openingTaxOrigic;
	propertyPrice = parseFloat(validaCampo(trim(document.creditoHipotecario.propertyPrice.value)));
	document.creditoHipotecario.propertyPrice.value = propertyPrice;
	credit = parseFloat(validaCampo(trim(document.creditoHipotecario.credit.value)));
	document.creditoHipotecario.credit.value = credit;
	monthlyPayment = parseFloat(validaCampo(trim(document.creditoHipotecario.monthlyPayment.value)));
	document.creditoHipotecario.monthlyPayment.value = monthlyPayment;
	monthlyIncome = parseFloat(validaCampo(trim(document.creditoHipotecario.monthlyIncome.value)));
	document.creditoHipotecario.monthlyIncome.value = monthlyIncome;
	document.creditoHipotecario.service.value = "enable";
	document.creditoHipotecario.serviceName.value = "creditoHipotecarioHomeAction";	
	document.creditoHipotecario.submit();
}

function validateMonthlyIncome(){
	monthlyIncome = parseFloat(validaCampo(trim(document.creditoHipotecario.monthlyIncome.value)));
	document.creditoHipotecario.monthlyIncome.value = monthlyIncome;
	minMonthlyIncome = parseFloat(validaCampo(document.creditoHipotecario.minMonthlyIncome.value));
	document.creditoHipotecario.minMonthlyIncome.value = minMonthlyIncome;

	if (monthlyIncome != 0 && monthlyIncome < minMonthlyIncome) {
		alert("El valor m\u00EDnimo del ingreso mensual es de $" + currencyFormat(minMonthlyIncome));
		document.creditoHipotecario.monthlyIncome.value = 0;
		return false;
	}
	
	destinationIndex = parseInt(document.creditoHipotecario.destination.selectedIndex);
	if (destinationIndex <= 0) {
		alert( "Por favor selecciona un Destino" );
		return false;
	}

	document.creditoHipotecario.ivaDesc.value = document.creditoHipotecario.iva[document.creditoHipotecario.iva.selectedIndex].text;
	document.creditoHipotecario.service.value = "enable";
	document.creditoHipotecario.serviceName.value = "creditoHipotecarioHomeAction";	
	document.creditoHipotecario.submit();
}
function validateIvaYes(){
	propertyPrice = parseFloat(validaCampo(trim(document.creditoHipotecario.propertyPrice.value)));
	document.creditoHipotecario.propertyPrice.value = propertyPrice;
	credit = parseFloat(validaCampo(trim(document.creditoHipotecario.credit.value)));
	document.creditoHipotecario.credit.value = credit;
	monthlyPayment = parseFloat(validaCampo(trim(document.creditoHipotecario.monthlyPayment.value)));
	document.creditoHipotecario.monthlyPayment.value = monthlyPayment;
	monthlyIncome = parseFloat(validaCampo(trim(document.creditoHipotecario.monthlyIncome.value)));
	document.creditoHipotecario.monthlyIncome.value = monthlyIncome;
	document.creditoHipotecario.service.value = "enable";
	document.creditoHipotecario.serviceName.value = "creditoHipotecarioHomeAction";	
	document.creditoHipotecario.submit();
}

/*
function activaTiie() {
	tiie = parseFloat(validaCampo(document.creditoHipotecario.tiie.value));

	if(document.creditoHipotecario.rateType.value == "Fija") {
		document.creditoHipotecario.tiie.disabled = true;
		document.creditoHipotecario.term.options[document.creditoHipotecario.term.options.length-1]=null;
		document.creditoHipotecario.term.options[document.creditoHipotecario.term.options.length-1]=null;
		document.creditoHipotecario.term.options[document.creditoHipotecario.term.options.length-1]=null;
		document.creditoHipotecario.term.options[document.creditoHipotecario.term.options.length-1]=null;
		document.creditoHipotecario.term.options[document.creditoHipotecario.term.options.length-1]=null;
		tiie = 0;
	}
	else {
		document.creditoHipotecario.tiie.disabled = false;
		if (document.creditoHipotecario.term.options.length < 17) {
			document.creditoHipotecario.term.options[document.creditoHipotecario.term.options.length] = new Option('192','192');
			document.creditoHipotecario.term.options[document.creditoHipotecario.term.options.length] = new Option('204','204');
			document.creditoHipotecario.term.options[document.creditoHipotecario.term.options.length] = new Option('216','216');
			document.creditoHipotecario.term.options[document.creditoHipotecario.term.options.length] = new Option('228','228');
			document.creditoHipotecario.term.options[document.creditoHipotecario.term.options.length] = new Option('240','240');
		}
	}
	
	if (tiie == 0) {
		document.creditoHipotecario.tiie.value = 0;
	}
	else {
		document.creditoHipotecario.tiie.value = tiie;
	}
}
*/	
function activaTiie() {

	tiie = parseFloat(validaCampo(document.creditoHipotecario.tiie.value));
       // if(document.creditoHipotecario.destination.value != "ADQUISICION CASA HABITACION MAS POR MENOS CON SEGURO HIPOTECARIO" && document.creditoHipotecario.destination.value != "ADQUISICION CASA HABITACION MAS POR MENOS" && document.creditoHipotecario.destination.value != "ADQUISICION CASA HABITACION MAS POR MENOS CON GARANTIA ADICIONAL"){
	if(document.creditoHipotecario.rateType.value == "Fija") {
		document.creditoHipotecario.tiie.disabled = true;
		if (document.creditoHipotecario.destination.value != "ADQUISICION CASA HABITACION SOCIAL/ECONOMICA"){
	            if (document.creditoHipotecario.destination.value != "ADQUISICION CASA HABITACION MAS POR MENOS CON SEGURO HIPOTECARIO" && document.creditoHipotecario.destination.value != "ADQUISICION CASA HABITACION MAS POR MENOS" && document.creditoHipotecario.destination.value != "ADQUISICION CASA HABITACION MAS POR MENOS CON GARANTIA ADICIONAL" && document.creditoHipotecario.destination.value != "CONSTRUCCION DE CASA HABITACION" && document.creditoHipotecario.destination.value != "PAGO DE PASIVO HIPOTECARIO BANORTE" && document.creditoHipotecario.destination.value != "ADQUISICION DE TERRENO URBANIZADO" && document.creditoHipotecario.destination.value != "REMODELACION" && document.creditoHipotecario.destination.value != "LIQUIDEZ HIPOTECARIA" && document.creditoHipotecario.destination.value != "ADQUISICION DE CASA HABITACION" && document.creditoHipotecario.destination.value != "ADQUISICION CASA HABITACION CON GARANTIA ADICIONAL" && document.creditoHipotecario.destination.value != "ADQUISICION CASA HABITACION CON SEGURO HIPOTECARIO"&& document.creditoHipotecario.destination.value != ""){
				
				document.creditoHipotecario.term.options[document.creditoHipotecario.term.options.length-1]=null;
				document.creditoHipotecario.term.options[document.creditoHipotecario.term.options.length-1]=null;
				document.creditoHipotecario.term.options[document.creditoHipotecario.term.options.length-1]=null;
				document.creditoHipotecario.term.options[document.creditoHipotecario.term.options.length-1]=null;
				document.creditoHipotecario.term.options[document.creditoHipotecario.term.options.length-1]=null;
			} else {
				if (document.creditoHipotecario.term.options.length < 27) {


if ( document.creditoHipotecario.destination.value != "LIQUIDEZ HIPOTECARIA" &&  document.creditoHipotecario.destination.value != "ADQUISICION DE TERRENO URBANIZADO"  && document.creditoHipotecario.destination.value != "ADQUISICION CASA HABITACION MAS POR MENOS CON SEGURO HIPOTECARIO" && document.creditoHipotecario.destination.value != "ADQUISICION CASA HABITACION MAS POR MENOS" && document.creditoHipotecario.destination.value != "ADQUISICION CASA HABITACION MAS POR MENOS CON GARANTIA ADICIONAL"){
				
				if(document.creditoHipotecario.cashBackType.value == "No") {
				
                                      document.creditoHipotecario.term.options[document.creditoHipotecario.term.options.length] = new Option('252','252');
					document.creditoHipotecario.term.options[document.creditoHipotecario.term.options.length] = new Option('264','264');
					document.creditoHipotecario.term.options[document.creditoHipotecario.term.options.length] = new Option('276','276');
					document.creditoHipotecario.term.options[document.creditoHipotecario.term.options.length] = new Option('288','288');
					document.creditoHipotecario.term.options[document.creditoHipotecario.term.options.length] = new Option('300','300');
					document.creditoHipotecario.term.options[document.creditoHipotecario.term.options.length] = new Option('312','312');
					document.creditoHipotecario.term.options[document.creditoHipotecario.term.options.length] = new Option('324','324');
					document.creditoHipotecario.term.options[document.creditoHipotecario.term.options.length] = new Option('336','336');
					document.creditoHipotecario.term.options[document.creditoHipotecario.term.options.length] = new Option('348','348');
					document.creditoHipotecario.term.options[document.creditoHipotecario.term.options.length] = new Option('360','360');
				}}else{


}

}
			}
		}	
		tiie = 0;
	} else {
		document.creditoHipotecario.tiie.disabled = false;
		if (document.creditoHipotecario.destination.value != "ADQUISICION CASA HABITACION SOCIAL/ECONOMICA"){
			if ( document.creditoHipotecario.destination.value != "LIQUIDEZ HIPOTECARIA" &&  document.creditoHipotecario.destination.value != "ADQUISICION DE TERRENO URBANIZADO" &&   document.creditoHipotecario.destination.value != "ADQUISICION CASA HABITACION MAS POR MENOS CON SEGURO HIPOTECARIO" &&  document.creditoHipotecario.destination.value != "ADQUISICION CASA HABITACION MAS POR MENOS" && document.creditoHipotecario.destination.value != "ADQUISICION CASA HABITACION MAS POR MENOS CON GARANTIA ADICIONAL"&& document.creditoHipotecario.destination.value != "ADQUISICION DE CASA HABITACION" && document.creditoHipotecario.destination.value != "ADQUISICION CASA HABITACION CON GARANTIA ADICIONAL" && document.creditoHipotecario.destination.value != "ADQUISICION CASA HABITACION CON SEGURO HIPOTECARIO"){		
				if (document.creditoHipotecario.term.options.length < 17) {
if(document.creditoHipotecario.cashBackType.value == "No") {					
document.creditoHipotecario.term.options[document.creditoHipotecario.term.options.length] = new Option('192','192');
					document.creditoHipotecario.term.options[document.creditoHipotecario.term.options.length] = new Option('204','204');
					document.creditoHipotecario.term.options[document.creditoHipotecario.term.options.length] = new Option('216','216');
					document.creditoHipotecario.term.options[document.creditoHipotecario.term.options.length] = new Option('228','228');
					document.creditoHipotecario.term.options[document.creditoHipotecario.term.options.length] = new Option('240','240');
				}}
			} else {
if (  document.creditoHipotecario.destination.value != "LIQUIDEZ HIPOTECARIA" &&  document.creditoHipotecario.destination.value != "ADQUISICION DE TERRENO URBANIZADO" &&  document.creditoHipotecario.destination.value != "ADQUISICION CASA HABITACION MAS POR MENOS CON SEGURO HIPOTECARIO" && document.creditoHipotecario.destination.value != "ADQUISICION CASA HABITACION MAS POR MENOS" && document.creditoHipotecario.destination.value != "ADQUISICION CASA HABITACION MAS POR MENOS CON GARANTIA ADICIONAL" ){
if(document.creditoHipotecario.cashBackType.value == "No") {
				document.creditoHipotecario.term.options[document.creditoHipotecario.term.options.length-1]=null;
				document.creditoHipotecario.term.options[document.creditoHipotecario.term.options.length-1]=null;
				document.creditoHipotecario.term.options[document.creditoHipotecario.term.options.length-1]=null;
				document.creditoHipotecario.term.options[document.creditoHipotecario.term.options.length-1]=null;
				document.creditoHipotecario.term.options[document.creditoHipotecario.term.options.length-1]=null;
				document.creditoHipotecario.term.options[document.creditoHipotecario.term.options.length-1]=null;
				document.creditoHipotecario.term.options[document.creditoHipotecario.term.options.length-1]=null;
				document.creditoHipotecario.term.options[document.creditoHipotecario.term.options.length-1]=null;
				document.creditoHipotecario.term.options[document.creditoHipotecario.term.options.length-1]=null;
				document.creditoHipotecario.term.options[document.creditoHipotecario.term.options.length-1]=null;
			
}
}else{}
}
		}	
	}
	//}
	if (tiie == 0) {
		document.creditoHipotecario.tiie.value = 0;
	}
	else {
		document.creditoHipotecario.tiie.value = tiie;
	}

}

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 validateAdvancePay(field){
	field.value = validField(trim(field.value));
}

function trim(str) {
//	return str.replace(/^\s*|\s*$|\,/g,"");
	
	str = str.replace(",","");
	str = str.replace(",","");		
	str = str.replace("$","");		
	return str
	
}
	
function validaCampo(valor) {
	return (valor == null || valor == "" || isNaN(valor)) ? 0 : valor;
}

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

function validateTiie(field){
	field = parseFloat(validaCampo(trim(field.value)));
	document.creditoHipotecario.tiie.value = field;
}

//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++){
		advancePayment = document.getElementById("advancePayment_" + i).value;
		advancePayment = trim(advancePayment);
		document.getElementById("advancePayment_" + i).value = advancePayment;
	}
	document.recalculate.submit();
}

// Valida Texto
function isAlphabetic (valor){
    var i;    
    for (i = 0; i < valor.length; i++)
    {   
        var c = valor.charAt(i);
        if (!isLetter(c)){            
            return false;
        }
    }
    return true;
}

function isLetter (c){
    var patron = "abcdefghijklmnopqrstuvwxyzáéíóúñü ABCDEFGHIJKLMNOPQRSTUVWXYZÁÉÍÓÚÑ.";
    return( ( patron.indexOf( c ) != -1 ))
}

