//alert("simcre");
var forma = document.frmCalcula;		

// Arreglo de rangos de valores..
var aRanVal;


function ObtenDato(TipDat)
{
	
	// Si es consulta del valor de la udi y del salario minimo
	// para los valores referencia para los rangos de valores
	if (TipDat =="ParIni")
	{
		CadParam = "?CvePro=ParIni"
	}
	else if (TipDat =="ParSub")
	{
		CadParam = "?CvePro=ParSub"
	}
	
	var xmldom, strURL;								  	
	xmldom = new ActiveXObject("microsoft.xmldom");
	with( document.location )
	strURL = protocol + "//" + hostname + forma.RutApl.value + "/Genera_XML.asp"
	strURL += CadParam
	xmldom.async = false;
	
	//alert(strURL);
	
	if( xmldom.load(strURL) )
	{
		var root = xmldom.documentElement;
		var element, strValor, strNombre;
		var obj;	    	    			
		
		element = root.childNodes(0)	  		  

		if (root.childNodes.length==0)
		{	alert("No se encontro ningun registro."); 	}		   			   			
						
		for( i = 0; i < element.childNodes.length; i++ )
		{	                                         
			strNombre = element.childNodes(i).nodeName
			strValor = element.childNodes(i).text;		
			
			//alert(strNombre);
			//alert(strValor);
			
			// Asignamos objeto y valor
			obj = eval(forma.name + "." + strNombre );
			obj.value = strValor;
		}			       	
	}
	else
	{
		alert( "No se pudo hacer la consulta" );
	}	
}

function ObtenProUsu()
{		

	// Obtenemos los valores de udi y sal.min
	ObtenDato('ParIni')

	var CboTic = eval(forma.name + ".TiCrSi");
	//alert(forma.Usuari.value);
//	return 0;
	
	///
	var xmldom, strURL;
	xmldom = new ActiveXObject("microsoft.xmldom");
	with( document.location )	
	  strURL = protocol + "//" + hostname + forma.RutApl.value + "/GeneraRegs_XML.asp"
	  strURL += "?CvePro=ProTicUsu&Usuari=" + forma.Usuari.value
	xmldom.async = false;		
	
/*	alert(strURL);
	alert(xmldom.load(strURL));*/

	
	if( xmldom.load(strURL) )
	{
		
		var strHTML ="";							
		var i = 0 
		var xmldom, root;
		
		// Inicializa variables
		root = xmldom.documentElement;									
		
		var aProTic
		var CvePro= ""; DesPro = ""
		//var CveTic= ""; DesTic = ""
		var EstBot= ""; LeyBot = ""; var DesTic = "";

		// Asignamos al combo 
		with(CboTic.options)	
		{	
			// Limpiamos el combo
			innerHTML = "";
			
			// Ciclo mientras tipos de crédito y productos
			for( i = 0; i < root.childNodes.length; i++ )
			{															
				// Lee elementos
				element = root.childNodes(i); 																        
	//			// Genera detalle html
				Renglon = i % 2
			
				// Seperamos los campos provenientes de la cadena
				aProTic = element.childNodes(0).text.split("|")
				
				// Guardamos cadena completa 
				forma.ProTicUsu.value += element.childNodes(0).text 								
				
				// Desglozamos campos
				CvePro = aProTic[0]			
				DesPro = aProTic[1]			
				
				// Para evitar nombres de producto repetidos
				// Si la descripción es diferente al nombre del producto
				if (DesTic != DesPro)
				{	add( new Option( DesPro,CvePro) );		}
				DesTic = DesPro
			}
			// Inicializamos el combo
			CboTic.selectedIndex = -1;
		}		
	}			
}

