var involvedError = false;
$(document).ready(function(){
	$("form:has(.required)").submit(function(ev){
		
		ev.stopImmediatePropagation();
		var textFieldError = false;
		var selectError = false;
		var radioError = false;
		var emailError = false;
		var radioNames = Array();
		var checkboxError = false;
		
		
		$("input.email").each(function(){
			var email = $(this).val();
			if (email != "" && !email.match(/^[-a-z0-9~!$%^&*_=+}{\'?]+(\.[-a-z0-9~!$%^&*_=+}{\'?]+)*@([a-z0-9_][-a-z0-9_]*(\.[-a-z0-9_]+)*\.(aero|arpa|biz|com|coop|edu|gov|info|int|mil|museum|name|net|org|pro|travel|mobi|[a-z][a-z])|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,5})?$/i)) emailError = true;
		});
		
		$("input[type=text].required:visible").each(function(){
			if ($(this).val() == "" && !textFieldError) {
				if ($(this).attr("rel") == undefined) textFieldError = "Please enter the " + $(this).attr("name") + ".";
					else textFieldError = $(this).attr("rel");
			}
		});
		
		$("select.required:visible").each(function(){
			if ($(this).val() == "" && !selectError) {				
				if ($(this).attr("rel") == undefined) selectError = "Please select an option from the dropdown.";
				 else selectError = $(this).attr("rel");
			}
		});
		
		if ($("input[name=involved[]]:checked").length == 0 && $("input[name=involved[]]:visible").length > 0 ) involvedError = true;
			else involvedError = false;
		
		$("input[type=radio].required:visible").each(function(){
			var name = $(this).attr("name");
			if ($.inArray(name, radioNames) == -1) radioNames.push(name);
		});
		$.each(radioNames, function(key, value){
			var checked = 0;
			$("input[type=radio][name=" + value + "]").each(function(){
				if ($(this).is(":checked")) checked++;
			});
			if (checked == 0 && !radioError) {
				if ($("input[type=radio][name=" + value + "]").attr("rel") == undefined) radioError = "Please select a " + value + ".";
				else radioError = $("input[type=radio][name=" + value + "]").attr("rel");
			}
		});
		
		if (emailError) {
			$("input.email[value=]").css("background", "#ffcccc");
			alert("Please enter a valid email address.");
			return false;
		}
					
		if (textFieldError || selectError || radioError || checkboxError || involvedError) {
			highlightErrors();
			alert("Please fill in all required fields.");
			return false;
			
		}	
		
		
		if ($(this).hasClass("subscriber-form")) {
			
			if ($(".email_1").val() != $(".email_2").val()) {
				alert("Email addresses don't match.");
				return false;
			}
			
			$.post("inc/ajax.php", { action: "add-subscriber", data: $(".subscriber-form").serialize() }, function(){
				$("#popupWindow .inner").html('<h2><a style="color: steelblue;" href="?action=article&amp;id=8495">Thank you for your submission</a></h2><p><span style="font-family: arial,helvetica,sans-serif; font-size: small;">Thank-you for registering&nbsp;for registering to&nbsp;North American Clean Energy Magazine.&nbsp; Your subscription is almost complete!&nbsp; Please check your email for the verification link, and click on it.&nbsp;&nbsp;Once&nbsp;verified your subscription will be complete, and you\'ll receive your&nbsp;Subscriber ID.&nbsp; Please allow up to 24 hours to receive your&nbsp;email.&nbsp; &nbsp;</span><br/><br/><input type="button" value="Close" onclick="closeWindow()" />');
				 $('html, body').animate({scrollTop:0}, 'fast');
				return false;
			});
			
			return false;
		}
		
		if ($(this).hasClass("advertiser-form")) {
			
			if ($(".email_1").val() != $(".email_2").val()) {
				alert("Email addresses don't match.");
				return false;
			}
			
			$.post("inc/ajax.php", { action: "add-advertiser", data: $(".advertiser-form").serialize() }, function(){
				$("#popupWindow .inner").html('<h2><a style="color: steelblue;" href="?action=article&amp;id=12336">Thank-you for your Inquiry</a></h2><p><span style="font-family: arial,helvetica,sans-serif; font-size: small;">Thank-you for your interest in advertising with the North American Clean Energy Magazine. Your request is almost complete! Please check your email for the verification link, and click on it. Once your validation is complete, a representative will contact you regarding your submission.&nbsp; &nbsp;</span><br/><br/><input type="button" value="Close" onclick="closeWindow()" />');
				 $('html, body').animate({scrollTop:0}, 'fast');
				return false;
			});
			
			return false;
		}
		
		
	});
});

function highlightErrors() {
	if (involvedError) $(".involved").css("background", "#ffcccc");
	else $(".involved").css("background", "white");
	$("input.required").css("background", "white");
	$("select.required").css("background", "white");
	$("input.required[value=]").css("background", "#ffcccc");
	$("select.required[value=]").css("background", "#ffcccc");
}
