/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Component:	stlDhtmlxCommon
//
// Description:	Provides exposure of basic Dhtmlx features
//
// Version:		2010-07
//
// Copyright:	2010 Joost Brugman, Brugman Holding B.V.
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function stlDhtmlxCommon()
{
	var imagePath;
	var skin;
	var uriServerComponent;
	var uriStreamline;

	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	//
	// Function:	setImagePath()
	//
	// Description:	Sets the image path for the component.
	//
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	this.setImagePath = function(newPath)
	{
		imagePath = newPath;
	}
	
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	//
	// Function:	getImagePath()
	//
	// Description:	Gets the image path for the component.
	//
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	this.getImagePath = function()
	{
		return imagePath;
	}

	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	//
	// Function:	setSkin()
	//
	// Description:	Sets the skin for the component.
	//
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	this.setSkin = function(newSkin)
	{
		skin = newSkin;
	}
	
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	//
	// Function:	getSkin()
	//
	// Description:	Gets the skin for the component.
	//
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	this.getSkin = function()
	{
		return skin;
	}
	
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	//
	// Function:	setUriStreamline()
	//
	// Description:	Sets the url location to the streamline server side components.
	//
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	this.setUriStreamline = function(newUri)
	{
		uriStreamline = newUri;
	}

	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	//
	// Function:	getUriStreamline()
	//
	// Description:	Gets the url location to the streamline server side components.
	//
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	this.getUriStreamline = function()
	{
		return uriStreamline;
	}
	
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	//
	// Function:	generateSystemError()
	//
	// Description:	Renerates a system error.
	//
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	this.generateSystemError = function(message)
	{
		alert(message);
		return false;
	}

	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	//
	// Function:	postSync()
	//
	// Description:	Post a request to the server side component
	//
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	this.postSync = function(postVars, getVars)
	{
		var loader = dhtmlxAjax.postSync(this.getUriServerComponent() + (getVars == undefined ? "" : "?" + getVars), postVars);
		
		return this.analyseLoader(loader);
	}
	
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	//
	// Function:	getSync()
	//
	// Description:	Gets a request to the server side component
	//
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	this.getSync = function(getVars)
	{
		var loader = dhtmlxAjax.getSync(this.getUriServerComponent() + (getVars == undefined ? "" : "?" + getVars));

		return this.analyseLoader(loader);
	}
	
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	//
	// Function:	analyseLoader()
	//
	// Description:	Analyses the response received by postSync() and getSync()
	//
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	this.analyseLoader = function(loader)
	{
		///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		// 1: Verify document was retrieved succesfully
		///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		if(loader.xmlDoc.status != 200)
		{
			stlApp.messages.add("Application error: Could not load data. HTTP " + loader.xmlDoc.status);
			return false;
		}

		///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		if(loader.xmlDoc.responseXML == null)
		{
			stlApp.messages.add("Application error: Invalid XML Response. Response is not a well formed XML document or content type is not set to text/xml.");
			return false;
		}

		var elStatus = loader.xmlDoc.responseXML.getElementsByTagName("status")[0];
		if(elStatus == null)
		{
			stlApp.messages.add("Application error: Invalid XML Response. Missing status element.");
			return false;
		}
		
		var elStatusText = elStatus.childNodes[0];
		if(elStatusText == undefined)
		{
			stlApp.messages.add("Application error: Invalid XML Response. Missing status content.");
			return false;
		}
		
		if(elStatusText.nodeValue !== "OK")
		{
			var elErrorCode		= loader.xmlDoc.responseXML.getElementsByTagName("errorcode")[0];
			if(elErrorCode == null)
			{
				stlApp.messages.add("Application error: Invalid XML Response. Missing errorcode element.");
				return false;
			}
			if(elErrorCode.childNodes[0] == undefined)
			{
				stlApp.messages.add("Application error: Invalid XML Response. Missing errorcode content.");
				return false;
			}
			var elErrorMessage	= loader.xmlDoc.responseXML.getElementsByTagName("errormessage")[0];
			if(elErrorMessage == null)
			{
				stlApp.messages.add("Application error: Invalid XML Response. Missing errormessage element.");
				return false;
			}		
			if(elErrorMessage.childNodes[0] == undefined)
			{
				stlApp.messages.add("Application error: Invalid XML Response. Missing errormessage content.");
				return false;
			}			
			
			stlApp.messages.add("Application error: " + elErrorMessage.childNodes[0].nodeValue);
			return false;
		}
		
		return loader.xmlDoc.responseXML;	
	}
	
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	//
	// Function:	findAncestorElement()
	//
	// Description:	For a given element finds an ancestor element of the given nodeName
	//
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	this.findAncestorElement = function(element, nodeName)
	{
		var reg = new RegExp(nodeName, "i");
	
		while(1)
		{
			element = element.parentNode;

			if(element == null)
				return null;
			if(element.nodeType == 9 /* DOCUMENT_NODE */)
				return null;
			if(element.nodeType != 1 /* ELEMENT_NODE */)
				continue;
			if(reg.test(element.nodeName) == false)
				continue;
			return element;
		}
	}

	/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	//
	// Constructor
	//
	/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

}

function stlMessages()
{
	this.show = function()
	{
	
	}
	
	this.hide = function()
	{
	
	}
	
	this.add = function(msg)
	{
		alert(msg);
	}

}