function MonPlaCofina()
{		
	var CboMon = "";
	var Produc = forma.Produc.value
	/*alert(Produc )
	alert(forma.MonPlaCof.value);*/

	// Si es INFONAVIT
	if (Produc == "INFCIA" || Produc == "INFCOF" || Produc == "INFGCO")
	{	
		CboMon = eval(forma.name + ".TipCre");
	
	}
	else if  (Produc == "FOVALI" || Produc == "FOVRES")
	{
		CboMon = eval(forma.name + ".CFMoCr");		
	}	



	//var CboMon = eval(forma.name + ".TipCre");
	//var CboPla = eval(forma.name + ".PlaCre");
	//alert(forma.Usuari.value);
//	return 0;
	
	///
	var xmldom, strURL;
	xmldom = new ActiveXObject("microsoft.xmldom");
	with( document.location )	
	  strURL = protocol + "//" + hostname + forma.RutApl.value + "/GeneraRegs_XML.asp"
	  strURL += "?CvePro=MonPlaCof&Usuari=" + forma.Usuari.value + "&TipCre=" + forma.TiCrSi.value;
  	  strURL += "&Produc=" + forma.Produc.value;	 
	  
	xmldom.async = false;		
/*	
alert(strURL);
	alert(xmldom.load(strURL));*/
	
	// Inicializamos 
	forma.MonPlaCof.value = ""

	
	if( xmldom.load(strURL) )
	{

		var strHTML ="";							
		var i = 0 
		var xmldom, root;
		
		// Inicializa variables
		root = xmldom.documentElement;									
		
		var aMonPla
		var CveMon = ""; Plazo = ""
		var MonTem = ""; DesMon = "";
		//var CveTic= ""; DesTic = ""
		//var EstBot= ""; LeyBot = ""; var DesTic = "";
		//alert(root.childNodes.length);
		// Asignamos al combo 
		with(CboMon.options)	
		{	
			// Limpiamos el combo
			innerHTML = "";
			
			// Ciclo mientras tipos de crédito y productos
			for( i = 0; i < root.childNodes.length; i++ )
			{															
				// Lee elementos
				element = root.childNodes(i); 																        
	//			// Genera detalle html
				Renglon = i % 2
			
				// Seperamos los campos provenientes de la cadena
				aMonPla = element.childNodes(0).text.split("|")
				
			//	alert(element.childNodes(0).text);
				
				// Guardamos cadena completa 
				forma.MonPlaCof.value += element.childNodes(0).text 								
				
				// Desglozamos campos
				CveMon = aMonPla[0]			
				Plazo = aMonPla[1]			

				// Para evitar nombres de producto repetidos
				// Si la descripción es diferente al nombre del producto
				if (CveMon != MonTem)
				{	
					if (CveMon == 1)
					{	DesMon = "SMF"	}
					else
					{	DesMon = "PESOS"	}
					add( new Option( DesMon,CveMon) );		
				}
				MonTem = CveMon
			}
			// Inicializamos el combo
			CboMon.selectedIndex = 0;
			ObtenPlaMonCof()
		}		
	}	
}

function ObtenPlaMonCof()
{
	// Obtenemos tipo de producto..
	var Moneda;
	var CboPla;
	var aPlaMon	= forma.MonPlaCof.value.split("|")
	var Produc = forma.Produc.value
	/*alert(Produc )
	alert(forma.MonPlaCof.value);*/

	// Si es INFONAVIT
	if (Produc == "INFCIA" || Produc == "INFCOF" || Produc == "INFGCO" || Produc == "INFCSU")
	{
		Moneda = forma.TipCre.value
		CboPla = eval(forma.name + ".PlaCre");
	}
	else if  (Produc == "FOVALI" || Produc == "FOVRES" || Produc == "FOALSU")
	{
		Moneda = forma.CFMoCr.value
		CboPla = eval(forma.name + ".CFPlCr");
	}	
	
	
	// Para el combo de tipos de crédito
	with(CboPla.options)	
	{				
		// Limpiamos el combo
		innerHTML = "";
		for(i = 0; i < aPlaMon.length - 1; i=i+2)
		{			
			// Si coincide el tipo de crédito
			if (aPlaMon[i] == Moneda)
			{				
				// Agregamos la clave y descripción del producto
				add( new Option( ((aPlaMon[i+1])/12) + " AÑOS",aPlaMon[i+1])) 
			}
		}		
	}	
	
	// Inicializamos combos
	/*if (TipCre == "TRADIC")
	{	CboTic.value = "UDIPES"	}
	else if (TipCre == "INFONA")
	{	CboTic.value = "INFCOF"	}
	else if (TipCre == "FOVISS")
	{	CboTic.value = "FOVALI"	}*/
	CboPla.selectedIndex =0;

	
}

