Behaviour.addEvent('window',	'onerror',	function(desc,page,line) { framework.errorHandling(desc,page,line); });
Behaviour.addEvent('window',	'onload',		function() { framework.initialise(); });
Behaviour.addEvent('window',	'onload',		function() { framework.setSizes(); });
Behaviour.addEvent('window',	'onresize',	function() { framework.setSizes(); });

var framework={
	pageError:	false,
	debugging:	true,
	scrollWidth:	'',
	scrollHeight:	'',
	innerWidth:	'',
	innerHeight:	'',
	offsetX:		'',
	offsetY:		'',
	overlay:		false,
	overlayEl:	null,
	overlayDivEl:	null,
	showImageCloseButton:	true,
	OKText:		'OK',
	cancelText:	'Cancel',
	yesText:		'Yes',
	noText:		'No',
	
	initialise:		function() {
	},
	
	numberBetween:		function(theNumber, startNumber, endNumber) {
		if(!isNaN(theNumber) && !isNaN(startNumber) && !isNaN(endNumber)) {
			return (parseInt(theNumber)>=parseInt(startNumber) && parseInt(theNumber)<parseInt(endNumber));
		}
		return false;
	},
	
	errorHandling:	function(desc,page,line) {
		var returnValue			= true;
		var errorMessage			= 'JavaScript error occurred! \n'
			+'\nError description: \t'+desc
			+'\nPage address:      \t'+page
			+'\nLine number:       \t'+line
			+'\n\nClick OK to prevent wizard to submit';
		returnValue				= alert(errorMessage);
		returnValue				= true;
		try {
			this.pageError		= returnValue;
			if(typeof validate=='function') {validate();}
		} catch(err) {
		}
		return returnValue;
	},
	
	setSizes:		function() {
		var dE = document.documentElement || document.body, scrollWidth, scrollHeight;
		if (window.innerHeight && window.scrollMaxY) {
			scrollWidth	= window.innerWidth		+ window.scrollMaxX;
			scrollHeight	= window.innerHeight	+ window.scrollMaxY;
		} else {
			scrollWidth	= Math.max(document.body.scrollWidth,	document.body.offsetWidth);
			scrollHeight	= Math.max(document.body.scrollHeight,	document.body.offsetHeight);
		}
		var innerWidth		= dE.clientWidth	|| window.innerWidth;
		var innerHeight	= dE.clientHeight	|| window.innerHeight;
		if (window.innerWidth) {
			scrollWidth	+= innerWidth	- window.innerWidth;
			innerWidth	= window.innerWidth;
		}
		if (window.innerHeight) {
			scrollHeight	+= innerHeight	- window.innerHeight;
			innerHeight	= window.innerHeight;
		}
		this.scrollWidth	= scrollWidth;
		this.scrollHeight	= scrollHeight;
		this.innerWidth	= innerWidth;
		this.innerHeight	= innerHeight;
		this.offsetX		= window.pageXOffset || dE.scrollLeft;
		this.offsetY		= window.pageYOffset || dE.scrollTop;
	},
	
	createDateObject:	function(dateFormat, dateDivider, dateString) {
		var	dateArray;
		var	yearObj	= '';
		var	monthObj	= '';
		var	dayObj	= '';
		var	yearPos;
		var	monthPos;
		var	dayPos;
		
		var	dateDividers	= Array('-', '/', '\\', '.');
			dateDividers.push(dateDivider);
		
		for(i=0; i<dateFormat.length; i++) {
			switch(true) {
			case (dateFormat.charAt(i)=='Y'):
				yearPos	= i;
				break;
			case (dateFormat.charAt(i)=='M'):
				monthPos	= i;
				break;
			case (dateFormat.charAt(i)=='D'):
				dayPos	= i;
				break;
			}
		}
		
		for(i=dateDividers.length-1; i>=0; i--) {
			dateArray		= dateString.split(dateDividers[i]);
			if(dateArray.length==3) {
				yearObj	= dateArray[yearPos];
				monthObj	= dateArray[monthPos];
				dayObj	= dateArray[dayPos];
			}
		}
		return {
			year:	yearObj,
			month:	monthObj,
			day:		dayObj,
			format:	dateFormat,
			divider:	dateDivider,
			toString:	function() {
				var returnValue	=	dateFormat;
				returnValue		=	returnValue.replace('Y', parseInt(this.year)		+ this.divider);
				returnValue		=	returnValue.replace('M', parseInt(this.month)	+ this.divider);
				returnValue		=	returnValue.replace('D', parseInt(this.day)		+ this.divider);
				return				returnValue.substr(0, returnValue.length-1);
			}
		};
	},
	
	createOverlay:		function() {
		if(!this.overlay) {
			this.overlay				= true;
			var overlay				= document.createElement('div');
			var overlayDiv				= document.createElement('div');
			overlay.id				= 'overlay';
			overlayDiv.id				= 'overlayDiv';
			this.overlayEl				= document.body.appendChild(overlay);
			this.overlayDivEl			= document.body.appendChild(overlayDiv);
			this.overlayEl.style.height	= this.scrollHeight	+ 'px';
			this.overlayEl.style.width	= this.scrollWidth	+ 'px';
			Behaviour.addEvent('window',	'onresize',	function() {
				framework.overlayEl.style.height	= framework.scrollHeight	+ 'px';
				framework.overlayEl.style.width	= framework.scrollWidth	+ 'px';
			});
		}
	},
	disposeOverlay:	function() {
		if(this.overlay) {
			this.overlayEl.removeNode();
			this.overlayDivEl.removeNode();
		}
	},
	showOverlay:	function() {
		if(!this.overlay) {
			this.createOverlay();
		}
		this.overlayEl.style.display		= 'inline';
		this.overlayDivEl.style.display	= 'inline';
		this.overlayDivEl.focus();
	},
	hideOverlay:		function() {
		this.overlayEl.style.display		= 'none';
		this.overlayDivEl.style.display	= 'none';
	},
	
	showImage:		function(imageUrl, showImageWidth, showImageHeight, description) {
		imageObject				= new Image();
		imageObject.onload			= function() {
			if(!showImageWidth || !showImageHeight) {
				framework.setSizes();
				maxWidth				= framework.innerWidth	- 20;
				maxHeight				= framework.innerHeight	- 20;
			} else {
				maxWidth				= showImageWidth;
				maxHeight				= showImageHeight;
			}
			if(imageObject.width>maxWidth || imageObject.height>maxHeight) {
				imageRatio		= imageObject.width	/ imageObject.height;
				screenRatio		= maxWidth		/ maxHeight;
				if(imageRatio>screenRatio) {
					maxHeight		= maxWidth		/ imageRatio;
				} else {
					maxWidth		= maxHeight		* imageRatio;
				}
			} else {
				maxWidth			= imageObject.width;
				maxHeight			= imageObject.height;
			}
			var dialogHTML	= '<img src="'+imageObject.src+'" width="'+maxWidth+'" height="'+maxHeight+'" class="dialogImage" onclick="framework.hideOverlay();">';
			if(framework.showImageCloseButton) {
				dialogHTML	+= '<div class="buttonDiv" onclick="framework.hideOverlay();">';
				dialogHTML	+= '<img src="/_images/error.gif" style="align: right;" onclick="framework.hideOverlay();">';
				dialogHTML	+= '</div>';
			}
			framework.dialog(dialogHTML, 'Image', maxWidth, maxHeight, 10);
		};
		imageObject.onerror	= function() {
			framework.error('Error while loading "'+imageUrl+'"');
		};
		this.dialog('Loading picture: '+description+'<br><div align="center"><img src="/_images/wait.gif" class="fliph"></div>', imageUrl, 400, 50, 10);
		imageObject.src	= imageUrl;
	},
	showImageSeries:	function(imageUrlArray, showImageWidth, showImageHeight, showNavigation, showThumbs, defaultItem) {
		this.showImage(imageUrlArray[defaultItem], showImageWidth, showImageHeight);
	},
	dialog:			function(message, title, width, height, top) {
		if(!width) {
			var width					= 400;
		}
		if(!height) {
			var height				= 100;
		}
		if(!top && top!=0) {
			var top					= 100;
		}
		this.setSizes();
		this.createOverlay();
		this.overlayDivEl.innerHTML		= message;
		this.overlayDivEl.className		= 'dialog';
		this.overlayDivEl.style.width		= width+'px';
		this.overlayDivEl.style.height	= height+'px';
		this.overlayDivEl.style.top		= (this.offsetY+top)+'px';
		this.overlayDivEl.style.left		= ((this.innerWidth-width)/2)+'px';
		this.showOverlay();
	},
	alert:			function(message, returnHandler) {
		this.buttons.initialise(returnHandler, 'alert');
		this.buttons.okButton = true;
		this.dialog('<img class="dialogSign" src="/_images/alert.gif"><span class="dialogMessage">'+message+'</span>'+this.buttons.getButtons(), 'Alert');
	},
	confirm:			function(message, returnHandler) {
		this.buttons.initialise(returnHandler, 'confirm');
		this.buttons.cancelButton = this.buttons.okButton = true;
		this.dialog('<img class="dialogSign" src="/_images/info.gif"><span class="dialogMessage">'+message+'</span>'+this.buttons.getButtons(), 'Confirm');
	},
	choose:			function(message, returnHandler) {
		this.buttons.initialise(returnHandler, 'choose');
		this.buttons.yesButton = this.buttons.noButton = true;
		this.dialog('<img class="dialogSign" src="/_images/info.gif"><span class="dialogMessage">'+message+'</span>'+this.buttons.getButtons(), 'Choose');
	},
	prompt:			function(message, returnHandler) {
		this.buttons.initialise(returnHandler, 'prompt');
		this.buttons.cancelButton = this.buttons.okButton = true;
		this.dialog('<img class="dialogSign" src="/_images/info.gif"><span class="dialogMessage">'+message+'<br><input id="promptField" name="promptField" type="text"></span>'+this.buttons.getButtons(), 'Prompt');
	},
	error:			function(message, returnHandler) {
		this.buttons.initialise(returnHandler, 'error');
		this.buttons.okButton = true;
		this.dialog('<img class="dialogSign" src="/_images/error.gif"><span class="dialogMessage">'+message+'</span>'+this.buttons.getButtons(), 'Error');
	},
	buttons: {
		okButton:		false,
		cancelButton:	false,
		yesButton:	false,
		noButton:		false,
		returnValue:	null,
		returnHandler:	null,
		dialogType:	null,
		initialise:	function(returnHandler, dialogType) {
			this.okButton		= false;
			this.cancelButton	= false;
			this.yesButton		= false;
			this.noButton		= false;
			this.returnValue	= '';
			this.returnHandler	= returnHandler;
			this.dialogType	= dialogType;
		},
		getButtons:	function() {
			if(this.yesButton) {
				this.returnValue	+= '<button onclick="framework.buttons.buttonHandler(true);">'+framework.yesText+'</button>';
			}
			if(this.noButton) {
				this.returnValue	+= '<button onclick="framework.buttons.buttonHandler(false);">'+framework.noText+'</button>';
			}
			if(this.okButton) {
				this.returnValue	+= '<button onclick="framework.buttons.buttonHandler(true);">'+framework.OKText+'</button>';
			}
			if(this.cancelButton) {
				this.returnValue	+= '<button onclick="framework.buttons.buttonHandler(false);">'+framework.cancelText+'</button>';
			}
			return '<div class="dialogButtonDiv">'+this.returnValue+'</div>';
		},
		buttonHandler:	function(returnValue) {
			framework.hideOverlay();
			switch(this.dialogType) {
			case 'alert':
				if(this.returnHandler) {
					eval(this.returnHandler+'()');
				}
				break;
			case 'confirm':
				if(this.returnHandler) {
					eval(this.returnHandler+'('+returnValue+')');
				}
				break;
			case 'choose':
				if(this.returnHandler) {
					eval(this.returnHandler+'('+returnValue+')');
				}
				break;
			case 'prompt':
				if(this.returnHandler) {
					if(returnValue) {
						returnValue	= '"'+$('promptField').value+'"';
					}
					eval(this.returnHandler+'('+returnValue+')');
				}
				break;
			case 'error':
				if(this.returnHandler) {
					eval(this.returnHandler+'()');
				}
				break;
			}
		}
	}
};
