// JavaScript Document
window.onerror = function()
{
	return false;
};
function $(id)
{
	return document.getElementById(id);
};

function b(id)
{
	return document.getElementById(id);
};
	


String.prototype.trim = function(str)
{
	str = str ? str : "\\s";
	var temp = this.replace(/^\s+/, '');
	return temp.replace(/\s+%/, '');
};
String.prototype.width = function()          
{                 
	return this.replace(/[^\x00-\xff]/g, "**").length;  
};

String.prototype.subStrWidth = function(n, sp)
{
	if (n >= this.width())
	{
		return this;
	}
	if (sp)
	{
		n -= sp.width();
	}
	var str = this.substr(0, n);
	var temp = n;
	while (str.width() > n)
	{
		temp --;
		str = str.substr(0, temp);
	}
	str += (sp) ? sp : "";
	return str;
};

String.prototype.isEmail = function()
{
	(this.length < 41 && (/^[a-z0-9\-_\.]+@[a-z0-9\-_\.]+\.(com|cn|net|cc|org)$/i.test(this))) ? true : false;
};


String.prototype.limits = function(a, b)
{
	var len = this.lenthg;
	if (a && !b)
	{
		return len >= b;
	}
	else if (b && !a)
	{
		return len <= b;
	}
	else
	{
		return len >= a && len <= b;
	}
}



var addEvent = function(o, t, f)
{
var d = 'addEventListener', n = 'on' + t, rO = o, clean = (t != 'unload');
if (o[d]) o[d](t, f, false);
else if (o.attachEvent) o.attachEvent(n, f);
else
{
clean = false;
if (!o._evts) o._evts = {};
if (!o._evts[t])
{
o._evts[t] = o[n] ? { b: o[n] } : {};
o[n] = new Function('e', 'var r = true, o = this, a = o._evts["' + t + '"], i; for (i in a) { o._f = a[i]; r = o._f(e||window.event) != false && r; o._f = null; } return r');
if (t != 'unload') clean = true;
}
if (!f._i) f._i = addEvent._i++;
o._evts[t][f._i] = f;
}
if (clean) addEvent(window, 'unload', function(){removeEvent(rO, t, f);});
};
addEvent._i = 1;

var removeEvent = function(o, t, f)
{
var d = 'removeEventListener';
try
{
if (o[d]) o[d](t, f, false);
else if (o.detachEvent) o.detachEvent('on' + t, f);
else if (o._evts && o._evts[t] && f._i) delete o._evts[t][f._i];
} catch (e) {}
};

function cancelEvent(e, c)
{
e.returnValue = false;
if (e.preventDefault) e.preventDefault();
if (c)
{
e.cancelBubble = true;
if (e.stopPropagation) e.stopPropagation();
}
};

