﻿jQuery(function($) {
	var title = $("#txtTitle"),
			name = $("#txtName"),
			contact = $("#txtContact"),
			content = $("#txtContent"),
			allFields = $([]).add(title).add(name).add(contact).add(content),
			reply = $("#txtReply"),
			tips = $("#validateTips"),
	tips1 = $("#validateTips1");

	function checkRequire(o, n, tip) {
		if ($.trim(o.val()).length <= 0) {
			o.addClass('ui-state-error');
			tip.text(n + " 必须填写。");
			return false;
		} else {
			return true;
		}
	}
	$("#dialogNewMessage").dialog({
		bgiframe: true,
		autoOpen: false,
		height: 'auto',
		width: 600,
		modal: true,
		buttons: {
			'提交': function(event) {
				var bValid = true;
				allFields.removeClass('ui-state-error');
				bValid = bValid && checkRequire(title, "留言标题", tips);
				bValid = bValid && checkRequire(name, "姓名", tips);
				bValid = bValid && checkRequire(contact, "联系方式", tips);
				bValid = bValid && checkRequire(content, "留言内容", tips);
				if (bValid) {
					var _self = event.target;
					_self.innerHTML = '数据提交中...';
					_self.disabled = true;
					$.post("Default.aspx?act=NewMessage", { title: title.val(), name: name.val(), contact: contact.val(), content: content.val() }, function(json) {
						if (json.status) {
							alert('留言成功！');
							location.href = 'Default.aspx';
						} else {
							alert(json.error);
							_self.innerHTML = '提交';
							_self.disabled = false;
						}
					}, 'json');
					//$(this).dialog('close');
				}
			},
			'取消': function() {
				$(this).dialog('close');
			}
		},
		close: function() {
			allFields.val('').removeClass('ui-state-error');
		}
	});
	$("#dialogReply").dialog({
		bgiframe: true,
		autoOpen: false,
		height: 'auto',
		width: 580,
		modal: true,
		buttons: {
			'提交': function(event) {
				var bValid = true;
				reply.removeClass('ui-state-error');
				bValid = bValid && checkRequire(reply, "留言标题", tips1);
				if (bValid) {
					var _self = event.target;
					_self.innerHTML = '数据提交中...';
					_self.disabled = true;
					$.post("Default.aspx?act=Reply", { reply: reply.val(), mid: $('#txtAutoId').val() }, function(json) {
						if (json.status) {
							alert('回复成功！');
							location.href = 'Default.aspx';
						} else {
							alert(json.error);
							_self.innerHTML = '提交';
							_self.disabled = false;
						}
					}, 'json');
					//$(this).dialog('close');
				}
			},
			'取消': function() {
				$(this).dialog('close');
			}
		},
		close: function() {
			allFields.val('').removeClass('ui-state-error');
		}
	});
	$('#btnNewMessage').click(function(event) {
		$('#dialogNewMessage').dialog('open');
		event.preventDefault();
	});
	$('form').get(0).onclick = function() { };
});
function DeleteTopic(autoid) {
	$.post("Default.aspx?act=DeleteTopic", { mid: autoid }, function(json) {
		if (json.status) {
			alert('删除成功！');
		} else {
			alert(json.error);
		}
		location.href = 'Default.aspx';
	}, 'json');
}

function ShowReplyDialog(autoid) {
	$('#txtAutoId').val(autoid);
	$('#dialogReply').dialog('option', 'title', '回复留言');
	$('#dialogReply').dialog('open');
}

function EditReplyDialog(autoid, reply) {
	$('#txtAutoId').val(autoid);
	//$('#dialogReply').dialog('option', 'title', '修改回复');
	$('#txtReply').val(reply);
	$('#dialogReply').dialog('open');
}
