// Standard Phase Form
// Fields that will be validated: Name, Surname, Email
// Requirements:	1. A form with id="form" and submit button with id="submit"
// 					2. An empty div with id="formerror" for error messages
//					3. A div with id="formmessage" that contains the thankyou message
//					4. Validated form fields should have the correct "name"s ("Name","Surname" and "Email")
//					5. Validated labels should have the correct "for" attributes (""Name","Surname" and "Email")
var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;


$(document).ready(function(){
	
	$("#formmessage").hide();
	
	$("#submit").click(function(){		// Submit Button ID			   				   
		$('#forms2').find('label').removeClass('red');
		$("#formerror").slideUp();
		
		var hasError = false;
		
		var nameVal 	= $("input[name='Name']").val();
		var surnameVal 	= $("input[name='Surname']").val();
		var emailVal 	= $("input[name='Email']").val();
		
		if(nameVal == '') {
			$("label[for='Name']").addClass('red');
			hasError = true;
		}
		if(emailVal == '') {
			$("label[for='Email']").addClass('red');
			hasError = true;
		} else if(!emailReg.test(emailVal)) {
			$("label[for='Email']").addClass('red');
			hasError = true;
		}
		
		if(hasError == true) {
			$("#formerror").html('Please correct the highlighted fields and try again.').slideDown();
		}
		else {
			$(this).hide();
			var formaction = $("#forms2").attr('action');	// Form ID
			var regex = new RegExp("[^0-9]*");
			formaction = formaction.replace(regex,"");
			formaction = 'scripts/submitform.asp?formid=' + formaction;
			
			$("#submit").after('<img src="images/loading.gif" alt="" width="16" height="16" id="loading" style="margin:2px 0px 0px 3px;"/> Processing...');		
			
			$.post(formaction,
					$("#forms2").serialize(),				// Form ID again
   					function(data){
						$('#forms2').hide(1300);
						$("#formmessage").show(1000);
					}
			);
		}
		
		return false;
	});						   
});