var pw_clicked = 0;
$(document).ready(function(){
  $('#remote_content').jqm({
    ajax: '@href',
    trigger: 'a.jqmodal',
    modal: false,
    onShow: function(h) {
      /* callback executed when a trigger click. Show notice */
      h.w.css('opacity',0.95).fadeIn(500);
      },
    onHide: function(h) {
      /* callback executed on window hide. Hide notice, overlay. */
      h.w.fadeOut(250,function() { if(h.o) h.o.remove(); }); }
  });
  $("a[rel=popup]").click(function(){
            popupWindow = window.open(this.href,this.title,'menu=no,toolbar=no,width=670,height=600,scrollbars=1,resizable=0,directories=no,location=no,screenX=0,screenY=0,top=48,left=48');
             popupWindow.focus()
             return false;
  });
  $("a[rel=popup2]").click(function(){
            popupWindow = window.open(this.href,this.title,'menu=no,toolbar=no,width=660,height=400,scrollbars=1,resizable=0,directories=no,location=no,screenX=0,screenY=0,top=48,left=48');
             popupWindow.focus()
             return false;
  });
  $("a[rel=popup3]").click(function(){
            popupWindow = window.open(this.href,this.title,'menu=no,toolbar=no,width=665,height=280,scrollbars=1,resizable=0,directories=no,location=no,screenX=0,screenY=0,top=48,left=48');
             popupWindow.focus()
             return false;
  });
  $("input[name='user[username]']").focus(function() {
    if($(this).val() == 'username') {
      $(this).val('');
    }
  });
  $("input[name='user[username]']").blur(function() {
    if(!$(this).val()) {
      $(this).val('username');
    }
  });
  $("input[name='user[password]']").focus(function() {
    if($(this).val() == 'password') {
      $(this).val('');
    }
  });
  $("input[name='user[password]']").blur(function() {
    if(!$(this).val()) {
      $(this).val('password');
    }
  });
});


//Login Form
function submit_simple_login() {
  var login_params = $("form.ajaxLoginForm").serialize();
  $.ajax({
          type: "POST",
          url:  "/user/login",
          data: login_params,
          success: function(data){
            response = JSON.parse(data);
            if(response["status"] == 'ok') {
              location = '/user/index';
            } else {
              location = '/user/login_form?error=1';
            }
          }
        });
}

function submit_ajax_login() {
  var login_params = $("form.ajaxLoginForm").serialize();
  $.ajax({
          type: "POST",
          url:  "/user/login",
          data: login_params,
          success: function(data){
            response = JSON.parse(data);
            if(response["status"] == 'ok') {
              location = '/user/index';
            } else {
              $(".form_message").hide();
              var errors = response["errors"]
              var error_txt = 'Please complete the fields highlighted';
              if(errors["invalid_username"]) {
                error_txt = 'Oops! The username you entered is incorrect, please try again.'; 
                errors["username"] = 1;
              } else if(errors["invalid_password"]) {
                error_txt = 'Oops! The password you entered is incorrect, please try again.'; 
                errors["password"] = 1;
              }
              $(".message.error_message").html(error_txt);
              $("#login_form_error").show();
              errors["username"] ? $("[for='login_username_label']").addClass("form_error") : $("[for='user_username']").removeClass("form_error");
              errors["password"] ? $("[for='login_password_label']").addClass("form_error") : $("[for='user_password']").removeClass("form_error");
            }
          }
        });
}

function forgot_my_password() {
  $("#remote_content").load("/user/forgot_password");
}
//Forgot password
function submit_ajax_forgot_password() {
  var login_params = $("form.ajaxForgotForm").serialize();
  $.ajax({
          type: "POST",
          url:  "/user/forgot_password",
          data: login_params,
          success: function(data){
            $("#remote_content").html(data);
          }
        });
}

//Contact Us Form
function send_customer_inquiry() {
  var contact_form_params = $("form.customerInquiryForm").serialize();
  $(".form_loader").show();

  $.ajax({
          type: "POST",
          url:  "/site/contact_form",
          data: contact_form_params,
          success: function(data){
            $(".form_loader").hide();
            if(data == 'ok') {
              data = "<p>Your thoughts have been sent!</p>";
              $(".form_message.error").hide();
              $(".btn.send_btn").hide();
              $(".success_message").show();
              $("#contact_form_email").val('');
              $("#contact_form_thoughts").val('');
              $("#contact_us_email_label").removeClass("form_error");
              $("#contact_us_thoughts_label").removeClass("form_error");
            } else {
              $(".form_message.headline").hide();
              if (data == 'error.email') {
                data = "Oops! Please put in your email so we know how to get back to you.";
                $(".message.error_message").html(data);
                $("#contact_us_email_label").addClass("form_error");
                $("#contact_us_thoughts_label").removeClass("form_error");
              } else if  (data == 'error.thoughts'){
                data = "Oops! It looks like you forgot to type in your thoughts.";
                $(".message.error_message").html(data);
                $("#contact_us_email_label").removeClass("form_error");
                $("#contact_us_thoughts_label").addClass("form_error");
              } else if (data == 'error.no_format') {
                data = "Sorry! That does not look like a proper email address. Please try typing it in again.";
                $(".message.error_message").html(data);
                $("#contact_us_email_label").addClass("form_error");
                $("#contact_us_thoughts_label").removeClass("form_error");
              } else {
                data = "<p>Oops! You forgot to enter your email and your thoughts!</p>";
                $(".message.error_message").html(data);
                $("#contact_us_email_label").addClass("form_error");
                $("#contact_us_thoughts_label").addClass("form_error");
              }
              $(".form_message.error").show();
          }
        }
  });
}

function submit_registration() {
  $("form#rinkoRegForm").submit();
}

function get_payment_form() {
  $("form#payment_form").submit();
}

