// PluginDetect script generator v0.3.2


// The select object holds the value of all the user selected checkboxes
var select={
    // Uppercase names are plugins
    JAVA:null, NOTF:null, QUICKTIME:null, DEVAL:null,
    SHOCKWAVE:null, FLASH:null, WINMEDIA:null, SILVERLIGHT:null, VLC:null,
    ADOBEREADER:null,

    // Lowercase names are methods
    ondetectdone:null, beforeinstance:null, getversion:null, winload:null,
    isminversion:null, getinfo:null, removelinebr:null, addalias:null};

var DIV = {}; // plugins that require the plugindetect <div>
              // Note: we add QuickTime to DIV.pluginNames only, because it instantiates for IE,
              // but does not actually use the plugindetect <div>
    DIV.plugins = ['JAVA', 'WINMEDIA', 'DEVAL', 'ADOBEREADER'];  // select[DIV.plugins[0]] etc...
    DIV.pluginNames = ['Java', 'Windows Media', 'DevalVR', 'QuickTime', 'Adobe Reader'];


var PDname = "PluginDetect";   // name of script

// ---------------------------------------------------------------------------------------------



var getObj = function(s){return document.getElementById(s)};

window.onload = function(){
   
    getObj('pdver').innerHTML = "v" + PluginDetect.version;
    getObj('divPlugins').innerHTML = DIV.pluginNames.join(", ");
    initCheckBoxes();

};

window.onunload = function(){
    initCheckBoxes();
};


// Array.prototype.push = function()
// IE5 does not have the .push command, so we add it.
if (typeof Array.prototype.push == 'undefined'){
  Array.prototype.push = function(a){this[this.length] = a; return this.length}
};

// Fill select object with values from the checkboxes
function checkBoxes(){

    var x, obj;
    for (x in select){    // ennumerate the select object properties
       obj = getObj(x);
       select[x] = obj ? obj.checked : null;
    };

};

function initCheckBoxes(){

    var x, obj;
    for (x in select){    // ennumerate the select object properties
       obj = getObj(x);
       if (obj) obj.checked = false;
    };

    getObj('getversion').checked = true;
    getObj('isminversion').checked = true;
    getObj('removelinebr').checked = true;

    getObj('NOTF_').style.display = "none";
    getObj('NOTF_').style.visibility = "hidden";
    
    getObj('getinfo_').style.display = "none";
    getObj('getinfo_').style.visibility = "hidden";

    getObj('beforeinstance_').style.display = "none";
    getObj('beforeinstance_').style.visibility = "hidden";

    getObj('sizeOut').value = "";
    getObj('codeOut').value = "";
    
    getObj('useralias_').style.display = select.addalias ? "block" : "none";
    getObj('useralias_').style.visibility = select.addalias ? "visible" : "hidden";
    
    getObj('useralias').value = "";


};

// Run this when a checkbox has been clicked
function CLICK(obj){

  checkBoxes();
  
  var PDdiv = div();   // Is PluginDetect div needed?
  var Instantiate = PDdiv || select.QUICKTIME; // Is any plugin instantiated?
                                               // We need to handle QuickTime separately, because
                                               // it does not use PluginDetect div to instantiate,
                                               // instead it uses codebase method (for IE).


  // OnWindowLoaded box was clicked
  if (obj===getObj('winload')){

    if (!select.winload && PDdiv)
        alert('onWindowLoaded() must be selected for the plugin(s) you have chosen');

  };
  if (PDdiv){   // if plugindetect <div> is being used, then we need winload as well.
                // When window has loaded, we empty the <div>
     getObj('winload').checked = true;
     select.winload = true;
  };


  // BeforeInstance() box
  // If plugindetect div is being used, then we may use BeforeInstance()
  getObj('beforeinstance_').style.display = Instantiate ? "block" : "none";
  getObj('beforeinstance_').style.visibility = Instantiate ? "visible" : "hidden";
  if (!Instantiate){
     getObj('beforeinstance').checked = false;
     select.beforeinstance = false;
  };


  // getInfo() box
  getObj('getinfo_').style.display = select.JAVA ? "block" : "none";
  getObj('getinfo_').style.visibility = select.JAVA ? "visible" : "hidden";
  if (!select.JAVA){
     getObj('getinfo').checked = false;
     select.getinfo = false;
  };

  // NOTF box
  getObj('NOTF_').style.display = select.JAVA ? "block" : "none";
  getObj('NOTF_').style.visibility = select.JAVA ? "visible" : "hidden";
  if (obj===getObj('JAVA')){ 
     getObj('NOTF').checked = select.JAVA;
     select.NOTF = select.JAVA;
  };

  // OnDetectionDone box
  if ((obj===getObj('JAVA') && select.JAVA) || (obj===getObj('NOTF') && select.NOTF)){
     getObj('ondetectdone').checked = true;
     select.ondetectdone = true;
  };
  if (obj===getObj('ondetectdone') && !select.ondetectdone && select.NOTF){
     alert('onDetectionDone() must be selected when NOTF is being used');
     getObj('ondetectdone').checked = true;
     select.ondetectdone = true;
  };
  
  
  // Alias text box
  if (obj===getObj('addalias')){
       getObj('useralias_').style.display = select.addalias ? "block" : "none";
       getObj('useralias_').style.visibility = select.addalias ? "visible" : "hidden";
       if (select.addalias && getObj('useralias').focus) getObj('useralias').focus();
  };


};   // end of CLICK()


