var smPrestitiFormManager={};

smPrestitiFormManager.formHeight=420;
smPrestitiFormManager.formWidth=600;
smPrestitiFormManager.waitOverlay=null;
smPrestitiFormManager.errorOverlay=null;
smPrestitiFormManager.checkCode=null;
smPrestitiFormManager.createWaitOverlay=function(){
  smPrestitiFormManager.waitOverlay=jQuery('<div id="divWaitOverlay" style="display:none;text-align:center;position:fixed;z-index:99999;top:0pt;left:0px;right:0pt;bottom:0pt;background-color:rgb(125,125,130);opacity:0.9;height:2555px;"><div style="top:250px;padding:6px 8px;"><p style="text-align:center;font-weight:bold;margin-bottom:8px;">Caricamento...</p><img alt="Caricamento" src="/images/loader.gif"></div></div>');
  jQuery("body").append(smPrestitiFormManager.waitOverlay);
}
smPrestitiFormManager.createErrorOverlay=function(){
  smPrestitiFormManager.errorOverlay=jQuery('<div id="divErrorOverlay" style="display:none;text-align:center;position:fixed;z-index:99999;top:0pt;left:0px;right:0pt;bottom:0pt;background-color:rgb(125,125,130);opacity:0.9;height:2555px;"><div style="top:250px;padding:4px 6px;"><p id="textError" style="">Per effettuare il confronto occorre inserire correttamente tutti i parametri richiesti (guarda i box in rosso per capire cosa ancora &egrave; mancante o non corretto).</p></div></div>');
  jQuery("body").append(smPrestitiFormManager.errorOverlay);
  smPrestitiFormManager.errorOverlay.click(function(){
    jQuery(this).hide();
  });
}
smPrestitiFormManager.addError=function(inputItem,errorMsg){         /* addError */
  inputItem.addClass("errorInputItem").attr("title",errorMsg).errorTipTip({maxWidth:"240px",defaultPosition:"right",edgeOffset:5});
}
smPrestitiFormManager.removeError=function(inputItem){               /* removeError */
  inputItem.removeClass("errorInputItem").removeAttr("title").unbind('mouseenter mouseleave');
  jQuery("#error_tiptip_holder").hide();
}
smPrestitiFormManager.initToolTips=function(){                         /* initToolTips */
  jQuery(".formHint").tipTip({maxWidth:"670px",defaultPosition:"right",ignoreTop:true,edgeOffset:10});
}
smPrestitiFormManager.impNoValid = ["Dipendente-pubblico-o-statale","Personale-delle-Forze-Armate","Pensionato-INPS","Pensionato-INPDAP-e-altri-Enti"];
smPrestitiFormManager.checkField=function(field) {                         /* checkField */
  var fFieldCheck=true;

  if (field.attr("id") == "edtImportoRichiesto"){
    var fieldValue = field.val().replace(/\./g,"");
    if (isNaN(fieldValue) || (fieldValue === "") || (fieldValue<1000) || (fieldValue>60000)) {
      smPrestitiFormManager.addError(field,"Inserisci un valore numerico tra 1.000 e 60.000");
      fFieldCheck=false;
    }
  }else if (field.attr("id") == "edtEtaRichiedente"){
    if (isNaN(field.val()) || (field.val() === "") || (field.val()<18) || (field.val()>90)) {
      smPrestitiFormManager.addError(field,"Inserisci un valore numerico tra 18 e 90");
      fFieldCheck=false;
    }
  }else if( field.attr("id") == "selDurataFinanziamento" || field.attr("id") == "selImpiegoRichiedente") {
    var $dur = jQuery("#selDurataFinanziamento").find("option:selected");
    var dur = parseInt($dur.val());
    var imp = jQuery("#selImpiegoRichiedente").find("option:selected").val();
    var impValid = smPrestitiFormManager.impNoValid.join().indexOf(imp) == -1;
    if(impValid && dur && dur>84) {
        jQuery("#ha_scelto_piu_di_7_anni").val("1");
        jQuery("#selDurataFinanziamento option").each(function() {
            if(this.value>84) this.value = "84";
        });
    }else {
        jQuery("#ha_scelto_piu_di_7_anni").val("0");
        jQuery("#selDurataFinanziamento option").each(function() {
            if(this.value==84) this.value = this.innerHTML.split(" ")[0]*12;
        });
    }

  }
  return fFieldCheck;
}
smPrestitiFormManager.initEditAndSelects=function(){                 /* initEditAndSelects */
  jQuery("#frmRicercaPrestito input,select").focusin(function(){
    smPrestitiFormManager.removeError(jQuery(this));
  }).focusout(function(){
    if (smPrestitiFormManager.checkField(jQuery(this))) {
      smPrestitiFormManager.removeError(jQuery(this));
    }
  });
}
smPrestitiFormManager.manage_short_form=function(){                         /* manage_short_form */

  var flag=true;
  var requested_amount = jQuery("#edtImportoRichiesto").val();
  var applicant_age = jQuery("#edtEtaRichiedente").val();

  var fieldValue = requested_amount.replace(/\./g,"");
  if (isNaN(fieldValue) || (fieldValue === "") || (fieldValue<1000) || (fieldValue>60000)) {
    smPrestitiFormManager.addError(jQuery("#edtImportoRichiesto"),"Inserisci un valore numerico tra 1.000 e 60.000");
    flag=false;
  }
  if (isNaN(applicant_age) || (applicant_age === "") || (applicant_age<18) || (applicant_age>90)) {
    smPrestitiFormManager.addError(jQuery("#edtEtaRichiedente"),"Inserisci un valore numerico tra 18 e 90");
    flag=false;
  }
  return flag;
}

smPrestitiFormManager.fillHiddenForm=function(map){                         /* fillHiddenForm */
  jQuery.each(map,function(key,val){
    jQuery('input[name="'+key+'"]').val(val);
  });
}
smPrestitiFormManager.initForm=function(){                         /* initForm */
  // -- auto numeric --
  jQuery("#edtImportoRichiesto").autoNumeric({
    aNum: '0123456789',
    aSep: '.',
    aDec: ',',
    aSign: '',
    pSign: 's',
    mNum: 7,
    mDec: 0,
    dGroup: 3,
    mRound: 'S',
    aPad: false
  });
  jQuery('#compare').click(function(){
    if(smPrestitiFormManager.manage_short_form()){
      jQuery(smPrestitiFormManager.waitOverlay).show();
      setTimeout('jQuery("#frmRicercaPrestito").submit()', 200);
    }else {
      jQuery(smPrestitiFormManager.errorOverlay).show();
    }
  });

}
smPrestitiFormManager.init=function(checkCode){                                 /* init */
  smPrestitiFormManager.checkCode=checkCode;
  smPrestitiFormManager.initToolTips();
  smPrestitiFormManager.createWaitOverlay();
  smPrestitiFormManager.createErrorOverlay();
  smPrestitiFormManager.initEditAndSelects();
  smPrestitiFormManager.initForm();
  window.onunload = function(){};
}

