$(document).ready(function() {

  if ($.browser.mozilla) {
    $("head").append("<link href='/stylesheets/mozilla.css' media='screen' rel='stylesheet' type='text/css' />");
  }
  
  if ($(".wysiwyg").length) {
    $(".wysiwyg").ckeditor();
  }  
  
  if ($(".wh_table").length) {
    load_wh_tooltip();
  }
  
  // LOGIN
  $("#login_switcher").overlay({
    top: 65,
    mask: {
     color: '#333',
     loadSpeed: 15,
     opacity: 0.5
    },
    closeOnClick: true
  });
  $("a#login_switch_btn").click(function() {
    $("#login_switcher").overlay().load();
    return false;
  });
  // END LOGIN
  
  // ADMIN
  if ($("a#add_image").length) {
    load_photo_gallery_overlay();
  }
  
  
  if ($(".pagination").length) {
      $(".pagination").append("<div style='clear:both;'></div>");

      var pag_current = $(".pagination span.current").html();
      var pag_new = "(" + pag_current + ")";
      $(".pagination span.current").html(pag_new);
  }

  if ($("#home_profile_wrapper").length) {
    load_home_profiles();
  }
  
  if ($("form#form_newsletter").length) {
    load_newsletter_form();
  }
  
  if ($("form#new_migration").length) {
    load_migration_form();
  }
  
  if ($("form#new_virtualserver").length) {
    load_vs_form();
  }

  if ($("form#new_enterprise_quote").length) {
    load_ent_form();
  }
  
  if ($("a.fancybox_lnk").length) {
    load_fancybox();
  }
  
  if ($("#browsable").length) {
    $("#dynamic_featured_0").addClass('active_featured');
    $("#browsable").scrollable({
      circular: true,
      onSeek: function(event, i) {
        // $(".dynamic_featured_items").css("border-bottom", "none");
        $(".dynamic_featured_items").removeClass('active_featured');
        var cobj = "#dynamic_featured_" + i;
        // $(cobj).css("border-bottom", "1px solid #009cdd");
        $(cobj).addClass('active_featured');
      }
    }).navigator({
      navi: "#browsable_selector",
      naviItem: 'a',
      activeClass: 'active'
    });
  }

  if ($("#answer_question_search").length) {
    // Answer Search Box
    $("#answer_question_search").val("I.e How to Migrate a MySQL Database").css('color', '#999').css('font-style', 'italic');
    $("#answer_question_search").click(function() {
      $(this).val("").css('color', '#000').css('font-style', 'normal');
    });
    $("#answer_question_search").keyup(function(e) {
      var code = (e.keyCode ? e.keyCode : e.which);
      if (code == '13') {
        submit_answer();
      }
    }); 
  }

  /*
    Form Status Messages
   */
  // vs
  if ($("#vs_status_message").length) {
    var vs_current_url = window.location.toString();
    var vs_params = vs_current_url.split("?");
    var vs_msg = vs_params[1].split("=");
    var vs_msg_type = vs_msg[1];
    if (vs_msg[0] != 'r') {
      if (vs_msg_type == "true") {
        $("#vs_msg").addClass("success").html("Thank You! Your order has been received.");
        $("#vs_status_message").show();
      } else {
        $("#vs_msg").addClass("error").html("Error! There was a problem with your form submission.");
        $("#vs_status_message").show();
      }
    }
  }
   // ep
  if ($("#ep_status_message").length) {
    var ep_current_url = window.location.toString();
    var ep_params = ep_current_url.split("?");
    var ep_msg = ep_params[1].split("=");
    var ep_msg_type = ep_msg[1];
    if (vs_msg[0] != 'r') {
      if (ep_msg_type == "true") {
        $("#ep_msg").addClass("success").html("Thank you for your Quote Request - a NR System Engineer will be contacting you within the next 24-28 hours.");
        $("#ep_status_message").show();
      } else {
        $("#ep_msg").addClass("error").html("Error! There was a problem with your form submission.");
        $("#ep_status_message").show();
      }
    }
  }

  // migration
  if ($("#migration_status_message").length) {
    var migration_current_url = window.location.toString();
    var migration_params = migration_current_url.split("?");
    var migration_msg = migration_params[1].split("=");
    var migration_msg_type = migration_msg[1];
    
    if (vs_msg[0] != 'r') {
      if (migration_msg_type == "true") {
        $("#migration_msg").addClass("success").html("Thank You! Your migration form has been successfully received.");
        $("#migration_status_message").show();
      } else {
        $("#migration_msg").addClass("error").html("Error! There was a problem with your form submission.");
        $("#migration_status_message").show();
      }
    }
  }

  setup_nav();
});


