﻿/*

    Author: Jeffrey Gordillo
    Date: 12/11/2006
    Description: Functions used by for the HTML Editor of the CMS

*/

var currEditorName, currEditorMaxLength;
var bErrorTitleFound, bErrorContentFound;
var strErrorMessage = '';


function FCKeditor_OnFocus ( editorInstance ) {
    setCurrEditor(editorInstance.Name);
    }

function FCKeditor_OnBlur ( editorInstance ) {
    currEditorName = '';
    }

function FCKeditor_OnSelectionChange ( editorInstance ) {
    onKeyTask(editorInstance.Name)
    }

function FCKeditor_OnComplete(editorInstance) {
	editorInstance.Events.AttachEvent( 'OnFocus', FCKeditor_OnFocus );
    if ( document.all ) {
        setOnKeyListener(editorInstance.Name);
        }
    else {
        editorInstance.Events.AttachEvent( 'OnSelectionChange', FCKeditor_OnSelectionChange );
        }
	
    initTextEditor(editorInstance.Name);
    }

function setCurrEditor(strObj) {
    if ( currEditorName != strObj ) {
        currEditorName = strObj;
        currEditorMaxLength = getMaxLength(strObj);
        }
    }

function setOnKeyListener(strObj) {
	var oEditor = FCKeditorAPI.GetInstance(strObj) ;
	var oDOM = oEditor.EditorDocument ;
    oDOM.onkeyup = function () { onKeyTask(strObj); };
	}

function initTextEditor(strObj) {
	var MaxLength = getMaxLength(strObj);
	
	if ( MaxLength > 0 ) {
        printavailableLength(strObj);
        }
    else {
        var objElement = document.getElementById(strObj + '_currLength');
        if ( objElement ) objElement.value = '[unlimited]';
        }
    }

function getMaxLength(strObj) {
    if ( document.getElementById(strObj + '_maxLength') )
        return document.getElementById(strObj + '_maxLength').value;
    else 
        return 0;
    }

function onKeyTask(strObj) {
    showButtons (strObj);

//  setCurrEditor(strObj);

    if (currEditorMaxLength > 0) {
        printavailableLength ( strObj );
        }
	}

function printavailableLength (strObj) {
    var availableLength = getMaxLength(strObj) - returnTextLength( strObj );
    var objElement = document.getElementById(strObj + '_currLength');

    if ( objElement ) {
        if ( availableLength < 0 ) {
            availableLength = availableLength * -1;
            objElement.style.color = 'red';
            }
        else {
            objElement.style.color = 'black';
            }
        objElement.value = availableLength;
        }
    }

function validateFields(source, arguments) {
    var strObj = arguments.Value;

    if ( currEditorName == strObj ) {
        if ( (bErrorTitleFound) || (bErrorContentFound) ) {
            arguments.IsValid = false;
            alert ('Sorry, cannot continue. The following errors where found:   \n' + strErrorMessage + '\n\n');
            }
        else {
            arguments.IsValid = true;
/*
            var objectName = document.getElementById(strObj + '_btnSaveName').value;
            if ( document.getElementById(objectName) ) {
                document.getElementById(objectName).disabled = true;
                document.getElementById(objectName).value = '...saving...';
                }
*/
            }
        }
    }

function validateTitle(source, arguments) {
    var bErrorFound = false;
    var strObj = arguments.Value;

    if ( (!( bErrorTitleFound )) && ( currEditorName == strObj ) ) {
        if ( document.getElementById(strObj + '_txtTitle') ) {
            var strTitle = document.getElementById(strObj + '_txtTitle').value;
            if ( trimStr (strTitle) == '' ) {
                bErrorTitleFound = true;
                arguments.IsValid = false;
                strErrorMessage += '\n    • The field "Title" is required';
                }
            else {
                arguments.IsValid = true;
                }
            }
        }
    }

function validateContent(source, arguments) {
    var bErrorFound = false;
    var strObj = arguments.Value;

    if ( (!( bErrorContentFound )) && ( currEditorName == strObj ) ) {
        if ( FCKeditorAPI.GetInstance(strObj) ) {
            var txtLength = returnTextLength(strObj);
            var maxLength = getMaxLength(strObj);

            if ( txtLength == 0 ) {
                bErrorContentFound = true;
                arguments.IsValid = false;
                strErrorMessage += '\n    • The field "Content" is required';
                }
            else if ( ( maxLength > 0 ) && ( txtLength > maxLength ) ) {
                bErrorContentFound = true;
                arguments.IsValid = false;
                strErrorMessage += '\n    • The field "Content" has a length of ' + txtLength + ' characters; the limit is ' + maxLength + '.   ';
                }
            else {
                arguments.IsValid = true;
                }
            }
        }
    }

function fillContentHTML (strObj) {
    strErrorMessage = '';
    currEditorName = strObj;
    bErrorTitleFound = false;
    bErrorContentFound = false;
    
    if ( FCKeditorAPI.GetInstance(strObj) ) {
        var maxLength = getMaxLength(strObj);

        if ( maxLength > 0 ) printavailableLength (strObj);

        var finalContent = GetContents(strObj);
        if ( document.getElementById(strObj + '_hidFinalContent') ) {
            document.getElementById(strObj + '_hidFinalContent').value = finalContent
            }
        }
    else {
        if ( document.getElementById(strObj + '_hidFinalContent') ) {
            document.getElementById(strObj + '_hidFinalContent').value = '&nbsp;';
            }
        }
    }

function showButtons(strObj) {
    var objectName;
    if ( document.getElementById(strObj + '_btnSaveName') ) {
        objectName = document.getElementById(strObj + '_btnSaveName').value;
        if ( document.getElementById( objectName ) )
            document.getElementById( objectName ).style.display = 'inline';
        }
    if ( document.getElementById(strObj + '_btnPrevName') ) {
        objectName = document.getElementById(strObj + '_btnPrevName').value;
        if ( document.getElementById( objectName ) )
            document.getElementById( objectName ).style.display = 'inline';
        }
    }

function returnTextLength( strObj ) {
	var oEditor = FCKeditorAPI.GetInstance(strObj) ;
	var oDOM = oEditor.EditorDocument ;
	var iLength = 0;

	if ( document.all ) {   // If Internet Explorer.
		iLength = oDOM.body.innerText.length ;
		}
	else {  // If Gecko.
		var r = oDOM.createRange() ;
		r.selectNodeContents( oDOM.body ) ;
		iLength = r.toString().length ;
		}
	return iLength;
	}

function SetContents(strObj, strText) {
	var oEditor = FCKeditorAPI.GetInstance(strObj) ;
	oEditor.EditorDocument.body.innerHTML = strText;
    }

function GetContents(strObj) {
	var oEditor = FCKeditorAPI.GetInstance(strObj) ;
	return oEditor.GetXHTML( true ) ;
    }


// *************************************


function trimStr (str) {
    return str.replace(/^\s*|\s*$/g,"");
    }

function replaceAll( str, from, to ) {
    var idx = str.indexOf( from );
    while ( idx > -1 ) {
        str = str.replace( from, to );
        idx = str.indexOf( from );
        }
    return str;
    }

function getContentFromField (strFieldObj) {
    if  ( document.getElementById(strFieldObj) )
        return document.getElementById(strFieldObj).value;
    }

function createEditor (strEditorName, strBasePath, strSkinPath, strFieldName) {        
    var oFCKeditor = new FCKeditor(strEditorName) ;
    oFCKeditor.BasePath	= strBasePath ;
    oFCKeditor.Value	= getContentFromField(strFieldName) ;
    oFCKeditor.Create();
    }
