var detectableWithVB = false;
var pluginFound = false;
var haveReal = false;
var haveQT = false;
var haveFlash = false;			// boolean. does the user have the flash at all?
var haveFlashVer = false;		// boolean. does the user have the correct version of flash? This isn't always required.
var flash2Installed = false;    // boolean. true if flash 2 is installed
var flash3Installed = false;    // boolean. true if flash 3 is installed
var flash4Installed = false;    // boolean. true if flash 4 is installed
var flash5Installed = false;    // boolean. true if flash 5 is installed
var flash6Installed = false;    // boolean. true if flash 6 is installed
var flash7Installed = false;    // boolean. true if flash 7 is installed
var maxVersion = 7;             // highest version we can actually detect
var actualVersion = 0;          // version the user really has
var requiredVersion = 7;		// base version flash that we're requiring
var pName = '';

function detectAndSet() {
    cookiesOfforOn();
    detectQuickTime();
    detectReal();
    setIncomingFormat();
}

function setIncomingFormat() {
    if (cookiesOn) {
        vTemp = getCookie("ckVidFormat");
        if ((!haveReal && !haveQT) && (vTemp == '' || vTemp == null || vTemp == 'null')) {
            //alert('No plugins at all - drop cookie for later redirect to plugins page');
            SetCookie('ckVidFormat', 'qtrv_not', null, '/');
            vTemp = getCookie("ckVidFormat");
            vFormat = vTemp;
																															//alert('The cookie is dropped as: ' + vFormat);
        } else if ((haveReal && !haveQT) && (vTemp == '' || vTemp == null || vTemp == 'null')) {
            //alert('Just have Real - drop cookie to Real low');
            SetCookie('ckVidFormat', 'rsmv_nqt', null, '/');
            vTemp = getCookie("ckVidFormat");
            vFormat = vTemp;
																																																						//alert('The cookie is dropped as: ' + vFormat);
        } else if ((!haveReal && haveQT) && (vTemp == '' || vTemp == null || vTemp == 'null')) {
            //alert('Just have QT - drop cookie to QT low');
            SetCookie('ckVidFormat', 'qsmv_nrp', null, '/');
            vTemp = getCookie("ckVidFormat");
            vFormat = vTemp;
																																																																													//alert('The cookie is dropped as: ' + vFormat);
        } else if ((haveReal && haveQT) && (vTemp == '' || vTemp == null || vTemp == 'null')) {
            //alert('Have both plugins - drop cookie to QT low');
            SetCookie('ckVidFormat', 'qsmv_qtr', null, '/');
            vTemp = getCookie("ckVidFormat");
            vFormat = vTemp;
																																																																																																				//alert('The cookie is dropped as: ' + vFormat);
        } else if (vTemp != '' || vTemp != null || vTemp != 'null') {
            //alert('Sniff does not need to happen again');
            vFormat = vTemp;
																																																																																																																			//alert('Cookie has already been set as ' + vFormat +' do not set again');
        }
    } else {
        if (!haveReal && !haveQT) {
            //alert('No plugins at all and Cookies not enabled');
            vTemp = 'qtrv_not';
        } else if (haveReal && !haveQT) {
            //alert('Just have Real and Cookies not enabled');
            vTemp = 'rsmv_nqt';
        } else if (!haveReal && haveQT) {
            //alert('Just have QT and Cookies not enabled');
            vTemp = 'qsmv_nrp';
        } else if (haveReal && haveQT) {
            //alert('Have both plugins and Cookies not enabled');
            vTemp = 'qsmv_qtr';
        } else if (vFormat != '') {
            //alert('Video format is already set and Cookies not enabled');
        }
        vFormat = vTemp;
																																																																																																																																																																//alert('No cookies but global vFormat is changed to: ' + vFormat);
    }
}

function pluginCheck(pluginFound, pName) {
    if (!pluginFound && (pName.indexOf('QuickTime') != -1)) {
        haveQT = false;
        return pluginFound;
    } else if (!pluginFound && (pName.indexOf('Real') != -1)) {
        haveReal = false;
        return pluginFound;
    } else if (pluginFound && (pName.indexOf('QuickTime') != -1)) {
        haveQT = true;
        return pluginFound;
    } else if (pluginFound && (pName.indexOf('Real') != -1)) {
        haveReal = true;
        return pluginFound;
    } else if (!pluginFound && (pName.indexOf('Flash') != -1)) {
        haveFlash = false;
        return pluginFound;
    } else if (pluginFound && (pName.indexOf('Flash') != -1)) {
        haveFlash = true;
        return pluginFound;
    }
}

