// JavaScript Document
function formValidate(){
	// Reset error messages and error highlighting (bg color)
	$('p.error').remove();
	$('.required').css('background-color','#FFF');

	// Begin checking

	var hasErrors = false;

	// Loop on each field
	$('.required').each(
		function() {

			if( $(this).val() == '' ) {
				$(this).prev('label').before('<p class="error">You must enter a value for '+ $(this).attr( $(this).attr('title') != '' ? 'title' : 'name' )+'.</p>');
				hasErrors = true;
			}
			else if ( $(this).attr('name') == 'company_short_name' ) {
				if ( !isValidShortName($(this).val())) {
					$(this).prev('label').before('<p class="error">Short names must contain only lowercase letters, numbers, underscores, and hyphens.</p>');
					hasErrors = true;
				} else if ( $(this).val().legnth <= 50 ) {
					$(this).prev('label').before('<p class="error">Your Company Short Name must be less than 50 characters.</p>');
					hasErrors = true;
				}
			}
			else if ( $(this).attr('name') == 'email' ) {
				var email = $(this).val();
				if ( !isValidEmail(email) ) {
					$(this).prev('label').before('<p class="error">You must enter a valid email address.</p>');
					hasErrors = true;
				}
			}
			else if ( $(this).attr('name') == 'user_password' ) {
				if ( !isValidPassword($(this).val())) {
					$(this).prev('label').before('<p class="error">You must enter a valid password. See <a href="http://preview.squishlist.com/help/Squish_II.htm#Password.htm" target="_blank">Squish Help</a> for details.</p>');
					hasErrors = true;
				} else if ( !verifyPassword($(this).val(), $('#id_password_verify').val())) {
					$(this).prev('label').before('<p class="error">Your passwords do not match. Please re-enter your password and password verficication.</p>');
					hasErrors = true;
				}
			}//end-else-if

			// Add highlight (background color) if errors
			if (hasErrors) $(this).css('background-color','#FFC0C0');

		}//end-function
	);

	// If errors found, print error warning and put focus at beginning of form
	if ( hasErrors )
	{
			$('form').prepend('<a id="review" href="#" title="Please review the form for errors."></a>');
			$('#review').focus();
			return false;
	}

	return true;
}

function isValidEmail(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 isValidPassword(password) {
	var i = 0;
	i += password.search(/[A-Z]/) < 0 ? 0 : 1;
	i += password.search(/[a-z]/) < 0 ? 0 : 1;
	i += password.search(/[0-9]/) < 0 ? 0 : 1;
	i += password.search(/[^0-9A-Za-z]/) < 0 ? 0 : 1;
	return i >= 2 && password.length >= 8;
}

function isValidShortName(shortName) {
	return shortName.search(/^[a-z0-9_\-]+$/) >= 0;
}

function verifyPassword(password1, password2) {
	return password1 == password2;
}