/********************************************************************************************************
*
*	This javascript file contains methods and objects to aid the handling of page events
*	for custom controls. This file should be included BEFORE any custom control files/code
*	and included ONLY ONCE.
*				
*	If the window.onload event needs to be overriden, then the page_load method must be called
*	in the overriden method. If the "window.onload" event is overriden before the inclusion point
*	for this code IT WILL NOT FIRE.
*
*	E.g.
*		<script language="javascript">window.onload = function(){DoSomething();page_load();}</script>
*		<script language="javascript" src="CustomControls.js"></script>
*
*	In the above instance the custom window.onload event will not fire.
*
*		<script language="javascript" src="CustomControls.js"></script>
*		<script language="javascript">window.onload = function(){DoSomething();page_load();}</script>
*
*	In this instance it will.
*
*	if any page events are required with custom code, then the event handler must be registered with
*	custom control engine, and it will be fired in turn:
*
*		_eventHandlers.addPageEventHandler(EH_MOUSECLICK, "myCustomHandler");
*
*	NB: The custom handler method is referenced as a string without parenthases.
*	The appropriate event object will be passed to the function referenced, therefore the signature
*	for the method should be as follows:
*			
*		function myCustomHander(e){}
*
*	Where "e" is the event object for the specific browser (window.event for IE and e for Mozilla)
*
*	For mouse events the passed event object can be made multibrowser compatible using the
*	getEventStruct(e) global method which returns a structure with the same signature as the IE 
*	window.event object but containing the data for the current browser.
*
******************************************************************************************************/
/***********************************
*	Events enumeration
***************************/
//Document events
var EH_MOUSEDOWN	=	0;
var EH_MOUSEUP		=	1;
var EH_MOUSEMOVE	=	2;
var EH_MOUSECLICK	=	3;
var EH_KEYDOWN		=	4;
var EH_KEYPRESS		=	5;
var EH_KEYUP		=	6;
var EH_CONTEXTMENU	=	7;
//Window events
var EH_WINDOWRESIZE	=	8;
var EH_WINDOWSCROLL	=	9;
var EH_WINDOWUNLOAD	=	10;
var EH_WINDOWBLUR	=	11;
var EH_WINDOWFOCUS	=	12;
/***********************************
*	The main page load event handler
*	if the page load event is to be overriden either
*	either with a window.onload or a body.onload (javascript or html)
*	then the page_load method must also be called in the handling method
***************************/
// GPM - Use better method for initialisation rather than overriding other page onload events
//window.onload = page_load;
if (window.addEventListener) {
	window.addEventListener('load', page_load, false);
} else if (window.attachEvent) {
	window.attachEvent('onload', page_load);
} else { 
	window.onload = page_load;
}