function stlLanguageEnabledClass()
{
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	//
	// Function:	lang()
	//
	// Description:	
	//
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	this.lang = function()
	{
		this.lang.en = function() {};
		this.lang.nl = function() {};
	}
	
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	//
	// Function:	setLanguage()
	//
	// Description:	
	//
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	this.getLanguage = function()
	{
		return this.language;
	}
	
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	//
	// Function:	setLanguage()
	//
	// Description:	
	//
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	this.setLanguage = function(language)
	{
		switch(language)
		{
			case "en":
				this.lang.current = this.lang.en;
				break;
			
			case "nl":
			default:
				language = "nl";
				this.lang.current = this.lang.nl;
				break;
		}
		this.language = language;
	}

	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	//
	// Function:	Constructor
	//
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		this.lang();
		this.setLanguage();
}

function stlWebComponent(idContainer)
{
	var uriServerComponent = "";

	/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	//
	// Inheritance: stlDhtmlxCommon, stlLanguageEnabledClass
	//
	/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	this.inheritFrom_stlDhtmlxCommon = stlDhtmlxCommon;
	this.inheritFrom_stlDhtmlxCommon();
	
	this.inheritFrom_stlLanguageEnabledClass = stlLanguageEnabledClass;
	this.inheritFrom_stlLanguageEnabledClass();

	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	//
	// Function:	initLanguages
	//
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	this.initLanguages = function()
	{
	}

	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	//
	// Function:	setUriServerComponent()
	//
	// Description:	Sets the url location to the server side component.
	//
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	this.setUriServerComponent = function(newUri)
	{
		uriServerComponent = newUri;
	}

	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	//
	// Function:	getUriServerComponent()
	//
	// Description:	Gets the url location to the server side component.
	//
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	this.getUriServerComponent = function()
	{
		return uriServerComponent;
	}
	
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	//
	// Function:	loadObjectToForm()
	//
	// Description:	Loads an the specified object from the server side component. The server side component must repond to a GET request with
	//				two parameters: ID=xxxx and Op=Get
	//
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	this.loadObjectToForm = function(id, callback)
	{
		var xml = this.getSync("ID=" + id + "&Op=Get");
		if(xml === false)
			return this.generateSystemError("ERROR\nCould not retrieve form contents for form " + this.idContainer + ".");

		var elUriEncodedFields = xml.getElementsByTagName("uriencodedfields")[0];
		if(elUriEncodedFields == null)
		{
			alert("ERROR\nInvalid XML Response.\n\nMissing uriencodedfields element.");
			return false;
		}
		
		var elUriEncodedFieldsText = elUriEncodedFields.childNodes[0];
		if(elUriEncodedFieldsText == undefined)
		{
			alert("ERROR\nInvalid XML Response.\n\nMissing uriencodedfields content.");
			return false;
		}

		var arrFieldValueCombos = elUriEncodedFieldsText.nodeValue.split("&");
		var currentFieldValueCombo;
		var arrFieldValuePair;

		var index;
		for(index in arrFieldValueCombos)
		{
			currentFieldValueCombo = arrFieldValueCombos[index];
			arrFieldValuePair = currentFieldValueCombo.split("=");
			callback(arrFieldValuePair[0], decodeURIComponent(arrFieldValuePair[1]));
		}
	}
	
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	//
	// Function:	Constructor
	//
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		this.idContainer				= idContainer;
		this.elContainer				= document.getElementById(this.idContainer);

		if(this.elContainer == null)
			return stlApp.messages.add("Appliation error: Could not find element " + this.idContainer + " to create form in.");
			
		this.initLanguages();
		this.setLanguage(stlApp.getLanguage());
}

function stlLibrary()
{
	this.getElementTop = function(el)
	{
		elTop	= el.offsetTop;
		var tempEl = el.offsetParent;
		while (tempEl != null)
		{
			elTop += tempEl.offsetTop;
			tempEl = tempEl.offsetParent;
		}
		return elTop;
	}

	this.getElementLeft = function(el)
	{
		elLeft	= el.offsetLeft;
		var tempEl = el.offsetParent;
		while (tempEl != null)
		{
			elLeft += tempEl.offsetLeft;
			tempEl = tempEl.offsetParent;
		}
		return elLeft;
	}
	
	this.tinymceHideToolbar = function(idEditor)
	{
		document.getElementById(idEditor + "_external").style.display = "none";
	}
	
	this.tinymceShowToolbar = function(idEditor)
	{
		document.getElementById(idEditor + "_external").style.display = "block";
	}

	this.tinymceHideToolbarsOfAlleditors = function()
	{
		var index;
		for(index = 0; index < tinyMCE.editors.length; index++)
			this.tinymceHideToolbar(tinyMCE.editors[index].id);
	}
}

function stlApplication()
{
	var that = this;
	var appComponentsPath = "";
	var stlComponentsPath = "";
	
	/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	//
	// Inheritance: stlLanguageEnabledClass
	//
	/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	this.inheritFrom_stlLanguageEnabledClass = stlLanguageEnabledClass;
	this.inheritFrom_stlLanguageEnabledClass();

	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	//
	// Function:	setAppComponentsPath
	//
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	this.setAppComponentsPath = function(path)
	{
		appComponentsPath = path;	
	}
	
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	//
	// Function:	getAppComponentsPath
	//
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	this.getAppComponentsPath = function()
	{
		return appComponentsPath;
	}
	
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	//
	// Function:	setStreamlineComponentsPath
	//
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	this.setStreamlineComponentsPath = function(path)
	{
		stlComponentsPath = path;	
	}
	
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	//
	// Function:	getStreamlineComponentsPath
	//
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	this.getStreamlineComponentsPath = function()
	{
		return stlComponentsPath;
	}
	
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	//
	// Function:	Constructor
	//
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		this.messages = new stlMessages();
		
		this.library = new stlLibrary();
}

var stlApp;
if(stlApp == undefined)
	stlApp = new stlApplication();

