var ie4=document.all;
var ns6=document.getElementById&&!document.all;
var intRemainingRedirectTime;
var intTotalRedirectTime;
var intAdminCountDown = 100;
var timTimer;     
var objFusionObject;

// NOTE: This function is used to initialise the countdown timer for Fusion
function adminRedirect(intTime)
{
  intRemainingRedirectTime = intTime;
  intTotalRedirectTime = intTime;
  var fltCountDisplay = intRemainingRedirectTime / 1000;
  document.frmRedirect.txtTimer.value = parseInt(fltCountDisplay);
  timTimer = setTimeout("adminCountDown()", intAdminCountDown);
}


// NOTE: This function is used to display a countdown timer for the Fusion
function adminCountDown()
{
  intRemainingRedirectTime -= intAdminCountDown;
  
  if (intRemainingRedirectTime<0)
  {
    clearTimeout(timTimer);
    top.location.href='index.php';
  }  
  else
  {
    var fltCountDisplay = intRemainingRedirectTime / 1000;
    document.frmRedirect.txtTimer.value = parseInt(fltCountDisplay);
    
    var strWidth = 400*((1/intTotalRedirectTime)*(intTotalRedirectTime-intRemainingRedirectTime)) + "px";
    //alert (strWidth);
    
    fnFindBrowserStyle(fnFindBrowserObject('fusionRedirectBar')).width=strWidth;
    
    timTimer = setTimeout("adminCountDown()", intAdminCountDown);
  }  
}


function fnDisplayFusionWarning(strStringToDisplay)
{
  fnFindBrowserObject('fusionErrorList').innerHTML = strStringToDisplay;
  fnFindBrowserStyle(fnFindBrowserObject('fusionPageWarning')).display="block"; 
}


function fnInitFusion(objIframe, objFormObject, strFrm, blnReadOnly)
{
  if (!(blnReadOnly))
    document.getElementById(objIframe).contentWindow.document.designMode="on";
 
  if (ie4) 
    setTimeout("fnAppendFusionValue('"+objIframe+"', 'document."+strFrm+"."+objFormObject+"')", 1000);
  else
    setTimeout("fnAppendFusionValue('"+objIframe+"', '"+objFormObject+"')", 100);
}


// NOTE: This function is used by the attachment page to populate the attachments 
function fnAdd(intValue, strText)
{          
  if (ie4)
    objSelectTo = fnFindBrowserObject('parent.document.frmPage.selAttached');
  else
    objSelectTo = fnFindBrowserObject('parent.selAttached'); // TODO: Fix this for Mozilla
  
  objSelectTo.options[objSelectTo.options.length] = new Option(strText, intValue);
  objSelectTo.options[objSelectTo.options.length-1].selected=true;
}


// This function returns a two decimal place float suitable for a currency
function parseCurrency(fltValue)
{
  fltValue = parseFloat(fltValue)
  
  if (isNaN(fltValue)) fltValue=100.00;  
  
  return (fltValue.toFixed(2));
}
        
        
function fnFusionPrint()
{
  document.getElementById('ifrPrint').contentWindow.document.body.innerHTML = "<h1>"+document.frmPage.txtTitle.value+"</h1><br /><br />";
  document.getElementById('ifrPrint').contentWindow.document.body.innerHTML += document.getElementById('ifrFull').contentWindow.document.body.innerHTML;
  top.document.ifrPrint.print();  
}

        

function fnAppendFusionValue(objIFrame, objFormObject)
{     
  document.getElementById(objIFrame).contentWindow.document.body.innerHTML = fnFindBrowserObject(objFormObject).value; 
}
  

function fnFusionRemoveFormatting()
{
  if (objFusionObject!=null)
  {
    // DEBUG: should function on Mozilla
     //if (ie4)
     //{       
       var strRanger = document.getElementById(objFusionObject).contentWindow.document.body.innerText;       
       document.getElementById(objFusionObject).contentWindow.document.body.innerHTML = strRanger;
       //alert(document.getElementById(objFusionObject).contentWindow.document.body.innerHTML);
    // }
  }
}  
  

