//************************************************************************
//FileName:	flash.js
//Description:	Provides functionality to display Flash animations
//              in a popup window and pass parameters to it.
//
// Version:	1.03 - 07/03/2007
//
// Revision:	
//		1.03 - Added extra paramters for licensing.
//			   Set deuyalt values for fault and language settings.
//			   (07/03/2007) DLP.
//		1.02 - Fix for sending a caption override to Flash.
//			   (09/10/2006) DLP.
// 		1.01 - Added check for a ClassCampus cookie before drawing
//			   Flash component on the screen. (27/09/2006) DLP.
// 		1.00 - First Version. (12/09/2006) DLP.
//
//************************************************************************

var sLangData = "";
var bShowVerInfo = false;

var sWindowTitle =""; // The text to be displayed in the window title taken 
					  // from the SWF File


var sFLASHFILE = "Simulator.swf"      //Default: Shockwave file
var sFLASHEXT = ".swf"                //Default: Shockwave file extension
var sDEFPARAM = "0"                   //Default: Fault Parameter value
var sDEFRCLICK = "0"                  //Default: Right Click SWF property
var lDEFFRAMEWIDTH = 5                //Default: Width of the frame round _
                                      //                     the flash control
var lDEFFLASHWIDTH = 800              //Default: Width of the flash control
var lDEFFLASHHEIGHT = 600             //Default: Height of the flash control
var lMINFRMWIDTH = 4700               //Minimum width Form can be resized to
var lMINFRMHEIGHT = 3150              //Minimum height Form can be resized to
var sDEFEXENAME = "SWPresent"         //The default name for the EXE file
 


//Flash Parameters:                    //Parameters used to communicate with the Flash component
var PARAM_FAULTNUM = "FaultNumber";    //SEND: Fault number to the flash file
var PARAM_LANGNUM = "LangSet";         //SEND: Language setting to the flash file
var PARAM_WINTITLE = "WindowTitle";    //READ: The window title text
var PARAM_RANGE = "ProductRange";      //READ: Optional Additional window text _
                                       //     displayed before the Title
var PARAM_DEFHEIGHT = "DefaultHeight"; //READ: Start up height of the window
var PARAM_DEFWIDTH = "DefaultWidth";   //READ: Start up width of the window
var PARAM_FRAME = "BackColor";         //READ: frame background colour
var PARAM_FRAMEWIDTH = "FrameWidth";   //READ: frame width round the flash control
var PARAM_FULLVIEW = "ShowFullScrn";   //READ: Displays window as Full screen _
                                       //      when first loaded
 
//Command Line Parameters:             //Parameters passed on the Command Line
var CMDLN_FAULT = "/F";                //Fault Code Parameter
var CMDLN_WINTITLE = '/T"';            //Replacement Window Title Parameter
var CMDLN_FNAME = '/S"';               //Replacement Flash filename
var CMDLN_VINFO = "/V";                //Version info Flag
var CMDLN_LANG = "/L";                 //Language Parameter
var CMDLN_XTRA = "/X";                 //Extra Unexpected Parameter flag


function IsNS6orGreater()
//Return whether the browser is Netscape 6 or greater
{
var NS6gt = false;

//Check to see if using Netscape 6 or greater
if (navigator.appName == 'Netscape')
{
	if(parseFloat(navigator.appVersion) >= 5)
		NS6gt = true;
}

return NS6gt;
}

function leftTrim(str)
// PURPOSE: Remove leading blanks from our string.
// IN: str - the string we want to leftTrim
// RETVAL: An leftTrimmed string!

{
  var whitespace = new String(" \t\n\r");
  var s = new String(str);

  if (whitespace.indexOf(s.charAt(0)) != -1) 
  {
    // We have a string with leading blank(s)...

    var j=0, i = s.length;

    // Iterate from the far left of string until we
    // don't have any more whitespace...
    while (j < i && whitespace.indexOf(s.charAt(j)) != -1) j++;


    // Get the substring from the first non-whitespace
    // character to the end of the string...
    s = s.substring(j, i);
  }

  return s;
}


function rightTrim(str)
// PURPOSE: Remove trailing blanks from our string.
// IN: str - the string we want to rightTrim
// RETVAL: An rightTrimmed string!

{
  // We don't want to trip JUST spaces, but also tabs,
  // line feeds, etc.  Add anything else you want to
  // "trim" here in Whitespace
  var whitespace = new String(" \t\n\r");

  var s = new String(str);

  if (whitespace.indexOf(s.charAt(s.length-1)) != -1) 
  {
    // We have a string with trailing blank(s)...
    var i = s.length - 1;       // Get length of string

    // Iterate from the far right of string until we
    // don't have any more whitespace...
    while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
      i--;

    // Get the substring from the front of the string to
    // where the last non-whitespace character is...
    s = s.substring(0, i+1);
  }

  return s;
}



