$(function() {
	// 预览模版
	$("a[@name='preview'],img[@name='preview'],area[@name='preview'],:button[@name='preview']").click(function() {
		var isAnchor = $(this).is("a") ? 1 : 0;
		var thisID = this.id;
		//var tplID = thisID.substring(7);
		var checkFlg = 0;
		var msgArr = new Array();
		$.ajax({
			type  : "POST",
			url   : "templet.ajax.php",
			data  : "type=1&tplid=1",
			async : false,
			success: function(msg) {
				msgArr = msg.split('|');
				switch (msgArr[0]) {
					case 's1' :                                            break;
					case 'e1' : alert("对不起，参数错误！");               break;
					case 'e2' : alert("对不起，请您先登录网站！");         break;
					case 'e3' : alert("对不起，该活动已结束！");           break;
					default   : alert("网络通讯错误！请您稍候再试！");     break;
				}
				if ('s1' == msgArr[0]) {
					if (msgArr.length > 3) {
						msgArr = msg.split('|', 3);
					}
					if (1 == isAnchor) {
						$("#"+thisID).attr("href", "###");
					}
					checkFlg = 1;
				}
				if ('e2' == msgArr[0]) {
					if (1 == isAnchor) {
						$("#"+thisID).attr("href", "###");
					}
					location.href = 'http://passport.51.com/passport.5p?gourl=' + location.href.replace('#','');
					return false;
				}
			},
			error: function() {
				alert("网络通讯错误！请您稍候再试！");
				return false;
			}
		});
		if (1 == checkFlg) {
			var rs = window.open("http://home.51.com/home.php?user=" + msgArr[2] + "&tplid=-" + msgArr[1] + "&cache=disable",'newwindow','top=0,left=0,toolbar=no,menubar=no,scrollbars=yes, resizable=yes,location=no, status=no');
			rs.focus();
		}
	})
	// 领取模版
	$("a[@name='obtain'],img[@name='obtain'],area[@name='obtain'],:button[@name='obtain']").click(function() {
		var isAnchor = $(this).is("a") ? 1 : 0;
		var thisID = this.id;
		var tplID = thisID.substring(6);
		$.ajax({
			type: "POST",
			url : "templet.ajax.php",
			data: "type=2&tplid=" + tplID,
			success: function(msg) {
				var msgArr = new Array();
				msgArr = msg.split('|');
				switch (msgArr[0]) {
					case 's2' : alert("恭喜您领取成功！");                 break;
					case 'e1' : alert("对不起，参数错误！");               break;
					case 'e2' : alert("对不起，请您先登录网站！");         break;
					case 'e4' : alert("对不起，您已经领取过了！");         break;
					case 'e3' : alert("对不起，该活动已结束！");           break;
					case 'e5' : alert("对不起，您已经领取过了！");         break;
					case 'e6' : alert("对不起，已经领取结束！");           break;
					case 'e10': alert("对不起，领取失败，请您稍候再试！"); break;
					default   : alert("网络通讯错误！请您稍候再试！");     break;
				}
				if ('s2' == msgArr[0]) {
					$("#obtain_count").html(msgArr[1]);
				}
				if ('e2' == msgArr[0]) {
					if (1 == isAnchor) {
						$("#"+thisID).attr("href", "###");
					}
					location.href = 'http://passport.51.com/passport.5p?gourl=' + location.href.replace('#','');
					return false;
				}
			},
			error: function() {
				alert("网络通讯错误！请您稍候再试！");
				return false;
			}
		});
	})
})