var notfilled = 'Поле не заполнено';

function clearField(field) {
	if (!field.getAttribute('initvalue')) {
		field.setAttribute('initvalue', field.value);
		field.value = '';
	}
	if (field.value == notfilled) {
		field.setAttribute('initvalue', field.value);
		$(field).css({background: '#fff', color: '#999'});
		field.value = '';
	}
	
}
function checkFields(form) {
	$("input:text[value='']",form).css({background: '#f55', color: '#000'});
	$("textarea[value='']",form).css({background: '#f55', color: '#000'});
	$("input:text[value='']",form).val(notfilled);
	$("textarea[value='']",form).val(notfilled);
	if ($("input:text",form).val().length < 1 || $("textarea",form).val().length < 1 || $("textarea",form).val() == notfilled || $("input:text",form).val() == notfilled) {
		return false
	} else {
		return true
	}
}


var SF = {
	_formId		: 'send_frend',
	_notfilled	: 'Поле не заполнено',
	_notEmail	: 'Поле «Ваш e-mail» не заполнено',
	_notName	: 'Поле «Ваше имя» не заполнено',
	_notEmailTo	: 'Поле «Кому, e-mail» не заполнено',
	_notComment	: 'Поле «Комментарий» не заполнено',

	toggle	: function () {
		$('#' + SF._formId).toggle();
		return false;
	},
	
	send	: function () {
		var form = $('#' + SF._formId);
		if (SF.check(form)) {
			$("input:text[type='image']",form).bind('click', function () {return false});
			$.post(
			document.URL,
			{
				tof_email	: $("input:text[name='tof_email']"		,form).val(),
				tof_name	: $("input:text[name='tof_name']"		,form).val(),
				tof_emailto	: $("input:text[name='tof_emailto']"	,form).val(),
				tof_comment	: $("textarea[name='tof_comment']"		,form).val()
			},
			SF.success
			);
		}
		return false;
	},
	
	success	: function(resp) {
		if (resp) {
		alert(resp);
		} else {
			var form = $('#' + SF._formId);
			$(form).html('<p id="SFsuccess" style="background:#fff; line-height: 92px; text-align: center;">Ваше сообщение отправлено!</p>');
			$('#SFsuccess').hide();
			$('#SFsuccess').show('slow');
		}
	},
	
	check	: function(form) {
		$("input:text[value='']",form).css({background: '#f55', color: '#000'});
		$("textarea[value='']",form).css({background: '#f55', color: '#000'});
		$("input:text[value='']",form).each(function () {
			if (this.name == 'tof_email') {this.value = SF._notEmail;}
			if (this.name == 'tof_name') {this.value = SF._notName;}
			if (this.name == 'tof_emailto') {this.value = SF._notEmailTo;}
		});
		$("textarea[value='']",form).val(SF._notComment);
		if ($("input:text",form).val().length < 1 || $("textarea",form).val().length < 1 || $("textarea",form).val() == SF._notComment || $("input:text[name='tof_email']",form).val() == SF._notEmail || $("input:text[name='tof_emailto']",form).val() == SF._notEmailTo || $("input:text[name='tof_name']",form).val() == SF._notName) {
			return false
		} else {
			return true
		}
	}
	
}












