function cambiar_modelo_subsidio(obj)
{
  var m = obj.form.modelo;
  var s = obj.form.subsidio;
  var mods = proyectos[obj[obj.selectedIndex].value]["modelos"];
  var subs = subsidios[obj[obj.selectedIndex].value];
  m.length = mods.length + 1;
  for(var i = 0; i < mods.length; i++) {
    m[i + 1].value = mods[i]["ID______MOD"];
    m[i + 1].text = mods[i]["MODELO__MOD"];
  }
  s.length = subs.length + 1;
  for(var i = 0; i < subs.length; i++) {
    s[i + 1].value = subs[i]["ID"];
    s[i + 1].text = subs[i]["TITULO"];
  }
}
function ponersubsidio(valor) {
 partes=valor.split(" ");

 if (valor==0) {
  subsidio=0;
  ahorro=0;
  document.getElementById("ahorro").innerHTML="&nbsp;";
  document.getElementById("pieuf").innerHTML="Monto del pie en UF ";
  document.getElementById("piepor").innerHTML="Monto del pie en % ";
 } else {
  subsidio=parseInt(partes[2]);
  ahorro=parseInt(partes[1]);
  document.getElementById("ahorro").innerHTML="Ahorro Obligatorio: <strong>"+partes[1]+" UF</strong>, &nbsp;Subsidio: <strong>"+partes[2]+" UF</strong>";
  document.getElementById("pieuf").innerHTML="Pie adicional subsidio en UF ";
  document.getElementById("piepor").innerHTML="Pie adicional subsidio en % ";
 }
}


/* R.U.T. Validation routines. Please call validaRut(rut,dv)
** Taken from the SII site. No copyright notice was found. */
function trimval(str){
	if(typeof str == 'undefined') {
		return str;
	}
	var ini=0;
	var fin=str.length-1;
	while(ini<=fin && str.charAt(ini)==" ") ini++;
	if (ini<=fin) {
		while(str.charAt(fin)==" ") fin--;
		if (fin<str.length-1) {
				fin++;
				return str.substring(ini,fin);
		} else return str.substr(ini);
	} else return '';
}

function isNumero(str)
{
	var flag=true;
	var i=0;
	var nums=new Array(1,1,1,1,1,1,1,1,1,1);
	while (i<str.length && flag) {
		flag= (nums[str.charAt(i++)]!=null);
	}
	return flag;
}

function isRut(strValue)
{
	if(typeof strValue == 'undefined') {
		return false;
	}
	if(strValue == '') {
		return false;
	}
	rutdv = strValue.split(/-/);
	if(typeof rutdv[1] == 'undefined') {
		return false;
	}
	rutdv[0] = rutdv[0].replace(/\./, '');
	return validaRut(rutdv[0], rutdv[1]);
}

function validaM11(rut,dv)
{
	var suma=0;
	var mul=2;
	var i=0;
	for (i=rut.length-1;i>=0;i--) {
		suma=suma+rut.charAt(i) * mul;
		mul= mul==7 ? 2 : mul+1;
	}
	var dvr = ''+(11 - suma % 11);
	if (dvr=='10') dvr = 'K';
	else if (dvr=='11') dvr = '0';
	if (dvr!=dv) return false;
	else return true;
}

var ruterror='';
function validaRut(rut,dv)
{
	if((typeof(rut) == 'undefined') || (typeof(dv) == 'undefined')) {
		return false;
	}
	rut=trimval(rut);
	rut=rut.replace(/\./g,'');
	dv=trimval(dv).toUpperCase();
	if (rut=='' && dv=='') {
		return true;
	}
	if (rut=='' || !isNumero(rut) || rut*1==0) {
		ruterror='Rut Inv\xe1lido';
		return false;
	}else if ((!isNumero(dv) && (dv != "K")) || (dv == "")) {
		ruterror='D\xedgito Verificador inv\xe1lido';
		return false;
	}else if (!validaM11(rut,dv)) {
		ruterror='D\xedgito Verificador Err\xf3neo';
		return(false);
	}
	return true;
}


function rut_formal( rut )
{
	if ( substr_count(rut,'.') > 0 )
		return rut;
	prut = rut.split('-');
	nrut = '';
	for (i=1;i<prut[0].length+1;i++)
	{
		if ( i%3 == 0 )
			nrut += '.';
    	nrut += prut[0].charAt(i-1); 
	}
	
	if ( typeof(prut[1]) == 'undefined' )
		return nrut;
	else	
		return nrut+'-'+prut[1];
	
}

