var ie4=document.all;
var ns6=document.getElementById&&!document.all;
var intRemainingRedirectTime;
var intTotalRedirectTime;
var intAdminCountDown = 100;
var timTimer;     
var objFusionObject;
var blnViewSource = false;


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;
      fnFindBrowserObject('document.frmPage.butViewSource').value='View source';
    }
    else
    {
      document.getElementById(objIframe).contentWindow.document.body.innerHTML = fnFindBrowserObject(objTxtObject).value;
      fnFindBrowserObject('butViewSource').value='View source';
    }
  }
  else
  {
    fnFindBrowserStyle(fnFindBrowserObject(divIfr)).display = 'none';
    fnFindBrowserStyle(fnFindBrowserObject(divTxt)).display = 'block';
    
    if (ie4)
    {
      fnFindBrowserObject("document.frmPage."+objTxtObject).value = document.getElementById(objIframe).contentWindow.document.body.innerHTML;
      fnFindBrowserObject('document.frmPage.butViewSource').value='View design';
    }
    else
    {
      fnFindBrowserObject(objTxtObject).value = document.getElementById(objIframe).contentWindow.document.body.innerHTML;
      fnFindBrowserObject('butViewSource').value='View design';
    }            
  }    
}

function fnCheckModified()
{
  // 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 
  return(true);
}

// 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;
       }
     }
}


// 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 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 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 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;
  if (!(ie4))
    document.getElementById(objIFrame).contentWindow.document.body.innerHTML = "<div onclick=\"parent.fnFusionSwitch('"+objIFrame+"');\">"+document.getElementById(objIFrame).contentWindow.document.body.innerHTML+"</div>";
}
  

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  // DEBUG : Mozilla based HTML editing
     {
       
       //var lngStart = objRanger.selectionStart;
        //var lngEnd = objRanger.selectionEnd;
       var sel = document.getElementById(objFusionObject).contentWindow.getSelection().getRangeAt(0);
        //alert(document.getElementById(objFusionObject).contentWindow.document.createRange().pasteHTML(strStart+sel+strEnd));
        
        //document.getElementById(objFusionObject).contentWindow.document.pasteHTML(strStart+sel+strEnd);
  
        // deselect everything        
        //sel.removeAllRanges();
  
        // make a new range for the new selection
        range=document.getElementById(objFusionObject).contentWindow.document.createRange();
        alert(range);
        //range.pasteHTML(strStart+sel+strEnd);
  
        // remove content of current selection from document
        //range.deleteContents();
  
        // get location of current selection      
        var container = range.startContainer;
        var pos = range.startOffset;          
  
        // if we insert text in a textnode, do optimized insertion
                
        container.insertData(pos, strStart+sel+strEnd);
        //container.insertData(pos+, strStart);
  
        // put cursor after inserted text
        //range.setEnd(container, pos+strStart.length+strEnd.length);
        //range.setStart(container, pos);
    }
  }
}


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/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); }