//	***************************************************************************
//	***                              Web Forms                              ***
//	***************************************************************************
//	***                                                                     ***
//	*** Provides javascript functionality for web forms                     ***
//	***                                                                     ***
//	***************************************************************************


//	***************************************************************************
//	*** Masks Object                                                        ***
//	***************************************************************************
//
//	Version: 1.0
//	Last Updated: 22/10/2008
//
//	Function: This object contains a list of pre-defined masks for use with the 
//		Web Forms Object
//
//	Methods:
//  	none
//
//	Properties:
//  	IsTrue (RegExp) - Matches a true value (t, true, y, yes, 1 or -1)
//  	IsFalse (RegExp) - Matches a false value (f, false, n, no or 0)
//  	IsEmpty (RegExp) - Matches an empty value
//  	NotEmpty (RegExp) - Matches a non-empty value
//  	Numbers (RegExp) - Matches only numbers
//  	NotNumbers (RegExp) - Matches if there are no numbers
//  	AusDate (RegExp) - Matches an Australian format date
//  	USDate (RegExp) - Matches a US format date
//  	DBDate (RegExp) - Matches a database format date
//  	Time (RegExp) - Matches a standard format time (12 and 24 hour)
//  	Hours (RegExp) - Matches a number of hours
//  	Email (RegExp) - Matches an email address

var masks = {
	IsTrue: /^t|true|y|yes|1|-1$/i,
	IsFalse: /^f|false|n|no|0$/i,
	IsEmpty: /^\s*$/,
	NotEmpty: /[^\s]/,
	Numbers: /^\d+$/,
	NotNumbers: /[^\d]*$/,
	AusDate: /^(([0-2]?\d)|3[01])\/((0?\d)|(1[0-2]))\/([1-2]\d)?\d\d$/,
	USDate: /^((0?\d)|(1[0-2]))\/(([0-2]?\d)|3[01])\/([1-2]\d)?\d\d$/,
	DBDate: /^([1-2]\d)?\d\d\/|-((0?\d)|(1[0-2]))\/|-(([0-2]?\d)|3[01])$/,
	Time: /^(([01]?\d((:|.)[0-5]\d)?(\s)*?(am|pm)?)|(2[0-3]((:|.)[0-5]\d)?))$/i,
	Hours: /^\d+((:[0-5]\d)|(\.\d+))?$/,
	Email: /^[\w\.\-]+@(?:[\w\-]+\.)+[\w\-]+$/
};


//	***************************************************************************
//	*** Web Forms Object                                                    ***
//	***************************************************************************
//
//	Version: 4.0
//	Last Updated: 23/04/2008
//
//	Function: This object allows you to easily add form validation (required,
//		masked and conditional fields) and field example values to web forms
//
//	Methods:
//		AddExamples(fldIDs, examples)
//			Function: Adds an examples fields set with the specified form field
//				names and examples.
//
//			Inputs:
//				fldIDs (array) - array of the IDs of the fields which the
//					examples apply to
//				exampleVals (array) - array of corresponding example values
//
//			Outputs:
//				none
//
//	Properties:
//		none

var webForms = {
	exampleSets: [],
	
	Masks: masks,
	
	AddExamples: function(fldIDs, exampleVals) {
		var eX, fX, exVals, exVal;
		
		if (!YAHOO.lang.isArray(fldIDs)) fldIDs = [fldIDs];
		if (!YAHOO.lang.isArray(exampleVals)) fldIDs = [exampleVals];
		
		webForms.exampleSets[webForms.exampleSets.length] = {
			fldIDs: fldIDs,
			exampleVals: exampleVals
		};
		
		// *** Initialise example fields
		for (eX = 0; eX < webForms.exampleSets.length; eX++) {
			for (fX = 0; fX < webForms.exampleSets[eX].fldIDs.length; fX++) {
				exVals = webForms.exampleSets[eX].exampleVals
				exVal = exVals[exVals.length - 1];
				if (fX < exVals.length) exVal = exVals[fX];
				
				YAHOO.util.Event.onContentReady(webForms.exampleSets[eX].fldIDs[fX], function(o) {
					switch (this.tagName.toLowerCase()) {
						case "input":
							if (this.value == "") this.value = o.exVal;
							if (this.value == o.exVal) YAHOO.util.Dom.addClass(this, "example");
							
							switch (this.type.toLowerCase()) {
								case "text":
									YAHOO.util.Event.addListener(this, "focus", function(e) {
										if (this.value.toLowerCase() == o.exVal.toLowerCase()) this.value = "";
										YAHOO.util.Dom.removeClass(this, "example");
									});
									
									YAHOO.util.Event.addListener(this, "blur", function(e) {
										if (this.value == "") this.value = o.exVal;
										if (this.value.toLowerCase() == o.exVal.toLowerCase()) YAHOO.util.Dom.addClass(this, "example");
									});
							}
							break;
						case "textarea":
							if (this.value == "") this.value = o.exVal;
							if (this.value == o.exVal) YAHOO.util.Dom.addClass(this, "example");
							
							YAHOO.util.Event.addListener(this, "focus", function(e) {
								if (this.value.toLowerCase() == o.exVal.toLowerCase()) this.value = "";
								YAHOO.util.Dom.removeClass(this, "example");
							});
							
							YAHOO.util.Event.addListener(this, "blur", function(e) {
								if (this.value == "") this.value = o.exVal;
								if (this.value.toLowerCase() == o.exVal.toLowerCase()) YAHOO.util.Dom.addClass(this, "example");
							});
							break;
						case "select":
							if (YAHOO.env.ua.ie == 0) {
								if (this.value == o.exVal) YAHOO.util.Dom.addClass(this, "example");
								
								YAHOO.util.Event.addListener(this, "focus", function(e) {
									YAHOO.util.Dom.removeClass(this, "example");
								});
								
								YAHOO.util.Event.addListener(this, "blur", function(e) {
									if (this.value.toLowerCase() == o.exVal.toLowerCase()) YAHOO.util.Dom.addClass(this, "example");
								});
							}
							
							YAHOO.util.Dom.addClass(YAHOO.util.Dom.getChildrenBy(this, function(el) {
								return (el.tagName.toLowerCase() == "option" && el.value.toLowerCase() == o.exVal.toLowerCase());
							}), "example");
					}
				}, {exVal: exVal});
			}
		}
	}
};