function submit_answer() {
  var params = $("#answer_question_search").val();
  
  if (params != '') {
    var url = '/answers/search?s=' + params;
    window.location = url; 
  }
}

function setup_nav() {
  // Adds blue line to nav item when it's path exists in the url
  var path = window.location.pathname;
  $("ul#menu li a").each(function(item) {
    var exp = $(this).attr("href");
    exp = exp.replace(/[/]/, '');
    var exp1 = new RegExp(exp);
    if (path.search(exp1) > 0) {
      $(this).css("border-bottom", "1px solid #009cdd");
    }
  });  
}

function load_home_profiles() {
    
  $(".home_profile_detail").overlay({
    top: 100,
    mask: {
      color: '#333',
      opacity: 0.8,
      loadSpeed: 0,
      closeSpeed: 0
    },
    closeOnClick: false
  });
  
  $("a.home_profile_popup_close").click(function() {
    $(this).parent().parent().parent().overlay().close();
  });
  
  $("a.profile_popup_action").click(function() {
    var pf = "#profile_popup_" + $(this).attr("profile");
    var current = $(this).parent().parent();
    var next = $(current).next().attr("profile");
    var prev = $(current).prev().attr("profile");
    var nextbtn = "#home_profile_next_control_" + $(this).attr("profile");
    var prevbtn = "#home_profile_prev_control_" + $(this).attr("profile");
    if (!(next)) {      
      $(nextbtn).css("visibility", "hidden");
    }
    if (!(prev)) {
      $(prevbtn).css("visibility", "hidden");
    }
    $(pf).overlay().load();
    return false;
  });

  $(".home_profile_image_up").click(function() {
    var pf2 = "#profile_popup_" + $(this).attr("profile");
    var current2 = $(this).parent();
    var next2 = $(current2).next().attr("profile");
    var prev2 = $(current2).prev().attr("profile");
    var nextbtn2 = "#home_profile_next_control_" + $(this).attr("profile");
    var prevbtn2 = "#home_profile_prev_control_" + $(this).attr("profile");
    if (!(next2)) {
      $(nextbtn2).css("visibility", "hidden");
    }
    if (!(prev2)) {
      $(prevbtn2).css("visibility", "hidden");
    }
    $(pf2).overlay().load();
    return false;
  });
  
  $("#home_profile_scrollable").scrollable({circular: true});
  
  $(".home_profile_detailtxt").hide();
  $(".home_profile_image_up").hide();
  
  $(".home_profile").hover(function() {
   var pid = $(this).attr("profile");
   var detail = "#profile_detail_" + pid;
   var uimg = "#home_profile_up_" + pid;
   var dimg = "#home_profile_down_" + pid;
   $(detail).toggle();
   $(uimg).toggle();
   $(dimg).toggle();
  });
  
  // Load Next section
  $(".home_profile_next").click(function() {
    var current = $(this).parent().parent().parent().parent().parent();
    var next = $(current).next().attr("profile");
    var next2 = $(current).next().next().attr("profile");
    var cwin = "#profile_popup_" + $(current).attr("profile");
    var nwin = "#profile_popup_" + next;
    var nextbtn = "#home_profile_next_control_" + $(current).next().attr("profile");
    if (!(next2)) {      
      $(nextbtn).css("visibility", "hidden");
    }
    
    $(nwin).overlay().load();    
    return false;
  });
  
  // Load Prev Section
  $(".home_profile_prev").click(function() {
    var current = $(this).parent().parent().parent().parent().parent();
    var prev = $(current).prev().attr("profile");
    var cwin = "#profile_popup_" + $(current).attr("profile");
    var nwin = "#profile_popup_" + prev;
    var prevbtn = "#home_profile_prev_control_" + $(current).next().attr("profile");
    if (!(prev)) {
      $(prevbtn).css("visibility", "hidden");
    }
    $(nwin).overlay().load();
    return false;
    
  });

}

