// JavaScript Document
var userid = "";
var currUser = null;
var tempurl = window.location.href;
var cook = new Cookie();

/*
 * user对象的结构 "result" : { "EID" : "rwerwe", "FID" : "", "admin" : true, "auth" : [
 * "312", "424", ..... ], "degree" : 8, "id" : 21442, "isVoid" : "Y",
 * "memberType" : "0", "searchTimes" : -1, "uid" : "luckstarzb", "uname" : "且且",
 * "validDays" : 151 }
 */
var isenter = function(p, per) {
	if (isAutoLogin()) {
		setwelcomeinfo(loadUserFromCookie());
		if (p)
			p();
	} else {
		$.ajax({
			type : "post",
			url : "/account/UserLogin.do?method=isLogin",
			data : null,
			complete : function(json) {
				var data = eval("(" + json.responseText + ")");
				if (data && data.state == 'failed') {
					$("#logininfo").html("您好,欢迎使用造价通!");
					var url = window.location.href;
					var fromUrl = "/index.html"
					var temp = url.split("module");
					if (temp.length == 2)
						fromUrl = "/module" + temp[1];
					$("#usertools")
							.html("<ul><li class='regli'><a href='/register.html' >注册</a></li><li class='logintxt'><a href='/login.html?from="
									+ fromUrl
									+ "' id='mylogin'>登录</a>&nbsp;</li></ul>");
					if (per == "y" || p)
						p();
				} else if (data && data.state == 'success') {
					setwelcomeinfo(data.result);
					if (p)
						p();
				}
			}
		})
	}
};

function setfromurl() {
	var url = window.location.href;
	var fromUrl = "/index.html"
	var temp = url.split("module");
	if (temp.length == 2)
		fromUrl = "/module" + temp[1];
	if (getE("mylogin"))
		getE("mylogin").setAttribute("href", "/login.html?from=" + fromUrl);

}

function goTO(href) {
	var url = href.split("/");
	var len = url.length;
	if (confirm("此类信息和操作仅限会员查看，您现在是否要登录？"))
		if (href.indexOf("module") == -1)
			window.location.href = "/login.html?from=/" + url[len - 1];
		else
			window.location.href = "/login.html?from=/module/" + url[len - 2]
					+ "/" + url[len - 1];
	return false;
};

function goTO2(href) {
	var url = href.split("/");
	var len = url.length;
	window.location.href = "/login.html?from=/module/" + url[len - 2] + "/"
			+ url[len - 1];
};

