var customer;

function getFromPage() {
	var loginPage = "/customer/login.jsp";
	var registerPage = "/customer/register/register.jsp";
	var from = "";
	if (location.pathname != "/") {
		if (location.pathname == "/customer/info/get_back_password.jsp"
				|| location.pathname == "/customer/login.jsp"
				|| location.pathname == "/customer/register/register.jsp")
			from = "/customer/";
		else
			from = location.pathname + location.search;
		from = encodeURIComponent(from);
		loginPage = loginPage + "?from=" + from;
		registerPage = registerPage + "?from=" + from;
	}
	var returnValue = new Array();
	returnValue[0] = loginPage;
	returnValue[1] = registerPage;
	return returnValue;
}

function check(div) {
	// var fromUrl=encodeURIComponent(location.pathname+location.search);
	var fromPage = getFromPage();
	var xmlHttp = createHttpRequest();
	
	var url = "/customer/customer_xml.jsp?ts=" + new Date().getTime();
	
	xmlHttp.open("GET", url, true);
	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState == 4) {
			if (xmlHttp.status == 200) {
				var responseXML = xmlHttp.responseXML;
				var customer = responseXML.getElementsByTagName("customer");
				if (customer == undefined || customer.length == 0) {
					div.innerHTML = "您好，欢迎光临新华书店.COM  [<a href=\""
							+ fromPage[0]
							+ "\" class=\"font5\"><strong>登录</strong></a>/<a href=\""
							+ fromPage[1]
							+ "\" class=\"font5\"><strong>注册</strong></a>]";
				} else {
					var registerName = responseXML
							.getElementsByTagName("registerName")[0].firstChild.nodeValue;
					div.innerHTML = "您好，"
							+ registerName
							+ "。[<strong onclick=\"logout()\" style=\"cursor:pointer\">退出登录</strong>]";
				}
			}
		}
	};
	xmlHttp.send(null);
}

function check4VP(div) {
	var fromPage = getFromPage();
	if(!isLogin()) {
		div.innerHTML = "您好，欢迎光临新华书店.COM  [<a href=\""
			+ fromPage[0]
			+ "\" class=\"font5\"><strong>登录</strong></a>/<a href=\""
			+ fromPage[1]
			+ "\" class=\"font5\"><strong>注册</strong></a>]";
	} else {
		var xmlHttp = createHttpRequest();
		var url = "/customer/customer_xml.jsp?ts=" + new Date().getTime();
		xmlHttp.open("GET", url, true);
		xmlHttp.onreadystatechange = function() {
			if (xmlHttp.readyState == 4) {
				if (xmlHttp.status == 200) {
					var responseXML = xmlHttp.responseXML;
					var customer = responseXML.getElementsByTagName("customer");
					if (customer == undefined || customer.length == 0) {
						
					} else {
						var registerName = responseXML.getElementsByTagName("registerName")[0].firstChild.nodeValue;
						div.innerHTML = "您好，"
								+ registerName
								+ "。[<strong onclick=\"logout()\" style=\"cursor:pointer\">退出登录</strong>]";
					}
				}
			}
		};
		xmlHttp.send(null);
	}
}

function getName() {
	var userName = window.document.getElementById("registername");
	var xmlHttp = createHttpRequest();
	var url = "/customer/customer_xml.jsp?ts=" + new Date().getTime();
	xmlHttp.open("GET", url, true);
	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState == 4) {
			if (xmlHttp.status == 200) {
				var responseXML = xmlHttp.responseXML;
				var customer = responseXML.getElementsByTagName("customer");
				if (customer == undefined || customer.length == 0) {

				} else {
					var registerName = responseXML
							.getElementsByTagName("registerName")[0].firstChild.nodeValue;
					userName.value = registerName;
				}
			}
		}
	};
	xmlHttp.send(null);
}
/**
 * 判断用户是否登录
 */
function isLogin() {
	var isLogin = true;
	var xmlHttp = createHttpRequest();
	var url = "/customer/customer_xml.jsp?ts=" + new Date().getTime();
	xmlHttp.open("GET", url, false);
	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState == 4) {
			if (xmlHttp.status == 200) {
				var responseXML = xmlHttp.responseXML;
				var loginObj = responseXML.getElementsByTagName("isLogin");
				if (loginObj == undefined || loginObj.length == 0) {
					isLogin = false;
				} else {
					var login = loginObj[0].firstChild.nodeValue;
					if (login == "false") {
						isLogin = false;
					}
				}
			}
		}
	};
	if (navigator.userAgent.indexOf("Firefox") > 0) {
		xmlHttp.onload = function() {
			var responseXML = xmlHttp.responseXML;
			var loginObj = responseXML.getElementsByTagName("isLogin");
			if (loginObj == undefined || loginObj.length == 0) {
				isLogin = false;
			} else {
				var login = loginObj[0].firstChild.nodeValue;
				if (login == "false") {
					isLogin = false;
				}
			}
		}
	}
	xmlHttp.send(null);
	return isLogin;
}
function logout() {
	// var fromUrl=encodeURIComponent(location.pathname+location.search);
	var fromPage = getFromPage();
	var url = "/customer/login.htm?operator=quit&__Accept=json&ts=" + new Date().getTime();;
	jQuery.ajax( {
		url :url,
		type :"GET",
		timeout :1000,
		dataType :"json",
		error : function() {
		},
		success : function(json) {
		}
	});
	var div = document.getElementById("loginDiv");
	div.innerHTML = "您好，欢迎光临新华书店.COM [<a href=\"" + fromPage[0]
			+ "\" class=\"font5\"><strong>登录</strong></a>/<a href=\""
			+ fromPage[1] + "\" class=\"font5\"><strong>注册</strong></a>]";
}

function createHttpRequest() {
	var xml_http;
	if (window.ActiveXObject) {
		xml_http = new ActiveXObject("Microsoft.XMLHTTP")
	} else if (window.XMLHttpRequest) {
		xml_http = new XMLHttpRequest();
	}
	return xml_http;
}