////////////// Class Definition: cEnviron ////////////////////////////////////////
// purpose: to provide environment information about the hosting web page 
// requires: 
//////////////////////////////////////////////////////////////////////////////////
function cEnviron(){
	//PROPERTIES
	//METHODS PUBLIC
	this.CurrentPath = CurrentPath;
	this.FilenameWithExt = FilenameWithExt; 
	this.FilenameNoExt = FilenameNoExt; 
	this.QueryValue = QueryValue;
	this.QueryValue2 = QueryValue2; //does correct unescaping only to the value portion of query strings
	this.QueryString = QueryString;
	this.__QueryString2 = __QueryString2; //does not unescape the whole query string
	this.getURLNoQueryString = getURLNoQueryString;
	this.BrowserName = BrowserName;
	this.BrowserVersion = BrowserVersion; //to be implemented
	this.OS = OS;
	this.AppendQueryString = AppendQueryString;
	this.ExtractQueryString = ExtractQueryString;
	
	//METHODS PRIVATE
	this.__KeyValue = __KeyValue;
	this.__KeyValue2 = __KeyValue2;
	this.__DumpBrowserInfo = __DumpBrowserInfo;
	this.__StripFileExtension = __StripFileExtension;
	this.__extractFileName = __extractFileName;
	this.__mAppendFileExt = __mAppendFileExt;
	this.__removeKey = __removeKey;
	
	//FLOW
	//METHOD DEFINITIONS
	function _______Public_______(){};
	function CurrentPath(){//tested on IE, Netscape, Firefox
		//returns the portion of the current URL without the current filename; 
		//so that absolute path can be constructed out of this path plus a relative path
		var sFullURL = window.location.href;
		if (sFullURL.indexOf("?") != -1) sFullURL = sFullURL.split("?")[0]; //works if there is only one ? in the URL string -- that is the query string starter character
		if (sFullURL.indexOf("#") != -1) sFullURL = sFullURL.split("#")[0]; //works if there is only one # to designate an anchor
		var a = sFullURL.split("/"); a.pop(); 
		return (a.join("/") + "/");
	}
	function QueryValue(sKey){
		return this.__KeyValue(sKey, this.QueryString(), false)
	}
	function QueryString(){
		//returns the queryString without the starting ? and converts ULR encoded chars back to normal string
		var s = location.search;
		return unescape(s.substr(1)); 
	}
	function QueryValue2(sKey){
		return this.__KeyValue2(sKey, this.__QueryString2());
	}
	function __QueryString2(){
		//this does not unescape the whole querystring, as compared with QueryString();
		return location.search.substr(1);	
	}
	function AppendQueryString(sKey, sValue, sQueryString){
		//returns the current querystring plus a new pair of key and value;
		//replaces the previous key and value if it exists previously;
		//sQueryString is optional, when present, returned QS updates this sQueryString, when abasent, returned QS updates current document's querystring
		if (sQueryString == null || typeof(sQueryString) != "string") {
			var sCleanQS = this.__removeKey(sKey, this.QueryString());
		} else {
			var sCleanQS = this.__removeKey(sKey, sQueryString);
		}
		if (sCleanQS=="" || sCleanQS == null) {
			return sKey+"="+sValue;	
		} else {
			return sCleanQS + "&" + sKey + "=" + sValue;
		}
	}
	function ExtractQueryString(sURL){//new not tested
		//returns the querystring part of the url 
		//this can be used to get the querystring of urls other than the current document's
		if (typeof(sURL)!="string") throw "Error in ExtractQueryString(), expecting a sting parameter";
		if (sURL.indexOf("?")==-1) return "";
		var a = sURL.split("?");
		a.shift();
		return a.join("?");
	}
	function getURLNoQueryString(sURL){
		if (typeof(sURL) != "string") return "";
		var a = sURL.split("?");
		return a[0];
	}
	function FilenameWithExt(){
		return this.__extractFileName(document.URL);
	}
	function FilenameNoExt(){
		return (this.__StripFileExtension(this.FilenameWithExt()));
	}
	function BrowserName(){
		if (navigator.appName) {
			if (navigator.userAgent.indexOf("Firefox") != -1) return "Firefox";
			if (navigator.userAgent.indexOf("Opera") != -1) return "Opera";
			if (navigator.userAgent.indexOf("Safari")!=-1) return "Safari";
			if (navigator.appName == "Netscape") return "Netscape";
			if (navigator.appName.indexOf("Microsoft") != -1) return "IE";
		}
		return "unknown";
	}
	function OS(){
		if (navigator.platform){
			if (navigator.platform.indexOf("Mac")!=-1) return "Mac";
			if ((navigator.userAgent.indexOf('Win') != -1) && (navigator.userAgent.indexOf('95') != -1)) return "Win95";
			if (navigator.platform.indexOf("Win32")!=-1) return "Win32";
			if (navigator.platform.indexOf("Linux")!=-1) return "Linux";
		}
		return "unknown";
	}
	
	function _______Private_______(){};
	function __KeyValue(sKey, sKeyValueString, bCaseSensitive){
		//return the value of the key; if the key does not exist in the key-value pair string, "" is returned;
		//expects sKeyValueString to be a clean string of key=value's separated by "&"
		if ((sKey == "")||(sKey==null)) return "";
		if ((sKeyValueString == "") || (sKeyValueString == null)) return "";
		var aKeyValues = sKeyValueString.split("&");
		var aPair;
		for (var i=0;i<aKeyValues.length;i++){
			aPair = aKeyValues[i].split("=");
			if (bCaseSensitive) {
				if (aPair[0] == sKey) {
					aPair.shift();
					return aPair.join("=");
				}
			} else {
				if (aPair[0].toLowerCase() == sKey.toLowerCase()) {
					aPair.shift();
					return aPair.join("=");
				}
			}
		}
		return "";
	}
	function __KeyValue2(sKey, sKeyValueString){
		//same as __KeyValue except that it unescapes the value it retrieve
		return unescape(this.__KeyValue(sKey, sKeyValueString));
	}
	function __DumpBrowserInfo(){
		var s = "";
		s += "appCodeName=" + navigator.appCodeName + "\n";
		s += "appName="  + navigator.appName +  "\n";
		s += "appVersion="  + navigator.appVersion +  "\n";
		s += "platform=" + navigator.platform + "\n";
		s += "userAgent=" + navigator.userAgent + "\n";
		return (s);
	}
	function __extractFileName(sFullURL){//tested OK;
		//sPath name is the full path URL 
		//Netscape returns a path with forward / from document.URL
		var sBackwardPathChar = String.fromCharCode(92);
		var sForwardPathChar = String.fromCharCode(47);
		var sPathChar = (sFullURL.indexOf(sBackwardPathChar) == -1)?sForwardPathChar:sBackwardPathChar
		var a = sFullURL.split(sPathChar);
		return a[a.length - 1];
		//return a.pop();
	}
	function __StripFileExtension(sFileName){//tested OK;
		//returns the filename without the extension;
		//filename extension must be separated with a .;
		//. must not be used in the filename except to indicate the file extension;
		if ((sFileName == null) || (sFileName == "")) return sFileName;
		if (sFileName.indexOf(".") == -1) return sFileName;
		return sFileName.split(".")[0];
	}
	function __mAppendFileExt(sFileName){
		//return the file name with .htm appended at the end;
		//all filenames must end in .htm,  not in .html;
		return sFileName + ".html";
	}
	
	function __removeKey(sKey, sQueryString){
		//keys are NOT case-sensitive
		if (typeof(sQueryString)!="string"||sQueryString == "") return "";
		var aPairs = sQueryString.split("&");
		var aNewPairs = new Array();
		for (var i = 0; i<aPairs.length; i++) {
			if (aPairs[i].split("=")[0].toUpperCase()!=sKey.toUpperCase()) aNewPairs[aNewPairs.length] = aPairs[i];
		}
		return aNewPairs.join("&");
	}
	function _______Undefined_______(){};
	function BrowserVersion(){}
	function isSupportedJScriptEngine(){
		//returns true if major and minor versions of the script engine is equal to or greater than 5.6
		//works only for Windows and IE
		if (this.OS()!="Win32") return false;
		if (this.BrowserName()!="IE") return false;
		if (ScriptEngineMajorVersion() > 5) return true;
		if (ScriptEngineMinorVersion() >= 6) return true;
		return false;
	}
}
	
  


