function extractId(longId)
{
  idInfo = longId.split("_");
  return idInfo[idInfo.length - 1];
}

function activateBar(color, defaultColor)
{
  $("#services_" + defaultColor).addClass("hidden");
  $("#services_" + color).removeClass("hidden");
  return false;
}

function deactivateBar(color, defaultColor)
{
  if (color != defaultColor)
    $("#services_" + color).addClass("hidden");
  $("#services_" + defaultColor).removeClass("hidden");
  return false;
}

$(document).ready( function()
{
  $("#container").corner();
  $(".framed").corner();
  $(".curve").corner();

  $(".header").click( function()
  {
    shortId = extractId( $(this).attr("id") );
    if ( $("#content_" + shortId).is(":visible") )
      $("#content_" + shortId).slideUp("slow");
    else
      $("#content_" + shortId).slideDown("slow");
  });

  defaultColor = $("#active_color").html();

  $(".nav_button").mouseover( function()
  {
    id = extractId($(this).attr("id"));
    activateBar(id, defaultColor);
    return false;
  });

  $(".nav_button").mouseout( function()
  {
    id = extractId($(this).attr("id"));
    deactivateBar(id, defaultColor);
    return false;
  });

  $(".service_bar").mouseover( function()
  {
    id = extractId($(this).attr("id"));
    activateBar(id, defaultColor);
    return false;
  });

  $(".service_bar").mouseout( function()
  {
    id = extractId($(this).attr("id"));
    deactivateBar(id, defaultColor);
    $(".sub-nav_menu-item").removeClass("sub-nav_active");
    return false;
  });

  $(".sub-nav_menu-item").mouseover( function()
  {
    id = extractId($(this).attr("id"));
    activateBar(id, defaultColor);
    $(".sub-nav_menu-item").removeClass("sub-nav_active");
    $(this).addClass("sub-nav_active");
    return false;
  });

  $(".sub-nav_menu-item").mouseout( function()
  {
    id = extractId($(this).attr("id"));
    deactivateBar(id, defaultColor);
    return false;
  });

  $(".capture").click( function()
  {
    longId = $(this).attr("id");
    idInfo = longId.split("|");
    shortId = idInfo[1];

    videoWindow = window.open ("http://www.postnetfranchise.com/video/play/" + shortId + "/", "window_" + shortId,"location=0,status=0,scrollbars=0,width=450,height=375");
    videoWindow.moveTo(500,400);
    
    return false;
  });

  $("#express_form").submit( function()
  {
    phoneRegex = /^[0-9]{10}$/;
    dirtyPhone = $("#phone").val();
    cleanPhone = dirtyPhone.replace(/[^0-9]/g, "");

    emailRegex = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
    email = $("#email").val();

    errors = "";

    if ( $("#first_name").val() == "" )
      errors += "Please enter your first name.<br />";
    if ( $("#last_name").val() == "" )
      errors += "Please enter your last name.<br />";
    if ( $("#phone").val() == "" )
      errors += "Please enter your phone number.<br />";
    else if ( ! phoneRegex.test(cleanPhone) )
      errors += "Please provide a valid phone number.<br />"; 
    if ( $("#email").val() == "" )
      errors += "Please enter your email address.<br />";
    else if ( ! emailRegex.test(email) )
      errors += "Please provide a valid email address.<br />";
    if ( $("#magicword").val() == "" )
      errors += "Please decipher the distorted text.<br />";

    $("#express_errors").html(errors);

    if (errors != "")
      return false;
  });

  $("#express_form_intl").submit( function()
  {
    phoneRegex = /^[0-9]{10}$/;
    dirtyPhone = $("#phone").val();
    cleanPhone = dirtyPhone.replace(/[^0-9]/g, "");

    emailRegex = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
    email = $("#email").val();

    errors = "";

    if ( $("#first_name").val() == "" )
      errors += "Please enter your first name.<br />";
    if ( $("#last_name").val() == "" )
      errors += "Please enter your last name.<br />";
    if ( $("#phone").val() == "" )
      errors += "Please enter your phone number.<br />";
    if ( $("#email").val() == "" )
      errors += "Please enter your email address.<br />";
    else if ( ! emailRegex.test(email) )
      errors += "Please provide a valid email address.<br />";
    if ( $("#magicword").val() == "" )
      errors += "Please decipher the distorted text.<br />";

    $("#express_errors").html(errors);

    if (errors != "")
      return false;
  });

});
