<!--
/*
	clsPage.js
		Loads html pages and writes them into any document object
		Caches them in an associative array attached to the window object
		Prepare target for pages i.e. writes out an iframe
*/

function clsPage(id, ownerDocument) {
	this.id=id;
	this.ownerDocument=ownerDocument;
	this.title='';
	this.innerHTML='';
	this.src='';
	this.document=null;
	this.xml=null;
			
	// Events - set a function to these props
	this.onupdate=null;
	
	// Creates an iframe to load content into
	this.create=function(container, position, align, scrolling, className, style) {
		if (typeof container == 'string') { container = this.ownerDocument.getElementById(container); }
		var html = '<iframe src="shim.html" id="'+this.id+'" name="'+this.id+'" '
			+(align.length>0?' align="'+align+'"':'')
			+(scrolling.length>0?' scrolling="'+scrolling+'"':'')
			+(className.length>0?' class="'+className+'"':'')
			+(style.length>0?' style="'+style+'"':'')+'></iframe>';
		switch (position) {
			case 'before' :
				container.innerHTML = html+container.innerHTML;
				break;
			case 'replace':
				container.innerHTML = html;
				break;
			default:	// or after
				container.innerHTML += html;
				break;
		}
		this.setDocument();
	}

	this.setDocument=function() {
		var frame = this.ownerDocument.getElementById(this.id);
		if (frame) { this.document=frame.contentWindow.document; }
	}
	
	this.load=function() {
		if (arguments.length>0) { this.src=arguments[0];}
		this.xml=getXMLDoc(this.src, this.onload);
	}
	
	this.onload=function() {
		if(this.xml) {
			if (this.xml.readyState == 4) {
				if (this.xml.status == 200) { 
					this.innerHTML=this.xml.responseText;
					this.print();
					if (this.onupdate) { this.onupdate(); }
				}
			}
		}
	}
	
	this.print=function() {
		var styles='';
		if(ownerDocument.styleSheets[0].cssText) {
			styles=ownerDocument.document.styleSheets[0].cssText;
		} else {
			var s=ownerDocument.document.styleSheets[0];
			for (var j=0; j<s.cssRules.length;j++) {
				styles+=s.cssRules[j].cssText;
			}		
		}
		if(this.document) {
			this.document.open();
			this.document.writeln('<html><head><title></title><style type="text/css">'
				+styles+'</style></head><body>'+this.innerHTML+'</body></html>');
			this.document.writeln('boo');
			this.document.close();
		}
	}

	/*********************************************************
		Form controls - each has optional arguments:
			classname
			style
			eventhandler
			buildpage - adds html to innerHTML property
	*********************************************************/
	this.dropdown=function(name, rs, currentval, showblank, showany) {
		var requiredargs=5;
		var html='<select name="'+name+'" id="'+name+'" '
			+(arguments.length>requiredargs && arguments[requiredargs].length>0 ? ' class="'+arguments[requiredargs]+'"':'')
			+(arguments.length>requiredargs+1 && arguments[requiredargs+1].length>0 ? ' style="'+arguments[requiredargs+1]+'"':'')
			+(arguments.length>requiredargs+2 && arguments[requiredargs+2].length>0 ? ' '+arguments[requiredargs+2]:'')
			+'>'+(showblank ? '<option value=""></option>':'')
			+(showany ? '<option value="%" '+(currentval=='%' ? 'selected': '')+'>Any</option>':'')
		if(typeof rs == 'string') { var options = rs.split(',') }
		else { var options=rs; }
		for(var i=0; i<options.length-1; i+=2) {
			html+='<option value="'+options[i]+'" '+(currentval==options[i] ? 'selected': '')+'>'+options[i+1]+'</option>';
		}
		html+='</select>';
		if(arguments.length>requiredargs+3 && arguments[requiredargs+3]==true) { this.innerHTML+=html; }		
		return (html);
	}
	this.multiselect=function(name, rs, currentvals, showblank, showany, height) {
		var requiredargs=6;
		var html='<select name="'+name+'" id="'+name+'" size="'+height+'" '
			+(arguments.length>requiredargs && arguments[requiredargs].length>0 ? ' class="'+arguments[requiredargs]+'"':'')
			+(arguments.length>requiredargs+1 && arguments[requiredargs+1].length>0 ? ' style="'+arguments[requiredargs+1]+'"':'')
			+(arguments.length>requiredargs+2 && arguments[requiredargs+2].length>0 ? ' '+arguments[requiredargs+2]:'')
			+'>'+(showblank ? '<option value=""></option>':'')
			+(showany ? '<option value="%" '+(currentval=='%' ? 'selected': '')+'>Any</option>':'')
		if(typeof rs == 'string') { var options = rs.split(',') }
		else { var options=rs; }
		for(var i=0; i<options.length-1; i+=2) {

			html+='<option value="'+options[i]+'" '+(currentvals.indexOf(options[i])!=-1 ? 'selected': '')+'>'+options[i+1]+'</option>';
		}
		html+='</select>';
		if(arguments.length>requiredargs+3 && arguments[requiredargs+3]==true) { this.innerHTML+=html; }		
		return (html);
	}

	this.textbox=function(name, value, size, maxlength, required) {
		var requiredargs=5;
		var html='<input type="text" name="'+name+'" id="'+name+'" '
			+(new String(value).length>0?' value="'+value+'" ':'')
			+(size>0?' size="'+size+'" ':'')
			+(maxlength>0?' maxlength="'+maxlength+'" ':'')
			+(required.length>0?' required="'+required+'" ':'')
			+(arguments.length>requiredargs && arguments[requiredargs].length>0 ? ' class="'+arguments[requiredargs]+'"':'')
			+(arguments.length>requiredargs+1 && arguments[requiredargs+1].length>0 ? ' style="'+arguments[requiredargs+1]+'"':'')
			+(arguments.length>requiredargs+2 && arguments[requiredargs+2].length>0 ? ' '+arguments[requiredargs+2]:'')
			+'/>';
		if(arguments.length>requiredargs+3 && arguments[requiredargs+3]==true) { this.innerHTML+=html; }		
		return (html);
	}
	this.checkbox=function(name, value, checked){
		var requiredargs=3;
		var html='<input type="checkbox" name="'+name+'" id="'+name+'" '
			+(value.length>0?' value="'+value+'" ':' value="true"')
			+(new String(checked).toLowerCase()=='true'?' checked':'')
			+(arguments.length>requiredargs && arguments[requiredargs].length>0 ? ' class="'+arguments[requiredargs]+'"':'')
			+(arguments.length>requiredargs+1 && arguments[requiredargs+1].length>0 ? ' style="'+arguments[requiredargs+1]+'"':'')
			+(arguments.length>requiredargs+2 && arguments[requiredargs+2].length>0 ? ' '+arguments[requiredargs+2]:'')
			+'/>';
		if(arguments.length>requiredargs+3 && arguments[requiredargs+3]==true) { this.innerHTML+=html; }		
		return (html);
	}
	this.calendar=function(name, value, required){
		var requiredargs=3;
		var html='<dlcalendar click_element_id="img'+name+'" date_format="dd/mm/yyyy" input_element_id="'+name+'" tool_tip="Click to choose date"></dlcalendar>'
			+this.textbox(name, value, 10, 10, required,
				(arguments.length>requiredargs && arguments[requiredargs].length>0 ? arguments[requiredargs]+'"':''),
				(arguments.length>requiredargs+1 && arguments[requiredargs+1].length>0 ? arguments[requiredargs+1]+'"':''),
				(arguments.length>requiredargs+2 && arguments[requiredargs+2].length>0 ? arguments[requiredargs+2]:'')			
			) +'/><img align="middle" id="img'+name+'" src="'+config_BaseDir+'images/dlcalendar_2.gif" height="13" width="13" alt="calendar"/>'
		if(arguments.length>requiredargs+3 && arguments[requiredargs+3]==true) { this.innerHTML+=html; }		
		return (html);
	}
	this.textarea=function(name, value, rows, cols, required){
		var requiredargs=5;
		var html='<textarea name="'+name+'" id="'+name+'" '
			+(rows>0?' rows="'+rows+'" ':'')
			+(cols>0?' cols="'+cols+'" ':'')
			+(required.length>0?' required="'+required+'" ':'')
			+(arguments.length>requiredargs && arguments[requiredargs].length>0 ? ' class="'+arguments[requiredargs]+'"':'')
			+(arguments.length>requiredargs+1 && arguments[requiredargs+1].length>0 ? ' style="'+arguments[requiredargs+1]+'"':'')
			+(arguments.length>requiredargs+2 && arguments[requiredargs+2].length>0 ? ' '+arguments[requiredargs+2]:'')
			+'/>'
			+(value.length>0?' value="'+value+'" ':'')
			+'</textarea>';
		if(arguments.length>requiredargs+3 && arguments[requiredargs+3]==true) { this.innerHTML+=html; }		
		return (html);
	}
	this.hidden=function(name, value){
		var requiredargs=2;
		var html='<input type="hidden" name="'+name+'" id="'+name+'" '
			+(value.length>0?' value="'+value+'" ':' value="true"')
			+(arguments.length>requiredargs && arguments[requiredargs].length>0 ? ' class="'+arguments[requiredargs]+'"':'')
			+(arguments.length>requiredargs+1 && arguments[requiredargs+1].length>0 ? ' style="'+arguments[requiredargs+1]+'"':'')
			+(arguments.length>requiredargs+2 && arguments[requiredargs+2].length>0 ? ' '+arguments[requiredargs+2]:'')
			+'/>';
		if(arguments.length>requiredargs+3 && arguments[requiredargs+3]==true) { this.innerHTML+=html; }		
		return (html);
	}
	this.button=function(name, value, title, issubmit){
		var requiredargs=4;
		var html='<button name="'+name+'" id="'+name+'" '
			+(value.length>0?' value="'+value+' " ':' value="true" ')
			+(title.length>0?' title="'+title+' " ':(issubmit?' title="submit" ':' title="'+value+'" '))
			+(issubmit?' type="submit" ':' type="button"')
			+(arguments.length>requiredargs && arguments[requiredargs].length>0 ? ' class="'+arguments[requiredargs]+'"':'')
			+(arguments.length>requiredargs+1 && arguments[requiredargs+1].length>0 ? ' style="'+arguments[requiredargs+1]+'"':'')
			+(arguments.length>requiredargs+2 && arguments[requiredargs+2].length>0 ? ' '+arguments[requiredargs+2]:'')
			+'>'+(title.length>0?title:(issubmit?'submit':value))+'</button>';
		if(arguments.length>requiredargs+3 && arguments[requiredargs+3]==true) { this.innerHTML+=html; }		
		return (html);
	}
}

// -->