

function runQuery( url, id, confirm_msg ) {
	
	// maybe trhow confirm box
	if ( confirm_msg.length > 0 ) {
		var confirmed = confirm( confirm_msg );
		
		if ( !confirmed ) {
			return false;
		}
	}
	
	// send ajax request
	var ajaxRequest = new Ajax.Request(
		url, {
			method: 'get',
			parameters: '',
			onSuccess: function ( req ) {
				try {
					$( id ).innerHTML = req.responseText;
				} catch( e ) {
				}
			}
		}
	);
}




var STR_PAD_LEFT = 1;
var STR_PAD_RIGHT = 2;
var STR_PAD_BOTH = 3;

function pad( str, len, pad, dir ) {

    if ( typeof( len ) == "undefined" ) { var len = 0; }
    if ( typeof( pad ) == "undefined" ) { var pad = ' '; }
    if ( typeof( dir ) == "undefined" ) { var dir = STR_PAD_RIGHT; }

    if ( len + 1 >= str.length ) {

        switch ( dir ) {

            case STR_PAD_LEFT:
                str = Array(len + 1 - str.length).join(pad) + str;
            break;

            case STR_PAD_BOTH:
                var right = Math.ceil((padlen = len - str.length) / 2);
                var left = padlen - right;
                str = Array(left+1).join(pad) + str + Array(right+1).join(pad);
            break;

            default:
                str = str + Array(len + 1 - str.length).join(pad);
            break;

        } // switch

    }

    return str;
}


function initArray() {
    this.length = initArray.arguments.length;
    for ( var i = 0; i < this.length; i++ ) {
        this[i] = initArray.arguments[i];
	}
}

function from10toradix( value, radix ) {
    var retval = '';
    var ConvArray = new initArray( 0,1,2,3,4,5,6,7,8,9,'A','B','C','D','E','F' );
    var intnum;
    var tmpnum;
    var i = 0;

    intnum = parseInt( value, 10 );
    if ( isNaN( intnum ) ) {
        retval = 'NaN';
    } else {
        if ( intnum < 1 ) {
            retval ="0";
        } else {
            retval = "";
        }
        while ( intnum > 0.9 ) {
            i++;
            tmpnum = intnum;
            // concatenate return string with new digit:
            retval = ConvArray[tmpnum % radix] + retval;  
            intnum = Math.floor( tmpnum / radix );
            if (i > 50 ) {
                // break infinite loops
                retval = 'NaN';
                break;
            }
        }
    }
    return retval;
}


var redBounds = new Array(); // 
var greenBounds = new Array(); // 
var blueBounds = new Array(); // 

//bounds[1] = '#FF0000';
//bounds[1] = '255 0 0;
//bounds[2] = '#00FF00';
//bounds[2] = '0 255 0';


redBounds[1] = 37;
redBounds[2] = 37;
redBounds[3] = 0;

greenBounds[1] = 18;
greenBounds[2] = 0;
greenBounds[3] = 18;

blueBounds[1] = 43;
blueBounds[2] = 37;
blueBounds[3] = 56;


function changeBG( newColor ) {
	document.bgColor = newColor;
}


var transWait;

var miniStepTime = 300;
var maxiStepTime = 4000;
var minStep = 1;
var numSteps = 20;
var thisStep = minStep;

var minBound = 1;
var maxBound = redBounds.length - 1;
var thisBound = minBound;
var nextBound = thisBound + 1;

var thisIteration = 1;
var numIterations = 20;

function runBGChange() {
	
	if ( document ) {
		thisRedBound = Math.round( ( redBounds[nextBound] - redBounds[thisBound] ) * ( thisStep / numSteps ) + redBounds[thisBound] );
		thisGreenBound = Math.round( ( greenBounds[nextBound] - greenBounds[thisBound] ) * ( thisStep / numSteps ) + greenBounds[thisBound] );
		thisBlueBound = Math.round( ( blueBounds[nextBound] - blueBounds[thisBound] ) * ( thisStep / numSteps ) + blueBounds[thisBound] );
		changeBG( '#' + pad( from10toradix( thisRedBound, 16 ), 2, 0, STR_PAD_LEFT) + 
						pad( from10toradix( thisGreenBound, 16 ), 2, 0, STR_PAD_LEFT) + 
						pad( from10toradix( thisBlueBound, 16 ), 2, 0, STR_PAD_LEFT) );
	}
	
	if ( thisStep == numSteps ) {
		thisBound = nextBound;
		if ( nextBound == maxBound ) {
			nextBound = minBound;
		} else {
			nextBound = thisBound + 1;
		}
		thisStep = minStep;
		if ( thisIteration < numIterations ) {
		trans_wait = setTimeout('runBGChange();', maxiStepTime);
		thisIteration = thisIteration + 1;
		}
	} else {
		thisStep = thisStep + 1;
		trans_wait = setTimeout('runBGChange();', miniStepTime);
	}
}