// Determine whether the plugindetect div will be used or not.
// This div is used to instantiate plugins.
// Certain plugins must be instantiated for plugin detection to occur.
function div(){

    var x;
    for (x=0;x<DIV.plugins.length;x++){
        if (select[DIV.plugins[x]]) return true;
    };
    return false;

};


// Given PluginDetect script and an alias, we insert the alias into the script.
//
// For some reason, when alias="$$" and we use replace(regex, ..alias..)
// then the result is that one of the "$" characters goes missing in the output.
//
// So we try an alternative method, which hopefully is more reliable.
function insertAlias(PDscript, alias){

    var Len = PDscript.length;
     
    // Find and replace the first instance of "PluginDetect={" with "alias=PluginDetect={"
    var r1 = new RegExp("([^a-zA-z\\d\\$\\_])" + "(" + PDname + "\\s*=\\s*\\{" + ")").exec(PDscript);
    if (r1) PDscript = RegExp.leftContext + RegExp.$1 + alias + "=" + RegExp.$2 + RegExp.rightContext;


    // Find and replace all instances of "PluginDetect."  with  "alias."
    if (PDscript.length>Len && alias.length<PDname.length){
        var r2 = new RegExp("([^a-zA-z\\d\\$\\_])" + "(" + PDname + "\\." + ")", "g");
        while(r2.exec(PDscript)){
              PDscript = RegExp.leftContext + RegExp.$1 + alias + "." + RegExp.rightContext;
              r2.lastIndex = 0; // start the regex search at beginning of string again
        }

    };

  return PDscript;

};


// check to make sure the alias is valid
// Return true/false
function isAliasValid(alias){

 var valid= /^[\$_a-zA-Z][\$_a-zA-Z\d]*$/;

 if (!valid.test(alias)) return false;

 // test for javascript keywords, and if found then return false
 var keys = [PDname, 'var', 'for', 'this', 'if', 'else', 'function', 'return',
   'true', 'false', 'null', 'typeof', 'window', 'document', 'parseInt', 'ActiveXObject',
   'onload', 'CollectGarbage', 'java', 'try', 'catch'], x;

 for (x=0;x<keys.length;x++){
     if ( new RegExp("^"+keys[x]+"$").test(alias) ) return false;
 };


 return true;

};


// Get rid of spaces from alias
function extractAlias(alias){

  var result = typeof alias == 'string' ? alias.replace(/\s/g, ""): "";

  return result;

};


function verifyAlias(){

  var UA = getObj("useralias");

  var alias = extractAlias(UA.value);
  
  UA.value = alias;

  if (!isAliasValid(alias)){
          alert("Error: Invalid alias")
  };

};



