//定义xmlhttp
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();
}


//創建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;
}

// 检测是否为0-9数字	-------------------------------------------------------------------------------------------------------------------------
function isnumeric(s){
	var patrn=/^\d*$/;
		if (!patrn.exec(s)){
			return false;
		}
	return true;
}



// 去字符串前后空格   -------------------------------------------------------------------------------------------------------------------------
function trim(s){
	return s.replace(/(^\s*)|(\s*$)/g, "");  
}


// 检测用户名,密码
function isAccount(str,flag){

	if(flag==1){
		if(/^[a-z]\w{3,10}$/i.test(str)){	
			return true;
		}else{
			return false;	
		}
	}else if(flag==2){
		var patrn=/^(\w){6,20}$/;
		if (!patrn.exec(str)){
			return false;
		}else{
			return true;
		}
	}else if(flag==3){
		if(/^[a-z]\w{3,20}$/i.test(str)){	
			return true;
		}else{
			return false;
		}
	}else if(flag==4){
		if(/^[a-z]\w{3,15}$/i.test(str)){	
			return true;
		}else{
			return false;	
		}
	}else if(flag==5){
		var pattern = /^[a-zA-Z\s]+$/i;

		if (!pattern.test(str)){
			return false;
		}else{
			return true;	
		}
	}
}


//判断是否网址
function ishttp(str){
	var pattern =/^(http:\/\/)?[a-zA-Z0-9]+\.[a-zA-Z0-9\-_]+\.[a-zA-Z0-9\-_%\?\.=&;\\]+/;

	if (pattern.test(str)){
		return true;
	}else{
		return false;	
	}
}


// 检测tag 字母,数字,中文,空格
function isChinese(str){
	var pattern = /^[0-9a-zA-Z\u4e00-\u9fa5\s]+$/i;

	if (!pattern.test(str)){
		return false;
	}else{
		return true;	
	}
}

//只能是中文，空格
function isChinesex(str){
	var pattern = /^[\u4e00-\u9fa5\s]+$/i;

	if (!pattern.test(str)){
		return false;
	}else{
		return true;	
	}
}

// 判断是否为空	   	-------------------------------------------------------------------------------------------------------------------------
/*
	obj 控件名
	mess 错误信息
*/
function checkempty(obj,mess){
	if(trim(obj.value)==''){
		alert(mess);
		obj.focus();		
		return false;
	}
}


//检测email
function ismail(email){
	return(new RegExp(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/).test(email));
}

//检测图片 			-------------------------------------------------------------------------------------------------------------------------
function checkphoto(obj){
var AllImgExt="|.jpg|.jpeg|";	//定义允许的后缀

fileext=obj.value.substr(obj.value.lastIndexOf(".")).toLowerCase();	//取后缀

if(AllImgExt.indexOf("|"+fileext+"|")==-1){		//后缀不正确
	return false;
}
}

function checkphotoall(obj){
var AllImgExt="|.jpg|.jpeg|.gif|.png|";	//定义允许的后缀

fileext=obj.value.substr(obj.value.lastIndexOf(".")).toLowerCase();	//取后缀

if(AllImgExt.indexOf("|"+fileext+"|")==-1){		//后缀不正确
	return false;
}
}

//检测zip 			-------------------------------------------------------------------------------------------------------------------------
function checkzip(obj){
var AllImgExt="|.zip|";	//定义允许的后缀

fileext=obj.value.substr(obj.value.lastIndexOf(".")).toLowerCase();	//取后缀

if(AllImgExt.indexOf("|"+fileext+"|")==-1){		//后缀不正确
	return false;
}
}

//检测swf 			-------------------------------------------------------------------------------------------------------------------------
function checkswf(obj){
var AllImgExt="|.swf|";	//定义允许的后缀

fileext=obj.value.substr(obj.value.lastIndexOf(".")).toLowerCase();	//取后缀

if(AllImgExt.indexOf("|"+fileext+"|")==-1){		//后缀不正确
	return false;
}
}

