// JavaScript Document
  
  /**
  * @description HTML form manipulation and validation
	* @param string sForm [name of the HTML form]
	* @return void
  */
  var Form = function ( sForm )
	{
		this._form = document.forms[sForm];
	}
	  
  /**
  * @description Main Form class definition, methods and properties
  * @requires Form
  * @return void
  */
  Form.prototype = 
  {
		/**
		* @description data formats for validation
		*/
		formats : {
			datetime : /[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}/ /*2008-05-26 06:48:35*/ ,
			date : /[0-9]{4}-[0-9]{2}-[0-9]{2}/ /*2008-05-26*/ , 	
			time : /[0-9]{2}:[0-9]{2}:[0-9]{2}/ /*06:48:35*/ , 		
			shorttime : /[0-9]{2}:[0-9]{2}/ /*06:48*/ , 		
			word : /^\w+$/ /*hello, Nelis*/ , 				
			number : /^\d+$/ /*1, 23*/ , 			 			 		
			letter : /^[a-z][A-Z]$/ /*a, B*/ ,  					
			email : /^(.+?)\@(.+?)\.(.+?)$/ /*mynelis@gmail.com*/ , 
			notempty : /.+/ /*any-thing_goes*/ 
		},
		  
		/**
		* @description Get the selected value/values of an element
		* @param object oElem [name of the form element]
		* @return string value
		*/
		getValueOf : function ( oElem )
		{
			switch ( oElem.type.toLowerCase ( ) )
			{
				case 'text': /*for text inputs*/
				case 'textarea': /*and textarea inputs*/
					return oElem.value;
					break;
					
				case 'radio': /*for radion inputs, look for the first selected with same name*/
					var e, el;
					for ( e=0; e<this._form.elements.length; e=e+1  )
					{
						el = this._form.elements[e]; 
						if ( el.type.toLowerCase ( ) == 'radio' && el.name == oElem.name && el.checked == true ) return el.value;
					}
					break;
					
				case 'checkbox': /*for check boxes, look for all checked with same name*/
					var e, el, ret = new Array ( );
					for ( e=0; e<this._form.elements.length; e=e+1  )
					{
						el = this._form.elements[e]; 
						if ( el.type.toLowerCase ( ) == 'checkbox' && el.name == oElem.name && el.checked == true ) ret.push ( el.value );
					}
					return ret.join ( ',' );
					break;
					
				case 'select-one': /*for single select options*/
					return oElem.options[oElem.options.selectedIndex].value;
					break;
					
				case 'select-multiple': /*for multiple select options, look for all selected*/
					var e, el, ret = new Array ( );
					for ( e=0; e<oElem.options.length; e=e+1  )
					{
						el = oElem.options[e]; 
						if ( el.selected == true ) ret.push ( el.value );
					}
					return ret.join ( ',' );
					break;
					
				default: /*for other element types*/
					return oElem.value;
					break;
			}
		},
		  
		/**
		* @description Validate the form
		* @requires Form.formats, Form.getValueOf ( )
		* @return bool true on success, false on failure
		*/
		Validate : function ( )
		{
			var e, el, elval, elformat;
			for ( e=0; e<this._form.elements.length; e=e+1 )
			{
				el = this._form.elements[e];
				if ( !el.name ) continue;
				if ( el.name.indexOf ( ':' ) == -1 ) continue;
				var chunks = el.name.split ( ':' );
				elformat = chunks[1];                
				var elLabel =  ( chunks[2] !== undefined ) ? chunks[2] : chunks[0];
				elval = this.getValueOf ( el );
                if ( !this.formats[elformat].test ( elval ) )
				{
					//alert ( '"'+elval+'" is not a valid '+elLabel );
					alert ( 'Invalid '+elLabel );
					el.focus ( );
					return false;
				}
			}
			return true;
		},
		  
		/**
		* @description Collect values from the form
		* @requires Form.formats, Form.getValueOf ( )
		* @return string true on success, false on failure
		*/
		Collect : function ( )
		{
			if ( this.Validate ( ) )
			{
				var e, ret=new Array ( );
				for ( e=0; e<this._form.elements.length; e=e+1 )
				{
					if ( this._form.elements[e].name.indexOf ( ':' ) > -1 )
					{
						this._form.elements[e].name = this._form.elements[e].name.split ( ':' , 2 )[0];
					}
					ret.push ( this._form.elements[e].name+'='+this.getValueOf ( this._form.elements[e] ) );
				}
				return ret.join ( '&' );
			}
			else
			{
				return false;
			}
		},
		  
		/**
		* @description Submit form
		* @requires Form.validate ( )
		* @return void
		*/
		Submit : function ( )
		{
			this.Validate ( );		
			var e;
			for ( e=0; e<this._form.elements.length; e=e+1 )
			{
				if ( this._form.elements[e].name.indexOf ( ':' ) > -1 )
				{
					this._form.elements[e].name = this._form.elements[e].name.split ( ':' , 2 )[0];
				}
			}
			this.submit ( );
		},
		  
		/**
		* @description Reset the form
		* @return void
		*/
		Reset : function ( )
		{
			this._form.reset ( );
			return false;
		}
  }