/**
 *	 Copyright 2003 Componence DDS. All rights reserved.
 *	 Last change date: 26.12.2003
 *
 *	 Browser compatibility :
 *	 PC :
 *			NS 4.7
 *			IE 5.0
 *			Mozilla 1.0
 *			Opera 5.0
 *	 Apple :
 *			?????
 *	 Description :
 *			This file is used for global work
 *  Defines global variable : 
 *			Global
 */

/**
 * JSGlobal is the javascript class which contains the most used functions
 * A JSGlobal object encapsulates static function for common work :
 *		isDefined
 *		include
 *		getObject
 *      debug
 *      required
 * 
 * @version	{version}
 * @constructor
 * @since		1.0
 */
function /*:JSClass:*/ JSGlobal() {
	this.version = "{version}";
	
	function /*:Object:*/ parseVersion( /*:String:*/ version ) {
		var /*:String:*/ tmp = String( version ).split(".");
		var /*:Object:*/ v = new Object();
		v.major = parseInt( tmp[0] );
		v.minor = parseInt( tmp[1] );
		v.patch = parseInt( tmp[2] );
		return v;
	}
	this.parseVersion = parseVersion;
	
	function /*:Object:*/ getVersion( /*:Object:*/ classInstance ) {
		if ( typeof( classInstance ) == "function" ) {
			return this.parseVersion( new classInstance().version );
		} else if ( typeof( classInstance ) == "object" ) {
			return this.parseVersion( classInstance.version );
		} else if( typeof( classInstance ) == "string" ) {
			return this.getVersion( window[classInstance] );
		}
		return null;
	}
	this.getVersion = getVersion;
	
	function extend( /*:JSClass:*/ subClass, /*:JSClass:*/ superClass ) {
		subClass.prototype = new superClass();
	}
	this.extend = extend;

	this.includeJSFiles = new Array();
	
	function /*:Boolean:*/ required() {
	  var err = 0;
		var msg = " required follow classes:\t";
		var clr = required.caller;
		var clrName = this.getCallerName( clr );
		clrName = ( this.isDefined( clrName ) ) ? clrName : "";
		msg = clrName + msg;
		for ( var a = 0; a < arguments.length; a++ ) {
		  var JSClass = arguments[a];
		  if ( !window[JSClass] ) {
				err++;
		  	msg += "\n  " + err + ". " + JSClass;
		    if ( ComponenceComponents[JSClass] ) {
					msg += " (Files: " + ComponenceComponents[JSClass] + ")";
				}
		  }
		}
		if( err > 0 ) {
			alert( msg );
			return false;
		}
		return true;
	}
	this.required = required;
	
	this.getCallerName = function( clr ) {
		var tmp = String( clr ).split( "{" );
		if( tmp ) {
			var clrName = String( tmp[0] );
			clrName = clrName.replace( /(.*\/.*\/)(.*)(\(.*\))(\s)(\{?)/g, "$2" );
			return clrName;
		}
	};

 /**
 	* @private
	* Returns void
	* @param  sourceFile - path to source file which will be included
	* @return void
	* @type boolean
	* @see    JSGlobal
	*/
	this.isSourceFileIncluded = function /*:boolean:*/ ( /*:string:*/ sourceFile ) {
		var result = false;
		for ( var i = 0; i < this.includeJSFiles.length; i++ ) {
			if ( this.includeJSFiles[i] == sourceFile ) {
				result = true;
				break;
			}
		}
		return result;
	};
	
 /**
 	* public method
	* Returns boolean result : true - if object is defined and false - if not defined
	* @param  value  object, will be checked
	* @return boolean value
	* @type boolean
	* @see    JSGlobal
	*/	
  function /*:Boolean:*/ isDefined ( /*:Object:*/ value ) {
		var s = String( value );
		return ( ( s != 'undefined' ) && ( s != 'null' ) );
	}
	this.isDefined = isDefined;

 /**
 	* public method
  * including script file into html page
  * @param  sourceFile  file name , will be included
  * @return void
  * @type void
	* @see    JSGlobal
  */	
	function /*:Void:*/ include ( /*:String:*/ sourceFile, /*:String:*/ clsName ) {
		if ( !this.isSourceFileIncluded( sourceFile ) ) {
			this.includeJSFiles[this.includeJSFiles.length] = sourceFile;
			if ( sourceFile.toLowerCase().lastIndexOf(".js") != -1 ) {
				document.writeln( "<script type='text/javascript' language='javascript' src='" + sourceFile + "'></script>" );
			} else if ( sourceFile.toLowerCase().lastIndexOf(".css") != -1 ) {
				document.writeln( "<link href='" + sourceFile + "' rel='stylesheet'>" );
			} else if (( sourceFile.toLowerCase().lastIndexOf(".vb") != -1 ) || ( sourceFile.toLowerCase().lastIndexOf(".vbs") != -1 )) {
				document.writeln( "<script  type='text/vbscript' language='vbscript' src='" + sourceFile + "'></script>" );
			} else if (( sourceFile.toLowerCase().lastIndexOf(".htm") != -1 ) || 
			( sourceFile.toLowerCase().lastIndexOf(".html") != -1 ) ||
			( sourceFile.toLowerCase().lastIndexOf(".pl") != -1 ) ||
			( sourceFile.toLowerCase().lastIndexOf(".php") != -1 ) ||
			( sourceFile.toLowerCase().lastIndexOf(".asp") != -1 ) ||
			( sourceFile.toLowerCase().lastIndexOf(".jsp") != -1 )) {
				document.writeln( "<iframe src='" + sourceFile + "' frameborder='no' class='" + clsName + "'></iframe>" );
			} 
		}
 	}
	this.include = include;

 /**
 	* public method
  * Returns HTML object by id or if browser is NS 4.7 - by name
  * @param  id  id of HTML object or its name if NS 4.7 
  * @return HTML object if is defined in HTML page
  * @type object
	* @see    JSGlobal
  */	
	function /*:Object:*/ getObject ( /*:String:*/ id ) {
		var obj = null;
		if ( typeof id == "object" ) {
			obj = id;
		} else if ( document.getElementById ) {
			obj = document.getElementById( id );
		} else if ( document.all ) {
			obj = document.all[id];
		} else if ( document.layers ) {
			/*for NS 4.7 , id must be name*/
			obj = document.layers[id];
			if ( !this.isDefined( obj ) ) {
				obj = document.images[id];
			} else {
				obj = null;
			}
		} else if ( document.forms[id] ) {
			obj = document.forms[id];
		}
		return obj;
	}
	this.getObject = getObject;
	
	function /*:Void:*/ debug( /*:Object:*/ obj, /*:Event:*/ evnt ) {
		var debugStr = "";
		
		debugStr += "OBJECT PROPERTIES:\r\n";
 		for( var i in obj ) {
 			debugStr += "\t[" + typeof obj[i] + "] " +i + " = " + ( ( typeof obj[i] == "function" ) ? String( obj[i] ).split("{")[0] : obj[i] ) + "\r\n";
 		}
 		
 		if( evnt ) {
 			debugStr += "\r\nEVENT PROPERTIES:\r\n";
 			for( var e in evnt ) {
				debugStr += "\t" + e + " = " + evnt[e] + "\r\n";
			}
 		}
 		
 		this.debugWindow = window.open( "", "debugWindow", "resizable=yes, scrollbars=yes, width=500, height=200, status=no" );
		with ( this.debugWindow.document ) {
			open();
			write( "<html><head><title>Debug Window</title></head><body style='margin:0;padding:0;' scroll=no><textarea style='width:100%;height:100%;margin:0;border:0;'>" + debugStr + "</textarea>" );
			close();
		}
		this.debugWindow.focus();
		return this.debugWindow;
	}
	this.debug = debug;
}

/**
*		Create global Global object
*/
var /*:Object:*/ Global = new JSGlobal();