function detectQuickTime() {
    pluginFound = detectPlugin('QuickTime');
																																																																																																																																																																																								        // if not found, try to detect with VisualBasic
    if (!pluginFound && detectableWithVB) {
        pluginFound = detectQuickTimeActiveXControl();
    }
    return pluginCheck(pluginFound, pName);
}

function detectReal() {
    pluginFound = detectPlugin('RealPlayer');
																																																																																																																																																																																											        // if not found, try to detect with VisualBasic
    if (!pluginFound && detectableWithVB) {
        pluginFound = (detectActiveXControl('rmocx.RealPlayer G2 Control') || detectActiveXControl('RealPlayer.RealPlayer(tm) ActiveX Control (32-bit)') || detectActiveXControl('RealVideo.RealVideo(tm) ActiveX Control (32-bit)'));
    }
    return pluginCheck(pluginFound, pName);
}

function detectPlugin(plugName) {
    pName = plugName;
																																																																																																																																																																																														        // allow for multiple checks in a single pass
    var daPlugins = detectPlugin.arguments;
																																																																																																																																																																																															        // consider pluginFound to be false until proven true
    var pluginFound = false;
																																																																																																																																																																																																        // if plugins array is there and not fake
    if (navigator.plugins && navigator.plugins.length > 0) {
        var pluginsArrayLength = navigator.plugins.length;
																																																																																																																																																																																																			// for each plugin...
        for (pluginsArrayCounter = 0; pluginsArrayCounter < pluginsArrayLength; pluginsArrayCounter++) {
            // loop through all desired names and check each against the current plugin name
            var numFound = 0;
            for (namesCounter = 0; namesCounter < daPlugins.length; namesCounter++) {
                // if desired plugin name is found in either plugin name or description
                if ((navigator.plugins[pluginsArrayCounter].name.indexOf(daPlugins[namesCounter]) >= 0) ||
                    (navigator.plugins[pluginsArrayCounter].description.indexOf(daPlugins[namesCounter]) >= 0)) {
                    // this name was found
                    numFound++;
                }
            }
																																																																																																																																																																																																																				    	    // now that we have checked all the required names against this one plugin,
            // if the number we found matches the total number provided then we were successful
            if (numFound == daPlugins.length) {
                pluginFound = true;
																																																																																																																																																																																																																											// if we've found the plugin, we can stop looking through at the rest of the plugins
                break;
            }
        }
    }
    return pluginFound;
}

																																																																																																																																																																																																																																// Write vbscript detection on ie win. IE on Windows doesn't support regular
