// Version 1.00 20051028
// Version 1.01 20051116 
// Version 1.20 20060307  - Code for handling creation of Flash object HTML code. 
// See ESDI Wiki page Javascript_Library_Of_Functions for Documentation of these functions 




function xl_GetDOMFeats( ) {
    if (document.images) {
        this.isCSS = (document.body && document.body.style) ? true : false;
        this.isW3C = (this.isCSS && document.getElementById) ? true : false;
        this.isIE4 = (this.isCSS && document.all) ? true : false;
        this.isNN4 = (document.layers) ? true : false;
        this.isIE6CSS = (document.compatMode && document.compatMode.indexOf("CSS1") >= 0) ? 
            true : false;
    }
}
/** Function:			xl_GetObj
 *  Purpose:   		Return a reference to an HTML element based on its id property (either as a string or a pointer) 
 *  Convert object name string or object reference into a valid element object reference
 *   @return  				ObjID 		Reference to an HTML element 
*/ 
function xl_GetObj(obj) 
{
    var ObjID;
    if (typeof obj == "string") {
        if (xl_DOM.isW3C) {
            ObjID = document.getElementById(obj);
        } else if (xl_DOM.isIE4) {
            ObjID = document.all(obj);
        } else if (xl_DOM.isNN4) {
            ObjID = xl_SeekLayer(document, obj);
        }
   	 } else {
        // pass through object reference
        ObjID = obj;
    }
		return ObjID; 
}
				
	
// Seek nested NN4 layer from string name
function xl_SeekLayer(doc, name) 
{
    var ObjID;
    for (var i = 0; i < doc.layers.length; i++) {
        if (doc.layers[i].name == name) {
            ObjID = doc.layers[i];
            break;
        }
        // dive into nested layers if necessary
        if (doc.layers[i].document.layers.length > 0) {
            ObjID = xl_SeekLayer(document.layers[i].document, name);
        }
    }
    return ObjID;
}

/** Function:			xl_GetEvent
 *  Purpose:   		Get Event - returns an Event object. To see its type, for example, use e.type
 *  @param        e - Event name
 *  Cross-browser notes:    FireFox passes variable e to the function, while IE doesn't - the event is window.event.
 *  Usage notes:  See Wiki 
*/  
function xl_GetEvent(e) 
{
  // e gives access to the event in all browsers
 
 if (!e) var e = window.event; 

 return e; 
} 

/** Function:			xl_GetEventTarg
 *  Purpose:   		Get object reference to target of an Event such as onClick 
 *  @return    EventObj = element upon which the event occurred. 
 *  Cross-browser notes:    FireFox passes variable e to the function, while IE doesn't - the event is window.event.
 *  Usage notes:  See Wiki  (includes info on how to register an event handler) 
*/  
function xl_GetEventTarg(e) 
{
  var EventObj; 
  var e = xl_GetEvent(e); 
  // e gives access to the event in all browsers
	
 if (e.target) EventObj  = e.target;
	else if (e.srcElement) EventObj = e.srcElement;

 return EventObj; 
} 

/** Function:			xl_AttachEvent
 *  Purpose:   		Attach a custom event handler to an object (such as document). Allows you to add multiple event handlers to objects.   
 *  @return       true -  it attached it ok.  false - it didn't, meaning browser doesn't support native methods used. 
 *  Cross-browser notes:    NS6 and Mozilla (FireFox) use addEventListener, IE uses attachEvent. 
 *                          in addition, Mozilla supports both event capturing and event bubbling while IE supports only event bubbling. 
 *  Usage notes:  See Wiki  
*/  
function xl_AttachEvent(obj, evType, fn, useCapture){
  if (!useCapture) var useCapture = true; 
  if (obj.addEventListener){
    obj.addEventListener(evType, fn, useCapture);
    return true;
  } else if (obj.attachEvent){
    var r = obj.attachEvent("on"+evType, fn);
    return r;
  } else {
    return false; 
  }
} 
/** Function:			xl_DetachEvent
 *  Purpose:   		Detach a custom event handler that was added with xl_AttachEvent  
 *  @return 		  true -  it attached it ok.  false - it didn't, meaning browser doesn't support native methods used. 
*/ 
function xl_DetachEvent(obj, evType, fn, useCapture){
  if (!useCapture) var useCapture = true; 
  if (obj.removeEventListener){
    obj.removeEventListener(evType, fn, useCapture);
    return true;
  } else if (obj.detachEvent){
    var r = obj.detachEvent("on"+evType, fn);
    return r;
  } else {
    return false;
  }
} 

