﻿
// Blocks characters from TextBox where numbers are only valid input
function blockNonNumbers(obj, e, allowDecimal, allowNegative) {
    var key;
    var isCtrl = false;
    var keychar;
    var reg;

    if (window.event) {
        key = e.keyCode;
        isCtrl = window.event.ctrlKey
    }
    else if (e.which) {
        key = e.which;
        isCtrl = e.ctrlKey;
    }

    if (isNaN(key)) return true;

    keychar = String.fromCharCode(key);

    // check for backspace or delete, or if Ctrl was pressed
    if (key == 8 || key == 13 || isCtrl) {
        return true;
    }

    reg = /\d/;
    var isFirstN = allowNegative ? keychar == '-' && obj.value.indexOf('-') == -1 : false;
    var isFirstD = allowDecimal ? keychar == '.' && obj.value.indexOf('.') == -1 : false;

    return isFirstN || isFirstD || reg.test(keychar);
}

// This function also blocks chars but works when Ajax is not in use
function blockChar(tb) {
    tb.value = tb.value.replace(/[^0-9]/g, ''); // strip non-digit chars
}



// Helper for assigning radio buttons groups when used within a repeater
function SetUniqueRadioButton(nameregex, current) {
    re = new RegExp(nameregex);
    for (i = 0; i < document.forms[0].elements.length; i++) {
        elm = document.forms[0].elements[i];
        if (elm.type == 'radio' && re.test(elm.name)) elm.checked = false;
    }
    current.checked = true;
}


// Gets all elements in the document by tag and class
function getElementsByTagAndClass(tagName, className)
{
    var elementArray = new Array();
    var inputs = document.getElementsByTagName(tagName);
    for (var x = 0; x < inputs.length; x++)
    {
        if (inputs[x].className.toLowerCase() == className.toLowerCase())
        {
            elementArray.push(inputs[x]);
        }
    }
    return elementArray;
}


    
// Do not allow return key to submit the form
// NOTE: this function is used in the OnKeyPress event which is not documented on asp:TextBox
function SuppressEnter(e)
{
    // Get the key that was pressed
    var keyCode = e.keyCode ? e.keyCode : e.which ? e.which : e.charCode;
    // Check for return key
    if (keyCode == 13) 
    {
        return false;
    } 
    return true;
}
     
     
// This script disables a button after
// it is selected.
function DisableButton(obj) 
{
    obj.setAttribute('disabled', true);
}


// Enables or disables a form field given its full ClientID
function ToggleFieldEditAbility(enabled, fullControlName)
{
    var element = document.getElementById(fullControlName);
    if (element != null)
    {
        element.disabled = !enabled;
    }
}

// Cleans a string and casts is to a number 
function CleanToNumber(numberAsString)
{
    return Number(numberAsString.replace(/[^0-9.]/g, ''));
}

function CleanNumberToString(number)
{
    return number.replace(/[^0-9]/g, '');
}

// Toggles an element's display between block and none
function ToggleElementDisplay(elementToToggle)
{
    if (elementToToggle.style.display == 'none')
    {
        elementToToggle.style.display = 'block';
    }
    else
    {
        elementToToggle.style.display = 'none';
    }
}

// Make an element visible or not
function SetElementVisiblity(theElement, turnOn)
{                

    theElement = document.getElementById(theElement);
    if(theElement != null && turnOn == true)
    {                      
        theElement.style.display = 'inline';
        theElement.style.visibility="visible";
    }
    else if(theElement != null)
    {
        theElement.style.display = 'none';
        theElement.style.visibility = "hidden";
    }
}
                
//check for firefox 
function IsFireFox()
{                                       
    if (/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent))
    { 
        return true;
    }
    
    return false;
}

// Adds one Option to a dropDownList/Select
function AddOption(ddl, ddlValue, ddlText, ddlSelected, ddlDisabled)
{
    var option = document.createElement('OPTION');
    option.value = ddlValue;
    option.text = ddlText;
    option.selected = ddlSelected;  
    ddl.disabled = ddlDisabled;
    
    if(IsFireFox() == true)
    {
        ddl.add(option, null);
    }
    else
    {
        ddl.add(option);
    }
}

function ResetDropDown(dropDown, theDiv)
{
    //remove all items from dropdown first
    while (dropDown.options.length > 0)
    {
        dropDown.remove(0);
    }  
    
    AddOption(dropDown, '0', 'No items at this level', true, true);
    
    if(theDiv != null)
    {
        theDiv.style.display = 'none';
    }
}

// Highlight all the columns in a row with the specified color
function hlRow(obj,color){
	var array = obj.childNodes;
	for (i=0; i<array.length; i++){
		array[i].style.backgroundColor = color;
		//array[i].style.borderBottom = "1px solid #eee5d1";
	}
}