// Create the customized output script based on the checkboxes that are selected
function makeScript(){

    var A = getObj('SCR').innerHTML;  // the PluginDetect script

    // Strip away the <script> tags
    A = A.replace(/^[^f]*funct/,'funct').replace(/\/\/\-\->[^\-]*$/,'');


    // Get rid of function BOUNDARY(){}
    A = A.replace(/function\s+BOUNDARY\s*[(][^)]*[)]\s*[{][^}]*[}]/gi, "");
    

    // Must have at least getversion or isminversion or both.
    // We set this before checkBoxes()
    if (!getObj('getversion').checked && !getObj('isminversion').checked)
           getObj('isminversion').checked = true;
    checkBoxes();


    // Add alias to script if needed
    if (select.addalias){
       var UA = getObj("useralias");
       var alias = extractAlias(UA.value);
       UA.value = alias;

       if (alias!=""){
         if (isAliasValid(alias)){ A = insertAlias(A, alias) }    // insert alias into script
         else{alert("Error: Invalid alias\nAlias will be ignored")};
       }
    };



    // Split the PluginDetect script into pieces
    // We split based on "BOUNDARY()" or "BOUNDARY:0,"
    var B = A.split(/BOUNDARY\s*[(][^)]*[)]|BOUNDARY\s*:\s*0\s*,/);


    // SCRIPT array will contains the sections of the output script
    var index=0, SCRIPT = [];




// --------------------------------------------------------------------
// Select the individual pieces that will go into the output script


SCRIPT.push(B[index]);
index++;


if (select.ADOBEREADER ||                        // (PFV0)     $.getPluginFileVersion()
    select.FLASH || 
    select.SHOCKWAVE) SCRIPT.push(B[index]);
index++;
SCRIPT.push(B[index]);                           // (PFV1)
index++;


if (select.winload) SCRIPT.push(B[index]);       // (WIN4)
index++;
SCRIPT.push(B[index]);                           // (WIN5)
index++;



if (select.ondetectdone || select.beforeinstance ||
      select.winload) SCRIPT.push(B[index]);        // (EH00)     Event handler stuff
index++;


if (select.isminversion) SCRIPT.push(B[index]);     // (MIN0)     isMinVersion() method
index++;


if (select.getversion) SCRIPT.push(B[index]);       //  (VER0)    getVersion() method
index++;


if (select.getinfo) SCRIPT.push(B[index]);          // (GETIN0)   getInfo() method
index++;
SCRIPT.push(B[index]);
index++;



if (select.JAVA || select.QUICKTIME) SCRIPT.push(B[index]);   // cleanup() method
index++;
SCRIPT.push(B[index]);
index++;


if (select.QUICKTIME) SCRIPT.push(B[index]);    // isActiveXObject(), codebaseSearch() method (ie. IE codebase detection)
index++;

if (select.winload) SCRIPT.push(B[index]);      // (WIN0)    onWindowLoaded()
index++;


if (select.beforeinstance) SCRIPT.push(B[index]);        //  (BI0) beforeInstantiate()
index++;


if (select.ondetectdone) SCRIPT.push(B[index]);                               //  (OJD0)  onDetectionDone() method
index++;
if (select.ondetectdone && select.JAVA && select.NOTF) SCRIPT.push(B[index])  //  (OJD1)     NOTF detection
index++;
if (select.ondetectdone) SCRIPT.push(B[index]);                               //  (OJD2)
index++;


if (div()) SCRIPT.push(B[index]);                  // (IP01)    instantiate() method
index++;
if (div() && select.beforeinstance)
     SCRIPT.push(B[index]);                        // (IP02)
index++;
if (div()) SCRIPT.push(B[index]);                  // (IP03)
index++; 



if (select.QUICKTIME) SCRIPT.push(B[index]);           //  (QT1)  Quicktime detection code
index++;



if (select.JAVA) SCRIPT.push(B[index]);                //  (JV1)  Java detection code begins here
index++;


if (select.JAVA && select.getinfo) SCRIPT.push(B[index]);     // (GI1)  getInfo() for Java - find multiple JREs for IE
index++;
if (select.JAVA) SCRIPT.push(B[index]);
index++;

if (select.JAVA && select.getinfo) SCRIPT.push(B[index]);     // (GI2) getInfo() for Java - find multiple JREs for IE
index++;
if (select.JAVA) SCRIPT.push(B[index]);
index++;


if (select.JAVA && select.getinfo) SCRIPT.push(B[index]);     // (GI3)  getInfo() / Plugin2
index++;
if (select.JAVA) SCRIPT.push(B[index]);
index++;