var setwelcomeinfo = function(user) {
	if (user) {
		if (user.degree) {
			currUser = user;
			userid = user.uid;

			var strDegree = memberdegree(user.degree);
			var html = "";
			if (user.degree == 1) {
				if (user.validDays < 30)
					html = '<div class="account"><div style="float:left; height:26px;padding-top:3px; "><span class="uname">'
							+ user.uname
							+ '</span> 您好！欢迎使用造价通！您是<span class="membercss">'
							+ strDegree
							+ '</span>，您还可以免费使用<span class="membercss">'
							+ user.validDays
							+ '</span>天！&nbsp;</div><div style="float:left;margin-left:6px;height:24px;margin-top:3px;"><a style="float:left" href="/module/zhengding/guangdong.html"><img src="http://c1.zjtzg.com/images/guangdong/anniu01.gif" name="Image1" width="80" height="19" border="0" id="Image1" /></a><a style="float:left;margin-left:6px;"  href="/module/ask/myask.html" ><img src="http://c1.zjtzg.com/images/guangdong/anniu02.gif" name="Image2" width="80" height="19" border="0" id="Image2" /></a></div></div>';
				else
					html = '<div class="account"><div style="float:left; height:26px;padding-top:3px; "><span class="uname">'
							+ user.uname
							+ '</span> 您好！欢迎使用造价通！您是<span class="membercss">'
							+ strDegree
							+ '</span>&nbsp;</div><div style="float:left;margin-left:6px;height:24px;margin-top:3px;"><a href="/module/ask/myask.html"><img src="http://c1.zjtzg.com/images/guangdong/anniu02.gif" name="Image2" width="80" height="19" border="0" id="Image2" /></a></div></div>';
			} else if (user.degree == 11) {
				html = '<div class="account"><div><span class="uname">'
						+ user.uname
						+ '</span> 您好！感谢注册造价通！您的资料正在审核中，服务电话02085544604.</div></div>';
			} else if (user.degree == 2) {
				if (user.validDays < 30)
					html = '<div class="account"><div style="float:left;padding-top:3px; "><span class="uname">'
							+ user.uname
							+ '</span> 您好！欢迎使用造价通！您是<span class="membercss">'
							+ strDegree
							+ '</span>，您还可以使用<span class="membercss">'
							+ user.validDays
							+ '</span>天！</div><div  style="float:left;margin-left:6px;height:24px;margin-top:3px;"><a  style="float:left;" href="/module/zhengding/guangdong.html" ><img src="http://c1.zjtzg.com/images/guangdong/anniu01.gif" name="Image1" width="80" height="19" border="0" id="Image1" /></a></div></div>';
				else
					html = '<div class="account"><div style="float:left;padding-top:3px; "><span class="uname">'
							+ user.uname
							+ '</span> 您好！欢迎使用造价通！您是<span class="membercss">'
							+ strDegree
							+ '</span></div><div  style="float:left;margin-left:6px;height:24px;margin-top:3px;"></div></div>';
			} else if (user.degree == 12) {
				html = '<div class="account"><div><span class="uname">'
						+ user.uname
						+ '</span> 您好！感谢注册造价通！您的资料正在审核中！服务电话02085544604.</div></div>'
			} else if (user.degree == 3) {
				if (user.validDays < 30)
					html = '<div class="account"><div style="float:left;padding-top:3px; "><span class="uname">'
							+ user.uname
							+ '</span> 您好！欢迎使用造价通！您是<span class="membercss">'
							+ strDegree
							+ '</span>，您还可以使用<span class="membercss">'
							+ user.validDays
							+ '</span>天！&nbsp;</div><div  style="float:left;margin-left:6px;height:24px;margin-top:3px;"><a  style="float:left;" href="/module/zhengding/guangdong.html"><img src="http://c1.zjtzg.com/images/guangdong/anniu01.gif" name="Image1" width="80" height="19" border="0" id="Image1" /></a><a  style="float:left;margin-left:6px;" href="/module/ask/myask.html"><img src="http://c1.zjtzg.com/images/guangdong/anniu02.gif" name="Image2" width="80" height="19" border="0" id="Image2" /></a></div></div>';
				else
					html = '<div class="account"><div style="float:left;padding-top:3px; "><span class="uname">'
							+ user.uname
							+ '</span> 您好！欢迎使用造价通！您是<span class="membercss">'
							+ strDegree
							+ '</span>&nbsp;</div><div  style="float:left;margin-left:6px;height:24px;margin-top:3px;"><a href="/module/ask/myask.html" ><img src="http://c1.zjtzg.com/images/guangdong/anniu02.gif" name="Image2" width="80" height="19" border="0" id="Image2" /></a></div></div>';
			} else if (user.degree == 4) {
				var myprice = "";
				if (user.validDays < 30) {
					if (user.EID)
						myprice = '<a  style="float:left;margin-left:6px;" href="/module/account/myprice.jsp"><img src="http://c1.zjtzg.com/images/guangdong/anniu03.gif" name="Image2" width="80" height="19" border="0" id="Image2" /></a>';
					html = '<div class="account"><div style="float:left;padding-top:3px; "><span class="uname">'
							+ user.uname
							+ '</span> 您好！欢迎使用造价通！您是<span class="membercss">'
							+ strDegree
							+ '</span>，您还可以使用<span class="membercss">'
							+ user.validDays
							+ '</span>天！</div><div  style="float:left;margin-left:6px;height:24px;margin-top:3px;"><a style="float:left;" href="/module/zhengding/guangdong.html" ><img src="http://c1.zjtzg.com/images/guangdong/anniu01.gif" name="Image1" width="80" height="19" border="0" id="Image1" /></a>'
							+ myprice + '</div></div>';
				} else {
					if (user.EID)
						myprice = '<a href="/module/account/myprice.jsp"><img src="http://c1.zjtzg.com/images/guangdong/anniu03.gif" name="Image2" width="80" height="19" border="0" id="Image2" /></a>';
					html = '<div class="account"><div style="float:left;padding-top:3px; "><span class="uname">'
							+ user.uname
							+ '</span> 您好！欢迎使用造价通！您是<span class="membercss">'
							+ strDegree
							+ '</span></div><div  style="float:left;margin-left:6px;height:24px;margin-top:3px;">'
							+ myprice + '</div></div>';
				}
			} else if (user.degree == 5) {
				if (user.validDays < 30)
					html = '<div class="account"><div><span class="uname">'
							+ user.uname
							+ '</span> 您好！欢迎使用造价通！您是<span class="membercss">'
							+ strDegree
							+ '</span>，您还可以使用<span class="membercss">'
							+ user.validDays
							+ '</span>天，查询材价<span class="membercss">'
							+ user.searchTimes
							+ '</span>次！&nbsp;<a href="/module/ask/myask.html" style="color:blue">会员续期</a>&nbsp;<a href="/account/info-zsxx.html" style="color:blue">成为正式会员</a></div></div>';
				else
					html = '<div class="account"><div><span class="uname">'
							+ user.uname
							+ '</span> 您好！欢迎使用造价通！您是<span class="membercss">'
							+ strDegree
							+ '</span>，查询材价<span class="membercss">'
							+ user.searchTimes
							+ '</span>次！&nbsp;<a href="/module/ask/myask.html" style="color:blue">会员续期</a>&nbsp;<a href="/module/zhengding/guangdong.html" style="color:blue">成为正式会员</a></div></div>';
			} else if (user.degree == 6) {
				if (user.validDays < 30)
					html = '<div class="account"><div style="float:left;padding-top:3px; "><span class="uname">'
							+ user.uname
							+ '</span> 您好！欢迎使用造价通！您是<span class="membercss">'
							+ strDegree
							+ '</span>，您还可以使用<span class="membercss">'
							+ user.validDays
							+ '</span>天！&nbsp;</div><div  style="float:left;margin-left:6px;height:24px;margin-top:3px;"><a  style="float:left;" href="/module/zhengding/guangdong.html"><img src="http://c1.zjtzg.com/images/guangdong/anniu01.gif" name="Image1" width="80" height="19" border="0" id="Image1" /></a><a  style="float:left;margin-left:6px;" href="/module/ask/myask.html"><img src="http://c1.zjtzg.com/images/guangdong/anniu02.gif" name="Image2" width="80" height="19" border="0" id="Image2" /></a></div></div>';
				else
					html = '<div class="account"><div style="float:left;padding-top:3px; "><span class="uname">'
							+ user.uname
							+ '</span> 您好！欢迎使用造价通！您是<span class="membercss">'
							+ strDegree
							+ '</span>&nbsp;</div><div  style="float:left;margin-left:6px;height:24px;margin-top:3px;"><a href="/module/ask/myask.html" ><img src="http://c1.zjtzg.com/images/guangdong/anniu02.gif" name="Image2" width="80" height="19" border="0" id="Image2" /></a></div></div>';
			} else if (user.degree == 7) {
				if (user.validDays < 30)
					html = '<div class="account"><div><span class="uname">'
							+ user.uname
							+ '</span> 您好！欢迎使用造价通！您是<span class="membercss">'
							+ strDegree
							+ '</span>，您还可以使用<span class="membercss">'
							+ user.validDays + '</span>天！&nbsp;</div></div>';
				else
					html = '<div class="account"><div><span class="uname">'
							+ user.uname
							+ '</span> 您好！欢迎使用造价通！您是<span class="membercss">'
							+ strDegree + '</span>&nbsp;</div></div>';
				// }
			} else if (user.degree == 8) {
				if (user.validDays < 30)
					html = '<div class="account"><div style="float:left;padding-top:3px; "><span class="uname">'
							+ user.uname
							+ '</span> 您好！欢迎使用造价通！您是<span class="membercss">'
							+ strDegree
							+ '</span>，您还可以使用<span class="membercss">'
							+ user.validDays
							+ '</span>天！&nbsp;</div><div  style="float:left;margin-left:6px;height:24px;margin-top:3px;"><a href="/module/zhengding/guangdong.html"><img src="http://c1.zjtzg.com/images/guangdong/anniu01.gif" name="Image1" width="80" height="19" border="0" id="Image1" /></a>&nbsp;&nbsp;</div></div>';
				else
					html = '<div class="account"><div style="float:left;padding-top:3px; "><span class="uname">'
							+ user.uname
							+ '</span> 您好！欢迎使用造价通！您是<span class="membercss">'
							+ strDegree
							+ '</span>&nbsp;</div><div  style="float:left;margin-left:6px"></div></div>';
			} else if (user.degree == 9) {
				if (user.validDays < 30)
					html = '<div class="account"><div style="float:left;padding-top:3px; "><span class="uname">'
							+ user.uname
							+ '</span> 您好！欢迎使用造价通！您是<span class="membercss">'
							+ strDegree
							+ '</span>，您还可以使用<span class="membercss">'
							+ user.validDays
							+ '</span>天！&nbsp;</div><div  style="float:left;margin-left:6px;height:24px;margin-top:3px;"><a href="/module/zhengding/guangdong.html"><img src="http://c1.zjtzg.com/images/guangdong/anniu01.gif" name="Image1" width="80" height="19" border="0" id="Image1" /></a>&nbsp;&nbsp;</div></div>';
				else
					html = '<div class="account"><div style="float:left;padding-top:3px; "><span class="uname">'
							+ user.uname
							+ '</span> 您好！欢迎使用造价通！您是<span class="membercss">'
							+ strDegree
							+ '</span>&nbsp;</div><div  style="float:left;margin-left:6px"></div></div>';
			}
			if (user.degree == 8) {

				var bol = tempurl.lastIndexOf("/module/vip/");
				if (bol == -1)
					bol = false;
				else
					bol = true;
				if (bol)
					var chtml = '<div class="tools"><ul><li  class="loginout" ><a href="javascript:logout();" >安全退出</a></li><li class="headhome"><a href="/index.html" >返回造价通首页</a></li></ul></div>';
				else
					var chtml = '<div class="tools"><ul><li  class="loginout" ><a href="javascript:logout();" >安全退出</a></li><li class="headhome"><a href="/module/vip/index.jsp" >VIP中心</a></li></ul></div>';
			} else if (user.degree == 9) {
				var bol = tempurl.lastIndexOf("/module/company/");
				if (bol == -1)
					bol = false;
				else
					bol = true;
				if (bol)
					var chtml = '<div class="tools"><ul><li  class="loginout" ><a href="javascript:logout();" >安全退出</a></li><li class="headhome"><a href="/index.html" >返回造价通首页</a></li></ul></div>';
				else
					var chtml = '<div class="tools"><ul><li  class="loginout" ><a href="javascript:logout();" >安全退出</a></li><li class="headhome"><a href="/module/company/index.jsp" >企业会员中心</a></li></ul></div>';
			} else
				var chtml = '<div class="tools"><ul><li  class="loginout" ><a href="javascript:logout();" >退出</a></li><li class="infocenter"><a href="/module/account/index.jsp" >会员中心</a></li></ul><div class="clearing"></div></div>';

			$("#logininfo").html(html);
			$("#usertools").html(chtml);
		}
	} else {
		var html = '<div class="account"><div><span>帐号:<input type="text" name="uid" size="12" id="uid" /></span><span>密码:<input type="password" name="pwd" size="12" id="pwd" /></span><span style="display:none">'
				+ '<input type="text" id="code" name="code"/></span><span>&nbsp;<input type="button" value="登 录" name="l" class="isclick" onclick="login();">&nbsp;</span> <span><a href="account/reg.html">注册</a></span><span><a href="/findpwd.html">忘记密码</a></span></div></div><div class="accountweb">您好！欢迎使用造价通！</div>';
		$("#logininfo").html(html);
	}
	$("#logininfo").show()
};
function logout() {
	if (confirm("您确认要退出造价通吗?")) {
		$.ajax({
					type : "post",
					url : "/account/UserLogin.do?method=logout",
					data : null,
					complete : function(json) {
						json = eval("(" + json.responseText + ")");
						if (json) {
							window.location= "/index.html"
						} else {
							alert("退出失败");
						}
					}
				});
	}
};

