inputs = new Array;

function elementError(req, error)
{
	$req = $(req)
	if(error==false)
	{
		$req.removeClass('error');
		$req.parent('div,td').removeClass('error');
		$req.parents('tr').removeClass('error');


		if(req.type=='select-one' && typeof(select_color)!='undefined')
		$req.css('backgroundColor', select_color);
	}
	else
	{
		debug('Error: '+$req.attr('name'));
		$req.addClass('error');
		$req.parent('div,td').addClass('error');
		$req.parents('tr').addClass('error');
		
		if(req.type=='select-one' && typeof(select_color_error)!='undefined')
		$req.css('backgroundColor', select_color_error);
	}

}

function checkemail(str){
	var filter=/^.+@.+\..{2,3}$/
	return (filter.test(str))
}

$.fn.formBuildRequest = function() {
	build = new Array;
	build.push('module='+router.module);
	build.push('language='+router.language);
	$('input[type=text]:visible, select:visible, textarea:visible, input[type=hidden], input[type=radio]:checked', this).not('.btn').each(function() {
		if(inputs[this.name]==this.value) this.value='';
		build.push(this.name+'='+this.value);
	});

	data = build.join('&');
	return data;
}

$.fn.formSendAjax = function() {
	$form = $(this);


	$('.btn', $form).hide();




	$.ajax({
		type: "POST",
		url: "/_extras/ajax.php",
		data: $form.formBuildRequest(),
		timeout: 5000,
		success: function(msg){
			data = msg.split('||');
			
			//TODO: replace .after with some page_inbuild_container
			if(data[0]=='OK')
			$form.fadeOut('normal', function() {$form.after('<div class="response">'+data[1]+'</div>');});
			else
			{
				$('.message_form', $form).blink('language_form_error')
				$('.btn', $form).fadeIn();

			}
		}
 });
}

$.fn.blink = function(text) {
	$blinked = $(this);
	if($blinked.attr('blocked')!='true')
	$blinked.attr('blocked', true).text(text).fadeIn().delay(1500).fadeOut('normal', function(){$blinked.attr('blocked', false)});
}

$(document).ready(function() {

	$('.only_digit').live('keypress', function (e) {
		if(e.which==13 || e.which==8)
		return true;

		if(!(e.shiftKey==false && e.which>47 && e.which< 59))
		return false;
		
	});
       


	$('input, textarea').each(function() {
		if($(this).val()=='')
		$(this).val(inputs[$(this).attr('name')]);
	});

	$('input, textarea').live('focus', function() {
		if($(this).val()== inputs[$(this).attr('name')])
			$(this).val('');
	});

	$('input, textarea').live('blur', function() {
		if($(this).val()=='')
			$(this).val(inputs[$(this).attr('name')]);
	});

	if(typeof(select_color)!='undefined')
	$('select').css('backgroundColor', select_color);


	if($('form.validate').length>0)
	{

		$('form.validate').submit(function() {
			$form = $(this);
			error = false;
			text = 'language_form_field_empty';
			counter = 0;

			$('.req:enabled, input[type=hidden].req', this).each(function() {
				$req = $(this);
				name = $req.attr('name');
				value = $req.val();
				
				if(value=='' || value==inputs[name])
				{
					counter++;
					if(counter>1)
					text = 'language_form_fields_empty';
					error = true;
					elementError(this, true);
				}
				else
				elementError(this, false);
			});

			$("input[name^='email']", $form).each(function() {

				$req = $(this);
				value = $req.val();

				if(value!=='' && value!==inputs[$(this).attr('name')] && !checkemail(value))
				{
					if(error==false)
					text = 'language_form_email';

					elementError($req, true);
					error = true;
				}
				
			});

			$form.myerror = error;
			checkFormExtras($form);

			if(error==true)
			{
				translator(text, function(data){$('.message_form', $form).blink(data);} );
				return false;
			}

			if($form.hasClass('ajax'))
			{
				$form.formSendAjax();
				return false;
			}

			if($form.hasClass('nosend'))
			return false;
		});
	}
});
