﻿//define console.log and console.debug for all browsers, only on localhost
if (!window.console){
    window.console = {
        debug : function(val){  
            if (isTesting)
                alert(val);
        },
        log : function(val){
            if (isTesting)
                alert(val);
        }
    }
}

function isTesting(){//if the url contains localhost, assume its the test environment
    return (location.href.indexOf("localhost") > 0);

}

function updateLayout(){
    var w = document.body.clientWidth;    
    var $c = $('#doc');
    if (w > 961){
        $c.css('width','960px');
    } else if (w < 771){
        $c.css('width','770px');
    } else {
        $c.css('width','100%');
    }
}

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>");
        
    });     
}

function updateLayoutForIE(){
    ua=navigator.userAgent.toLowerCase();
    if (ua.indexOf('msie 6.')!=-1 || ua.indexOf('msie 5.')!=-1){
        $(window).resize (updateLayout);
        updateLayout();    
    }
}

function convertToRoundedDecimal(val) {
    return val.replace(/[.][1346890]/g, '.').replace(/[.][2][012346789.]/g, '.25').replace(/[.][7][012346789.]/g, '.75').replace(/[.][5][0-9.]/g, '.5').replace(/[.][2][5][0-9.]/g, '.25').replace(/[.][7][5][0-9.]/g, '.75').replace(/[.][.]/g, '.');
}

$(document).ready(function() {
    /*@cc_on
    updateLayoutForIE();
    @*/
    giveShadow($("img.shd"));
    giveShadow($("div.shdfull"), true);
    giveShadow($("div.shd"));

    //on the date picker, output dates in "MM/DD/YYYY" format
    $.extend(DateInput.DEFAULT_OPTS, {
        stringToDate: function(string) {
            var matches;
            if (matches = string.match(/^(\d{1,2})\/(\d{1,2})\/(\d{4})$/)) {
                return new Date(matches[3], matches[1] - 1, matches[2]);
            } else {
                return null;
            };
        },
        dateToString: function(date) {
            var month = (date.getMonth() + 1).toString();
            var day = (date.getDate()).toString();
            var year = (date.getFullYear()).toString();
            return month + "/" + day + "/" + year;
        }
    });

    $("span.DateBox").find("input").date_input();
    $("input.decimal").keypress(function(event) {
        return keyRestrict(event, '1234567890.');
    }).keyup(function(event) {
        $(this).val(convertToRoundedDecimal($(this).val()));
    }).blur(function(event) {
        $(this).val(convertToRoundedDecimal($(this).val()));
        if ($(this).attr("SectionNum") != "") {
            ComputeSectionSum($(this).attr("SectionNum"));
        }
    });
    $("input.integer").keypress(function(event) {
        return keyRestrict(event, '1234567890');
    }).blur(function(event) {
        if ($(this).attr("SectionNum") != "") {
            ComputeSectionSum($(this).attr("SectionNum"));
        }
    });
    var ua = navigator.userAgent.toLowerCase();
    if (ua.indexOf("msie") != -1) {
        $(".PrintWarning").hide();
    }
});

//get parents siblings
jQuery.fn.uncle = function(sel) {
    return this.parent().siblings(sel);
};

//turn on "class on", and off "classOff"
jQuery.fn.switchClass = function(classOn, classOff) {
    this.each(function(){
        $(this).addClass(classOn);
        $(this).removeClass(classOff);
    })
    return this;
};

//Custom selector for selecting asp.net elements.  Usage $(":asp('ID')")
    //taken from: http://john-sheehan.com/blog/index.php/custom-jquery-selector-for-aspnet-webforms/
    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]);
    };
    
