
/*
Written by Ron Clabo
This file is intended to house common routines that can be used
to improve the UI experience.
*/

jQuery.noConflict();        /* turn off jquery use of $ so prototype can use it*/
var $j = jQuery;            /* setup $j as a shorthand way to reference jquery*/


//Used to show or hid a layer
//params: string divId, bit visible (pass 1 or 0 for visible)
function showLayer(divId, visible) {
    var divObj = document.getElementById(divId);
    if (visible) {
        divObj.style.display = 'block';
    } else {
        divObj.style.display = 'none';
    }
}



/*
 FocusMgr restores field focus after update panel partial update.
 Needed because update panel replaces fields with new ones by setting div.innerHTML
 The focus handeling code is based on:
  http://couldbedone.blogspot.com/2007/08/restoring-lost-focus-in-update-panel.html
  which apparently is based on ScriptManager.SetFocus() code.
*/

var Lib = new Object();

Lib.initFocusMgr = function() {
    function focusHandler(e) {
        document.activeElement = e.originalTarget;
    }
    function pageLoadingHandler(sender, args) {
        Lib.lastFocusedControlId = "";
        if (typeof (document.activeElement) !== "undefined") {
            Lib.lastFocusedControlId = document.activeElement.id;
        }
    }

    function pageLoadedHandler(sender, args) {
        if (typeof (Lib.lastFocusedControlId) !== "undefined" && Lib.lastFocusedControlId != "") {
            var ctlObj = $get(Lib.lastFocusedControlId);
            if (ctlObj) {
                Lib.setFocus(ctlObj);
                Lib.moveToEndOfTextField(ctlObj);
            }
        }
    }

    if (typeof (window.addEventListener) !== "undefined") { // !== undefined also checks against null, while === doesn't 
        window.addEventListener("focus", focusHandler, true);
    }
    Sys.WebForms.PageRequestManager.getInstance().add_pageLoading(pageLoadingHandler);
    Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(pageLoadedHandler);
}

Lib.lastFocusedControlId = "";

Lib.setFocus = function(ctlObj) {
    if (Sys.Browser.agent === Sys.Browser.InternetExplorer) {
        if (ctlObj.isDisabled == true) {
            return;     //otherwise it'll throw an error when we call focus()
        }

        var specialWork = false;
        var origValue;
        if (ctlObj && (typeof (ctlObj.contentEditable) !== "undefined")) {
            specialWork = true;
            origValue = ctlObj.contentEditable;
            ctlObj.contentEditable = false;
        }
        ctlObj.focus();
        if (specialWork) {
            ctlObj.contentEditable = origValue;
        }
    }
    else {
        ctlObj.focus();
    }
}

Lib.moveToEndOfTextField = function(ctlObj) {
    if (ctlObj.type != "text") {
        return;
    }

    if (ctlObj.isDisabled == true) {
        return;
    }

    if (ctlObj.createTextRange) {                //for ie
        var range = ctlObj.createTextRange();
        range.moveStart('character', (ctlObj.value.length));
        range.collapse();
        range.select();

    } else if (ctlObj.setSelectionRange) {        //for firefox & safari
        ctlObj.setSelectionRange(ctlObj.value.length, ctlObj.value.length);
    }

}

//---End Focus Handeling code---


Lib.newVisitor = function() {
    function asyncResult(jsonData) {
        return;
    }

    $j.post("/ajax.aspx", { reqType: "newVisitor", infoStr: "" }, asyncResult, "text");
    return false;
}



