﻿/// <reference path="jquery-1.2.6-vsdoc.js" />
/// <reference path="jquery.limittext.js" />
/// <reference path="jquery.date_input.fix.js" />
var keycodes = {
    backspace: 8,
    tab: 9,
    enter: 13,
    shift: 16,
    ctrl: 17,
    alt: 18,
    caps: 20,
    esc: 27,
    leftarrow : 37,
    uparrow : 38,
    rightarrow : 39,
    downarrow : 40
};

function updateLayout() {
    var w = document.body.clientWidth;
    var c = $('#doc')[0];
    if (w > 961) {
        c.style.width = '960px';
    } else if (w < 771) {
        c.style.width = '770px';
    } else {
        c.style.width = '100%';
    }
}

function addSelectInput($input, itemList, allowEnter) {
    $input.blur(function() {
        var $valInput = $input.siblings("input");
        $valInput.val($input.val());        
    });

    $input.limittext(itemList, {
        matchPartial: true,
        onMatch: function(found) {
            if (found) {
                $input.css({ color: '#000' });
            }

            else {
                $input.css({ color: '#f00' });
            }
        },

        onItemSelect: function(li) {
            $input.css({ color: '#000' });
        }
    })

    if (allowEnter === true) {
        // these inputs don't work if Enter submits a form - so return false on enter keypress
        $input.keypress(function(e) {
            if (e.keyCode == keycodes.enter) {
                return false;
            };
        });
    }
};


function removeNonNumeric(val) {
    if (val != undefined)
        return val.replace(/[^0-9\.]/g, "");
};

function updateLayoutForIE() {
    ua = navigator.userAgent.toLowerCase();
    if (ua.indexOf('msie 6.') != -1 || ua.indexOf('msie 5.') != -1) {
        $(window).resize(updateLayout);
        updateLayout();
    }
};

function giveShadow(el, isFullWidth) {
    el.each(function(i) {
        var c = (isFullWidth) ? ' fullsh' : '';
        $(this).wrap("<div class='sh'><div class='sh1" + c + "'><div class='sh2'><div class='sh3'><div class='sh4'><div class='sh5'><div class='sh6'></div></div></div></div></div></div></div>");

    });
}


//round "val" to "places" decimal places
function roundToDecimal(val, places) {
    return (Math.round(val * Math.pow(10, places)) / (Math.pow(10, places)));
};

function FormatCurrency(val) {
    //convert val to a string
    val = "" + val;
    val = removeNonNumeric(val);
    var cents = Math.round((val % 1) * 100);

    if (val.length <= 0)
        return "";

    if (cents < 10)
        cents = "0" + cents;

    return "$" + Math.floor(val) + "." + cents;
}


//jquery extensions - check/uncheck checkbox
// ex : $("input:checkbox").check();
$.fn.check = function() {
    var $boxes = $(this);
    $boxes.each(function() {
        $(this).attr("checked", "checked");
    });

    return $boxes;
}
// ex : $("input:checkbox").uncheck();
$.fn.uncheck = function() {
    var $boxes = $(this);
    $boxes.each(function() {
        $(this).removeAttr("checked");
    });

    return $boxes;
}

// ex : if ($("input:checkbox").ischecked())
$.fn.ischecked = function() {
    var $box = $(this);
    var checked = $box.attr("checked");
    return (checked == "checked" || checked);
}

// switches classes
// ex : $("test").switchClass("removeClass", "addClass");
$.fn.switchClass = function(fromClass, toClass) {
    var $box = $(this);
    $box.removeClass(fromClass);
    $box.addClass(toClass);
    return $box;
};


// client side validation for checkbox list
function IsCBLValid(val) {
    var $control = $("#" + val.controltovalidate);
    var numNeeded = parseInt(val.MinSelected);

    var selectedItemCount = 0;
    $control.find("input:checkbox").each(function() {
        var $checkbox = $(this);
        if ($checkbox.ischecked()) {
            selectedItemCount++;
        }
    });

    return selectedItemCount >= numNeeded;
}

$(function() {
    /*@cc_on
    updateLayoutForIE();
    @*/
    giveShadow($("img.shd"));
    giveShadow($("div.shd"));
    giveShadow($("div.shdfull"), true);

    $("table.thickbox-table:has(div)").addClass("noFormat");

    // :asp jquery selector
    //from: http://john-sheehan.com/blog/index.php/custom-jquery-selector-for-aspnet-webforms/
    // ex : $(":asp(ServerID)");
    String.prototype.endsWith = function(str) { return (this.match(str + '$') == str) };

    jQuery.expr[":"].asp = function(a, i, m) { return (id = jQuery(a).attr('id')) && id.endsWith(m[3]); };
    jQuery.expr[":"].containsNoCase = function(el, i, m) {
        var search = m[3]; if (!search) return false;
        return eval("/" + search + "/i").test($(el).text());
    };

});

// begin app_functions
// JScript File
function SessionTimeout(milli, btnLogout) {
    if (milli != -1) setTimeout(function() { Post(btnLogout) }, milli);
}

function Post(bl) {
    document.title = 'Your session is about to timeout';
    var btnLogout = document.getElementById(bl);
    $(":asp(divTimeoutWarning)").show();
    $("#content").hide();
    setTimeout(function() { btnLogout.click(); }, 18000);
}

function ShowElement(controlName, doEffect) {
    c = $("#" + controlName);
    if (doEffect) {
        if (c[0].style.display == 'none') {
            c.fadeIn("fast");
        }
    } else {
        c[0].style.display = '';
    }
}

