var BoxDownload = function(options, item){
  options = $.extend({
    uuid: '',
    platform: '',
    showDisclaimer: false,
    showCF: false,
    downloadLinkApp: ''
  }, options);

  var $item = $(item);
  var $betaform = $item.find("form.betaform");
  var $stepSms = $item.find("fieldset.stepSms");
  var $stepMobile = $item.find("fieldset.stepMobile");
  var $stepConf = $item.find("fieldset.stepConf");

  $item.find(".downloadLink").bind("click", function(){
    $(this).hide();
    $(this).parent().find(".txt").hide();
    if (!options.showDisclaimer){
      $.ajax({
        type: "POST",
        url: contextPath + "/actions/registered/wappush.action",
        data: {
          uuid: options.uuid,
          platform: options.platform
        }
      });
      $stepSms.show();
    }
    $item.find(".contentBoxSH").slideDown(1000);
    return false;
  });

  $item.find(".submit1").bind("click", function(){
    var browser = navigator.appName;
    var checkValidCF = true;
    if (options.showCF){
      var cf = $item.find(".regCF").val();
      checkValidCF = checkCF(cf);
      if (checkValidCF){
        var d = new Date();
        var userYear = parseInt(cf.substr(6,2));
        if (browser=="Microsoft Internet Explorer") userYear += 1900;
        userYear = d.getYear() - userYear;
        if (userYear <= 18) checkValidCF = false;
      }
    }
    if ($item.find("input[name='regTerms1']").is(":checked") && checkValidCF){
      $item.find(".stepConditions").hide();
      var $mobilePlatform = $item.find("select[name='position']");
      if ($mobilePlatform.find("option").length == 1 && $mobilePlatform.find("option").get(0).text == ""){
        $item.find(".submit2").trigger("click");
      } else {
        $stepMobile.show();
      }
    } else {
      if (!checkValidCF) niceMessage("Codice Fiscale errato", true);
      else niceMessage("E' necessario accettare il disclaimer", true);
    }
    return false;
  });

  $item.find(".submit2").bind("click", function(){
    $betaform.submit();
    return false;
  });

  $item.find(".submit3").bind("click", function(){
    $betaform.submit();
    return false;
  });

  $betaform.ajaxForm({
    url: contextPath + "/actions/registered/wappush.action",
    success: function(text){
      $stepMobile.hide();
      $stepSms.show();
      if (text === 'NO'){
        $stepSms.fadeOut("fast");
        niceMessage("Numero massimo di richieste superato", true);
        return;
      }
      if (!$item.find("input[name='smsReceived'][value='true']").is(":checked")){
        $stepSms.fadeOut("fast");
        $stepSms.fadeIn("fast");
      } else {
        $stepSms.hide();
        $stepConf.show();
      }
    },
    error: function(XMLHttpRequest, textStatus, errorThrown){
      niceMessage("Errore nell'invio sms", true);
    }
  });

  $item.find(".downloadApp").bind("click", function(){
    $.ajax({
      type: "POST",
      url: contextPath + "/actions/registered/addcountdownload.action",
      data: {
        uuid: options.uuid,
        platform: options.platform
      },
      async: true,
      success: function(msg){
        // su ie questo blocca il redirect ai siti esterni
        // location.reload();
        window.location.href = options.downloadLinkApp;
      },
      error: function (XMLHttpRequest, textStatus, errorThrown){
        // typically only one of textStatus or errorThrown
        // will have info
        niceMessage("Impossibile scaricare: " + textStatus + errorThrown, true);
      }
    });
  });
};
