/*
	(c) Copyright Mobile Bridges Ltd 2005. All Rights Reserved.

	Project: Project independent
	Version: 1.0
	Date:    04-03-2006

	Description: Eventhandler for ActionForm, generated by Code Generator
*/

/*
 *  define outerHTML setter and getter for non IE browsers
 */
if (navigator.appVersion.indexOf("MSIE") == -1) {

	var theEmptyTagList = {
	   "IMG":   true,
	   "BR":    true,
	   "META":  true,
	   "LINK":  true,
	   "PARAM": true,
	   "HR":    true
	};

	HTMLElement.prototype.__defineGetter__("outerHTML", function () {
	   var theAttributeList = this.attributes;
	   var theString = "<" + this.tagName;
	   for (var i = 0; i < theAttributeList.length; i++)
	      theString += " " + theAttributeList[i].name + "=\"" + theAttributeList[i].value + "\"";

	   if (theEmptyTagList[this.tagName])
	      return theString + ">";

	   return theString + ">" + this.innerHTML + "</" + this.tagName + ">";
	});

	HTMLElement.prototype.__defineSetter__("outerHTML", function (inHTML) {
	   var theRange = this.ownerDocument.createRange();
	   theRange.setStartBefore(this);
	   var theContextualFragment = theRange.createContextualFragment(inHTML);
	   this.parentNode.replaceChild(theContextualFragment, this);
	});
}


var theXMLHttpRequest = null;

function CreateXMLHttpRequest() {
	if (!theXMLHttpRequest)	{
		if (window.XMLHttpRequest)
			theXMLHttpRequest = new XMLHttpRequest();
		else if (window.ActiveXObject)
			//theXMLHttpRequest = new ActiveXObject("MSXML2.XMLHTTP");
			theXMLHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
	}

	return theXMLHttpRequest;
}

function AjaxCallFrontController() {
	
	fireEvent('AjaxBegin');
	
	var theForm = document.getElementById("Form");
	if (theForm) {
		var thePage = theForm.Page.value;
		var theData = "ClassName="+gProjectPrefix+thePage+"FrontController&";
        var HasFiles = false;
		
		var theElements = theForm.elements;
		for (i=0;i<theElements.length;i++) {
			var theElement = theElements[i];
			var theType = theElement.type;
			if ((theType == "text") || (theType == "password") || (theType == "hidden"))
				theData += GetInputTextData(theElement);
			else if (theType == "radio")
				theData += GetInputRadioData(theElement,inData);
			else if (theType == "checkbox")
				theData += GetInputCheckBoxData(theElement);
			else if ((theType == "select-one") || (theType == "select-multiple"))
            	theData += GetSelectBoxData(theElement);
			else if (theType == "textarea")
	        	theData += GetTextAreaData(theElement);
        }

		theData = theData.substr(0,theData.length-1);

		var theXMLHttpRequest = CreateXMLHttpRequest();
		if (theXMLHttpRequest) {
			var theUrl = "/Service/"+gProjectPrefix+"AjaxService.php5";
			theXMLHttpRequest.open("POST",theUrl,true);
            theXMLHttpRequest.setRequestHeader("Content-type","application/x-www-form-urlencoded");
			
			theXMLHttpRequest.onreadystatechange = FrontControllerCallBack;

			theXMLHttpRequest.send(theData);
		}
	}
}

function GetInputTextData(inElement) {
	var theName = inElement.id || inElement.name;
	var theValue = inElement.value;

	return theName+"="+escape(theValue)+"&";
}

function GetInputRadioData(inElement, inData) {
	var theData = "";
	var theName = inElement.id || inElement.name;

	var theRegExp = new RegExp(theName);
	if (inData.search(theRegExp) == -1)	{
		if (inElement.checked) {
			var theValue = inElement.value;
			theData += theName+"="+escape(theValue)+"&";
		}
	}

	return theData;
}

function GetInputCheckBoxData(inElement) {
	var theData = "";
	var theName = inElement.id || inElement.name;

	if (inElement.checked) {
		var theValue = inElement.value;
		theData += theName+"="+escape(theValue)+"&";
	}

	return theData;
}

function GetSelectBoxData(inElement) {
	var theData = "";
	var theName = inElement.id || inElement.name;

	var theOptions = inElement.options;
	var theSelectedIndex = inElement.selectedIndex;

	if (theSelectedIndex != -1)	{
		var theValue = theOptions[theSelectedIndex].value;
		theData += theName+"="+escape(theValue)+"&";
	}

	return theData;
}

function GetTextAreaData(inElement) {
	var theName = inElement.id || inElement.name;
	var theValue = inElement.value;

	return theName+"="+escape(theValue)+"&";
}

function FrontControllerCallBack() {
	var theXMLHttpRequest = CreateXMLHttpRequest();
	if ((theXMLHttpRequest.readyState == 4) && (theXMLHttpRequest.status == 200))
		AjaxCallView();
}