if (select.JAVA && select.getinfo) SCRIPT.push(B[index]);     // (GI4)  getInfo() / Plugin2
index++;
if (select.JAVA) SCRIPT.push(B[index]);
index++;


if (select.JAVA && select.getinfo) SCRIPT.push(B[index]);     // (GI5)  getInfo() / Plugin2
index++;
if (select.JAVA) SCRIPT.push(B[index]);
index++;


if (select.JAVA && select.NOTF) SCRIPT.push(B[index]);         //  (JV2)    NOTF detection
index++;
if (select.JAVA) SCRIPT.push(B[index]);                        //  (JV3)
index++;


if (select.JAVA && select.getinfo) SCRIPT.push(B[index]);     // (GI6)  getInfo() / Plugin2
index++;
if (select.JAVA) SCRIPT.push(B[index]);
index++;


if (select.JAVA && select.NOTF) SCRIPT.push(B[index]);         //  (JV6)    NOTF detection
index++;
if (select.JAVA) SCRIPT.push(B[index]);                        //  (JV15)
index++;


if (select.JAVA && select.getinfo) SCRIPT.push(B[index]);     // (JV16)  java.getInfo()
index++;
if (select.JAVA) SCRIPT.push(B[index]);                       // (JV17)
index++;



if (select.DEVAL) SCRIPT.push(B[index]);        // DevalVR Detection code
index++;


if (select.FLASH) SCRIPT.push(B[index]);                      // Flash detection code
index++;


if (select.SHOCKWAVE) SCRIPT.push(B[index]);                  // Shockwave detection code
index++;


if (select.WINMEDIA) SCRIPT.push(B[index]);                   // Windows Media detection code
index++;


if (select.SILVERLIGHT) SCRIPT.push(B[index]);                // Silverlight detection code
index++;


if (select.VLC) SCRIPT.push(B[index]);                        // VLC detection code
index++;


if (select.ADOBEREADER) SCRIPT.push(B[index]);                // ADOBEREADER detection code
index++;


SCRIPT.push(B[index]);                                 // Initialize PluginDetect script
index++;


// --------------------------------------------------------------------------------------------

    var C = SCRIPT.join("");

    // Remove linebreaks $OD and $0A from the script if present
    if (select.removelinebr) C = C.replace(/[\n\r]/gi,'');


    // replace ';}' with '}'
    // replace ';;' with ';'
    C = C.replace(/;[}]/gi,"}").replace(/;;/gi,";").replace(/[{];/gi,"{");


    var Plugins = 
       (select.winload ? 'onWindowLoaded ' : '') +
       (select.isminversion ? 'isMinVersion ' : '') +
       (select.getversion ? 'getVersion ' : '') +
       (select.ondetectdone ? 'onDetectionDone ' : '') +
       (select.beforeinstance ? 'beforeInstantiate ' : '') +
           (select.JAVA ?
           ('Java(OTF' + (select.NOTF ? '&NOTF': '') +
           (select.getinfo ? '&getInfo' : '') + ') ') : '') +
       (select.QUICKTIME ? 'QT ':'') + (select.DEVAL ? 'DevalVR ':'') +
       (select.SHOCKWAVE ? 'Shockwave ':'') +
       (select.FLASH ? 'Flash ':'') +
       (select.WINMEDIA ? 'WMP ':'') +
       (select.SILVERLIGHT ? 'Silverlight ':'') +
       (select.VLC ? 'VLC ':'') + 
       (select.ADOBEREADER ? 'AdobeReader ':'');
    var Plugins0 = (Plugins?'[ ':'') + Plugins + (Plugins?'] ':'');


    C = '/* PluginDetect v' + PluginDetect.version +
        ' by Eric Gerds www.pinlady.net/PluginDetect ' +
         Plugins0 + '*/' + C;

       
    // We can remove the PluginDetect={version:"...", property here, since it is not really
    // needed.
    var reg = /\=\s*\{\s*(version\s*:[^,]*,)/;
    if (reg.test(C)){                  // look for FIRST instance of   ={version:"...",
        C = C.replace(RegExp.$1,"");
    };

    getObj('sizeOut').value = C.length;
    getObj('codeOut').value = C;


}; // end of makeScript() function

