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

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

	//Form Default Values
	$(".replacedefault input[type=text], .replacedefault input[type=password], .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});
	});

	
	//Forgot Password Popup
	$('#forgotpasswordlink').click(function() {
		$("#logininfo").hide();
		$("#forgotpassworddone").hide();
		$("#forgotpassword").slideToggle("slow");
	});
   
	$("#newslettersignupform").submit(function() {
		var strName = document.newslettersignupform.name.value;
		var strEmail = document.newslettersignupform.email.value;
		if (strName == "Name") strName = '';
		if (strEmail == "Email") strEmail = '';
		
		if (isValidEmailAddress(strEmail)) {
			$.post("/wp-content/themes/afferoproject/addsubscriber.php", {
				username: strName,
				email: strEmail
			}, function(data) {
				$("#newsletterthankyou").jqm({opacity: 75}).jqmShow();
				document.newslettersignupform.reset();
			});
		
		} else {
			alert('Invalid Email Address');
		}
	});
   
   	$("#about").popupmenu({ target: "#aboutmenu", time: 300, addStyle: "abouthover" });

	$("#sendforgot").click(function() { sendrecoveryemail(document.loginform); });
	
	$("#nominateform").submit(function(e) {
		e.preventDefault();
		thisForm = document.nominateform;
		if (checkForm(thisForm,'nominateform')) {
			$.post("/wp-content/themes/afferoproject/formsubmit.php", {
				type: "nominateform",
				name: thisForm.name.value,
				email: thisForm.email.value,
				website: thisForm.website.value,
				phone: thisForm.phone.value,
				orgname: thisForm.orgname.value,
				orgemail: thisForm.orgemail.value,
				orgwebsite: thisForm.orgwebsite.value,
				orgphone: thisForm.orgphone.value,
				reasons: thisForm.reasons.value,
				category: thisForm.catlist.options[thisForm.catlist.selectedIndex].value
			}, function(data){
				$("#nominatethankyou").jqm({opacity: 75}).jqmShow();
				thisForm.reset();
				$(".footerpopup-pane").hide();
				$("#footerwrap ul li a").removeClass("selected");
				$("#footerpopup").hide();
			});
		}
		return false;
	});
	$("#feedbackform").submit(function(e) {
		e.preventDefault();
		thisForm = document.feedbackform;
		if (checkForm(thisForm,'feedbackform')) {
			$.post("/wp-content/themes/afferoproject/formsubmit.php", {
				type: "feedbackform",
				name: thisForm.name.value,
				email: thisForm.email.value,
				comments: thisForm.comments.value,
				reason: thisForm.reason.options[thisForm.reason.selectedIndex].value
			}, function(data){
				$("#feedbackthankyou").jqm({opacity: 75}).jqmShow();
				thisForm.reset();
				$(".footerpopup-pane").hide();
				$("#footerwrap ul li a").removeClass("selected");
				$("#footerpopup").hide();
			});
		}
		return false;
	});

	$("ul#footernav li a").click(function () {
		var elName = $(this).attr("id");
		if (elName == "partners") return true;
		if ($("#popup-"+elName).is(":visible")) {
			$(".footerpopup-pane").hide();
			$("#footerwrap ul li a").removeClass("selected");
			$("#footerpopup").fadeOut();
			return false;
		} else {
			$(".footerpopup-pane").hide();
			$("#footerwrap ul li a").removeClass("selected");
			$("#popup-"+elName).show();
			$(this).addClass("selected");
			$("#footerpopup").fadeIn();
		}
		return false;
	});
	$("a#closewindow").click(function () {
		$(".footerpopup-pane").hide();
		$("#footerwrap ul li a").removeClass("selected");
		$("#footerpopup").fadeOut();
		return false;
	});
	


	
});

function getHash() { var hash = window.location.hash; return hash.substring(1); }

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);
}

function stopDefAction(evt) {
	evt.preventDefault();
}

function checkForm(theForm,checkType){
	if (checkType == 'nominateform') {
		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.orgname.value == theForm.orgname.defaultValue) {
			alert("Please enter the organization's name.");
			theForm.orgname.focus();
			return false;
		}
		if(theForm.reasons.value == theForm.reasons.defaultValue) {
			alert("Please enter your reasons for nomination.");
			theForm.reasons.focus();
			return false;
		}
		if(theForm.catlist.selectedIndex == 0) {
			alert("Please select a category for this organization.");
			return false;
		}
		return true;
	
	} else if (checkType == 'feedbackform') {
		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 if (checkType == 'contactform') {
		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;
	}
}

function sendrecoveryemail(theForm) {
	if (!isValidEmailAddress(theForm.forgotemail.value)) {
		alert("Please Enter a Valid Email Address.");
		theForm.forgotemail.focus();
	} else {
		theEmail = theForm.forgotemail.value;
		jQuery.post("/reset-password/", {
			sendemail: "1",
			email: theEmail
		}, function(data){
			jQuery("#forgotpassword").slideToggle("slow", function() {
				theForm.reset();
				jQuery("#forgotpassworddone").html("An email with password recovery instructions has been sent to " + theEmail + ".");
				jQuery("#forgotpassworddone").fadeIn();
			});
		} );
		
	}
	
}

function loginformcheck(theForm) {
	if (theForm.forgotemail.value && theForm.forgotemail.value != theForm.forgotemail.defaultValue) {
		sendrecoveryemail(theForm);
		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;
}

