/*
 * DON'T REMOVE THE FOLLOWING LICENSE
 *           INFORMATION!
 * ----------------------------------
 * Copyright by
 * Dennis Ritz
 * Author: Dennis Ritz
 * dennis.ritz@gmx.net
 * 2007-2008
 * ----------------------------------
 */
function Form() {
	this.viewTab = function viewTab(p_id) {
		var formParent = document.getElementById(p_id).parentNode;
		var form = formParent.getElementsByTagName('FORM');
		for(i=0;i < form.length;i++)
		{
			form[i].style.display = 'none';
		}
		document.getElementById(p_id).style.display = 'inline';
	}

	function checkForm(form) {
		try{
		var arrarg = new Array();
		var ret = true;
		for(var i=1; i<arguments.length;i++) {
			arrarg[i-1] = arguments[i];		
		}

		for(var i=arrarg.length-1; i>=0;i--){
			var item = arrarg[i].split(":");
			var element = form.elements[item[0]];
			var itemError = false;
			
			if(element.value == '') itemError = true;
			if(item.length>1) {
				switch(item[1]) {
				case "PATH": if(element.value.charAt(element.value.length-1) != "\\") itemError = true;
							 break;
				}
			}

			if(itemError){
				if(element.className.search(/-xerror/) == -1)
					element.className = element.className+'-xerror';
				element.focus();
				ret = false;
			}else{
				element.className = element.className.replace(/-xerror/,"");
			}
		}
		return ret;	
		}catch(e) {Log.error(e);return false;}
	}
	this.checkForm = checkForm;

	this.submit = function submit(p_submitForm,p_submitClass,p_submitClassMethod) {
		var ret = false;
	
		// Prepare for xformCheck	
		var arrarg = new Array();
		arrarg[0] = p_submitForm;
		for(var i=0; i <= arguments.length-4; i++) {
			// Arguments after p_submitClassMethod will be checked
			arrarg[i+1] = arguments[i+3];		
		}
		ret = checkForm.apply(this,arrarg);	

		// Prepare to send form elements
		var xformElements = new Array();
		xformElements.push(p_submitClass);
		xformElements.push(p_submitClassMethod);
		for(var i=0; i<p_submitForm.elements.length;i++){
			var element = p_submitForm.elements[i];
			if(element.tagName == "FIELDSET")
				continue;
			if(element.type == 'submit')
				continue;
			if(element.type == 'reset')
				continue;
			if(element.type == 'button')
				continue;
			if(element.type == 'password'){
				if(typeof(MD5) == "function") {
					// [optional] - Password fields are md5 encrypted before transfered
					var value = element.value;
					xformElements.push(value);
				} else {
					xformElements.push(element.value);					
				}
			} else if(element.type == 'radio') {
				// Any radio element is a form element
				var radioElement = document.getElementsByName(element.name);
				for(var k=0;k<radioElement.length;k++) {
					if(radioElement[k].checked){
						xformElements.push(radioElement[k].value);
						break;
					}
				}
				// IMPORTANT TO RISE FORM ELEMENT INDEX!
				i = i + radioElement.length-1;
			} else if(element.type == 'select-multiple') {
				// Select-multiple element is only one form element
				var selectElement = document.getElementsByName(element.name)[0];
				var selectedElements = new Array();
				for(var k=0;k<selectElement.length;k++) {
					if(selectElement[k].selected){
						selectedElements.push(selectElement[k].value);
					}
				}		
				xformElements.push(selectedElements);
			} else if(element.type == 'checkbox') {
				if(element.checked) xformElements.push(true);
				else xformElements.push(false);
			} else {
				// Any other form element			
				xformElements.push(element.value);
			}
		}
		// Prepare and send Sajax request
		if(ret) {
			for(var i=0; i<p_submitForm.elements.length;i++){
				var element = p_submitForm.elements[i];
				element.disabled = true;
			}
			var xformCb = new Object();
			xformCb.callback = submitCb;
			xformCb.calldata = p_submitForm;	
			xformElements.push(xformCb);

			Ajax.request.apply(this,xformElements);
		}	
		return false;
	}
	
	
	 function submitCb(p_cb,p_form){
		for(var i=0; i<p_form.elements.length;i++){
			var element = p_form.elements[i];
			element.disabled = false;
		}
		CMgr.evalCb(p_cb);
	}
	this.submitCb = submitCb;
	
	this.__construct = function __construct() {
	
	}
}
var Form = new Form();