function trim(str)
// PURPOSE: Remove trailing and leading blanks from our string.
// IN: str - the string we want to trim
// RETVAL: A trimmed string!
{
  return rightTrim(leftTrim(str));
}


function inStr(iStart, strSearch, charSearchFor)
// Returns the first location a substring (SearchForStr)
// was found in the string str.  (If the character is not
// found, -1 is returned.)
                          
{
	
	for (i=iStart; i < String(strSearch).length+1; i++)
	{
	    if (charSearchFor == mid(strSearch, i, String(charSearchFor).length))
	    {
			return i;
	    }
	}
	return 0;
}


function mid(str, start, len)
// IN: str - the string we are LEFTing
//     start - our string's starting position (0 based!!)
//     len - how many characters from start we want to get
//
// returns the substring from start to start+len

{
 
  var sRetVal = "";
   
  if (!len || len==0) len=String(str).length;
  
  // Make sure start and len are within proper bounds
  if (start < 0 || len < 0) return "";

  var iEnd;
  var iLen = String(str).length;
  
  if (start + len > iLen)
    iEnd = iLen;
  else
    iEnd = start + len-1;
   
  sRetVal = String(str).substring(start-1,iEnd);
  return sRetVal;
}

function IsNumeric(sText)
{
  var ValidChars = "0123456789.";
  var IsNumber=true;
  var Char;
 
  for (i = 0; i < sText.length && IsNumber == true; i++) 
  { 
    Char = sText.charAt(i); 
    if (ValidChars.indexOf(Char) == -1) 
    {
      IsNumber = false;
    }
  }
  return IsNumber;
}


function parseCmdLine(sCmdLine)
//Desc:    Extracts the information passed on the command line and acts upon it _
//         where possible
//		   Code converted from Steve's VB program.
//Data In: sCmdLine - The contents of the command line passed to this program
{ 
  var sTmpStr="";
  var lPos=0;
  var lEndPos=0;
  var lDataStart=0;
  var sTmpFile="";
  var sValues="";
  
    //On Error Resume Next
    //Check to see if the version info flag has been included in the command line
    bShowVerInfo = (inStr(1,(String(sCmdLine).toUpperCase()), CMDLN_VINFO) > 0);
    
    //Check to see if a fault code parameter has been passed on the command line
    sFaultNumber = sGetCMDNumVal(sCmdLine, CMDLN_FAULT, sDEFPARAM);
    
    if ((sFaultNumber) && (sFaultNumber!="")) 
    {
      sValues = PARAM_FAULTNUM+"="+sFaultNumber;
    }
    else
    {
      // Set a deafult fault number.  Added 07/03/2007 DLP.
      sValues = PARAM_FAULTNUM+"=0";
    }
    
    //Check to see if a Language setting parameter has been passed on the command line
    sLangData = sGetCMDNumVal(sCmdLine, CMDLN_LANG, "0");
   
    if ((sLangData) && (sLangData!="")) 
    {
     if (sValues!="") 
     {
       sValues+="&";
     }
     sValues += PARAM_LANGNUM+"="+sLangData;
    }
    else
    {
      // Set a default language.  Added 07/03/2007 DLP.
      sValues += "&"+PARAM_LANGNUM+"=0";
    }
    
    //Check to see if a different filename has been passed on the command line - NOT REQUIRED.
   
    //Check to see if a different window Title bar title has been passed on the command line
    sWindowTitle = "";
    lPos = inStr(1,(sCmdLine.toUpperCase()), CMDLN_WINTITLE);
    if (lPos > 0)
    {
       //Extract the parameter value from the file
       lDataStart = lPos + CMDLN_WINTITLE.length;
       lEndPos = inStr(lDataStart, sCmdLine, '"');

       if (lEndPos > 0)
       {
          sWindowTitle = mid(sCmdLine, lDataStart, (lEndPos - (lDataStart)));
       }
    }
    
    //Now check to see if the Extra Data flag has been passed
    lPos = inStr(1,(sCmdLine.toUpperCase()), CMDLN_XTRA);
    if (lPos > 0)
    {
       //Extract the parameter value(s) from the file & store in an array
       lDataStart = lPos + (CMDLN_XTRA.length);

       sTmpStr = mid(sCmdLine, lDataStart);
       var sValues = extractExtraParams(sTmpStr, sValues);
    }       
	else
	{
	  // Set default information for the parameter names.
	  // Added 07/03/3007 DLP.
	  sValues += '&ParamNames=&EndOfParams=1';
	}

    return sValues;
     
}

