//-------------------------------------------------------//
// Author:Rodolfo Machon
// Company:WebSoft, SA de CV 
// Nombre del archivo: bsws.financiamiento.js
// Created: 2016-07-17
// Updates: 2016-07-17
// Copyright - All Rights Reserved
//-------------------------------------------------------//
//-------------------------------------------------------//
var xmlHttp1;
var xmlHttpStream1;
//var GridData;
//--------------------------------------------------------//
function getFinanciamientoActivo(){
	//GridData 	= document.form.GridDatos.value;
	var generador = document.form.generador.value;
    var URL		= "/cgi-bin/"+appid+"/pl/financiamiento.activo.get.pl?lan="+lan+"&appid="+appid+"&clase="+generador+"&id="+id;
    xmlHttp1	= GetXmlHttpObject(getFinanciamientoActivoSC);
    xmlHttp1.open("GET",URL,true) ;
    xmlHttp1.send(null);	
}
function getFinanciamientoActivoSC(){
    if (xmlHttp1.readyState==4 || xmlHttp1.readyState=="complete"){ 
        xmlHttpStream1=xmlHttp1.responseText;
		//document.write(xmlHttpStream1);		
		var StreamA = xmlHttpStream1.split("~");
		var finan_activo = StreamA[0];
		var FinanDiv	 = StreamA[1];
		if(finan_activo=='SI'){
			document.getElementById('FinanciamientoDiv').innerHTML=FinanDiv;
			document.all.FinanciamientoDiv.style.visibility	= 'visible';
			document.all.GridDiv.style.visibility			= 'hidden';
			document.all.GridDiv.style.display				= 'none';
		}else{
			calcFinancimientoPlan();
		}
    } 
}
function calcFinancimientoPlan(){
	var Generador		= document.form.generador.value;
	var PeriodosVencidos= 0;
	var Desde 			= "";
	var Hasta 			= "";
	var MontoVencido 	= 0;
	var CargoCorriente 	= 0;
	var Fin				= 1;
	var Rows   		= GridData.split("~~");	
	if(Rows<1){alert(document.form.empty_grid.value);return}
	for(var k=1;k<Rows.length;k++ ) {
		if(eval("document.form.chk_"+k+".checked")){
			var RowsValues	= Rows[k-1].split("&&");
			for(var l=0;l<RowsValues.length-1;l++ ) {
				var TariffGridFIE= 0;
				var TariffGridINT= 0;
				var TariffGridMUL= 0;
				var TariffValues = RowsValues[l].split("|");
				var TariffGridPER= eval("document.form.PER_"+k+".value");
				var TariffGridDIM= eval("document.form.DIM_"+k+".value");
				var TariffGridVAL= eval("document.form.TAR_"+TariffValues[2].substr(0,8)+"_"+k+".value");
				var TarData = RowsValues[l].split("|");				
				if(TariffGridVAL>0){
					var Trib  = GetTributo(TariffValues[2].substr(0,8));
					if(document.form.ACC_FIESTAS_chk){if(document.form.ACC_FIESTAS_chk.checked){if(TarData[4]>0){TariffGridFIE = Math.round(TariffGridVAL*GridParams['FIE']*DDigits[i2_digitos])/DDigits[i2_digitos];}}}
					if(document.form.ACC_INTERESES_chk){if(document.form.ACC_INTERESES_chk.checked){if(TarData[5]>0){TariffGridINT = Math.round(TariffGridDIM*TariffGridVAL*(GridParams['INT']/365)*DDigits[i2_digitos])/DDigits[i2_digitos];}}}
					if(document.form.ACC_MULTAS_chk){
						if(document.form.ACC_MULTAS_chk.checked){
							if(TarData[6]>0){
								if(TariffGridDIM>=1 && TariffGridDIM<90){TariffGridMUL = TariffGridVAL*GridParams['M89']}
								if(TariffGridDIM>=90){TariffGridMUL = TariffGridVAL*GridParams['M90']}
								if(TariffGridDIM<1){TariffGridMUL = 0}
								TariffGridMUL = Math.round(TariffGridMUL*DDigits[i2_digitos])/DDigits[i2_digitos];
							}
						}
					}
					if(k==1){Desde=TariffGridPER}
					Hasta = TariffGridPER;
					if(k==Rows.length-1){Actual = TariffGridPER;CargoCorriente += (TariffGridVAL*1)+(TariffGridFIE*1)+(TariffGridINT*1)+(TariffGridMUL*1);}					
				}
				if (TariffGridDIM>0) {
						MontoVencido +=(TariffGridVAL*1)+(TariffGridFIE*1)+(TariffGridINT*1)+(TariffGridMUL*1);
				}
			}
			if(eval("document.form.DIM_"+k+".value")>0){
				PeriodosVencidos++
			}
		}
	}
	MontoVencido = Math.round(MontoVencido*DDigits[i2_digitos])/DDigits[i2_digitos];
	MontoVencido = MontoVencido.toFixed(2) 
	if (!openwindow1 || openwindow1.closed){
		win ="window.open('/cgi-bin/"+appid+"/pl/financiamiento.plan.calc.pl?lan="+lan+"&appid="+appid+"&clase="+Generador+"&id="+id+"&periodos_vencidos="+PeriodosVencidos+"&dt_desde="+Desde+"&dt_hasta="+Hasta+"&monto_vencido="+MontoVencido+"&cargo_corriente="+CargoCorriente+"','FinaciarEstado','left=0,top=0,width=600,height=400,scrollbars=yes,toolbar=no,menu=no,statusbar=no,location=no')";
		openwindow1=eval(win);
	}
	else{openwindow1.focus()}
}
function GoFinanciamientos(){
    var winame = "GoFinancimientos";
    var fields = "";
    var values = "";
    if (!OpenWindows[winame] || OpenWindows[winame].closed){
		win ="window.open('/cgi-bin/"+appid+"/pl/financiamientos_rept.pl?lan="+lan+"&appid="+appid+"&clase="+clase+"&id="+id+"','"+winame+"','left=0,top=0,width=100,height=100,resizable=yes,scrollbars=yes')";
		OpenWindows[winame]=eval(win);
    }
    else{OpenWindows[winame].focus()}
}
function PagarCuota(){
	document.all.FinanciamientoDiv.style.display= 'none';	
	document.all.GridDiv.style.visibility		= 'visible';
	document.all.GridDiv.style.display			= 'inline';

	var T		= document.getElementById('plan_pago');
	var rows	= T.getElementsByTagName('tr');
	var apl_fid	= T.rows[1].cells[0].innerHTML;
	var apl_fie	= T.rows[1].cells[7].innerHTML;
	var apl_int	= T.rows[1].cells[8].innerHTML;
	var	apl_mul = T.rows[1].cells[9].innerHTML;
	var	apl_men = T.rows[1].cells[10].innerHTML;
	var	tip_cuo = T.rows[1].cells[11].innerHTML;

	document.form.i8_financiamiento_id.value=apl_fid;

	if (apl_fie=="NO") {
		if (document.form.ACC_FIESTAS_chk) {
			if(eval("document.form.ACC_FIESTAS_chk.checked")){
				document.form.ACC_FIESTAS_chk.checked=false;
				var hilo1=ClearColumnByName("ACC_FIESTAS_chk");
			}
		}
	}
	if (apl_int=="NO") {
		if (document.form.ACC_INTERESES_chk) {
			if(eval("document.form.ACC_INTERESES_chk.checked")){
				document.form.ACC_INTERESES_chk.checked=false;
				var hilo2=ClearColumnByName("ACC_INTERESES_chk");
			}		
		}
	}
	if (apl_mul=="NO") {
		if (document.form.ACC_MULTAS_chk) {		
			if(eval("document.form.ACC_MULTAS_chk.checked")){
				document.form.ACC_MULTAS_chk.checked=false;
				var hilo2=ClearColumnByName("ACC_MULTAS_chk");
			}
		}
	}
	//alert(document.form.i8_financiamiento_id.value+" <> "+tip_cuo);
	if (tip_cuo=="FIJA") { PagarCuotaFija(apl_men,apl_fie,apl_int,apl_mul) }
	if (tip_cuo=="VARIABLE") { PagarCuotaVariable(apl_men) }
}
function PagarCuotaFija(am,af,ai,amu){
	var count   = 0;
	var saldo	= 0;
	var val_per = 0;	
    var cuo_mor	= "";
    var cuo_tot	= "";	
	var ATributos	= Tributos.split("|");
    var T		= document.getElementById('plan_pago_detalle');
    var rows	= T.getElementsByTagName('tr');
    for(var i=1;i<rows.length;i++){
		var x = T.rows[i].cells[8].innerHTML;
		if(x==="DEBIDA" && count===0){
			cuo_dat = T.rows[i].cells[1].innerHTML;			
			cuo_mor = T.rows[i].cells[5].innerHTML;
			cuo_tot = T.rows[i].cells[7].innerHTML;			
			count++;
			document.form.dt_cuota.value=cuo_dat;
		} 
    }
	saldo = cuo_mor;
    var T1 = document.getElementById('Grid');
    for (var i=1;i<T1.rows.length-1;i++){
		saldo -= val_per;
		if (val_per>=saldo && saldo>0 ) {
			var tribs   = 0;
			var mul_por = 0;
			var dim_val	= eval("document.form.DIM_"+i+".value")*1;
			var per 	= eval("document.form.PER_"+i+".value");			
			if(dim_val>=90){mul_por = GridParams['M90']}else{mul_por = GridParams['M89']}
			if (af=="SI" && ai=="SI" && amu=="SI") { tribs = (saldo/((1)+(1*GridParams['FIE'])+(1*GridParams['INT']*(dim_val/365))+(1*GridParams[mul_por]*(dim_val/365)))) }
			if (af=="SI" && ai=="SI" && amu=="NO") { tribs = (saldo/((1)+(1*GridParams['FIE'])+(1*GridParams['INT']*(dim_val/365)))) }			
			if (af=="SI" && ai=="NO" && amu=="NO") { tribs = (saldo/((1)+(1*GridParams['FIE']))) }
			if (af=="NO" && ai=="NO" && amu=="NO") { tribs = (saldo/(1)) }			
		
			for (var p=0;p<ATributos.length-1;p++ ) {
				var ATributo = ATributos[p].split(",");
				val_tri = eval("document.form.TAR_"+ATributo[0]+"_"+i+".value");
				if (tribs<=val_tri && saldo>0) {
					eval("document.form.TAR_"+ATributo[0]+"_"+i+".value="+Math.round((tribs)*DDigits[i2_digitos])/DDigits[i2_digitos]);
					saldo = 0;
				}else if(tribs>=val_tri && saldo>0){
					tribs -= val_tri;
					eval("document.form.TAR_"+ATributo[0]+"_"+i+".value="+Math.round((val_tri)*DDigits[i2_digitos])/DDigits[i2_digitos]);					
				}else{
					eval("document.form.TAR_"+ATributo[0]+"_"+i+".value='0.00'");
				}
				var hilo = ChangeCellByName("TAR_"+ATributo[0]+"_"+i);
			}
			continue;
		}
		if (saldo<=0) {
			if(am=="SI" && i==T1.rows.length-2){
				
			}else{
				T1.rows(i).cells(0).firstChild.click();
			}
		}
		val_per = eval("document.form.TOT_"+i+".value");
    }
	var int_tot = document.form.STA_INT.value*1;
	var val_tot	= document.form.TTOT.value*1;
	if(cuo_tot>val_tot) {
		document.form.STA_INT.value=Math.round((int_tot+(cuo_tot-val_tot))*DDigits[i2_digitos])/DDigits[i2_digitos];
	}else if (cuo_tot<val_tot) {
		document.form.STA_INT.value=Math.round((int_tot-(val_tot-cuo_tot))*DDigits[i2_digitos])/DDigits[i2_digitos];
	}
	//document.form.TSTA.value = ((document.form.STA_FIE.value*1)+(document.form.STA_INT.value*1)+(document.form.STA_MUL.value*1))
	//document.form.TTOT.value =((document.form.TSTA.value*1)+(document.form.TSTT.value*1));
//Pendiente verificar si aplica fiestas,intereses y multas para esta ultima parte
}
function PagarCuotaVariable(am){
    var desde   = "";
    var hasta   = "";
    var mensual = "";
    var count   = 0;
    var T		= document.getElementById('plan_pago_detalle');
    var rows	= T.getElementsByTagName('tr');
    for(var i=1;i<rows.length;i++){
		var x = T.rows[i].cells[8].innerHTML;
		if(x==="DEBIDA" && count===0){
			count++;
			cuo_dat = T.rows[i].cells[1].innerHTML;
			desde   = T.rows[i].cells[3].innerHTML;
			hasta   = T.rows[i].cells[4].innerHTML;
			mensual = T.rows[i].cells[6].innerHTML;
			document.form.dt_cuota.value=cuo_dat;
		} 
    }
    var T1     = document.getElementById('Grid');
    var rows1  = T1.rows;
    for (var i=1;i<rows1.length-1;i++){
		var Per = eval("document.form.PER_"+i+".value");
		if (Per>=desde & Per<=hasta){

		}else{
			if(am=="SI" && i==rows1.length-2){
				
			}else{
				T1.rows(i).cells(0).firstChild.click();
			}
		}
    }	
}
//-------------------------------------------------------//