var Nlop = new Object();
Nlop.debugEnabled = false;
Nlop.infoEnabled = false;
Nlop.debugAjaxResponse = false;
Nlop.scrollConsole = false;
Nlop.showProgressBar = true;
Nlop.ajaxObjectInstance;

var nlopProgBarCount = 0;
var lastProgHideTimerId;
var requestMessageStart = "Loading...";
var requestMessageComplete = "Completed";
var requestMessageFailed = "Failed";
var requestStatusStart = "";
var requestStatusComplete = "";

Nlop.changeOnload = function( newFunc ) {
    var tmpFunc = window.onload;
    if ( !tmpFunc ) {
		window.onload = newFunc;
		return;
	}
    bothFunc = function() {
        tmpFunc();
    	newFunc();
    }
    window.onload = bothFunc;
}

Nlop.byId = function( id ){
	return window.document.getElementById( id );
}

Nlop.isElementExist = function( elementId ){
	if( elementId ){
		return Nlop.byId( elementId ) != null;
	}
	return false;
}

Nlop.getElementOffsetTop = /*number*/ function( /*element*/ e ){
	var res = 0, maxDepth = 10, i = 0, tmp = e;
	while( tmp != null && i <= maxDepth ){
		res += tmp.offsetTop;
		tmp = tmp.offsetParent;
		i += 1;
	}
	return res;
}

Nlop.getElementOffsetLeft = /*number*/ function( /*element*/ e ){
	var res = 0, maxDepth = 10, i = 0, tmp = e;
	while( tmp != null && i <= maxDepth ){
		res += tmp.offsetLeft;
		tmp = tmp.offsetParent;
		i += 1;
	}
	return res;
}

Nlop.changeContent = function( text, id ){
	return Nlop.changeContent( text, id, false );
}

Nlop.decode = function( text ){
	var res = "";
	if( !Nlop.isBlankString( text ) ){
		if( typeof decodeURIComponent != 'undefined' ){
			res = decodeURIComponent( text );
		} else {
			res = unescape( res );
			res = res.replace( "\\+", " " );
		}
	}
	return res;
}

Nlop.encode = function( text ){
	var res = "";
	if( !Nlop.isBlankString( text ) ){
		if( typeof encodeURIComponent != 'undefined' ){
			res = encodeURIComponent( text );
		} else {
			res = escape( res );
		}
	}
	return res;
}

Nlop.changeContent = function( text, id, unescapeString ){
	var elem = Nlop.byId( id );
	if( elem != null ){
		if( text ){
			if( unescapeString ){
				var tmp = Nlop.decode( text );
				Nlop.debug( "Unescaped content: " + tmp );
				elem.innerHTML = tmp;
			} else {
				elem.innerHTML = text;
			}
		} else {
			elem.innerHTML = "";
		}
		return true;
	}
	return false;
}

Nlop.setElementDisabled = function( elementId, disabled ){
	Nlop.debug( "Set element '" + elementId + "' disabled: " + disabled );
	var e = Nlop.byId( elementId );
	if( e != null ){
		e.disabled = disabled;
		return true;
	}
	return false;
}

Nlop.getElementValue = function( elementId ){
	var e = Nlop.byId( elementId );
	if( e != null ){
		return e.value;
	}
}

Nlop.setElementVisible = function( elementId, visible ){
	var elem = Nlop.byId( elementId );
	if( elem != null ){
		Nlop.debug( "Set element '" + elementId + "' visible: " + visible );
		elem.style.display = visible ? "inline" : "none";
		return true;
	}
	Nlop.debug( "Failed to set element '" + elementId + "' visible: " + visible );
	return false;
}

Nlop.cutText = function( txt, name ){
	if( Nlop.isBlankString( txt ) || Nlop.isBlankString( name ) ){
		return undefined;
	}
	var len = Nlop.beginTag( name ).length;
	var nameStart = txt.indexOf( Nlop.beginTag( name ) );
	var nameEnd = txt.indexOf( Nlop.endTag( name ) );
	if( nameStart >= 0 && nameEnd >= 0 && nameStart <= nameEnd ){
		return txt.substring( nameStart + len, nameEnd );
	}
	return undefined;
}

