/**
* 检测是否为0-9数字
* @param string s 字符串
* @return string
*/
function isnumeric(s){
	var patrn=/^\d*$/;
		if (!patrn.exec(s)){
			return false;
		}
	return true;
}

/**
* 位數限制
* @param string s 字符串
* @param int lmin 最小位數
* @param int lmax 最大位數
* @return bool
*/
function checklen(s,lmin,lmax){
	if(s.length<lmin || s.length>lmax){
		return false;
	}else{
		return true;
	}
}

/**
* 檢查身份證號
* @param string s 身份證號
* @return bool
*/
function checkidcard(s,flag){
	if(flag==1){
		var pattern = /^[A-Za-z][0-9]{4}$/;
		return pattern.test(s);
	}else{
		var pattern = /^[A-Za-z][A-Za-z][0-9]{3}$/;
		return pattern.test(s);
	}
}

/**
* 去字符串前后空格
* @param string s 字符串
* @return string
*/
function trim(s){
	return s.replace(/(^\s*)|(\s*$)/g, "");
}

/**
* 判斷是否為空
* @param object obj 控件
* @param string mess 出錯信息
* @return bool
*/
function checkempty(obj,mess){
	if(trim(obj.value)==''){
		alert(mess);
		obj.focus();
		return false;
	}
}

/**
* 判断是否网址
* @param string str 字符串
* @return bool
*/
function ishttp(str){
	var pattern =/^(http:\/\/)?[a-zA-Z0-9\-_]+\.[a-zA-Z0-9\-_%\?\.=&;\\]+/;

	if (pattern.test(str)){
		return true;
	}else{
		return false;
	}
}


/**
* 只能是中文，空格
* @param string str 字符串
* @return bool
*/
function ischinese(str){
	var pattern = /^[\u4e00-\u9fa5]+$/i;

	if (!pattern.test(str)){
		return false;
	}else{
		return true;
	}
}

/**
* 只能是英文，空格
* @param string str 字符串
* @return bool
*/
function isenglish(str){
	var pattern = /^[a-zA-Z\s]+$/i;

	if (!pattern.test(str)){
		return false;
	}else{
		return true;
	}
}

/*
*不能是中文
*/
function havechinese(str){
if(/.*[\u4e00-\u9fa5]+.*$/.test(str))
{
	return true;
}
	return false;
}

/**
* 只能是英文，下劃線，中劃線
* @param string str 字符串
* @return bool
*/
function isusername(str){
	var pattern = /^[a-zA-Z_\-\s]+$/i;

	if (!pattern.test(str)){
		return false;
	}else{
		return true;
	}
}


/**
* 只能是英文，下劃線，中劃線，數字，中文
* @param string str 字符串
* @return bool
*/
function isusernamex(str){
	var pattern = /^[a-zA-Z0-9\u4e00-\u9fa5_\-\s]+$/i;

	if (!pattern.test(str)){
		return false;
	}else{
		return true;
	}
}

/**
* 检测email
* @param string email 字符串
* @return bool
*/
function ismail(email){
	return(new RegExp(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/).test(email));
}


/**
* 检测後綴
* @param object obj 控件
* @param string ext 允許的後綴名,例如|.jpg|.jpeg|
* @return bool
*/
function checkext(obj,ext){
fileext=obj.value.substr(obj.value.lastIndexOf(".")).toLowerCase();	//取后缀

	if(ext.indexOf("|"+fileext+"|")==-1){		//后缀不正确
		return false;
	}else{
		return true;
	}
}

/**
* 检测後綴
* @param object obj 控件
* @param string ext 允許的後綴名,例如|.jpg|.jpeg|
* @return bool
*/
function checkextvalue(values,ext){
fileext=values.substr(values.lastIndexOf(".")).toLowerCase();	//取后缀

	if(ext.indexOf("|"+fileext+"|")==-1){		//后缀不正确
		return false;
	}else{
		return true;
	}
}

/**
* 判断字符串长度
* @param string str 字符串
* @return int
*/
function getlen(str) {
   var totallength=0;
   for (var i=0;i<str.length;i++) {
	var intCode=str.charCodeAt(i);
		if (intCode>=0&&intCode<=128) {
     		totallength=totallength+1; //非中文单个字符长度加 1
    	}else {
     		totallength=totallength+2; //中文字符长度则加 2
    	}
   } //end for
 return totallength;
}


