

// Rules Pop-Up
function pop_up(url, title) {
  window.open(url, title, "menu=no,toolbar=no,width=700,height=500,scrollbars=1,resizable=0,directories=no,location=no,screenX=0,screenY=0,top=88,left=124");
}

// User Information Update
function update_user_attr(field_name, field_value) {
  if(field_name && field_value) {
    $.ajax({
            type: "POST",
            url:  "/user/update_user_attr",
            data: "name=" + field_name + "&value=" + field_value + '&authenticity_token='+authenticity_token,
            success: function(data){
            }
          });
  }
}

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

  $.ajax({
          type: "POST",
          url:  "/email/contact_form",
          data: contact_form_params,
          success: function(data){
            
            $("#contact_form_loader").hide();

            if(data == 'ok') {
              data = "<p>Your thoughts have been sent!</p>";
              $("#contact_form_error").hide();
              $("#contact_form_thanks").html(data);
              $("#contact_form_thanks").show();
              $("#contact_form_email").val('');
              $("#contact_form_thoughts").val('');
              $("#contact_form_email_label").removeClass("form_label_error");
              $("#contact_form_thoughts_label").removeClass("form_label_error");
            } else if (data == 'error.email') {
              data = "<p>Oops! You forgot to enter your email!</p>";
              $("#contact_form_error").html(data);
              $("#contact_form_error").show();
              $("#contact_form_email_label").addClass("form_label_error");
              $("#contact_form_thoughts_label").removeClass("form_label_error");
            } else if  (data == 'error.thoughts'){
              data = "<p>Oops! You forgot to enter your thoughts!</p>";
              $("#contact_form_error").html(data);
              $("#contact_form_error").show();
              $("#contact_form_email_label").removeClass("form_label_error");
              $("#contact_form_thoughts_label").addClass("form_label_error");
            } else if (data == 'error.no_format') {
              data = "<p>Oops! Your email address is not formatted correctly!</p>";
              $("#contact_form_error").html(data);
              $("#contact_form_error").show();
              $("#contact_form_email_label").addClass("form_label_error");
              $("#contact_form_thoughts_label").removeClass("form_label_error");
            } else {
              data = "<p>Oops! You forgot to enter your email and your thoughts!</p>";
              $("#contact_form_error").html(data);
              $("#contact_form_error").show();
              $("#contact_form_email_label").addClass("form_label_error");
              $("#contact_form_thoughts_label").addClass("form_label_error");
            }
          }
        });
}

//Login Form
function submit_ajax_login() {
  var login_params = $("form.ajaxLoginForm").serialize();
  $("#login_form_loader").show();
  
  $.ajax({
          type: "POST",
          url:  "/user/login",
          data: login_params,
          success: function(data){
            
            $("#login_form_loader").hide();

            if(data == 'ok') {
                location = '/site/index';
            } else if (data == 'error.email') {
              data = "<p>Oops! You forgot to enter your email!</p>";
              $("#login_form_error").html(data);
              $("#login_form_error").show();
              $("#login_form_email_label").addClass("form_label_error");
              $("#login_form_password_label").removeClass("form_label_error");
            } else if  (data == 'error.password'){
              data = "<p>Oops! You forgot to enter your password!</p>";
              $("#login_form_error").html(data);
              $("#login_form_error").show();
              $("#login_form_email_label").removeClass("form_label_error");
              $("#login_form_password_label").addClass("form_label_error");
            } else if (data == 'error.no_user') {
              data = "<p>Sorry! We can not find an account with that email and password.</p>";
              $("#login_form_error").html(data);
              $("#login_form_error").show();
              $("#login_form_email_label").addClass("form_label_error");
              $("#login_form_password_label").addClass("form_label_error");
            } else if (data == 'error.no_format') {
              data = "<p>Oops! Your email address is not formatted correctly!</p>";
              $("#login_form_error").html(data);
              $("#login_form_error").show();
              $("#login_form_email_label").addClass("form_label_error");
              $("#login_form_password_label").removeClass("form_label_error");
            } else {
              data = "<p>Oops! You forgot to enter your email and your password!</p>";
              $("#login_form_error").html(data);
              $("#login_form_error").show();
              $("#login_form_email_label").addClass("form_label_error");
              $("#login_form_password_label").addClass("form_label_error");
            }
          }
        });
}

//Site Search Query
function submit_site_search() {
  var search_term = $("form.search_form").serialize();
  $("#search_results_popup").slideDown();
  $("#search_results_list").load('/site/search?'+search_term);
}

//Subscription Status Update
function update_user_optin(optin_name,optin_value) {
  $.ajax({
          type: "POST",
          url:  "/user/update_optin",
          data: 'name='+optin_name+'&value='+optin_value+'&authenticity_token='+authenticity_token,
          success: function(data){
            if(data == '1') {
                $(".statusmsg span").html("Subscribed");
                $("#status_image img").attr({ src: "/images/myaccount/myaccount_subscribe.gif", alt: "You are NOT subscribed to the newsletter!" });
            } else {
                $(".statusmsg span").html("Unsubscribed");
                $("#status_image img").attr({ src: "/images/myaccount/myaccount_unsubscribe.gif", alt: "You are NOT subscribed to the newsletter!" });
            }
          }
        });
}

// Terms of Use Pop-Up
function terms_of_use() {
  window.open("/site/terms_of_use","Terms of Use","menu=no,toolbar=no,width=550,height=600,scrollbars=1,resizable=0,directories=no,location=no,screenX=0,screenY=0,top=48,left=48");
}

// Privacy Policy Pop-Up
function privacy_policy() {
  window.open("/site/privacy_policy","Terms of Use","menu=no,toolbar=no,width=550,height=600,scrollbars=1,resizable=0,directories=no,location=no,screenX=0,screenY=0,top=48,left=48");
}

// Image Select
function select_image() {
  window.open('/admin/select_image', 'Select an image', "menu=no,toolbar=no,width=700,height=500,scrollbars=0,resizable=0,directories=no,location=no,top=88,left=124");
}
