function calender(){
setInterval("aa.innerHTML=new Date().toLocaleString()+' 星期'+'日一二三四五六'.charAt(new Date().getDay());",1000);
}
//基本函数
function $(tmp){
	return document.getElementById(tmp);
}
String.prototype.trim = function(){
    return this.replace(/(^\s*)|(\s*$)/g, "");
}
String.prototype.ltrim = function(){
    return this.replace(/(^\s*)/g, "");
}
String.prototype.rtrim = function(){
    return this.replace(/(\s*$)/g, "");
}
function isregname(value){
	var reg =/^[a-zA-Z0-9_\u4e00-\u9fa5]+$/ ;
	return reg.test(value);
} 

//用户注销
function logout(){
	var xmlhttp;
	var url="http://www.17diyba.cn/includes/ajax/logout.php?time="+new Date().getTime();
	try{
		xmlhttp=new XMLHttpRequest();
	}catch(e){
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function(){
		if (4==xmlhttp.readyState){
			if (200==xmlhttp.status){
                // document.location = document.location;
                ajaxCheckLogin();
			}else{
                  alert("服务端返回状态" + xmlhttp.statusText);
			}
		}
	}
	xmlhttp.open("GET", url, true);
	xmlhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded');
	xmlhttp.send(null);	
}

function ajaxChangeEmail(url){
	var xmlhttp;
	try{
		xmlhttp=new XMLHttpRequest();
	}catch(e){
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function(){
		if (4==xmlhttp.readyState){
			if (200==xmlhttp.status){
				alert(xmlhttp.responseText);
			}else{
                  alert("服务端返回状态" + xmlhttp.statusText);
			}
		}
	}
	xmlhttp.open("GET", url, true);
	xmlhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded');
	xmlhttp.send(null);	
}

function ajaxCheckLogin(){
	var url="http://www.17diyba.cn/includes/ajax/checkLogin.php?time="+new Date().getTime();
	var xmlhttp;
	try{
		xmlhttp=new XMLHttpRequest();
	}catch(e){
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function(){
		if (4==xmlhttp.readyState){
			if (200==xmlhttp.status){
				eval(xmlhttp.responseText);
			}else{
                  alert("服务端返回状态" + xmlhttp.statusText);
			}
		}
	}
	xmlhttp.open("GET", url, true);
	xmlhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded');
	xmlhttp.send(null);	
}

function checkTitleUK(title){
	var url="http://www.17diyba.cn/includes/ajax/checkArticleTitleUk.php?title="+title+"&time="+new Date().getTime();
	try{
		xmlhttp=new XMLHttpRequest();
	}catch(e){
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function(){
		if (4==xmlhttp.readyState){
			if (200==xmlhttp.status){	
				if(xmlhttp.responseText!='0') {
					alert('标题已经存在了！');
					window.open('article.php?id='+xmlhttp.responseText);
				}
			}else{
                  alert("服务端返回状态" + xmlhttp.statusText);
			}
		}
	}
	xmlhttp.open("GET", url, true);
	xmlhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded');
	xmlhttp.send(null);
}

function changeEmail(id,email){
	ajaxChangeEmail("http://www.17diyba.cn/includes/ajax/changemail.php?action=changeEmail&id="+id+"&email="+email+"&time="+new Date().getTime());
}
function changeAcceptEmail(id,val){
	var accept = val?1:0;
	var url="http://www.17diyba.cn/includes/ajax/changemail.php?action=changeAcceptEmail&id="+id+"&accept="+accept+"&time="+new Date().getTime();
	ajaxChangeEmail(url);
}
//文章展开与折叠
function hiddenOrshow(show,articleId){
	var sp = $('article_'+articleId+'_s');
	var lp = $('article_'+articleId+'_l');
	var href = $('article_'+articleId+'_href'); 
	if(show){
		sp.style.display = 'none';
		lp.style.display = 'block';
		href.innerHTML='<<折叠';
		href.href = 'javascript:hiddenOrshow(false,'+articleId+')';
	}
	else{
		lp.style.display = 'none';
		sp.style.display = 'block';
		href.innerHTML='展开>>';
		href.href = 'javascript:hiddenOrshow(true,'+articleId+')';
	}
}

//获取文章评论页
function goto_articleRemarkPage(articleId,totalcount,pagesize,pagenum){
 	var url="http://www.17diyba.cn/includes/ajax/getArticleRemarkPage.php";
	try{
		xmlhttp=new XMLHttpRequest();
	}catch(e){
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function(){
		if (4==xmlhttp.readyState){
			if (200==xmlhttp.status){	
				$('remarkDiv').innerHTML = xmlhttp.responseText;
			}else{
                  alert("服务端返回状态" + xmlhttp.statusText);
			}
		}
	}
	xmlhttp.open("POST", url, true);
	xmlhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded');
	xmlhttp.send("articleId="+articleId+"&totalcount="+totalcount+"&pagesize="+pagesize+"&pagenum="+pagenum);
	
}

//切换Tab 
function changeMenu(pre,total,to){
  	$(pre+'menu'+to).className = "tabOn";
  	$(pre+'menu'+to).innerHTML = $(pre+'menu'+to).title;
  	$(pre+'menu_content_'+to).style.display = 'block';
  	for(var i=1;i<=total;i++){
  		if(i!=to){
  			$(pre+'menu'+i).className = "tabOff";
  			$(pre+'menu'+i).innerHTML = "<a href='javascript:changeMenu(\""+pre+"\","+total+","+i+")' class='tabLink'>"+$(pre+'menu'+i).title+"</a>";
  			$(pre+'menu_content_'+i).style.display = 'none';
  		}
  	}
  }
 
 //首页显示tag文章列表tab切换
 function showArticleList(total,i){
 	if(i<0) $('articleDiv').style.display = 'none';
 	else $('articleDiv').style.display = 'block';
 	for(var k=0;k<total;k++){
		 $('articleListDiv_'+k).style.display='none';
		 $('articleDemoDiv_'+k).style.display='none';
	}
 	if(i>=0){
	 	$('articleListDiv_'+i).style.display='block';
	 	$('articleDemoDiv_'+i).style.display='block';
 	}
 }


 function copyArticle(){
 	 setTimeout( function () { 
 	 var text = clipboardData.getData("text"); 
 	 if (text) {text = text + "\r\n\r\n该文章转自[一起DIY吧] 原文链接："+location.href; clipboardData.setData("text", text); } }, 100 )
 }

function search(){
	document.location.href = 'search.php';
}
function closeDiv(div){
	$(div).style.display = 'none';
}
function reg(){
	$('findPwdDiv').style.display = 'none';
	$('regDiv').style.display = 'block';
	$('username').focus();
}
function findPwd(){
	$('regDiv').style.display = 'none';
	$('findPwdDiv').style.display = 'block';
	$('find_username').focus();
}
function checkUserNameFormat(v){
 	if(v==''){
 		$('username_tip').innerHTML ="<font color='red'>用户名不能为空！</font>";
 	}
 	else if(!isregname(v)){
 		$('username_tip').innerHTML="<font color='red'>用户名只能包含：字母，汉字，数字，下划线！</font>";
 	}
 	else{
		var xmlhttp;
		var url="http://www.17diyba.cn/includes/ajax/validateUserName.php?username="+v+"&time="+new Date().getTime();
		try{
			xmlhttp=new XMLHttpRequest();
		}catch(e){
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
		xmlhttp.onreadystatechange=function(){
			if (4==xmlhttp.readyState){
				if (200==xmlhttp.status){
                  	if(xmlhttp.responseText!='ok')
						$('username_tip').innerHTML = "<font color='red'>该用户名已经存在！</font>";
					else
						$('username_tip').innerHTML = "<font color='green'>OK!</font>";
				}else{
                   alert("服务端返回状态" + xmlhttp.statusText);
				}
			}
           else {
           		$('username_tip').innerHTML = "<font color='red'>验证中...</font>";
			}
		}
		xmlhttp.open("GET", url, true);
		xmlhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded');
		xmlhttp.send(null);
 	}
 }
 function checkPassword1Format(v){
 	if(v.length<3) $('password1_tip').innerHTML = "<font color='red'>密码太短(3到12个字符)!</font>";
 	else if(v.length>12) $('password1_tip').innerHTML = "<font color='red'>密码太长(3到12个字符)<！</font>";
 	else $('password1_tip').innerHTML = "<font color='green'>OK!</font>";
 }
 function checkPassword2Format(v){
 	if(v=='') $('password2_tip').innerHTML = "<font color='red'>请再次输入密码!</font>";
 	else if(v!=$('password1').value) $('password2_tip').innerHTML = "<font color='red'>两次密码输入不一致!</font>";
 	else $('password2_tip').innerHTML = "<font color='green'>OK!</font>";
 }
 function checkEmailFormat(v){
 	var emailReg =/^[\w-]+(\\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/ ;
 	if(v=='') $('email_tip').innerHTML = "<font color='red'>Email不能为空!</font>";
 	else if(!emailReg.test(v)) $('email_tip').innerHTML = "<font color='red'>Email格式不正确!</font>";
 	else $('email_tip').innerHTML = "<font color='green'>OK!</font>";
 }