// The validateField(el) Function:		
// This function below is to validate the form fields
// that are found in account.php, upgrade-form.php, billing-info.php and
// confirmation.php, which are all in the create-account.php file.
		
// How it works: 
// Each text input or form field is set to catch if the user enters
// a value when their cursor is on the field, only if the field is visible or shown. 
// If they leave the field without giving a value, it will throw an error.
// Each case has a different error message depending upon the input field. 
// If a user puts in an incorrect value, the global functions at the bottom of this page 
// will check to see if the form field has a correct value or not.

/************************************************************************************
	UPDATED 6-23-10 (Jordan)

	The way that this function works has been modified.  This function now expects
	a global list to be populated that defines how each field should be validated.
	
	Each page that is added into the collection should add its fields to this list.
	
	var field_validators should be defined, with each entry being of the format:
	{
		name: '',						// Field name
		error_message: '',				// The error message to display
		validator: function() {},		// A function to call with the validator's value
		args: [],						// Additional values to pass to the validator
		mark_selector: function() {}	// See below
	}
	
	Each validator should return true or false to represent the validity of a field.
	
	In addition, each field will be checked to make sure that its value is not equal
	to its error message, since that may be valid data for a field but is not desired.
	
	The mark_selector function is called to get a reference to the required / invalid
	mark at the end of each field.  If it is supplied, it will be called with the element,
	the class selector (.required or .verified) and any args as its arguments, in that
	order.  Defaults to { return $(el).siblings(class_selector); }
	
	Any field that is passed into this function but which does not have an entry in
	field_validators will be assumed to have passed validation.
*************************************************************************************/
		
function validate_field(el){
	if (typeof(field_validators) == 'undefined' || field_validators == null) {
		return;
	}
	
	var valid = false;
	
	for (var i = 0; i < field_validators.length; i++) {
		validator_entry = field_validators[i];
		
		if (validator_entry.name == $(el).attr('name')) {			
			if (typeof(validator_entry.args) == 'undefined' || validator_entry.args == null) {
				validator_entry.args = [];	
			}
			
			valid = validator_entry.validator.call(this, [].concat([$(el).val()], validator_entry.args));
			
			if (validator_entry.error_message == $(el).val()) {
				valid = false;
			}
			
			if (valid) {
				mark_valid(el, validator_entry);
			} else {
				mark_invalid(el, validator_entry);
			}
			
			break;
		}
	}
}

// Adds the error class to the input and switches out the required mark for an invalid one.  Also sets the error message.
function mark_invalid(el, validator_entry) {
	var mark_element;
	
	if (validator_entry.mark_selector != undefined) {
		if (typeof(validator_entry.args) == 'undefined' || validator_entry.args == null) {
			validator_entry.args = [];	
		}
		
		// Call the mark selector with window scope, the element's value, the selector (required / verified), and any other arguments passed in
		mark_elment = validator_entry.mark_selector.call(this, [].concat([$(el).val(), '.verified'], validator_entry.args));
	} else {
		mark_element = $(el).siblings('.verified');
	}
	
	$(el).val(validator_entry.error_message);
	$(el).addClass('error');
	mark_element.removeClass('verified').addClass('required');
}

// Removes the error class from the input and switches out the invalid mark for a required one.
function mark_valid(el, validator_entry) {
	var mark_element;
	
	if (validator_entry.mark_selector != undefined) {
		if (typeof(validator_entry.args) == 'undefined' || validator_entry.args == null) {
			validator_entry.args = [];	
		}
		
		// Call the mark selector with window scope, the element's value and any other arguments passed in
		mark_elment = validator_entry.mark_selector.call(this, [].concat([$(el).val(), '.required'], validator_entry.args));
	} else {
		mark_element = $(el).siblings('.required');
	}
	
	$(el).removeClass('error');
	mark_element.removeClass('required').addClass('verified');
}