//檢測pdf
function checkpdf(obj){
var AllImgExt="|.pdf|";	//定义允许的后缀

fileext=obj.value.substr(obj.value.lastIndexOf(".")).toLowerCase();	//取后缀

if(AllImgExt.indexOf("|"+fileext+"|")==-1){		//后缀不正确
	return false;
}
}

//检测excel 			-------------------------------------------------------------------------------------------------------------------------
function checkexcel(obj){
var AllImgExt="|.xls|";	//定义允许的后缀

fileext=obj.value.substr(obj.value.lastIndexOf(".")).toLowerCase();	//取后缀

if(AllImgExt.indexOf("|"+fileext+"|")==-1){		//后缀不正确
	return false;
}
}

//判断字符串长度
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;
}


//页数跳转 			-------------------------------------------------------------------------------------------------------------------------
function gopage(path,para){
	var jumppage=document.getElementById("jumppage").value;
	if(jumppage!="0"){
		window.location=path+"&page="+jumppage+para;
	}
}


function gopagex(path,tname,para){
	var jumppage=document.getElementById("jumppage").value;
	if(jumppage!="0"){
		window.location=path+"?"+tname+"="+jumppage+para;
	}
}


function DrawImage(ImgD,twidth,theight){ 
 var flag=false; 
 var image=new Image(); 
 var iwidth = twidth;  //定义允许图片宽度 
 var iheight = theight;  //定义允许图片高度 
 image.src=ImgD.src; 
 if(image.width>0 && image.height>0){ 
 flag=true; 
 if(image.width/image.height>= iwidth/iheight){ 
  if(image.width>iwidth){   
  ImgD.width=iwidth; 
  ImgD.height=(image.height*iwidth)/image.width; 
  }else{ 
  ImgD.width=image.width;   
  ImgD.height=image.height; 
  } 
  //ImgD.alt=image.width+"×"+image.height; 
  } 
 else{ 
  if(image.height>iheight){   
  ImgD.height=iheight; 
  ImgD.width=(image.width*iheight)/image.height;   
  }else{ 
  ImgD.width=image.width;   
  ImgD.height=image.height; 
  } 
  //ImgD.alt=image.width+"×"+image.height; 
  } 
 } 
}

//時間比較
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);

// alert(beginTime+endTime+beginTime);
// alert(Date.parse(endTime));
// alert(Date.parse(beginTime));
 
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'
}

}


//顯示字符數
/*
	c:控件名稱
	currnum:顯示當前字符數的id
*/
function showwordnum(c,currnum){
	document.getElementById(currnum).innerHTML = getLen(c.value)/2;
}


//預加載圖片
function imgpreload(img){
	var image = new Array();
	for(var i=0;i<img.length;i++){
		image[i] = new Image();
		image[i].src = img[i];
	}
}