/** Function:  	 xl_FindPosX 
 *  Purpose: 		 Find left position (x coordinate) of an object on a page. 
 *  @param			 Object reference (can be got with xl_GetObj) 
/*  @return			 curleft  left position of object
*/  
function xl_FindPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

/** Function:  	 xl_FindPosY 
 *  Purpose: 		 Find top position (y coordinate) of an object on a page. 
 *  @param			 Object reference (can be got with xl_GetObj) 
/*  @return			 curleft  left position of object
*/  
function xl_FindPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

/** Function: 	   xl_Encode 
 *  Purpose: 		 	 URL-encode a string to ensure compatibility with all character sets
 *  @param				 uri - raw string
 *  @return 			 uri - encoded string
*/
function xl_Encode( uri ) {
    if (encodeURIComponent) {
        return encodeURIComponent(uri);
    }

    if (escape) {
        return escape(uri);
    }
} 

/** Function: 	   xl_Decode 
 *  Purpose: 		 	 Decode a URL-encoded string to ensure compatibility with all character sets
 *  @param				 uri - raw string
 *  @return        uri - decoded string
*/
function xl_Decode( uri ) {
    uri = uri.replace(/\+/g, ' ');

    if (decodeURIComponent) {
        return decodeURIComponent(uri);
    }

    if (unescape) {
        return unescape(uri);
    }

    return uri;
}

/**
 * FlashObject v1.3c: Flash detection and embed - http://blog.deconcept.com/flashobject/
 *
 * FlashObject is (c) 2006 Geoff Stearns and is released under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 *
 * Modified by DJKenzie Feb 27, 2006 to customize it to handle Clover and FusionCharts
 */