function load_newsletter_form() {
  $("input#newsletter_signup_submit").show(); // Require JS to be enabled in order to see signup button
  $("form#form_newsletter").submit(function() {
    var ready = true;
    
    if ($("input#newsletter_fname").val() == '') {
      $("input#newsletter_fname").addClass("error");
      $("#newsletter_fname_error").show();
      ready = false;
    } else {
      $("input#newsletter_fname").removeClass("error");
      $("#newsletter_fname_error").hide();
      ready = true;
    }
    
    if ($("input#newsletter_lname").val() == '') {
      $("input#newsletter_lname").addClass("error");
      $("#newsletter_lname_error").show();
      ready = false;
    } else {
      $("input#newsletter_lname").removeClass("error");
      $("#newsletter_lname_error").hide();
    }
    
    if ($("input#newsletter_email").val() == '') {
      $("input#newsletter_email").addClass("error");
      $("#newsletter_email_error").show();
      ready = false;
    } else {
      $("input#newsletter_email").removeClass("error");
      $("#newsletter_email_error").hide();
    }
    
    if (!(ready)) {
      return false;
    }
    
  });
}


function load_migration_form() {
  $("input#migration_form_submit").show();
  
  $("form#new_migration").submit(function() {
    
    var ready = true;
    var email_ok = true;
    
    // Validations
    if ($("input#migration_fname").val() == '') {
      ready = false;
      $("#migration_fname_error").show();
      $("input#migration_fname").addClass("error");
    } else {
      ready = true;
      $("#migration_fname_error").hide();
      $("input#migration_fname").removeClass("error");
    }
    
    if ($("input#migration_lname").val() == '') {
      ready = false;
      $("input#migration_lname").addClass("error");
      $("#migration_lname_error").show();
    } else {
      $("input#migration_lname").removeClass("error");
      $("#migration_lname_error").hide();  
    }
    
    // Email Confirm
    if (!($("input#migration_email").val() == $("input#migration_email_confirmation").val())) {
      ready = false;
      $("input#migration_email_confirmation").addClass("error");
      $("input#migration_email").addClass("error");
      $("#migration_email_error").show();
      email_ok = false;
    }
    
    if ($("input#migration_email").val() == '') {
      ready = false;
      $("input#migration_email").addClass("error");
      $("#migration_email_error").show();
    } else {
      // Only remove error style if email is equal
      if (email_ok) {
        $("input#migration_email").removeClass("error");
        $("#migration_email_error").hide();
      }      
    }
    
    if ($("input#migration_email_confirmation").val() == '') {
      ready = false;
      $("input#migration_email_confirmation").addClass("error");
      $("#migration_email_error").show();
    } else {
      if (email_ok) {
        $("input#migration_email_confirmation").removeClass("error");
        $("#migration_email_error").hide();
      }      
    }
    
    
    if ($("input#migration_phone").val() == '') {
      ready = false;
      $("input#migration_phone").addClass("error");
      $("#migration_phone_error").show();
    } else {
      $("input#migration_phone").removeClass("error");
      $("#migration_phone_error").hide();
    }
    
    
    if ($("input#migration_current_web_host").val() == '') {
      ready = false;
      $("input#migration_current_web_host").addClass("error");
      $("#migration_webhost_error").show();
    } else {
      $("input#migration_current_web_host").removeClass("error");
      $("#migration_webhost_error").hide();
    }
    
    if ($("input#migration_current_control_panel").val() == '') {
      ready = false;
      $("input#migration_current_control_panel").addClass("error");
      $("#migration_controlpanel_error").show();
    } else {
      $("input#migration_current_control_panel").removeClass("error");
      $("#migration_controlpanel_error").hide();
    }
     
     
    if ($("input#migration_current_os").val() == '') {
      ready = false;
      $("input#migration_current_os").addClass("error");
      $("#migration_currentos_error").show();
    } else {
      $("input#migration_current_os").removeClass("error");
      $("#migration_currentos_error").hide();
    }
    
    if ($("input#migration_any_ssl").val() == '') {
      ready = false;
      $("input#migration_any_ssl").addClass("error");
      $("#migration_ssl_error").show();
    } else {
      $("input#migration_any_ssl").removeClass("error");
      $("#migration_ssl_error").hide();
    }
    
    if ($("input#migration_any_static_ip").val() == '') {
      ready = false;
      $("input#migration_any_static_ip").addClass("error");
      $("#migration_ip_error").show();
    } else {
      $("input#migration_any_static_ip").removeClass("error");
      $("#migration_ip_error").hide();
    }
    
    if ($("input#migration_number_of_domains").val() == '') {
      ready = false;
      $("input#migration_number_of_domains").addClass("error");
      $("#migration_domains_error").show();
    } else {
      $("input#migration_number_of_domains").removeClass("error");
      $("#migration_domains_error").hide();
    }
  
     
    if (!(ready)) {
      return false;
    }
  });
  
}

