var lib10 = {};

// *** browser detection

lib10.browserDetect = function()
{
	var agt = navigator.userAgent.toLowerCase();

	if( agt.indexOf("gecko")!=-1 )
	{
		lib10._IsGekko = true;
	};

    lib10._IsNav = ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('spoofer')==-1)
          && (agt.indexOf('compatible') == -1) && (agt.indexOf('hotjava')==-1));

	lib10._IsNavVersion = parseFloat(navigator.appVersion);

	if( (/msie/).test( agt ) )
	{
		lib10._IsIE = true;
		temp=navigator.appVersion.split("MSIE");
		lib10._IsVersion = parseFloat(temp[1]);
	}
	else if( agt.indexOf("firefox")!=-1 )
	{
		lib10._IsFF = true;
		versionindex= agt.indexOf("firefox")+8;
		lib10._IsVersion = parseInt(navigator.userAgent.charAt(versionindex));
	}
	else if( agt.indexOf("opera")!=-1 )
	{
		lib10._IsOpera = true;
		versionindex= agt.indexOf("Opera")+6;
		lib10._IsVersion = parseInt(navigator.userAgent.charAt(versionindex));
	}
	else if( agt.indexOf("webtv") != -1 )
	{
		lib10._IsWebTV = true;
	};
};

lib10.browserDetect();

// *function emulation

// insertAdjacent ... for Netscape

if(typeof HTMLElement!="undefined" && ! HTMLElement.prototype.insertAdjacentElement)
{
	HTMLElement.prototype.insertAdjacentElement = function(where,parsedNode)
	{
		switch (where){
		case 'beforeBegin':
			this.parentNode.insertBefore(parsedNode,this);
			break;
		case 'afterBegin':
			this.insertBefore(parsedNode,this.firstChild);
			break;
		case 'beforeEnd':
			this.appendChild(parsedNode);
			break;
		case 'afterEnd':
			if (this.nextSibling) 
				this.parentNode.insertBefore(parsedNode,this.nextSibling);
				else this.parentNode.appendChild(parsedNode);
			break;
		}
	}

	HTMLElement.prototype.insertAdjacentHTML = function(where,htmlStr)
	{
		var r = this.ownerDocument.createRange();
		r.setStartBefore(this);
		var parsedHTML = r.createContextualFragment(htmlStr);
		this.insertAdjacentElement(where,parsedHTML);
	}


	HTMLElement.prototype.insertAdjacentText = function(where,txtStr)
	{
		var parsedText = document.createTextNode(txtStr);
		this.insertAdjacentElement(where,parsedText);
	}
}

// *** onload event stacking

if( ! window.lib10 ) lib10 = new Object();

lib10.getHttpRequest_mode = -1;

if( window.XMLHttpRequest )
{
	lib10.getHttpRequest_mode = 1;

	lib10.getHttpRequest = function()
	{
		try
		{
			return new XMLHttpRequest();
		}
		catch(e)
		{
			return null;
		}
	};
}
else
{
	lib10.getHttpRequest = function()
	{
		var A;

		try
		{	
			if( lib10.getHttpRequest_mode < 0 )
			{
				try
				{
					A = new ActiveXObject( "Msxml2.XMLHTTP" );
					lib10.getHttpRequest_mode = 2;
				}
				catch( eTryOtherMS )
				{
					A = new ActiveXObject( "Microsoft.XMLHTTP" );				
					lib10.getHttpRequest_mode = 3;
				}
			}
			else
			{
				switch( lib10.getHttpRequest_mode )
				{
					case 2:
						A = new ActiveXObject( "Msxml2.XMLHTTP" );
						break;

					case 3:
						A = new ActiveXObject( "Microsoft.XMLHTTP" );
						break;

					default:
						A = null;
				}
			}
		}
		catch ( eNoAjax )
		{
			A = null;
			lib10.getHttpRequest_mode = 0;
		}

		return A;
	}
}

lib10._modules = {};
lib10._loadedmodules = {};
lib10._loadstack = [];
lib10._isloading = false;

