$(document).ready(function(){

	SeleccionarDestino($("#ddlDestino"));
	
	$("#txtValorVivienda").regexMask(/^\d+$/);
	$("#txtValorTerreno").regexMask("integer");
	$("#txtValorObraTotal").regexMask("integer");
	$("#txtAvanceDeObra").regexMask("integer");
	$("#txtPlazo").regexMask("integer");
	$("#txtIngresosNetos").regexMask("integer");
	$("#txtMonto").regexMask("integer");
	$("#txtCuota").regexMask("integer");
	
	if ($("#radioObraIniciadaNO").is(':checked'))
		ObraIniciada(false);
	else
		ObraIniciada(true);

	$("#txtMonto").attr("readonly", true);
	$("#txtMonto").css("background-color", "#F2F2F2");
	
	$("#txtCuota").attr("readonly", true);
	$("#txtCuota").css("background-color", "#F2F2F2");

	if ($("#opcMonto").is(':checked'))
	{
		$("#txtMonto").attr("readonly", false);
		$("#txtMonto").css("background-color", "#FFFFFF");
	}
	else if ($("#OpcCuotaMaxima").is(':checked'))
	{
		$("#txtCuota").attr("readonly", false);
		$("#txtCuota").css("background-color", "#FFFFFF");
	}
	
	$("#tablaDatosCreditoDestinoConstruccion").hide();
	$("#tablaDatosCreditoDestinoConstruccionDetalle th:nth-child(2)").hide();
	$("#tablaDatosCreditoDestinoConstruccionDetalle td:nth-child(2)").hide();
});

function SeleccionarDestino(objDestino)
{
	var idDestino = $(objDestino).val();
	if (idDestino == 22 || idDestino == 25 || idDestino == 19 || idDestino == 29)
	{
		$("#trValorPropiedad").hide();
		$("#trZonaTerreno").show();
		$("#trValorTerreno").show();
		$("#trValorObra").show();
		$("#trObraIniciada").hide();
	}
	else
	{
		$("#trValorPropiedad").show();
		$("#trZonaTerreno").hide();
		$("#trValorTerreno").hide();
		$("#trValorObra").hide();
		$("#trObraIniciada").hide();
	}
}

function ObraIniciada ( obraIniciada )
{
	if (obraIniciada)
		$("#txtAvanceDeObra").attr("readonly", false);
	else
	{
		$("#txtAvanceDeObra").val("0");
		$("#txtAvanceDeObra").attr("readonly", true);
	}
}


function SetModoCalculo(idBoton)
{

	switch (idBoton)
	{
		case "BtnMonto": 
		{
			$("#txtMonto").css("background-color", "#FFFFFF");
			$("#txtMonto").attr("readonly", false);
			$("#txtMonto").val("");
			$("#txtMonto").focus();

			$("#txtCuota").css("background-color", "#F2F2F2");
			$("#txtCuota").attr("readonly", true);
			$("#txtCuota").val("");
			
			$("#tdMontoCalculadoConIngresos").attr("innerText", "");

			break;
		}
					
		case "BtnCuotaMaxima": 
		{				
			$("#txtMonto").css("background-color", "#F2F2F2");
			$("#txtMonto").attr("readonly", true);
			$("#txtMonto").val("");
			
			$("#txtCuota").css("background-color", "#FFFFFF");
			$("#txtCuota").attr("readonly", false);
			$("#txtCuota").val("");
			$("#txtCuota").focus();

			$("#tdMontoCalculadoConIngresos").attr("innerText", "");

			break;
		}
		
		case "BtnIngresosNetos":
		{
			$("#txtMonto").css("background-color", "#F2F2F2");
			$("#txtMonto").attr("readonly", true);
			$("#txtMonto").val("");
			
			$("#txtCuota").css("background-color", "#F2F2F2");
			$("#txtCuota").attr("readonly", true);
			$("#txtCuota").val("");

			$("#tdMontoCalculadoConIngresos").attr("innerText", "");

			break;
		}
	}
}





























function init()
{								
	if (!isDOM1)
		return;
	
	if (getObj("opcMonto").checked)
		modoCalculo('BtnMonto');
	
	if (getObj("OpcCuotaMaxima").checked)
		modoCalculo('BtnCuotaMaxima');
	
	if (getObj("opcIngresosNetos").checked)
		modoCalculo('BtnIngresosNetos');
	
	
	
	var txtLastFocus = getObj("txtLastFocus");
	if (txtLastFocus.value != "")
	{		
		var txt = getObj(txtLastFocus.value);
		if (txt.style.visibility !='hidden')
		{
			if (txt.disabled == false)
				txt.focus();
		}
	}

	
	
	var txtPosPagina = getObj("txtPosPagina");
	if (txtPosPagina.value != "")	
		document.location.href = "#" + txtPosPagina.value ;
	
}



/*function simularKeyPress()
{		
	if (!isDOM1)
		return;

	getObj("txtLastFocus").value = event.srcElement.id;	
	
	if ((event.keyCode < 48 || event.keyCode > 57) && event.keyCode != 13)
		event.keyCode = 0;
		
	
	if (event.keyCode == 13)
		getObj("BtnSimular").focus();
}*/
			
function modoCalculo(idBoton)
{
	if (!isDOM1)
		return;

	var txtCuota = getObj("txtCuota");
	var txtMonto = getObj("txtMonto");	
	var lblMontoCalculadoConIngresos = getObj("lblMontoCalculadoConIngresos");
	
	
	
	txtCuota.disabled = true;
	txtCuota.style.backgroundColor='eeeeee';
	
	txtMonto.disabled = true;
	txtMonto.style.backgroundColor='eeeeee';
	
	
	
	switch (idBoton)
	{
		case "BtnMonto": 
		{			
			txtCuota.innerText = "";
			txtMonto.disabled = false;				
			txtMonto.style.backgroundColor='#FFF4CA';						
			lblMontoCalculadoConIngresos.innerText = "";
			txtMonto.focus();			
			break;
		}
					
		case "BtnCuotaMaxima": 
		{				
			txtMonto.innerText = "";
			txtCuota.disabled = false;				
			txtCuota.style.backgroundColor='#FFF4CA';						
			lblMontoCalculadoConIngresos.innerText = "";
			txtCuota.focus();			
			break;
		}
		
		
		
	}
}

	



