/* PageBuilder useage sample
var test	= new createElement('p');
test.setParams({
	id		: 'testHTML',
	content	: temp.result(),
	style	: {
			position	: 'absolute',
			top			: document.body.clientHeight/2,
			left		: document.body.clientWidth/2,
			border		: '1px outset silver',
			backgroundColor	: 'silver',
			cursor		: 'move',
			zIndex		: 5000
		},
	onselectstart	: 'return false;',
	onmousedown		: 'this.moving=true',
	onmouseup		: 'this.moving=false',
	onmousemove		: 'moveElement(this)'
});
test.render();
*/
// Create global useful stuff.
global	= {
			browser	: {
						name	: (navigator.appName.indexOf('Microsoft') > -1) ? 'msie' : 'ns',
						version	: navigator.appVersion
					},
			hash		: function (str) {
							var out		= new Object();
							var temp1	= str.split('&');
							var temp,nameVal;
							while (temp1.length > 0) {
								temp		= temp1.pop();
								nameVal		= temp.split('=');
								out[nameVal[0]] = unescape(nameVal[1]);
							}
							return out;
						},
			today		: new Date(),
			MonthsOfTheYear	: new Array ('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'),
			DaysOfTheWeek	: new Array ('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'),
			formatTime	: function(which) {
								var Today = new Date();
								// Create the date
								var yyyy = (Today.getYear().toString().length == 4)
									? Today.getYear()
									: Today.getYear() + 1900;
								var DATE = (Today.getMonth() + 1) + "/" 
									+ (Today.getDate()) + "/" 
									+ yyyy;
								// Create a clock
								var TIME = (Today.getHours() > 12)
									? (Today.getHours() - 12) + ":"
									: Today.getHours() + ":";
								TIME += (Today.getMinutes().toString().length < 2)
									? "0" + Today.getMinutes() + ":"
									: Today.getMinutes() + ":";
								TIME += (Today.getSeconds().toString().length < 2)
									? "0" + Today.getSeconds()
									: Today.getSeconds();
								var AMPM = (Today.getHours() > 11)
									? 'PM'
									: 'AM';
								// Return the requested values
								switch (which) {
									case 'date':
										return DATE;
									case 'time':
										return TIME + " " + AMPM + " PST";
									case 'simpletime':
										return TIME + " " + AMPM;
									case 'datetime':
										return DATE + " " + TIME + " " + AMPM + " PST";
									case 'datesimpletime':
										return DATE + " " + TIME + " " + AMPM;
									case 'longday':
										return global.DaysOfTheWeek[Today.getDay()];
									case 'longmonth':
										return global.MonthsOfTheYear[Today.getMonth()];
								};
						},
			interrogate	: function (obj) {
							var i;
							var out = new Array();
							for (i in obj) {
								out.push (i + '	= ' + obj[i]);
							}
							return out.join('\n');
						},
			showDebug	: false,
			debug		: function (el) {
							//alert (typeOf el);
							if (!document.getElementById('debug') && global.showDebug) {
								// Make moveable holder.
								var temp	= new createElement('p');
								temp.setParams({
									id		: 'debugHolder',
									content	: '<input type="text" id="debugClock" name="debugClock" style="border:0px;background-color:silver"><br>',
									style	: {
											position	: 'absolute',
											top			: 250,
											left		: 500,
											border		: '1px outset silver',
											backgroundColor	: 'silver',
											cursor		: 'move',
											zIndex		: 5000
										},
									onselectstart	: 'return false;',
									onmousedown		: 'this.moving=true',
									onmouseup		: 'this.moving=false',
									onmousemove		: 'moveElement(this);'
								});
								temp.render();
								// Now make textarea to hold debug data
								var temp	= new createElement('textarea');
								temp.setParams({
									id		: 'debug',
									style	: {
												width	: 350,
												height	: 200
											},
									onmousedown	: 'event.cancelBubble=true;'
								});
								temp.render('debugHolder');
							}
							if (global.showDebug) {
								if (global.browser.name == 'msie') {
									document.getElementById('debug').innerText += "\n" + global.formatTime('datesimpletime') + '--->\n' + global.interrogate(el);
									document.getElementById('debug').innerText += "\n---";
								} else {
									document.getElementById('debug').firstChild.nodeValue += global.formatTime('datesimpletime') + '--->\n' + global.interrogate(el) + '\n---\n';
								}
								debugClock();
							}
						},
		supressErrors	: function (on) {
							window.onerror  = function () { 
						      if (!on) {
						       // Show standard IE error prompt
						       event.returnValue = true;
						       return true;
						      } else {
						       // Supress IE errors...show custom?
						       var msg = 'line=' + event[errorLine] + '&msg=' + event[errorMessage];
						       debug.debug({error:msg});
							   event.returnValue = false;
						       return false;
						      }
						     };
						}
};
global.ENV_QUERY	= global.hash(window.location.search.substring(1));

