/** Validate Simple Form 
* @requires validate.form.postprocess.strategies.js
*/

/** @class Parse Form for input/select elements, validate each and perform post-processing  */
SimpleForm = Class(
/** @lends SimpleForm */
{
	/**
	* @constucts
	* @param attribute_validate {String} Name of Attribute eg. class that tags the element for validation.
	* @param attribute_action {String} Name of Attribute eg. class that tags element for post-processing.
	* @param strategies {Array} Describes available validation strategies. 
	* @param fields {Array} References Formular Nodes.
	* @param process {Array} References Elements for Postprocessing.
	*/
	initialize: function()
	{
		this.strategies = new ExtendedStrategies();
		
		if(navigator.appName == "Microsoft Internet Explorer")
		{
			var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
			if (re.exec(navigator.userAgent) != null)
			{
				var ieversion = parseFloat( RegExp.$1 );
			}
			
			if (ieversion < 8)
			{
				this.attribute_validate = "className";
				this.attribute_action = "className";
			}
			else
			{
			this.attribute_validate = "class";
			this.attribute_action = "class";					
			}
		}
		else
		{
			this.attribute_validate = "class";
			this.attribute_action = "class";			
		}
		
		this.fields = null;
		this.process = null;
	},
	/** @deprecated 
	* @param {String} attribute 
	* @param {String} value The name of the attribute
	/** @returns {Bool} True if success, else false */
	setAttribute: function(attribute, value)
	{
		switch (attribute)
		{
			case "attribute_validate":
				(navigator.appName == "Microsoft Internet Explorer" && value == "class") ? this.attribute_validate = "className" : this.attribute_validate = value;
				return true;
			case "attribute_action":
				(navigator.appName == "Microsoft Internet Explorer" && value == "class") ? this.attribute_action = "className" : this.attribute_action = value;
				return true;
			default:
				return false;
		}
	},
	/** @param {Object} node */	
	parse: function ( node )
	{
		for (node = node.firstChild; node != null; node=node.nextSibling)
		{
			if (node.nodeType == 1)
			{
				if ((node.nodeName == 'INPUT' || node.nodeName == 'SELECT' || node.nodeName == 'TEXTAREA') && (node.getAttribute(this.attribute_validate) in this.strategies.validate))
				{
					this.fields.push({"strategy" : node.getAttribute(this.attribute_validate), "node" : node});
				}
				else if (node.getAttribute(this.attribute_action) && (node.getAttribute(this.attribute_action).split(' ')[0] in this.strategies.postprocess))
				{
					var action = node.getAttribute(this.attribute_action).split(' ')[0];
					this.process.push({"action" : action, "node" : node});
				}
			}
			this.parse( node );	
		}
	},
	/** @param {Object} form*/
	validate: function( form )
	{
		if (this.fields == null) 
		{
			this.fields = new Array();
			this.process = new Array();
			result = this.parse( form );
		}

		var tmp = true;
		for(var i=0; i < this.fields.length; i++)
		{
			var strategy = this.fields[i]["strategy"] ;
			if ( strategy in this.strategies.validate)
			{
				var isValid = new Validate().isValid(this.strategies.validate[strategy][0], this.fields[i]["node"]);
				(!isValid) ?  this.fields[i]["node"].setAttribute(this.attribute_validate,this.strategies.validate[strategy][1]) : this.fields[i]["node"].setAttribute(this.attribute_validate, strategy);
				if (!isValid)  tmp = false;
			}
		}
		
		return tmp;
	},
	
	postprocess: function ()
	{
		for (var i=0; i<this.process.length; i++)
		{
			var class_name = this.strategies.postprocess[this.process[i]["action"]][0];
			var root = this.process[i]["node"].getAttribute(this.attribute_action).split(" ")[1];
			for (strategy in this.strategies.validate)
			{
				if ( document.getElementById(root).getAttribute(this.attribute_validate) == strategy )
				{

					process = new window[class_name]().postprocess(this.process[i]["node"], this.attribute_action, this.process[i]["action"] + ' ' + root, root);
				}
				else if  ( document.getElementById(root).getAttribute(this.attribute_validate) == this.strategies.validate[strategy][1] )
				{
						process = new window[class_name]().postprocess(this.process[i]["node"], this.attribute_action, this.strategies.postprocess[this.process[i]["action"]][1] + ' ' + root, root);
				}
			}	
		}
	}	
})