function ObtenTicPro(TipCre)
{
	// Obtenemos tipo de producto..
	var CboTic = eval(forma.name + ".Produc");
	var aProduc	= forma.ProTicUsu.value.split("|")
	
	// Para el combo de tipos de crédito
	with(CboTic.options)	
	{				
		// Limpiamos el combor
		innerHTML = "";
		for(i = 0; i < aProduc.length - 1; i=i+4)
		{			
			// Si coincide el tipo de crédito
			if (aProduc[i] == TipCre)
			{				
				// Agregamos la clave y descripción del producto
				add( new Option( aProduc[i+3],aProduc[i+2])) 
			}
		}		
	}	
	

	
	// Inicializamos combos
	if (TipCre == "TRADIC")
	{	
		if (forma.Usuari.value != "FONDEG")
		{	CboTic.value = "UDIPES"		}		
	}
	else if (TipCre == "INFONA")
	{	CboTic.value = "INFCOF"	}
	else if (TipCre == "FOVISS")
	{	CboTic.value = "FOVALI"	}
	ValoresPro();
	SelPro(CboTic.value);
	//CboTic.selectedIndex =-1;
	
}

function ValoresPro()
{		

	var CboTic = eval(forma.name + ".TiCrSi");
	var TipPro = "";
	var oTR_GanSAD = document.getElementById("TR_GanSAD")
	//alert(forma.Usuari.value);
//	return 0;

	/*alert(forma.TiCrSi.value);
	alert(forma.Produc.value);*/
	
	///
	var xmldom, strURL;
	xmldom = new ActiveXObject("microsoft.xmldom");
	with( document.location )	
	  strURL = protocol + "//" + hostname + forma.RutApl.value + "/Genera_XML.asp"
	  strURL += "?CvePro=RanTic&Param='" + forma.Usuari.value  + "','" + forma.TiCrSi.value + "','" + forma.Produc.value + "'"
	xmldom.async = false;	
	

	//alert(strURL);
/*		alert(xmldom.load(strURL));
	return 0;*/

	
	if( xmldom.load(strURL) )
	{
		
		var strHTML ="";							
		var i = 0 
		var xmldom, root;
		
		// Inicializa variables
		root = xmldom.documentElement;									
		
/*		var aProTic
		var CvePro= ""; DesPro = ""
		//var CveTic= ""; DesTic = ""
		var EstBot= ""; LeyBot = ""; var DesTic = "";*/

			
		// Ciclo mientras tipos de crédito y productos
		for( i = 0; i < root.childNodes.length; i++ )
		{															
			// Lee elementos
			element = root.childNodes(i); 	

			// Asignamos valores  de rangos y valores del producto
			forma.RanValPro.value = element.childNodes(0).text
			
			// Asignamos valor al arreglo
			aRanVal = element.childNodes(0).text.split("|")
			
		//	alert(aRanVal.length)
			//VaMivi
			
			//alert(forma.RanValPro.value);
		}				
	}	
	
	if (forma.Produc.value == "INFCOF" || 
		forma.Produc.value == "INFCIA" ||
		forma.Produc.value == "INFGCO" ||
		forma.Produc.value == "FOVALI" || 
		forma.Produc.value == "FOVRES" )
	{	MonPlaCofina()	}
}

