/* 
Description	:	Form field validation routines for various data types

Author		:	Kyle Kepley
Date		:	11/29/2001
Notice		:	Copyright (c) Passfire.com
Last Mod	:	

*/
	var template
	
	//' Test if value even exists
	//'--------------------------------------------------------
	function HasValue( value )
	{
		var test = new String( value )
		if( test == "undefined" ) return false
		
		if( test.length > 0 )
			return true
		else
			return false
	}

	//' Test if value has any characters specified in comma delimited list
	//'-------------------------------------------------------------------
	function HasChar( text, char_list )
	{	
		var text_str = new String( text )
		var list_str = new String( char_list )
		var char_arry = list_str.split( "," )
		
		for( i=0; i<char_arry.length; i++ )
		{
			if( text_str.indexOf( char_arry[i], 0 ) != -1 )
				return true
		}
		return false
	}

	//' Test if value has any characters specified in comma delimited list
	//'-------------------------------------------------------------------
	function HasCharAt( text, index, char_list )
	{	
		var text_str = new String( text )
		var list_str = new String( char_list )
		var char_arry = list_str.split( "," )
		
		for( i=0; i<char_arry.length; i++ )
		{
			if( text_str.charAt( index ) == char_arry[i] )
				return true
		}
		return false
	}

	//' Test if string has any numerical digits
	//'--------------------------------------------------------	
	function HasDigits( text )
	{
		return HasChar( text, "1,2,3,4,5,6,7,8,9,0" )
	}

	//' Test size constraints of text
	//'--------------------------------------------------------
	function TextLength( text, min, max )
	{
		if( text.length < min || text.length > max ) 
			return false
		else
			return true
	}

	//' Test size constraints of a number
	//'--------------------------------------------------------
	function NumberRange( num, min, max )
	{
		if( num < min || num > max ) 
			return false
		else
			return true	
	}

	//' Test if string is a text string
	//'--------------------------------------------------------
	function IsText( string )
	{
		var i, code
		
		if( !HasValue( string ) ) return false
		for( i=0; i<string.length; i++ )
		{
			code = string.charCodeAt( i )
			//alert( "code["+i+"] = "+code )
			if( code < 65 || code > 122 || (code > 90 && code < 97 ) )
				return false
		}	
		return true
	}

	//' Test if string is a number
	//'--------------------------------------------------------
	function IsNumber( string )
	{
		for( i=0; i<string.length; i++ )
		{
			if( isNaN( string.charAt( i ) ) )
				return false
		}
		return true
	}

	//' Test if string is a date
	//'--------------------------------------------------------
	function IsDate( string )
	{
		var test = new Date( string )

		if( test.getDate() >= 1 )
			return true
		else
			return false
	}

	//' Test if string is a valid name
	//'--------------------------------------------------------
	function IsName( string )
	{
		return IsText( string )
	}

	//' Test if string is a valid name
	//'--------------------------------------------------------
	function IsAddress( string )
	{
		if( HasValue( string ) && HasChar( string, "0,1,2,3,4,5,6,7,8,9" ) )
			return true
		else
			return false
	}

	//' Test if string is valid email
	//'--------------------------------------------------------
	function IsEmail( string )
	{
		if( HasChar( string, "@" ) && HasChar( string, "." ) )
			return true
		else
			return false
	}
	
	//' Test if string is valid zip
	//'--------------------------------------------------------
	function IsZip( string )
	{
		template = new Array()
		template[0] = "xxxxx"
		template[1] = "xxxxx-xxxx"

		return ValidateTemplate( string )
	}

	//' Test if string is valid SS#
	//'--------------------------------------------------------
	function IsSocialSecurity( string )
	{
		template = new Array()
		template[0] = "xxx-xx-xxxx"
		template[1] = "xxxxxxxxx"

		return ValidateTemplate( string )
	}

	//' Test if string is valid phone number
	//'--------------------------------------------------------
	function IsPhoneNumber( string )
	{
		template = new Array()
		template[0] = "xxx-xxx-xxxx"
		template[1] = "x-xxx-xxx-xxxx"
		template[2] = "xxxxxxxxxx"
		template[3] = "xxxxxxxxxxx"
		template[4] = "xxx-xxxxxxx"
		template[5] = "xxxxxx-xxxx"
		template[6] = "(xxx) xxx-xxxx"
		template[7] = "(xxx)-xxx-xxxx"

		return ValidateTemplate( string )
	}
	
	//' Generic validator for all templates
	//'--------------------------------------------------------	
	function ValidateTemplate( string )
	{	
		var passed = true
		
		for( i=0; i<template.length; i++ )
		{
			if( string.length == template[i].length )
			{
				passed = true
				for( j=0; j<template[i].length; j++ )
				{
					if( template[i].charAt(j) == 'x' && isNaN( string.charAt(j) ) )
						passed = false
					if( template[i].charAt(j) == '-' && !isNaN( string.charAt(j) ) )
						passed = false
				}
				if( passed == true ) return true
			}
		}	
		return false
	}			
	
