Comments = {
	addAnswer : function(id_a)
	{
		if(id_a)
		{
			// Delete old form
			if($("#answer_add"))
			{
				var inp = $("answer_add")['answer_id'];
				var inp_val = $(inp).attr('value');

				if($('#insertaddform'+ inp_val)) $('insertaddform'+ inp_val).empty();
			}
			$('#tableaddcomm').hide("slow");
			if($('#insertaddform'+ id_a))
			{
				$('#insertaddform'+ id_a).empty();
				var cntn = '<table cellpadding="0" cellspacing="0" width="100%" border="0"><tr><td id="insansferform" style="display: none;">';
				cntn += '</td></tr></table>';
				$('#insertaddform'+ id_a).html(cntn);
				var dans = $('#insansferform');
				var fcontent = $('#comment_add').html();
				// Create new form
				var newform = "<form id='answer_add' action='"+$('#comment_add').attr("action")+"' method='"+$('#comment_add').attr("method")+"'>";
				newform += '<input type="hidden" name="answer_id" value="'+id_a+'"/>';
				newform += fcontent + '</form>';
				$('#insansferform').append(newform);
				$('#insansferform').show("slow");
			}
		}
	}
}


Storys = {
	ShowForm : function (url, tit)
	{
		var name = $("#name"),
			R = $("#R"),
			anonse = $("#anonse"),
			allFields = $([]).add(name).add(R).add(anonse),
			tips = $(".validateTips");

		$('<div id="dialog-form"></div>').dialog({
			autoOpen: false,
			height: 500,
			width: 550,
			modal: true,
			title: tit,
			open: function() { $(this).load(url); },
			buttons: {
				'Сохранить' : function() {
					var bValid = true;
					allFields.removeClass('ui-state-error');

					bValid = bValid && Storys.checkLength("name",1,255);
					bValid = bValid && Storys.checkSelected("R");
					bValid = bValid && Storys.checkLength("anonse",1,255);

					if (bValid) {
						$('form', this).submit();
					}
				},
				'Отмена' : function() {
					if(confirm('Все изменения внесенные Вами при закрытии окна пропадут.\nПродолжить?'))
						$(this).dialog('close');
				}
			}
		}).dialog('open').focus();
	},
	updateTips : function(t) {
		$(".validateTips").text(t).addClass('ui-state-highlight').css('display','');
		setTimeout(function() {
			$(".validateTips").removeClass('ui-state-highlight', 1500);
		}, 1000);
	},
	checkLength : function(n,min,max) {
		var vals = jQuery.trim($('#'+ n).val());
		if ( vals.length > max || vals.length < min ) {
			$('#'+ n).addClass('ui-state-error');
			Storys.updateTips("Длина введенных данных в поле (" + $("#" + n).attr('title') + ") должно быть от "+min+" и не больше "+max+" символов!");
			return false;
		} else {
			return true;
		}
	},
	checkSelected : function(n) {
		if($('#' + n + ' option:selected').val() <= 0) {
			$('#'+ n).addClass('ui-state-error');
			Storys.updateTips("Вы не выбрали вариант в поле (" + $("#" + n).attr('title') + ")!");
			return false;
		} else {
			return true;
		}
	}
}

