﻿/**
*   ErrorChecker
*
*   Class compiling of all static utility functions which will be used 
*	in the general application for error checking
*
*   @usage    Used by all forms to check inputs before posting data
*   @author   Daniel Ivanovic dan@substance001.com
*/
var ErrorChecker = Class.extend(
	{
		init: function() {},
		
		/**
		*   test
		*
		*   Tests the class - returns string
		*/
		test: function() 
		{ 
			return "Test Complete"; 
		},
		
		/**
		*   checkString
		*
		*   Checks a string against a default, empty and regular expression
		*
		*   @param	t - string to check
		*	@param	df - default text string
		*	@param	re - regular expression "[a-zA-Z0-9]"
		*/
		checkString: function(t, df, re)
		{
			var pass = true;
			var feedback = "NoErrors";
			df = ( df == undefined ) ? "" : df;
			re = ( re == undefined ) ? "[a-zA-Z0-9]" : re;
			
			//alert ( "Checking : " + t + " : " + df + " : " + re );
			
			// test the text against the default string & empty string
			switch ( t )
			{
				case df:
				
					//alert ( "ErrorDefaultText" );
				
					feedback = "ErrorDefaultText";
					pass = false;
				
					break;
				case "":
				
					//alert ( "ErrorNoText" );
				
					feedback = "ErrorNoText";
					pass = false;
				
					break;
				default:
				
					//alert( new RegExp( re ).test( t ) + "  " + re.test( t ) + " : " + re + " : " + new RegExp( re ) );
				
					// test against regular expression
					if ( !new RegExp( re ).test( t ) )
					{
						feedback = "ErrorRegularExpresssion";
						pass = false;
					}
				
					break;
			}
			
			return { pass:pass, feedback:feedback };
		},
		
		/**
		*   checkNumber
		*
		*   Checks a number for validity
		*
		*   @param	n - number to check
		*/
		checkNumber: function(n)
		{
			/*var pass = true;
			var feedback = "NoErrors";
			
			if ( isNaN( n ) )
			{
				pass = false;
				feedback = "ErrorInvalidNumber";
			}
			
			return { pass:pass, feedback:feedback };*/
		},
		
		/**
		*   validateString
		*
		*   Validates a string - returns a valid string if the passed string is not valid
		*
		*	@usage	Used to default strings if they are empty or left as the prompt
		*	@param	s - string to validate
		*	@param	p - prompt string
		*	@param	d - default string
		*/
		validateString: function(s, p, d)
		{
			/*var str  = "";
			
			if ( s == "" ) {
			// if the string is blank
				str = d;
			} else if ( s == p ) {
			// if the string has been left as the prompt
				str = d;
			} else {
			// if the string is okay
				str = s;
			}
			
			return str;*/
		},
		
		/**
		*   validateNumber
		*
		*   Validates a number - returns a valid number if the passed number is not valid
		*
		*	@usage	Used to default numbers if they are empty or NaN
		*	@param	n - number to validate
		*	@param	d - default number
		*/
		validateNumber: function(n, d)
		{
			/*var num = 0;
			
			if ( isNaN( n ) ) {
			// if the number is not a number
				num = d;
			} else {
			// if the number is okay
				num = n;
			}
			
			return num;*/
		}
	}
);