/**---------------------------------------------------------------------
 * Page Contact Form Manager
 *----------------------------------------------------------------------
 * @author Marco Muracchioli <marco.muracchioli@supermoney.eu>
 * @version 1.0
 * @copy 2011, SuperMoney S.p.A.
 *--------------------------------------------------------------------*/

// Singleton
var smPageContact=smPageContact||{};
// Internal references
smPageContact.checkCode=null;
smPageContact.subSite=null;
smPageContact.productCode=null;
smPageContact.emailRegEx=/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
// Class functions
smPageContact.addError=function(inputItem,errorMsg){
  if (inputItem.attr("id")=='chkPrivacySingleCheck') {
    jQuery("#aSelectPrivacyOptions").addClass("errorCheckBoxItem").attr("title",errorMsg).errorTipTip({maxWidth:"240px",defaultPosition:"right",edgeOffset:0});
  } else {
    inputItem.addClass("errorInputItem").attr("title",errorMsg).errorTipTip({maxWidth:"240px",defaultPosition:"right",edgeOffset:0}).prev("label").addClass("errorLabelItem");
  }
}
smPageContact.removeError=function(inputItem){
  if (inputItem.attr("id")=='chkPrivacySingleCheck') {
    jQuery("#aSelectPrivacyOptions").removeClass("errorCheckBoxItem").removeAttr("title").unbind('mouseenter mouseleave');
    jQuery("#error_tiptip_holder").hide();
  } else {
    inputItem.removeClass("errorInputItem").removeAttr("title").unbind('mouseenter mouseleave').prev("label").removeClass("errorLabelItem");;
    jQuery("#error_tiptip_holder").hide();
  }
}
smPageContact.assignEvents=function() {
  jQuery("#aSelectPrivacyOptions").click(function(e) {
    e.preventDefault();
    smPageContact.showPrivacyShort();
  });
  jQuery("#chkPrivacySingleCheck").click(function() {
    smPageContact.removeError(jQuery("#chkPrivacySingleCheck"));
    if (jQuery(this).is(":checked")) {
      smPageContact.checkFullPrivacy();
    } else {
      smPageContact.uncheckFullPrivacy();
    }
  });
  jQuery("#chkContactPopupPrivacyFullAccepted").click(function() {
    smPageContact.removeError(jQuery("#chkPrivacySingleCheck"));
    if (jQuery(this).is(":checked")) {
      smPageContact.checkFullPrivacy();
    } else {
      jQuery("#chkContactPopupPrivacyFullNotAccepted").attr("checked","checked");
      smPageContact.uncheckFullPrivacy();
    }
  });
  jQuery("#chkSinglePrivacyAccepted_1,#chkSinglePrivacyAccepted_2,#chkSinglePrivacyAccepted_3,#chkSinglePrivacyAccepted_4,#chkSinglePrivacyAccepted_5").click(function() {
    smPageContact.removeError(jQuery("#chkPrivacySingleCheck"));
    if (jQuery("#chkSinglePrivacyAccepted_1").is(":checked") && jQuery("#chkSinglePrivacyAccepted_2").is(":checked") && jQuery("#chkSinglePrivacyAccepted_3").is(":checked")) {
      jQuery("#chkContactPopupPrivacyFullNotAccepted").removeAttr("checked");
      jQuery("#chkPrivacySingleCheck").attr("checked","checked");
    } else if (jQuery("#chkSinglePrivacyAccepted_1").is(":checked") || jQuery("#chkSinglePrivacyAccepted_2").is(":checked") || jQuery("#chkSinglePrivacyAccepted_3").is(":checked") || jQuery("#chkSinglePrivacyAccepted_4").is(":checked") || jQuery("#chkSinglePrivacyAccepted_5").is(":checked")) {
      jQuery("#chkContactPopupPrivacyFullNotAccepted").removeAttr("checked");
      jQuery("#chkPrivacySingleCheck").removeAttr("checked");
    } else {
      jQuery("#chkContactPopupPrivacyFullNotAccepted").attr("checked","checked");
    }
  });
  jQuery("#chkContactPopupPrivacyFullNotAccepted").click(function() {
    jQuery("#chkPrivacySingleCheck").removeAttr("checked");
    smPageContact.removeError(jQuery("#chkPrivacySingleCheck"));
    if (jQuery(this).is(":checked")) {
      smPageContact.uncheckFullPrivacy();
    }
  });
  jQuery("#privacyButtonBack").click(function() {
    smPageContact.showPersonalInfo();
  });
  jQuery("#personalDataButtonConfirm").click(function() {
    if (smPageContact.checkData()) {
      smPageContact.sendData();
    }
  });
  jQuery("#privacyButtonConfirm").click(function() {
    smPageContact.showPersonalInfo();
    if (smPageContact.checkData()) {
      smPageContact.sendData();
    }
  });
  /*
   * ENABLE THIS SECTION TO MAKE "CONTACT SENT" PAGE CLICKABLE
   * (ON CLICK, THE DIALOG IS RESTORED WITHOUT CLEARING DATA)
   *
  jQuery("#divContactSent").click(function(){
    jQuery("#divContactSent").hide();
    jQuery("#divContactOnPageFormArea").show();
    smPageContact.showPersonalInfo();
  });
  */
}
smPageContact.setErrorRemoving=function(){
  jQuery("#edtTelefonoContatto,#edtEMailContatto").focusin(function(){
    smPageContact.removeError(jQuery("#edtTelefonoContatto"));
    smPageContact.removeError(jQuery("#edtEMailContatto"));
  });
}
smPageContact.checkFullPrivacy=function() {
  smPageContact.hidePrivacyLong();
  smPageContact.checkAllPrivacyItems();
  jQuery("#chkContactPopupPrivacyFullAccepted").attr("checked","checked");
  jQuery("#chkContactPopupPrivacyFullNotAccepted").removeAttr("checked");
}
smPageContact.uncheckFullPrivacy=function() {
  smPageContact.showPrivacyLong();
  smPageContact.uncheckAllPrivacyItems();
  jQuery("#chkPrivacySingleCheck").removeAttr("checked");
  jQuery("#chkContactPopupPrivacyFullAccepted").removeAttr("checked");
  jQuery("#chkContactPopupPrivacyFullNotAccepted").attr("checked","checked");
}
smPageContact.checkAllPrivacyItems=function() {
  jQuery("#chkPrivacySingleCheck,#chkSinglePrivacyAccepted_1,#chkSinglePrivacyAccepted_2,#chkSinglePrivacyAccepted_3,#chkSinglePrivacyAccepted_4,#chkSinglePrivacyAccepted_5").attr("checked","checked");
}
smPageContact.uncheckAllPrivacyItems=function() {
  jQuery("#chkPrivacySingleCheck,#chkSinglePrivacyAccepted_1,#chkSinglePrivacyAccepted_2,#chkSinglePrivacyAccepted_3,#chkSinglePrivacyAccepted_4,#chkSinglePrivacyAccepted_5").removeAttr("checked");
}
smPageContact.showPersonalInfo=function() {
  jQuery("#divContactOnPage").addClass("contactOnPageBackgroundVisible");
  jQuery("#divContactOnPageFormPrivacyOptions").hide();
  jQuery("#divContactOnPageFormPersonalInformation").show();
}
smPageContact.showPrivacyShort=function() {
  jQuery("#divContactOnPage").removeClass("contactOnPageBackgroundVisible");
  jQuery("#divContactOnPageFormPersonalInformation").hide();
  jQuery("#divContactOnPageFormPrivacyOptions").show();
}
smPageContact.showPrivacyLong=function() {
  jQuery("#divContactOnPageFormPrivacySpecificOptions").show();
}
smPageContact.hidePrivacyLong=function() {
  jQuery("#divContactOnPageFormPrivacySpecificOptions").hide();
}
smPageContact.checkData=function() {
  var fFormCheck=true;
  var fEMailVal       = jQuery("#edtEMailContatto").val();
  var fEMail          = smPageContact.emailRegEx.test(fEMailVal);
  var fTelefono       = jQuery("#edtTelefonoContatto").val()!='';
  var fTelefonoEMail  = fTelefono || fEMail;
  var fPrivacy        = jQuery("#chkContactPopupPrivacyFullAccepted").is(":checked") ||
                        (jQuery("#chkSinglePrivacyAccepted_1").is(":checked") &&
                         jQuery("#chkSinglePrivacyAccepted_2").is(":checked") &&
                         jQuery("#chkSinglePrivacyAccepted_3").is(":checked"));
  if (!fTelefonoEMail) {
    smPageContact.addError(jQuery("#edtTelefonoContatto"), fETMancaNumeroTelefono);
    smPageContact.addError(jQuery("#edtEMailContatto"), fETMancaIndirizzoEMail);
    fFormCheck=false;
  } else {
    if (fEMailVal!='') {
      if (!fEMail) {
        smPageContact.addError(jQuery("#edtEMailContatto"), fETIndirizzoMailErrato);
        fFormCheck=false;
      }
    }
  }
  if (!fPrivacy) {
    smPageContact.addError(jQuery("#chkPrivacySingleCheck"), fETPrivacyNonAccettata);
    fFormCheck=false;
  }

  return fFormCheck;
}
smPageContact.sendData=function(){
  var siteAddr=null;
  switch (smPageContact.subSite) {
    case 'conto_corrente':
      siteAddr="/conto-corrente/save_contact.php";
      break;
    case 'conto_deposito':
      siteAddr="/conto-deposito/save_contact.php";
      break;
    case 'energia-elettricita':
      siteAddr="/save_contact.php";
      break;
    case 'energia-bifuel':
      siteAddr="/save_contact.php";
      break;
    case 'energia-gas':
      siteAddr="/save_contact.php";
      break;
    default:
      siteAddr="/save_contact.php";
  }
  jQuery("#personalDataButtonConfirm,#privacyButtonConfirm").attr("disabled","disabled");
  jQuery.post(siteAddr,{
    's'   : smPageContact.subSite,
    'n'   : jQuery("#edtNomeContatto").val(),
    'c'   : jQuery("#edtCognomeContatto").val(),
    't'   : jQuery("#edtTelefonoContatto").val(),
    'e'   : jQuery("#edtEMailContatto").val(),
    'i'   : jQuery("#txtNoteContatto").val(),
    'p1'  : jQuery("#chkSinglePrivacyAccepted_1").is(":checked")?1:0,
    'p2'  : jQuery("#chkSinglePrivacyAccepted_2").is(":checked")?1:0,
    'p3'  : jQuery("#chkSinglePrivacyAccepted_3").is(":checked")?1:0,
    'p4'  : jQuery("#chkSinglePrivacyAccepted_4").is(":checked")?1:0,
    'p5'  : jQuery("#chkSinglePrivacyAccepted_5").is(":checked")?1:0,
    'pr'  : smPageContact.productCode,
    'k'   : smPageContact.checkCode
  },
  function(data,textStatus) {
    jQuery("#personalDataButtonConfirm,#privacyButtonConfirm").removeAttr("disabled");
    if (textStatus=='success') {
      if (data.opr=='ok') {
        // Saved
        smPageContact.showContactSent();
      } else {
        alert("Sembra che ci siano problemi sul nostro server.\nPotresti premere di nuovo \"Invia\"? Grazie!");
      }
    } else {
      alert("Il servizio al momento sembra non essere disponibile.\nTi chiediamo di contattarci via mail o telefono\ne ti forniremo tutta l'assistenza necessaria. Grazie!");
    }
  },"json");
}
smPageContact.blankPrivacy=function(){
  jQuery("#chkPrivacySingleCheck,#chkContactPopupPrivacyFullAccepted,#chkContactPopupPrivacyFullNotAccepted,#chkSinglePrivacyAccepted_1,#chkSinglePrivacyAccepted_2,#chkSinglePrivacyAccepted_3,#chkSinglePrivacyAccepted_4,#chkSinglePrivacyAccepted_5").removeAttr("checked","checked");
}
smPageContact.showContactSent=function(){
  jQuery("#divContactOnPage").removeClass("contactOnPageBackgroundVisible");
  jQuery("#divContactOnPageFormArea").hide();
  jQuery("#divContactSent").show();
}
smPageContact.init=function(pCheckCode,pSubSite) {
  smPageContact.checkCode=pCheckCode;
  smPageContact.subSite=pSubSite;
  smPageContact.assignEvents();
  smPageContact.setErrorRemoving();
  smPageContact.blankPrivacy();
}