function ValidaIngSub()
{

	// Si es subsidio tradicional
	if(forma.Produc.value != "SUBSID" && forma.Produc.value != "FOALSU" && forma.Produc.value != "INFCSU")
	{	return 0;	}
	
	
		
	var IngCon = forma.SubCon.value;
	var IngTot;
	var TopSam;
	var ValSMM = DN(forma.SaMiMe.value)	
	
	var aParSub = forma.ParSub.value.split("|")

	// Si es subsidio tradicional
	if(forma.Produc.value == "SUBSID")
	{
		// Si es ingreso conyugal
		if (IngCon == "S")
		{
			// Consideramos como ingresos los del acreditado y conyuge
			IngTot = DN(forma.IngAcrSub.value) +  DN(forma.IngConSub.value)
			// Calculamos tope de ingesos
			TopSam = DN(aParSub[1]) * ValSMM
			
			// Validamos los ingresos del acreditado
			if (DN(forma.IngAcrSub.value) == 0)
			{
				alert("El valor de los los ingresos del acreditado es obligatorio no puede ser cero.");
				return 0;
			}
			
			// Validamos los ingresos del acreditado
			if (DN(forma.IngConSub.value) == 0)
			{
				alert("El valor de los los ingresos del conyuge es obligatorio no puede ser cero.");
				return 0;
			}
			
			// Validamos los ingresos diferente de ceros
			if (IngTot == 0)
			{
				alert("El valor de los los ingresos conyugales es obligatorio no pueden ser cero, ya que los ingresos son un factor determinante parte calcular el monto de crédito que se puede financiar.");
				return 0;
			}
			
			// Si los ingresos totales superan al tope de ingresos
			if (IngTot > TopSam)
			{
				alert("Los ingresos conyugales por " + formatonum(IngTot,2) + " pesos superan el tope establecido de " + formatonum(TopSam,2) + " pesos para ser candidato(a) de este producto");
				forma.IngConSub.value = formatonum((TopSam - DN(forma.IngAcrSub.value)),2);
				if (DN(forma.IngConSub.value) < 0)			
				{	
					//alert(forma.IngConSub.value);
					forma.IngConSub.value	= 0.00	
					forma.IngAcrSub.value	= DN(aParSub[0]) * ValSMM
				}
				return 0
			}
	
		}
		else
		{
			
			// Consideramos solamente los ingresos del acreditado
			IngTot = DN(forma.IngAcrSub.value)
			// Calculamos tope de ingesos
			TopSam = DN(aParSub[0]) * ValSMM
			
			// Validamos los ingresos diferente de ceros
			if (IngTot == 0)
			{
				alert("El valor de los ingresos es obligatorio no puede ser cero, ya que los ingresos son un factor determinante parte calcular el monto de crédito que se puede financiar.");
				return 0;
			}
		
			
			// Si los ingresos totales superan al tope de ingresos
			if (IngTot > TopSam)
			{
				alert("Los ingresos por " + formatonum(IngTot,2) + " pesos superan el tope establecido de " + formatonum(TopSam,2) + " pesos para ser candidato(a) de este producto");
				forma.IngAcrSub.value = formatonum(TopSam,2);
				return 0
			}		
		}
		forma.SHF_IngCom.value = formatonum(IngTot,2)
	}
	
	// Si es subsidio con cofinanciamiento
	if(forma.Produc.value == "FOALSU" || forma.Produc.value == "INFCSU")
	{	
		
		//return 0;
		var IngAcr = 0;
		var IngCoa = 0;			
			
		if(forma.Produc.value == "FOALSU")
		{	
			// Obtenemos si es crédito conyugal
			IngCon = forma.FovCon.value
			// Ingresos del acreditado
			IngAcr= DN(forma.CFIngr.value)
			// Ingresos del conyuge
			IngCoa= DN(forma.CFCOIn.value)										
		}
		
		if(forma.Produc.value == "INFCSU")
		{	
			// Obtenemos si es crédito conyugal
			IngCon = forma.CofCon.value
			// Ingresos del acreditado
			IngAcr= DN(forma.Ingres.value)
			// Ingresos del conyuge
			IngCoa= DN(forma.IngCon.value)										
		}
			
		// Si es ingreso conyugal
		if (IngCon == "S")
		{
			// Consideramos como ingresos los del acreditado y conyuge
			IngTot = DN(IngAcr) +  DN(IngCoa)
			// Calculamos tope de ingesos
			TopSam = DN(aParSub[1]) * ValSMM
			
			// Validamos los ingresos del acreditado
			if (DN(IngAcr) == 0)
			{
				alert("El valor de los los ingresos del acreditado es obligatorio no puede ser cero.");
				return 0;
			}
			
			// Validamos los ingresos del acreditado
			/*if (DN(IngCoa) == 0)
			{
				alert("El valor de los los ingresos del conyuge es obligatorio no puede ser cero.");
				return 0;
			}*/
			
			// Validamos los ingresos diferente de ceros
			if (IngTot == 0)
			{
				alert("El valor de los los ingresos conyugales es obligatorio no pueden ser cero, ya que los ingresos son un factor determinante parte calcular el monto de crédito que se puede financiar.");
				return 0;
			}
			
			// Si los ingresos totales superan al tope de ingresos
			if (IngTot > TopSam)
			{
				alert("Los ingresos conyugales por " + formatonum(IngTot,2) + " pesos superan el tope establecido de " + formatonum(TopSam,2) + " pesos para ser candidato(a) de este producto");
				
				if(forma.Produc.value == "FOALSU")
				{
					forma.CFCOIn.value = formatonum((TopSam - DN(IngAcr)),2);
					if (DN(forma.CFCOIn.value) < 0)	{	forma.CFCOIn.value	= 0.00	;	forma.CFIngr.value	= DN(aParSub[0]) * ValSMM		}
				}
				if(forma.Produc.value == "INFCSU")
				{
					forma.IngCon.value = formatonum((TopSam - DN(IngAcr)),2);
					if (DN(forma.IngCon.value) < 0)	{	forma.IngCon.value	= 0.00	;	forma.Ingres.value	= DN(aParSub[0]) * ValSMM		}
				}
				return 0
			}
	
		}
		else
		{

			// Ingresos del conyuge
			if(forma.Produc.value == "FOALSU")
			{	IngAcr= DN(forma.CFIngr.value)	}
		
			if(forma.Produc.value == "INFCSU")
			{	IngAcr= DN(forma.Ingres.value)	}			
			
			// Consideramos solamente los ingresos del acreditado
			IngTot = DN(IngAcr) 
			// Calculamos tope de ingesos
			TopSam = DN(aParSub[0]) * ValSMM						
			
			// Validamos los ingresos diferente de ceros
			if (IngTot == 0)
			{
				alert("El valor de los ingresos es obligatorio no puede ser cero, ya que los ingresos son un factor determinante parte calcular el monto de crédito que se puede financiar.");
				return 0;
			}
		
			
			// Si los ingresos totales superan al tope de ingresos
			if (IngTot > TopSam)
			{
				alert("Los ingresos por " + formatonum(IngTot,2) + " pesos superan el tope establecido de " + formatonum(TopSam,2) + " pesos para ser candidato(a) de este producto");
				
				if(forma.Produc.value == "FOALSU")	{	forma.CFIngr.value = formatonum(TopSam,2);	}
				if(forma.Produc.value == "INFCSU")	{	forma.Ingres.value = formatonum(TopSam,2);	}
				return 0
			}		
		}
	}
	
	
	return 1;
}


