// <![CDATA[
/*
* Copyright: 2005 - 2007 SI Works Internet Solutions
* If you have come across this page, its cause you are snooping through code, and are generally a developer as such
* If you would like to use some of the code on this page, please simply email support@si-works.net and ask permission
* it would be much appreciated, as we have worked very hard on these fucntions and scripts
* 
* Note: We are not checking for DOM complience in any of these 
* functions, as all of the functions are called from another script
* where we will check for DOM (document.getElementById && document.createTextNode)
* this is generally called in the validation page per each form created.
*
* General class of validation functions that are used for validating forms, this is the lite version for simple form validations
* @page validation.class.lite.js
* @version 1.2
* @author Greg Shiers, Jarratt Ingram (SI Works Internet)
* @copyright: SI Works Internet Solutions 2005 - 2007
*/

/* 
* Set the validation namespace "validation" and we can then define all other 
* functions within this namespace.
*/
var validation = {
	/*
	* Function to check weather a field is empty
	* RegEx includes validation to check if they have added multipe white spaces to the text field.
	* @usage validation.empty(field)
	* @param (field) which field needs to be checked
	* @version 0.1
	* @returns String
	*/
	empty: function ( field ) {
		var re = /^\s*$/;
		return re.test( field.value );
	},
	/*
	* Function to checks than an email address entered is valid @ & .
	* @usage validation.email()
	* @param (field) which field needs to be checked
	* @version 0.3
	* @returns Boolean
	*/
	email: function ( field ) {
		var re = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/
		return re.test( field.value );
	},
	/*
	* Function to checks than a phone number is valid
	* Can include a number, spaces, dashes, and an extention
	* @usage !validation.phone_number(field)
	* @param (field) which field needs to be checked
	* @version 0.3
	* @returns String (NaN)
	*/
	phone_number: function( field ) {
		var re  = /^(\+\d{1,3} ?)?(\(\d{1,5}\)|\d{1,5}) ?\d{3,4} ?\d{0,7} ?(x|xtn|ext|extn|extension)??\.? ?\d{1,5}?$/i;
		return re.test( field.value );
	},
	/*
	* Function to group an object 
	* Use this function together with is_minimum_checked &| is_minimum_selected to group elements into an array
	* @usage !validation.group( obj )
	* @param ( obj ) teh object that needs to be created as an array
	* @version 0.4
	* @returns Array
	*/
	group: function ( obj ) {
		return ( typeof obj[0] != 'undefined' ) ? obj : [obj];
	},
	/*
	* Function to check that a minimum length of objects are checked, can be from one to a max
	* @usage !validation.is_min_checked( grp, min )
	* @param (field) which field needs to be checked
	* @version 0.6
	* @returns Boolean
	*/
	is_min_checked: function ( grp , min ) {
		var checked = 0, i = grp.length; // 
		do {
			if ( grp[--i].checked ) {
				if ( ++checked >= min ) {
					return false;
				}
			}
		}
		while ( i );
		return true;
	},
	/*
	* Function to check that a single checkbox is checked
	* @usage validation.checked( field )
	* @param (field) which field needs to be checked
	* @version 0.2
	* @returns Boolean
	*/
	checked: function ( field ) {
		return ( field.checked ) ? false : true;
	},
	/*
	* Function to check that an options has been selected from the dropdown
	* @usage validation.selected( field )
	* @param (field) which field needs to be checked
	* @version 0.6
	* @returns Boolean
	*/
	selected: function ( field ) {
		return (field.selectedIndex == 0) ? true : false;
	},
	/*
	* Function to check user has checked the radio button option
	* @usage validation.radio( field )
	* @param (field) which field needs to be checked
	* @version 0.5
	* @returns Boolean
	*/
	radio: function ( radio ) {		
		// Run a for loop through the array of radio buttons to check if they 1 is checked
   		for ( var i = 0; i < radio.length; i++ ) {
			if ( radio[i].checked ) {
				return true;
       		}
   		}
	},
	/*
	* Function to convert month formatt from m to mm
	* @usage validation.friendly_date( string )
	* @param (string) which field needs to be converted
	* @version 0.5
	* @returns String
	*/
	friendly_date: function ( string ) {
		if ( string < 10 ) {
			string='0'+string;
		}
	 	return string;
	}
}
/*
* Function prototyped to be able to trime white space from the beggining and end of a string
* @usage validation.friendly_date( string )
* @param (string) which field needs to be converted
* @version 0.1
* @returns String
*/
String.prototype.trim = function() {
	return this.replace(/^\s*|\s*$/g, '');
}
// ]]>
