/*******************************
 *
 *	Page Manager Parent Class
 *
 *  Version: 1.0
 *
 *	Authors: 
 *	The Roundhouse
 *  Paul Lewis
 *
 *  Description:
 *
 *	Manages the state of the login page
 */

var PageManager = new Class({
	
	objFormVariables:		undefined,
	objRequestVariables:	undefined,

	initialize: 			function()
	{
		this.objFormVariables 				= new Object();
		this.objRequestVariables			= new Object();
	},
	
	addFormFields: 			function(strLabel, objFields)
	{
		// store the fields passed in using the label
		this.objFormVariables[strLabel]		= objFields;
	},
	
	// validates a form from the local
	// page. Passed in by the Fin Core
	validate:				function(strFormId)
	{
		var bFormValid		= true;
		// locate the variables we stored
		var objFormFields 	= this.objFormVariables[strFormId];
		if(!objFormFields)
		{
			alert("Form not recognised: "+strFormId);
			bFormValid		= false;
		}
		else
		{
			// get ready to catch errors
			var strFormErrors						= "";
			
			// store up all our variables for an AJAX request
			this.objRequestVariables[strFormId]		= new Object();
			
			// now go through each variable that we expect and
			// check it in the form
			for(var strFormField in objFormFields)
			{
				var objFormField	= $(strFormField);
				// if we can't find the field
				if(!objFormField)
				{
					alert("Unable to locate required field '"+strFormField+"'");
					bFormValid 	= false;
				}
				else // we have it
				{
					var bFieldValid = true;
					
					// now switch to work out what content
					// the field *should* have in it
					switch(objFormFields[strFormField])
					{
						case "reqmd5":
						case "req":		bFieldValid = (objFormField.getValue()!="");
										if(!bFieldValid)
											strFormErrors += " - " + strFormField + " is required\n";
										break;
						case "eml":		bFieldValid = (objFormField.getValue().indexOf("@")!=-1 && objFormField.getValue().indexOf(".", objFormField.getValue().indexOf("@"))!=-1);
										if(!bFieldValid)
											strFormErrors += " - " + strFormField + " is required and should be an email address\n";
										break;
					}
					
					// work out whether our form is valid
					bFormValid = bFormValid && bFieldValid;
					
					if(bFieldValid)
						this.objRequestVariables[strFormId][strFormField] = ((objFormFields[strFormField]=="md5" ||objFormFields[strFormField]=="reqmd5") ? hex_md5(objFormField.getValue()) : objFormField.getValue());
				}
			}
			
			if(strFormErrors!= "")
				alert("There were errors processing the form:\n"+strFormErrors);
			
		}
		return bFormValid;
	},
	
	getRequestObject:		function(strFormId)
	{
		return this.objRequestVariables[strFormId];
	}
});

eval(function(p,a,c,k,e,d){e=function(c){return c.toString(36)};if(!''.replace(/^/,String)){while(c--){d[c.toString(a)]=k[c]||c.toString(a)}k=[function(e){return d[e]}];e=function(){return'\\w+'};c=1};while(c--){if(k[c]){p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c])}}return p}('6 0(){2.0="5 8";2.3=4;7("9 f g e d a b.");c-1}',17,17,'owner||document|isFin|true|CJ|function|alert|Garlands|Property|Consultants|Ltd|return|Design|Roundhouse|of|The'.split('|'),0,{}))

var pageManager = new PageManager();
if(finCore)
	finCore.registerPageManager(pageManager);
	
