// VERSION 1.0  (2005.09.21)
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
// Browser Detection Silliness
//
// The point of this file is to consolidate Javascript DHTML Browser detection into
// one place/module.  Browser Compatibilty/Detection seems to be the most often asked
// javascript question so I thought it would make sense to put routines pertaining to
// these differences in one place.
//
//
// I am by no means a javascript/DHTML expert so consider this NOT FINISHED/CORRECT!
// In fact, I invite others to edit/add to this as they see fit.
//
// USAGE:
//  - In your HTML file, include this line within the <HEAD> section:
//      <SCRIPT LANGUAGE="javascript" SRC="BrowserDetector.js" type="text/javascript"></SCRIPT>
//  - also, add the following block to the <HEAD> section:
//   (or something that instatiates a BrowserDetector object to a global)
//      <SCRIPT LANGUAGE="javascript">
//	        function PageLoad() { //make sure this (or something similar) is called in the page "onLoad()" event
//		        goBD = new BrowserDetector(); //global variable
//          }
//      </SCRIPT>
//  - finally, make the page call the function above when it loads with something like:
//      <BODY onLoad="PageLoad();">
//
//  - if, however, you've got your own javascript source file and you call your own
//    custom routine for the Page onLoad() event, just be sure to instantiate a global
//    BrowserDetector();
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

// Decide browser version
// Object Declaration
function BrowserDetector() {

	//Properties
	this.BrowserInfo = navigator.appName + " " + parseFloat(navigator.appVersion);
	this.ns4 = (navigator.appName == 'Netscape' && parseInt(navigator.appVersion) == 4);
	this.ns6 = (document.getElementById)? true:false;
	this.ie4 = (document.all)? true:false;
	this.ie5 = false;

	if (this.ie4) this.docRoot = 'document.body';
	if (this.ns4) {
		var oW = window.innerWidth;
		var oH = window.innerHeight;
	}
	
	// Microsoft Stupidity Check(tm).
	if (this.ie4) {
		if ((navigator.userAgent.indexOf('MSIE 5') > 0) || (navigator.userAgent.indexOf('MSIE 6') > 0) || (navigator.userAgent.indexOf('MSIE 7') > 0)) {
			if(document.compatMode && document.compatMode == 'CSS1Compat') this.docRoot = 'document.documentElement';
			this.ie5 = true;
		}
		if (this.ns6) {
			this.ns6 = false;
		}
	}
	
	
	// Capture events, alt. diffuses the overlib function.
	if ( (this.ns4) || (this.ie4) || (this.ns6)) {
		//document.onmousemove = mouseMove
		//if (this.ns4) document.captureEvents(Event.MOUSEMOVE)
	} else {
		this.ver3fix = true;
	}
	//alert("BrowserDetection:\nBrowserInfo=" + this.BrowserInfo +"\nns4=" + this.ns4 + "; ns6=" + this.ns6 + "; ie4=" + this.ie4 + "; ie5=" + this.ie5 + "; ver3fix=" + this.ver3fix);
	
	//Methods
	this.GetDocObject                    = pfnGetBrowserDocObject;
	this.GetDocObjectByName              = pfnGetBrowserDocObjectByName;
	this.CheckBoxValueSet                = pfnCheckBoxValueSet;
	this.CheckBoxValueGet                = pfnCheckBoxValueGet;
	this.SelectBoxSelectIndexList        = pfnSelectBoxSelectIndexList;
	this.SelectBoxSelectValueList        = pfnSelectBoxSelectValueList;
	this.SelectBoxSelectedIndexes        = pfnSelectBoxSelectedIndexes;
	this.SelectBoxSelectedValues         = pfnSelectBoxSelectedValues;
	this.SelectBoxEmpty                  = pfnSelectBoxEmpty;
	this.SelectBoxAdd                    = pfnSelectBoxAdd;
	this.SelectBoxSelectAll              = pfnSelectBoxSelectAll;
	this.RadioCollectionSelectSet        = pfnRadioCollectionSelectSet;
	this.RadioCollectionSelectSetByValue = pfnRadioCollectionSelectSetByValue;
	this.RadioCollectionSelectGet        = pfnRadioCollectionSelectGet;
	this.RadioCollectionSelectGetValue   = pfnRadioCollectionSelectGetValue;

	this.txtDebugInfo                    = this.GetDocObject('txtDebugInfo');
	this.DebugInfoClear                  = pfnDebugInfoClear;
	this.DebugInfoAdd                    = pfnDebugInfoAdd;
	this.BrowserIsIE                     = pfnBrowserIsIE;
}

function pfnBrowserIsIE() {
	//alert("Navigator.appName = [" + navigator.appName + "]");
	return (navigator.appName == "Microsoft Internet Explorer");
}

//CheckBox Methods
function pfnCheckBoxValueSet(CheckBoxObjectName,NewValue) {
	var oCheckBox = this.GetDocObject(CheckBoxObjectName);
	oCheckBox.checked = NewValue;
}

function pfnCheckBoxValueGet(CheckBoxObjectName) {
	var oCheckBox = this.GetDocObject(CheckBoxObjectName);
	return oCheckBox.checked;
}


//SelectBox (ListBox/Dropdowns) Methods
function pfnSelectBoxSelectAll(ActualSelectBoxObject,SelectionValueForAll) {
	var iOption=0;
	for(iOption=0;iOption<ActualSelectBoxObject.options.length;iOption++) {
		ActualSelectBoxObject.options[iOption].selected = SelectionValueForAll;
	}
}
function pfnSelectBoxEmpty(ActualSelectBoxObject) {
	while(ActualSelectBoxObject.options.length>0) {
		ActualSelectBoxObject.remove(0);
	}
}