//会员注册 ----------------------------------------------------------------------------
function reg(){

	if(checkempty(document.form1.checkcode,"請輸入Captcha！")==false){
		return false;
	}

	if(checkempty(document.form1.username,"請輸入Login Name！")==false){
		return false;
	}

	if(isAccount(document.form1.username.value,4)==false){
		alert("Login Name由字母、數字、下劃線組成，且只能以字母開頭，長度為4-15位，小寫！");
		document.form1.username.focus();
		return false;
	}

	if(checkempty(document.form1.email,"請輸入Email！")==false){
		return false;
	}

	if(ismail(document.form1.email.value)==false){;
		alert("Email地址不正確！");
		document.form1.email.focus();
		return false;
	}

	if(checkempty(document.form1.password,"請輸入Password！")==false){
		return false;
	}

	if(isAccount(document.form1.password.value,2)==false){
		alert("Password由字母、數字、下劃線組成，長度為6-20位！");
		document.form1.password.focus();
		return false;
	}

	if(checkempty(document.form1.tpassword,"請輸入Re-type Password！")==false){
		return false;
	}

	if(isAccount(document.form1.tpassword.value,2)==false){
		alert("Re-type Password由字母、數字、下劃線組成，長度為6-20位！");
		document.form1.tpassword.focus();
		return false;
	}

	if(document.form1.password.value!=document.form1.tpassword.value){
		alert("Re-type Password 與 Password不一致！");
		document.form1.password.focus();
		return false;
	}

	if(trim(document.form1.cname.value)!=''){
		if(isChinesex(document.form1.cname.value)==false){
			alert("Full Name In Chinese 必須是中文！");
			document.form1.cname.focus();
			return false;
		}
	}

	if(checkempty(document.form1.ename,"請輸入Full Name In English！")==false){
		return false;
	}

	if(isAccount(document.form1.ename.value,5)==false){
		alert("Full Name In English 必須是英文！");
		document.form1.ename.focus();
		return false;
	}

	if(document.getElementById("sex1").checked==false && document.getElementById("sex2").checked==false && document.getElementById("sex3").checked==false){
		alert("請選擇Salutation！");
		document.getElementById("sex1").focus();
		return false;
	}


	//if(checkempty(document.getElementById("photo"),"請輸入Change Picture！")==false){
	//	return false;
	//}
	if(trim(document.getElementById("photo").value)!=''){
		if(checkphoto(document.getElementById("photo"))==false){
			alert("User Icon格式只能是JPG！");
			document.getElementById("photo").focus();
			return false;
		}	
	}

	if(checkempty(document.form1.mobile,"請輸入Mobile No！")==false){
		return false;
	}

	if(isnumeric(document.form1.mobile.value)==false){
		alert("Mobile No必須是數字！");
		document.form1.mobile.focus();
		return false;
	}

	if(trim(document.form1.mobile.value)!=''){
		if(isnumeric(document.form1.mobile.value)==false){
			alert("Mobile No必須是數字！");
			document.form1.mobile.focus();
			return false;
		}
	}

	if(document.form1.mm.value!='' || document.form1.dd.value!='' || document.form1.yy.value!=''){

		if(isnumeric(document.form1.mm.value)==false){
			alert("Date Of Brith 月份不正確！");
			document.form1.mm.focus();
			return false;
		}else if(document.form1.mm.value>12 || document.form1.mm.value<1){
			alert("Date Of Brith 月份不正確！");
			document.form1.mm.focus();
			return false;;
		}


		if(isnumeric(document.form1.dd.value)==false){
			alert("Date Of Brith 日不正確！");
			document.form1.dd.focus();
			return false;
		}else if(document.form1.dd.value>31 || document.form1.dd.value<1){
			alert("Date Of Brith 日不正確！");
			document.form1.dd.focus();
			return false;;
		}

		var curyear1,curyear2;
		var curyear = new Date();

		curyear1=curyear.getFullYear()-15;
		curyear2=curyear.getFullYear()-60;

		if(isnumeric(document.form1.yy.value)==false){
			alert("Date Of Brith 年份不正確！");
			document.form1.yy.focus();
			return false;
		}else if(document.form1.yy.value>curyear1 || document.form1.yy.value<curyear2){
			alert("Date Of Brith 年份必須在"+curyear2+"-"+curyear1+"之間！");
			document.form1.yy.focus();
			return false;;
		}

	}else{
		alert("請輸入Date Of Brith！");	
		document.form1.yy.focus();
		return false;
	}

	/*
	if(document.form1.education.value=="0"){
		alert("請選擇Education！");	
		document.form1.education.focus();
		return false;
	}

	if(document.form1.profession.value=="0"){
		alert("請選擇Profession！");	
		document.form1.profession.focus();
		return false;
	}

	if(document.form1.income.value=="0"){
		alert("請選擇Monthly Income！");	
		document.form1.income.focus();
		return false;
	}*/


	if(document.getElementById("isagree").checked!=true){	
		alert("必須同意條款才能成功注冊！");
		document.getElementById("isagree").focus();
		return false;
	}

	if(document.getElementById("xmail").checked==true){
		document.form1.ismail.value="1";	
	}else{
		document.form1.ismail.value="0";
	}

	if(trim(document.getElementById("photo").value)!=''){
		document.form1.modp.value="1"	
	}


	document.form1.submit();
}
//---------------------------------------------------------------------------------