/**
* 時間比較
* @param datetime beginTime 開始時間 2009-01-01 12:00:00
* @param datetime endTime 結束時間 2009-01-01 12:00:00
* @return int
*/
function comptime(beginTime,endTime){

var beginTimes=beginTime.substring(0,10).split('-');
var endTimes=endTime.substring(0,10).split('-');

beginTime=beginTimes[1]+'/'+beginTimes[2]+'/'+beginTimes[0]+' '+beginTime.substring(10,19);
endTime=endTimes[1]+'/'+endTimes[2]+'/'+endTimes[0]+' '+endTime.substring(10,19);

var a =(Date.parse(endTime)-Date.parse(beginTime))/3600/1000;
	if(a<0){
		return -1;
	}else if (a>0){
		return 1;
	}else if (a==0){
		return 0;
	}else{
		return 'exception'
	}
}


/**
* 預加載圖片
* @param array img 圖片數組
*/
function imgpreload(img){
	var image = new Array();
	for(var i=0;i<img.length;i++){
		image[i] = new Image();
		image[i].src = img[i];
	}
}

/**
* 驗證youtube link
* @param string str youtube url
* return bool
*/
function checkyoutube(str){
	reg = /http:\/\/www.youtube.com\/watch\?v=/;
	if(reg.test(str))
		return true;
	else
		return false;
}

/**
* 創建xmlhttp
* @return obj xmlhttp對象
*/

function createxmlhttp(){
	var xmlhttp = false;
	try {
  	xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e){
  	try {
    	xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  	} catch (e2) {
    	xmlhttp = false;
  	}
	}
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
  	xmlhttp = new XMLHttpRequest();
	}

	return xmlhttp;
}

/**
* 顯示flash
* @param string swf flash文件
* @param int width 寬度
* @param int height 高度
* @return string
*/

function showFlash(swf,width,height){
	fcode = "<object width=\""+width+"\" height=\""+height+"\"><param name=\"movie\" value=\""+swf+"\" /><param name=\"wmode\" value=\"transparent\" /><param name=\"allowFullScreen\" value=\"true\" /><param name=\"allowscriptaccess\" value=\"always\" /><embed src=\""+swf+"\" type=\"application/x-shockwave-flash\" allowscriptaccess=\"always\" allowfullscreen=\"true\" wmode=\"transparent\"  width=\""+width+"\" height=\""+height+"\"></embed></object>";
	return fcode;
}

/**
*替換
*/
 function ReplaceAll(str, sptr, sptr1)
	{
	while (str.indexOf(sptr) >= 0)
	{
	   str = str.replace(sptr, sptr1);
	}
	return str;
	}



/**
* 全站留言列表
* @param string curl 請求路徑
*/
function ReplyList(curl){
	$.ajax({
		type:"GET",
		url:curl,
		dataType:"html",
		cache:false,
		success:function(msg){
			$("#replylist").html(msg);
			document.getElementById("rpck").src = "/checkcode.php?t="+Math.random();
		}
	});
}

