/*通过JavaScript验证
Array.prototype.contains = function(obj) {
	var i = this.length;
	while (i--) {
		if (this[i] === obj) {
			return true;
		}
	}
	return false;
};
// 定义网站域名（不带www）
var domain = "zhidian12.com";
// 定义首页URL数组
var homeURLArr = new Array("http://" +  domain + "/", "http://www." +  domain + "/", "http://tj." +  domain + "/");
// 定义分站名称数组
var substationNameArr = new Array("tj");
// 定义错误页URL
var errorURL = "http://" + domain + "/error.htm";
// 获得完整的URL
var href = location.href;
// 获得当前URL的协议
var protocol = location.protocol;
// 获得当前URL的主机名
var hostname = location.hostname;
// 获得当前URL的端口号
var port = location.port;
// 获得当前URL的路径部分
var pathname = location.pathname;
// 判断当前URL是否为首页URL
if (homeURLArr.contains(href)) {
	// 暂无代码
} else {
	// 获得主机名的二级域名
	var twoDomain = "";
	if (hostname.indexOf("." + domain) > 0) {
		twoDomain = hostname.substring(0, hostname.indexOf("." + domain)).toLowerCase();
	}
	// 判断主机名的二级域名是否为分站名称
	if (twoDomain != "" && substationNameArr.contains(twoDomain)) {
		// 获得城市ID
		var pathnameArr = pathname.split("/");
		var cityId = pathnameArr[1].toLowerCase();
		// 判断城市ID是否与主机名的二级域名相同
		if (cityId == twoDomain) {
			// 定义分站目录形式访问分站首页URL
			var substationDirURL = "";
			if (port != 80) {
				substationDirURL = protocol + "//" + hostname + ":" + port + "/" + cityId + "/";
			} else {
				substationDirURL = protocol + "//" + hostname + "/" + cityId + "/";
			}
			// 判断当前URL是否为分站目录形式访问分站首页URL
			if (href == substationDirURL) {
				if (port != 80) {
					location.href = protocol + "//" + hostname + ":" + port + "/";
				} else {
					location.href = protocol + "//" + hostname + "/";
				}
			}
		} else {
			location.href = errorURL;
		}
	} else {
		location.href = errorURL;
	}
}*/

/*通过AJAX验证*/
var xmlhttp;
function loadXMLDoc(method, url, async, string, callback) {
	xmlhttp = null;
	if (window.XMLHttpRequest) {// code for Firefox, Mozilla, IE7, etc.
		xmlhttp = new XMLHttpRequest();
	} else if (window.ActiveXObject) {// code for IE6, IE5
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	if (xmlhttp != null) {
		var isFirefox = /firefox/.test(window.navigator.userAgent.toLowerCase());
		if (async == true || (async == false && !isFirefox)) {
			xmlhttp.onreadystatechange = callback;
		}
		xmlhttp.open(method, url, async);
		xmlhttp.setRequestHeader("If-Modified-Since", "0");// 避免缓存
		xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlhttp.send(string);
		if (async == false && isFirefox) {
			callback();
		}
	} else {
		alert("Your browser does not support XMLHTTP.");
	}
}
var domainVerification = (function() {
	function verifyDomain() {
		// 获得完整的URL
		var href = location.href;
		// 获得当前URL的协议
		var protocol = location.protocol;
		// 获得当前URL的主机名
		var hostname = location.hostname;
		// 获得当前URL的端口号
		var port = location.port;
		// 获得当前URL的路径部分
		var pathname = location.pathname;
		// 定义请求需要发送的数据
		var string = "href=" + href + "&protocol=" + protocol + "&hostname=" + hostname + "&port=" + port + "&pathname=" + pathname;
		loadXMLDoc("POST", "/domainVerification", false, string, function() {
			if (xmlhttp.readyState == 4) {// 4 = "loaded"
				if (xmlhttp.status == 404) {// 404 = "Not Found"
					location.href = "http://zhidian12.com/error.htm";
				}
			}
		});
	}
	return {"verifyDomain":verifyDomain}
})();
domainVerification.verifyDomain();

