var ie4=document.all;
var ns6=document.getElementById&&!document.all;
var intRemainingRedirectTime;
var intTotalRedirectTime;
var intAdminCountDown = 100;
var timTimer;     
var objFusionObject;
var blnViewSource = false;
var objLastSwitch = "";
var objLast = "";


function fnScaleProp(lngNewVal, objOldVal, objNewVal, objHistVal)
{
  if (document.frmPage.chkProportional.checked == true)
  {
    objNewVal.value = Math.floor(objNewVal.value * (lngNewVal / objOldVal.value));
    objOldVal.value = lngNewVal;
    objHistVal.value = objNewVal.value;
  }
  
  fnFindBrowserStyle(fnFindBrowserObject('frmPage.imgImageScale')).width=document.frmPage.txtImgWidth.value;
  fnFindBrowserStyle(fnFindBrowserObject('frmPage.imgImageScale')).height=document.frmPage.txtHeight.value;  
}

function fnViewSource(divIfr, divTxt, objIframe, objTxtObject)
{  
  if (blnViewSource)
  {
    fnFindBrowserStyle(fnFindBrowserObject(divIfr)).display = 'block';
    fnFindBrowserStyle(fnFindBrowserObject(divTxt)).display = 'none';
      
    if (ie4)
      document.getElementById(objIframe).contentWindow.document.body.innerHTML = fnFindBrowserObject("document.frmPage."+objTxtObject).value;
    else
      document.getElementById(objIframe).contentWindow.document.body.innerHTML = fnFindBrowserObject(objTxtObject).value;
    
    fnSwitchFusionButton('fusionButViewSource', !blnViewSource);
  }
  else
  {
    fnFindBrowserStyle(fnFindBrowserObject(divIfr)).display = 'none';
    fnFindBrowserStyle(fnFindBrowserObject(divTxt)).display = 'block';
    
    if (ie4)
    {
      strHTML = document.getElementById(objIframe).contentWindow.document.body.innerHTML;
      
      strHTML = strHTML.replace(/<\/P>/g, "<\/P>\n");
      strHTML = strHTML.replace(/<BR>/g, "<BR>\n");
      strHTML = strHTML.replace(/<\/H1>/g, "<\/H1>\n");
      strHTML = strHTML.replace(/<\/H2>/g, "<\/H2>\n");
      
      fnFindBrowserObject("document.frmPage."+objTxtObject).value = strHTML;
    }
    else
      fnFindBrowserObject(objTxtObject).value = document.getElementById(objIframe).contentWindow.document.body.innerHTML;
    
    fnSwitchFusionButton('fusionButViewSource', !blnViewSource);
  }    
}


function fnSwitchFusionButton(objButId, blnOn)
{
  if (blnOn)
  {
    fnFindBrowserStyle(fnFindBrowserObject(objButId)).backgroundImage = "url(../images/fusion/button-on.jpg)";
    fnFindBrowserStyle(fnFindBrowserObject(objButId)).color = "#ffffff";
  }
  else
  {
    fnFindBrowserStyle(fnFindBrowserObject(objButId)).backgroundImage = "url(../images/fusion/button-off.jpg)";
    fnFindBrowserStyle(fnFindBrowserObject(objButId)).color = "#354957";
  }
}


function fnSwitchMeta(objButId, objDivID)
{
  if (objLast!=objButId)
  {
    if (objLast!="")
      fnSwitchFusionButton(objLast, false);
    
    fnSwitchFusionButton(objButId, true);
    
    if (objLastSwitch!="")
      fnFindBrowserStyle(fnFindBrowserObject(objLastSwitch)).display='none';
    fnFindBrowserStyle(fnFindBrowserObject(objDivID)).display='block';
    
    fnFindBrowserStyle(fnFindBrowserObject('fusionFunctionsTD')).display='block';
    
    objLastSwitch = objDivID;
    objLast = objButId;
  }
  else
  {
    fnFindBrowserStyle(fnFindBrowserObject(objDivID)).display='none';
    fnFindBrowserStyle(fnFindBrowserObject('fusionFunctionsTD')).display='none';
    fnSwitchFusionButton(objButId, false);
    objLastSwitch = "";
    objLast = "";
  }
}


