Utils = {};

Utils.isIE = function() {
    return navigator.appName.indexOf('Microsoft') > -1;
}

Utils.isIE7 = function() {
    return navigator.userAgent.indexOf("MSIE 7.") > -1;
}

Utils.getPosition = function(pixelPos) {
    return parseInt(pixelPos.replace("px", ""));
}

Utils.justSwapImg = function(imgSrc, id) {
    document.getElementById(id).src = imgSrc;
}

Utils.swapBackground = function(src, id) {
    document.getElementById(id).style.background = "url(" + src + ")";
}

Utils.toggleShowLayer = function(id) {
    if (document.getElementById(id).style.display == 'none' || document.getElementById(id).style.display == '') {
        document.getElementById(id).style.display = 'block';
    } else {
        document.getElementById(id).style.display = 'none';
    }
}

Utils.showLayer = function(id) {
    document.getElementById(id).style.display = "block";
}
Utils.hideLayer = function(id) {
    document.getElementById(id).style.display = "none";
}

Utils.clearInputBox = function(id) {
    if (document.getElementById(id).value == "Search") {
        document.getElementById(id).value = "";
    }
}

Utils.resetInputBox = function(id, string) {
    if (document.getElementById(id).value == "") {
        document.getElementById(id).value = string;
    }
}


Utils.getWindowWidth = function() {
    if (Utils.isIE()) {
        winW = document.body.offsetWidth;
    } else {
        winW = window.innerWidth;
    }
    return winW;
}

Utils.getWindowHeight = function() {
    if (Utils.isIE()) {
        winH = document.body.offsetHeight;
    } else {
        winH = window.innerHeight;
    }
    return winH
}

Utils.enableStyleSheet = function(name, on) {
    for (var i = 0; (a = document.getElementsByTagName("link")[i]); i++) {
        if (a.getAttribute("rel") &&
            a.getAttribute("rel").indexOf("style") != -1 &&
            a.getAttribute("title")) {
            if (a.getAttribute("title") == name) {
                a.disabled = !on;
                Utils.createCookie(name, on, 365);
            }
        }
    }
}

Utils.createCookie = function (name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}

Utils.readCookie = function (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;
}


Utils.enableStyleSheet("loRes", Utils.readCookie("loRes") != null && Utils.readCookie("loRes") == 'true');


Utils.setCheckboxes = function(name, value) {
    var boxes = document.getElementsByName(name);
    for (var i = 0; i < boxes.length; i++) {
        boxes[i].checked = value;
    }
}

Utils.randomString = function(string_length) {
    var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
    var randomstring = '';
    for (var i = 0; i < string_length; i++) {
        var rnum = Math.floor(Math.random() * chars.length);
        randomstring += chars.substring(rnum, rnum + 1);
    }
    return randomstring;
}


//these shuold all really be prefix Util.
function dropDown(call2action) {
    if (document.getElementById(call2action).className == "none") {
        document.getElementById('tab2Area').className = "none"
        document.getElementById('tab3Area').className = "none"
        document.getElementById(call2action).className = "block";
    }
    else {
        document.getElementById(call2action).className = "none";
    }
}

tabs = ['tab1','tab2','tab3'];
function showHideTabs(id) {
    for (var i = 0; i < tabs.length; i++) {
        if (i==0) {
            document.getElementById(tabs[i] + "Btn").style.borderLeft = "1px solid #666666";
        }
        if (i==2) {
            document.getElementById(tabs[i] + "Btn").style.borderRight = "1px solid #666666";
        }
        if (document.getElementById(tabs[i] + "Area") != null) {
            document.getElementById(tabs[i] + "Area").className = "none";
            document.getElementById(tabs[i] + "Btn").className = "nonactive";
            document.getElementById(tabs[i] + "Btn").style.background = "#d0d1d3";
            if (i==1 && id == 'tab1') {
                document.getElementById(tabs[i] + "Btn").style.borderLeft = 'none';
                document.getElementById(tabs[i] + "Btn").style.borderRight = "1px solid #666666";
            } else if (i==1 && id == 'tab3') {
                document.getElementById(tabs[i] + "Btn").style.borderRight = 'none';
                document.getElementById(tabs[i] + "Btn").style.borderLeft = "1px solid #666666";
            } else if (i==1) {
                document.getElementById(tabs[i] + "Btn").style.borderRight = '1px solid #000000';
                document.getElementById(tabs[i] + "Btn").style.borderLeft = "1px solid #000000";
            }
            document.getElementById(tabs[i] + "Btn").style.color = "#666666";
        }
    }
    document.getElementById(id + "Area").className = "block";
    document.getElementById(id + "Btn").className = "active";
    document.getElementById(id + "Btn").style.background = "none";
}
function showHideBlock(blockId) {
    if (document.getElementById(blockId).className == 'none') {
        document.getElementById(blockId).className = 'block';
    } else {
        document.getElementById(blockId).className = 'none';
    }
}

function hideBlock(blockId) {
    document.getElementById(blockId).className = 'none';
}

function showBlock(blockId) {
    document.getElementById(blockId).className = 'block';
}

box = ['sendtoafriendArea','commentsArea','Comments.displayPost'];
function showHide(id) {
    for (var i = 0; i < tabs.length; i++) {
        if (document.getElementById(box[i]) != null) {
            document.getElementById(box[i]).className = "none";
        }
    }

    document.getElementById(id).className = "block";
}
function clearInputBox(id) {
    if (document.getElementById(id).value == "Search") {
        document.getElementById(id).value = "";
    }
}


