/**
	<!--
	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>')
		.addClass('formError')
		.css({
			'background': '#DD9999',
			'border'	: '1px solid #AA6666',
			'padding'	: '2px 5px',
			'font-size'	: '11px',
			'color'		: '#FFF',
			'float'		: 'right'
		});

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

	return hint;
}

function applyFormValidator(f){
	
	jQuery('.formSuccessMessage', jQuery(f).parent()).hide();
	jQuery('.formErrorContainer').remove();
		
	var errors=0;
	var inputs = jQuery('.needToValidate', f);
	var emailError = false;
	
	inputs.each(function()
	{
		emailError = false;
		if (jQuery(this).attr('name') == 'form_email')
		{
			var emailCheck = this.value;
			var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
			if (!filter.test(emailCheck))
			{
				emailError = true;
			}
		}
		
		
		
		if (this.value=='' || emailError) {
			
			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) {

	var blockId = $('input[name="invite_block_id"]').val();
	
	jQuery('.formSuccessMessage', jQuery(f).parent()).hide();
	
	$('li', $(f)).removeClass('err');
	$('li p.error', $(f)).html('');
	
	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['formattedHostName'] != undefined) {
				$('#' + json['formattedHostName']['id']).val(json['formattedHostName']['value']);	
			}
			
			var rand = Math.round(new Date().getTime() / 1000)+(Math.random()*100) ;
			
			$('#captcha-image-' + blockId).attr('src', '/ll/controller/captcha.gif?b=' + blockId + '&' + rand);
			$('#invite_block_captcha_' + blockId).val('');
			
			if (json.errors) {
				
				for (x in json.errors_on_field) {
				
					$('#' + x + '_error').html(json.errors_on_field[x]);
					$('#' + x + '_error').parent('li').addClass('err');
				}
			} 
			else {
			
				jQuery('input[type="text"][id^=invite_block_]', f).val('');
				jQuery('.formSuccessMessage', jQuery(f).parent()).fadeIn();
			}
		}
	);
}