function createElement(type) {
	var temp				= document.createElement(type);
	//temp.innerHTML			= '&nbsp;';
	if (global.browser.name == 'msie') {
		temp.toString		= function () {
								return this.outerHTML;
							};
	}
	temp.setParams			= function (obj) {
								var i,j,test;
								for (i in obj) {
									if (i == 'content') {continue;}
									test = i.split('.');
									if (test[1]) {
										this[test[0]][test[1]] = obj[i];
									} else {
										if (typeof obj[i] == 'object') {
											for (j in obj[i]) {
												this[i][j] = obj[i][j];
											}
										} else {
											this[i] = obj[i];
										}
									}
								}
								if (obj.content) {
									this.innerHTML		= obj.content;
								}
							};
	temp.render			= function (parent) {
							var renderTarget	= (parent) ? document.getElementById(parent) : document.body;
							
							if (global.browser.name == 'msie') {
								renderTarget.insertAdjacentHTML('BeforeEnd', this.toString() + '\n');
							} else {
								renderTarget.innerHTML += '<'+this.nodeName+' id="'+this.id+'" style="'+this.style.cssText+'" onmouseover="'+this.onmouseover+'" onmouseout="'+this.onmouseout+'" onmousedown="'+this.onmousedown+'" onclick="'+this.onclick+'" align="'+this.align+'">'+this.innerHTML+'</'+this.nodeName+'>'+'\n';
							}
						};
	return temp;
}

function renderElements(dataObj) {
	dataObj.xPath		= '//element';
	dataObj.selectType	= 'multi';
	var pageElements	= dataObj.result();
	
	dataObj.xPath		= '//script';
	dataObj.selectType	= 'multi';
	var pageScripts		= dataObj.result();
	
	var numEl			= pageElements.length
	var numScript		= pageScripts.length
	var numStyle,thisEl,elId,docEl,styles,thisScript,i,j;
	//alert (numEl)
	// Render page elements
	for(i=0;i<numEl;i++) {
		thisEl	= pageElements[i];
		//global.debug (thisEl);
		//alert (thisEl.nodeName);
		if (global.browser.name == 'ns') {
			if (thisEl.nodeName != 'element') continue;
		}
		elId	= thisEl.getAttribute('id');
		//alert (elId)
		if (!document.getElementById(elId)) continue;
		
		docEl	= document.getElementById(elId);
		
		dataObj.xPath		= "//element[@id='"+elId+"']/style";
		dataObj.selectType	= 'multi';
		styles				= dataObj.result();
		if (styles.length > 0) {
			numStyle	= styles.length;
			//alert (numStyle)
			for (j=0;j<numStyle;j++) {
				if (!styles[j]) continue;
				temp	= styles[j];
				
				if (global.browser.name == 'ns') {
					while (!temp.nodeValue) {
						if (temp.firstChild) {
							temp	= temp.firstChild;
						} else {
							temp	= temp.nextSibling;
						}
					}
					//global.debug (temp)
					docEl.style[styles[j].getAttribute('id')]	= temp.nodeValue;
				} else {
					docEl.style[styles[j].getAttribute('id')]	= styles[j].text;
				}
			}
		}
		if (document.getElementById(elId)) {
			dataObj.xPath		= "//element[@id='"+elId+"']/text";
			dataObj.selectType	= 'single';
			temp				= dataObj.result();
			if (global.browser.name == 'ns') {
				if (temp.nodeName == "text") {
					while (!temp.nodeValue || temp.nodeType == 3) {
						if (temp.firstChild) {
							temp	= temp.firstChild;
						} else {
							temp	= temp.nextSibling;
						}
					}
					//global.debug (temp)
					document.getElementById(elId).innerHTML	= temp.nodeValue;
				}
			} else {
				if (temp && temp.text)  {
					document.getElementById(elId).innerHTML = temp.text;
				}
			}
		}
	}
	
	// Gather script lines to run
	global.executeScript = new Array();
	for(i=0;i<numScript;i++) {
		thisScript	= pageScripts[i];
		if (thisScript.text) {
			eval(thisScript.text);
		}
	}
}

function debugClock() {
	if (global.clockTimer) clearTimeout(global.clockTimer);
	document.getElementById('debugClock').value = global.formatTime('datesimpletime');
	global.clockTimer = setTimeout("debugClock()",1000);
}

function moveElement(el) {
	if (!el.moving) return false;
	el.style.zIndex	= 5000;
	var xPos		= window.event.clientX - (el.offsetWidth/2);
	var yPos		= window.event.clientY - (el.offsetHeight/2);
	if (global.browser.name == 'ns') {
		el.pixelTop		= yPos;
		el.pixelLeft	= xPos;
	} else {
		el.style.top	= yPos;
		el.style.left	= xPos;
	}
}

function detectWirelessDevice() {
		var retVal		=  false;
		var screenSize	= false;
		if (navigator.appName == 'BlackBerry') {
			retVal	= true;
		}
		if (navigator.userAgent.toLowerCase().indexOf('windows ce') >= 0) {
			retVal	= true;
		}
		// Check for screen size
		screenSize	= /(\d+)x(\d+)/.exec(navigator.userAgent);
		if (screenSize) {
			wirelessScreenSize	= {
									h	: screenSize[1],
									w	: screenSize[2]
								};
		}
		return retVal;
	}