function toggle_tab(tab) {
  $("#tabs").toggleClass("company");

  if (tab == "job") {
    $("#company_tab").css("display", "none");
    $("#job_tab").fadeIn("def");
  }
  else {
    $("#job_tab").css("display", "none");
    $("#company_tab").fadeIn("def");
  }
  return false;
}

var i_msg = 1;
var i_msg_length = new Array();
i_msg_length[1] = 6200;
i_msg_length[2] = 3700;
i_msg_length[3] = 5000;
i_msg_length[4] = 4000;
i_msg_length[5] = 3000;
i_msg_length[6] = 6500;


function animate_i_msg_on() {
  i_msg = (i_msg >= 6) ? 1 : i_msg + 1;
  offset = (i_msg-1) * -29;
//  alert($("#i_msgs").css("backgroundPosition"));
  $("#i_msgs").css("backgroundPosition", '0 '+offset+'px');
  $("#i_msgs").animate({width: "556px"}, "slow", "linear");

  setTimeout("animate_i_msg_off()",i_msg_length[i_msg]);
}

function animate_i_msg_off() {
  $("#i_msgs").animate({width: 0}, "slow", "linear", function () {animate_i_msg_on();});
}

function validate_form() {
  errors = false;
  errorMsg = "Please complete the following fields:\n";

  if($("#first_name").val() == "") {
    errors = true;
    errorMsg += "- First Name\n";
  }
  if($("#last_name").val() == "") {
    errors = true;
    errorMsg += "- Last Name\n";
  }
  if($("#age_range").val() == "") {
    errors = true;
    errorMsg += "- Age Range\n";
  }
  if($("#email_address").val() == "") {
    errors = true;
    errorMsg += "- Email\n";
  }
  if($("#city").val() == "") {
    errors = true;
    errorMsg += "- City\n";
  }
  if($("#state").val() == "") {
    errors = true;
    errorMsg += "- State / Province\n";
  }
  if($("#zip").val() == "") {
    errors = true;
    errorMsg += "- Zip / Postal Code\n";
  }
  
  if(errors) {
    alert(errorMsg);
    return false;
  }
  else {
    return true;
  }
}

$(document).ready(function () {
  $("#tab_job > a").bind("click", function() {
    return toggle_tab('job');
  });
  $("#tab_company > a").bind("click", function() {
    return toggle_tab('company');
  });
  $("#tab_locations > a").bind("click", function() {
    $("#locations_tab").slideToggle("def");
    return false;
  });
  $("#close").bind("click", function() {
    $("#locations_tab").slideToggle("def");
    return false;
  });
  
  setTimeout("animate_i_msg_off()",i_msg_length[1]);

  $("#home_phone1").bind("keyup", function (e) {
    if ((e.which != 9 && e.which != 16) && $("#home_phone1").val().length >= 3) {
      $("#home_phone2").select();
    }
  });
  $("#home_phone2").bind("keyup", function (e) {
    if ((e.which != 9 && e.which != 16) && $("#home_phone2").val().length >= 3) {
      $("#home_phone3").select();
    }
  });

  $("#cell_phone1").bind("keyup", function (e) {
    if ((e.which != 9 && e.which != 16) && $("#cell_phone1").val().length >= 3) {
      $("#cell_phone2").select();
    }
  });
  $("#cell_phone2").bind("keyup", function (e) {
    if ((e.which != 9 && e.which != 16) && $("#cell_phone2").val().length >= 3) {
      $("#cell_phone3").select();
    }
  });

  $("#apply_form").bind("submit", function () {
    if(validate_form()) {
      $("#apply_form").css("display", "none");
      $("#spinner").fadeIn("def");
      
      $.post("form_process.php", {
        process: "process",
        first_name: $("#first_name").val(),
        last_name: $("#last_name").val(),
        age_range: $("#age_range").val(),
        home_phone1: $("#home_phone1").val(),
        home_phone2: $("#home_phone2").val(),
        home_phone3: $("#home_phone3").val(),
        cell_phone1: $("#cell_phone1").val(),
        cell_phone2: $("#cell_phone2").val(),
        cell_phone3: $("#cell_phone3").val(),
        email_address: $("#email_address").val(),
        city: $("#city").val(),
        state: $("#state").val(),
        zip: $("#zip").val()
      }, 
      function () {
        $("#spinner").css("display", "none");
        $("#apply_thanks").fadeIn("def");
      });
    }
    
    return false;
  });
});