﻿//  Utilities for dealing with image buttons.
var btns = new Object();
var edits = new Object();
var btnSubmitLcl;
var keyEscape = 27;

//  Register an image button along with its images.
function RegisterImgBtn(btn, submit, buttonWidth) {
    var btnDesc = new Object();

    if (btn != null) {
        btnDesc.control = btn;
        btnDesc.submit = submit;
        btnDesc.buttonWidth = buttonWidth;

        btns[btn.name] = btnDesc;

        btn.onmouseout = function() { UpdImgBtns(this, false); }
        btn.onmouseover = function() { UpdImgBtns(this, true); }
        btn.onblur = function() { UpdImgBtns(this, false); }
        btn.onfocus = function() { UpdImgBtns(this, true); }

        if (submit == true)
            btnSubmitLcl = btn;
    }
}

//UPDATE:  BEC-09/03/10-Added condition to check for null edit object.  
//When inside Wizard control, controls are initialized but not rendered.  
//Meaning that this function is called by Kimball.Web.Controls.
function RegisterEdit(edit, required, multiline, span, max) {
    if (edit != null) {
        var editDesc = new Object();
        editDesc.control = edit;
        editDesc.required = required;
        edits[edit.name] = editDesc;

        //if span and max exist, load into object for future use.
        var showRemainingEdit = null;
        if (span != null || max != null) {
            //load span and max into object
            showRemainingEdit = new Object;
            showRemainingEdit.span = span;
            showRemainingEdit.max = max;
            showRemainingEdit.control = edit;                 
        }

        if (multiline == true) {
            edit.onkeyup = function() { OnKeyUp(true, showRemainingEdit); }
        }
        else
            edit.onkeyup = function() { OnKeyUp(false, showRemainingEdit); }
    }
}

//  Enable/disable an image button.
function EnableImgBtn(btn, enabled) {
    if (btn != null) {
        btn.disabled = !enabled;
        UpdImgBtns(btn, false);
    }
}

//  Update an image button based on its current state.
function UpdImgBtns(btn, mouseOver) {
    for (btnName in btns) {
        var btnDesc = btns[btnName];
        var btnLoop = btnDesc.control;
        var newClass = "btnNormal";

        if (btnLoop.disabled)
            newClass = "btnDisabled";
        else {
            if (mouseOver == true)
                newClass = (btnLoop.name == btn.name) ? "btnMouseOver" : "btnNormal"; 
            else
                newClass = (btnDesc.submit == true) ? "btnMouseOver" : "btnNormal"; 
        } 
        
        newClass += btnDesc.buttonWidth;
        
        if (btnLoop.className != newClass)
            btnLoop.className = newClass; 
    }
}

//  Generic keyboard handler to deal with required fields and submit image 
//  buttons.
function OnKeyUp(multiline, showRemainingEdit) {
    if (multiline == false && DoSubmit() == true)
        return;

    //check to see if object exists, and update remaining characters for multiline with maxlength set
    if (showRemainingEdit) {
        if (showRemainingEdit.max > 0) {
            showRemainingText(showRemainingEdit.control, showRemainingEdit.span, showRemainingEdit.max);
        }
    }
    
    //  Check for required fields on the form only if we have a submit button.
    if (btnSubmitLcl == null)
        return;          
    
    //  Enable the submit button if and only if the user has supplied all of 
    //  the required fields on the form.
    for(editName in edits) {
        var editDesc = edits[editName];        
        
        if (editDesc.required == true) {
            if (Trim(editDesc.control.value).length == 0) {
                EnableImgBtn(btnSubmitLcl, false);
                return;
            }
        }
    }
        
    EnableImgBtn(btnSubmitLcl, true);
}

//  Handle an <enter> as a submit button click.
function DoSubmit(evt) {
    evt = (evt) ? evt : ((window.event) ? window.event : "");

    if (evt) {
        if (btnSubmitLcl != null && btnSubmitLcl.disabled == false && evt.keyCode == 13) {
            evt.cancelBubble = true;
            evt.returnValue = false;
            btnSubmitLcl.click();
            return true;
        }
    }
    
    return false;
}

//  Show or hide a document element.
function ShowElement(element, show) {
    if (element != null && element.style != null)
        element.style.display = (show == true) ? "block" : "none";
}

//  Show or hide a document element by name.
function ShowElementByName(elementName, show) {
    var element = document.getElementById(elementName);

    ShowElement(element, show);
}

//  Set the cursor for the browser window of the current page.
function SetPageCursor(cursorName) {
    document.body.style.cursor = cursorName;
}

//  Select the text in a textbox.
function SelectText(ctrl) {
    if (ctrl.select != null)
        ctrl.select();
}

//  Get the "wrapper" window for a RadWindow popup window.
function GetRadWindow() {
    //  The current window is not a Rad window.
    if (window.radWindow == null && window.frameElement == null)
        return null;

    //  Will work in Moz in all cases, including clasic dialog
    if (window.radWindow)
        return window.radWindow;

    //  IE (and Moz as well)
    if (window.frameElement.radWindow)
        return window.frameElement.radWindow;

    return null;
}

//  Open a RadWindow based on the manager name, URL, and window name.
function OpenRadWindow(mgrName, url, windowName, width, height, center) {
    var wMgr = $find(mgrName);
    var wnd = wMgr.getWindowByName(windowName);

    wnd.setUrl(url);

    if (width != null && height != null)
        wnd.setSize(width, height);

    if (center == true)
        wnd.center();

    wnd.show();
}

//  Close a RadWindow optionally passing an '|' delimited arguement list to the 
//  callback method supplied in the markup for the window.
function CloseRadWindow() {    
    var winOpener = GetRadWindow();

    if (arguments.length == 0) {
        winOpener.argument = null;
        winOpener.close();
        return;
    }
    
    var callbackArgs = "";

    for (var idx = 0; idx < arguments.length; idx++) {
        if (callbackArgs.length > 0)
            callbackArgs += "|";

        if (arguments[idx] != null) callbackArgs += arguments[idx];
    }
    
    winOpener.close(callbackArgs);
}

//  Common split routine to return multiple results from a RadWindow popup dialog.
function GetRadWindowDialogResults(eventArgs, separatorChar) {
    _dialogResults = null;

    var callbackArgs = eventArgs.get_argument();

    if (callbackArgs != null) {
        //  Default separator character is an "|", but can be overriden by passing 
        //  the second, optional, param to this function.
        if (separatorChar == null)
            separatorChar = "|";

        _dialogResults = callbackArgs.split(separatorChar)
    }

    return _dialogResults;
}

//Shows the remaining allowable chars based off the MaxLength property.
//PARAMS:   1) input=text box.
//          2) span=Span to display remaining chars in.
//          3) max=maximum number of characters allowed.
function showRemainingText(input, span, max) {
    var totalChars = input.value.length;
    
    if (max < totalChars) {
        input.value = input.value.substring(0, max);
        totalChars = input.value.length;
    }

    window.setTimeout(function() {
        var spn = document.getElementById(span);
        spn.innerHTML = (max - totalChars); //update <SPAN>
    }, 1);        
}
