/* CMS Base */

var Dom = YAHOO.util.Dom;
var Connect = YAHOO.util.Connect;
var Event = YAHOO.util.Event;

function consolewrite(msg) {
    // using a function to centralise turn on/offable all console.log statments.
    // so all console.log statements run trough this.
    try {
        //if(console.log) {
        console.log(msg);
        //}
    }
    catch (e) {

    }
}

function FieldValue(FieldID, Default) {

    var retVal = Default;
    try {
        var field = Dom.get(FieldID);

        if (typeof (field != 'undefined')) {
            retVal = field.value;
        }
    }
    catch (e) {
        // alert("Problem trying to get field value for field: " + FieldID + "[" + e + "]");
    }
    return retVal;
}

function TruncateString(String, Length) {

    var retVal = "";

    if (String.length >= Length) {
        retVal = String.substr(0, Lenght) + "...";
    }
    else {
        retVal = String;
    }

    return retVal;
}

function SetListOptionByValueO(oSection, ValueText) {
   
    try {
        for (var i = 0; i < oSection.length; i++) {

            oSection.options[i].selected = oSection.options[i].value == ValueText;
            //if (oSection.options[i].text == Text) {
            //    oSection.options[i].selected = true;
            //    oSection.selectedIndex = i;
            //}

        }
    }
    catch (e) {
        alert("A problem occured trying to set the option item:" + e);
    }

    return true;
}

function SetListOptionByValue(selectID, ValueText) {
    var oSection = Dom.get(selectID);

    try {
        for (var i = 0; i < oSection.length; i++) {

            oSection.options[i].selected = oSection.options[i].value == ValueText;
            //if (oSection.options[i].text == Text) {
            //    oSection.options[i].selected = true;
            //    oSection.selectedIndex = i;
            //}
            
        }
    }
    catch (e) {
        alert("A problem occured trying to set the option item:" + e);
    }

    return true;
}

function SetStatusMessage(_MSG) {
    // Dosomething with message.
    //var oStatus = Dom.get("StatusBarMessage");
    //oStatus.innerHTML = _MSG;
    alert(_MSG);
    return true;
}

function InitialiseHTMLControl(YUIcontrol, TextArea, defaultText) {

    try {
        if (typeof (YUIcontrol) != 'undefined') {
            YUIcontrol.destroy();
        }

        var newYUIcontrol = new YAHOO.widget.Editor(TextArea, {
            height: PageController.Variables.HTMLControl.height,
            width: PageController.Variables.HTMLControl.width,
            dompath: false, //Turns on the bar at the bottom
            animate: true //Animates the opening, closing and moving of Editor windows
        });

        newYUIcontrol.render();
        newYUIcontrol.on("editorContentLoaded", function() {
            newYUIcontrol.setEditorHTML(defaultText);
        });
    }
    catch (e) {
        alert(e);
    }

    return newYUIcontrol;
}
