jQuery(document).ready(function($){

	//Email Obfuscation
	$(".replaceAt").replaceWith("@");
  	$(".obfuscate").each(function () {
  		$(this).attr("href", "mailto:"+$(this).text());
  		thisrel = $(this).attr("rel");
  		if (thisrel) $(this).text(thisrel);
  	});

	//Form Default Values
	$(".replacedefault input[type=text], .replacedefault textarea").bind('focus', function(){
		var currentValue = $(this).attr("value");
		var defaultValue = $(this).attr("defaultValue");
		if (currentValue == defaultValue) $(this).attr({ value: ""});
	});
	$(".replacedefault input[type=text], .replacedefault textarea").bind('blur', function(){
		var currentValue = $(this).attr("value");
		var defaultValue = $(this).attr("defaultValue");
		if (currentValue == '') $(this).attr({ value: defaultValue});
	});

	$('#contact').popupmenu({
		target: "#contactdropdown",
		time: 300,
		addStyle: 'contactover'
	});



	$("#askaquestionform").submit(function(e) {
		e.preventDefault();
		thisForm = document.askaquestionform;
		if (checkForm(thisForm,'askaquestionform')) {
			$.post("/wp-content/themes/nwtl/formsubmit.php", {
				type: "askaquestionform",
				name: thisForm.name.value,
				email: thisForm.email.value,
				question: thisForm.question.value
			}, function(data){ alert("Thank you for your question.\n\nWe will be in touch soon."); } );
			var t = setTimeout("thisForm.reset()", 1000);
		}
		return false;
	});
	$("#newslettersignupform").submit(function(e) {
		e.preventDefault();
		thisForm = document.newslettersignupform;
		if (checkForm(thisForm,'newslettersignupform')) {
			
			$.post("/wp-content/themes/nwtl/formsubmit.php", {
				type: "newslettersignupform",
				name: thisForm.name.value,
				email: thisForm.email.value,
				zip: thisForm.zip.value
			}, function(data){
				alert("Thank you so much for signing up!\n\nYou have been added to our mailing list.");
				thisForm.reset()
			});
		}
		return false;
	});
	


});

function checkForm(theForm,checkType){
	if (checkType == 'askaquestionform') {
		if(theForm.name.value == theForm.name.defaultValue) {
			alert("Please enter your name.");
			theForm.name.focus();
			return false;
		}
		if(theForm.email.value == theForm.email.defaultValue || !isValidEmailAddress(theForm.email.value)) {
			alert("Please enter a valid email address.");
			theForm.email.focus();
			return false;
		}
		if(theForm.question.value == theForm.question.defaultValue) {
			alert("Please enter your question.");
			theForm.question.focus();
			return false;
		}
		return true;
	} else if (checkType == 'newslettersignupform') {
		if(theForm.name.value == theForm.name.defaultValue) {
			alert("Please enter your name.");
			theForm.name.focus();
			return false;
		}
		if(theForm.email.value == theForm.email.defaultValue || !isValidEmailAddress(theForm.email.value)) {
			alert("Please enter a valid email address.");
			theForm.email.focus();
			return false;
		}
		return true;
	
	} else {
		return true;
	}
}

function isValidEmailAddress(emailAddress) {
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);
}

