﻿/**
*   FormUtility
*
*   Class offers all utility functions with regard to html forms & their functionality
*
*   @usage    Used by all forms to offer usefule methods
*   @author   Daniel Ivanovic dan@anti-blanks.co.uk
*/
var FormUtility = Class.extend(
	{
		init: function() {},
		
		/**
		*   submitForm
		*
		*   Submits the form
		*/
		submitForm: function( f )
		{
			//alert( "submitting : " + f );
			
			document.forms[ f ].submit();
		},
		
		/**
		*	textCounter
		*	
		*	Counts the remeining text in a passed text area
		*
		*	@param	f - field
		*	@param	cf - count field
		*	@param	mx - max limit
		*/
		textCounter: function( f, cf, mx ) 
		{
			// check if length has reached max
			if ( f.value.length > mx )
			{
				f.value = f.value.substring( 0, mx );
				alert( 'Sorry! You cannot enter more than ' + mx + ' characters in this field' );
				return false;
			} else {
				// display the count
				cf.value = mx - f.value.length;
			}
		},
		
		/*
		*	getForm
		*
		*	Returns a form elements
		*
		*	@param	name - name of form
		*	@param	action - form action
		*/
		getForm: function(name, action)
		{
			var frm = $(document.createElement('form'));
			$( frm ).attr( 'name', name );
			$( frm ).attr( 'action', action );
			
			return frm;
		},
		
		/*
		*	getInput
		*
		*	Returns a form input tag
		*
		*	@param	props - array of properties - array [ { attribute: "", value: "" } ]
		*/
		getInput: function(props)
		{
			var el = $(document.createElement('input'));
			for ( var i in props )
			{
				$( el ).attr( props[i].attribute, props[i].value );
			}
			
			return el;
		},
		
		/*
		*	getTextArea
		*
		*	Returns a text area
		*
		*	@param	props - array of properties to add - array [ { attribute: "", value: "" } ]
		*	@param	value - input value
		*/
		getTextArea: function(props, value)
		{
			var el = $(document.createElement('textarea'));
			for ( var i in props )
			{
				$( el ).attr( props[i].attribute, props[i].value );
			}
			$( el ).append( value );
			
			return el;
		},
		
		/*
		*	getDropDownMenu
		*
		*	Returns a drop down menu based on an array of props
		*
		*	@param	prop - prop name string
		*	@param	value - value
		*	@param	options - array [ { label: "", value: "" } ]
		*/
		getDropDownMenu: function(prop, value, opts)
		{
			var el = $(document.createElement('select')).attr( 'size', '1' );
			for ( var i in opts )
			{
				var opt = $(document.createElement('option')).append( opts[i].label );
				$( opt ).attr( 'value', opts[i].value );
				if ( opts[i].value == value ) $( opt ).attr( 'selected' );
				$( el ).append( opt );
			}
			
			return el;
		},
		
		/*
		*	getCheckbox
		*
		*	Returns a check box
		*
		*	@param	prop - prop name string
		*	@param	value - value
		*	@param	selected - selected flag
		*/
		getCheckbox: function(prop, value, selected)
		{
			/*$checked = ( $selected == 1 ) ? 'checked' : '';
			
			return '<input name="' . $prop . '" type="checkbox" value="' . $value . '" ' . $checked . ' />';*/
		}
	}
);