if(typeof console != 'object') {
  function console(){};
  function log(txt){}
  console.log=log;
}

function showError(where,text){
  jQuery(""+where+"").show("").html(text);
}
function hideError(where){
  jQuery(""+where+"").hide("fast");
}

function checkSimulators(){
  var porc1,porc2,porc3,sumPor,type;


  totalImporte=parseInt(jQuery("#totalImporte").val().replace(/\./g,""));
  if (isNaN(totalImporte)){
    showError("div.importe.error","No se ha definido ning&uacute;n  valor en <strong>\"Importe Total a financiar\"</strong>");
    return false;
  }

  sumaParciales=0;
  ip1=parseInt(jQuery("#importeparcial_1").val().replace(/\./g,""));
  if (!isNaN(ip1)){
    sumaParciales=ip1;
  }
  if (numSim>1){
    ip2=parseInt(jQuery("#importeparcial_2").val().replace(/\./g,""));
    if (!isNaN(ip2)){
      sumaParciales=parseInt(sumaParciales)+ip2;
    }
  }
  if (numSim>2){
    ip3=parseInt(jQuery("#importeparcial_3").val().replace(/\./g,""));
    if (!isNaN(ip3)){
      sumaParciales=parseInt(sumaParciales)+ip3;
    }
  }

  if ((sumaParciales!=totalImporte) && (numSim>1)){
      showError("div.importe.error","La suma de los importes parciales, deben sumar " + jQuery("#totalImporte").val() + ". Ahora suman" + sumaParciales);
      return false;
  }

  if (numSim==1){
    jQuery("#importeparcial_1").val(totalImporte);
  }

  //Ahora chequeamos cada simulador unitariamente
  //Primer simulador
  jQuery("div.importe.error").hide();
  checkSimulator(1,ip1);
  if (numSim>1){checkSimulator(2,ip2);}
  if (numSim>2){checkSimulator(3,ip3);}
}

function checkSimulator(identif){
  type=parseInt(jQuery("#type_1").val());
  switch (type){
    case 0://Hipotecario
      if (calculo_prestamo(identif)){
        hideError("p.error.identif_"+identif);
      }
      break;
    case 1://Personal
      if (calculo_prestamomes(identif)){
        hideError("p.error.identif_"+identif);
      }
      break;
    case 2://Leasing
      if(calculo_leasing(identif)){
        hideError("p.error.identif_"+identif);
      }
      break;
    default:
      console.log("ERROR. "+type);
      break;
  }
}

function calculo_prestamo(identif) {
  datos=recogerDatos(identif);
  if (!datos){
    return false;
  }

  if (datos.importe>datos.importe_max) {
    showError("p.error.identif_"+identif,"El importe del prestamo no puede ser mayor de " + importe_max + " Euros.");
    return false;
  }
  
  if (datos.importe<datos.importe_min) {
    showError("p.error.identif_"+identif,"El importe del prestamo no puede ser menor de " + importe_min + " Euros.");
    return false;
  }
  
  if (datos.interes<datos.interes_min){
    showError("p.error.identif_"+identif,"El inter&eacute;s tiene que ser mayor o igual que " + datos.interes_min + "");
    return;  
  }
  
  if (datos.interes>datos.interes_max){
	showError("p.error.identif_"+identif,"El inter&eacute;s tiene que ser menor o igual que " + datos.interes_max + "");
	return;  
  }
   
  if (datos.periodo>datos.periodo_max) {
    showError("p.error.identif_"+identif,"El Plazo de A&ntilde;os no puede ser mayor de " + periodo_max + " A&ntilde;os.");
    return false;
  }
  if (datos.periodo<datos.periodo_min) {
    showError("p.error.identif_"+identif,"El Plazo de A&ntilde;os no puede ser menor de " + periodo_min + " A&ntilde;os.");
    return false;
  }
  if (datos.interes==0) {
    cuota=(datos.importe/(datos.periodo*datos.cuotas));
    cuota=cuota.toFixed(2);
  } else {
     datos.interes=((datos.interes/100)/12);
     cuota =   (datos.importe*datos.interes)/(1-(Math.pow((1+datos.interes),-(datos.periodo*datos.cuotas))));
     cuota=cuota.toFixed(2);
  }
  jQuery("#cuota_"+identif).val(cuota);
  //jQuery("#cuota_"+identif).formatCurrency({dropDecimals:false,region: 'es-ES',symbol: '',digitGroupSymbol:'.',decimalSymbol:',',groupDigits:'true'});
  return true;
 }

