// check a match
function checkMatch()
{
    // get the colors
    var c1 = document.getElementById("c1").value;
    var c2 = document.getElementById("c2").value;

    // put it in the show table
    var sttxt = "<table cellspacing='0' align='center' style='padding: 2em; background-color: #7F7F7F'>";
    for (var y = 0; y < 5; y++) {
        sttxt += "<tr>";

        for (var x = 0; x < 5; x++) {
            sttxt += "<td style='width: 3em; height: 3em; background-color: ";

            if (((x+y) % 2) == 0) {
                sttxt += c1;

            } else {
                sttxt += c2;

            }

            sttxt += "'>&nbsp;</td>";
        }

        sttxt += "</tr>";
    }
    sttxt += "</table>";
    document.getElementById("showtable").innerHTML = sttxt;

    c1 = hexToCM(c1);
    c2 = hexToCM(c2);

    // then get the result
    var res = checkColors(c1, c2);

    // get ready to make the disagree form
    var ht =
        "<form action='q/' method='post' target='_BLANK'>" +
        "<input type='hidden' name='s' value='1'/>" +
        "<input type='hidden' name='r1' value='" + Math.floor(c1[0] * 255) + "'/>" +
        "<input type='hidden' name='g1' value='" + Math.floor(c1[1] * 255) + "'/>" +
        "<input type='hidden' name='b1' value='" + Math.floor(c1[2] * 255) + "'/>" +
        "<input type='hidden' name='r2' value='" + Math.floor(c1[9] * 255) + "'/>" +
        "<input type='hidden' name='g2' value='" + Math.floor(c1[10] * 255) + "'/>" +
        "<input type='hidden' name='b2' value='" + Math.floor(c1[11] * 255) + "'/>";

    // make a word of it
    var ht;
    if (res > 0.5) {
        ht = "Match<br/>" + ht;

        ht += "<input type='submit' name='y' value='Agree'/>" +
            "<input type='submit' name='n' value='Disagree'/>";
    } else {
        ht = "Non-match<br/>" + ht;

        ht += "<input type='submit' name='n' value='Agree'/>" +
            "<input type='submit' name='y' value='Disagree'/>";
    }

    ht += "</form>";

    document.getElementById("match").innerHTML = ht;
}

function randomColor2(match)
{
    // the boxes to put it in
    var c1 = hexToCM(document.getElementById("c1").value);
    var box2 = document.getElementById("c2");

    // try to get a match
    var match = randomColorMatch(c1, match);
    if (match === false) return;
    box2.value = match[0];

    // and rematch
    checkMatch();
}

function generateMatch(match)
{
    document.getElementById("c1").value = randomColor();
    randomColor2(match);
}

function generateColorScheme()
{
    var sch = assignPageColorScheme(randomPageColorScheme());
    if (sch != false) {
        document.getElementById("debugout").innerHTML =
            "Generated color scheme: " +
            sch[0][0] + ", " +
            sch[1][0] + ", " +
            sch[2][0];
    }
}

function onEnter(ev, f)
{
    if (ev.which) {
        if (ev.which == 13) f();
    } else if (ev.keyCode) {
        if (ev.keyCode == 13) f();
    }
}
