﻿
var FieldEditorJs_ShowErrors = true;

function FieldEditor_EditField(objLabel) {
    // Debug Code
    var _debug = false;
    var functionName = "[FieldEditor_EditField]";
    if (_debug) { alert(functionName + ": begin"); }

    try {
        if (!objLabel) { throw new Error("Argument objLabel cannot be null"); }

        // Get the textbox to show
        var objText = $(objLabel.getAttribute("EditControlId"));
        if (!objText) { throw Error("Unabled to find editor control"); }
        objText.style.display = 'inline';
        //objText.style.width = '240px';
        objText.Canceling = false;
        if (objText.EmptyText == objLabel.innerHTML) {
            objText.value = "";
        }
        else {
            objText.value = objLabel.innerHTML;
        }
        objText.focus();
        objText.select();

        // Hide our label
        objLabel.style.display = 'none';

    } catch (e) {
        if (_debug || FieldEditorJs_ShowErrors) { alert(functionName + ' Error: ' + e.message); }
    }
}

function FieldEditor_SaveField(objTextbox) {
    // Debug Code
    var _debug = false;
    var functionName = "[FieldEditor_SaveField]";
    if (_debug) { alert(functionName + ": begin"); }

    try {
        if (!objTextbox) { throw new Error("Argument objTextbox cannot be null"); }
        if (objTextbox.Canceling) return;

        var objLabel = $(objTextbox.getAttribute("LabelControlId"));
        if (!objLabel) { throw Error("Unabled to find label control"); }
        var objProgress = $(objTextbox.getAttribute("ProgressControlId"));
        if (!objProgress) { throw Error("Unabled to find progress control"); }

        if (objTextbox.value == "") { objLabel.innerHTML = objTextbox.getAttribute("EmptyText"); }
        objTextbox.style.display = 'none';

        if (((objLabel.innerHTML == objTextbox.getAttribute("EmptyText")) && (objTextbox.value == "")) 
            || (objTextbox.value == objLabel.innerHTML)) {
            // We have no changes to make
            objLabel.style.display = 'inline';
        }
        else {
            // Save our changes
            objProgress.style.display = 'inline';
            setTimeout("__doPostBack('" + objTextbox.name + "','')", 0);
        }

    } catch (e) {
        if (_debug || FieldEditorJs_ShowErrors) { alert(functionName + ' Error: ' + e.message); }
    }

}

function FieldEditor_CancelEditField(objTextbox) {
    // Debug Code
    var _debug = false;
    var functionName = "[FieldEditor_CancelEditField]";
    if (_debug) { alert(functionName + ": begin"); }

    try {
        if (!objTextbox) { throw new Error("Argument objTextbox cannot be null"); }
        var objLabel = $(objTextbox.getAttribute("LabelControlId"));
        if (!objLabel) { throw Error("Unabled to find label control"); }

        objTextbox.Canceling = true;
        
        objTextbox.style.display = 'none';
        objLabel.style.display = 'inline';
        
    } catch (e) {
        if (_debug || FieldEditorJs_ShowErrors) { alert(functionName + ' Error: ' + e.message); }
    }
}
function FieldEditor_OnKeyDown(objTextBox, e) {
    // Debug Code
    var _debug = false;
    var functionName = "[FieldEditor_OnKeyDown]";
    if (_debug) { alert(functionName + ": begin"); }

    try {
        var keynum;
        
        if (window.event) // IE
        {
            keynum = e.keyCode;
        }
        else if (e.which) // Netscape/Firefox/Opera
        {
            keynum = e.which;
        }
              
        // Escape Key
        if (keynum == 27) {
            FieldEditor_CancelEditField(objTextBox);
        }
        // Enter Key
        if ((keynum == 13) && (objTextBox.nodeName != "TEXTAREA")) {
            FieldEditor_SaveField(objTextBox);
        }
        
    } catch (e) {
        if (_debug || FieldEditorJs_ShowErrors) { alert(functionName + ' Error: ' + e.message); }
    }
}

function FieldEditor_SetEditHover(objLabel) {
    // Debug Code
    var _debug = false;
    var functionName = "[FieldEditor_SetEditHover]";
    if (_debug) { alert(functionName + ": begin"); }

    try {
        if (objLabel) {
            objLabel.style.backgroundColor = '#FEF194';
            objLabel.style.borderStyle = 'solid';
            objLabel.style.borderColor = '#E2DED6';
            objLabel.style.borderWidth = '1px';
            objLabel.style.padding = '5px';
        }
        else {
            alert('Unable to get a reference to the title edit control.');
        }
    } catch (e) {
        if (_debug || FieldEditorJs_ShowErrors) { alert(functionName + ' Error: ' + e.message); }
    }
}

function FieldEditor_ExitEditHover(objLabel) {
    // Debug Code
    var _debug = false;
    var functionName = "[FieldEditor_ExitEditHover]";
    if (_debug) { alert(functionName + ": begin"); }

    try {
        if (objLabel) {
            objLabel.style.backgroundColor = '';
            objLabel.style.borderStyle = 'none';
        }
        else {
            alert('Unable to get a reference to the title edit control.');
        }
    } catch (e) {
        if (_debug || FieldEditorJs_ShowErrors) { alert(functionName + ' Error: ' + e.message); }
    }
}


// Used to snag a reference to an object quickly
// aka The Prototype $ Dollar Function
// See http://www.dustindiaz.com/top-ten-javascript
function $() {
    var elements = new Array();

    for (var i = 0; i < arguments.length; i++) {
        var element = arguments[i];
        if (typeof element == 'string')
            element = document.getElementById(element);

        if (arguments.length == 1)
            return element;

        elements.push(element);
    }

    return elements;
}

// Tells the ScriptManager that the script has been loaded
//   This is required on any external javascript files included as a reference
//   using the script manager object.
if (typeof (Sys) !== "undefined") {
    Sys.Application.notifyScriptLoaded();
}

