var CAPTCHA_CHECK_EMPTY = false;
var catpcha_setFocusFlag = false;

function CaptchaDisplayError( field , msg )
{
	try {
		errField = document.getElementById( field.name + "Error" ) ;
		errField.style.display = '';
		if( field.tagName == 'INPUT' ) {
			field.className = ERROR_CLASS ;
		}
		errField.innerHTML = msg ;
	}
	catch( e ) { }
	
}

function captcha_SetFocus( field )
{
	if( catpcha_setFocusFlag && (!fieldToFocus) ) {
		if( lastFocus != field ) {
			field.focus( );
			lastFocus = field ;
			fieldToFocus = field ;
		}
	}
}

function CaptchaundisplayError( field )
{
	try {
		errField = document.getElementById( field.name + "Error" ) ;
		errField.style.display = 'none';
		field.className = 'ts';
	}
	catch( e ) { }
}

function captcha_ValidateForm( form )
{
	CAPTCHA_CHECK_EMPTY = true ;
	catpcha_setFocusFlag = true;
	form.code.value = jQuery.trim(form.code.value) ;
	result = checkCode( form.code );
	catpcha_setFocusFlag = false;
	CAPTCHA_CHECK_EMPTY = false; 
	
	if( ! form.agree.checked ) {
		captcha_DisplayError( document.getElementById( 'agreeInput' ) , document.getElementById('agreeError') , document.getElementById("agreeLabel") , message[38] ) ;
		return false;
	}
	else {
		captcha_undisplay_checkbox_error( document.getElementById( 'agreeInput' ) , document.getElementById('agreeError') );
	}

	if( form.tos ) {
		if( ! form.tos.checked ) {
			captcha_DisplayError( document.getElementById( 'tosInput' ) , document.getElementById('tosError')
			 , document.getElementById("tosLabel") , message[44] + '"' + forumName + '"' ) ;
			return false;
		}
		else {
			captcha_undisplay_checkbox_error( document.getElementById( 'tosInput' ) , document.getElementById('tosError') );
		}
	}
	
	return result ;
}

function captcha_DisplayError( inputTd , errorTd , errorLabel , errorMessage )
{
	inputTd.className = 'orng';
	errorTd.style.display = '';
	errorLabel.innerHTML = errorMessage ;
}

function captcha_undisplay_checkbox_error( inputTd , errorTd )
{
	inputTd.className = 'lblreg';
	errorTd.style.display = 'none';
} 

function showDiv( divName )
{
	try {
		divElm = document.getElementById( divName );
		divElm.style.display = ""; 
	}
	catch( e ) { }
}

function hideDiv( divName )
{
	try {
		divElm = document.getElementById( divName );
		divElm.style.display = "none"; 
	}
	catch( e ) { }
}

function captcha_reloadCaptcha()
{
	var captchaImage = document.getElementById('capthcaImage');
	var rand = Math.round( Math.random() * 1000 + 1 );
	captchaImage.src = 'plugins/captcha/CAPTCHA.php?nm=' +  rand ;
}

function checkCode( codeField )
{
	codeField.value = jQuery.trim(codeField.value) ;
	if( CAPTCHA_CHECK_EMPTY ) {
		if( codeField.value.length == 0 ) {
			CaptchaDisplayError( codeField , message[37] );
			captcha_SetFocus( codeField );
			return false;
		}
	}
	
	 if ( ( codeField.value.length != 5 ) && ( codeField.value.length != 0 ) ) {
	 	CaptchaDisplayError( codeField , message[37] );
		captcha_SetFocus( codeField );
		return false;
	 }
/*	  
	 if( isNaN(codeField.value) && ( codeField.value.length != 0 ) ) {
	 	CaptchaDisplayError( codeField , message[42] );
        captcha_SetFocus( codeField );
        return false;
	}
  */  
    CaptchaundisplayError( codeField );
    return 1;
}

