var navInfo = new Object();
navInfo.browserName = "";
navInfo.browserVersion = 0;
navInfo.browserCheck = false;
navInfo.osName = "";
navInfo.osVersion = 0;
navInfo.osCheck = false;


function CheckEnvironment(){
	// First we'll do an extensive check to see whether the users' system meets all requirements:
	// OS = WinNT4, Win2K, WinXP, Win98 or WinME
	// Browser = IE5.5 or higher
	// MSXML2 = 2.6 or higher
	
	CheckOSBrowser();
	
	var xmlsniffer = new XmlVersionSniffer();

	var problem = false;
	var errorObject = new Object();
	errorObject.simpleMsg = "";

	if (!navInfo.browserCheck){
		problem = true;
		errorObject.simpleMsg = "Your browser doesn't support Siebel Simulations. You will need Microsoft Internet Explorer 5.5 or 6 to run the course.";
	} else {
		//Browser is OK...
		if (!navInfo.osCheck){
			problem = true;
			errorObject.simpleMsg = "Your operating system doesn't support Siebel Simulations. You will need one of the following to run the course:\n   Microsoft Windows NT 4\n   Windows 2000\n   Windows XP\n   Windows 98\n   Windows ME\n";
		} else {
			//OS is OK...
			if (xmlsniffer.version < 2.6){
				problem = true;
				errorObject.simpleMsg = "Your system doesn't have a recent XML Parser.";
			} else {
				if (xmlsniffer.version == "unknown"){
					problem = true;
					errorObject.simpleMsg = "No XML Parser could be found. This might have to do with your security settings preventing the use of ActiveX components. Please consult your system administrator.";
				} else {
					//MSXML is OK
					problem = false;
					//alert("Your system is ready to run Siebel Simulations!");					
				}
			}
		}
	}

	if	(problem){
		//There was a problem, we're going to show the error window....

	   var errMsg = "";
	   errMsg += "Browser: " + navigator.appName + " " + navigator.appVersion + "\n";
   	errMsg += "------------------------------------------\n";

		errMsg += "XML:\n";
		errMsg += "Highest Version: " + xmlsniffer.version + "\n";
		errMsg += "Replace Mode: " + xmlsniffer.replace + "\n";
		errMsg += "All Versions:\n";
		for (var version in xmlsniffer.versions) {
			errMsg += "MSXML.versions[\"" + version + "\"] == " + xmlsniffer.versions[version] + "\n";
		}     
		
   	errMsg += "------------------------------------------\n";
		errMsg += "Browser Name: " + navInfo.browserName + "\n";
		errMsg += "Operating System: " + navInfo.osName + "\n";
   	errMsg += "------------------------------------------\n";
		errMsg += "Log:\n" + xmlsniffer.log + "\n";
		
		errorObject.extendedMsg = errMsg;
		errorObject.topicXML = "";
	   
		errorWindow = showModalDialog("errorreport.html", errorObject, "status:no; dialogLeft:" + parseInt((screen.width - 400) / 2) + "px; dialogTop:150px; dialogWidth:400px; dialogHeight:136px; resizable:no; help:no;");
		return false;
	} else {
		//No problem found...
		return true;
	}
}


function Trim(textString){
	//Trims out any trailing and ending spaces
	if (textString == 'undefined'){
		return "";
	}
	if (textString == null){
		return "";
	}
	if (textString.length == 0){
		return "";
	}
	
	var i = 0;
	while (textString.charAt(i) == " "){
		i++;
	}
	textString = textString.substr(i);
	
	i = textString.length - 1;
	while (textString.charAt(i) == " "){
		i--;
	}
	textString = textString.substr(0, i+1);
	
	return textString;
}

function XmlVersionSniffer()
{
    this.log = "";
    this.version = "unknown";
    this.versions = new Array();
    this.replace = false;
    
    this._dummyxml = '<?xml version="1.0"?><books><book/></books>';
    
    // set up methods
    this._log = _XmlVersionSniffer_log;
    this._check = _XmlVersionSniffer_check;
    this._createdoc = _XmlVersionSniffer_createdoc;
    this._checkfor30sp1 = _XmlVersionSniffer_checkfor30sp1;
    this._checkforreplace = _XmlVersionSniffer_checkforreplace;
    
    // do the check
    this._check();    
}

function _XmlVersionSniffer_log(s)
{
    this.log = this.log + s + "\n";
}