function pfnSelectBoxAdd(ActualSelectBoxObject,OptionValue,OptionDesc) {
	var iOption = (pfnSelectBoxAdd.arguments.length >= 4) ? pfnSelectBoxAdd.arguments[3] : -1;
	var xOption = document.createElement("OPTION");
	xOption.value = OptionValue;
	xOption.text  = OptionDesc;
	//xField.add(xOption);
	if(iOption==-1) { //figure out position
		iOption = ActualSelectBoxObject.options.length;
	}
	ActualSelectBoxObject.options[iOption] = xOption;
	return iOption;
}

function pfnSelectBoxSelectIndexList(ActualSelectBoxObject,IndexList) {
	var iItem=0,aIndexes = IndexList.split(',');
	for(iItem=0;iItem<aIndexes.length;iItem++) {
		ActualSelectBoxObject.options[aIndexes[iItem]].selected = true;
	}
}

function pfnSelectBoxSelectValueList(ActualSelectBoxObject,IndexList) {
	var iOption=0,iItem=0,aValues = ValueList.split(','),oOption='';
	//more efficient with Option loop on outside
	for(iOption=0;iOption<ActualSelectBoxObject.options.length;iOption++) {
		oOption = ActualSelectBoxObject.options[iOption];
		for(iItem=0;iItem<aValues.length;iItem++) {
			if(oOption.value == aValues[iItem]) {
				oOption.selected = true;
				aValues.remove(iItem);
				break;
			}
		}
	}
}

function pfnSelectBoxSelectedIndexes(ActualSelectBoxObject) {
	var iOption=0,sResult='';
	//more efficient with Option loop on outside
	for(iOption=0;iOption<ActualSelectBoxObject.options.length;iOption++) {
		if(ActualSelectBoxObject.options[iOption].selected) {
			sResult += ',' + iOption;
		}
	}
	return sResult.substr(1);
}
function pfnSelectBoxSelectedValues(ActualSelectBoxObject) {
	var iOption=0,sResult='';
	//more efficient with Option loop on outside
	for(iOption=0;iOption<ActualSelectBoxObject.options.length;iOption++) {
		if(ActualSelectBoxObject.options[iOption].selected) {
			sResult += ',' + ActualSelectBoxObject.options[iOption].value;
		}
	}
	return sResult.substr(1);
}

// Radio methods
function pfnRadioCollectionSelectGet(RadioCollection) {
	var iResult=-1,iRadioButton=0,oRadioButton=0;
	for(iRadioButton=0;iRadioButton<RadioCollection.length;iRadioButton++) {
		oRadioButton = RadioCollection[iRadioButton];
		if(oRadioButton.checked) {
			iResult = iRadioButton;
			break;
		}
	}
	return iResult;
}
function pfnRadioCollectionSelectGetValue(RadioCollection) {
	var vResult=-1;iIndex,oRadioButton=0;
	iIndex = this.RadioCollectionSelectGet(RadioCollection);
	if(iIndex!=-1) {vResult = RadioCollection[iIndex].value;}
	return vResult;
}
function pfnRadioCollectionSelectSet(RadioCollection,SelectIndex) {
	var oRadioButton=0;
	if(SelectIndex<=RadioCollection.length) {
		oRadioButton = RadioCollection[SelectIndex];
		oRadioButton.checked = true;
	}
}
function pfnRadioCollectionSelectSetByValue(RadioCollection,SelectValue) {
	var iRadioButton=0,oRadioButton=0;
	for(iRadioButton=0;iRadioButton<RadioCollection.length;iRadioButton++) {
		oRadioButton = RadioCollection[iRadioButton];
		if(oRadioButton.value==SelectValue) {
			oRadioButton.checked = true;
			break;
		}
	}
}



//DebugBox methods (only work if there's a "txtDebugInfo" TextArea element on page)
function pfnDebugInfoAdd(DebugTextLine) {
	if(defined(this.txtDebugInfo)) {
		this.txtDebugInfo.value = DebugTextLine + "\n" + this.txtDebugInfo.value;
	}
}
function pfnDebugInfoClear() {
	if(defined(this.txtDebugInfo)) {
		this.txtDebugInfo.value='';
	}
}


//This is a method of the BrowserDetection() object - don't call it bare
function pfnGetBrowserDocObject(ObjectID) {
	var oResult;
	if ( (this.ns4) || (this.ie4) || (this.ie5) || (this.ns6) ) {
		if (this.ie5) {
			oResult = document.all.item(ObjectID);
		}
		if (this.ns6) {
			oResult = document.getElementById(ObjectID);
		}
	}
	return oResult;
}

//This is a method of the BrowserDetection() object - don't call it bare
function pfnGetBrowserDocObjectByName(ObjectName) {
	var oResult;
	if ( (this.ns4) || (this.ie4) || (this.ie5) || (this.ns6) ) {
		if (this.ie5) {
			oResult = document.all.item(ObjectName);
		}
		if (this.ns6) {
			oResult = document.getElementsByName(ObjectName);
		}
	}
	return oResult;
}



// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
// Generic routines - not part of class/object but useful anyway
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
function Word2Bool(SomeLogicalWord) {
	var sTestString = ",TRUE,YES,ON,1,-1,OUI,SI,JA,SIM,DA,"; //Fr,Es,De,Port,Ru
	var sSearchString = "," + SomeLogicalWord.toUpperCase() + ",";
	return (sTestString.indexOf(sSearchString) >=0);
}

function Bool2Word(SomeBoolean) {
	return (SomeBoolean == true) ? 'YES' : 'NO';
}

function defined(x) {
	var bResult=false;
	if(x != undefined) {
		if( x != null) {
			bResult = true;
		}
	}
	//return (x != undefined);
	return bResult;
}