//会员修改 -------------------------------------------------------------------------
function modmember(){

	if(checkempty(document.form1.email,"請輸入Email！")==false){
		return false;
	}

	if(ismail(document.form1.email.value)==false){;
		alert("Email地址不正確！");
		document.form1.email.focus();
		return false;
	}

	if(trim(document.form1.opassword.value)!='' || trim(document.form1.password.value)!='' || trim(document.form1.tpassword.value)!=''){ //-

	if(checkempty(document.form1.opassword,"請輸入Old Password！")==false){
		return false;
	}

	if(isAccount(trim(document.form1.opassword.value),2)==false){
		alert("Old Password由字母、數字、下劃線組成，長度為6-20位！");
		document.form1.opassword.focus();
		return false;
	}

	if(checkempty(document.form1.password,"請輸入New Password！")==false){
		return false;
	}

	if(isAccount(document.form1.password.value,2)==false){
		alert("New Password由字母、數字、下劃線組成，長度為6-20位！");
		document.form1.password.focus();
		return false;
	}

	if(checkempty(document.form1.tpassword,"請輸入Re-type Password！")==false){
		return false;
	}

	if(isAccount(document.form1.tpassword.value,2)==false){
		alert("Re-type Password由字母、數字、下劃線組成，長度為6-20位！");
		document.form1.tpassword.focus();
		return false;
	}

	if(document.form1.password.value!=document.form1.tpassword.value){
		alert("Re-type Password 與 New Password不一致！");
		document.form1.password.focus();
		return false;
	}

	}

	if(trim(document.form1.cname.value)!=''){
		if(isChinesex(document.form1.cname.value)==false){
			alert("Full Name In Chinese 必須是中文！");
			document.form1.cname.focus();
			return false;
		}
	}

	if(checkempty(document.form1.ename,"請輸入Full Name In English！")==false){
		return false;
	}

	if(isAccount(document.form1.ename.value,5)==false){
		alert("Full Name In English不正確！");
		document.form1.ename.focus();
		return false;
	}

	if(trim(document.getElementById("photo").value)!=''){
		if(checkphoto(document.getElementById("photo"))==false){
			alert("User Icon格式只能是JPG！");
			document.getElementById("photo").focus();
			return false;
		}	
	}

	if(checkempty(document.form1.mobile,"請輸入Mobile No！")==false){
		return false;
	}

	if(isnumeric(document.form1.mobile.value)==false){
		alert("Mobile No必須是數字！");
		document.form1.mobile.focus();
		return false;
	}

	if(document.form1.mm.value!='' || document.form1.dd.value!='' || document.form1.yy.value!=''){

		if(isnumeric(document.form1.mm.value)==false){
			alert("Date Of Brith 月份不正確！");
			document.form1.mm.focus();
			return false;
		}else if(document.form1.mm.value>12 || document.form1.mm.value<1){
			alert("Date Of Brith 月份不正確！");
			document.form1.mm.focus();
			return false;;
		}


		if(isnumeric(document.form1.dd.value)==false){
			alert("Date Of Brith 日不正確！");
			document.form1.dd.focus();
			return false;
		}else if(document.form1.dd.value>31 || document.form1.dd.value<1){
			alert("Date Of Brith 日不正確！");
			document.form1.dd.focus();
			return false;;
		}


		var curyear1,curyear2;
		var curyear = new Date();

		curyear1=curyear.getFullYear()-15;
		curyear2=curyear.getFullYear()-60;

		if(isnumeric(document.form1.yy.value)==false){
			alert("Date Of Brith 年份不正確！");
			document.form1.yy.focus();
			return false;
		}else if(document.form1.yy.value>curyear1 || document.form1.yy.value<curyear2){
			alert("Date Of Brith 年份必須在"+curyear2+"-"+curyear1+"之間！");
			document.form1.yy.focus();
			return false;;
		}
	}else{
		alert("請輸入Date Of Brith！");	
		document.form1.yy.focus();
		return false;
	}
	/*
	if(document.form1.education.value=="0"){
		alert("請選擇Education！");	
		document.form1.education.focus();
		return false;
	}

	if(document.form1.profession.value=="0"){
		alert("請選擇Profession！");	
		document.form1.profession.focus();
		return false;
	}

	if(document.form1.income.value=="0"){
		alert("請選擇Monthly Income！");	
		document.form1.income.focus();
		return false;
	}
	*/
	document.form1.submit();
}
//---------------------------------------------------------------------------------