function sGetCMDNumVal(sCmdLine, sCMDFlag, sDefVal)
//Desc:     Extract the numerical value of the specified parameter from the command line
//		   Code converted from Steve's VB program.
//Data In:  sCmdLine - The command line string _
//           sCMDFlag - The command line parameter flag to search for _
//           sDefVal  - The default value for this entry
//Data Out: sGetCMDNumVal - The Fault number (Default entry if not specified)
{
  var lPos=0;
  var lEndPos=0;
  var lDataStart=0; 
  var sTmpEntry="";
    
  //On Error Resume Next
  //Check to see if a fault code parameter has been passed on the command line
  lPos = inStr(1,(String(sCmdLine).toUpperCase()), sCMDFlag);
  if (lPos > 0) 
  {
    //Extract the parameter value from the command line string
    lDataStart = lPos + sCMDFlag.length;
    lEndPos = inStr(lPos + 1, sCmdLine, " ");
    lEndPos = inStr(lDataStart, sCmdLine, " ");
    if (lEndPos > 0)
    {
      sTmpEntry = mid(sCmdLine, lDataStart, (lEndPos - lDataStart));
    }  
    else
    {
      sTmpEntry = mid(sCmdLine, lDataStart);
    }  
     
    //Check to ensure we have been passed a valid number
    if (!IsNumeric(sTmpEntry))
	{
      sTmpEntry = sDefVal;  //Just set the default parameter value
    }  
    else
    {
      if (((0+sTmpEntry) < 0) || ((0+sTmpEntry) > 128))
      {
        //The number is not in the acceptable range so use the default value
        sTmpEntry= sDefVal;
      }
      
      //Perform a final check to ensure we will be passing something to the flash control
      if (sTmpEntry=="")
      { 
        sTmpEntry = sDefVal;
      }
    }
    
    
    return sTmpEntry;
  }
}


function extractExtraParams(sStartStr, sReturnValues)
//Desc:     Extract all Extra parameters from the command line. These will exist within _
//          mini records containing two fields (Name & Data) enclosed within three pipe _
//          characters (Before, between and end)
//			Code converted from Steve's VB program.
//
//Data In:  sStartStr - The command line string containing 1+ extra parameter records
//Data Out: N/A
//
//Updated:  atExtraParams() - The array containing extra parameters (Name & Data) _
//                             (1st record located at position 0) _
//           iXtraCount      - The number of extra parameter records
{
var i1stPos=0; 
var i2ndPos=0;
var i3rdPos=0;
var sWork="";
var sTmpName=""; 
var sTmpData="";

var sPIPE = "|";
var sParamNames = "";

    try 
    {
      //On Error GoTo ExtractExtraParamsErr
      //Read in the initial command line string
      sWork = trim(sStartStr);
      
      //Get the record start, middle and end record separator character positions
      i1stPos = inStr(1,sWork, sPIPE);
      i2ndPos = inStr((i1stPos + 1), sWork, sPIPE);
      i3rdPos = inStr((i2ndPos + 1), sWork, sPIPE);
      
      while ((i1stPos > 0) && (i2ndPos > 0) && (i3rdPos > 0))
      {

        //Read the record title and data
        sTmpName = "";
        sTmpData = "";
        
        if (i2ndPos > i1stPos)
        {
          sTmpName = mid(sWork, i1stPos + 1, (i2ndPos - (1 + i1stPos)));
        }
        if (i3rdPos > i2ndPos)
        {
          sTmpData = mid(sWork, i2ndPos + 1, (i3rdPos - (1 + i2ndPos)));
        }

        //Add the data to the array
        if (sReturnValues != "")
          sReturnValues += "&";
           
        sReturnValues += sTmpName + "=" + sTmpData;

        if (sParamNames!= "")
          sParamNames += ",";

        sParamNames += sTmpName;

        //Remove the record from the command line string
        if ((sWork.length) > (i3rdPos))
        {
          sWork = mid(sWork, i3rdPos); 
        }
        else
        {
          sWork = "";
        }
      
        //Get the record start, middle and end record separator character positions
        i1stPos = inStr(1,sWork, sPIPE);
        i2ndPos = inStr((i1stPos + 1), sWork, sPIPE);
        i3rdPos = inStr((i2ndPos + 1), sWork, sPIPE);
      }

      if (sReturnValues!="")
      {
        sReturnValues = sReturnValues+'&'+'ParamNames='+sParamNames
      }
      else
      {
        sReturnValues = 'ParamNames="'+sParmaNames+'"'
      }
      sReturnValues += '&EndOfParams=1';
      return sReturnValues;
    }
    catch(err)
    {
      //ExtractExtraParamsErr:
      alert("Error while extracting extra parameters for the Flash Animation: '"+err.description+"'");
      return ""; //err.description;
    }
}

