/*
	***********************************************************************************
	
	packyourbags.com Javascript File
	
	Competition Form Validation	// November 12 2009 //
	
	***********************************************************************************
	
	jQuery code by Richard Shepherd
	
	Code is commented, so take a peak and see what
	you can find :)

	***********************************************************************************
*/

// Set our global variables
var form_errors = '';
var interests = '';
$(document).ready(function() {

	
}); // End of $(document).ready(function()


function validate_signup() {

	// reset the form errors string
	form_errors = '';
	
	// call our validation functions
	validate_name();
	validate_email();
	
	// Generate the Salutation Text	
	F_FirstName = ($('#FirstName').val()).substr(0,1);
	F_rest = ($('#FirstName').val()).substr(1) ;	
	F_FirstName = F_FirstName.toUpperCase();
	F_rest = F_rest.toLowerCase();

	FirstName = F_FirstName + F_rest;
	$('#FirstName').val(FirstName);

	L_LastName = ($('#LastName').val()).substr(0,1);
	L_rest = ($('#LastName').val()).substr(1) ;	
	L_LastName = L_LastName.toUpperCase();
	L_rest = L_rest.toLowerCase();

	LastName = L_LastName + L_rest;
	$('#LastName').val(LastName);
						 
	greeting = 'Hi ' + FirstName + ',';
	$('#Greeting').val(greeting);
		
	// The user must tell us the code
	if ($('#entrycode').val() == '') {
		form_errors += 'Please enter the competition entry code. \n';		
	}
	
	// Check if they've got the right code. Sure, they can hack this
	// javascript file, but it's really not that hard to enter anyway!
	if (($('#entrycode').val().toLowerCase() != 'mincepies') &&	($('#entrycode').val().toLowerCase() != 'snowman') && ($('#entrycode').val().toLowerCase() != 'xmastree') && ($('#entrycode').val().toLowerCase() != 'mistletoe') && ($('#entrycode').val().toLowerCase() != 'reindeer') && ($('#entrycode').val().toLowerCase() != 'northpole')) {
		form_errors += 'The entry code is incorrect! \n';		
	}

	var compstring = ($('#entrycode').val()) + ' ' + ($("input[name='comp-options']:checked").val());
			
	// alert('interests: ' + interests); // For debugging only.
	$('#Additional1').val(compstring);
		
	// return our results
	if (form_errors != '') {
		// prepare to display the errors
		alert(form_errors);
		return false;
	} else {
		// all is well, so display the loading graphic
		$("#loading").css("display", "block");
		// and submit the form!
		return true;
	}
	
} // End of validate_signup() 


function validate_name() {

	// Check that they've entered a first and last name
	if (($('#FirstName').val() == '') || ($('#LastName').val() == '')) {
		form_errors += 'Please enter a first and last name. \n';		
	}
	
} // End of validate_name()


function validate_email() {
	
	var email = $("#email").val();		 
	if(email != 0) {
		if(isValidEmailAddress(email)) {		 
			$("#validEmail").css({ "background-image": "url('css/validYes.png')" }); 
		} else {		 
			$("#validEmail").css({ "background-image": "url('css/validNo.png')" });	
			form_errors += 'Please enter a valid email address. \n';						
		}	 
	} else {			 
		$("#validEmail").css({ "background-image": "none" });		 
		form_errors += 'Please enter your email address. \n';						
	}
	
} // End of validate_email() 


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);
	
} // End of isValidEmailAddress(emailAddress)