Nlop.beginTag = function( name ){
	return name ? "<!--/" + name + "Start/-->" : "";
}
Nlop.endTag = function( name ){
	return name ? "<!--/" + name + "End/-->" : "";
}

Nlop.hideErrorsAndMessages = function(){
	Nlop.setElementVisible( "js_error", false );
	Nlop.setElementVisible( "js_message", false );
}
			
Nlop.error = function( message ){
	Nlop.changeContent( message, "js_error" );
	Nlop.setElementVisible( "js_error", true );
	Nlop.setElementVisible( "js_message", false );
}

Nlop.message = function( message ){
	Nlop.changeContent( message, "js_message" );
	Nlop.setElementVisible( "js_message", true );
	Nlop.setElementVisible( "js_error", false );
}

Nlop.writeScript = function( script ){
	// document.write( script );
	if( !Nlop.isBlankString( script ) ){
		try{
			eval( Nlop.trim( script ) );
			return true;
		}catch( e ){
			Nlop.debug( "Invalid Eval Script: " + script );
			Nlop.error( "An error has occurred!" );
			return false;
		}
	}
	return false;
};

Nlop.getNumber = function( txt ){
	if( !Nlop.isBlankString( txt ) ){
		txt = Nlop.trim( txt );
		var num = parseInt( txt );
		return num;
	}
	return NaN;
}

Nlop.newLink = function( href, innerHTML, title, style ){
	var lnk = document.createElement("<a></a>");
	if( href ){
		lnk.href = href;
	}
	if( innerHTML ){
		lnk.innerHTML = innerHTML;
	}
	if( title ){
		lnk.title = title;
	}
	if( style ){
		lnk.style.cssText = style;
	}
	return lnk;
}

Nlop.getAjaxObject = function(){
	//if( !Nlop.ajaxObjectInstance ){
		Nlop.ajaxObjectInstance = Nlop.createAjaxObject();
	//}
	return Nlop.ajaxObjectInstance;
}