// JavaScript plugins array detection.
var isIE = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;    // true if we're on ie
var isWin = (navigator.appVersion.indexOf("Windows") != -1) ? true : false; // true if we're on windows
var VBcode = '';
if (isIE && isWin) {
    VBcode += '<SCR' + 'IPT LANGUAGE=VBScript\> \n';
    VBcode += '\'do a one-time test for a version of VBScript that can handle this code\n';
    VBcode += 'on error resume next\n';
    VBcode += 'detectableWithVB = True\n';

    VBcode += '\'and the following function handles QuickTime\n';
    VBcode += 'Function detectQuickTimeActiveXControl()\n';
    VBcode += '  on error resume next\n';
    VBcode += '  pName = "QuickTime"\n';
    VBcode += '  detectQuickTimeActiveXControl = False\n';
    VBcode += '  If detectableWithVB Then\n';
    VBcode += '    detectQuickTimeActiveXControl = False\n';
    VBcode += '    hasQuickTimeChecker = false\n';
    VBcode += '    Set hasQuickTimeChecker = CreateObject("QuickTimeCheckObject.QuickTimeCheck.1")\n';
    VBcode += '    If IsObject(hasQuickTimeChecker) Then\n';
    VBcode += '      If hasQuickTimeChecker.IsQuickTimeAvailable(0) Then \n';
    VBcode += '        detectQuickTimeActiveXControl = True\n';
    VBcode += '      End If\n';
    VBcode += '    End If\n';
    VBcode += '  End If\n';
    VBcode += 'End Function\n';

    VBcode += '\'this next function will detect most plugins\n';
    VBcode += 'Function detectActiveXControl(activeXControlName)\n';
    VBcode += '  on error resume next\n';
    VBcode += '  pName = activeXControlName\n';
    VBcode += '  detectActiveXControl = False\n';
    VBcode += '  If detectableWithVB Then\n';
    VBcode += '     detectActiveXControl = IsObject(CreateObject(activeXControlName))\n';
    VBcode += '  End If\n';
    VBcode += 'End Function\n';

    VBcode += '\'this next function will detect Flash\n';
    VBcode += 'Function detectFlashActiveXControl() \n';
    VBcode += '  on error resume next\n';
    VBcode += '	flash2Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.2"))) \n';
    VBcode += '	if (flash2Installed) then flashVersion=2 \n';
    VBcode += '	flash3Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.3"))) \n';
    VBcode += '	if (flash3Installed) then flashVersion=3 \n';
    VBcode += '	flash4Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.4"))) \n';
    VBcode += '	if (flash4Installed) then flashVersion=4 \n';
    VBcode += '	flash5Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.5"))) \n';
    VBcode += '	if (flash5Installed) then flashVersion=5 \n';
    VBcode += '	flash6Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.6"))) \n';
    VBcode += '	if (flash6Installed) then flashVersion=6 \n';
    VBcode += '	flash7Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.7"))) \n';
    VBcode += '	if (flash7Installed) then flashVersion=7 \n';
    VBcode += 'End Function\n';
    VBcode += '</SCR' + 'IPT\> \n';
    document.writeln(VBcode);
}

function detectFlash(fVer) {
    if (fVer != null) {
        var requiredVersion = fVer;// Version the user needs to view site (max 7, min 2)
    } else {
        var requiredVersion = 4;
    }
																																																																																																																																																																																																																																														   // If navigator.plugins exists...

    if (navigator.plugins) {
        if (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]) {
            haveFlash = true;
            var isVersion2 = navigator.plugins["Shockwave Flash 2.0"] ? " 2.0" : "";
            var flashDescription = navigator.plugins["Shockwave Flash" + isVersion2].description;
																																																																																																																																																																																																																																																		     	//alert("Flash plugin description: " + flashDescription);
            //alert("Flash plugin description: " + flashDescription);
            var startIndex = flashDescription.indexOf("Flash ") + 6;
            var endIndex = flashDescription.indexOf(".", startIndex);
            var flashVersion = parseInt(flashDescription.substring(startIndex, endIndex));
            //alert("flashVersion: " + flashVersion);
            flash2Installed = flashVersion == 2;
            flash3Installed = flashVersion == 3;
            flash4Installed = flashVersion == 4;
            flash5Installed = flashVersion == 5;
            flash6Installed = flashVersion == 6;
            flash7Installed = flashVersion >= 7;
        } else if (detectableWithVB) {
            //flash2Installed = false;
            detectFlashActiveXControl();
            if (flash7Installed) haveFlash = true;
        }
    }

																																																																																																																																																																																																																																																																									   	
																																																																																																																																																																																																																																																																											// loop through all versions we're checking, and set actualVersion to highest detected version
    for (var i = 2; i <= maxVersion; i++) {
        if (eval("flash" + i + "Installed") == true)
            actualVersion = i;
    }
																																																																																																																																																																																																																																																																																			// if we're on webtv, the version supported is 2 (pre-summer2000, or 3, post-summer2000)
    // note that we don't bother sniffing varieties of webtv. you could if you were sadistic...
    if (navigator.userAgent.indexOf("WebTV") != -1) actualVersion = 2;
																																																																																																																																																																																																																																																																																						
																																																																																																																																																																																																																																																																																							// uncomment next line to display flash version during testing
    //alert("version detected: " + actualVersion);
    //alert("Required Version: " + requiredVersion);
    // we're finished getting the version. time to take the appropriate action
    if (actualVersion >= requiredVersion) {
        haveFlashVer = true;
    } else {
        haveFlashVer = false;
    }
}