/*
  Virtual Server Form
*/
function load_vs_form() {
  
  var month_total = 0;
  var year_total = 0;
  var one_time = 0;
  var amount_saved = 0;
  // Force user to enable javascript
  $("input#vs_form_submit").show();
  $(".small_virtual_server_table").show();
  $("#vs_form").show();
  
  // line item setup
  $("#order_year_section").hide();
  $("#order_saved").hide();

  /*
    Setup Tooltips
   */
  $("tr#vs_stack_wrapper h3").tooltip({
    position: "top center",
    offset: [15,0]
  });
  /*
    SETUP CURRENT PLAN
    choose current plan based on plan param
    Break up url, choose everything after ?plan=
  */
  var current_url = window.location.toString();
  var params = current_url.split("?");
  var plan = params[1].split("=");
  plan = plan[1];

  $("input[name='virtualserver[plan]']").each(function() {
    if($(this).val() == plan) {
      $(this).parent().parent().addClass("active");
      $(this).attr("checked","checked");
      current_val = $(this).val();
      overlay = "#vs_plan_overlay_" + current_val;
      $(overlay).show();
      construct_order_line_item("order_plan", $(this).parent().attr("desc"), parseInt($(this).parent().attr("month")), parseInt($(this).parent().attr("year")));
      month_total += parseInt($(this).parent().attr("month"));
      year_total += parseInt($(this).parent().attr("year"));
      $("#order_total").html("$" + month_total);
      $("#month_total").html("$" + month_total);
      $("#year_total").html("$" + year_total);
    }
  });
  
  //select plan by clicking row
  $("tr.vs_row").click(function() {
    var vs_row_plan = $(this).attr("plan");
    var vs_row_id = "input#virtualserver_plan_" + vs_row_plan;
    $(vs_row_id).attr("checked", true);
    $(".vs_plan_overlay").hide();
    $("#order_plan").parent().parent().remove(); // removes order plan from line items
    load_vs_order_line_items();


    // Delete old plan from totals
    month_total -= parseInt($("tr.active td:first").attr("month"));
    year_total -= parseInt($("tr.active td:first").attr("year"));

    $(".small_virtual_server_table tr").removeClass("active");
    current_val = $(vs_row_id).val();
    overlay = "#vs_plan_overlay_" + current_val;
    $(overlay).show();
    $(vs_row_id).parent().parent().addClass("active");

    month_total += parseInt($(vs_row_id).parent().attr("month"));
    year_total += parseInt($(vs_row_id).parent().attr("year"));
    $("#month_total").html("$" + month_total);
    $("#year_total").html("$" + year_total);

    // determine if you should use year or month total
    if ($("#order_month_section").css("display") == 'none') {
      $("#order_total").html("$" + (year_total + one_time));
      amount_saved = (month_total * 12) - year_total;
      $("#amount_saved").html("$" + amount_saved);
    } else {
      $("#order_total").html("$" + (month_total + one_time));
    }
    construct_order_line_item("order_plan", $(vs_row_id).parent().attr("desc"), parseInt($(vs_row_id).parent().attr("month")), parseInt($(vs_row_id).parent().attr("year")));
  });

  // Price Table
  $("input[name='virtualserver[plan]']").click(function() {
    $(".vs_plan_overlay").hide();
    $("#order_plan").parent().parent().remove(); // removes order plan from line items
    load_vs_order_line_items();


    // Delete old plan from totals
    month_total -= parseInt($("tr.active td:first").attr("month"));
    year_total -= parseInt($("tr.active td:first").attr("year"));

    $(".small_virtual_server_table tr").removeClass("active");
    if ($(this).is(":checked")) {
      current_val = $(this).val();
      overlay = "#vs_plan_overlay_" + current_val;
      $(overlay).show();
      $(this).parent().parent().addClass("active");

      month_total += parseInt($(this).parent().attr("month"));
      year_total += parseInt($(this).parent().attr("year"));
      $("#month_total").html("$" + month_total);
      $("#year_total").html("$" + year_total);

      // determine if you should use year or month total
      if ($("#order_month_section").css("display") == 'none') {
        $("#order_total").html("$" + (year_total + one_time));
        amount_saved = (month_total * 12) - year_total;
        $("#amount_saved").html("$" + amount_saved);
      } else {
        $("#order_total").html("$" + (month_total + one_time));
      }
      construct_order_line_item("order_plan", $(this).parent().attr("desc"), parseInt($(this).parent().attr("month")), parseInt($(this).parent().attr("year")));
    }
  });
  
  // Toggle copy billing address  
  $('#copy_billing_address').click(function() {
    if ($(this).is(":checked")) {
      $("input#virtualserver_bill_address").val($("input#virtualserver_address").val());
      $("input#virtualserver_bill_zip").val($("input#virtualserver_zip").val());
      $("input#virtualserver_bill_city").val($("input#virtualserver_city").val());
      $("input#virtualserver_bill_state").val($("input#virtualserver_state").val());
      $("#virtualserver_bill_country").val($("#virtualserver_country").val());
    } else {
      $("input#virtualserver_bill_address").val("");
      $("input#virtualserver_bill_zip").val("");
      $("input#virtualserver_bill_city").val("");
      $("input#virtualserver_bill_state").val("");
      $("#virtualserver_bill_country").val("United States");
    }
  });
  
  // Domain
  $("input[name='virtualserver[own_domain]']").click(function() {
    $("#vs_domain_wrap span").removeClass("active");
    if ($(this).is(":checked")) {
      $(this).parent().next().addClass("active");
      if ($(this).parent().attr("yearly")) {
        one_time += parseInt($(this).parent().attr("yearly"));

        // determine if you should use year or month total
        if ($("#order_month_section").css("display") == 'none') {
          $("#order_total").html("$" + (year_total + one_time));
        } else {
          $("#order_total").html("$" + (month_total + one_time));
        }
        construct_one_time_order_line_item("order_domain", "Domain Registration (1yr)", parseInt($(this).parent().attr("yearly")));

        // if domain name entered, lets check it
        if (!($("input#virtualserver_domain").val() == '')) {
           // Perform call to api   
        }
      } else {
        if ($("#order_domain").length) {
          one_time -= 12;
          // determine if you should use year or month total
          if ($("#order_month_section").css("display") == 'none') {
            $("#order_total").html("$" + (year_total + one_time));
          } else {
            $("#order_total").html("$" + (month_total + one_time));        
          }
          $("#order_domain").parent().parent().remove();
          load_vs_order_line_items();
        }
      }
    }
  });
  
  // Distro
  $("input[name='virtualserver[distro]']").click(function() {
    $("#vs_distro_wrap span").removeClass("active");    
    if ($(this).is(":checked")) {
      $(this).parent().next().addClass("active");
    }
  });
  
  // Control Panel
  $("input[name='virtualserver[control_panel]']").click(function() {
    $("#vs_cp_wrap span").removeClass("active");
    if ($(this).is(":checked")) {
      if ($(this).attr("monthly")) {
        month_total += parseInt($(this).attr("monthly"));
        year_total += parseInt($(this).attr("yearly"));
        $("#month_total").html("$" + month_total);
        $("#year_total").html("$" + year_total);

        // determine if you should use year or month total
        if ($("#order_month_section").css("display") == 'none') {
          $("#order_total").html("$" + (year_total + one_time));
        } else {
          $("#order_total").html("$" + (month_total + one_time));
        }
        construct_order_line_item("order_cp", "cPanel <span style='font-size:12px !important;'>(Note: 20% yearly discount does not apply to add-on services)</span>",  parseInt($(this).attr("monthly")), parseInt($(this).attr("yearly")));
      } else {
        // if we had cpanel, lets remove it
        if ($("#order_cp").length) {
          month_total -= 10;
          year_total -= 96;
          $("#month_total").html("$" + month_total);
          $("#year_total").html("$" + year_total);

          // determine if you should use year or month total
          if ($("#order_month_section").css("display") == 'none') {
            $("#order_total").html("$" + (year_total + one_time));
          } else {
            $("#order_total").html("$" + (month_total + one_time));        
          }
          $("#order_cp").parent().parent().remove();
          load_vs_order_line_items();
        }
        
      }
      
      $(this).parent().next().addClass("active");
    }
  });
  
  // Billing Info
  $("input[name='virtualserver[bill_type]']").click(function() {
    $("#vs_bill_type_wrap span").removeClass("active");
    if ($(this).is(":checked")) {
      $(this).parent().next().addClass("active");
    }
  });
  
  // Bill Term
  $("input[name='virtualserver[bill_term]']").click(function() {
    $("#vs_bill_wrap span").removeClass("active");
    $("#order_year_section").hide();
    $("#order_month_section").hide();
    $("#order_saved").hide();
    
    if ($(this).is(":checked")) {
      $(this).parent().next().addClass("active");
      if ($(this).val() == "monthly") {
        $("#order_year_section").hide();
        $("#order_month_section").show();
        $("tr.vs_form_line_item td .year_price").hide();
        $("tr.vs_form_line_item td .month_price").show();
        $("#order_total").html("$" + (month_total + one_time));
      } else {
        $("#order_year_section").show();
        $("#order_month_section").hide();
        $("#order_saved").show();
        $("tr.vs_form_line_item td .year_price").show();
        $("tr.vs_form_line_item td .month_price").hide();
        amount_saved = (month_total * 12) - year_total;
        $("#amount_saved").html("$" + amount_saved);
        $("#order_total").html("$" + (year_total + one_time));
        
      }
    }
  });
  
  // Hide Distro and CP when Stack is selected
  $("input[name='virtualserver[stack]']").click(function() {
     if ($(this).val() == "none") {
       $("tr#vs_d_wrapper").show();
       $("tr#vs_cp_wrapper").show();
     } else {
       $("tr#vs_d_wrapper").hide();
       $("tr#vs_cp_wrapper").hide();
     }
  });
  
  $("input#virtualserver_domain").blur(function() {
    // Domain check
    if ($("input#virtualserver_own_domain_false").is(":checked")) {
      if ($("input#virtualserver_domain").val() != "") {
        var domain_check = $("input#virtualserver_domain").val();
        var domain_url = "/virtualservers/api/domain?domain=" + domain_check;
        var id_message = "#" + $("input#virtualserver_domain").attr("id") + "_extra_error";
        $(id_message).html("Please wait while we check this domain...").show().css("margin-left", "275px").css("color", "#333");
        var d_error_id = "#" + $("input#virtualserver_domain").attr("id") + "_error";
        $.get(domain_url, function(data){       
          if (data == "false") {
            $(d_error_id).show().css("margin-top", "25px");
            $("input#virtualserver_domain").addClass("error");
            $(id_message).html("Error! That domain is not availble").css("margin-left", "275px").css("color", "red");
            ready = false;
          } else {
            $(d_error_id).hide();
            $("input#virtualserver_domain").removeClass("error");
            $(id_message).html("Domain Available!").css("margin-left", "275px").css("color", "green");
          }
        });
      }
    }
  });
  
  $("input#virtualserver_username").blur(function() {
    // Check if username is available
    if ($("input#virtualserver_username").val() != "") {
      var user_check = $("input#virtualserver_username").val();
      var id_user_message = "#virtualserver_username_extra_error";
      $(id_user_message).html("Checking username").show().css("margin-left", "100px").css("color", "#333");
      var user_check_url = "/virtualservers/api/user_check?user=" + user_check;
      var user_error_id = "#virtualserver_username_error";
      $.get(user_check_url, function(data) {
        if (data == "true") {
          $(user_error_id).hide();
          $("input#virtualserver_username").removeClass("error");
          $(id_user_message).html("Username Available!").css("margin-left", "100px").css("color", "green");
        } else {
          $(user_error_id).show().css("margin-top", "18px");
          $("input#virtualserver_username").addClass("error");
          $(id_user_message).html(data).css("margin-left", "100px").css("color", "#333");
        }
      });      
    }
  });
  
  $("input#virtualserver_own_domain_false").click(function() {
    if ($("input#virtualserver_domain").val() != "") {
      var domain_check = $("input#virtualserver_domain").val();
      var domain_url = "/virtualservers/api/domain?domain=" + domain_check;
      var id_message = "#" + $("input#virtualserver_domain").attr("id") + "_extra_error";
      $(id_message).html("Please wait while we check this domain...").show().css("margin-left", "275px").css("color", "#333");
      var d_error_id = "#" + $("input#virtualserver_domain").attr("id") + "_error";
      $.get(domain_url, function(data){      
        if (data == "false") {
          $(d_error_id).show().css("margin-top", "25px");
          $("input#virtualserver_domain").addClass("error");
          $(id_message).html("Error! That domain is not availble").css("margin-left", "275px").css("color", "red");
          ready = false;
        } else {
          $(d_error_id).hide();
          $("input#virtualserver_domain").removeClass("error");
          $(id_message).html("Domain Available!").css("margin-left", "275px").css("color", "green");
        }
      });
    }
  });
  
  $("input#virtualserver_own_domain_true").click(function() {
    var id_message = "#" + $("input#virtualserver_domain").attr("id") + "_extra_error";
    var d_error_id = "#" + $("input#virtualserver_domain").attr("id") + "_error";
    $(d_error_id).hide();
    $("input#virtualserver_domain").removeClass("error");
    $(id_message).html("");
  })
  
  
  // Form submission with validation
  $("form#new_virtualserver").submit(function() {
    
    var ready = true;
    var id = "";
    var dcheck = false;
    var ccheck = false;
    var bcheck = false;
    var tcheck = false;
    var pre_ready = true;
    
    // domain check
    var id_message = "#" + $("input#virtualserver_domain").attr("id") + "_extra_error";
    if ($(id_message).html() == "Error! That domain is not availble") {
      ready = false;
    }
    
    $("input[type='text']").each(function() {
      id = "#" + $(this).attr("id") + "_error";
      if ($(this).val() == '' && $(this).attr("id") != "vs_referral_other") {
        ready = false;        
        $(id).show();
        $(this).addClass("error");
      } else {
        $(id).hide();
        $(this).removeClass("error");
      }
    });
    $("input[type='password']").each(function() {
      id = "#" + $(this).attr("id") + "_error";
      if ($(this).val() == '') {
        ready = false;        
        $(id).show();
        $(this).addClass("error");
      } else {
        $(id).hide();
        $(this).removeClass("error");
      }
    });
    // Distro validation
    pre_ready = ready;
    $("input[name='virtualserver[distro]']").each(function() {
      id = "#virtualserver_distro_error";
      if ($(this).is(":checked")) {
        $(id).hide();
        dcheck = true;
        if (pre_ready) {
          ready = true;
        }
      } else {
        if (!(dcheck)) {
          ready = false;
          $(id).show();
        }        
      }
    });
    // Control Panel validation
    pre_ready = ready;
    $("input[name='virtualserver[control_panel]']").each(function() {
      id = "#virtualserver_control_panel_error";
      if ($(this).is(":checked")) {
        $(id).hide();
        ccheck = true;
        if (pre_ready) {
          ready = true;
        }
      } else {
        if (!(ccheck)) {
          ready = false;
          $(id).show();
        }        
      }
    });
    // Billing Type validation
    pre_ready = ready;
    $("input[name='virtualserver[bill_type]']").each(function() {
      id = "#virtualserver_bill_type_error";
      if ($(this).is(":checked")) {
        $(id).hide();
        bcheck = true;
        if (pre_ready) {
          ready = true;
        }
      } else {
        if (!(bcheck)) {
          ready = false;
          $(id).show();
        }        
      }
    });
    // Bill term validation
    pre_ready = ready;
    $("input[name='virtualserver[bill_term]']").each(function() {
      id = "#virtualserver_bill_term_error";
      if ($(this).is(":checked")) {
        $(id).hide();
        tcheck = true;
        if (pre_ready) {
          ready = true;
        }
      } else {
        if (!(tcheck)) {
          ready = false;
          $(id).show();
        }        
      }
    });
    // Email Confirmation
    if (!($("input#virtualserver_email").val() == $("input#virtualserver_email_confirmation").val())) {
      ready = false;
      $("input#virtualserver_email").addClass("error");
      $("input#virtualserver_email_confirmation").addClass("error");
      $("#virtualserver_email_error").show();
      $("#virtualserver_email_confirmation_error").show();
    }
    // Password Confirmation
    if (!($("input#virtualserver_password").val() == $("input#virtualserver_password_confirmation").val())) {
      ready = false;
      $("input#virtualserver_password").addClass("error");
      $("input#virtualserver_password_confirmation").addClass("error");
      $("#virtualserver_password_error").show();
      $("#virtualserver_password_confirmation_error").show();
    }
    // Accept policy?
    if ($("input#virtualserver_accepted_policy").is(":checked")) {
      $("#virtualserver_accepted_policy_error").hide();
      $("label#vs_policy").removeClass("error");
    } else {
      ready = false;
      $("#virtualserver_accepted_policy_error").show();
      $("label#vs_policy").addClass("error");
    }

    // if any errors, don't send form
    if (!(ready)) {
      // scroll back up automatically
      $("html, body").animate({scrollTop: '150px'}, 800);
      return false;
    }
  });
  
}

