
var jsTVNZValidate = {


	// commonly used validation sets
	// format is <field>: { <re: regexp > <matchField: fieldname> <optional:<true|false>>, hint: string  }
	END_USER : {
			// hint for whole form
			form_hint: 'We\'ve found some errors in your information. Please see the highlighted fields below',

			// Validation and hints for each field
			first_name: 	{ re: /^[a-z]{1,255}$/i, hint: 'Your first name may only contain letters and special characters' },
			last_name: 		{ re: /^[a-z]{1,255}$/i,	hint: 'Your last name may only contain letters and special characters' },
			email: 			{ re:/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i, hint:'Please enter a valid e-mail address'	},
			gender:			{ re:/^[mf]$/i, optional: true, hint:'M, F or blank' },
			region: 		{ re:/.+/, 	hint:'Please select your region of interest' },
			dob_year: 		{ re:/^\d{4}$/, hint:'Please select a valid 4-digit year' },
			mobile_number:  { re:/^\d{1,11}$/, optional: true, hint:'If specified, mobile number may contain up to 11 digits'	},
			nickname: 		{ re:/^[\w ]{1,50}$/i, hint:'Please enter your Username' },
			password: 		{ re:/^[a-z\d]{6,20}$/i, hint:'Your password must be between 6 and 20 characters' },
			confirm: 		{ matchField: 'password', hint:'The Password and Confirm Password fields do not match' },
			terms: 			{ re:/^y$/i, hint:'Please read and accept TVNZ\'s terms & conditions of access'
			}
	},

	// fieldValidation: array of validation info, as above
	// form form to validate
	// hintFunc(form,field,hint): optional function to be called during validation. hintFunc will be called for each field which exists in
	// the form with either null (success) or the hint as to how the field should be filled in
	// It will finally be called with a null field and either null (success) or a message indicating that an error has occured
	validate : function( fieldValidation, form, hintFunc ) {
		var success = true;
		for (field in fieldValidation) {
			var validation = fieldValidation[field];
			if ( form[field] ) {
				var fieldSuccess =
						(form[field].type != 'checkbox' || form[field].checked ) &&
						(
						 (validation.optional &&  (!form[field].value || form[field].value == '')) ||
						 (validation.matchField && form[field].value == form[validation['matchField']].value) ||
						 (validation.re && validation.re.test(form[field].value))
						);
				if ( hintFunc )
					hintFunc(form,field,fieldSuccess ? null : validation.hint);
				if (!fieldSuccess)
					success = false;
			}
		}
		if ( hintFunc )
			hintFunc( form, null, success ? null : (fieldValidation.form_hint ? fieldValidation.form_hint : 'Validation failed' ) );
		return success;
	}

};