    // http://www.csgnetwork.com/csgcolorsel4.html
    function getObjectByID(id) {
        // Cross-browser function to return the object with the specific id
        if (document.all) { // IE
            return document.all[id];
        } else { // Mozilla and others
            return document.getElementById(id);
        }
    }

    function HSVobject(hue, saturation, value) {
        // Object definition.
        this.h = hue; this.s = saturation; this.v = value;
        this.validate = function() {
            if (this.h <= 0) { this.h = 0; }
            if (this.s <= 0) { this.s = 0; }
            if (this.v <= 0) { this.v = 0; }
            if (this.h > 360) { this.h = 360; }
            if (this.s > 100) { this.s = 100; }
            if (this.v > 100) { this.v = 100; }
        };
    }

    function RGBobject(red, green, blue) {
        // Object definition.
        this.r = red; this.g = green; this.b = blue;
        this.validate = function() {
            if (this.r <= 0) { this.r = 0; }
            if (this.g <= 0) { this.g = 0; }
            if (this.b <= 0) { this.b = 0; }
            if (this.r > 255) { this.r = 255; }
            if (this.g > 255) { this.g = 255; }
            if (this.b > 255) { this.b = 255; }
        };
    }

    function hexify(number) {
        var digits = '0123456789ABCDEF';
        var lsd = number % 16;
        var msd = (number - lsd) / 16;
        var hexified = digits.charAt(msd) + digits.charAt(lsd);
        return hexified;
    }

    function decimalize(hexNumber) {
        var digits = '0123456789ABCDEF';
        return ((digits.indexOf(hexNumber.charAt(0).toUpperCase()) * 16) + digits.indexOf(hexNumber.charAt(1).toUpperCase()));
    }

    function hex2RGB(colorString, RGB) {
        RGB.r = decimalize(colorString.substring(1, 3));
        RGB.g = decimalize(colorString.substring(3, 5));
        RGB.b = decimalize(colorString.substring(5, 7));
    }

    function RGB2hex(RGB) {
        return "#" + hexify(RGB.r) + hexify(RGB.g) + hexify(RGB.b);
    }

    function rgbChange() {
        var RGB = new RGBobject(getObjectByID("rChannel").value, getObjectByID("gChannel").value, getObjectByID("bChannel").value);
        var HSV = new HSVobject(getObjectByID("hChannel").value, getObjectByID("sChannel").value, getObjectByID("vChannel").value);
        RGB.validate();
        RGB2HSV(RGB, HSV);
        getObjectByID("hexColor").value = RGB2hex(RGB);
        updateSwatch();
        getObjectByID("rChannel").value = Math.round(RGB.r);
        getObjectByID("gChannel").value = Math.round(RGB.g);
        getObjectByID("bChannel").value = Math.round(RGB.b);
        getObjectByID("hChannel").value = Math.round(HSV.h);
        getObjectByID("sChannel").value = Math.round(HSV.s);
        getObjectByID("vChannel").value = Math.round(HSV.v);
    }

    function hsvChange() {
        var RGB = new RGBobject(getObjectByID("rChannel").value, getObjectByID("gChannel").value, getObjectByID("bChannel").value);
        var HSV = new HSVobject(getObjectByID("hChannel").value, getObjectByID("sChannel").value, getObjectByID("vChannel").value);
        HSV.validate();
        HSV2RGB(HSV, RGB);
        getObjectByID("hexColor").value = RGB2hex(RGB);
        updateSwatch();
        getObjectByID("rChannel").value = Math.round(RGB.r);
        getObjectByID("gChannel").value = Math.round(RGB.g);
        getObjectByID("bChannel").value = Math.round(RGB.b);
        getObjectByID("hChannel").value = Math.round(HSV.h);
        getObjectByID("sChannel").value = Math.round(HSV.s);
        getObjectByID("vChannel").value = Math.round(HSV.v);
    }

    function hexChange() {
        var colorString = getObjectByID("hexColor").value;
        var RGB = new RGBobject(0, 0, 0);
        var HSV = new HSVobject(0, 0, 0);
        hex2RGB(colorString, RGB);
        RGB2HSV(RGB, HSV);
        getObjectByID("rChannel").value = Math.round(RGB.r);
        getObjectByID("gChannel").value = Math.round(RGB.g);
        getObjectByID("bChannel").value = Math.round(RGB.b);
        getObjectByID("hChannel").value = Math.round(HSV.h);
        getObjectByID("sChannel").value = Math.round(HSV.s);
        getObjectByID("vChannel").value = Math.round(HSV.v);
    }

    function updateSwatch() {
        getObjectByID("swatch").style.backgroundColor = getObjectByID("hexColor").value;
    }

    function RGB2HSV(RGB, HSV) {
        r = RGB.r / 255; g = RGB.g / 255; b = RGB.b / 255; // Scale to unity.

        var minVal = Math.min(r, g, b);
        var maxVal = Math.max(r, g, b);
        var delta = maxVal - minVal;

        HSV.v = maxVal;

        if (delta == 0) {
            HSV.h = 0;
            HSV.s = 0;
        } else {
            HSV.s = delta / maxVal;
            var del_R = (((maxVal - r) / 6) + (delta / 2)) / delta;
            var del_G = (((maxVal - g) / 6) + (delta / 2)) / delta;
            var del_B = (((maxVal - b) / 6) + (delta / 2)) / delta;

            if (r == maxVal) { HSV.h = del_B - del_G; }
            else if (g == maxVal) { HSV.h = (1 / 3) + del_R - del_B; }
            else if (b == maxVal) { HSV.h = (2 / 3) + del_G - del_R; }

            if (HSV.h < 0) { HSV.h += 1; }
            if (HSV.h > 1) { HSV.h -= 1; }
        }
        HSV.h *= 360;
        HSV.s *= 100;
        HSV.v *= 100;
    }

    function HSV2RGB(HSV, RGB) {
        var h = HSV.h / 360; var s = HSV.s / 100; var v = HSV.v / 100;
        if (s == 0) {
            RGB.r = v * 255;
            RGB.g = v * 255;
            RGB.b = v * 255;
        } else {
            var_h = h * 6;
            var_i = Math.floor(var_h);
            var_1 = v * (1 - s);
            var_2 = v * (1 - s * (var_h - var_i));
            var_3 = v * (1 - s * (1 - (var_h - var_i)));

            if (var_i == 0) { var_r = v; var_g = var_3; var_b = var_1 }
            else if (var_i == 1) { var_r = var_2; var_g = v; var_b = var_1 }
            else if (var_i == 2) { var_r = var_1; var_g = v; var_b = var_3 }
            else if (var_i == 3) { var_r = var_1; var_g = var_2; var_b = v }
            else if (var_i == 4) { var_r = var_3; var_g = var_1; var_b = v }
            else { var_r = v; var_g = var_1; var_b = var_2 };

            RGB.r = var_r * 255;
            RGB.g = var_g * 255;
            RGB.b = var_b * 255;
        }
    }
    function GetInputField(Name, Value) {
        return "<input type='text' name='" + Name + "' value='" + Value + "'/>"
    }
    /*
    // function GetSelect(Name, Options) {
    //     var s = ''
    //     for (i = 0; i < Options.length; i++)
    //         s += '<option value="' + Options[i][1] + '"' + (i == 0 ? 'selected' : '') + '>' + Options[i][0] + '</option>'
    //     return '<select name="' + Name + '" id="' + Name + '">' + s + '</select>'
    // }
    */
    var Width = 600
    var Height = 450
    var Rand = 10
    var Background = 'EEFFFF'
    var Legendenbreite = 0
    var Legendenhoehe = 0
    var GoogleServer = 'http://chart.apis.google.com/chart'
    var ParameterChart = new Array('europa', 'eu', 'euro', 'uefa')
    var Title = new Array('Europa', 'EU', 'EURO-Zone', 'UEFA')
    var LaenderFarbe = new Array('000044', '0000AA', 'CCCC00', '00CC00')
    var SpieleAnzahl = new Array(0,2,4,8,16,32,64,128,256,512);
    var SpieleErfolg = new Array(0,0.5,1.0,1.5,2.0,2.5,3);
    var TitleColor = "0000FF"
    var TitleSize = 24
    var NeutraleFarbe = '444444'
    var Laender = 
    new Array( // Name, ISO2, Spiele, Erfolg
        new Array('Albanien', 'AL', 0, 0),
        new Array('Andorra', 'AD', 0, 0),
        new Array('Armenien', 'AM', 0, 0),
        new Array('Aserbaidschan', 'AZ', 0, 0),
        new Array('Belgien', 'BE', 0, 0),
        new Array('Bosnien-H.', 'BA', 0, 0),
        new Array('Bulgarien', 'BG', 0, 0),
        new Array('Daenemark', 'DK', 0, 0),
        new Array('Deutschland', 'DE', 0, 0),
        new Array('England', 'GB-ENG', 0, 0),
        new Array('Estland', 'EE', 0, 0),
        new Array('Faeroeer', 'FO', 0, 0),
        new Array('Finnland', 'FI', 0, 0),
        new Array('Frankreich', 'FR', 0, 0),
        new Array('Georgien', 'GE', 0, 0),
        new Array('Griechenland', 'GR', 0, 0),
        new Array('Irland', 'IE', 0, 0),
        new Array('Island', 'IS', 0, 0),
        new Array('Israel', 'IL', 0, 0),
        new Array('Italien', 'IT', 0, 0),
        new Array('Kasachstan', 'KZ', 0, 0),
        new Array('Kroatien', 'HR', 0, 0),
        new Array('Lettland', 'LV', 0, 0),
        new Array('Liechtenstein', 'LI', 0, 0),
        new Array('Litauen', 'LT', 0, 0),
        new Array('Luxemburg', 'LU', 0, 0),
        new Array('Malta', 'MT', 0, 0),
        new Array('Mazedonien', 'MK', 0, 0),
        new Array('Moldawien', 'MD', 0, 0),
        new Array('Montenegro', 'ME', 0, 0),
        new Array('Niederlande', 'NL', 0, 0),
        new Array('Nordirland', 'GB-NIR', 0, 0),
        new Array('Norwegen', 'NO', 0, 0),
        new Array('Oesterreich', 'AT', 0, 0),
        new Array('Polen', 'PL', 0, 0),
        new Array('Portugal', 'PT', 0, 0),
        new Array('Rumaenien', 'RO', 0, 0),
        new Array('Russland', 'RU', 0, 0),
        new Array('San+Marino', 'SM', 0, 0),
        new Array('Schottland', 'GB-SCT', 0, 0),
        new Array('Schweden', 'SE', 0, 0),
        new Array('Schweiz', 'CH', 0, 0),
        new Array('Serbien', 'RS', 0, 0),
        new Array('Slowakei', 'SK', 0, 0),
        new Array('Slowenien', 'SI', 0, 0),
        new Array('Spanien', 'ES', 0, 0),
        new Array('Tschechische+Rep.', 'CZ', 0, 0),
        new Array('Tuerkei', 'TR', 0, 0),
        new Array('Ukraine', 'UA', 0, 0),
        new Array('Ungarn', 'HU', 0, 0),
        new Array('Vatikan', 'VA', 0, 0),
        new Array('Wales', 'GB-WLS', 0, 0),
        new Array('Weissrussland', 'BY', 0, 0),
        new Array('Zypern', 'CY', 0, 0)
    )
    function WriteMap(Chart) {
        var Chart = 'all'
        var Inverse = false
        var Color = ""
        var Debug = false
        if (window.location.search != "") {
            qs = window.location.search.substr(1)
            arrayOfStrings = qs.split("&")
            var Chart = ''
            var Inverse = false
            var Color = ""
            var Debug = false
            for (var i = 0; i < arrayOfStrings.length; i++) {
                parameter = arrayOfStrings[i].split("=")
                if (parameter[1] == undefined) parameter[1] = ''
                switch (parameter[0]) {
                    case "chart":
                        if (parameter[1] != '') Chart = parameter[1]
                        break;
                    case "inv":
                        Inverse = true
                        break;
                    case "color":
                        if (parameter[1] != '') Color = parameter[1]
                        break;
                    case "debug":
                        Debug = true
                        break;
                }
            }
            WriteEuropeMap(Chart, Inverse, Color, Debug)
        }
        function WriteEuropeMap(Chart, Inverse, Color, Debug)
        {
        var FormName = 'ifrm_' + Chart
        var sp = GetEuropeMap()
        var s =
    '<iframe '
    + 'id="map" name="map" '
    + 'width="' + Width + '" height="' + Height + '" '
    + 'scrolling="no" '
    + 'frameborder="0" ></iframe>'
        document.write(s)
        document.close()
        var iframe = getObjectByID('map')
        iframe = (iframe.contentWindow) ? iframe.contentWindow : (iframe.contentDocument.document) ? iframe.contentDocument.document : iframe.contentDocument;
        iframe.document.open()
        iframe.document.write(sp)
        iframe.document.close()
        eval('iframe.document.' + FormName + '.submit()')
    }
    function GetEuropeMap() {
        var Index = 0
        switch (Chart) {
            case 'europe':
                Index = 3
                break;
            case 'eu':
                Index = 4
                break;
            case 'euro':
                Index = 5
                break;
            case 'uefa':
                Index = 6
                break;
        }
        // chtVal = 'map:auto=' + Rand + ',' + Rand + ',' + Rand + ',' + Rand
        var chtVal = 'map:fixed=30,-20,65,60'
        var chsVal = Width + 'x' + Height
        var chttVal = Index == 0 ? 'Alle+Laender' : escape(Title[Index - 3])
        var chtsVal = TitleColor + ',' + TitleSize
        var chfVal = 'bg,s,' + Background
        var chmaVal = '10,10,10,10|' + Legendenbreite + ',' + Legendenhoehe
        var chldVal = ''
        var chcoVal = NeutraleFarbe
        var chmVal = ''
        var Land = 0
        for (i = 0; i < Laender.length; i++) {
            if ((Laender[i][Index] ^ Inverse) || (Chart == 'all')) {
                chldVal += (chldVal == '' ? '' : '|') + Laender[i][1];
                if (Chart == 'all') {
                    var HSV = new HSVobject((360 / Laender.length) * i, 40, 80)
                    var RGB = new RGBobject(0, 0, 0)
                    HSV2RGB(HSV, RGB)
                    chcoVal += '|' + RGB2hex(RGB).substr(1)
                }
                else
                    chcoVal += '|' + (Color == "" ? LaenderFarbe[Index - 3] : Color)
                chmVal += (chmVal == '' ? '' : '|') + 'f' + escape(Laender[i][1]) + ',000088,0,' + Land + ',8'
                Land++
            }
        }
        var FormName = 'ifrm_' + Chart
        var sp = "<form action='" + GoogleServer + "' method='POST' id='" + FormName + "' name='" + FormName + "'>"
        + GetInputField('cht', chtVal)
        + GetInputField('chs', chsVal)
        + GetInputField('chld', chldVal)
        + GetInputField('chco', chcoVal)
        + GetInputField('chm', chmVal)
        + GetInputField('chtt', chttVal)
        + GetInputField('chts', chtsVal)
        + GetInputField('chf', chfVal)
        + GetInputField('chma', chmaVal)
        + (Debug ? 'chof' : 'validate')
        + "</form>"
        return sp
    }