function _XmlVersionSniffer_check() 
{
    // First of all we want to check the highest version
    // of the parser that is installed by using the
    // version-dependent ProgID.
    try {
        var doc   = this._createdoc("2.5", "Microsoft.XMLDOM");
        var doc26 = this._createdoc("2.6", "MSXML2.DOMDocument.2.6");
        var doc30 = this._createdoc("3.0", "MSXML2.DOMDocument.3.0");
        
        // Check to see if 3.0 sp1 is installed
        this._checkfor30sp1(doc30);
        
        var doc40 = this._createdoc("4.0", "MSXML2.DOMDocument.4.0");
    }
    catch (e) {
        this._log("Error: " + e.description);
    }
    
    // Check to see if we're in replace mode
    this._checkforreplace(doc);
    }

function _XmlVersionSniffer_createdoc(version, progId)
{
    // Try to create the requested dom document.
    // If successful, then set the version.
    try {
        this._log("Creating: " + progId);
        
        var doc = new ActiveXObject(progId);
        this.version = version;
        this.versions[version.toString()] = true;
        
        return doc;
    }
    catch (e) {
        this._log("Error: " + e.description);
        return null;
    }
}

function _XmlVersionSniffer_checkfor30sp1(doc30)
{
    // There were a lot of bug fixes in version 3.0 sp1
    // so we want to see if that has been installed.
    // Check the KB article Q290988 which describes this
    // test.   
    try {
        this._log("Checking for version 3.0 sp1");
    
        doc30.async = false;
        doc30.loadXML(this._dummyxml);
        
        doc30.setProperty("SelectionLanguage", "XPath");
        doc30.setProperty("SelectionNamespaces", "xmlns:my='urn:http//www.my.com/schema/'");
        
        var nodelist = doc30.documentElement.selectNodes("child::my:book");

        var expected = "xmlns:my='urn:http//www.my.com/schema/'";
        var actual = nodelist.getProperty("SelectionNamespaces");
        
        if (actual == expected) {
            this.versions["3.0sp1"] = true;
        }
    }
    catch (e) {        
        this._log("Error: " + e.description);
    }

}

function _XmlVersionSniffer_checkforreplace(doc)
{
    // At this point, we know the version, but we need to find out
    // if we are in replace mode. That is, see if the old ProgID
    // Microsoft.XMLDOM is really one of the newer MSXML2+ references. 
    // Version 2.6+ introduced a new property called .namespaces, so
    // check to see if it exists in old doc. Since xmlinst did not
    // exist until version 3, we can safely assume that if this test
    // passes, then the user has version 3 in replace mode.

    try {
	    this._log("checking typeof doc.namespaces: " + typeof doc.namespaces);
	    if (typeof doc.namespaces != "undefined") {
	        this.replace = true;
	    }
    } catch(e) {
	    //alert("Problem running MSXML, likely cause: no ActiveX components allowed.");
    }
}

function CheckOSBrowser(){
	var version = 0;
	
	navInfo.browserVersion = 0;
	navInfo.browserCheck = false;
	navInfo.osName = "";
	navInfo.osVersion = 0;
	navInfo.osCheck = false;

	if (navigator.appName == "Microsoft Internet Explorer") {
		var i = navigator.appVersion.indexOf("MSIE");
		navInfo.browserVersion = parseFloat(navigator.appVersion.substr(i+5,5));
		if (isNaN(navInfo.browserVersion)){
			navInfo.browserVersion = 0;
		}
	}
	
	navInfo.browserName = navigator.appName + " " + navInfo.browserVersion;

	if (navInfo.browserVersion >= 5.5){
		navInfo.browserCheck = true;
	} else {
		navInfo.browserCheck = false;
	}
	
	if (navigator.appName == "Microsoft Internet Explorer") {
		var navArray = navigator.appVersion.split(";");
		for (var i = 0; i < navArray.length; i++){
			var tmp = Trim(navArray[i].toLowerCase());
			if (tmp.indexOf("windows") == 0){
				navInfo.osName = navArray[i];
				tmp = tmp.replace("windows nt", "");
				tmp = tmp.replace("windows", "");
				tmp = Trim(tmp);
				tmp = parseFloat(tmp);
				if (isNaN(tmp)){
					tmp = 0;
				}
				navInfo.osVersion = tmp;
				break;
			}
		}
	}
	if (navInfo.osVersion >= 4 && navInfo.osVersion < 10){
		// NT4, Win2K or WinXP
		navInfo.osCheck = true;
	}
	if (navInfo.osVersion >= 98){
		// Win98 or WinME
		navInfo.osCheck = true;
	}
	
}