function fnCheckModified()
{
  // THIS FUNCTION DOESN'T OPERATE CORRECTLY DUE TO IFRAMES ALWAYS CALLING THE BEFOREUNLOAD EVENT!
  
  
  // This function is to check to see if the user is navigating away from a page 
  // they could have been editing and present them with a warning 
  
  
  /*var strMessage;
  strMessage = document.frmPage.txtAudit.value;
  
  alert(strMessage);
  
  if (strMessage!="")
  {
    if (ie4)
      event.returnValue = "The following actions have occured:\n\n"+strMessage.replace("<br />", "\n");
    else
      return "The following actions have occured:\n\n"+strMessage.replace("<br />", "\n");
  }
  */
}

// 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";
    
    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, blnInline)
{
  if (!(blnReadOnly))
    document.getElementById(objIframe).contentWindow.document.designMode="on";   
 
  if (ie4) 
    setTimeout("fnFusionReady('"+objIframe+"', 'document."+strFrm+"."+objFormObject+"', "+blnReadOnly+", "+blnInline+")", 1000);
  else
    setTimeout("fnFusionReady('"+objIframe+"', '"+objFormObject+"', "+blnReadOnly+", "+blnInline+")", 100);   
}


function fnFusionReady(objIframe, objFormObject, blnReadOnly, blnInline)
{
   fnAppendFusionValue(objIframe, objFormObject); 
   
   if (!(blnReadOnly)&&(objIframe!="ifrTodoList"))
     if (ie4) 
     {
       fnFindBrowserObject('frmPage.butSubmit').disabled = false;
       if (!blnInline)
       {
         //fnFindBrowserObject('frmPage.butViewSource').disabled = false;
         //fnFindBrowserObject('frmPage.butEditMode').disabled = false;
         //fnFindBrowserObject('frmPage.butRemoveAllFormat').disabled = false;
         //fnFindBrowserObject('frmPage.butRemoveFormat').disabled = false;
       }
     }
     else
     {
       fnFindBrowserObject('butSubmit').disabled = false;
       if (!blnInline)
       {
         //fnFindBrowserObject('butViewSource').disabled = false;
         //fnFindBrowserObject('butEditMode').disabled = false;
       }
     }
     
   // Clear out the onchange event firing for IFrames
   document.frmPage.txtAudit.value="";
}



// This function returns a given decimal place float suitable for a currency or percentage
function parseCurrency(fltValue, strDefault, intPrecision, blnAllowblank)
{
  if (fltValue=='')
    return (blnAllowblank) ? '' : strDefault;
  else
  {
    fltValue = fltValue.replace(/ /g, "");      
    fltValue = fltValue.replace(/,/g, "");        
    fltValue = parseFloat(fltValue);
    
    if (isNaN(fltValue)) 
      return(strDefault);
    else  
      return (fltValue.toFixed(intPrecision));
  }
}



function fnCheckSpelling()
{ fnCheckInternet("the spelling", "http://dictionary.cambridge.org/results.asp?searchword="); }      

function fnCheckThesaurus()
{ fnCheckInternet("the thesaurus", "http://thesaurus.reference.com/search?q="); }     

function fnCheckGoogle()
{ fnCheckInternet("Google", "http://www.google.com/search?hl=en&ie=UTF-8&oe=UTF-8&q="); }

function fnCheckWikiPedia()
{ fnCheckInternet("the WikiPedia encyclopedia", "http://en.wikipedia.org/wiki/Special:Search?search="); }