lib10.stackModule = function( script , obj , callback , args )
{
	if ( script in lib10._loadedmodules ) return false;

	lib10._loadedmodules[script] = true;

	var si = this._modules[ script ] ;

	if ( ! si )
	{
		lib10.alert( 'The script "' + script + '" was not registered' );
		return false;
	};

	var sBaseScriptName = lib10basedir + si.BasePath + script.toLowerCase();

	var so = new Object();

	so.filename = sBaseScriptName + '.js';
	so.obj = obj;
	so.callback = callback;
	so.args = args || [];

	lib10._loadstack.push( so ) ;

	for ( var i = 0 ; i < si.Dependency.length ; i++ )
	{
		lib10.stackModule( si.Dependency[i] );
	}

	return true;
};

lib10.loadModule = function( scriptName , obj , callback , args )
{
	if( lib10.stackModule( scriptName , obj , callback , args ) ) 
	{
		lib10._loadScripts();
	}
	else if( callback ) callback.apply( obj , args || [] );
}

lib10._loadScripts = function()
{
	if( lib10._isloading ) return;
	lib10._isloading = true;
	
	var x = lib10.getHttpRequest(); 
	if( ! x ) return true;

	so = lib10._loadstack.pop();

	x.open('GET', so.filename , true);
	
	x.onreadystatechange = function() 
	{
		if (x.readyState != 4) return;
		var data = String(x.responseText);
		eval.call( document , data );

		if( so.callback ) so.callback.apply( so.obj , so.args );

		lib10._isloading = false;

		if( lib10._loadstack.length > 0 ) lib10._loadScripts();
	}

	x.send('');

	return false;
}

lib10.registerModule = function( script, scriptBasePath, dependency )
{
	this._modules[ script ] =
	{
		BasePath : scriptBasePath || '',
		Dependency : dependency || []
	};
}

lib10.addLoadEvent = function( func , win)
{	
	if( ! win ) win = window;

	var oldonload = win.onload;
	if (typeof win.onload != 'function'){
    	win.onload = func;
	} else {
		win.onload = function(){
		oldonload();
		func();
		}
	}
}

lib10.loadDiv = function( div , request , callback , arg )
{	
	var x = lib10.getHttpRequest(); 
	if( ! x ) return true;

	x.open('GET', request , true);
	
	x.onreadystatechange = function() 
	{
		if (x.readyState != 4) return;
		var data = String(x.responseText);
		
		document.getElementById( div ).innerHTML = data;
		
		if( callback ) callback( arg );
	}

	x.send('');

	return false;
}


lib10.xposition = function( item )
{
	return item.offsetLeft + lib10.xcontainment( item );
}

lib10.yposition = function( item )
{
	return item.offsetTop + lib10.ycontainment( item );
}

lib10.xcontainment = function( item )
{
	var x=0;

	walk = item.offsetParent;

	while( walk )
	{
		x += walk.offsetLeft;
		walk = walk.offsetParent;
	};

	return x;
};

lib10.ycontainment = function( item )
{
	var y=0;

	walk = item.offsetParent;

	while( walk )
	{
		y += walk.offsetTop;
		walk = walk.offsetParent;
	};

	return y;
};


// *** default confirm and alert wrappers

lib10.alert = function( txt , action )
{
	alert( txt );
	if( action ) action();
}

lib10.confirm = function( txt , caption , action )
{
	if( confirm( txt ) )
	{
		if( action ) 
		{
			action();
			return false;
		}
		else return true; 
	};

	return false;
};

lib10.submitForm = function( f , button , value )
{
	nh = document.createElement("div");
	nh.type='hidden';
	nh.value=value;
	nh.name=button;	
	f.appendChild( nh );
	f.submit();
	return;
};

// *** Mouse tracking and dragging

lib10.addMouseMoveEvent = function( func , doc )
{	
	if( ! doc ) doc = document;

	var oldmouse = doc.onmousemove;
	
	if (typeof doc.onmousemove != 'function'){
    	doc.onmousemove = func;
	} else {
		doc.onmousemove = function( e ){
		oldmouse( e );
		func( e );
		}
	}
}

lib10.keepMouseInfo = function( e )
{
	if(document.all)
	{
		document.mouseX = event.x;
		document.mouseY = event.y;
	}
	else
	{	
		document.mouseX = ( e.pageX ? e.pageX : e.x );
		document.mouseY = ( e.pageY ? e.pageY : e.y );
	};

	if( lib10.dragfunction ) 
	{
		if( lib10.dragobject ) lib10.dragfunction.apply(lib10.dragobject, []);
		  else lib10.dragfunction();
	};
};

lib10.addMouseMoveEvent( lib10.keepMouseInfo );


