/*
	***********************************************************************************
	
	packyourbags.com Javascript File
	
	Email Signup & Form Validation	// September 17 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 regions = '';
$(document).ready(function() {
	
	// Hide all our instructions
	$(".form-instructions").css("display", "none");

	// Hover over effects for text inputs
	$(".text-input").focus(function() {
		$(this).parent().parent().addClass("yellow");
		$(this).parent().parent().parent().children(".form-instructions").css("display", "block");
	});
	$(".text-input").blur(function() {
		$(this).parent().parent().removeClass("yellow");
		$(this).parent().parent().parent().children(".form-instructions").css("display", "none");		
	});

	// Hover over effects for check boxes
	$(".check-input").focus(function() {
		$(this).parent().parent().parent().addClass("yellow");
	});
	$(".check-input").blur(function() {
		$(this).parent().parent().parent().removeClass("yellow");
	});

	// Display instructions when hover or selected
	$(".form-elements").hover(function() {
		$(this).children(".form-instructions").css("display", "block");
	}, function() {
		$(this).children(".form-instructions").css("display", "none");
	});	
	
	// As the user types in their email, continuously validate it
	$("#email").keyup(function(){
	   validate_email();
	});

	
}); // End of $(document).ready(function()


function validate_signup() {

	// reset the form errors string
	form_errors = '';
	regions = '';
	
	// call our validation functions
	validate_name();
	validate_email();
	validate_dob();

	// Now we need to populate our country preferences
	// based on checkbox input. We need to do this to make
	// sure if the user doesn't check a checkbox we still
	// feed in the value of FALSE rather than empty	
	if ($('#Europe-checkbox').is(':checked')) { regions += 'Europe '; }
	if ($('#Asia-checkbox').is(':checked')) { regions += 'Asia '; }
	if ($('#Caribbean-checkbox').is(':checked')) { regions += 'Caribbean '; }
	if ($('#SouthAmerica-checkbox').is(':checked')) { regions += 'SouthAmerica '; }
	if ($('#USA-checkbox').is(':checked')) { regions += 'USA '; }
	if ($('#NorthAfrica-checkbox').is(':checked')) { regions += 'NorthAfrica '; }
	if ($('#MiddleEast-checkbox').is(':checked')) { regions += 'MiddleEast '; }

	// alert('Regions: ' + regions);
	$('#Regions').val(regions);

	// 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);
						 
	salutation_text = 'Hi ' + FirstName + ',';
	$('#Greeting').val(salutation_text);
	//alert(salutation_text);

	// The user must choose at least one destination
	if (regions == '') {
		form_errors += 'Please enter at least one destination. \n';		
	}
	
	// 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 validate_dob() {
	
	day = $('#day').val();
	month = $('#month').val();
	year = $('#year').val();

	if ((day != '--') && (month != '--') && (year != '--')) {		
		date = day + '/' + month + '/' + year;
		$('#DOB').val(date);	
	} else if ((day == '--') && (month == '--') && (year == '--')) {
		// they've left the date empty, so leave them well alone
		$('#DOB').val('');			
	} else {
		form_errors += 'Please fill in all the birthday fields. \n';								
	}
	
} // End of validate_dob()


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)