// Funciones que regresan valores maximos y rangos
function PorMaxCre()
{	return aRanVal[4]	}

function ValMinViv()
{	return aRanVal[0]	}

function ValMaxViv()
{	
	var DenVal = aRanVal[3]
	var ValDen = 1;	
	
	// Dependiendo de la denominación
	if (DenVal == "UDI")
	{	ValDen = DN(forma.ValUdi.value)	}
	if (DenVal == "SAM")
	{	ValDen = DN(forma.SaMiMe.value)	}
	
	// Multiplicamos por la denominación
	return (aRanVal[2] * ValDen);
	//return formatonum((aRanVal[0] * ValDen),2);
}

function ValMinViv()
{	
	var DenVal = aRanVal[1]
	var ValDen = 1;	
	
	// Dependiendo de la denominación
	if (DenVal == "UDI")
	{	ValDen = DN(forma.ValUdi.value)	}
	if (DenVal == "SAM")
	{	ValDen = DN(forma.SaMiMe.value)	}
	
	// Multiplicamos por la denominación
	return (aRanVal[0] * ValDen);
}

function ValMaxCre()
{	
	var DenVal = aRanVal[8]
	var ValDen = 1;	
	
	// Dependiendo de la denominación
	if (DenVal == "UDI")
	{	ValDen = DN(forma.ValUdi.value)	}
	if (DenVal == "SAM")
	{	ValDen = DN(forma.SaMiMe.value)	}
	
	// Multiplicamos por la denominación
	return (aRanVal[7] * ValDen);
	//return formatonum((aRanVal[0] * ValDen),2);
}