// *********************************** sprintf implementation

sprintfWrapper = {

    init : function () {

        if (typeof arguments == 'undefined') { return null; }
        if (arguments.length < 1) { return null; }
        if (typeof arguments[0] != 'string') { return null; }
        if (typeof RegExp == 'undefined') { return null; }

        var string = arguments[0];
        var exp = new RegExp(/(%([%]|(\-)?(\+|\x20)?(0)?(\d+)?(\.(\d)?)?([bcdfosxX])))/g);
        var matches = new Array();
        var strings = new Array();
        var convCount = 0;
        var stringPosStart = 0;
        var stringPosEnd = 0;
        var matchPosEnd = 0;
        var newString = '';
        var match = null;

        while (match = exp.exec(string)) {
            if (match[9]) { convCount += 1; }

            stringPosStart = matchPosEnd;
            stringPosEnd = exp.lastIndex - match[0].length;
            strings[strings.length] = string.substring(stringPosStart, stringPosEnd);

            matchPosEnd = exp.lastIndex;
            matches[matches.length] = {
                match: match[0],
                left: match[3] ? true : false,
                sign: match[4] || '',
                pad: match[5] || ' ',
                min: match[6] || 0,
                precision: match[8],
                code: match[9] || '%',
                negative: parseInt(arguments[convCount]) < 0 ? true : false,
                argument: String(arguments[convCount])
            };
        }
        strings[strings.length] = string.substring(matchPosEnd);

        if (matches.length == 0) { return string; }
        if ((arguments.length - 1) < convCount) { return null; }

        var code = null;
        var match = null;
        var i = null;

        for (i=0; i<matches.length; i++) {

            if (matches[i].code == '%') { substitution = '%' }
            else if (matches[i].code == 'b') {
                matches[i].argument = String(Math.abs(parseInt(matches[i].argument)).toString(2));
                substitution = sprintfWrapper.convert(matches[i], true);
            }
            else if (matches[i].code == 'c') {
                matches[i].argument = String(String.fromCharCode(parseInt(Math.abs(parseInt(matches[i].argument)))));
                substitution = sprintfWrapper.convert(matches[i], true);
            }
            else if (matches[i].code == 'd') {
                matches[i].argument = String(Math.abs(parseInt(matches[i].argument)));
                substitution = sprintfWrapper.convert(matches[i]);
            }
            else if (matches[i].code == 'f') {
                matches[i].argument = String(Math.abs(parseFloat(matches[i].argument)).toFixed(matches[i].precision ? matches[i].precision : 6));
                substitution = sprintfWrapper.convert(matches[i]);
            }
            else if (matches[i].code == 'o') {
                matches[i].argument = String(Math.abs(parseInt(matches[i].argument)).toString(8));
                substitution = sprintfWrapper.convert(matches[i]);
            }
            else if (matches[i].code == 's') {
                matches[i].argument = matches[i].argument.substring(0, matches[i].precision ? matches[i].precision : matches[i].argument.length)
                substitution = sprintfWrapper.convert(matches[i], true);
            }
            else if (matches[i].code == 'x') {
                matches[i].argument = String(Math.abs(parseInt(matches[i].argument)).toString(16));
                substitution = sprintfWrapper.convert(matches[i]);
            }
            else if (matches[i].code == 'X') {
                matches[i].argument = String(Math.abs(parseInt(matches[i].argument)).toString(16));
                substitution = sprintfWrapper.convert(matches[i]).toUpperCase();
            }
            else {
                substitution = matches[i].match;
            }

            newString += strings[i];
            newString += substitution;

        }
        newString += strings[i];

        return newString;

    },

    convert : function(match, nosign){
        if (nosign) {
            match.sign = '';
        } else {
            match.sign = match.negative ? '-' : match.sign;
        }
        var l = match.min - match.argument.length + 1 - match.sign.length;
        var pad = new Array(l < 0 ? 0 : l).join(match.pad);
        if (!match.left) {
            if (match.pad == '0' || nosign) {
                return match.sign + pad + match.argument;
            } else {
                return pad + match.sign + match.argument;
            }
        } else {
            if (match.pad == '0' || nosign) {
                return match.sign + match.argument + pad.replace(/0/g, ' ');
            } else {
                return match.sign + match.argument + pad;
            }
        }
    }
}

sprintf = sprintfWrapper.init;

