$(document).ready(function(){
	// subscription client validation
		// set up the vaules in the required form
	var formColours = Array();
		formColours[0]="#a23a6d";	// red text
		formColours[1]="#d4fbbf";	// light green form fill colour 
		formColours[2]="#f8dfeb"; 	// light red form fill colour
		formColours[3]="#000"; 		// black text

	var formMessages = Array();
		formMessages[0]="Name Required";	// name field msg
		formMessages[1]="Email Required";	// email field msg
		formMessages[2]="Confirmation Required";	// confirm email msg
		formMessages[3]="Please enure ALL mandatory fields are completed"; //error msg
		formMessages[4]="Invalid Email address"; // invalid email msg
		formMessages[5]="Only number required"; // msg for phone number
	var msgDuration = 2000; //display message duration eg 2000 = 2 secs 
	
	// hide the error message
	$("#errorMessage").hide();
	// call back error if the php validation fails then display erro rmessage
	<?php
	if($_GET['fail']=="yes"){
	?>
		$("#errorMessage").fadeIn('slow');
			// remove the message
		var pauseError=setTimeout(function(){$("#errorMessage").fadeOut('slow');}, msgDuration);
	<? } ?>
	
	
		// on load set the messages for the required fields
	$("input[name='name']").attr("value",formMessages[0]).css({"color":formColours[0]});
	$("input[name='email']").attr("value",formMessages[1]).css({"color":formColours[0]});
	$("input[name='emailConfirm']").attr("value",formMessages[2]).css({"color":formColours[0]});
	
		//client side from validation
		// check all required fields
		// the focus function
	$("#name,#email,,#emailConfirm").focus(function(){
			var selectedFocus = this.name;
			var focusMsgID;
			
			if (selectedFocus=="name"){
					focusMsgID=0;
				} else if (selectedFocus=="email"){
					focusMsgID=1;
				} else if (selectedFocus=="emailConfirm"){
					focusMsgID=2;
				};
			
			$(this).css({"background-color":formColours[1],"color":formColours[3]})
				if($(this).attr("value")==formMessages[focusMsgID]){
					$(this).attr("value","");
				}
			});
			
			// the blur function
	$("#name,#email,,#emailConfirm").blur(function(){
			var selectedBlur = this.name;
			var blurMsgID;
			if (selectedBlur=="name"){
					blurMsgID=0;
				} else if (selectedBlur=="email"){
					blurMsgID=1;
				} else if (selectedBlur=="emailConfirm"){
					blurMsgID=2;
				};
			
			if($(this).attr("value")==""){
				$(this).attr("value",formMessages[blurMsgID]).css({"color":formColours[0],"background-color":formColours[2]});
				} else if(selectedBlur=="emailConfirm"){
					if ($(this).attr("value")!=$("input[name='email']").attr("value")){
					$(this).attr("value",formMessages[blurMsgID]).css({"color":formColours[0],"background-color":formColours[2]});
					}
				}
				// check to see if a valid email address
			 if(selectedBlur=="email"){
			 	var theEmail = $("input[name='email']").attr("value");
				if(!validateEmail(theEmail)) {
						fadewarning(formMessages[4]);
					}
				}
				
			});
						
		// non required checks
		//focus function
	$("#organisation,#position,#phoneNumber").focus(function(){
			$(this).css({"background-color":formColours[1],"color":formColours[3]})
			});
		// blur function
	$("#organisation,#position,#phoneNumber").blur(function(){
		if($(this).attr("value")==""){$(this).css({"color":formColours[0],"background-color":formColours[2]});}
			});
			
	// check phone number feild to numbers only
	$("#phoneNumber").keypress(function (e){
	  //if the letter is not digit then display error and don't type anything
	  if( e.which!=8 && e.which!=0 && (e.which<48 || e.which>57)) {
		//display error message
		fadewarning(formMessages[5]);
		return false;
	  }
	});

		// the hover effect of the join btn
	$("#join").hover(function(){
		$(this).fadeTo('fast',0.5);
		}, function(){
		$(this).fadeTo('fast',1.0);
	});
	
		// submit checks if validations ok
	$("#join").click(function(){
		// second form of validation on submit
		var formError=false;
		var emailError = false;
		var theName = $("input[name='name']").attr("value");
		var theEmail = $("input[name='email']").attr("value");
		var confirmEmail = $("input[name='emailConfirm']").attr("value");
		
		if(theName=="" || theName==formMessages[0]){
			formError=true;
			} else if(theEmail=="" || theEmail==formMessages[1]){
			formError=true;
			} else if(confirmEmail=="" || confirmEmail==formMessages[2]){
			formError=true;
			} else if(theEmail != confirmEmail){
			formError=true;
		}
	
		// if all is ok now check to see if the e-mail is an email address
			if (!formError){
				if(!validateEmail(theEmail)) {
					emailError = true;
					formError=true;
				}
			}				
	
	
	if(formError){
			// form has erros display message and do not submit
			if (emailError){
				fadewarning(formMessages[4]);
			} else {
				fadewarning(formMessages[3]);
			}
			return false;
		} else {
				//submit form if all ok
			return true;
			}	
			
	
	});

function validateEmail(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 fadewarning(theMessage){
			// display out message
		$("#message").fadeIn('slow').html('<p class="messageInfo">'+ theMessage + '</p>');
			// remove the message after 3 seconds
		var pause=setTimeout(function(){$("#message p").fadeOut('slow');}, msgDuration);
		}
	// end of subscription client validation


	});// End