var $T = function( txt )
{
	return document.createTextNode(txt);
}

var $jq = jQuery.noConflict();

$jq.ajaxSetup({
	type: 'POST',
	dataType: 'json',
	timeout: 30000
});



var Form__removeErrorHandler = function( event )
{
	var _input = event.data.input;
	
	if ( _input.nextSibling && _input.nextSibling.firstChild &&
		_input.nextSibling.firstChild.className == 'form_input_error' )
	{
		_input.style.color = '';
		$jq(_input.nextSibling).remove();
		$jq(_input).unbind('focus', Form__removeErrorHandler);
	}
}


var Form_drawError = function( input, error_msg )
{
	if ( input.nextSibling && input.nextSibling.firstChild &&
			input.nextSibling.firstChild.className == 'form_input_error' ) {
		return false;
	}
	
	input.style.color = '#ddd';
	
	$jq(input.parentNode).createAppend(
		'div', {style: {position: 'relative'}}
	).createAppend(
		'div', {className: 'form_input_error'}
	).append(
		$T(error_msg)
	).click(
		function() {
			$jq(this.parentNode.previousSibling).focus();
		}
	);
	
	$jq(input).bind('focus', {input: input}, Form__removeErrorHandler);
	
	return false;
}


var Form_drawMessage = function( form, msg, msg_type ) {
	$jq('.form_msg', form).remove();
	
	$jq(form).createPrepend(
		'div', {className: 'form_msg form_'+msg_type+'_msg'}
	).append(
		$T(msg)
	);
}


// revive form inputs
$jq(function() {
	$jq('input[@type=text], input[@type=password]').focus(
		function() {
			this.style.borderColor = '#1c2a3b';
			this.style.color = '#1c2a3b';
		}
	).blur(
		function() {
			this.style.borderColor = '';
			this.style.color = '';
		}
	);
});