var memberdegree = function(degree) {
	var t = "";
	switch (parseInt(degree)) {
		case 11 :
			t = "注册未审核信息会员";
			break;
		case 1 :
			t = "试用信息会员";
			break;
		case 12 :
			t = "注册未审核厂商会员";
			break;
		case 2 :
			t = "试用厂商会员";
			break;
		case 3 :
			t = "正式信息会员";
			break;
		case 4 :
			t = "正式厂商会员";
			break;
		case 5 :
			t = "赠送会员";
			break;
		case 6 :
			t = "持卡会员";
			break;
		case 7 :
			t = "内部会员";
			break;
		case 8 :
			t = "VIP信息会员";
			break;
		case 9 :
			t = "企业会员";
			break;
	}
	return t;
}

function getState(state, funcObj, result) {
	var func = function() {
	};
	if (funcObj && funcObj[state])
		func = funcObj[state] || func;
	switch (state) {
		case contant.success :
			func();
			return true;
			break;
		case contant.failure :
			msg = result;
			func(result);
			return false;
			break;
		case contant.nologin :
			msg = contant.nologinMsg;
			goTO(window.location.href);
			return false;
			break;
		case contant.noauth :
			msg = contant.noauthMsg;
			func();
			return false;
			break;
		default :
			msg = contant.noaccess;
			return false;
	}
};

