// <![CDATA[
/******************************************************************************************
* Project: BSP2.0 (BankSeachPlus Version 2.5)
* Module: validation.namespace.js
* Version: 1.5.6
* Description: General class of validation functions that are used for validating forms
* 
* CBNet Ltd
* 
* Copyright: 2006 CBNet Ltd
*
* Email:  support@cbnet.info
* Author: Greg Shiers, Jarratt Ingram
* 
* 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.
******************************************************************************************/

// Set the validation 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.
	// if(validation.empty(field){...}
	empty: function ( field ) {
		var re = /^\s*$/;
		return re.test( field.value );
	},
	
	// Check that the field contains no illegal charactors
	// Allows "_", " " and "." otherwise other charactors are not allowed
	// if(!validation.alpha_numeric(field)){...}
	alpha_numeric: function ( field ) {
		var re = /^[A-Za-z0-9_ \.]+$/
		return re.test( field.value );
	},
	
	// Function to check for a valid email address, includes a check for @ and . in the string
	// if(validation.email(field)){...}
	email: function ( field ) {
		var re = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/
		return re.test( field.value );
	},
	
	// Function to check for a valid phone number
	// Can include a number, spaces, dashes, and an extention
	// if(!validation.phone_number(field)){...}
	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 2 fields match eachtoher
	// Generally used for when asking confirmation for an email address or password
	// if(validation.password_match(field1,field2){...}
	fields_match: function ( field1 , field2 ) {
		return ( field1.value != field2.value ) ? true : false;
	},
	
	// Function to check the length of a field
	// if (validation.field_length ( field , min , max)) {...}
	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
	// if (validation.min_length ( field , min )) {...}
	min_length: function ( field , min ) {
		return ( ( field.value.length < min ) ) ? true : false;
	},
	
	// Function to check the length of a field
	// if (validation.field_length ( field , min , max)) {...}
	max_length: function ( field , max ) {
		return ( ( field.value.length != max ) ) ? true : false;
	},
	
	// To check that input type is a word document
	// if(!validation.word(field){...}
	word: function ( field ) {
		var re = /(.doc$)/; // allow only word
		return re.test( field.value );
	},
	
	// Checks that input is an image
	// if(!validation.image(field){...}
	image: function ( field ) {
		var re = /(.jpe?g$)|(.gif$)|(.png$)/; // allow jpg / jpeg / gif / png
		return re.test( field.value );
	},
	
	// Checks that input is an file
	// if(!validation.file(field){...}
	file: function ( field ) {
		var re = /(.pdf$)|(.txt$)|(.csv$)|(.doc$)/; // allow pdf / txt / csv / doc
		return re.test( field.value );
	},

	// Function to group a bunch of check boxes
	// Use this function is conjunction with is_minimum_checked & is_minimum_selected to group and make an array
	// group = group_it(field.getElementsByTagName('option')) // group = group_it(field);
	group: function ( obj ) {
		return (typeof obj[0] != 'undefined') ? obj : [obj];
	},
	
	// Function to validate that one of the groups are checked
	// first we must make sure that create an array of the elements that we want to check
	// by using the above group_it function
	is_min_checked: function ( min , grp ) {
	var checked = 0, i = grp.length;
	do
		if (grp[--i].checked)
			if (++checked >= min)
				return false;
	while (i);
	return true;
	},
	
	// Object/ Namespsace that checks that the min options are selected in a multiple select.
	// to call this function Call function
	// group = group_it(field.getElementsByTagName('option')) this will group the elements
	// validation.is_min_selected(min,field)
	// remember to set the selected variable to 0 to initialize that it starts on 0
	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 singe checkbox is checked.
	// if(validation.checked(field)){...}
	checked: function ( field ) {
		return ( field.checked ) ? false : true;
	},
	// Function to make sure that an option is selected.
	// if(validate.selected(field)){...};
	selected: function ( field ) {
		return (field.selectedIndex == 0) ? true : false;
	},
	// Function to check for a certain selected index
	// if(validate.selected(field)){...};
	selected_index: function ( field , index) {
		return (field.selectedIndex == index) ? true : false;
	},

	// validate that the user has checked one of the radio buttons
	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;
       		}
   		}
	},
	
	// Object that converts m to mm
	// IE is we have a date function that spits out 2 as the month of february
	// This funtion will make the value now 02.
	friendly_date: function ( string ) {
		if ( string < 10 ) {
			string='0'+string;
		}
	 	return string;
	},
	// Object to validate that the date formatt has been entered correctly (dd/mm/yyyy)
	date_formatted : function ( field ) {
		var re = /^(\d{2,2})\/(\d{2,2})\/(\d{4,4})$/
		return re.test( field.value );
	}
}

// we create a trim function with a small reg ex to trim
// white space from the back and front of the value that's to be stored.
// This function is to be used when validating forms
String.prototype.trim = function() {
	return this.replace(/^\s*|\s*$/g, '');
}
// ]]>