Nlop.createAjaxObject = function(){
	var xmlhttp = false;
	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
	// JScript gives us Conditional compilation, we can cope with old IE versions.
	// and security blocked creation of the objects.
		try {
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
	    	try {
	    		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (E) {
				xmlhttp = false;
			}
		}
	@end @*/
	if ( !xmlhttp && typeof XMLHttpRequest != 'undefined' ) {
		try {
			xmlhttp = new XMLHttpRequest();
		} catch (e) {
			xmlhttp = false;
		}
	}
	if ( !xmlhttp && typeof window.createRequest != 'undefined' ) {
		try {
			xmlhttp = window.createRequest();
		} catch (e) {
			xmlhttp = false;
		}
	}
	return xmlhttp;
}

function showProgressBar( id, show ){
	if( typeof ajaxProgBar != 'undefined' && id ){
		var container = Nlop.byId( id );
		if( container != null ){
			if( !show ){
				container.style.display = "none";
			} else {
				container.style.display = "inline";
			}
		}
	}
}

makeAjaxRequest = function( url, listener, asynchronous ){
	// var xmlhttp = Nlop.createAjaxObject();
	var xmlhttp = Nlop.getAjaxObject();
	var showBar = Nlop.showProgressBar && typeof ajaxProgBar != 'undefined';
	if( xmlhttp ){
		var stat = null;
		var msg = null;
		if( showBar ){
			if( lastProgHideTimerId ){
				window.clearTimeout( lastProgHideTimerId );
				lastProgHideTimerId = undefined;
			}
			ajaxProgBar.setBar( 0.1 );
			showProgressBar( ajaxProgBar.containerId, true );
			stat = Nlop.byId( ajaxProgBar.statusId );
			msg = Nlop.byId( ajaxProgBar.messageId );
			if( stat != null ){
				stat.innerHTML = requestStatusStart;
			}
			if( msg != null ){
				msg.innerHTML = requestMessageStart;
			}
		}
		xmlhttp.open( "GET", url, asynchronous );
		xmlhttp.setRequestHeader( "Cache-Control", "no-cache" );
		xmlhttp.setRequestHeader( "Pragma", "no-cache" );
		xmlhttp.setRequestHeader( "Expires", "0" );
		if( asynchronous ){
			// 0 = uninitialized, 1 = open, 2 = sent, 3 = receiving and 4 = loaded.
			xmlhttp.onreadystatechange = function() {
				/*if ( xmlhttp.readyState == 0 && showBar ) {ajaxProgBar.setBar( 0.17 );}
				if ( xmlhttp.readyState == 1 && showBar ) {ajaxProgBar.setBar( 0.34 );}*/
				if ( xmlhttp.readyState == 2 && showBar ) {
					ajaxProgBar.setBar( /*0.51*/ 0.25 );
				}
				if ( xmlhttp.readyState == 3 && showBar ) {
					ajaxProgBar.setBar( /*0.68*/ 0.5 );
				}
				if ( xmlhttp.readyState == 4 ) {
					if( showBar ){
						ajaxProgBar.setBar( /*0.85*/ 0.75 );
					}
					var res = xmlhttp.responseText;
					if( Nlop.debugAjaxResponse ){
						Nlop.debug( "Ajax Response: " + res );
					}
					if( typeof listener != 'undefined' ){
						listener( res );
					}
					if( showBar ){
						ajaxProgBar.setBar( 1 );
						if( stat != null ){
							if( xmlhttp.status == 200 ){
								stat.innerHTML = requestStatusComplete;
							} else {
								stat.innerHTML = requestMessageFailed;
							}
						}
						if( msg != null ){
							msg.innerHTML = requestMessageComplete;
						}
						lastProgHideTimerId = window.setTimeout( "showProgressBar( '" + ajaxProgBar.containerId + "', false )", 500 );
					}
				}
			}
			xmlhttp.send(null);
		} else {
			xmlhttp.send(null);
			if( xmlhttp.readyState == 4 && xmlhttp.status == 200 ){
				Nlop.debug( "Synchronous Ajax request success" );
				var res = xmlhttp.responseText;
				if( typeof listener != 'undefined' ){
					listener( res );
				}
			} else {
				Nlop.debug( "Synchronous Ajax request failed" );
			}
		}
	}
}

Nlop.trim = function( value ){
	if( !value || value.length < 1 ){
		return "";
	}
	value = value.replace(/^\s+/g, '').replace(/\s+$/g, '');
	return value;
}

Nlop.isBlankString = function( txt ){
	if( txt ){
		var res = true;
		var len = txt.length;
		for( i = 0; i < len; i = i + 1 ){
			if( txt.charAt( i ) == " " || txt.charAt( i ) == "\r" || txt.charAt( i ) == "\n"
				|| txt.charAt( i ) == "\t" ){
			} else {
				return false;
			}
		}
		return res;
	}
	return true;
}

Nlop.getElementText = function( /*string*/ elementId ){
	var e = Nlop.byId( elementId );
	if( e != null ){
		return e.innerText;
	}
	return undefined;
}

Nlop.debug = function( txt ){
	if( Nlop.debugEnabled && txt ){
		Nlop.writeLog( "" + new Date().toLocaleString() + " [DEBUG] " + txt );
	}
}

Nlop.info = function( txt ){
	if( Nlop.infoEnabled && txt ){
		Nlop.writeLog( "" + new Date().toLocaleString() + " [INFO] " + txt );
	}
}

Nlop.writeLog = function( txt ){
	//alert( txt );
	/*if( !Nlop.debugEnabled ){
		return;
	}*/
	try{
		var console = Nlop.byId( "nlopDebugConsole" );
		var status = Nlop.byId( "nlopDebugStatus" );
		if( console != null ){
			console.style.display = "inline";
			if( typeof document.all != 'undefined' && typeof console.insertAdjacentText != 'undefined' ){
				//var len = console.innerText.length;
				var len = console.value.length;
				if( status != null ){
					status.style.display = "inline";
					status.innerText = "" + len;
				}
				console.insertAdjacentText( "beforeEnd", "\n" + txt + "" );
				if( len > 20000 ){
					//console.innerText = ""; //console.innerText.substr( len - 15000 );
					console.value = "";
				}
				try{
					if( Nlop.scrollConsole && typeof console.doScroll != 'undefined' ){
						console.doScroll( "pageDown" );
						console.doScroll( "pageDown" );
						console.doScroll( "pageDown" );
						console.doScroll( "pageDown" );
					}
				}catch(e){}
			} else if( typeof console.textContent != 'undefined' ) {
				//var len = console.textContent.length;
				var len = console.value.length;
				if( status != null ){
					status.style.display = "inline";
					status.textContent = "" + len;
				}
				//console.textContent = console.textContent + "\n" + txt + "";
				console.value = console.value + "\n" + txt + "";
				if( len > 20000 ){
					//console.textContent = ""; //console.textContent.substr( len - 15000 );
					console.value = "";
				}
				if( Nlop.scrollConsole && typeof console.scrollTop != 'undefined' ){
					console.scrollTop = 99999;
				}
			}
			return true;
		}
	}catch(e){
		//alert(e.message);
	}
	return false;
}

Nlop.writeDebugElements = function( consoleWidth /*number*/, consoleHeight /*number*/, statusWidth /*number*/, statusHeight /*number*/ ){
	if( !Nlop.debugEnabled && !Nlop.infoEnabled ){
		return;
	}
	var cW = consoleWidth ? consoleWidth : 400;
	var cH = consoleHeight ? consoleHeight : 400;
	var sW = statusWidth ? statusWidth : 100;
	var sH = statusHeight ? statusHeight : 50;
	var text = "";
	text += '<textarea id="nlopDebugConsole" readonly="readonly" style="display: none; width: ' + cW + 'px; height: ' + cH + 'px; z-index: 200; overflow: scroll; top: 0; right: 0; position: absolute; background-color: white; color: blue; border: 1px solid white; font-size: 11pt;">';
	text += '</textarea>';
	//text += '<pre id="nlopDebugConsole" style="display: none; width: ' + cW + 'px; height: ' + cH + 'px; z-index: 200; overflow: scroll; top: 0; right: 0; position: absolute; background-color: white; color: blue; border: 1px solid white; font-size: 11pt;">';
	//text += '</pre>';
	text += '<div id="nlopDebugStatus" style="display: none; width: ' + sW + 'px; height: ' + sH + 'px; z-index: 201; overflow: hidden ; top: ' + ( cH + 50 ) + 'px; right: 0; position: absolute; background-color: white; color: black; border: 1px solid black;">';
	text += '</div>';
	document.write( text );
}

function getRefToDivNest( divID, oDoc ) {
	if( !oDoc ) {
		oDoc = document;
	}
	if( document.layers ) {
		if( oDoc.layers[divID] ) {
			return oDoc.layers[divID];
		} else {
			for( var x = 0, y; !y && x < oDoc.layers.length; x++ ) {
				y = getRefToDivNest( divID, oDoc.layers[x].document );
			}
			return y;
		}
	}
	if( document.getElementById ) {
		return document.getElementById(divID);
	}
	if( document.all ) {
		return document.all[divID];
	}
	return document[divID];
}

function progressBar( p_borderThickness, p_borderColor, p_bgColor, p_barColor, p_barWidth, p_barHeight, p_direction ) {
	nlopProgBarCount++;
	this.id = 'NlopProgBar' + nlopProgBarCount;
	this.containerId = 'NlopProgressBarTable' + nlopProgBarCount;
	this.statusId = 'NlopProgressStatus' + nlopProgBarCount;
	this.messageId = 'NlopProgressMessage' + nlopProgBarCount;
	this.dir = p_direction;
	this.width = p_barWidth;
	this.height = p_barHeight;
	this.amt = 0;
	var text = '<table style="display:none; position:absolute; top:3px; right:3px; z-index: 1000" border="0" id="' + this.containerId + '" cellspacing="0" cellpadding="' + p_borderThickness + '"><tr><td bgcolor="' + p_borderColor + '">' +
		'<table border="0" cellspacing="0" cellpadding="0"><tr><td height="' + p_barHeight + '" width="' + p_barWidth + '" bgcolor="' + p_bgColor + '">';
	if( document.layers ) {
		text += '<ilayer height="' + p_barHeight + '" width="' + p_barWidth + '">' +
			'<layer bgcolor="' + p_barColor + '" name="' + this.id + '"></layer>' +
			'<layer style="font-size:10px; text-align: center;" height="' + p_barHeight + '" width="' + p_barWidth + '">' +
			'<span id="' + this.messageId + '">' + requestMessageStart + '</span>&nbsp;<span id="' + this.statusId + '"></span>' +
			'</layer>' +
			'</ilayer>';
	} else {
		text += '<div style="position:relative;top:0px;left:0px;height:' + p_barHeight + 'px;width:' + p_barWidth + 'px;">' +
			'<div style="position:absolute; top:0px; left:0px; font-size:1px; height:0px; width:0; background-color:' + p_barColor + ';" id="' + this.id + '">' +
			'</div>' +
			'<div style="position:absolute; top:0px; left:0px; font-size:10px; text-align:center; vertical-align: middle; height:' + p_barHeight + 'px; width:' + p_barWidth + 'px;">' +
			'<span id="' + this.messageId + '">' + requestMessageStart + '</span>&nbsp;<span id="' + this.statusId + '"></span>' +
			'</div>' +
			'</div>';
	}
	text += '</td></tr></table></td></tr></table>\n';
	document.write( text );
	this.setBar = resetBar;
	this.setCol = setColour;
}

function resetBar( p_amount, p_incr ) {
	this.amt = ( typeof( p_incr ) == 'undefined' ) ? p_amount : p_incr ? ( this.amt + p_amount ) : ( this.amt - p_amount );
	if( isNaN( this.amt ) ) {
		this.amt = 0;
	}
	if( this.amt > 1 ) {
		this.amt = 1;
	}
	if( this.amt < 0 ) {
		this.amt = 0;
	}
	var theWidth = Math.round( this.width * ( ( this.dir % 2 ) ? this.amt : 1 ) );
	var theHeight = Math.round( this.height * ( ( this.dir % 2 ) ? 1 : this.amt ) );
	var theDiv = getRefToDivNest( this.id );
	if( !theDiv ) {
		window.status = 'Progress: ' + Math.round( 100 * this.amt ) + '%';
		return;
	}
	if( theDiv.style ) {
		theDiv = theDiv.style;
		theDiv.clip = 'rect(0px ' + theWidth + 'px ' + theHeight + 'px 0px)';
	}
	var oPix = document.childNodes ? 'px' : 0;
	theDiv.width = theWidth + oPix; theDiv.pixelWidth = theWidth;
	theDiv.height = theHeight + oPix; theDiv.pixelHeight = theHeight;
	if( theDiv.resizeTo ) {
		theDiv.resizeTo( theWidth, theHeight );
	}
	theDiv.left = ( ( this.dir != 3 ) ? 0 : this.width - theWidth ) + oPix;
	theDiv.top = ( ( this.dir != 4 ) ? 0 : this.height - theHeight ) + oPix;
}
function setColour( p_color ) {
	var theDiv = getRefToDivNest( this.id );
	if( theDiv.style ) {
		theDiv = theDiv.style;
	}
	theDiv.bgColor = p_color;
	theDiv.backgroundColor = p_color;
	theDiv.background = p_color;
}