// json begin
(function (){
var m = {
'\b': '\\b',
'\t': '\\t',
'\n': '\\n',
'\f': '\\f',
'\r': '\\r',
'"' : '\\"',
'\\': '\\\\'
},
s = {
array: function (x) {
var a = ['['], b, f, i, l = x.length, v;
for (i = 0; i < l; i += 1) {
v = x[i];
f = s[typeof v];
if (f) {
v = f(v);
if (typeof v == 'string') {
	if (b) {
		a[a.length] = ',';
	}
	a[a.length] = v;
	b = true;
}
}
}
a[a.length] = ']';
return a.join('');
},
'boolean': function (x) {
return String(x);
},
'null': function (x) {
return "null";
},
number: function (x) {
return isFinite(x) ? String(x) : 'null';
},
object: function (x) {
if (x) {
if (x instanceof Array) {
return s.array(x);
}
var a = ['{'], b, f, i, v;
for (i in x) {
v = x[i];
f = s[typeof v];
if (f) {
	v = f(v);
	if (typeof v == 'string') {
		if (b) {
			a[a.length] = ',';
		}
		a.push(s.string(i), ':', v);
		b = true;
	}
}
}
a[a.length] = '}';
return a.join('');
}
return 'null';
},
string: function (x) {
if (/["\\\x00-\x1f]/.test(x)) {
x = x.replace(/([\x00-\x1f\\"])/g, function(a, b) {
var c = m[b];
if (c) {
	return c;
}
c = b.charCodeAt();
return '\\u00' +
	Math.floor(c / 16).toString(16) +
	(c % 16).toString(16);
});
}
return '"' + x + '"';
}
};

})();
String.prototype.parseJSON = function () {
try {
return !(/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.test(
this.replace(/"(\\.|[^"\\])*"/g, ''))) &&
eval('(' + this + ')');
} catch (e) {
return false;
}
};


COOKIE = {
"get" : function(name)
{
	var rs = new RegExp("(^|)" + name + "=([^;]*)(;|$)", "gi").exec(document.cookie), tmp;
	if(tmp = rs)
	{
		return unescape(tmp[2]);
	}
	return false;
},
 
"set" : function(name, value)
{
	var expdate = new Date();
	var argv = arguments;
	var argc = arguments.length;
	var expires = (argc > 2) ? argv[2] : null;
	var path = (argc > 3) ? argv[3] : "/";
	var domain = (argc > 4) ? argv[4] : null;
	var secure = (argc > 5) ? argv[5] : false;
	if (null != expires)
	{
		expdate.setTime(expdate.getTime() + (expires * 1000));
	}
	document.cookie = name + "=" + escape(value)
	+ ((expires == null) ? "" : (";expires=" + expdate.toGMTString()))
	+ ((path == null) ? "" : (";path=" + path))
	+ ((domain == null) ? "" : (";domain=" + domain))
	+ ((secure == false) ? "" : ";secure");
},
"del" : function(name)
{
	document.cookie = name + "=";
}
};

function Ajax(url)
{
	this.url = url;
	this.ttl = 20000;
	this.async = true;
	this.callBakc = function(){};
	this.backFormat = "JSON";
	var xmlObj = null;
	var tCallback = null;
	var noCache = true;
	
	var workingBtm;
	var errorBack = function(msg)
	{
		if ("JSON" == this.backFormat)
		{
			tCallback(1, '{"message":"'+msg+'"}');
		}
		else if("XML" ==this.backFormat)
		{
			tCallback(1, '<?xml version="1.0" encoding="utf-8"?><message>'+msg+'</message>');
		}
		else
		{
			tCallback(1, msg);
		}
	};
	this.cancel = function()
	{
		try{
			xmlObj.abort();
			clearTimeout(ct);
		}catch(e){}
	};
	this.formToStr = function(f)
	{
		var queryString = "";
		var temp = "";
		for(i = 0, n = f.length; i < n ;i ++)
		{
			var e = f[i];
			var elementValue = "";
			if ("" != e.name)
			{
				var type = e.getAttribute("type");
				var tag = e.tagName.toLowerCase();
				if ("select" == tag)
				{
					for (it = 0; it < e.options.length; it ++)
					{
						if (e.options[it].selected)
						{
							temp += "&" + e.name + "=" + encodeURIComponent(e.options[it].value);
						}
					}
				}
				else
				{
					if ("checkbox" == e.type || "radio" == type)
					{
						if (e.checked)
						{
							elementValue = e.value;
						}
						else
						{
							continue;
						}
					}
					else
					{
						elementValue = e.value;
					}
					queryString += e.name + "=" + encodeURIComponent(elementValue) + "&";
				}
				
			}
		}
		return queryString + temp;
	};
	
	this.send = function(purl, pc, pcb, pm, ps, pttl, pa)
	{
		try
		{
			xmlObj = new XMLHttpRequest();
		}
		catch(e)
		{
			try
			{
				xmlObj = new ActiveXObject("MSXML2.XMLHTTP");
			}
			catch(e2)
			{
				try
				{
					xmlObj = new ActiveXObject("Microsoft.XMLHTTP");
				}
				catch(e3)
				{
					rrorBakc("ajax object init error");
					return false;
				}
			}
		}
		if (-1 != purl.indexOf("?"))
		{
			purl += "&ajax=1&rtype=" + ps.toLowerCase();
		}
		else
		{
			purl += "?ajax=1&rtype=" + ps.toLowerCase();
		}
		if (true == this.noCache)
		{
			purl += "&t=" + Math.random();
		}
		tCallback = pcb;
		xmlObj.onreadystatechange = function()
		{
			if(4 == xmlObj.readyState)
			{
				try
				{
					clearTimeout(ct);
				}catch(e){}
				try{
					workingBtm.disabled = false;
				}catch(e){}

				try{
					if(200 == xmlObj.status || 204 == xmlObj.status || 304 == xmlObj.status)
					{
						var temp = xmlObj.responseText.parseJSON();
						if (temp.jump)
						{
							location = temp.jump;
							return;
						}
						if ("JSON" == ps)
						{
							tCallback(0, temp);
						}
						else if("XML" == ps)
						{
							tCallback(0, xmlObj.responseXml);
						}
						else
						{
							tCallback(0, xmlObj.responseText);
						}
					}
					if (xmlObj.status > 399 && xmlObj.status < 500)
					{
						errorBack("clinent request error");
					}
					if (xmlObj.status > 499)
					{
						errorBack("server error");
					}
					//登录不上.
					//xmlObj.abort();
					return;
				}
				catch(e){errorBack(e)}
			}
		};
		pm = pm.toUpperCase();
		ps = ps.toUpperCase();
		this.callBack = this.pcb;
		this.backFormat = ps;
		pttl = parseInt(pttl);
		xmlObj.open(pm, purl, pa);
		var ct = setTimeout(function(){
			xmlObj.abort();
			errorBack("time out");
		}, pttl);
		if("POST" == pm)
		{
			xmlObj.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			xmlObj.send(pc);
		}
		else
		{
			xmlObj.send(null);
		}
		return true;
	};
	
	this.get = function()
	{
		var purl = (arguments[0]) ? arguments[0] : this.url;
		var pcb = (arguments[1]) ? arguments[1] : this.callBack;
		var ps = (arguments[2]) ? arguments[2] : this.backFormat;
		var pttl = (arguments[3]) ? arguments[3] : this.ttl;
		var pa = (arguments[4]) ? arguments[4] : this.async
		if(!purl)
		{
			return false;
		}

		return this.send(purl, "", pcb, "get", ps, pttl, pa);
	};

	
	this.post = function()
	{
		var purl = (arguments[0]) ? arguments[0] : this.url;
		var parant = (arguments[1]) ? arguments[1] : "";
		var pcb = (arguments[2]) ? arguments[2] : this.callBack;
		var ps = (arguments[3]) ? arguments[3] : this.backFormat;
		var pttl = (arguments[4]) ? arguments[4] : this.ttl;
		var pa = (arguments[5]) ? arguments[5] : this.async
		if(!purl)
		{
			return false;
		}		
		if (parant)
		{
			var pc = "";
			for (key in parant)
			{
				pc += "&" + encodeURIComponent(key) + "=" + encodeURIComponent(parant[key]);
			}
			pc = pc.substr(1);
		}
				try{
					kuhaDlg.loading1.style.display = "block";
				}
				catch(e){}

		return this.send(purl, pc, pcb, "POST", ps, pttl, pa);
	},
	
	this.sendForm = function()
	{
		var pf = (arguments[0]) ? arguments[0] : null;
		var pcb = (arguments[1]) ? arguments[1] : this.callBack;
		var ps = (arguments[2]) ? arguments[2] : this.backFormat;
		var pttl = (arguments[3]) ? arguments[3] : this.ttl;
		var pa = (arguments[4]) ? arguments[4] : this.async
		purl = pf.getAttribute("action") || this.url;
		pm = pf.getAttribute("method") || "post";
		pc = this.formToStr(pf);

		var btm = pf.getElementsByTagName("input");
		for(i = 0; i < btm.length; i++)
		{
			if ("submit" == btm[i].getAttribute("type"))
			{
				btm[i].disabled = true;
				workingBtm = btm[i];
				break;
			}
		}
		return this.send(purl, pc, pcb, pm, ps, pttl, pa);
	};
};

var Ajax1 = Ajax;
function showVcode(pic, update)
{
	if (!pic.getAttribute("src") || update)
	{
		pic.setAttribute("src", "/vcode.php?t=" + Math.random());
	}
	pic.style.display = "inline";
	return false;
}


function checkInputDefault(ipt, str)
{
	if (ipt.value == str)
	{
		ipt.value = "";
	}
}
function checkSearch(o, num)
{
	if (1 == num)
	{
		if ("输入关键词" == o.value)
		{
			o.value = "";
		}
		else if ("" == o.value)
		{
			o.value = "输入关键词";
		}
	}
}
function checkTopSH(form)
{
	form.keywords.value = form.keywords.value.trim();
	if("" == form.keywords.value || "输入关键词" == form.keywords.value)
	{
		alert("输入关键词");
		return false;
	}
}






var getTips;
var filterO = 0;
var fOpacity = 0;
var showVidL = null;
var hidVidL = null;
var hidtipsTt=null;
function tips(msg, n)
{
	n = n ? n : "info";
	getTips = $("tips");
	getTips.innerHTML = msg;
	getTips.className = n;
	showTips();
	getTips.onclick=hidtips;
	hidtipsTt = setTimeout("hidtips()",5000);
}
function showTips(){
	clearTimeout(showVidL);
	clearTimeout(hidVidL);
	getTips.style.display="block";
	if(document.all){
		if(getTips.style.filter == "Alpha(opacity=100)"){clearTimeout(showVidL);return;}
		filterO =filterO+10;
		getTips.style.filter="Alpha(opacity="+filterO+")";
	}else{
		if(getTips.style.opacity==1){clearTimeout(showVidL);return;}
		fOpacity = fOpacity + 1;
		getTips.style.opacity = fOpacity*0.1;
	}
	showVidL = setTimeout("showTips()",50);
}
function hidtips(){
	clearTimeout(hidtipsTt);
	clearTimeout(showVidL);
	if(document.all){
		if(getTips.style.filter == "Alpha(opacity=0)"){getTips.style.display='none';clearTimeout(hidVidL);return;}
		filterO =filterO-10;
		getTips.style.filter="Alpha(opacity="+filterO+")";
	}else{
		if(getTips.style.opacity==0){getTips.style.display="none";clearTimeout(hidVidL);return;}
		fOpacity = fOpacity - 1;
		getTips.style.opacity = fOpacity*0.1;
	}
	hidVidL = setTimeout("hidtips()",50);
}



function inputMsg(msg, o, type)
{
	switch (type)
	{
		case 'ok':
			o.className = "warning3";
			break;
			
		case "error":
			o.className = "warning2";
			break;
			
		case "wait":
			o.className = "warning1";
			break;
			
		default:
			o.calssName = type;
	}
	
	o.innerHTML = msg;
	o.style.display = "block";
}

function pageTo(c, url)
{
	location = url.replace('<page>', c.value);
}

var showPopDivTime;
function showPopDiv(o)
{
	clearTimeout(showPopDivTime);
	o.style.display = "block";
}
function hidePopDiv(o)
{
	clearTimeout(showPopDivTime);
	showPopDivTime = setTimeout(function(){o.style.display = "none";}, 500);
}


function checkAll(list, form, flags)
{
	for (i = 0; i < list.length; i++)
	{
		if ("checkbox" == list[i].getAttribute("type"))
		{
			if (flags)
			{
				list[i].checked = form.checked
			}
			else
			{
				list[i].checked = list[i].checked ? false : true;
			}
		}
	}
}

function inArray(arr, key)
{
	for (i = 0; i < arr.length; i++)
	{
		if (arr[i] == key)
		{
			return true;
		}
	}
	return false;
}
function showRules(show)
{
	if (1 == show)
	{
		COOKIE.set("show_rules_" + uid, 1, 86400000 * 30);
		b("rules_content").style.display = "block";
		b("rules_title").style.display = "none";
	}
	else
	{
		COOKIE.del("show_rules_" + uid);
		b("rules_content").style.display = "none";
		//b("rules_title").innerHTML = '　　　　　　　　　　　　　　<a href="javascript:;" onclick="return showRules(1)">发布规则</a>';
		b("rules_title").style.display = "block";

	}
	return false;
}