function calculo_prestamomes(identif) {
  datos=recogerDatos(identif);
  if (!datos){
    return false;
  }
  if (datos.importe>datos.importe_max) {
    showError("p.error.identif_"+identif,"El importe del prestamo no puede ser mayor de " + importe_max + " Euros.");
    return false;
  }
  if (datos.importe<datos.importe_min) {
    showError("p.error.identif_"+identif,"El importe del prestamo no puede ser menor de " + importe_min + " Euros.");
    return false;
  }
  
  if (datos.interes<datos.interes_min){
    showError("p.error.identif_"+identif,"El inter&eacute;s tiene que ser mayor o igual que " + datos.interes_min + "");
    return;  
  }
	  
  if (datos.interes>datos.interes_max){
    showError("p.error.identif_"+identif,"El inter&eacute;s tiene que ser menor o igual que " + datos.interes_max + "");
    return;  
  }
  
  if (datos.periodo>datos.periodo_max) {
    showError("p.error.identif_"+identif,"El Plazo de meses no puede ser mayor de " + periodo_max + " meses");
    return false;
  }
  if (datos.periodo<datos.periodo_min) {
    showError("p.error.identif_"+identif,"El Plazo de meses no puede ser menor de " + periodo_min + " meses");
    return false;
  }
  if (datos.interes==0) {
    cuota=(datos.importe/(datos.periodo*datos.cuotas));
  } else {
    datos.interes=((datos.interes/100)/12);
    cuota = (datos.importe*datos.interes)/(1-(Math.pow((1+datos.interes),-(datos.periodo*datos.cuotas))));
  }
  jQuery("#cuota_"+identif).val(cuota.toFixed(2));
  return true;
}

function calculo_leasing(identif) {
  datos=recogerDatos(identif);
  if (!datos){
    return false;
  }
  
  if (datos.importe>datos.importe_max) {
    showError("p.error.identif_"+identif,"El importe del Leasing no puede ser mayor de " + datos.importe_max + " Euros.");
    return;
  }
  if (datos.importe<datos.importe_min) {
    showError("p.error.identif_"+identif,"El importe del Leasing no puede ser menor de " + datos.importe_min + " Euros.");
    return;
  }

  if (datos.interes<datos.interes_min){
    showError("p.error.identif_"+identif,"El inter&eacute;s tiene que ser mayor o igual que " + datos.interes_min + "");
    return;   
  }
		  
  if (datos.interes>datos.interes_max){
    showError("p.error.identif_"+identif,"El inter&eacute;s tiene que ser menor o igual que " + datos.interes_max + "");
    return;  
  }  
  
  if (datos.periodo>datos.periodo_max) {
        showError("p.error.identif_"+identif,"El Plazo de Cuotas no puede ser mayor de " + datos.periodo_max + " A&ntilde;os.");
        return;
  }
  if (datos.periodo<datos.periodo_min) {
     showError("p.error.identif_"+identif,"El Plazo de Cuotas no puede ser menor de " + periodo_min + " A&ntilde;os." + periodo);
     return;
  }

  datos.interes=((datos.interes/100)/datos.cuotas);
  cuota = (datos.importe*datos.interes)/((1-(Math.pow((1+datos.interes),-((datos.periodo*datos.cuotas)+datos.interes))))*(1+datos.interes));
  jQuery("#cuota_"+identif).val(cuota.toFixed(2));

  return true;
}


function recogerDatos(identif){
  var importe, interes, cuotas, periodo;

  cuotas=new Number(parseInt(jQuery("#cuotas_"+identif).val()));
  if (isNaN(cuotas)){
    cuotas=12;
  }
  importe=jQuery("#importeparcial_"+identif).val().replace(/\./g,"");;
  if (isNaN(importe)){
    showError("p.error.identif_"+identif,"Defina un importe parcial v&aacute;lido");
    return false;
  }

  periodo=new Number(jQuery("#years_"+identif).val());
  if (isNaN(periodo)){
    showError("p.error.identif_"+identif,"Defina un periodo v&aacute;lido");
    return false;
  }
  interes=parseFloat(jQuery("#interes_"+identif).val().replace(",","."));
  if (isNaN(interes)){
    showError("p.error.identif_"+identif,"Defina un inter&eacute;s v&aacute;lido");
    return false;
  }
  
  interes_min=parseFloat(jQuery("#interes_min_"+identif).val().replace(",","."));
  if (isNaN(interes_min)){ interes_min=0; }
  interes_max=parseFloat(jQuery("#interes_max_"+identif).val().replace(",","."));
  if (isNaN(interes_max)){ interes_max=0; }
  
  importe_max=new Number(jQuery("#importmax_"+identif).val().replace(".","").replace(",","."));
  if (isNaN(importe_max)){
    showError("p.error.identif_"+identif,"Defina un importe m&aacute;ximo v&aacute;lido: "+importe_max);
    return false;
  }

  importe_min=new Number(jQuery("#importmin_"+identif).val().replace(".","").replace(",","."));
  if (isNaN(importe_min)){
    showError("p.error.identif_"+identif,"ERROR. Defina un importe m&iacute;nimo v&aacute;lido: "+importe_min);
    return false;
  }

  periodo_max=new Number(jQuery("#pmax_"+identif).val().replace(".","").replace(",","."));
  if (isNaN(periodo_max)){
    showError("p.error.identif_"+identif,"Defina un periodo m&aacute;ximo v&aacute;lido: "+periodo_max);
    return false;
  }

  periodo_min=new Number(jQuery("#pmin_"+identif).val().replace(".","").replace(",","."));
  if (isNaN(periodo_max)){
    showError("p.error.identif_"+identif,"Defina un periodo m&iacute;nimo v&aacute;lido: "+periodo_min);
    return false;
  }
  return {"cuotas":cuotas,
          "importe":importe,
          "periodo":periodo,
          "interes":interes,
          "interes_min":interes_min,
          "interes_max":interes_max,
          "importe_max":importe_max,
          "importe_min":importe_min,
          "periodo_max":periodo_max,
          "periodo_min":periodo_min
         };
}