/**
* 發表留言
*/
function AddReply(){

	if(document.getElementById("rplogin").value=="0"){
		if(trim(document.getElementById("rpuname").value)=='' && document.getElementById("rpguest").value==0){
			alert("請輸入姓名！");
			document.getElementById("rpuname").focus();
			return false;
		}

		if(trim(document.getElementById("rpuname").value)!=''){
			if(isusernamex(document.getElementById("rpuname").value)==false){
				alert("姓名只能是字母、數字、中文或_或-！");
				document.getElementById("rpuname").focus();
				return false;			
			}
		}

		if(document.getElementById("rpguest").value==0){			//不可Guest
			if(trim(document.getElementById("rppass").value)==''){
				alert("請輸入密碼！");
				document.getElementById("rppass").focus();
				return false;
			}
		}
	}

	if(trim(document.getElementById("rpcontent").value)==''){
		alert("請輸入你的回應！");
		document.getElementById("rpcontent").focus();
		return false;
	}

	if(getlen(document.getElementById("rpcontent").value)>300){
		alert("回應不能超過150個中文字符！");
		document.getElementById("rpcontent").focus();
		return false;
	}

	if(trim(document.getElementById("rpcode").value)==''){
		alert("請輸入驗證碼！");
		document.getElementById("rpcode").focus();
		return false;
	}

	var stra = "rpuname=" + encodeURIComponent(document.getElementById("rpuname").value) + "&rppass=" + encodeURIComponent(document.getElementById("rppass").value) + "&rpcontent=" + encodeURIComponent(document.getElementById("rpcontent").value) + "&rpcode=" + encodeURIComponent(document.getElementById("rpcode").value) + "&tablename=" + encodeURIComponent(document.getElementById("tablename").value) + "&topicid=" + encodeURIComponent(document.getElementById("topicid").value) + "&idname=" + document.getElementById("idname").value + "&rpguest=" + document.getElementById("rpguest").value;

	$.ajax({
		type:"POST",
		url:"/index.php?controller=reply&action=Add",
		data:stra,
		dataType:"json",
		cache:false,
		success:function(msg){
			if(msg['error']=="false"){
				alert("回應提交成功！");
				//統一登入
					$.ajax({
						type:"POST",
						url:"/index.php?controller=login&action=PostLogin&username=" + encodeURIComponent(document.getElementById("rpuname").value) + "&password=" + encodeURIComponent(document.getElementById("rppass").value),
						dataType:"html",
						cache:false,
						success:function(msg){
							$("#relogin").html(msg);
							ReplyList("/reply/List/tablename/" + document.getElementById("tablename").value + "/topicid/" + document.getElementById("topicid").value + "/idname/" + document.getElementById("idname").value);
						}
					});
			}else if(msg['error']=="1"){
				alert(msg['errorMsg']);
				document.getElementById("rpcode").value = "";
				document.getElementById("rpcode").focus();
				document.getElementById("rpck").src = "/checkcode.php?t="+Math.random();
				return false;
			}else if(msg['error']=="2"){
				alert(msg['errorMsg']);
				document.getElementById("rpuname").focus();
				document.getElementById("rpck").src = "/checkcode.php?t="+Math.random();
				return false;
			}else if(msg['error']=="3"){
				alert(msg['errorMsg']);
				document.getElementById("rpcontent").focus();
				document.getElementById("rpck").src = "/checkcode.php?t="+Math.random();
				return false;
			}else if(msg['error']=="4"){
				alert(msg['errorMsg']);
				document.getElementById("rppass").value = "";
				document.getElementById("rpck").src = "/checkcode.php?t="+Math.random();
				return false;
			}else if(msg['error']=="5"){
				alert(msg['errorMsg']);
				document.getElementById("rpcontent").focus();
				document.getElementById("rpck").src = "/checkcode.php?t="+Math.random();
				return false;
			}
		}
	});

}

/**
* 刪除留言
*/
function replyDel(id){

	$.ajax({
		type:"GET",
		url:"/reply/replyDel/replyid/"+id,
		dataType:"json",
		cache:false,
		success:function(msg){
			if(msg['isSuccess']=="true"){
				alert("回應刪除成功！");
				ReplyList("/reply/List/tablename/" + document.getElementById("tablename").value + "/topicid/" + document.getElementById("topicid").value + "/page/" + document.getElementById("rppage").value + "/idname/" + document.getElementById("idname").value);
			}else{
				alert(msg['errorMsg']);
				return false;
			}
		}
	});
}

/**
* more 搜索
* @param int flag 1右上角查詢 2搜索頁面查詢
*/
function moresearch(flag){
	if(flag==0){
		/*
		if(trim(document.outsearch.searchkey.value)==""){
			alert("請輸入要搜索的關鍵字！");
			document.outsearch.searchkey.focus();
			return false;
		}*/
		document.outsearch.submit();
	}else{
		/*
		if(trim(document.insearch.searchkey.value)==""){
			alert("請輸入要搜索的關鍵字！");
			document.insearch.searchkey.focus();
			return false;
		}*/
		document.insearch.submit();
	}
}

/**
* 分享至facebook
*/
function shareFacebook()
{
	var u=location.href;
	try{t=document.title}catch(e){};
	window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
	return false;
}