// <![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
* @page validation.class.js
* @version 1.6
* @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 Boolean
	*/
	empty: function ( field ) {
		var re = /^\s*$/;
		return re.test( field.value );
	},
	/*
	* Function to check that the field contains no illegal characters
	* Allows "_", " " and "." otherwise other characters are not allowed
	* @usage !validation.alpha_numeric(field)
	* @param (field) which field needs to be checked
	* @version 0.1
	* @returns Boolean
	*/
	alpha_numeric: function ( field ) {
		var re = /^[A-Za-z0-9_ \.]+$/
		return re.test( field.value );
	},
	/*
	* Function to check that the field contains no illegal characters
	* Allows only a to z no spaces nothing else
	* @usage !validation.alpha_no_spaces()
	* @param (field) which field needs to be checked
	* @version 0.2
	* @returns Boolean
	*/
	alpha_no_spaces: function ( field ) {
		var re = /^[A-z]+$/
		return re.test( field.value );
	}, 
	/*
	* Function to check for valid domain name
	* Allows only a to z no spaces nothing else
	* @usage validation.domain_string()
	* @param (field) which field needs to be checked
	* @version 0.3
	* @returns Boolean
	*/
	domain_string : function ( field ) { 
		var re = /^[A-z0-9-]+$/
		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 Boolean
	*/
	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 check that the entry is a positive or negative number
	* @usage !validation.numeric_no_spaces(field)
	* @param (field) which field needs to be checked
	* @version 0.3
	* @returns Boolean
	*/
	numeric_no_spaces: function ( field ) {
		var re  = /^[0-9]+$/;
		return re.test( field.value );
	},
	/*
	* Function to check that the entry is a positive or negative number
	* @usage !validation.numeric_with_spaces(field)
	* @param (field) which field needs to be checked
	* @version 0.3
	* @returns Boolean
	*/
	numeric_with_spaces: function ( field ) {
		var re  = /^[0-9 ]+$/;
		return re.test( field.value );
	},
	/*
	* Function to check for valid alpha numeric no spaces
	* Allows only a to z no spaces nothing else
	* @usage validation.alpha_numeric_no_spaces()
	* @param (field) which field needs to be checked
	* @version 0.3
	* @returns Boolean
	*/
	alpha_numeric_no_spaces : function ( field ) { 
		var re = /^[A-z0-9#]+$/
		return re.test( field.value );
	}, 
	/*
	* Function to check that 2 fields match eachtoher
	* @usage validation.password_match(field1,field2)
	* @param ( field1, field2 ) field1: which field needs to be checked, field2: field that needs to be made sure its the same
	* @version 0.3
	* @returns Boolean
	*/
	fields_match: function ( field1 , field2 ) {
		return ( field1.value != field2.value ) ? true : false;
	},
	/*
	* Function to check the max and min length of a field
	* Normally used for a textarea
	* Can include a number, spaces, dashes, and an extention
	* @usage validation.field_length ( field , min , max)
	* @param ( field, min, max )field: which field needs to be checked, min: minimum value, max: maximum value;
	* @version 0.3
	* @returns Boolean
	*/
	field_length: function ( field , min , max ) {
		return ( ( field.value.length < min ) || ( field.value.length > max ) ) ? true : false;
	},
	/*
	* Function to check the length of a field
	* @usage validation.max_length ( field , max)
	* @param ( field, max ) field: the field that needs to be checked, max: maximum characters
	* @version 0.3
	* @returns Boolean
	*/
	max_length: function ( field , max ) {
		return ( ( field.value.length != max ) ) ? true : false;
	},
	/*
	* Function to make sure that its only a word document being uploaded
	* @usage !validation.word(field)
	* @param (field) which field needs to be checked
	* @version 0.4
	* @returns String
	*/
	word: function ( field ) {
		var re = /(.doc$)|(.rft$)/; // allow only word
		return re.test( field.value );
	},
	/*
	* Function to check for only images (jpg,png,gif)
	* @usage !validation.image(field)
	* @param (field) which field needs to be checked
	* @version 0.3
	* @returns String
	*/
	image: function ( field ) {
		var re = /(.jpe?g$)|(.gif$)|(.png$)/; // allow jpg / jpeg / gif / png
		return re.test( field.value );
	},
	/*
	* Function to check for common file inputs
	* @usage !validation.file(field)
	* @param (field) which field needs to be checked
	* @version 0.2
	* @returns String
	*/
	file: function ( field ) {
		var re = /(.pdf$)|(.txt$)|(.csv$)|(.doc$)/; // allow pdf / txt / csv / doc
		return re.test( field.value );
	},
	/*
	* Function to check for uploading of a web formatted document
	* @usage !validation.web(field)
	* @param (field) which field needs to be checked
	* @version 0.2
	* @returns String
	*/
	web: function ( field ) {
		var re = /(.css$)|(.html$)|(.xhtml$)|(.xml$)|(.js$)|(.php$)/; // allow css / html / xhtml / xml / js / php
		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 minimum length of objects are selected from a drop down
	* @usage !validation.is_min_selected( grp, min )
	* @param (grp, min) grp: field that must be checked, min: minimum to be counted
	* @version 0.6
	* @returns Boolean
	*/
	is_min_selected: function ( grp , min ) {
	
		var selected = 0, i = grp.length; // set "selected" to 0 so as to initialize the variable
		do {
			if ( grp[--i].selected ) {
				if ( ++selected >= 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 that an option does not match a certain selected index for an option
	* @usage validation.selected_index( field, index )
	* @param (field) which field needs to be checked
	* @version 0.4
	* @returns Boolean
	*/
	selected_index: function ( field , index) {
		return (field.selectedIndex == index) ? 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.2
* @returns String
*/
String.prototype.trim = function() {
	return this.replace(/^\s*|\s*$/g, '');
}
// ]]>