function fnInsertFusionCommand(strStart, strEnd)
{
  if (objFusionObject!=null)
  {
     if (ie4)
     {        
       if (strEnd=="")
       {
         //alert(objFusionObject);         
         eval("document."+objFusionObject).focus();  
         document.getElementById(objFusionObject).contentWindow.document.selection.createRange().pasteHTML(strStart);                
       }
       else
       {
         // TODO: Write start and end command to IFrame
        var objRanger = document.getElementById(objFusionObject).contentWindow.document.selection.createRange();
        //alert(document.getElementById(objFusionObject).contentWindow.document.selection.createRange().selectionStart);        

        document.getElementById(objFusionObject).contentWindow.document.selection.createRange().pasteHTML(strStart+objRanger.htmlText+strEnd);
        // TODO: Should look at re-selecting selected text        
        //document.getElementById(objFusionObject).contentWindow.document.selection.createRange().moveStart(lngStart);
        //document.getElementById(objFusionObject).contentWindow.document.selection.createRange().moveEnd(lngEnd);
        document.getElementById(objFusionObject).contentWindow.focus();
       }
     }  
     else  // DEBUG : Mozilla based HTML editing
     {
       
       //var lngStart = objRanger.selectionStart;
        //var lngEnd = objRanger.selectionEnd;
       var sel = document.getElementById(objFusionObject).contentWindow.getSelection();      
        //var sel = win.getSelection();
  
        // get the first range of the selection
        // (there's almost always only one range)      
        var range = sel.getRangeAt(0);
        //alert(sel);
  
        // deselect everything        
        sel.removeAllRanges();
  
        // remove content of current selection from document
        range.deleteContents();
  
        // get location of current selection      
        var container = range.startContainer;
        var pos = range.startOffset;
  
        // make a new range for the new selection
        //range=document.createRange();
  
        // if we insert text in a textnode, do optimized insertion
                
        container.insertData(pos, strCommand);
  
        // put cursor after inserted text
        range.setEnd(container, pos+strCommand.length);
        range.setStart(container, pos+strCommand.length);
    }
  }
}


function fnSelectAll(objSelect)
{
  if (ie4)
    objSelect = fnFindBrowserObject(eval('frmPage.'+objSelect));
  else
    objSelect = fnFindBrowserObject(objSelect);
  
  for (var intIndex=0 ; intIndex < objSelect.options.length ; intIndex++)
    objSelect.options[intIndex].selected=true;
}


function fnSelectRemove(objSelect)
{          
  if (ie4)
    objSelect = fnFindBrowserObject(eval('frmPage.'+objSelect));
  else
    objSelect = fnFindBrowserObject(objSelect);
  
  for (var intIndex=0 ; intIndex < objSelect.options.length ; intIndex++)
  {
    if (objSelect.options[intIndex].selected)
    {     
      objSelect.options[intIndex] = null;
      intIndex--;
    }
    else
    {
      objSelect.options[intIndex].selected=true;
    }
  }
}


function fnSelectRemoveAll(objSelect)
{          
  if (ie4)
    objSelect = fnFindBrowserObject(eval('frmPage.'+objSelect));
  else
    objSelect = fnFindBrowserObject(objSelect);
  
  for (var intIndex=0 ; intIndex < objSelect.options.length ; intIndex++)
  {   
    objSelect.options[intIndex] = null;
    intIndex--;
  }
}


function fnWriteFusionCommand(strCommand, strCommand3)
{   
  if (objFusionObject!=null)
  {
   document.getElementById(objFusionObject).contentWindow.document.execCommand(strCommand, false, strCommand3);
   document.getElementById(objFusionObject).contentWindow.focus();
  }
}

// NOTE: This function is used to set IFrames for use with the control panel or null when a normal text box is focus
function fnFusionSwitch(objNewObject)
{  
  objFusionObject = objNewObject;
}


// NOTE: This function is used for the fancy table rollover
function fnSwitchTr(objArray, strColour)
{
  for (intIndex = 0 ; intIndex < objArray.length ; intIndex++)    
    fnFindBrowserStyle(fnFindBrowserObject(objArray[intIndex])).backgroundColor=strColour;
}

function fnCSSAssign(objDiv, strProperty, strValue)
{
  
  // DEBUG: Should assign property here
  var objElement = eval(fnFindBrowserStyle(fnFindBrowserObject(objDiv)));
  objElement = eval(objElement+"."+strProperty);
  
  objElement.value = strValue;
  //alert(objElement);
}

function fnWriteDiv(objDiv, strHTML)
{ fnFindBrowserObject(objDiv).innerHTML = strHTML; }

function fnSwitchObject(objObject, objImage)
{ 
  fnFindBrowserStyle(fnFindBrowserObject(objObject)).display = ((fnFindBrowserStyle(fnFindBrowserObject(objObject)).display=='none')||(fnFindBrowserStyle(fnFindBrowserObject(objObject)).display=='')) ? 'block' : 'none';
  
  if (objImage!="")
    fnFindBrowserObject(objImage).src = ((fnFindBrowserStyle(fnFindBrowserObject(objObject)).display=='none')||(fnFindBrowserStyle(fnFindBrowserObject(objObject)).display=='')) ? 'images/dropdown.gif' : 'images/dropdown-down.gif';
}

function fnFindBrowserObject(objIdent)
{ return (ie4 ? eval(objIdent) : ns6 ? document.getElementById(objIdent) : ns4 ? document.objIdent : ""); }

function fnFindBrowserStyle(objIdent)
{ return ((ie4||ns6) ? objIdent.style : objIdent); }