function writeBaseRef()
{
  var sPath = getBaseRef();
  document.write('<base href="'+sPath+'" />');
}

function getBaseRef()
{
  var index = sGetAppIndex();
  var sPath = BuildFullPath(as_FlashFile[index]);

  if ((sPath.lastIndexOf(":\\")>0) && !(sPath.toUpperCase().lastIndexOf("file:")>0))
  {
    sPath = "file:///"+sPath;
  }
    
  return sPath.substr(0, sPath.lastIndexOf("/")+1);
}

function sGetAppIndex()
{
  var sQueryString = ""+document.location;
  var sQueryString = unescape(sQueryString);
  
  var StaticPageQueryPos = sQueryString.search(/index=/i);

  if(StaticPageQueryPos != - 1)
  {
	//Read out the static URL information
	var PageURL = sQueryString.substring(StaticPageQueryPos + 6, sQueryString.length);
	var PageEnd = PageURL.indexOf('&');
	
	if(PageEnd != - 1)
		PageURL = PageURL.substring(0, PageEnd);
	
	return PageURL;	
  }
}


function writeFlash()
{


  var index = sGetAppIndex()
  if (sWindowTitle=="") 
  {
    sWindowTitle=as_AppName[index];
  }
  
  if (document.title) document.title=sWindowTitle;

  var sFilename = as_FlashFile[index];
  
  sFilename = trim(sFilename.substr(sFilename.lastIndexOf("/")+1, sFilename.length));
  var sParams = parseCmdLine(as_Cmds[index]);
  
  // If a launch sequence is used; see if the Security setting should be used.
  // Added 05/03/2007 DLP.
  try
  {
    if (!((as_Launch==null) || (as_Launch[index]==null) || (as_Launch[index]==''))) 
    { 
      // Get the first parameter of the launch sequence (Flash pop-up is always first).
      var sSeq = as_Launch[index].split(",");
      
      // If the security flag is set, then add the override parameter to the parameters.
      if (bFlagSet(sSeq[0], FL_RunAT))
      {
        if (sParams != '') sParams += '&'; 

        sParams += 'LicenseInfo=1';
      } 
    } 
  }
  catch(err){ }

  //window.alert("sParams = '"+sParams+"'");

  if (!sHolder)
  {
    var sHolder = 'slot';
  }
  try 
  {
    var sccFlag=checkCCFlag();
  }
  catch(err){ }
 
  //See if a module cookie has been set.
  var sModSet = unescape(GetCookieStr('LJModID='));
  if (sModSet!="")
  {
    var sNewString = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"' 
                   + ' codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0"'
                   + ' id="oFlash" width="100%" height="100%" align="middle">'
                   + '<param name="allowScriptAccess" value="always" />'
                   + '<param name="FlashVars" value="'+sParams+'" />'
                   + '<param name="movie" value="'+sFilename+'" />'
                   + '<param name="quality" value="high" />'
                   + '<embed src="'+sFilename+'" FlashVars="'+sParams+'" quality="high"'
                   + ' width="100%" height="100%" swLiveConnect=true id="oFlash" name="oFlash" align="middle"'
                   + ' allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />'
                   + '</object>';

    if (IsNS6orGreater())
    {
      oLayer = window.document.getElementById(sHolder);
    
      if (oLayer)
      {
        sNewString = '<embed src="'+sFilename +'" FlashVars="'+sParams+'" quality="high"'
                 + ' width="100%" height="100%" swLiveConnect=true id="oFlash" name="oFlash" align="middle"'
                 + ' allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />';
    
        oLayer.innerHTML=sNewString;
        document.oFlash.play();
      }
    } 
    else if (document.all)
    {
      document.all(sHolder).innerHTML=sNewString;
    }
    else
    {
      window.document.getElementById(sHolder).innerHTML=sNewString;
      if ((document.oFlash) && (document.oFlash.play))
      {
        document.oFlash.play();
      }
    }
  }
  else
  {
    window.document.getElementById(sHolder).innerHTML=sFlshTxt_ErrorAccessingSim;
  }
}
