function bmtForm(el) {
  var $el = jQuery(el);
  jQuery(".product-id").val($el.attr("data-value"));
}

function fillDiscountCode(code) {
  jQuery("#DISCOUNTCODE").val(code);
}

function fillBmtId(id, key) {
  jQuery("#BMTID_"+key).val(id);
}

function download(url)
{
  setTimeout(function() { location.href = url; }, 500);
  jQuery("#subscription_first_name").focus();
}

function subscribing($form) {
  var $this = jQuery(".subscribe", $form);
  $this.hide();
  var text = $form.attr("data-progress") || "Subscribing..."
  var subscribing = jQuery("<div/>").addClass('subscribing').html(text);
  $this.after(subscribing);
}

function validate_newsletter_form(form) 
{
  var $form = jQuery(form);
  var info = "Please fill in ";
  var email = jQuery.validation.getRule('email').check(jQuery('.email', $form).val());
  if (email) {
    $form.find("input").removeClass(t.required);
    subscribing($form);
    return true;
  } else {
    jQuery('.email', $form).addClass(t.required).focus();
    info += t.valid_email;
    //jQuery(".subscribing", $form).remove();
    //jQuery(".subscribe", $form).show();
    alert(info);
    return false;
  }
}

function validate_uninstall_from(form) 
{
  var $form = jQuery(form);
  var info = "";
  var email = jQuery.validation.getRule('email').check(jQuery('.email', $form).val());
  var comments = jQuery('#support_request_message').val();
  if (!email && !comments) {
    info = "Please type in the main issue or your comments or valid email.";
    //jQuery("#sending").remove();
    //jQuery("#send").show();
    alert(info);
    return false;
  }
  $form.find("input[name=magicwand]").val(t.antispam);
  sending();
  return true;
}

function sending() {
  var $el = jQuery("#send");
  $el.hide();
  var sending = jQuery("<div/>").attr("id", "sending").addClass("sending").html('Sending message...');
  $el.after(sending);
}

function validate_contact_from(form) 
{
  var info = "Please fill in ";
  var email = jQuery.validation.getRule('email').check(jQuery('#support_request_email').val());
  var msg = jQuery.validation.getRule('required').check(jQuery('#support_request_message').val());
  
  if (email && msg) {
    jQuery(form).find("input, textarea").removeClass(t.required);
    jQuery(form).find("input[name=magicwand]").val(t.antispam);
    sending();
    return true;
  } else {
    if (!email) { 
      info += t.valid_email; 
      jQuery('#support_request_email').addClass(t.required);
      jQuery('#support_request_email').focus();
    }
    if (!email && !msg) { info += " and "; }
    if (!msg) { 
      info += t.message;
      jQuery('#support_request_message').addClass(t.required);
      if (email) jQuery('#support_request_message').focus();
     }
    info += ".";
    //jQuery("#sending").remove();
    //jQuery("#send").show();
    alert(info);
    return false;
  }
};

function feedback_sent() 
{
  jQuery("#sending").remove();
  jQuery("#support_request_name").val("");
  jQuery("#support_request_email").val("");
  jQuery("#support_request_message").val("");
  var bar = jQuery("<div/>").addClass("sent-notice").html(t.thanks_for_message);
  jQuery("#send").after(bar);
};

function why_uninstall_sent()
{
  jQuery("#sending").remove();
  var bar = jQuery("<div/>").addClass("sent-notice").html(t.thanks_for_why_uninstall);
  jQuery("#send").after(bar);
};

function subscription_saved(formId)
{
  var $form = jQuery(formId);
  var subscribing = jQuery(".subscribing", $form);
  subscribing.addClass("subscribed").html(t.thank_you);
  jQuery(".email", $form).val("").blur();
  jQuery(".name", $form).val("");
  setTimeout(function() {
    subscribing.fadeOut(150, function() {
      jQuery(".subscribe", $form).fadeIn(150);
      subscribing.remove();
    });
  }, 7000);
};

function download_subscription_saved(formId)
{
  parent.jQuery("#fancybox-outer").removeClass("fancybox-highlight");
  parent.jQuery.fancybox(t.thanks_for_subscription);
};

function feedback_not_sent(msg)
{
  jQuery("#sending").remove();
  jQuery("#send").show();
  alert(msg);
};

function subscription_not_saved(msg)
{
  jQuery(".subscribing").remove();
  jQuery(".subscribe").show();
  alert(msg);
};

function order_feedback_sent(notice)
{
  jQuery.fancybox(t.order_thanks_for_message);
};

function send_another()
{
  jQuery(".sent-notice").remove();
  jQuery("#send").show();
  jQuery("#support_request_name").focus();
};

function order_send_another()
{
  jQuery("#show-contact-form").click();
};

function close_fancybox()
{
  jQuery.fancybox.close();
};

function rot13(str) {
	return str.replace(/[a-zA-Z]/g, function(c){
		return String.fromCharCode((c <= "Z" ? 90 : 122) >= (c = c.charCodeAt(0) + 13) ? c : c - 26);
	});
};

function download_email(product_code) {
  setTimeout(function() { 
    jQuery.fancybox('<a/>', { 
      href: '/subscriptions/from_download/'+product_code, 
      type: 'iframe', 
      transitionIn: 'elastic',
      centerOnScroll: true,
      width: 600,
      height: 215,
      onStart: function() {
        jQuery("#fancybox-outer").addClass("fancybox-highlight");
      }, 
      onClosed: function() {
        jQuery("#fancybox-outer").removeClass("fancybox-highlight");
      }
    });
  }, 10);
}