var sections = new Array("about", "contact");

$(document).ready(function () {
  var urlArray = new String(window.location).split("#");

  if (urlArray && urlArray.length == 2) {
    expand(urlArray[1]);
  }
});

expand = function(pSectionName) {
  for (var i = 0; i < sections.length; i++) {
    if (sections[i] === pSectionName) {
      $("div#section-" + pSectionName).css("display", "block");
    }
    else {
      $("div#section-" + sections[i]).css("display", "none");
    }
  }

  if (pSectionName === "contact") {
    $("input#name").focus();
  }

  window.location = "#" + pSectionName;
};

buttonHover = function(frm, btn) {
  $("#contactButton").css("background-color", "6aa994");
  $("#contactButton").css("cursor", "pointer");
};

buttonUnhover = function (frm, btn) {
  $("#contactButton").css("background-color", "568C79");
  $("#contactButton").css("cursor", "auto");
};

validateForm = function() {
  var isValid = true;

  if (jQuery.trim($("input#name").val()) === "") {
    isValid = false;
    $("#name_error").text("This field is required");
  } else {
    $("#name_error").text("");
  }

  var email = jQuery.trim($("input#email").val());
  if (email === "") {
    isValid = false;
    $("#email_error").text("This field is required");
  } else if (!isEmailValid(email)) {
    isValid = false;
    $("#email_error").text("Email address is invalid");
  } else {
    $("#email_error").text("");
  }

  if (jQuery.trim($("textarea#message").val()) === "") {
    isValid = false;
    $("#message_error").text("This field is required");
  } else {
    $("#message_error").text("");
  }

  return isValid;
};

isEmailValid = function(email) {
  var regex = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
  return email.match(regex)
};