//留言前檢測！
function checkarea(c){
	if(trim(c.value)=='只限會員留言，請先按「新登記」進行登記！'){
		c.value='';
	}
}

//激活自動下載
function actiondown(id){
	window.location="index.php?controller=Hdnm&action=Hdnmdown&id=" + id;
}

/**
* 驗證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;
}

//ghost 留言列表
function showghostreply(curl){
	$.ajax({
		type:"GET",
		url:curl,
		dataType:"html",
		cache:false,
		success:function(msg){	
			$("#greplylist").html(msg);
		}
	});
}

//ghost 發留言
function addghostreply(curl){
	$.ajax({
		type:"GET",
		url:curl,
		dataType:"html",
		cache:false,
		success:function(msg){	
			$("#addgreply").html(msg);
		}
	});
}

/**
* 發表留言
*/
function AddReply(){

	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(document.getElementById("rplogin").value=="0"){
		if(trim(document.getElementById("rpuname").value)==''){
			alert("請輸入登入名稱！");	
			document.getElementById("rpuname").focus();
			return false;
		}
	
		if(trim(document.getElementById("rppass").value)==''){
			alert("請輸入密碼！");	
			document.getElementById("rppass").focus();
			return false;
		}
	}
	
	if(trim(document.getElementById("rpcode").value)==''){
		alert("請輸入驗證碼！");
		document.getElementById("rpcode").focus();
		return false;
	}

	if(document.getElementById("marker").checked == true){
		ismarker = 1;
	}else{
		ismarker = 0;
	}
	
	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) + "&marker=" + encodeURIComponent(ismarker);
	
	$.ajax({
		type:"POST",
		url:"/index.php?controller=Ghostreply&action=Add",
		data:stra,
		dataType:"json",
		cache:false,
		success:function(msg){	

			if(msg['error']=="false"){
				alert("留言提交成功！");
				document.getElementById("rplogin").value = 1;
				document.getElementById("loginspan").style.display = "none";

				//podcast sms start
				if(document.getElementById("tablename").value=='ghost_podcast'){
					document.getElementById("mologin").value = 1;
					document.getElementById("mlogindiv").style.display = "none";				
				}
				//podcast sms end

				document.getElementById("rpcontent").value = "";
				document.getElementById("rpcode").value = "";
				document.getElementById("rpck").src = "/checkcode.php?t="+Math.random();
				document.getElementById("setsession").innerHTML = '<iframe src="' + document.getElementById("nmweb").value + 'index.php?controller=Ghost&action=SetSession&uid=' + msg['uid'] + '&uname=' + msg['uname'] + '" style="display:none"></iframe>';
				
				showghostreply('/index.php?controller=Ghostreply&action=Index&tname=' + document.getElementById("tablename").value + '&tid=' + document.getElementById("topicid").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;				
			}
		}
	});	
	
}

//gatag flash 調用
//例 getURL("javascript:gaTrackEvent('Social Media','Weibo')");
function gaTrackEvent(category,action,label,value){
	var Tevent = ['_trackEvent',category,action];
	typeof(label)!='undefined' ? Tevent.push(label) : false;
	typeof(value)!='undefined' ? Tevent.push(value) : false;
	_gaq.push(Tevent);
}


function shareTo(t,u,p){
	var p=p?p:'';
	$(".toshare").load("/index.php?controller=share&action=index",{'t':t,'u':u,'p':p});
}


//相關內容調用
function showghosttag(tag,tname,tid){
	$.ajax({
		type:"GET",
		url:"/Ghosttag/Index/tag/"+tag+"/tname/"+tname+"/tid/"+tid,
		dataType:"html",
		cache:false,
		success:function(msg){	
			if(trim(msg)==''){
				$('#reladiv').hide();
			}else{
				$('#reladiv').html(msg);
			}
		}
	});
}