function MM_findObj(n, d) { //v4.01
    var p, i, x; if (!d) d = document; if ((p = n.indexOf("?")) > 0 && parent.frames.length) {
        d = parent.frames[n.substring(p + 1)].document; n = n.substring(0, p);
    }
    if (!(x = d[n]) && d.all) x = d.all[n]; for (i = 0; !x && i < d.forms.length; i++) x = d.forms[i][n];
    for (i = 0; !x && d.layers && i < d.layers.length; i++) x = MM_findObj(n, d.layers[i].document);
    if (!x && d.getElementById) x = d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
    var i, j = 0, x, a = MM_swapImage.arguments; document.MM_sr = new Array; for (i = 0; i < (a.length - 2); i += 3)
        if ((x = MM_findObj(a[i])) != null) { document.MM_sr[j++] = x; if (!x.oSrc) x.oSrc = x.src; x.src = a[i + 2]; }
}

function MM_swapImgRestore() { //v3.0
    var i, x, a = document.MM_sr; for (i = 0; a && i < a.length && (x = a[i]) && x.oSrc; i++) x.src = x.oSrc;
}

function MM_preloadImages() { //v3.0
    var d = document; if (d.images) {
        if (!d.MM_p) d.MM_p = new Array();
        var i, j = d.MM_p.length, a = MM_preloadImages.arguments; for (i = 0; i < a.length; i++)
            if (a[i].indexOf("#") != 0) { d.MM_p[j] = new Image; d.MM_p[j++].src = a[i]; }
    }
}

function formatPhoneNumber(number) {
    var formatted = '';
    if (number != null && number != '') {
        var index = 0;
        if (number.length == 11) {
            formatted += number.substring(index, 1) + ' ';
            index++;
        }
        formatted += '(' + number.substring(index, index + 3) + ') ' + number.substring(index + 3, index + 6) + '-' + number.substring(index + 6);
    }

    return formatted;
}

function login(url) {
    if (url != '') {
        window.open(url);
    }
    else {
        alert('The login information could not be retrieved.');
    }
}



// Inline popup helpers

// Clients that use this script can define the following in order to override positioning of the popup
// var xOffset = 20;
// var yOffset = 5;
function changeObjectVisibility(objectId, newVisibility) {
    // get a reference to the cross-browser style object and make sure the object exists
    var styleObject = document.getElementById(objectId);
    if (styleObject) {
        styleObject.style.visibility = newVisibility;
        return true;
    } else {
        // we couldn't find the object, so we can't change its visibility
        return false;
    }
} // changeObjectVisibility

function moveObject(objectId, newXCoordinate, newYCoordinate) {
    // get a reference to the cross-browser style object and make sure the object exists
    var styleObject = document.getElementById(objectId);
    if (styleObject) {
        styleObject.style.left = newXCoordinate + 'px';
        styleObject.style.top = newYCoordinate + 'px';
        return true;
    } else {
        // we couldn't find the object, so we can't very well move it
        return false;
    }
} // moveObject



function showPopup(targetObjectId, eventObj) {
    // Get offsets, if overridden
    var finalXOffset = window.xOffset ? xOffset : 20;
    var finalYOffset = window.yOffset ? yOffset : 5;

    if (eventObj) {
        // hide any currently-visible popups
        hideCurrentPopup();
        // stop event from bubbling up any farther
        eventObj.cancelBubble = true;
        // move popup div to current cursor position 
        var posx = 0;
        var posy = 0;
        if (!eventObj) var e = window.event;
        if (eventObj.pageX || eventObj.pageY) {
            posx = eventObj.pageX;
            posy = eventObj.pageY;
        }
        else if (eventObj.clientX || eventObj.clientY) {
            posx = eventObj.clientX + document.body.scrollLeft + document.documentElement.scrollLeft + finalXOffset;
            posy = eventObj.clientY + document.body.scrollTop + document.documentElement.scrollTop + finalYOffset;
        }
        // posx and posy contain the mouse position relative to the document
        moveObject(targetObjectId, posx, posy);
        // and make it visible
        if (changeObjectVisibility(targetObjectId, 'visible')) {
            // if we successfully showed the popup
            // store its Id on a globally-accessible object
            window.currentlyVisiblePopup = targetObjectId;
            return true;
        } else {
            // we couldn't show the popup, boo hoo!
            return false;
        }
    } else {
        // there was no event object, so we won't be able to position anything, so give up
        return false;
    }
} // showPopup

function hideCurrentPopup() {
    // note: we've stored the currently-visible popup on the global object window.currentlyVisiblePopup
    if (window.currentlyVisiblePopup) {
        changeObjectVisibility(window.currentlyVisiblePopup, 'hidden');
        window.currentlyVisiblePopup = false;
    }
} // hideCurrentPopup



if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();