﻿function jenaForm()
{
	var self = this;
	
	this.clearForm = function(pId)
	{
		var Elements, iElem;
	
		Elements = jCore("#" + pId).find("SELECT").member;
		if (Elements !== null)
		{
			for (iElem = 0; iElem < Elements.length; iElem++)
			{
				Elements[iElem].options[0].selected;
			}
		}
		Elements = jCore("#" + pId).find("INPUT").member;
		if (Elements !== null)
		{
			for (iElem = 0; iElem < Elements.length; iElem++)
			{
				Elements[iElem].value = "";
			}
		}
	};
	this.clearControl = function(pControl)
	{
		switch (pControl.tagName.toLowerCase())
		{
			case "select":
				pControl.options.length = 0;
				break;
				
			case "text":
				jForm().value(pControl, "");
				break;
		}
	};
	//test whether the enter was pressed
	this.enter = function(pKeyEvent)
	{
		return (this.getKeyPress(pKeyEvent) === 13);
	};
	//test the key press event to determine what key was pressed
	this.getKeyPress = function(pKeyEvent)
	{
		var retValue = (window.event) ? window.event.keyCode : pKeyEvent.which;
		return retValue;
	};
	this.getControl = function(pName)
	{
		if (typeof pName == "string") 
		{
			if (pName.indexOf("#") == 0) pName = pName.substring(1);
			pName = document.getElementById(pName);
		}
		
		return pName;
	};
	//create the xmlHttp object reference
	this.getXmlObject = function()
	{
		var oXml = null;
		
		if (window.XMLHttpRequest)
		{
			oXml = new XMLHttpRequest();
		}
		else if (window.ActiveXObject)
		{
			try
			{
				oXml = new ActiveXObject("MSXML2.XMLHTTP");
			}
			catch (e)
			{
				try
				{
					oXml = new ActiveXObject("Microsoft.XMLHTTP");
				}
				catch (e)
				{
				}
			}
		}
		
		return oXml;
	};
	//what to do when the enter key has been pressed
	this.onEnter = function(pKeyEvent, pEvent)
	{
		if (this.enter(pKeyEvent))
		{
			pEvent();
		}
		return false;
	};
	//set focust to the desired control
	this.setFocus = function(pControl)
	{
		if (typeof pControl == "string")
		{
			pControl = document.getElementById(pControl);
		}
		
		if (pControl !== null)
		{
			if ((helperMode === true) && (jTip !== null))
			{
				var HelperText = pControl.getAttribute("helper");
				if (HelperText === null) HelperText = "";
			
				if (HelperText !== "") jTip(pControl).show(HelperText);
			}
			
			if (pControl.tagName.toLowerCase() == "input")
			{
				pControl.focus();
				pControl.select();
			}
		}
	};
	//submit the data to the desired page (url) asynchronously (pAsunc) with arguments (pArguments) 
	//and type (pContentType) with headers (pHeaders) and optional on-complete function call (pOnComplete)
	this.submitPage = function(pPageUrl, pAsync, pArguments, pContentType, pHeaders, pOnComplete)
	{
		var sObject = "Microsoft.XMLHTTP";
		var sApp = (pContentType == null ? "application/x-www-form-urlencoded" : pContentType);
		var oRequest = this.getXmlObject();
		var Method = (pArguments == null) ? "GET" : "POST";
		pAsync = (pAsync) ? pAsync : false;
		
		if (oRequest)
		{
			if (pAsync)
			{		
				oRequest.onreadystatechange = function()
				{
					if (oRequest.readyState == 4)
					{
						if (pOnComplete != null)
						{
							pOnComplete(oRequest.responseText);
						}
					}
				}; 
			}
			pPageUrl += (pPageUrl.indexOf("?") > 0 ? "&" : "?") + ((pArguments == null) ? "callback" : "postback") + "=true";

			oRequest.open(Method, pPageUrl, pAsync);
			oRequest.setRequestHeader("Content-Type", sApp);
			if (pHeaders != null)
			{
				for (var Key in pHeaders)
				{
					oRequest.setRequestHeader(Key, pHeaders[Key]);
				}
			}
			oRequest.send(pArguments);
			
			if (!pAsync)
			{
				document.body.style.cursor = 'wait';

				while (oRequest.readyState != 4)
				{
				}
				if (oRequest.status != 500)
				{
					if (oRequest.status == 404)
					{
					}
					else
					{
						while (oRequest.status != 200)
						{
						}
					}
					
					try
					{
						document.body.style.cursor = 'default';
						return oRequest.responseText;
					}
					catch(e)
					{
					}
				}
				else
				{
					document.body.style.cursor = 'default';
					return oRequest.responseText;
				}
			}
		}	
	};
	//get the value from the desired control
	this.value = function(pName, pValue)
	{
		var RetValue = "";

		pName = this.getControl(pName);
		
		if (pName != null)
		{
			if (pValue != null) this.setValue(pName, pValue);
			else RetValue = pName.value;
		}
		
		return RetValue;
	};
	this.values = function(pId, pSetTo)
	{
		function getElementName(pControl)
		{
			if (pControl.id !== "")
			{
				return pControl.id;
			}
			else
			{
				return pControl.getAttribute("name");
			}
		};
		
		var allElements = jCore("#" + pId).find("input").member;
		var RetValue = "";
		if (pSetTo === undefined) pSetTo = null;
		
		for (var iElem = 0; iElem < allElements.length; iElem ++)
		{
			if (pSetTo === null)
			{
				value = allElements[iElem].value;
				if (allElements[iElem].type === "checkbox") value = allElements[iElem].checked.toString();
				
				RetValue += getElementName(allElements[iElem]) + "," + value.Escape() + ",";
			}
			else
			{
				this.setValue(allElements[iElem], pSetTo);
			}
		}
		
		allElements = jCore("#" + pId).find("select").member;
		for (var iElem = 0; iElem < allElements.length; iElem ++)
		{
			if (pSetTo === null)
			{
				value = "";
				for (var iOption = 0; iOption < allElements[iElem].options.length; iOption++)
				{
					if (allElements[iElem].options[iOption].selected === true)
					{
						value += ";" + allElements[iElem].options[iOption].value.Escape();
					}
				}
				if (value !== "") value = value.substring(1);
				
				RetValue += getElementName(allElements[iElem]) + "," + value + ",";
			}
			else
			{
				this.setValue(allElements[iElem], pSetTo);
			}
		}
		
		allElements = jCore("#" + pId).find("textarea").member;
		for (var iElem = 0; iElem < allElements.length; iElem ++)
		{
			if (pSetTo === null)
			{
				RetValue += getElementName(allElements[iElem]) + "," + allElements[iElem].value.Escape() + ",";
			}
			else
			{
				this.setValue(allElement[iElem], pSetTo);
			}
		}
		
		if (pSetTo === null)
		{
			if (RetValue !== "") RetValue = RetValue.substring(0, RetValue.length - 1);

			return RetValue.split(",");
		}
	};
	this.text = function(pName, pValue)
	{
		var RetValue = "";
		
		pName = this.getControl(pName);
		
		if (pName != null)
		{
			if (pValue != null) this.setValue(pName, pValue);
			else RetValue = pName[pName.selectedIndex].text;
		}
		
		return RetValue;
	};
	this.setValue = function(pControl, pValue)
	{
		switch (pControl.tagName.toLowerCase())
		{
			case "select":
				for (var iSelect = 0; iSelect < pControl.length; iSelect++)
				{
					if (pValue == pControl[iSelect].text || pValue == pControl[iSelect].value) pControl[iSelect].selected = true;
				}
				break;
				
			case "input":
			case "textarea":
				if (jCore(pControl).attribute("type").toLowerCase() == "checkbox") {pControl.checked = pValue;}
				else {pControl.value = pValue;}
				break;
		}
	};
	//get or sst the attribute (pAttribute) value of the form control (pName)
	this.attr = function(pName, pAttribute, pValue)
	{
		var RetValue = "";
		
		if (typeof pName == "string") pName = document.getElementById(pName);
		
		if (pName != null)
		{
			if (pValue != null) pName.setAttribute(pAttribute, pValue);
			else 
			{
				if (pName.attributes != null)
				{
					if (pName.attributes.getNamedItem(pAttribute) != null)
					{
						RetValue = pName.attributes.getNamedItem(pAttribute).value;
					}
				}
			}
		}
		
		return RetValue;
	};
	
	return self;
};