/************************************************************************************
	VALIDATOR LIST
	
	Each function here is a validator for a field, which should return true or false
	to indicate if a field is valid.  ALL validation must be contained within this
	function.  If a field can't be empty, put that here!
	
	That is, unless you want to specify an auxiliary function that in turn calls this.
	For example, you could pass function(val) { return not_blank(val) && validate_phone; }
	to anything expecting a validator.
	
	UPDATED 6-23-10 (Jordan)
	
	Modified these to have more consistent names with our internal coding style.
*************************************************************************************/

// not_blank
// Tests if a field is blank
function not_blank(val) {
	return (val != "");
}

// The validate_phone Function:
// This checks if the field value is a 10 digit phone number.
function validate_phone(val) {
	return /^[0-9]{10}$/.test(val);
}

// The validate_apt Function:
// Apartment numbers must be in the format of 301-1 or 301-A,
// with capital letters being required.
function validate_apt(val) {
	return /^[0-9]{3,4}\-[0-9,A-Z]$/.test(val);
}

// The validate_email Function:
// The email fields must have a value of myemail@domain.com, or .info.
// If domain naming conventions can exeed 4 characters, this will need
// to change from {2,4} to whatever character limit is necessary.
function validate_email(val) {
	return /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/.test(val);
}

// The validate_password Function:
// This checks to see that the password field has at least 6 characters.
function validate_password(val) {
	return /^([a-zA-Z0-9!@#$%^&*_\-+=.?~]){6,45}$/.test(val);
}

// The confirm_password Function:
// This checks the element in password_field to make sure that its value is the same as the passed value.
function confirm_password(val, password_field) {
	return val === $(password_field).val();
}

// The validate_zip Function:
// Since we are in the US, we only need 5 numbers.
function validate_zip(val) {
	return /^[0-9]{5}$/.test(val);
}

// The validate_state Function:
// Two character state code
function validate_state(val) {
	return /^[a-zA-Z]{2}$/.test(val);
}

// The validate_credit_card Function:
// Depending up on the credit card company, credit cards need to be
// between 15-16 numbers long. No need for dashes for this.
function validate_credit_card(val) {
	return /^((X{12}[0-9]{3,4})|([0-9]{15,16}))$/.test(val);	
}

// The validate_year Function:
// A year must be selected from the combobox.
function validate_year(val) {
	return /^[0-9]{2}$/.test(val);
}

// The validate_month Function:
// A month must be selected from the combobox.
function validate_month(val) {
	return /^[0-9]{2}$/.test(val);
}

// The validate_ccv Function:
// This checks to make sure the CCV code for a credit
// card is between 3-4 numbers.
function validate_ccv(val) {
	return /^[0-9]{3,4}$/.test(val);
}

// The validate_dob_month Function:
// This checks to make sure the date of birth month
// is 2 numbers.
function validate_dob_month(val) {
	if (val < 0 || val > 12) {
		return false;
	}
	
	return /^[0-9]{2}$/.test(val)	
}

// The validate_dob_day Function:
// This checks to make sure the date of birth day
// is 2 numbers.
function validate_dob_day(val) {
	if (val < 0 || val > 31) {
		return false;
	}
	return /^[0-9]{2}$/.test(val)	
}

// The validate_dob_year Function:
// This checks to make sure the date of birth year
// is 4 numbers.
function validate_dob_year(val) {
	return /^[0-9]{4}$/.test(val)	
}

// The validate_ssn Function:
// We only require a SSN if a user selects a bundle.
// The SSN must be in the format of xxx-xx-xxxx, where
// each "x" represents a number.
function validate_ssn(val) {
	return /^[0-9]{3}[\- ]?[0-9]{2}[\- ]?[0-9]{4}$/.test(val);
}

// The validate_card_exp_date Function:
// Must be a two digit month code, followed by a slash and either two or four digits for a year
function validate_card_exp_date(val) {
	return /^[0-9]{2}\/([0-9]{2}|[0-9]{4})$/.test(val);
}

function validate_mac_address(val) {
	return /^([0-9a-fA-F]{2}:){5}([0-9a-fA-F]{2})$/.test(val);
}
