/* ---------------------------------
  @ CingoKit.Form.base
  
  This base class will set up the structure for the Form classes
----------------------------------- */

// Form constructor
CingoKit.Form = function ()
{
	// register baseclass
	this._class = CingoKit.Form;
	
	// html element attribute
	this._attribute = "useform";
	
	// set up registration array
	this.registration = new Array();
	
	// initialize
	this.init();
};


// prototype structure
CingoKit.Form.prototype = {

	init : function()
	{
		// grab all specified forms on the page
		var elements = getElementsByTagAndClassName('form', 'cingokit', null);
		
		for (var i=0; i<elements.length; i++) {
			var element = elements[i];
			
			// test for the useForm attribute
			var attribute = getNodeAttribute(element, this._attribute);
			if (!isNull( attribute )) {
				
				// apply functionality
				var form = this._generate(element);
				
				// set a reference to the default form
				if (isUndefinedOrNull(this._defaultForm)) {
					this._defaultForm = form;
				}
				
			};
			
		};
	},
	
	// private function accessible via subscribed click handlers
	_generate : function(element)
	{
		// form mgr object
		var mgr = getNodeAttribute(element, 'useform-mgr');
		mgr = (isNull(mgr) || mgr=='null') ? this : mgr;
		
		// form submit method - action/ajax, each will use action attribute for xfa/url...
		var formmethod = getNodeAttribute(element, 'method');
		if (!isNull(formmethod)) {
			switch ( formmethod.toLowerCase() )
			{
				case 'ajax':
					var submitmethod = 'ajax';
					break;
				case 'get':
				case 'post':
				case 'action':
				default:
					var submitmethod = 'action';
			};
		} else {
			logError("Form method not defined.");
			return;
		};
		
		// formulate the ajax object
		var ajaxstr = getNodeAttribute(element, 'useform-ajax');
		ajaxstr = (isNull(ajaxstr) || ajaxstr=='null') ? null : ajaxstr;
		
		var ajax = null;
		if (!isNull(ajaxstr)) {
			var str = ajaxstr.split(',');
			if (str.length == 2) {
				ajax = { 'src' : eval(str[0]), 'func' : str[1] };
			};
		};
		
		// formulate the callback object
		var callbackstr = getNodeAttribute(element, 'useform-callback');
		callbackstr = (isNull(callbackstr) || callbackstr=='null') ? null : callbackstr;
		
		var callback = null;
		if (!isNull(callbackstr)) {
			var str = callbackstr.split(',');
			if (str.length == 2) {
				callback = { 'src' :  eval(str[0]), 'func' : str[1] };
			};
		};
		
		// grab a modalnav reference if there is one...
		var modal = getNodeAttribute(element, 'useform-modal');
		modal = (isNull(modal) || modal=='null') ? null : modal;
		
		// create form using default function
		var form = this.generate(element, mgr, submitmethod, ajax, callback, modal);
		
		// prevent multiple form submissions - default setting
		form.denyMultipleSubmissions();
		
		// set up the submit button - default setting
		if (form.memberExists('submit')) {
			form.setSubmitButton( form.getMemberByName('submit') );
		};
		
		// return a reference to the form
		return form;
	},
	
	// creation of a new Form
	generate : function( element, mgr, submitmethod, ajax, callback, modal )
	{   
		// next, register the form
		var form = new CingoKit.Form.Manager({
			'elem' : element,
			'mgr' : mgr,
			'submitmethod' : submitmethod,
			'ajax' : ajax,
			'callback' : callback,
			'_modal' : modal
		});

		// append to registration
		this.registration.push(form);
		
		// return reference
		return form;
	},
	
	resetDefaultForm : function()
	{
		if (!isUndefinedOrNull(this._defaultForm)) {
			this._defaultForm.hideAllMessages();
			this._defaultForm.unerrorAllElements();
		};
	},
	
	getForm : function( id )
	{
		var matched = null;
		// search through element objects
		for (var i=0; i<this.registration.length; i++) {
		
			var obj = this.registration[i];
			if (obj.elem.id == id) {
				matched = obj;
				break;
			}
			
		}
		return matched;
	}
	
};

setdefault(CingoKit.Form.prototype, CingoKit);