// 返回数据的通用处理函数
var commonResultFunc = {};
commonResultFunc[contant.noauth] = function() {
	alert(contant.noauthMsg);
	return false;
}
commonResultFunc[contant.nologin] = function() {
	alert(contant.nologinMsg)
	goTO(window.location.href);
	return;
}
commonResultFunc[contant.failure] = function(result) {
	if (result)
		alert(result);
	else
		alert(contant.failureMsg);
}

var goback = function(page, target) {
	if (target == "blank") {
		window.open(page);
	} else {
		window.location.href = page;
	}
}

function islogin() {
	if (currUser) {
		return true;
	} else
		return false;
};


function loadUserFromCookie() {
	var user = {};
	user.uid = cook.getCookie("uid");
	user.uname = cook.getCookie("uname");
	var degree = cook.getCookie("degree");
	if (degree)
		user.degree = parseInt(degree);
	var searchTimes = cook.getCookie("searchTimes");
	if (searchTimes)
		user.searchTimes = parseInt(searchTimes);
	var validDays = cook.getCookie("validDays");
	if (validDays)
		user.validDays = parseInt(validDays);
	user.EID = cook.getCookie("EID");
	user.corpName = cook.getCookie("corpName");
	var isAdmin = cook.getCookie("isAdmin");
	user.isAdmin = (isAdmin == "true" ? true : false);
	user.memberType =cook.getCookie("memberType");
	user.webProvince = cook.getCookie("webProvince");
	return user;
}

function isAutoLogin() {
	var result = false;
	var curr_sid = cook.getCookie("curr_sid");
	var sid = cook.getCookie("JSESSIONID");
	if (curr_sid && sid && curr_sid == sid) {
		result = true;
	}
	return result;
}
function nullToEmpty(value){
	if(!value)
		return "";
	return value;
}