$(document).ready(function(){
	$("select").uniform();
	
	//Por menu
	$.post("dival_web.menu/menu_index_sis.php", { },
		function(data){
		$("div[id='div_menu_header_left']").html(data);
	});
	
	$.post("dival_web.menu/menu_index.php", { },
		function(data){
		$("div[id='div_menu_header']").html(data);
	});
	
	$.post("dival_web.resultados/resultado_index.php", { },
			function(data){
			$("div[id='div_principal_resultado']").html(data);
	});
		
	$("#cmbRegion").change(function(){
		//alert('cmbRegion');
		var id_region = $("select[name='cmbRegion']").val();
		
		$.post("php/cmb_comuna.php", { id_region: id_region },
		function(data){			
		$("select[name='cmbComuna']").html(data);			
		});
		
		$.post("php/cmb_proyecto.php", { id_region: id_region },
		function(data){
		$("select[name='cmbProyecto']").html(data);			
		});
	});
		
	$("#cmbComuna").change(function(){
		//alert('cmbComuna')	
		var id_comuna = $("select[name='cmbComuna']").val();
		var id_region = $("select[name='cmbRegion']").val();
		$.post("php/cmb_proyecto.php", { id_comuna: id_comuna,
										 id_region: id_region },
		function(data){
			$("select[name='cmbProyecto']").html(data);
			
		
		});
	
	});		
	

	
	
	$("#btnBuscar").click(function(){
		
		reg_id = $("select[name='cmbRegion']").val();
		com_id = $("select[name='cmbComuna']").val();
		pin_id = $("select[name='cmbProyecto']").val();
		uf_desde = $("select[name='cmbUFDesde']").val();
		uf_hasta = $("select[name='cmbUFHasta']").val();
		
		if (pin_id != '-1'){
			//BUSCAMOS PROYECTOS			
			if (parseFloat(uf_desde) > parseFloat(uf_hasta)){
				alert('La UF Desde no puede ser superior a la UF Hasta');
				return false;
				
			}
			//MOSTRAMOS LOS MODELOS DEL PROYECTO 
			var randomnumber=Math.floor(Math.random()*11); 
			$("div[id='div_principal_resultado']").html("<iframe name='MyIFrame' id='MyIFrame' src='' width='680' height='500px' frameborder='0' scrolling='yes' > </iframe>");
			
			var w = window.open('dival_web.resultados/resultado_modelo.php?id='+pin_id+'&uf_desde='+uf_desde+'&uf_hasta='+uf_hasta+'&'+randomnumber, 'MyIFrame');
			if (w && !w.closed) {
				// si efectivamente hemos logrado abrirla
				// la ponemos en foco
				w.focus();
				// y cancelamos el comportamiento por defecto
				// del enlace
				e.preventDefault();
			}
			
		}else{
			
			if (parseFloat(uf_desde) > parseFloat(uf_hasta)){
				alert('La UF Desde no puede ser superior a la UF Hasta');
				return false;
				
			}
			var randomnumber=Math.floor(Math.random()*11);
			$("div[id='div_principal_resultado']").html("<iframe name='MyIFrame' id='MyIFrame' src='' width='680' height='500px' frameborder='0' scrolling='yes' > </iframe>"); 
			var w = window.open('dival_web.resultados/resultado_proyecto.php?reg_id='+reg_id+'&com_id='+com_id+'&uf_desde='+uf_desde+'&uf_hasta='+uf_hasta+'&'+randomnumber, 'MyIFrame');
			if (w && !w.closed) {
				// si efectivamente hemos logrado abrirla
				// la ponemos en foco
				w.focus();
				// y cancelamos el comportamiento por defecto
				// del enlace
				e.preventDefault();
			}	
			
		}
		
    });
}); 