if(typeof com == "undefined") var com = new Object();
if(typeof com.deconcept == "undefined") com.deconcept = new Object();
if(typeof com.deconcept.util == "undefined") com.deconcept.util = new Object();
if(typeof com.deconcept.FlashObjectUtil == "undefined") com.deconcept.FlashObjectUtil = new Object();
com.deconcept.FlashObject = function(swf, id, pgm, w, h, ver, c, useExpressInstall, quality, xiRedirectUrl, redirectUrl, detectKey){
  if (!document.createElement || !document.getElementById) return;
  this.DETECT_KEY = detectKey ? detectKey : 'detectflash';
  this.skipDetect = com.deconcept.util.getRequestParameter(this.DETECT_KEY);
  this.params = new Object();
  this.variables = new Object();
  this.attributes = new Array();
  this.useExpressInstall = useExpressInstall;

  if(pgm) {this.setAttribute("pgm", pgm); } 
  if(w) this.setAttribute('width', w);
  if(h) this.setAttribute('height', h);
  
  if (swf) { 
	this.swfurl = function()
	{
		var myswf = swf +  "?rnd="+ Math.round(100*Math.random());
		myswf += "&chartWidth=" + this.getAttribute("width") + "&chartHeight=" + this.getAttribute("height") +  "&dataUrl=" +this.getAttribute("pgm"); 
		var swfparms = "?task=RUN_REPORT"; 
		var query = window.location.search.substring(1);
		var qryparms  = query.split("&");
		for (var i=0;i< qryparms.length;i++)
		{
			var pair = qryparms[i].split("=");
			if (pair[0].toUpperCase() != "TASK")  
			{
				swfparms += "&" + pair[0] + "=" + pair[1]; 
			}
		}

		swfparms = xl_Encode(swfparms); 
		myswf += swfparms; 
		return myswf; 
	} 	
	this.setAttribute("swf", this.swfurl());  
  }
  
  if(id) this.setAttribute('id', id);
  
  
  if(ver) this.setAttribute('version', new com.deconcept.PlayerVersion(ver.toString().split(".")));
  this.installedVer = com.deconcept.FlashObjectUtil.getPlayerVersion(this.getAttribute('version'), useExpressInstall);
  if(c) this.addParam('bgcolor', c);
  var q = quality ? quality : 'high';
  this.addParam('quality', q);
  var xir = (xiRedirectUrl) ? xiRedirectUrl : window.location;
  this.setAttribute('xiRedirectUrl', xir);
  this.setAttribute('redirectUrl', '');
  if(redirectUrl) this.setAttribute('redirectUrl', redirectUrl);
}
com.deconcept.FlashObject.prototype = {
  setAttribute: function(name, value){
    this.attributes[name] = value;
  },
  getAttribute: function(name){
    return this.attributes[name];
  },
  addParam: function(name, value){
    this.params[name] = value;
  },
  getParams: function(){
    return this.params;
  },
  addVariable: function(name, value){
    this.variables[name] = value;
  },
  getVariable: function(name){
    return this.variables[name];
  },
  getVariables: function(){
    return this.variables;
  },
  createParamTag: function(n, v){
    var p = document.createElement('param');
    p.setAttribute('name', n);
    p.setAttribute('value', v);
    return p;
  },
  getVariablePairs: function(){
    var variablePairs = new Array();
    var key;
    var variables = this.getVariables();
    for(key in variables){
      variablePairs.push(key +"="+ variables[key]);
    }
    return variablePairs;
  },
  getFlashHTML: function() {
    var flashNode = "";
    if (navigator.plugins && navigator.mimeTypes && navigator.mimeTypes.length) { // netscape plugin architecture
      if (this.getAttribute("doExpressInstall")) this.addVariable("MMplayerType", "PlugIn");
      flashNode = '<embed type="application/x-shockwave-flash" src="'+ this.getAttribute('swf') +'" width="'+ this.getAttribute('width') +'" height="'+ this.getAttribute('height') +'"';
      flashNode += ' id="'+ this.getAttribute('id') +'" name="'+ this.getAttribute('id') +'" ';
      var params = this.getParams();
       for(var key in params){ flashNode += [key] +'="'+ params[key] +'" '; }
      var pairs = this.getVariablePairs().join("&");
       if (pairs.length > 0){ flashNode += 'flashvars="'+ pairs +'"'; }
      flashNode += '/>';
    } else { // PC IE
      if (this.getAttribute("doExpressInstall")) this.addVariable("MMplayerType", "ActiveX");
      flashNode = '<object id="'+ this.getAttribute('id') +'" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="'+ this.getAttribute('width') +'" height="'+ this.getAttribute('height') +'" viewastext>';
      flashNode += '<param name="movie" value="'+ this.getAttribute('swf') +'" />';
      var params = this.getParams();
       for(var key in params) {
        flashNode += '<param name="'+ key +'" value="'+ params[key] +'" />';
       }
      var pairs = this.getVariablePairs().join("&");
       if(pairs.length > 0) flashNode += '<param name="flashvars" value="'+ pairs +'" />';
      flashNode += "</object>";
    }
    return flashNode;
  },
  write: function(elementId){
    if(this.useExpressInstall) {
      // check to see if we need to do an express install
      var expressInstallReqVer = new com.deconcept.PlayerVersion([6,0,65]);
      if (this.installedVer.versionIsValid(expressInstallReqVer) && !this.installedVer.versionIsValid(this.getAttribute('version'))) {
        this.setAttribute('doExpressInstall', true);
        this.addVariable("MMredirectURL", escape(this.getAttribute('xiRedirectUrl')));
        document.title = document.title.slice(0, 47) + " - Flash Player Installation";
        this.addVariable("MMdoctitle", document.title);
      }
    } else {
      this.setAttribute('doExpressInstall', false);
    }
    if(this.skipDetect || this.getAttribute('doExpressInstall') || this.installedVer.versionIsValid(this.getAttribute('version'))){
      var n = (typeof elementId == 'string') ? document.getElementById(elementId) : elementId;
      n.innerHTML = this.getFlashHTML();
    }else{
      if(this.getAttribute('redirectUrl') != "") {
        document.location.replace(this.getAttribute('redirectUrl'));
      }
    }
  }
}