jenaForm.prototype =
{
	//set the control on the form to "disabled"
	disable:function(pName)
	{
		pName = this.getControl(pName);
		pName.disabled = true;
	},
	//set the control on the form to "enabled"
	enable:function(pName)
	{
		pName = this.getControl(pName);
		pName.disabled = false;
	},
	setStatus:function(pName, pEnabled)
	{
		if (pEnabled) this.enable(pName); else this.disable(pName);
	},
	setFormControlStatus:function(pParent, pEnabled, pControlType)
	{
		if (typeof pParent === "string") pParent = jCore("#" + pParent).member;
		
		var controls = jCore(pParent).find(pControlType).member;
		for (var iControl = 0; iControl < controls.length; iControl++)
		{
			this.setStatus(controls[iControl], pEnabled);
		}
	},
	setFormStatus:function(pParent, pEnabled)
	{
		this.setFormControlStatus(pParent, pEnabled, "INPUT");
		this.setFormControlStatus(pParent, pEnabled, "SELECT");
		this.setFormControlStatus(pParent, pEnabled, "TEXTAREA");
		this.setFormControlStatus(pParent, pEnabled, "A");
	},
	//move the cell left by removing the first indentation span
	moveCellLeft:function(pElement)
	{
		var allSpans = jCore(pElement).find("span>@Indent");
		if (allSpans.size > 0)
		{
			jCore(allSpans.getMember(0)).remove();
		}
	},
	//move the cell right by adding an indentation span
	moveCellRight:function(pElement)
	{
		var Span = jCore().create("span").css("Indent").text("&nbsp;").member;
		jCore(pElement).insert(Span, 0);
	},
	//get the row and sibling to move a cell down in a table structure
	moveRowDown:function(pElement)
	{
		var Return = {Row:null, Sibling:null};
		
		Return.Row = jCore(pElement).parent().member.nextSibling;
		if (Return.Row !== null)
		{
			Return.Sibling = jCore(pElement).parent().member;	
			jCore(Return.Row).remove();
		}
		
		return Return;
	},
	//get the row and sibling to move a cell up in a table structure
	moveRowUp:function(pElement)
	{
		var Return = {Row:null, Sibling:null};
		
		Return.Sibling = jCore(pElement).parent().member.previousSibling;
		if (Return.Sibling !== null) Return.Row = jCore(pElement).parent().remove(true);
		
		return Return;
	},
	//move a cell in the table structure (this actually moves the row)
	moveRow:function(pElement, pDirection)
	{
		var Parent = jCore(pElement).parent(2),
			Sibling = null,
			Row = null,
			Move = null;
			
		if ("updown".indexOf(pDirection) >= 0)
		{
			if (pDirection == "up") Move = this.moveRowUp(pElement);
			else Move = this.moveRowDown(pElement);
			
			if (Move.Sibling !== null && Move.Row !== null && Parent !== null)
			{
				Parent.insert(Move.Row, Move.Sibling);
			}
		}
		else if ("leftright".indexOf(pDirection) >= 0)
		{
			if (pDirection == "left") this.moveCellLeft(pElement);
			else this.moveCellRight(pElement);
		}
	}
};

jenaForm.prototype.callPage = function(pPageUrl, pAsync)
{
	var sArguments = jCore().args().concat(this.callPage.arguments, 2);

	if (sArguments != "") pPageUrl += (pPageUrl.indexOf("?") > 0 ? "&" : "?") + sArguments;

	if (pAsync) jForm().submitPage(pPageUrl, true, null);
	else return jForm().submitPage(pPageUrl, false, null);
};

jenaForm.prototype.postPage = function(pPageUrl, pAsync)
{
	var sArguments = jCore().args().concat(this.postPage.arguments, 2);

	if (pAsync) jForm().submitPage(pPageUrl, true, sArguments);
	else return jForm().submitPage(pPageUrl, false, sArguments);
};
	
var jForm = function()
{
	return new jenaForm();
};