/***********************/
/*                     */
/*  utils.js           */
/*                     */
/***********************/

var g_moveFocus = null;
function _afs()
{
	g_moveFocus.focus();
	if(g_moveFocus.select) g_moveFocus.select();
}

function asyncFocus(element)
{
	g_moveFocus = $(element);
	setTimeout('_afs();', 0);
}

function displayNotice(className, msg, moveFocus)
{
	var notice = $('#notice');
	if(msg == null)
	{
		notice.css('display', 'none');
	}
	else	
	{
		notice.attr('class', className).attr('innerHTML', msg).css('display', 'block');
	
		if(moveFocus != null)
			asyncFocus("#" + moveFocus);
	}
}

function displayProblem(msg, moveFocus)
{
	displayNotice('Problem', msg, moveFocus);
}

function displayInfo(msg, moveFocus)
{
	displayNotice('Confirm', msg, moveFocus);
}

function evalResponse(responseText)
{
	if(responseText.substr(0, 9) == 'while(1);')
    {
        responseText = responseText.substring(9);
        //return responseText.evalJSON(true);
        return eval('(' + responseText + ')');
    }
    
    return null;
}

function random()
{
	return Math.round(999999999 * Math.random());
}

var g_newCaptcha;
function reloadCaptcha(clear)
{
    var captcha_image = $('#captcha_image');	
	captcha_image.attr('src', __g_static+'img/buttonShine.png');
    
    g_newCaptcha = new Image();
    g_newCaptcha.onload = displayCaptcha;
    g_newCaptcha.src = '/captcha?' + random();
	
	if(clear)
	{
		$('#captcha').val('');
		asyncFocus('#captcha');
	}
}

function displayCaptcha()
{
    if(g_newCaptcha)
    {
        $('#captcha_image').attr('src', g_newCaptcha.src);
    }
}