/***********************************
*	Global EventHandlers object
***************************/
var _eventHandlers = new EventHandlers();
/***********************************
*	Global array containing all methods to call on page load
***************************/
var _loadEvents = new Array();
/***********************************
*	Global custom control container object
***************************/
var _controls = new CustomControlsContainer();
/***********************************
*	Global method for adding a page load event handler
***************************/
function addLoadEvent(handler){
	_loadEvents[_loadEvents.length] = handler;
}
/***********************************
*	EventHandlers class
***************************/
function EventHandlers(){
	this.handlers = new Array();
}
/***********************************
*	EventHandlers.addPageEvent method
*	Adds the passed method to the handlers member array
***************************/
EventHandlers.prototype.addPageEventHandler = function(theEvent, handler){
	if(!this.handlers[theEvent])
		this.handlers[theEvent] = new Array();
	this.handlers[theEvent][this.handlers[theEvent].length] = handler;
}
/***********************************
*	EventHandlers.handlePageEvent method
*	This method calls the appropriate events based on the passed event enumeration value
*	
*	N.B. This is a SYSCALL method and should not be called 
*	by a custom script
***************************/
/*__SYSCALL*/EventHandlers.prototype.handlePageEvent = function(eventType, e){
	var ret = true;
	var handlerRet = true;
	var handlers = this.handlers[eventType];
	if(!handlers) return ret;
	var execString = "";
	for(var i = 0; i < handlers.length; i++){
		execString = handlers[i] + "(e)";
		handlerRet = eval(execString);
		if(typeof(handlerRet) != "undefined")
			if(!handlerRet)
				ret = false;
	}
	return ret;
}
/***********************************
*	Global event handlers
*	
*	N.B. These are all SYSCALL methods and should not be invoked 
*	by a custom script
***************************/
/*__SYSCALL*/function page_mousedown(e){return _eventHandlers.handlePageEvent(EH_MOUSEDOWN, e);}
/*__SYSCALL*/function page_mouseup(e){return _eventHandlers.handlePageEvent(EH_MOUSEUP, e);}
/*__SYSCALL*/function page_mousemove(e){return _eventHandlers.handlePageEvent(EH_MOUSEMOVE, e);}
/*__SYSCALL*/function page_click(e){return _eventHandlers.handlePageEvent(EH_MOUSECLICK, e);}
/*__SYSCALL*/function page_keydown(e){return _eventHandlers.handlePageEvent(EH_KEYDOWN, e);}
/*__SYSCALL*/function page_keypress(e){return _eventHandlers.handlePageEvent(EH_KEYPRESS, e);}
/*__SYSCALL*/function page_keyup(e){return _eventHandlers.handlePageEvent(EH_KEYUP, e);}
/*__SYSCALL*/function page_contextmenu(e){return _eventHandlers.handlePageEvent(EH_CONTEXTMENU, e);}
/*__SYSCALL*/function page_unload(e){return _eventHandlers.handlePageEvent(EH_WINDOWUNLOAD, e);}
/*__SYSCALL*/function page_scroll(e){return _eventHandlers.handlePageEvent(EH_WINDOWSCROLL, e);}
/*__SYSCALL*/function page_resize(e){return _eventHandlers.handlePageEvent(EH_WINDOWRESIZE, e);}
/*__SYSCALL*/function page_blur(e){return _eventHandlers.handlePageEvent(EH_WINDOWBLUR, e);}
/*__SYSCALL*/function page_focus(e){return _eventHandlers.handlePageEvent(EH_WINDOWFOCUS, e);}
/***********************************
*	Global page_load event handler
*	this method must be called when the page is
*	loaded, either by the engine call, or in
*	the user defined onload handler
*	
*	N.B. This is a SYSCALL method and should not be invoked 
*	by a custom script
***************************/
/*__SYSCALL*/function page_load(e){for(var i = 0; i < _loadEvents.length; i++)eval(_loadEvents[i] + "()");}
/***********************************
*	Global helper method for browser compatibility
*	the method returns a structure containing all
*	the IE event object properties, in the case of
*	a Mozilla browser, then property data will have
*	been exchanged for the appropriate Mozilla event
*	object property. The returned structure can then 
*	be used as if it was the IE event object (contains
*	the same properties) but will work in bot IE and 
*	Mozilla browsers.
***************************/
function getEventStruct(e){
	if(!e) var e = window.event;
	var eventStruct = {
		clientX : e.clientX ? e.clientX : e.pageX,
		clientY : e.clientY ? e.clientY : e.pageY,
		srcElement : e.srcElement ? e.srcElement : e.target,
		button : document.all ? e.button : e.button+1
	}
	return eventStruct;
}
/***********************************
*	Capture all window and document events
*	the engine may need to pass control to
*	a js usercontrol
***************************/
document.onmousedown = page_mousedown;
document.onmouseup = page_mouseup;
document.onmousemove = page_mousemove;
document.click = page_click;
document.keydown = page_keydown;
document.keypress = page_keypress;
document.keyup = page_keyup;
document.contextmenu = page_contextmenu;
if(document.all){
	window.unload = page_unload;
	window.scroll = page_scroll;
	window.resize = page_resize;
	window.blur = page_blur;
	window.focus = page_focus;
}
else{
	window.captureEvents(Event.Unload);
	window.captureEvents(Event.Scroll);
	window.captureEvents(Event.Resize);
	window.captureEvents(Event.Blur);
	window.captureEvents(Event.Focus);
	window.unload = page_unload;
	window.scroll = page_scroll;


	window.resize = page_resize;
	window.blur = page_blur;
	window.focus = page_focus;
}
/***********************************
*	CustomControlsContainer class
***************************/
function CustomControlsContainer(){
	this.controls = new Array();
}
/***********************************
*	CustomControlsContainer.add method
*	Adds a custom control to the control container
***************************/
CustomControlsContainer.prototype.add = function(control){
	this.controls[this.controls.length] = control;
}
/***********************************
*	CustomControlsContainer.getControlById method
*	Returns a control based on the id of the control
***************************/
CustomControlsContainer.prototype.getControlById = function(id){
	for(var i = 0; i < this.controls.length; i++)
		if(this.controls[i].id)
			if(this.controls[i].id == id)
				return this.controls[i];
	return null;
}

CustomControlsContainer.prototype.getControlsByType = function(type){
	var retarr = new Array();
	for(var i = 0; i < this.controls.length; i++)
		if(this.controls[i].type)
			if(this.controls[i].type == type)
				retarr[retarr.length] = this.controls[i];
	return retarr;
}

CustomControlsContainer.prototype.scanForChanges = function(){
	for(var i = 0; i < this.controls.length; i++)
			if(this.controls[i].scanForChanges)
				this.controls[i].scanForChanges();
}

/* Handle max length on textareas */
function checkMaxLength(txtarea, mxlngth)
{
	if (txtarea.value.length > mxlngth)
	{
		txtarea.value = txtarea.value.substring(0, mxlngth);
	}
}

/* *** */

//Set all the default values for the hidden inputs that corresponds to checkboxes
function FBForm_SetCheckboxDefaultValue()
{
	//iterate through all inputs on the page
	var inputs = document.getElementsByTagName('input');

	for (var i = 0; i < inputs.length; i++)
	{
		// check to see if they are a formbuilder checkbox
		if (inputs[i].getAttribute('type') == 'checkbox' && inputs[i].id.search('fd_control') >= 0)
		{
			//check to see if it has a corresponding hidden input;
			var hidden = document.getElementById(inputs[i].id + '_check')
			if (hidden)
			{
				//if got this far, set the hidden value to be the same as the checkbox's current state
				hidden.value = (inputs[i].checked ? 'True' : 'False');
			}
		}
	}
}

/* *** */