$(document).ready(function()
{
    if($('table.smart').length > 0)
	{
        $('table.smart').each(function()
		{
            $(this).find('tr:even').addClass('even');
            $(this).find('tr:odd').addClass('odd');
        });
    }

	if($('ul.h_menu li').length > 0)
	{
        $('ul.h_menu li').each(function()
		{
            $(this).mouseover(function()
			{
				$(this).addClass('focus');
			}).mouseout(function()
			{
				$(this).removeClass('focus');
			});
        });
    }
});

var FormSendOptions =
{ 
	success:  successSendForm,
	type: 'post',
	url:  '/ajax/form/',
	clearForm: false 

};
var emailfilter = /^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i;
var formId = new String;

function checkmail(v)
{
	return emailfilter.test(v);
}

function formCheck(formobj)
{
	var fieldRequired = Array();
	var fieldDescription = Array();
	fieldRequired = formobj.requiredfileds.value.split(',');
	fieldDescription = formobj.requireddescription.value.split(',');
	var alertMsg = "Пожалуйста правильно заполните следующие поля:\n";
	var focused = false;
	formId = formobj.formname.value + 'Form';
	var defaultStr = 'Введите';	
	var l_Msg = alertMsg.length;	
	for (var i = 0; i < fieldRequired.length; i++)
	{
		var obj = formobj.elements[fieldRequired[i]];
		if (obj)
		{
			obj.style.border = '';
			switch(obj.type)
			{
			case "select-one":
				if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == "")
				{
					alertMsg += " - " + fieldDescription[i] + "\n";
					obj.style.border = '1px solid red';
				}
				break;
			case "select-multiple":
				if (obj.selectedIndex == -1)
				{
					alertMsg += " - " + fieldDescription[i] + "\n";
					obj.style.border = '1px solid red';
				}
				break;
			case "text":
			case "textarea":
				if ((obj.value == "" || obj.value == null || obj.value.substring(0, defaultStr.length) == defaultStr) || (fieldRequired[i] == 'email' && !checkmail(obj.value)))
				{
					alertMsg += " - " + fieldDescription[i] + "\n";
					obj.style.border = '1px solid red';
					if (!focused)
					{
						obj.focus();
						focused = true;
					}
				}
				break;
			default:
			}
			if (obj.type == undefined)
			{
				var blnchecked = false;
				for (var j = 0; j < obj.length; j++)
				{
					if (obj[j].checked)
					{
						blnchecked = true;
					}
				}
				if (!blnchecked)
				{
					alertMsg += " - " + fieldDescription[i] + "\n";
					obj.style.border = '1px solid red';
				}
			}
		}
	}
	if (alertMsg.length == l_Msg)
	{
		$('#' + formobj.formname.value + 'Form').ajaxSubmit(FormSendOptions);
		return false;
	}
	else
	{
		alert(alertMsg);		
		return false;
	}
}

function CaptchaError(form, focus, message) 
{
	var input_captcha = document.getElementById(form).kcaptcha;
	input_captcha.style.border = '1px solid red';
	input_captcha.value = '';
	if (focus)
	{
		input_captcha.focus();
	}
	document.getElementById(form + '-image-kcaptcha').src = '/kcaptcha/' + Math.random() + '/';
	if (message)
	{
		alert('Код безопасности (защиты от спама) введён не верно !');
	}
}

function successSendForm(result, message) 
{
		//alert(result);
		form = document.getElementById(formId);		
		if (result == 1)
		{
			alert(form.successmessage.value);
			parent.location.reload(1);
		}
		else if (result == 0)
		{
			alert(form.errormessage.value);
			CaptchaError(formId, false, false);
		}
		else if (result == -1)
		{
			CaptchaError(formId, true, true);
		}
}

function searchCheck(formobj)
{
	if (formobj.search.value == "" || formobj.search.value == null || formobj.search.value.length < 4 || formobj.search.value == 'ПОИСК ПО САЙТУ')
	{
		alert('Поисковое слово не введено или меньше 4 символов!');
		formobj.search.style.border = '1px solid red';
		formobj.search.focus();
		return false;
	}
	return true;
}
