﻿// $$(ID);
// SetDiv(URL,ID); 
// SetPage(Count,Size,Length,Now,Query{_auto}); 
// Query(ID); 
// ChkLength(ID,min,[Max]); 
// TextCount(ID{_i},Maxlength);

function $$(ID){return document.getElementById(ID);}
function SetDiv(Url,DivId,Fun){ 
	var ajax;
	try{ajax = new XMLHttpRequest();}
	catch(e){
		try{ajax=new ActiveXObject("Msxml2.XMLHTTP");}
		catch(e){
			try{ajax=new ActiveXObject("Microsoft.XMLHTTP");}
			catch(e){$$(DivId).innerHTML="No AJAX";return false;}
		}
	}
	ajax.onreadystatechange=function(){
		if(ajax.readyState==4){
			if(ajax.status==200){$$(DivId).innerHTML=ajax.responseText;}
			else{$$(DivId).innerHTML="Sorry.";}
			if(Fun==true){SetedDiv(ajax.status==200?"true":"false");}
		}   
	}
	var d=new Date();
	if(Url.indexOf("?")<0){Url+="?"+d.getTime();}
	else{Url+="&"+d.getTime();}
	ajax.open("get", Url, true);
	ajax.send(null);
}
function Query(Key){
	var s=window.location.search;
	if(s==""){return"";}
	var q=s.substr(1).split("&");
	for(var i=0;i<q.length;i++){
		var n=q[i].split("=");
		if(n[0].toUpperCase()==Key.toUpperCase()){return decodeURIComponent(n[1].replace(/\+/g," "));}
	}
	return"";
}
//ShowNote
function ShowNote(note){
	var noteIfm=(note==undefined?Query("note"):note);
	if(note==undefined) {
		document.write("<div id='_noteBox'></div>");
		if(noteIfm!=""){setTimeout("$$('_note').style.display='none'",5000);}
	}
    if(noteIfm!=""){
		$$("_noteBox").innerHTML="<a href='javascript:' onclick='javascript:$$(\"_note\").style.display=($$(\"_note\").style.display==\"none\"?\"block\":\"none\");this.blur()' style='float:right;text-decoration:none;color:#f00;margin:-20px 10px 0 0'>[消息]</a><div id='_note' style='padding:8px 15px;background:#FFC;font-size:10pt'>"+noteIfm+"</div>";
	}
}
function ChkLength(I,m,M){
	var l=$$(I).value.replace(/(^\s*)|(\s*$)/g,"").length;
	if(l<m){return false;}
	if(M!=undefined&&l>M){return false;}
	return true;
}
function TextCount(o,m){
	if(o.value.length>m){o.value=o.value.substring(0,m);}
	else{$$(o.id+"_i").innerHTML="还能输入"+(m-o.value.length)+"字";}
}
//分页
function SetPage(nCount,pSize,pLen,pNow,query){
if(query=="_auto"){
	query="";
	var t=location.search.substr(1).split("&");
	for(i=0;i<t.length;i++)
		if(t[i]!="" && t[i].substring(0,2)!="p=" && t[i].substring(0,5)!="note="){query+=t[i]+"&";}
}
if(nCount>pSize){
	var pStart=1,pEnd=1;
	var pEndOut=false;
	var pageStyle="";
	var pCount=Math.ceil(nCount/pSize);
	if(pNow>pCount){pNow=pCount;}
	if(pNow!=1){pageStyle+="<a href='?p="+(pNow-1)+"&"+query+"'>上一页</a> ";}
	if(pNow>(pLen/2+1)){
		pageStyle+="<a href='?p=1&"+query+"'>1 ..</a> ";
		pStart=Math.ceil(pNow-pLen/2+1);
	}
	if(pStart+pLen+1>=pCount){
		pEnd=pCount;
		pEndOut=true;
	}
	else{pEnd=pStart+pLen;}
	for(i=pStart;i<pNow;i++)pageStyle+="<a href='?p="+i+"&"+query+"'>"+i+"</a> ";
	pageStyle+="<b>"+pNow+"</b> ";
	for(i=pNow+1;i<=pEnd;i++)pageStyle+="<a href='?p="+i+"&"+query+"'>"+i+"</a> ";
	if(!pEndOut){pageStyle+="<a href='?p="+pCount+"&"+query+"'>.. "+pCount+"</a> ";}
	if(pNow!=pCount){pageStyle+="<a href='?p="+(pNow + 1)+"&"+query+"'>下一页</a> ";}
	document.write(pageStyle);
}
else if(pNow>1){document.write("<a href='?p=1&"+query+"'>上一页</a> <a href='?p=1&"+query+"'>1</a> ");}
}

//cookie
function SetCookie(name,value,expire){
	var exp=new Date();
	exp.setTime(exp.getTime()+expire);
	document.cookie=name+"="+escape(value)+";expires="+exp.toGMTString();
}
function getCookie(name){
	var arr=document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
	if(arr!=null){return unescape(arr[2]);}return null;
}
function delCookie(name){
	var exp=new Date();
	exp.setTime(exp.getTime()-1);
	var cval=getCookie(name);
	if(cval!=null){document.cookie=name+"="+cval+";expires="+exp.toGMTString();}
}
