<!--
// $Id: sudokugrid.js 490 2007-03-15 00:42:38Z abeltje $
//var has_focus = 'cell0000';
//var base_w = [% p_base_w + 1 %];
//var base_h = [% p_base_h + 1 %];
//var mmod   = Math.floor(base_w / 2);

var max_r = base_w * base_h;
var max_c = max_r;

var all_chars = '123456789abcdefghijklmnopqrstuvwxyz'.substr(0, max_r );

function zero_pad( n, s )
{
    var v = n.toString();
    while ( v.length < s ) { v = '0' + v }
    return v;
}

function init_form( withcolour )
{
    document.forms[ 'grid' ].elements[ has_focus ].focus();

    if ( withcolour )
        set_colour( document.forms[ 'grid' ].elements[ 'stype' ][0] );

    return true;
}

function go_next( e )
{
    // Do the event-model song and dance
    var t;
    if ( ! e )  e = window.event;
    if ( e.target ) t = e.target
    else if ( e.srcElement ) t = e.srcElement;

    var key;
    if ( e.keyCode ) key = e.keyCode
    else if ( e.which ) key = e.which;
    var value = String.fromCharCode( key );

    // Handle TAB, SHIFT-TAB, ^G, option-R
    if  ( key == 9 || key == 25 || key == 19 || key == 114 )
    { 
        e.returnValue = true;
        return true;
    }
    else // 1-9 + SPACE set value and move to next, rest ignore
    {
//window.alert( "Keypressed: " + key + " (" + value +")" );

        if ( (all_chars.indexOf( value ) >= 0) || key == 32 )
        {
             t.value = key == 32 ? '' : value;
             var elems = t.form.elements;
             for ( var i = 0; i < elems.length; i++ )
             {
                 if ( elems[ i ].tabIndex == t.tabIndex + 1 )
                 {
                     has_focus = elems[ i ].name;
                     elems[ i ].focus();
                     break;
                 }
             }
        }
//        else window.alert( 'Only 0-9 and SPACE allowed!' );

        e.cancelBubble = true;
        if ( e.stopPropagation ) e.stopPropagation();

        return false; // Don't do the default keypress-event
    }
}

function clk_set_colour( e )
{
    // Do the event-model song and dance
    var t;
    if ( ! e )  e = window.event;
    if ( e.target ) t = e.target
    else if ( e.srcElement ) t = e.srcElement;

    set_colour( t );

    return init_form();
}

function set_colour( t )
{
    switch ( t.value )
    {
        case '1': colour_inner(  t, 'white', 'silver' ); break;
        case '2': colour_middle( t, 'white', 'silver' ); break;
        case '3': colour_x(      t, 'white', 'silver' ); break;
         default: colour_all( t, 'silver' );
    }
}

function colour_all( t, c )
{
    for ( var y = 0; y < max_r; y++ )
    {
        for ( var x = 0; x < max_c; x++ )
        {
            var cname = 'cell' + zero_pad(y, 2) + '' + zero_pad(x, 2);
            t.form.elements[ cname ].style.background = c;
        }
    }
    t.checked = true;
}

function colour_inner( t, c1, c2 )
{
    for ( var y = 0; y < max_r; y++ )
    {
        for ( var x = 0; x < max_c; x++ )
        {
            var cname = 'cell' + zero_pad(y, 2) + zero_pad(x, 2);
            if ( (y % (base_h + 1)) != 0 && (x % (base_w+1)) != 0 )
                t.form.elements[ cname ].style.background = c1
            else
                t.form.elements[ cname ].style.background = c2
        }
    }
}

function colour_middle( t, c1, c2 )
{
    for ( var y = 0; y < max_r; y++ )
    {
        for ( var x = 0; x < max_c; x++ )
        {
            var cname = 'cell' + zero_pad(y, 2) + zero_pad(x, 2);
            if ( (y % base_h) == mmod && (x % base_w) == mmod )
                t.form.elements[ cname ].style.background = c1
            else
                t.form.elements[ cname ].style.background = c2
        }
    }
}

function colour_x( t, c1, c2 )
{
    for ( var y = 0; y < max_r; y++ )
    {
        for ( var x = 0; x < max_c; x++ )
        {
            var cname = 'cell' + zero_pad(y, 2) + zero_pad(x, 2);
            if ( (x == y) || ((x + y) == (max_r - 1)) )
                t.form.elements[ cname ].style.background = c1
            else
                t.form.elements[ cname ].style.background = c2
        }
    }
}
// -->
