/////////////////
// Master API. //
/////////////////
var COMPOSE_Page = function() {
	// Document setup.
	this.document = (document)?document:contentDocument;
	this.window = (window)?window:contentWindow;
	this.doc = this.document;
	this.win = this.window;
	this.window._parentAPI = this;
	this.document._parentAPI = this;
	this.AddEvent(this.window, 'load', this.PageInitialized);
	this.AddEvent(this.window, 'beforeunload', this.PageKill);
	this.SecureForms = new Object;
	
	// Browser object.
	this.Browser = new COMPOSE_Browser_Detect();
	
	// Preload images.
	if (this.document.images) { 
		var AuthErrMsgPntr = new Image(9, 8);
		AuthErrMsgPntr.src = "/images/pointer2.gif"
	}	
};

COMPOSE_Page.prototype.PageInitialized = function() { // Page has just loaded.
	this._parentAPI.FixViewState();

	if (this.document.forms) {
		for (var i=0;i<this.document.forms.length;i++) { // Forms validation setup.	
			if (String(this.document.forms[i].onsubmit).indexOf("return Validate(this)")>0) { // Legacy
				alert("The method return Validate(this) is dissolved, please add the Attribute auth-required=\"true\" instead.")
				this._parentAPI.SecureForms[this.document.forms[i].id] =
				new COMPOSE_SecureForm(this.document.forms[i]);
			} else if (this.document.forms[i].getAttribute('auth-required')) {				
				this._parentAPI.SecureForms[this.document.forms[i].id] =
				new COMPOSE_SecureForm(this.document.forms[i]);
			}
		}
	} else {
		// User has no form collections and therefore validation cannot be assigned.
		// this is a pretty bad error, meaning that the user cannot get validation on data submition-
		// this event is very, very unlikely.
		alert('The system was unable to setup client side validation for you, you may experience lack of support when submitting data to system, as such it is highly possible you will encounter technical error messages.\r\nWe recommend you upgrade your browser, or download a modern browser such as Firefox at www.firefox.com.');
	}
	
	if (this.document.links) {
		for (var i=0;i<this.document.links.length;i++) { // Setup the outbound animation for a link click.
			COMPOSE.AddEvent(this.document.links[i], 'click', function() { COMPOSE.FindObj('transbox').style.display="inline"; });
		}
	}
}

COMPOSE_Page.prototype.PageKill = function() { // Page has just started to unload (refresh or redirect).
	this._parentAPI.FindObj('transbox').style.display="inline";
}

COMPOSE_Page.prototype.FindObj = function(obj, doc) { // Find object standard method.
	var p,i,x;if(!doc)doc=this.document;if((p=obj.indexOf("?"))>0&&parent.frames.length){
	doc=parent.frames[obj.substring(p+1)].document;obj=obj.substring(0,p);}
	if(!(x=doc[obj])&&d.all)x=doc.all[obj];for(i=0;!x&&i<doc.forms.length;i++)x=doc.forms[i][obj];
	for(i=0;!x&&doc.layers&&i<doc.layers.length;i++)x=this._parentAPI.FindObj(obj,doc.layers[i].document);
	if(!x&&doc.getElementById)x=doc.getElementById(obj);return x;
};

COMPOSE_Page.prototype.AddEvent = function(obj, type, func) { // Event handling.
	if (obj.addEventListener) { obj.addEventListener(type, func, true); }
	else if (obj.attachEvent) {	obj.attachEvent('on'+type, func); }
	else { obj['on'+type] = func; }
}

COMPOSE_Page.prototype.FixViewState = function() { // Viewstate FIX (sometimes viewstates appear, make sure they dont).
	if (this.Browser.isDHTML){n = this.document.getElementsByTagName("INPUT");
	for(var i=0;i<n.length;i++){if ((n[i].name=="__VIEWSTATE")&&(n[i].parentNode.nodeName=="DIV")){
	n[i].parentNode.style.display = 'none';}}}
}

//////////////////////////
// GLOBALIZATION SETUP. //
//////////////////////////
var COMPOSE = new COMPOSE_Page();
var COMPOSE_WYSIWYG_API = new Object();

//////////////////////////////////////////
// Code to be implemented into the API. //
//////////////////////////////////////////
function COMPOSE_Browser_Detect() { // Browser detect object.
	d=window.document;v=navigator.appVersion.toLowerCase();
	this.isDHTML=((d.getElementById||d.all||d.layers)&&(d.createElement))?true:false;
	this.ie4=(!d.getElementById&&d.all)?true:false;this.ie5=(v.indexOf("msie 5.0")!=-1)?true:false;
	this.ie55=(v.indexOf("msie 5.5")!=-1)?true:false;this.ie6=(v.indexOf("msie 6.0")!=-1)?true:false;
	this.isIE=(this.ie5||this.ie55||this.ie6)?true:false;this.isSafari=(v.indexOf("safari")!=-1)?true:false;
	this.isIEMAC=(this.isIE&&v.indexOf("powerpc")!=-1)?true:false;this.isGecko=!this.isIE;this.isW3CSTRICT=this.isGecko;
};

////////////////////////////
// GLOBALIZATION HANDLES. //
////////////////////////////
/*COMPOSE_AddEventHandle(window, 'load', COMPOSE_MasterInit);
COMPOSE_AddEventHandle(window, 'beforeunload', COMPOSE_MasterOutit); // Not massively supported (dont overuse).

function COMPOSE_MasterInit(){ // Global scope init.
	COMPOSE_FixViewState()
	
	//var asnetstring = "";
	//for (var i=0;i<=255;i++) { asnetstring += i + ': ' +String.fromCharCode(i) + '<br />'; }
	//document.write(asnetstring);
}

function COMPOSE_MasterOutit(){ // Global scope out.
	COMPOSE_FindObj('transbox').style.display="inline";
}

function COMPOSE_FixViewState(){ // Viewstate FIX (sometimes viewstates appear due to a legacy CSS property, make sure that viewstates are hidden.
	if (COMPOSE_Browser.isDHTML){n = document.getElementsByTagName("INPUT");
	for(var i=0;i<n.length;i++){if ((n[i].name=="__VIEWSTATE")&&(n[i].parentNode.nodeName=="DIV")){
	n[i].parentNode.style.display = 'none';}}}
}

function addEventHandle(obj, type, func) {
	alert("addEventHandle is now legacy, please update to 'COMPOSE_AddEventHandle(obj, type, func)'");
	COMPOSE_AddEventHandle(obj, type, func)
}
function COMPOSE_AddEventHandle(obj, type, func){ // Event handler and legacy method.
	if (obj.addEventListener) { obj.addEventListener(type, func, true); }
	else if (obj.attachEvent) {	obj.attachEvent('on'+type, func); }
	else { obj['on'+type] = func; }
}*/