
/*
	Jax Form Checks v1.02 - JavaScript Form Check class
	
	Copyright © 2007 by Andreas John aka Jack (tR). All rights reserved.
	
	This software is published under the terms of the Lesser General Public 
	License (LGPL) Version 3 or higher: 
	
	http://www.fsf.org/licensing/licenses/lgpl.html
		
	You are not allowed to publish, copy or use this software in ways which 
	are not explicitely allowed by this license. 
	
	---------------------------
	
	File        : /js/form_checks.js (script for form validation)
	Language    : JavaScript 1.2
	Last Update : 2007-10-23, Andreas John - Jack (tR)
*/


// ======================= DOM access functions and substitutes ====================
function _get_obj( id )
{
	if (document.getElementById)
	{
		return document.getElementById(id);
	}
	else if (document.all)
		{
			return document.all[id];
		}
		else if (document.layers)
			{
				return document.layers[id];
			}
}




// ========================= prototype for input field validation class =============
function class_form_checks( )
{
	
	// =============== private vars =============
	this.debug_verbosity = 0;
	this.settings        = new Array();
	this.icss_ok         = new Array();
	this.icss_failure    = new Array();
	

	// =============== public methods ============
	
	this.config = function config( icss_ok, icss_failure )
	{
		this.icss_ok      = icss_ok;
		this.icss_failure = icss_failure;
	}
	
	this.add_inline_css = function add_inline_css( htmlobj, css_data )
	{
		var j = css_data.length;
		
		for (var i = 0; i < j; i++)
		{
			var attr_name   = css_data[i][0];
			var attr_values = css_data[i][1];
			htmlobj.style[attr_name] = attr_values;
		}
		
		return false;
	}
	
	this.reg_input = function reg_input( input_id, is_obligatory, min_len, max_len, filter, alphabet, error_message )
	// adds a new input_field
	{
		var i = this.settings.length;
		
		this.settings[i] = new Object();
		this.settings[i]["id"]            = input_id;
		this.settings[i]["is_obligatory"] = is_obligatory;
		this.settings[i]["min_len"]       = min_len;
		this.settings[i]["len"]           = max_len;
		this.settings[i]["filter"]        = filter;
		this.settings[i]["alphabet"]      = alphabet;
		this.settings[i]["error_message"] = error_message;
	}
		
	
	this.raise_error = function raise_error( inp_field, user_msg, msg )
	{
		if ( user_msg ) 
		{
			m = user_msg;
		}
		else
			{						
				m = "form_check.js:\ninput field " + inp_field;
			}
					
		alert( m + "\r\n\r\n" + msg );
	}
	
	/**
	 * returns true if all inputs are valid
	 *
	 */
	this.inputs_ok = function inputs_ok()
	{
		is_error = false;
		for (var i = 0; i < this.settings.length; i++) 
		{
			var ipf = _get_obj( this.settings[i]["id"] );
			
			if ( ipf == null )
			{
				alert( 'form_check.js critical error: input field '+this.settings[i]["id"] + ' does not exist!');
				return false;
			}
			else
			{
				// test 1: is empty?
				if( (ipf.value.length == 0) && (this.settings[i]["is_obligatory"] == true) ) 
				{					
					this.raise_error( this.settings[i]["id"], this.settings[i]["error_message"], 'Is empty!');
					
					ipf.focus();
					this.add_inline_css( ipf, this.icss_failure );
					return false;
				}
				
				// test 2: is too long?
				if( (ipf.value.length > this.settings[i]["len"] ) ) 
				{
					this.raise_error( this.settings[i]["id"], this.settings[i]["error_message"], "Too long! ("+ipf.value.length+" characters)\nMaximum is: "+this.settings[i]["len"] );
					ipf.focus();
					this.add_inline_css( ipf, this.icss_failure );
					return false;
				}
				
				// test 3: is invalid element of a defined alphabet?
				if ( (ipf.value.length > 0) && this.settings[i]["filter"] == "SET" && this.settings[i]["alphabet"].length > 0 )
				{
					is_in_alphabet = false;
					for (var arrEl in this.settings[i]["alphabet"])
					{
						if (this.settings[i]["alphabet"][arrEl] == ipf.value)
						{
							is_in_alphabet = true;
						}
					}
					
					if ( !(is_in_alphabet) )
					{
						this.raise_error( this.settings[i]["id"], this.settings[i]["error_message"], "Has an unexpected value (\"" +ipf.value+ "\") " + "\nvalid values are: " + this.settings[i]["alphabet"]);
						ipf.focus();
						this.add_inline_css( ipf, this.icss_failure );
						return false;
					}
				}
				
				// test 4: is invalid integer?
				if ( (ipf.value.length > 0) && this.settings[i]["filter"] == "INTEGER" && this.settings[i]["alphabet"].length == 2 )
				{
					if (ipf.value < this.settings[i]["alphabet"][0] || ipf.value > this.settings[i]["alphabet"][1] )
					{
						this.raise_error( this.settings[i]["id"], this.settings[i]["error_message"], "Out of range (#" +ipf.value+ ") " + "\nlimits are: " + this.settings[i]["alphabet"] );
						ipf.focus();
						this.add_inline_css( ipf, this.icss_failure );
						return false;
					}
				}
				
				// test 5: is invalid email?
				if ( this.settings[i]["filter"] == "EMAIL" )
				{
					var pattern_email = new RegExp( '^([^.: ])([a-zA-Z0-9\\-\\.\\_]+)(\\@)([a-zA-Z0-9\\-\\.]+)([\\.])([a-zA-Z]{2,4})$' );
	
					if ( !pattern_email.test(ipf.value) )
					{
						this.raise_error( this.settings[i]["id"], this.settings[i]["error_message"], "Is not a valid email address" );
						ipf.focus();
						return false;
					}    
				}
				
				// test 6: is checkbox?
				if ( this.settings[i]["filter"] == "CHECKBOX" )
				{
					if ( ipf.checked == false )
					{
							this.raise_error( this.settings[i]["id"], this.settings[i]["error_message"], "Check box "+this.settings[i]["id"]+" has to be checked " );
							ipf.focus();
							return false;
					} 
				}
				
				
				
				
				this.add_inline_css( ipf, this.icss_ok );
			}
		}
		return !is_error;
	}
}	

function form_checks() 
{	

}

form_checks.prototype = new class_form_checks();

