﻿function hex2int(htal) {
    return parseInt(htal, 16);
}

function doublehex2int(htal) {
    return (hex2int(htal.substr(0, 1)) * 16 + hex2int(htal.substr(1, 1)));
}

function int2hex(ital) {
    return ital.toString(16);
}

function doubleint2hex(ital) {
    return (int2hex(parseInt(ital / 16)) + int2hex(ital % 16));
}

function makeLightColor(iColor, lightpct) {
    var colorRed, colorGreen, colorBlue;
    var lightRed, lightGreen, lightBlue;

    colorRed = doublehex2int(iColor.substr(1, 2));
    colorGreen = doublehex2int(iColor.substr(3, 2));
    colorBlue = doublehex2int(iColor.substr(5, 2));

    lightRed = 255 - parseInt((((255 - colorRed) / 255) * (1 - lightpct / 100)) * 255);
    lightGreen = 255 - parseInt((((255 - colorGreen) / 255) * (1 - lightpct / 100)) * 255);
    lightBlue = 255 - parseInt((((255 - colorBlue) / 255) * (1 - lightpct / 100)) * 255);

    return ("#" + doubleint2hex(lightRed) + doubleint2hex(lightGreen) + doubleint2hex(lightBlue));
}

function makeDarkColor(iColor, darkpct) {

    if (darkpct > 0) {
        var colorRed, colorGreen, colorBlue;
        var darkRed, darkGreen, darkBlue;

        colorRed = doublehex2int(iColor.substr(1, 2));
        colorGreen = doublehex2int(iColor.substr(3, 2));
        colorBlue = doublehex2int(iColor.substr(5, 2));

        darkRed = parseInt(((colorRed / 255) * (1 - darkpct / 100)) * 255);
        darkGreen = parseInt(((colorGreen / 255) * (1 - darkpct / 100)) * 255);
        darkBlue = parseInt(((colorBlue / 255) * (1 - darkpct / 100)) * 255);

        return ("#" + doubleint2hex(darkRed) + doubleint2hex(darkGreen) + doubleint2hex(darkBlue));
    }
    else {
        
        return(makeLightColor(iColor, Math.abs(darkpct)));
    }
}

function makeTextColor(iColor) {
    var colorRed, colorGreen, colorBlue;
    colorRed = doublehex2int(iColor.substr(1, 2));
    colorGreen = doublehex2int(iColor.substr(3, 2));
    colorBlue = doublehex2int(iColor.substr(5, 2));
    if (colorBlue + colorGreen + colorRed < 512) {
        return ("#FFFFFF");
    }
    else {
        return ("#000000");
    }
}

function removeallchildnodes(oDiv) {
    while (oDiv.hasChildNodes()) {
        oDiv.removeChild(oDiv.lastChild);
    }
}

function gradientbarbuet(divBar, topColor, barheight, gradientpct) {

    //removeallchildnodes(divBar);

    for (var i = 0; i < (barheight / 2); i++) {

        var oDiv = document.createElement("DIV");
        divBar.appendChild(oDiv);
        oDiv.style.width = "100%";
        oDiv.style.fontSize = "1px";    //Bugfix, else height is 15px on ie
        oDiv.style.height = "2px";
        oDiv.style.backgroundColor = topColor;

        topColor = makeDarkColor(topColor, gradientpct / 2);
    }
}

function gradientbarkantet(divBar, topColor, barheight, gradientpct) {

    var halfbar = barheight / 2;
    var thiscolor = makeLightColor(topColor, 20);

    for (var i = 0; i < (parseInt(halfbar / 2)); i++) {

        var oDiv = document.createElement("DIV");
        divBar.appendChild(oDiv);
        oDiv.style.width = "100%";
        oDiv.style.fontSize = "1px";    //Bugfix, else height is 15px on ie
        oDiv.style.height = "2px";
        oDiv.style.backgroundColor = thiscolor;

        thiscolor = makeDarkColor(thiscolor, gradientpct / 2);
    }

    thiscolor = makeDarkColor(topColor, 20);

    for (var i = 0; i < (halfbar-parseInt(halfbar / 2)); i++) {

        var oDiv = document.createElement("DIV");
        divBar.appendChild(oDiv);
        oDiv.style.width = "100%";
        oDiv.style.fontSize = "1px";    //Bugfix, else height is 15px on ie
        oDiv.style.height = "2px";
        oDiv.style.backgroundColor = thiscolor;

        thiscolor = makeLightColor(thiscolor, gradientpct / 2);
    }
}


