var version = 999; // we assume a sane browser    
if (navigator.appVersion.indexOf("MSIE") != -1)      // bah, IE again, lets downgrade version number      
    version = parseFloat(navigator.appVersion.split("MSIE")[1]);

String.prototype.beginsWith = function(t, i) {
    if (i == false) {
        return (t == this.substring(0, t.length));
    } else {
        return (t.toLowerCase() == this.substring(0, t.length).toLowerCase());
    }
}

String.prototype.endsWith = function(t, i) {
    if (i == false) {
        return (t == this.substring(this.length - t.length));
    } else {
        return (t.toLowerCase() == this.substring(this.length - t.length).toLowerCase());
    }
}

function popup(page) {
    var win = window.open(page, "_blank", "");
}

function resizeCorners() {
    if (version < 7) {
        $(".center_block").css("left", ($(window).width() * 0.05) + 'px');
        if ($(".center_block").width() < 600) {
            $(".center_block").css("width", '600px');
        }
        else {
            $(".center_block").css("width", '90%');
        }

        $(".top_left").each(function() {
            var main_offset = $(this).parent().find(".main").offset().left;
            var top_left_offset = $(this).offset().left;
            if (top_left_offset != main_offset) {
                $(this).css("left", (main_offset - top_left_offset) + 'px');
                $(this).parent().children(".bottom_left").css("left", (main_offset - top_left_offset) + 'px');
            }
        });
        $(".top_right").each(function() {
            $(this).css("left", ($(this).parent().width() - $(this).width()) + 'px');
        });
        $(".bottom_right").each(function() {
            $(this).css("left", ($(this).parent().width() - $(this).width()) + 'px');
            $(this).css("top", ($(this).parent().height() - $(this).height()) + 'px');
        });
        $(".bottom_left").each(function() {
            $(this).css("top", ($(this).parent().height() - $(this).height()) + 'px');
        });
    }
    //$('.backing').css("left", ($('.center_block').offset().left - 10)+'px');
    //$('.backing').css("width", ($('.center_block').width() + 20)+'px');
    //var backing_height = $('.center_block').height();
    //if($(window).height() > backing_height){
    //    backing_height = $(window).height();
    //}
    //$('.backing').css("height", backing_height +'px');
}

function createCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name, "", -1);
}

function setFont() {
    changeFontSize(0);
}

function fontSmall() {
    changeFontSize(-1);
}

function fontLarge() {
    changeFontSize(1);
}

var fontSize;
var changed = false;
function changeFontSize(newSize) {
    if (fontSize == null) {
        // try to get from the cookie
        cookieSize = readCookie('fesafont');
        if (fontSize == null) fontSize = 0;
        if (parseInt(cookieSize) < 0) fontSize = -1;
        if (parseInt(cookieSize) > 0) fontSize = 1;
    }

    //decide whether to increase the size. -1 means small, 0 means normal, 1 means large
    var oldSize = fontSize;
    if (newSize != null) {
        if (newSize < 0) {
            if (fontSize > 0) fontSize = 0; else fontSize = -1;
        }
        else if (newSize > 0) {
            if (fontSize < 0) fontSize = 0; else fontSize = 1;
        }
        createCookie('fesafont', fontSize, 0);
    }

    //set the difference to apply each time
    var diff = changed ? fontSize - oldSize : fontSize;

    //find all font tags with a size

    if (diff != 0) {
        var scaleSize = 1;
        if (fontSize < 0) {
            scaleSize = 0.8;
        }
        else if (fontSize > 0) {
            scaleSize = 1.2;
        }

        $('.main_block').css('font-size', scaleSize + 'em');
    }


    changed = true;
}

function printProperties(obj) {
    var output = "";
    for (var prop in obj) {
        if (obj[prop] != null) {
            output += prop + " = " + obj[prop] + "\n";
        }
    }
    alert(output);
}

function setCurrentDate(fieldName) {

    if (location.pathname.endsWith("NewForm.aspx")) {
        var now = new Date();
        var h = now.getHours();

        var ampm = "AM";
        if (h > 11) {
            h -= 12;
            ampm = "PM";
        }
        if (h == 0) {
            h = 12;
        }
        var hourVal = h + " " + ampm;
        var minuteVal = "" + Math.floor(now.getMinutes() / 5) * 5;


        $("TD.ms-formbody").each(function() {
            if ($(this).text().beginsWith(fieldName)) {
                $(this).find("TD.ms-dttimeinput").each(function() {
                    var timefields = $(this).find("SELECT");
                    var hours = timefields[0];
                    var minutes = timefields[1];

                    hours.value = hourVal;
                    if (minuteVal < 10) {
                        minutes.value = '0' + minuteVal;
                    }
                    else {
                        minutes.value = minuteVal;
                    }
                });
            }
        });
    }
}