function fnCheckInternet(strTitle, strURL)
{
  if (objFusionObject==null)
    alert("Sorry, you can only check "+strTitle+" with text highlighted within a HTML text box");
  else
  {
    strSearchWord = fnFusionFindCurrentString();
    
    if (strSearchWord!="")
      spellWin = window.open(strURL + strSearchWord);
    else
      alert("Please select a word to search "+strTitle);
  }
}       


function fnFusionFindCurrentString()
{
  // This function returns the current highlighted section or the closest word
  // TODO: If no word is selected then find the closest word to the cursor
  return(document.getElementById(objFusionObject).contentWindow.document.selection.createRange().text);
}
        

function fnAppendFusionValue(objIFrame, objFormObject)
{
  document.getElementById(objIFrame).contentWindow.document.body.innerHTML = fnFindBrowserObject(objFormObject).value;
}
  

function fnFusionRemoveFormatting()
{
  if (objFusionObject!=null)
  {
       document.getElementById(objFusionObject).contentWindow.document.body.innerHTML = document.getElementById(objFusionObject).contentWindow.document.body.innerHTML.replace(/<P/g, "#P#<P");
       document.getElementById(objFusionObject).contentWindow.document.body.innerHTML = document.getElementById(objFusionObject).contentWindow.document.body.innerHTML.replace(/<BR/g, "#BR#<BR");
       var strRanger = document.getElementById(objFusionObject).contentWindow.document.body.innerText;
       strRanger = strRanger.replace(/#BR#/g, "<BR>");
       document.getElementById(objFusionObject).contentWindow.document.body.innerHTML = strRanger.replace(/#P#/g, "</P><P>");
  }
}


function fnFusionRemoveSelFormatting()
{
  if (objFusionObject!=null)
  {
          var objRanger = document.getElementById(objFusionObject).contentWindow.document.selection.createRange();
          document.getElementById(objFusionObject).contentWindow.document.selection.createRange().pasteHTML(objRanger.text);
  }
}  
  

function fnInsertFusionCommand(strStart, strEnd)
{
  if (objFusionObject!=null)
  {
     if (ie4)
     {        
       if (strEnd=="")
       {
         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();

        document.getElementById(objFusionObject).contentWindow.document.selection.createRange().pasteHTML(strStart+objRanger.htmlText+strEnd);
        // TODO: Should look at re-selecting selected text        
        document.getElementById(objFusionObject).contentWindow.focus();
       }
     }  
     else
     {
       // cut-up from: http://www.mozilla.org/editor/midasdemo/
       
      var win = document.getElementById(objFusionObject).contentWindow;
      var sel = win.getSelection();
      
      strInsertCommand = strStart+sel+strEnd;
      
      var range = sel.getRangeAt(0);

      sel.removeAllRanges();


      var container = range.startContainer;
      var pos = range.startOffset;
      
      range=document.createRange();
      var textNode = container;
      container = textNode.parentNode;
      var text = textNode.nodeValue;
      var textBefore = text.substr(0,pos);
          // text after the split
      var textAfter = text.substr(pos);
      
      document.getElementById(objFusionObject).contentWindow.document.body.innerHTML = textBefore+strInsertCommand+textAfter;
    }
  }
}




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, strTitle)
{  
  objFusionObject = objNewObject;
  fnFindBrowserObject('fusionCurrentlyEditing').innerHTML = "currently editing: <strong>"+strTitle+"</strong>";
}


// 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!="")
  {
    if (ie4)
      fnFindBrowserObject("document."+objImage).src = ((fnFindBrowserStyle(fnFindBrowserObject(objObject)).display=='none')||(fnFindBrowserStyle(fnFindBrowserObject(objObject)).display=='')) ? '../images/fusion/dropdown.gif' : '../images/fusion/dropdown-down.gif';
     else
      fnFindBrowserObject(objImage).src = ((fnFindBrowserStyle(fnFindBrowserObject(objObject)).display=='none')||(fnFindBrowserStyle(fnFindBrowserObject(objObject)).display=='')) ? '../images/fusion/dropdown.gif' : '../images/fusion/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); }