/**
	FormValidator v.0.1 ( author Alex.B. )
	Script will fork if form structured like:
	<form><ul>
		<li>
			<label></label>
			<input class="needToValidate" />
		</li>
	</ul></form>
*/
var formError = null; 

function createFormErrorItem(){
	
	var hintHTML = 	jQuery('<div></div>')
		.attr('id', "formError")
		.css({
			'background': '#DD9999',
			'border'	: '1px solid #AA6666',
			'padding'	: '2px 5px',
			'font-size'	: '11px',
			'color'		: '#FFF',
			'float'		: 'right'
		});

	var hint = 	jQuery('<div></div>')
		.attr('id', "formErrorContainer")
		.css({
			'white-space'	: 'nowrap',
			'display'		: 'none',
			'position'		: 'absolute',
			'padding-left'	: '4px',
			'background'	: 'url(/i/formErrorArrow.gif) left 6px no-repeat',
			'zIndex'		: '999'
		})
		.append(hintHTML);

	
	return hint;
}

function applyFormValidator(f){
	
	jQuery('#formErrorContainer').each( function(){jQuery(this).remove();});
	jQuery('#formErrorContainer').each( function(){jQuery(this).remove();});
		
	var errors=0;
	var inputs = jQuery('.needToValidate', f);
	inputs.each(function(){
		if(this.value==''){
			this.style.backgroundColor = '#FDD';
			
			var newItem = createFormErrorItem();
			newItem.css({
				'left'	: jQuery(this).offset().left + jQuery(this).width() + 10,
				'top'	: jQuery(this).offset().top
			});
			jQuery('#formError',newItem).html(
				$('label[for='+this.name+']').html()
			);
			jQuery(document.body).after( newItem.fadeIn('slow') );
			jQuery('#formError').fadeIn('slow');
			if(!errors) this.focus();
			errors++;
		} else {
			this.style.backgroundColor = '#FFF';
		}
	});
	return errors ? false : true;
}


function inviteBlockSubmit(f){
	
	jQuery('#formErrorContainer').each( function(){jQuery(this).remove();});
	
	var fields = {};
	jQuery('*[name^=invite_block_]',f).each( function(i) {
		fields[this.name] = this.value;
	});

	$.getJSON(
		"/ll/controller/ajax.captcha.return", 
		fields, 
		function(json){
			if( json.errors ){
			
				for(x in json.errors_on_field){
					var newItem = createFormErrorItem();
					newItem.css({
						'left'	: jQuery(f[x]).offset().left + jQuery(f[x]).width() + 10,
						'top'	: jQuery(f[x]).offset().top
					});
					jQuery('#formError',newItem).html(
						json.errors_on_field[x]
					);
					jQuery(document.body).after( newItem.fadeIn('slow') );
					jQuery('#formError').fadeIn('slow');
					f[x].focus();
				}
			} else {
				jQuery(f).fadeOut('slow');
				jQuery('.formSuccessMessage', jQuery(f).parent()).fadeIn('slow');
			}
		}
	);

}