﻿var ChkList = new Array();

$(document).ready(function () {
   $("#pnl_intro").hide();
   $("#btnLogin").button();
   $("#btnInfo").button().click(function () {
      if ($("div[name^='news']").attr('name') != null) {
         var news = $("div[name^='news']").attr('name').toString();
         news = news.split('_');
         news = news[1].toString();
         CommLoginServ.InfoClikPlus('Click', news, resultOnDialog);
         $("#dialog-form").dialog("open");
      }
   });
   $("#btnInfo").toggleClass("btnInfoStyle", 0);

   $("#tabs").tabs();
   $("#privacydiv").dialog({
      autoOpen: false,
      height: 600,
      width: 400,
      modal: true,
      resizable: true
   });
   document.getElementById("txtUser").focus();

});


function addInfo(ch) {
   if (ChkList.indexOf(ch) != -1)
   { } // ChkList.splice(ChkList.indexOf(ch), 1);
   else
      ChkList.push(ch);
   document.getElementById('elCk').value = ChkList;

}

function resultOnDialog(result) {
   if (result != "") {
      $("#dialog-box").html("<p>" + result.toString() + "</p>");
      $("#dialog-box").dialog("open");
   }
}

function resultCustmer(result) {
   var message = result.split("||||");

   $("#dialog-cust").dialog({
      autoOpen: false,
      height: 400,
      width: 350,
      modal: true,
      resizable: false,

      buttons: [{
         text: "Ok",
         click: function () {
            if (message[0] == 1) {//se è andata a buon fine attivo redirect alla pressione del tasto ok
               var user = message[2]; var psw = message[3]; var lan = message[4]; var dea = document.getElementById("dealer").value;
               if (dea == "generic") dea = "";
               var url = document.getElementById("destinationurl").value;
               var rediUrl = url + '?uid=' + user.toString() + '&pwd=' + psw.toString() + '&cui=' + lan + '&dea=' + dea;
               location.replace(rediUrl);
            } else {
               $(this).dialog("close");
               $("#dialog-form").dialog("open");
            }
         }
      }]

   });
   $("#dialog-cust").html("<p>" + message[1].toString() + "</p>");
   $("#dialog-cust").dialog("open");
}

function getPrivacyHtm() {
   var lan = getActiveLan();
   var p = document.getElementById("brand").value;
   CommLoginServ.getPrivacyterms(lan,p,showPrivacyTerms);
}
 function showPrivacyTerms(result) {
    var schema = result.split("||||")
    if (schema != "") {
       $("#privacydiv").html(schema[0]);
       $("#privacydiv").dialog("option","title",schema[1])
    }
    $("#privacydiv").dialog("open");
 }