function ValMinCre()
{	
	var DenVal = aRanVal[6]
	var ValDen = 1;	
	
	// Dependiendo de la denominación
	if (DenVal == "UDI")
	{	ValDen = DN(forma.ValUdi.value)	}
	if (DenVal == "SAM")
	{	ValDen = DN(forma.SaMiMe.value)	}
	
	// Multiplicamos por la denominación
	return (aRanVal[5] * ValDen);
}

function CofinaSinCon(TipCof)
{		
	if (TipCof == "INF")
	{
		var oTR_CofInfSC = document.getElementById("TR_CofInfSC");
		var oTR_LeySCInf = document.getElementById("TR_LeySCInf");
		
		if (oTR_CofInfSC.style.display == "none")
		{
			oTR_CofInfSC.style.display = ""
			oTR_LeySCInf.style.display = ""
		}
		else
		{
			oTR_CofInfSC.style.display = "none"			
			oTR_LeySCInf.style.display = "none"
		}			
	}
		var oTR_AjAcCF = document.getElementById("TR_AjAcCF");
		var oTR_AjAcSV = document.getElementById("TR_AjAcSV");		
		var oTR_AjCoCF = document.getElementById("TR_AjCoCF");
		var oTR_AjCoSV = document.getElementById("TR_AjCoSV");		
		var oLEY_SinConFov = document.getElementById("LEY_SinConFov");
	
	if (TipCof == "FOV")
	{		
		
		if (oTR_AjAcCF.style.display == "none")
		{
			oTR_AjAcCF.style.display = ""
			oTR_AjAcSV.style.display = ""
			oLEY_SinConFov.style.display = ""			
		}
		else
		{
			oTR_AjAcCF.style.display = "none"			
			oTR_AjAcSV.style.display = "none"
			if (oTR_AjCoCF.style.display == "none")
			{	oLEY_SinConFov.style.display = "none"	}
			forma.AjAcCF.value = "0.00";
			forma.AjAcSV.value = "0.00";			
		}			
	}
	
	if (TipCof == "FOVC")
	{		
	
		
		if (oTR_AjCoCF.style.display == "none")
		{
			oTR_AjCoCF.style.display = ""
			oTR_AjCoSV.style.display = ""
			oLEY_SinConFov.style.display = ""			
		}
		else
		{
			oTR_AjCoCF.style.display = "none"			
			oTR_AjCoSV.style.display = "none"

			if (oTR_AjAcCF.style.display == "none")
			{	oLEY_SinConFov.style.display = "none"	}
			forma.AjCoCF.value = "0.00";
			forma.AjCoSV.value = "0.00";			
		}			
	}	
	
//	alert("embn");
}