CheckFields(false);
$("#where").hide();


function CheckFields(changeColor)
{
	var IsOk = CheckValue(document.forms.ActionsForm.firstname, changeColor);

	IsOk = CheckValue(document.forms.ActionsForm.lastname, changeColor) && IsOk;
	IsOk = CheckValue(document.forms.ActionsForm.email, changeColor) && IsOk;
	IsOk = CheckValue(document.forms.ActionsForm.url, changeColor) && IsOk;
	IsOk = CheckValue(document.forms.ActionsForm.city, changeColor) && IsOk;
	IsOk = ((0 != document.forms.ActionsForm.station.value) || CheckValue(document.forms.ActionsForm.where, changeColor)) && IsOk;

	return IsOk;
}

function onStationChanged()
{
	if (0 != document.forms.ActionsForm.station.value)	
		$("#where").hide();
	else
		$("#where").show();
}

function CheckValue(el, changeColor)
{
	if (0 == el.value.length)
	{
		if (changeColor)
		{
			el.style.backgroundColor="pink";
		}
		return false;
	} else
	{
		el.style.backgroundColor="white";
		return true;
	}
}

function CheckEmail (el)
{	
	var IsCorrect = (/^[_.0-9a-z-]+@([0-9a-z][0-9a-z_-]+.)+[a-z]{2,4}$/i).test(el.value);

	el.style.backgroundColor = IsCorrect? "white" : "pink";	
	return IsCorrect;
}

function Send()
{	
	if (CheckFields(true) && CheckEmail(document.forms.ActionsForm.email))
	{
		$.post("/save_action_wish.html", $("#ActionsForm").serialize(), EndPost);
	}
}


function EndPost(data)
{
	var Result =  eval("(" + data + ")");

	if (Result.IsOk)
	{
		$("#error_text").hide();
		$("#success_text").show();
		document.forms.ActionsForm.firstname.value = "";
		document.forms.ActionsForm.lastname.value = "";
		document.forms.ActionsForm.email.value = "";
		document.forms.ActionsForm.url.value = "";
		document.forms.ActionsForm.city.value = "";
		document.forms.ActionsForm.where.value = "";
		document.forms.ActionsForm.station.value = 1;
		$("#where").hide();
	} else
	{
		$("#error_text").show();
	}
}