function HideElement(controlName, doEffect) {
    c = $("#" + controlName);
    if (doEffect) {
        if (c[0].style.display != 'none') {
            c.fadeOut("fast");
        }
    } else {
        c[0].style.display = "none";
    }
}

function vShowElement(controlName) {
    document.getElementById(controlName).style.visibility = "visible";
}

function vHideElement(controlName) {
    document.getElementById(controlName).style.visibility = "hidden";
}

function ShowElement(controlName) {
    var el = document.getElementById(controlName);
    if (el != null) {
        el.style.display = "";
    }
}

function HideElement(controlName) {
    var el = document.getElementById(controlName);
    if (el != null) {
        el.style.display = "none";
    }
}

function RefreshAnimatedGif(controlName, imageSrc) {
    if (navigator.appName == "Microsoft Internet Explorer")
        document.getElementById(controlName).style.backgroundImage = "url(" + imageSrc + ")";
    //document.getElementById(controlName).style.backgroundImage = document.getElementById(controlName).style.backgroundImage;

    vShowElement(controlName);
}

function EnableValidator(validatorid, controlid, enable) {
    if (enable == null) {
        document.getElementById(element).enabled = isEnabled;
    }
    else {
        // validatorenable calls Validate() - but we don't want the error text to show until a focus exit or submit click
        var val = document.getElementById(controlid).value; // save orig value
        document.getElementById(controlid).value = '0'; // make sure a valid value is there for nested validate call
        try// no validatorenable() function in some browsers
        {
            ValidatorEnable(document.getElementById(validatorid), enable);
        } catch (e) { }
        document.getElementById(controlid).value = val; // change value back
    }
}

function DisplayElement(element, display) {
    document.getElementById(element).style.display = display;
}
function Trim(strToTrim) {
    return strToTrim.replace(/^\s+|\s+$/g, '');
}
//*** FOR NUMERIC TEXTBOXES
//
function getKeyCode(e) {
    if (window.event)
        return window.event.keyCode;
    else if (e)
        return e.which;
    else
        return null;
}

function keyRestrict(e, validchars) {
    var key = '', keychar = '';
    key = getKeyCode(e);
    if (key == null) return true;
    keychar = String.fromCharCode(key);
    keychar = keychar.toLowerCase();
    validchars = validchars.toLowerCase();
    if (validchars.indexOf(keychar) != -1)
        return true;
    if (key == null || key == 0 || key == 8 || key == 9 || key == 13 || key == 27)
        return true;
    return false;
}
//Syntax:  onKeyPress="return keyRestrict(event,'1234567890')"
//
function getKeyCode(e) {
    if (window.event)
        return window.event.keyCode;
    else if (e)
        return e.which;
    else
        return null;
}
function keyRestrict(e, validchars) {
    var key = '', keychar = '';
    key = getKeyCode(e);
    if (key == null) return true;
    keychar = String.fromCharCode(key);
    keychar = keychar.toLowerCase();
    validchars = validchars.toLowerCase();
    if (validchars.indexOf(keychar) != -1)
        return true;
    if (key == null || key == 0 || key == 8 || key == 9 || key == 13 || key == 27)
        return true;
    return false;
}
function keyAllow(e, invalidchars) {
    var key = '', keychar = '';
    key = getKeyCode(e);
    if (key == null) return true;
    keychar = String.fromCharCode(key);
    keychar = keychar.toLowerCase();
    invalidchars = invalidchars.toLowerCase();
    if (invalidchars.indexOf(keychar) == -1)
        return true;
    if (key == null || key == 0 || key == 8 || key == 9 || key == 13 || key == 27)
        return true;
    return false;


}

/*    Escape function   */
String.prototype.addslashes = function() {
    return this.replace(/(["\\\.\|\[\]\^\*\+\?\$\(\)])/g, '\\$1');
}
String.prototype.trim = function() {
    return this.replace(/^\s*(\S*(\s+\S+)*)\s*$/, "$1");
};
/* --- Escape --- */

/* Offset position from top of the screen */
function curTop(obj) {
    toreturn = 0;
    while (obj) {
        toreturn += obj.offsetTop;
        obj = obj.offsetParent;
    }
    return toreturn;
}
function curLeft(obj) {
    toreturn = 0;
    while (obj) {
        toreturn += obj.offsetLeft;
        obj = obj.offsetParent;
    }
    return toreturn;
}
/* ------ End of Offset function ------- */

/* Types Function */

// is a given input a number?
function isNumber(a) {
    return typeof a == 'number' && isFinite(a);
}

/* Object Functions */

var Color = new Array(); Color[1] = "ff"; Color[2] = "ee"; Color[3] = "dd"; Color[4] = "cc"; Color[5] = "bb"; Color[6] = "aa"; Color[7] = "99";
function highlightFade(elString) {
    $(elString).each(function() {
        this.style.backgroundColor = "#ffff99";
        setTimeout("colorFade(7, '" + this.id + "')", 1000);
    });

}

function colorFade(where, id) {
    if (where >= 1) {
        document.getElementById(id).style.backgroundColor = "#ffff" + Color[where];
        if (where > 1) {
            where -= 1;
            setTimeout("colorFade(" + where + ", '" + id + "')", 200);
        } else {
            where -= 1;
            setTimeout("colorFade(" + where + ", '" + id + "')", 200);
            document.getElementById(id).style.backgroundColor = "transparent";
        }
    }
}
$(function() {
    highlightFade("div.blink");
});