//###############################################################################################################################
 $(function () {

    $("#dialog:ui-dialog").dialog("destroy");

    var nome = $("#nome"),
			email = $("#email"),
			tel = $("#tel"),
         terms = $("#terms"),
         allFields = $([]).add(nome).add(email).add(tel).add(terms),
			tips = $(".validateTips");

    var news = $("div[name^='news']").attr('name'); //nome_id della news visualizzata
    var termdiv = $("#termsdiv");
    function updateTips(t) {
       tips
				.text(t)
				.addClass("ui-state-highlight");
       setTimeout(function () {
          tips.removeClass("ui-state-highlight", 1500);
       }, 500);
    }

    function checkLength(o, n, min, max) {
       //            if (o.val().length > max || o.val().length < min) {
       //               o.addClass("ui-state-error");
       //               updateTips("Lunghezza " + n + " deve essere compresa tra " +
       //					min + " e " + max + ".");
       if (o.val().length < 1) {
          o.addClass("ui-state-error");
          updateTips("*Campo obbligatorio");
          return false;
       } else {
          return true;
       }
    }

    function checkRegexp(o, regexp, n) {
       if (!(regexp.test(o.val()))) {
          o.addClass("ui-state-error");
          updateTips(n);
          return false;
       } else {
          return true;
       }
    }

    function checkTerms(o, n) {
       if (!o.get(0).checked) {
          termdiv.addClass("ui-state-error");
          updateTips(n);
          return false;
       } else {
          return true;
       }
    }
    var lan = getActiveLan();
    var btnInvia = "Invia richiesta";
    switch (lan) {
       case "it-IT": break;
       case "en-US": btnInvia = "Send";
          break;
       case "es-ES": btnInvia = "Enviar";
          break;
    }

    $("#dialog-form").dialog({
       autoOpen: false,
       height: 500,
       width: 450,
       modal: true,
       resizable: false,

       buttons: [
        {
           text: btnInvia,
           click: function () { InfoClick(); }
        }]
    });

    $("#dialog-form").dialog({
       close: function () {
          allFields.val("").removeClass("ui-state-error");
          termdiv.removeClass("ui-state-error");
          document.getElementById("txtUser0").value = "";
          document.getElementById("txtPsw0").value = "";
          terms.get(0).checked = false;
       }

    });

    function InfoClick() {
       var selTab = $("#tabs").tabs("option", "selected");
       switch (selTab) {
          //NON CLIENTE                         
          case 1:

             var bValid = true;
             allFields.removeClass("ui-state-error");

             bValid = bValid && checkLength(nome, "nome cognome", 1, 50);
             //bValid = bValid && checkLength(ragsoc, "ragsoc", 1, 50);
             //bValid = bValid && checkLength(tel, "tel", 1, 50);
             bValid = bValid && checkLength(email, "email", 6, 80);
             //bValid = bValid && checkRegexp(nome, /^([a-zA-Z])+$/, "Consentite solo lettere a-z senza spazi");
             //bValid = bValid && checkRegexp(tel, /^([0-9])+$/, "Consentiti solo  0-9");
             bValid = bValid && checkRegexp(email, /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i, "es. indirizzo@dominio.com");
             bValid = bValid && checkTerms(terms, "Accettare le condizioni");

             if (bValid) {//SE è tutto Ok inviare la mail
                var lan = getActiveLan();
                var brand = document.getElementById("dealer").value;
                CommLoginServ.SendInfoMail(nome.val(), email.val(), tel.val(), news.toString(), lan, brand.toString(), resultOnDialog);
                $("#dialog-form").dialog("close");
             }
             break;
          // GIà CLIENTE                          
          case 0:

             var user = document.getElementById("txtUser0").value;
             var psw = document.getElementById("txtPsw0").value;
             if (user != "" && psw != "") {
                var lan = getActiveLan();
                var brand = document.getElementById("dealer").value;
                CommLoginServ.CustInfoRequest(user, psw, news, lan, brand.toString(), resultCustmer);
                //redirect 
                $("#dialog-form").dialog("close");
             }
             break;

       }
    }

    $("#dialog-box").dialog({
       autoOpen: false,
       height: 300,
       width: 350,
       modal: true,
       resizable: false
    });



 });
//###############################################################################################################################
function standardLogin() {
   var user = document.getElementById("txtUser").value;
   var psw = document.getElementById("txtPsw").value;
   var dea = document.getElementById("dealer").value;
   var url = document.getElementById("destinationurl").value;
   var lan = getActiveLan();
   $("#pnl_intro").show();
   CommLoginServ.CheckLogin(user, psw,lan,dea, url, resultLogin);
}

function resultLogin(result) {
   $("#pnl_intro").hide();
   if (result != "") {
      location.replace(result);
   }

}

function getActiveLan() {
   var language = "it-IT";
   if (document.getElementById("ActiveLan").value != "") {
      language = document.getElementById("ActiveLan").value.toString();
   }
   return language;
}

function setLanguage(parLan) {
   document.getElementById("ActiveLan").value = parLan;
   CommLoginServ.setLan(parLan);
   var t = setTimeout("__doPostBack()", 500);
     
}