function AjaxCallView() {
	var theForm = document.getElementById("Form");
	if (theForm) {
		var thePage = theForm.Page.value;
		var theData = "ClassName="+gProjectPrefix+thePage+"View&";

		var theElements = theForm.elements;
		for (i=0;i<theElements.length;i++) {
			var theElement = theElements[i];
			var theType = theElement.type;
			if ((theType == "text") || (theType == "password") || (theType == "hidden"))
				theData += GetInputTextData(theElement);
			else if (theType == "radio")
				theData += GetInputRadioData(theElement,inData);
			else if (theType == "checkbox")
				theData += GetInputCheckBoxData(theElement);
			else if ((theType == "select-one") || (theType == "select-multiple"))
            	theData += GetSelectBoxData(theElement);
			else if (theType == "textarea")
	        	theData += GetTextAreaData(theElement);
        }

		theData = theData.substr(0,theData.length-1);

		var theXMLHttpRequest = CreateXMLHttpRequest();
		if (theXMLHttpRequest) {
			var theUrl = "/Service/"+gProjectPrefix+"AjaxService.php5";
			theXMLHttpRequest.open("POST",theUrl,true);

			theXMLHttpRequest.setRequestHeader("Content-type","application/x-www-form-urlencoded");
			theXMLHttpRequest.onreadystatechange = ViewCallBack;

			theXMLHttpRequest.send(theData);
		}
	}
}

function LoadXML(inXml) {
	var theDOMDocument = null;

	if (window.ActiveXObject) {
		//theDOMDocument = new ActiveXObject("MSXML2.XMLHTTP");
		theDOMDocument = new ActiveXObject("Microsoft.XMLDOM");
		theDOMDocument.async = false;
		theDOMDocument.loadXML(inXml);
	}
	else {
		var theDOMParser = new DOMParser();
		theDOMDocument = theDOMParser.parseFromString(inXml,"text/xml");
	}

	return theDOMDocument;
}

function ViewCallBack() {
	var theXMLHttpRequest = CreateXMLHttpRequest();

	if ((theXMLHttpRequest.readyState == 4) && (theXMLHttpRequest.status == 200)) {
		
		theResponseText = theXMLHttpRequest.responseText;
		var theDOMDocument = LoadXML(theResponseText);

		if (theDOMDocument)	{
			theElements = theDOMDocument.documentElement.childNodes;

			for (i=0;i<theElements.length;i++) {
				var theElement = theElements[i];

				if (theElement.nodeType == 1) {
					theId = theElement.getAttribute("id");
					var theSourceElement = document.getElementById(theId);

					if (theSourceElement) {
                		if (theElement.xml)
							theSourceElement.outerHTML = "\n"+theElement.xml;
						else {
							var theXMLSerializer = new XMLSerializer();

							var theString = theXMLSerializer.serializeToString(theElement);
							var regex = new XRegExp("<textarea([^<]*)/>", "gim");
                            var output = theString.replace(regex, "<textarea$1></textarea>");
                            
							theSourceElement.outerHTML = output;
						}
					}
				}
			}
		}
		fireEvent('AjaxDone');
	}
}

var gProjectPrefix = "";
function InitializeAjax(inProjectPrefix) {
	gProjectPrefix = inProjectPrefix;

	theForms = document.forms;
	for (i=0;i<theForms.length;i++) {
		// If two grids are shown and one of them needs to upload files
		// (which can't be done by ajax request), the ajax request function can be disabled with an
		// extra attribute in the form tag: <form action="..." noajax="noajax">...</form>
		if (!theForms[i].getAttribute('noajax')) {
			theForms[i].action = "javascript:AjaxCallFrontController()";
			theForms[i].onsubmit = AjaxCallFrontController;		
		}
	}
}


// These functions only work when MooTools is included previously
// When not using MooTools, copy the this code to the ajax.js file at their corresponding fireEvent calls
window.addEvent('AjaxDone', function() {
	
	// Turn off wait screen
	var theOverlay = document.getElementById("WaitOverlay");
	var theBodyTable = document.getElementById("bodytable");
	
	if(theOverlay) {
		theOverlay.style.display = "none";
		theOverlay.style.cursor = "auto";
		if(theOverlay)
			theOverlay.style.height = theBodyTable.offsetHeight+'px';
	}
	
	// Turn off wait button effect
	var theButton = document.getElementById("FormSubmit");
	if(theButton) {
		theButton.disabled = false;
		theButton.style.cursor = "auto";
	}
	
	var theUrl = document.getElementById("Redirect");
	if(theUrl) {
		window.location = theUrl.value;
	}
    	                       	
});

// These functions only work when MooTools is included previously
// When not using MooTools, copy the this code to the ajax.js file at their corresponding fireEvent calls
window.addEvent('AjaxBegin', function() {
	
	// Turn on wait screen
	var theOverlay = document.getElementById("WaitOverlay");
	if(theOverlay) {
		var theBodyTable = document.getElementById("bodytable");
			theOverlay.style.height = theBodyTable.offsetHeight+'px';
		theOverlay.style.display = "block";
		theOverlay.style.cursor = "wait";
	} 
	
	// Turn on wait button effect
	var theButton = document.getElementById("FormSubmit");
	if(theButton) {
		theButton.disabled = true;
		theButton.style.cursor = "wait";
	}
	
});