function construct_order_line_item(id, desc, month_price, year_price) {
  var element_first = "<tr class='vs_form_line_item'><td>";
  var element_last = "<div class='clear'></div></td></tr>";
  
  var item = element_first + "<div id='" + id + "' class='desc'>" + desc + "</div><div class='price month_price'>$" + month_price + " / Monthly</div><div class='price year_price'>$"+ year_price + " / Yearly</div>" + element_last;
  $("table#vs_form_line_items").append(item);
  
  // show year or month price
  if ($("#order_month_section").css("display") == 'none') {
    $("tr.vs_form_line_item td .year_price").show();
    $("tr.vs_form_line_item td .month_price").hide();
  } else {
    $("tr.vs_form_line_item td .year_price").hide();
    $("tr.vs_form_line_item td .month_price").show();
  }
  
  load_vs_order_line_items();
}

function construct_one_time_order_line_item(id, desc, price) {
  var element_first = "<tr class='vs_form_line_item'><td>";
  var element_last = "<div class='clear'></div></td></tr>";
  
  var item = element_first + "<div id='" + id + "' class='desc'>" + desc + "</div><div class='price'>$" + price + "</div>" + element_last;
  $("table#vs_form_line_items").append(item);  
  load_vs_order_line_items();
}

function load_vs_order_line_items() { 
  $("tr.vs_form_line_item").css("background-color", "#fff");
  $("tr.vs_form_line_item:even").css("background-color", "#f5f5f5");
  $("tr.vs_form_line_item:first td").css("border-top", "1px solid #e4e6e6");
}