function rut_informal(rut)
{
	nrut = '';
	
	for (i=0;i<rut.length;i++)
	{
	
		if ( rut.charAt(i) != '.' )
			nrut += rut.charAt(i);
    	
	}
	
	return nrut;
}

function substr_count(s,p)
{
	c = 0;
	for(i=0;i<s.length;i++)
	{
		if (s.charAt(i)==p)
			c++;
	}
	return c;
}




$(function(){
	$('#sliderGaleria2').anythingSlider({
		// Appearance
		width               : 618,      // Override the default CSS width
		height              : 267,      // Override the default CSS height
		resizeContents      : false,      // If true, solitary images/objects in the panel will expand to fit the viewport
		tooltipClass        : 'tooltip', // Class added to navigation & start/stop button (text copied to title if it is hidden by a negative text indent)
		theme               : 'default', // Theme name
		themeDirectory      : 'css/theme-{themeName}.css', // Theme directory & filename {themeName} is replaced by the theme value above

		// Navigation
		startPanel          : 1,         // This sets the initial panel
		hashTags            : true,      // Should links change the hashtag in the URL?
		enableKeyboard      : true,      // if false, keyboard arrow keys will not work for the current panel.
		buildArrows         : true,      // If true, builds the forwards and backwards buttons
		toggleArrows        : false,     // If true, side navigation arrows will slide out on hovering & hide @ other times
		buildNavigation     : true,      // If true, builds a list of anchor links to link to each panel
		enableNavigation    : true,      // if false, navigation links will still be visible, but not clickable.
		toggleControls      : true,     // if true, slide in controls (navigation + play/stop button) on hover and slide change, hide @ other times
		appendControlsTo    : null,      // A HTML element (jQuery Object, selector or HTMLNode) to which the controls will be appended if not null
		navigationFormatter : null,      // Details at the top of the file on this use (advanced use)
		forwardText         : "forwardText", // Link text used to move the slider forward (hidden by CSS, replaced with arrow image)
		backText            : "backText", // Link text used to move the slider back (hidden by CSS, replace with arrow image)

		// Slideshow options
		enablePlay          : false,      // if false, the play/stop button will still be visible, but not clickable.
		autoPlay            : false,      // This turns off the entire slideshow FUNCTIONALY, not just if it starts running or not
		startStopped        : false,     // If autoPlay is on, this can force it to start stopped
		pauseOnHover        : false,      // If true & the slideshow is active, the slideshow will pause on hover
		resumeOnVideoEnd    : false,      // If true & the slideshow is active & a youtube video is playing, it will pause the autoplay until the video is complete
		stopAtEnd           : false,     // If true & the slideshow is active, the slideshow will stop on the last page
		playRtl             : false,     // If true, the slideshow will move right-to-left
		startText           : "Start",   // Start button text
		stopText            : "Stop",    // Stop button text
		delay               : 3000,      // How long between slideshow transitions in AutoPlay mode (in milliseconds)
		animationTime       : 600,       // How long the slideshow transition takes (in milliseconds)
		easing              : "swing"
	});

});
	  function abrircotizador(modelo){
		$("#modelo-detalle").slideToggle(500);
		$("#cotizador").slideToggle(500);
		return false;
	  }

	  var subsidio=0;
	  var ahorro=0;
	  var consubsidios=false;

	  var pre=0;

	  function valida() {
	   if (!isRut(document.formcotiz.rut.value)){
		alert("por favor ingrese un rut valido");
		return false;
	   }
	   document.formcotiz.rut.value = rut_formal(document.formcotiz.rut.value);


	   if (document.formcotiz.rut.value=="" || document.formcotiz.nombres.value=="" ||document.formcotiz.paterno.value=="" ||document.formcotiz.materno.value=="" ||document.formcotiz.email.value=="") {
		alert("por favor ingrese todos los datos solicitados.");
		return false;
	   }
	   
	   if (document.formcotiz.plazo.selectedIndex<=0) {
		alert("Por favor indique el plazo del crédito hipotecario.");
		return false;  
	   } 
	  
	  
	   if (document.formcotiz.pie.selectedIndex>0 && document.formcotiz.pie_uf.value!="") {
		alert("Por favor indique el pie del crédito hipotecario en % o UF pero no ambos.");
		return false;  
	   } 
	  
	   if (document.formcotiz.pie_uf.vaue!="" && isNaN(document.formcotiz.pie_uf.value)) {
		alert("El pie en UF indicado no es válido. Por favor ingrese un número.");
		return false;  
	   } 
	  
	   if (document.formcotiz.pie_uf.vaue!="" && parseInt(document.formcotiz.pie_uf.value)+subsidio<58) {
		alert("El pie en UF indicado debe ser mayor o igual a 58.");
		return false;  
	   } 
	  
	   if (ahorro>0 && document.formcotiz.pie.selectedIndex>0 && parseFloat(document.formcotiz.pie[document.formcotiz.pie.selectedIndex].value)*1150<ahorro) {
		alert("El pie en % indicado debe ser mayor o igual al ahorro minimo requerido.");
		return false;  
	   } 
	  
	   if (ahorro>0 && document.formcotiz.pie.selectedIndex>0 && parseFloat(document.formcotiz.pie[document.formcotiz.pie.selectedIndex].value)*1150+subsidio<58) {
		alert("El pie en UF indicado más el subsidio debe ser mayor o igual a 58.");
		return false;  
	   } 
	  
	   if (ahorro>0 && document.formcotiz.pie_uf.vaue!="" && parseInt(document.formcotiz.pie_uf.value)<ahorro) {
		alert("El pie en UF indicado debe ser mayor o igual al ahorro mínimo requerido.");
		return false;  
	   } 
	  
	   if (document.formcotiz.pie_uf.vaue!="" && parseInt(document.formcotiz.pie_uf.value)+subsidio>575) {
		alert("El pie en UF indicado debe ser menor a 575.");
		return false;  
	   } 
	  
	  
	   if (pre==2) { // CON PRE EVALUACION
		   if (document.formcotiz.ingreso1.value=="" || isNaN(document.formcotiz.ingreso1.value)) {
			alert("Falta el Ingreso $ del titular para la Pre Evaluacion.");
			return false;  
		   } 
	  
		   if (document.formcotiz.empleador1.value=="") {
			alert("Ingrese el Empleador del titular.");
			return false;  
		   } 
	  
	  
		   if (document.formcotiz.ingreso2.value=="" || isNaN(document.formcotiz.ingreso2.value)) {
			alert("Falta el Ingreso $ del Codeudor para la Pre Evaluacion.");
			return false;  
		   } 
	  
	  
		   if (document.formcotiz.empleador2.value=="") {
			alert("Ingrese el Empleador del Codeudor.");
			return false;  
		   } 
	  
	  
	   }

	jQuery.post("/cotizar/resultado.php", $("#formcotiz").serialize(), function(data) {
		var r;
		if(r = data.match(/^<div class="alert">(.*)<\/div>$/)) {
			alert(r[1]);
			return;
		}
		$('#cotizador .contenido-cotizador').html(data);
	});
	   return false;
	  
           /*
	   if (document.formcotiz.autorizo.checked) autorizo=document.formcotiz.autorizo.value; else autorizo="";
 
	   n="v="+document.formcotiz.v.value+"&rut="+document.formcotiz.rut.value+"&nombres=" +document.formcotiz.nombres.value+"&paterno="+document.formcotiz.paterno.value+"&materno="+document.formcotiz.materno.value+"&email="+document.formcotiz.email.value+"&telefono="+document.formcotiz.telefono.value+"&comuna="+document.formcotiz.comuna[document.formcotiz.comuna.selectedIndex].text+"&plazo="+document.formcotiz.plazo[document.formcotiz.plazo.selectedIndex].value+"&pie_uf="+document.formcotiz.pie_uf.value+"&pie="+document.formcotiz.pie[document.formcotiz.pie.selectedIndex].value+"&autorizo="+autorizo;
	  
	   n=n+"&ingreso1="+document.formcotiz.ingreso1.value+"&empleador1="+document.formcotiz.empleador1.value+"&rut2="+document.formcotiz.rut2.value+"&ingreso2="+document.formcotiz.ingreso2.value+"&empleador2="+document.formcotiz.empleador2.value;
	  
	   if (consubsidios) n=n+"&subsidio="+document.formcotiz.subsidio[document.formcotiz.subsidio.selectedIndex].value;
	   window.parent.popup('cotizar/resultado.php?'+n,0,0,560,600,'http://www.socovesa.cl/');
	   */

// document.formcotiz.submit();
 
	}

