/**
 * This class represent javascript environnement.
 * Compatible : IE, Firefox, Safari, Opera
 * @package DHL.JS
 */
if (DHL == undefined) var DHL = {};
(DHL.JS = function () {
	
	if (DHL.JS.className == undefined) {

		DHL.JS.sIncludePath = '/lib/javascript/';
		DHL.JS.strBasePath  = '';
		DHL.JS.oHTTPRequest = null;

		if (typeof(XMLHttpRequest) == 'undefined') {
			DHL.JS.oHTTPRequest = new ActiveXObject("Microsoft.XMLHTTP");
		} else {
			DHL.JS.oHTTPRequest = new XMLHttpRequest();
		}

		DHL.JS.typeJS = 1;  
		DHL.JS.typeXML = 2;

		DHL.JS.setBasePath = function(strBasePath) {
		    DHL.JS.strBasePath = strBasePath;
		}
		
		DHL.JS.packageExists = function packageExists(psPackage) { 
			var aPackage = psPackage.split('.');
			var bOk = true, sPackage = 'window';
			for(i in aPackage) {
				sPackage += '.' + aPackage[i].toUpperCase();
				if (bOk) { bOk = eval('typeof(' + sPackage + ') != \'undefined\''); }
			}
			return bOk;
		}

		/**
		 * Require statement includes and evaluates the specific file.
		 * @param string psPackage
		 * @param integer piType
		 * @return boolean
		 */
		DHL.JS.require = function require(psPackage, piType, strBaseUrl) {

		    	if(typeof strBaseUrl == "undefined"){ strBaseUrl = DHL.JS.strBasePath; }
			if (piType == null) { piType = DHL.JS.typeJS; }
			if (DHL.JS.packageExists(psPackage) == false) {

				sPath = psPackage.replace(/\./g, '/');
				switch(piType) {
				case DHL.JS.typeJS:
					sPath = sPath.concat('.js');
				break;
				case DHL.JS.typeXML:
					sPath = sPath.concat('.xml');
				break;
				}

				DHL.JS.oHTTPRequest.open("GET", strBaseUrl + DHL.JS.sIncludePath + sPath, false);
				DHL.JS.oHTTPRequest.send(null);
				
            if (DHL.JS.oHTTPRequest.status == 200 || DHL.JS.oHTTPRequest.status == 0) {
            	var osHTTPResponse;

	            switch(piType) {
					case DHL.JS.typeJS:
						osHTTPResponse = DHL.JS.oHTTPRequest.responseText;
					break;
					case DHL.JS.typeXML:
						if (DHL.JS.oHTTPRequest.responseXML.documentElement.firstChild.nextSibling != null) {
							// FireFox
							osHTTPResponse = DHL.JS.oHTTPRequest.responseXML.documentElement.firstChild.nextSibling.nodeValue;
						} else {
							// IE
							osHTTPResponse = DHL.JS.oHTTPRequest.responseXML.documentElement.firstChild.data;
						}
					break;
					}
            	
					var oSelf = DHL; // current scope.
					(function () {
						var DHL = oSelf;
						eval(osHTTPResponse);
						var aPackage = psPackage.split('.');
						var sPackage = '';
						for(i in aPackage) {
							sPackage += (sPackage ? '.' : '');
							sPackage += aPackage[i].toUpperCase();
							var sAction = ("if (typeof(window." + sPackage + ") == 'undefined') { window." + sPackage + " = {}; } ");
							eval(sAction);
						}
					})();
				}
			}

			return true;
		}

		/**
		 * STATIC: This method is called to create an 'inheritance' in javascript
		 * @param object objChild
		 * @param object objParent
		 * @return void
		 */
		DHL.JS.copyPrototype = function (objChild, objParent) {
			 for (var strFunction in objParent.prototype) {
			 	objChild.prototype[strFunction] = objParent.prototype[strFunction];
			 }
		}
		
		DHL.JS.stdWakeup = function(sClassName, aData) {
			oTmp = new Object();
			for(i in aData) { oTmp[i] = aData[i]; }
			oTmp.className = sClassName;
			return oTmp;
		}
		
		DHL.JS.className = 'DHL.JS';

	}

	this.jsClass = DHL.JS;


})();