/* ---- detection functions ---- */
com.deconcept.FlashObjectUtil.getPlayerVersion = function(reqVer, xiInstall){
  var PlayerVersion = new com.deconcept.PlayerVersion(0,0,0);
  if(navigator.plugins && navigator.mimeTypes.length){
    var x = navigator.plugins["Shockwave Flash"];
    if(x && x.description) {
      PlayerVersion = new com.deconcept.PlayerVersion(x.description.replace(/([a-z]|[A-Z]|\s)+/, "").replace(/(\s+r|\s+b[0-9]+)/, ".").split("."));
    }
  }else{
    try{
      var axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
      for (var i=3; axo!=null; i++) {
        axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash."+i);
        PlayerVersion = new com.deconcept.PlayerVersion([i,0,0]);
      }
    }catch(e){}
    if (reqVer && PlayerVersion.major > reqVer.major) return PlayerVersion; // version is ok, skip minor detection
    // this only does the minor rev lookup if the user's major version 
    // is not 6 or we are checking for a specific minor or revision number
    // see http://blog.deconcept.com/2006/01/11/getvariable-setvariable-crash-internet-explorer-flash-6/
    if (!reqVer || ((reqVer.minor != 0 || reqVer.rev != 0) && PlayerVersion.major == reqVer.major) || PlayerVersion.major != 6 || xiInstall) {
      try{
        PlayerVersion = new com.deconcept.PlayerVersion(axo.GetVariable("$version").split(" ")[1].split(","));
      }catch(e){}
    }
  }
  return PlayerVersion;
}
com.deconcept.PlayerVersion = function(arrVersion){
  this.major = parseInt(arrVersion[0]) || 0;
  this.minor = parseInt(arrVersion[1]) || 0;
  this.rev = parseInt(arrVersion[2]) || 0;
}
com.deconcept.PlayerVersion.prototype.versionIsValid = function(fv){
  if(this.major < fv.major) return false;
  if(this.major > fv.major) return true;
  if(this.minor < fv.minor) return false;
  if(this.minor > fv.minor) return true;
  if(this.rev < fv.rev) return false;
  return true;
}
/* ---- get value of query string param ---- */
com.deconcept.util = {
  getRequestParameter: function(param){
    var q = document.location.search || document.location.href.hash;
    if(q){
      var startIndex = q.indexOf(param +"=");
      var endIndex = (q.indexOf("&", startIndex) > -1) ? q.indexOf("&", startIndex) : q.length;
      if (q.length > 1 && startIndex > -1) {
        return q.substring(q.indexOf("=", startIndex)+1, endIndex);
      }
    }
    return "";
  },
  removeChildren: function(n){
    while (n.hasChildNodes()) n.removeChild(n.firstChild);
  }
}

/* add Array.push if needed (ie5) */
if (Array.prototype.push == null) { Array.prototype.push = function(item) { this[this.length] = item; return this.length; }}

/* add some aliases for ease of use/backwards compatibility */
var getQueryParamValue = com.deconcept.util.getRequestParameter;
var FlashObject = com.deconcept.FlashObject;



