/* jQuery */
$(document).ready(function() {$("label.overlabel").overlabel();});


$(document).ready(function() {
	$("#submitted").click(function(){					   				   
		$(".error").hide();
		var hasError = false;
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		
		var email = $("#email").val();
		if(email == '') {
			$("#l_email").after('<div class="error">You forgot to enter the email address.</div>');
			hasError = true;
		} else if(!emailReg.test(email)) {	
			$("#l_email").after('<div class="error">Enter a valid email address.</div>');
			hasError = true;
		}
		
		var name = $("#name").val();
		if(name == '') {
			$("#l_name").after('<div class="error">You forgot to enter your name.</div>');
			hasError = true;
		}

		var department = $("#department").val();
		if(department == '') {
			$("#l_department").after('<div class="error">You forgot to choose the department.</div>');
			hasError = true;
		}
		
		var message = $("#message").val();
		if(message == '') {
			$("#l_message").after('<div class="error">You forgot to enter the message.</div>');
			hasError = true;
		}
		
		
		if(hasError == false) {
			$(this).hide();
			$("#form_contact div.buttons").append('<div class="loading"></div>');
			
			$.post("./form/form_contact_verify.php",
   				{ company: company, email: email, name: name, department: department, message: message, submit: submit },
   					function(data){
						$("#form_contact").slideUp("normal", function() {				   
							
							$("#form_contact").before('<h1>Success</h1><p>I will be in contact with you shortly. Until then, feel free to browse the rest of the site.</p>');											
						});
   					}
				 );
		}
		
		return false;
	});
});


/* jQuery: overlabel */
(function($){
$.fn.overlabel = function() {
    this.each(function(index) {
      var label = $(this);
      var name = this.htmlFor || label.attr('for');
      var label_content = label.text();
      var control = label.siblings("input[@name="+ name +"]:first");
      var password_input = "<input type='text' value='"+ label_content +"' name='"+ control.attr("name") +"' class='"+ control.attr("class") +" blur password' onfocus='$(this).prev().show().focus().end().hide();' />";
      label.hide();
      if(control.attr("type") == "password") control.after(password_input).hide();
      control.attr("value", label_content).toggleClass("blur").focus(function(){
        if(control.val() == label_content) control.attr("value", "").toggleClass("blur");
        if(control.attr("type") == "password" && control.next().is(".password")) control.next().remove();
      }).blur(function(){
        if(control.val() === ""){
          control.attr("value", label_content).toggleClass("blur");
          if(control.attr("type") == "password") control.after(password_input).hide();
        }
      }).parent("form").submit(function(){
        if(control.val() == label_content) control.attr("value", "").toggleClass("blur");
        if(control.attr("type") == "password" && control.next().is(".password")) control.show().next().remove();
      });
    });
}
})(jQuery);

