/* 
	| Main Functions For Javascript
	| Includes the following
	| @sfHover functions for sub nav
	| @eVal for email validation
	| @jQuery functions
*/

/* *** @sfHover *** */

sfHover = function() {
	var sfEls = document.getElementById("top-nav").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);

/* *** @eVal *** */

function e_val(e) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   return (!reg.test(e)) ? false : true;
}

/* *** @jQuery *** */

$(document).ready(function() {
	/* *** Input onFocus *** */

	$('input').focus(function() {
		title = $(this).attr('title');

		if(title != '') {
			if($(this).attr('value') == title) $(this).attr('value', '');
		}
	});

	/* *** Input onBlur *** */

	$('input').blur(function() {
		title = $(this).attr('title');

		if(title != '') {
			if($(this).attr('value') == '') $(this).attr('value', title);
		}
	});

	/* *** Email form validation and submission *** */
	$('#email-gather').submit(function() {
		var title = $('#email-gather input').attr('title');
		var value = $('#email-gather input').attr('value');
		var e = false;
		var m = '<h2 class="error">Error</h2><p>Please enter a valid email address</p>';

		if(title == value || value == '' || !e_val(value)) e = true;

		if(e) {
			$.facebox(m);
		} else {
			$.ajax({
			   type: "POST",
			   url: "app/add_email.php",
			   data: "email=" + value,
			   success: function(msg){
			     $.facebox('<h2 class="success">Success</h2><p>Your email has been added</p>');
				 $('#email-tool input').attr('value', title);
			   }
			 });

		}

		return false;
	});
	
	/* *** Contact Form *** */
	
	$('#contact-form').submit(function() {
		
		var full_name = $('#contact-form #full_name').attr('value');
		var phone = $('#contact-form #phone_number').attr('value');
		var email_address = $('#contact-form #email_address').attr('value');
		var comments = $('#contact-form #comments').attr('value');
		
		var e = false;
		var m = '<p><strong>The Following Errors Were Found</strong></p><ul>';
		
		if(full_name == '') { e = true; m += '<li>Full Name</li>'; }
		if(phone == '') { e = true; m += '<li>Phone Number</li>'; }
		if(email_address == '') { e = true; m += '<li>Email Address</li>'; }
		if(comments == '') { e = true; m += '<li>Comments / Questions</li>'; }
		
		m += '</ul>';
		
		if(e) {
			$.facebox(m);
		} else {
			$('.sending').fadeIn('slow');
			
			$.ajax({
				type: "POST",
				url: "app/send-mail.php",
				data: 'full_name=' + full_name + '&phone=' + phone + '&email_address=' + email_address + '&comments=' + comments,
				success: function(msg){
					$('.sending').fadeOut('fast');
					$('.success').fadeIn('slow');
				}
			});
			
			$('#contact-form #full_name').attr('value', '');
			$('#contact-form #phone_number').attr('value', '');
			$('#contact-form #email_address').attr('value', '');
			$('#contact-form #comments').attr('value', '');
		}
		
		
		return false;
	});
});