function load_ent_form() {
  $("form#new_enterprise_quote").submit(function() {

    var ready = true;
    var pre_ready = ready;
    var check = false;
    $("input[type='text']").each(function() {
      id = "#" + $(this).attr("id") + "_error";
      if ($(this).val() == '') {
        var exp1 = /_other/;
        var match1 = $(this).attr("name").search(exp1);
        if (match1 < 0) {
          ready = false;
          $(id).show();
          $(this).addClass("error");
        }
      } else {
			  $(id).hide();
	      $(this).removeClass("error");	        
      }
    });


    if (!(ready)) {
      $("html, body").animate({scrollTop: '650px'}, 800);
      return false;
    }

  });

}


function load_wh_tooltip() {
  
  $(".dialog").overlay({
    top: 65,
    mask: {
     color: '#333',
     loadSpeed: 15,
     opacity: 0.5
    },
    closeOnClick: true
  });
  
  $(".dialog").parent().click(function() {
    $(this).children().overlay().load();
  });  
  
}


function load_fancybox() {
  $("a.fancybox_lnk").fancybox();
}

function load_photo_gallery_overlay() {
  
  $("#image_viewer").overlay({
    top: 65,
    mask: {
      color: '#333',
      loadSpeed: 15,
      opacity: 0.5
    },
    closeOnClick: false
  });

  $("a#add_image").click(function() {
    $("#image_viewer").overlay().load();
    $("#gal_content").html("<img src='/images/content/ajax-loader.gif' />");
    $("#gal_content").load('/admin/files/list');
    return false
  });
}






















