
var TPL_BASE_PATH = '/';

//去除左右空格
var trim = function (str) {return str.replace(/^\s*(.*?)[\s\n]*$/g, '$1');};

//切换文本框文字
function changeText(obj,text,type){
    if(type == '1'){
	    if(obj.value == text){ 
			obj.value = '';
			obj.style.color='#000000';
		}
	}
	if(type == '2'){
	    if(obj.value == ""){ 
			obj.value = text;
			obj.style.color='#999999';
		}
	}
}

//表单输入验证
//inputType：输入字符类型，objArr：表单元素对像数组；isAllow：是否允许输入，1：允许，0：不允许
function inputCheck(inputType,objArr,isAllow) {
	var keyCodeArr = [
					  ['48','49','50','51','52','53','54','55','56','57'],//数字
					  ['48','49','50','51','52','53','54','55','56','57','116','8','9','13'],//数字+允许键
					  ['97','98','99','100','101','102','103','104','105','106','107','108','109','110','111','112','113','114','115','116','117','118','119','120','121','122'],//字母
					  ['97','98','99','100','101','102','103','104','105','106','107','108','109','110','111','112','113','114','115','116','117','118','119','120','121','122','8','9','13'],//字母+允许键
					  ['39','34'],//引号
					  ['46'],//点号
					  ['48','49','50','51','52','53','54','55','56','57','46','116','8','9','13']//数字+允许键+点号
					 ];
	if(isAllow){
		objArr.each( function(_obj, index) {
							 
			_obj.observe("keypress",
					function(e) {
						var _isInArr = false;
						
						for (i=0;i<keyCodeArr[inputType].length;i++) {
							
							if (e.keyCode == keyCodeArr[inputType][i] || e.charCode == keyCodeArr[inputType][i]) {
								_isInArr = true;
								break;
							}
						}
						if (window.event) {
							e.returnValue = _isInArr;
						} else {
							if (!_isInArr) {
								e.preventDefault();
							}
						}
	
						return _isInArr;
					}.bindAsEventListener(_obj, keyCodeArr[inputType]));
		});
	}else{
		objArr.each( function(_obj, index) {
			_obj.observe("keypress",
					function(e) {
						var _isInArr = false;
						 
						for (i=0;i<keyCodeArr[inputType].length;i++) {
							if (e.keyCode == keyCodeArr[inputType][i] || e.charCode == keyCodeArr[inputType][i]) {
								_isInArr = true;
								break;
							}
						}
	
						if(_isInArr){
							if (window.event) {
								e.returnValue = false;
							} else {
								e.preventDefault();
							}
						}
						
						return _isInArr;
					}.bindAsEventListener(_obj, keyCodeArr[inputType]));
		});
	}
	
}

//表单去除单双引号
function checkQuotes(obj) {
     var objValue = obj.value;
	 var p1 = /\"+/g;
     if(p1.test(objValue)){
	    obj.value = objValue.replace(p1,"");
	 }else{
	    var p2 = /\'+/g;
		if(p2.test(objValue)){
		   obj.value = objValue.replace(p2,"");
		}
	 }
}

//表单验证数字
function checkNum(obj) {
   var objValue = obj.value;
	 var p1 = /\"+/g;
     if(p1.test(objValue)){
	    obj.value = objValue.replace(p1,"");
	 }else{
	    var p2 = /\'+/g;
		if(p2.test(objValue)){
		   obj.value = objValue.replace(p2,"");
		}
	 }
	 var p3 = /[^0-9]/g;	
	 if(p3.test(objValue)){
	    obj.value = objValue.replace(p3,"");
	 } 
}

//表单验证小数
function checkFloat(obj) {
   var objValue = obj.value;
	 var p1 = /\"+/g;
     if(p1.test(objValue)){
	    obj.value = objValue.replace(p1,"");
	 }else{
	    var p2 = /\'+/g;
		if(p2.test(objValue)){
		   obj.value = objValue.replace(p2,"");
		}
	 }
	 var p3 = /[^0-9.]/g;

	 if(p3.test(objValue)){
	    obj.value = objValue.replace(p3,"");
	 } 
	 
}

//，转,
function checkQj(obj) {
   var objValue = obj.value;
	 var p1 = /，+/g;
     if(p1.test(objValue)){
	    obj.value = objValue.replace(p1,",");
	 }
}

//全选/反选
function selectAll(){
  var input = document.getElementsByTagName("Input");
  var checkAll = document.getElementById("checkAll");
  for (i = 0; i < input.length; i++){
    if(input[i].type=="checkbox"){
	  if(checkAll.checked == false){
	    input[i].checked = false;
	  }
	  if(checkAll.checked == true){
	    input[i].checked = true;
	  }
	}
  }
}

//全选
function allSelect(id){
	var input = document.getElementById(id).getElementsByTagName('input');
	for (i = 0; i < input.length; i++){
	   if(input[i].type=="checkbox" && input[i].value != '-1'){
	      input[i].checked = true;
	   }
	}	
}

//反选
function reSelect(id){
	var input = document.getElementById(id).getElementsByTagName('input');
	for (i = 0; i < input.length; i++){
	   if(input[i].type=="checkbox" && input[i].value != '-1'){
	      if(input[i].checked == true){
		     input[i].checked = false;
		  }else{
		     input[i].checked = true;
		  }
	   }
	}
}

//写cookie
function setCookie(name,value,expiresd,path,domain,secure){
	var expdate = new  Date();
	var expires = arguments[2] || null;
	var  path  =  arguments[3] || "/";
	var  domain  =  arguments[4] || null;
	var  secure  =  arguments[5] || false;
	if(expires){
		expdate.setTime(expdate.getTime() + expires);
	}
	var cookietemp =escape(name) + '=' + escape(value) + (expires ? '; expires=' + expdate.toGMTString() : '') + (path ? '; path=' + path : '')+ (domain ? '; domain=' + domain : '')+(secure ? '; secure' : '');
	document.cookie=cookietemp;
}

//取cookie
function getCookie(name){
    var cookieValue = "";
    if(document.cookie.length > 0){
        var vec = document.cookie.split(';');
        var search = name + "=";
        for(var i = 0; i < vec.length; ++i){
            vec[i] = vec[i].replace(/(^\s*)|(\s*$)/g, "");
            var offset = vec[i].indexOf(search);
            if(offset != 0)
                continue;
            cookieValue = unescape(vec[i].substring(offset + search.length));
            break;
        }
    }
    return cookieValue;
}

//删除cookie
function clearCookie(cookieName){
	var now = new Date();
	var yesterday = new Date(now.getTime() - 1000 * 60 * 60 * 24);
	setCookie(cookieName, "", yesterday);
}

//ajax更新层
function updateContent(page,oDiv){
  var request_url;      
  var request_pars = ''; 
  var oDiv=$(oDiv); 
  request_url = page+'&'+new Date().getTime();
  var myAjax = new Ajax.Updater(oDiv, request_url,{
	  method     : "get", // http 请求方法,get or post
	  parameters : request_pars
  });
}

//关注
function addFriend(url,userId,friendId,goUrl){
	if(userId == '0'){
		openWindow('shadowBg','winObj','winContent',TPL_BASE_PATH+'home/loginAjax.php?a');
		return false;
	}else if(userId == friendId){
		alert("不能关注自己！");
		return false;
	}else{
	   var url=url;	
	   var pars = '?friendId='+friendId+'&'+new Date().getTime();
	
	   var myAjax = new Ajax.Request(url,{
		   method:'get',
		   parameters:pars,
		   onComplete:result_f
	   });
	}
}

function result_f(response){
   var result = trim(response.responseText);
   var infoArr = result.split('*|*');
   if(infoArr[0] == 'succ'){
	   alert("您好！您已经成功关注了 "+infoArr[1]+"！");
	   return;
   }else{
	   alert("您好！您已经关注了 "+infoArr[1]+"！");
	   return;
   }
}

//添加愿望箱
function addWish(userId,uRecId){
	var logId = Object.isUndefined(arguments[2]) ? 0 : arguments[2];
	if(userId == '0'){
		openWindow('shadowBg','winObj','winContent',TPL_BASE_PATH+'home/loginAjax.php?a');
	}else{
		var url = TPL_BASE_PATH+"uCenter/handle/addWishHandle.php";	
		var pars = '?uRecId='+uRecId+'&'+new Date().getTime();
		var myAjax = new Ajax.Request(url,{
		   method:'get',
		   parameters:pars,
		   onComplete:result_wish.bindAsEventListener(this, logId)
		});
    }
}

function result_wish(response){
	var data = $A(arguments);
	data.shift();
	var logId = data[0];
	var result = trim(response.responseText);
    var infoArr = result.split('*|*');
	if(infoArr[0] == 'succ'){
		new PopAlert("您已经成功的把宝贝 "+infoArr[1]+" 添加到愿望箱！");
		if($("wishNum-"+logId)!=null){
			var _strNum = $('wishNum-'+logId).innerHTML;
			_strNum = _strNum.replace("(", '');
			_strNum = _strNum.replace(")", '');
			_strNum ++;
			$('wishNum-'+logId).update("("+_strNum+")")
		}
	}else{
		new PopAlert("您已经把宝贝 "+infoArr[1]+" 添加到愿望箱！");
	}   

	return;
}

function result_al(response){
	popAlert("操作成功！");
	location.reload();
}



//加入群组
function joinGroup(userId,goUrl,type){
   if(verif.isLogin()){
	   var gId = $("gIdJ").value;
	   var reason = '';
	   if(type != '1'){
		   reason = trim($("joinReason").value);
		   if(reason == ''){
			   alert("请输入申请理由！");
			   return false;
		   }
		   if(reason.length > 500){
			   alert("申请理由不能超过500个字符！");
			   return false;
		   }
	   }
	   
	   var url=TPL_BASE_PATH+"group/handle/joinGroupHandle.php";	
	   var pars = '?userId='+userId+'&gId='+gId+'&reason='+encodeURIComponent(reason)+'&'+new Date().getTime();
	   
	   var myAjax = new Ajax.Request(url,{
		   method:'get',
		   parameters:pars,
		   onComplete:result_jg
	   });
   }
   
}

function result_jg(response){
	var result = trim(response.responseText);
	
	popDiv.close();
	if(result == '1'){
		new PopAlert("您好，您已经成功加入本群！", {stopTime: 5000});
	}else if(result == '0'){
		new PopAlert("您好，您已经成功提交加入申请，请耐心等待群主审核！", {stopTime: 5000});
	}else if(result == '2'){
		new PopAlert("您好，群主拒绝了您的申请！", {stopTime: 5000});
	}else if(result == 'exist'){ 
		new PopAlert("您好，您已是本群成员！", {stopTime: 5000});
	}else{
		new PopAlert("您好，操作有误！", {stopTime: 5000});
	}
	winReload();
}

//显示层
function showDiv(div){
   var dialogDiv = $(div);
   dialogDiv.style.display = '';  
   dialogDiv.style.position="absolute";
   
   if (!window.XMLHttpRequest){//ie6
		dialogDiv.style.left=(document.documentElement.clientWidth-dialogDiv.clientWidth)/2+document.documentElement.scrollLeft+"px";
		dialogDiv.style.top=(document.documentElement.clientHeight-dialogDiv.clientHeight)/2+document.documentElement.scrollTop+"px";
	}else{//IE 7,mozilla
		dialogDiv.style.left=(document.body.clientWidth-dialogDiv.clientWidth)/2+document.body.scrollLeft+"px";
		dialogDiv.style.top=(document.body.clientHeight-dialogDiv.clientHeight)/2+document.body.scrollTop+"px";
	}
}

//隐藏层
function colseDiv(div){
    $(div).style.display = 'none';
}

//打开弹出框
function openWindow(bgObj,winObj,winContent,page){
	$(bgObj).style.display = '';
	$(winObj).style.display = '';
	if(page != ''){
		$(winContent).style.display = '';
		updateContent(page,winContent);
	}
}

//刷新
function winReload(){
	location.reload();
}

//关闭弹出框
function colseWindow(bgObj,winObj){
	$(bgObj).style.display = 'none';
	$(winObj).style.display = 'none';
	$(winContent).style.display = 'none';
}

function shadowCenter(){ 
	//控制id=dropShadow的这个层的居中显示
	var element = document.getElementById("winObj");
	element.style.marginTop = -element.offsetHeight/2 + "px";
	var posX, posY;
	
	if(window.innerHeight){
		posX = window.pageXOffset;
		posY = window.pageYOffset;
	}else if(document.documentElement && document.documentElement.scrollTop){
		posX = document.documentElement.scrollLeft;
		posY = document.documentElement.scrollTop;
	}else if(document.body){
		posX = document.body.scrollLeft;
		posY = document.body.scrollTop;
	}
	
	element.style.top = (document.documentElement.clientHeight/2 + posY) +"px";
	
	setTimeout('shadowCenter()', 30);
}

//登录
function login(){
	flag = '0';
	var account = trim($("account").value);
	var pwd = trim($("pwd").value);
	var autoLogin = $('autologin').checked ? 1 : 0;
	if(account == ''){
		$("accountTip").style.display = ''
		$("accountTip").innerHTML = '请填写账号！';
		return false;
	}else{
		$("accountTip").style.display = 'none'
		$("accountTip").innerHTML = '';
	}
	
	if(pwd == ''){
		$("pwdTip").style.display = ''
		$("pwdTip").innerHTML = '请填写密码！';
		return false;
	}else{
		$("pwdTip").style.display = 'none'
		$("pwdTip").innerHTML = '';
	}
	
	var url = TPL_BASE_PATH+"home/handle/loginHandle.php";	
	var pars = '?isAjax=1&account='+account+'&pwd='+pwd+'&autologin='+autoLogin+'&'+new Date().getTime();
	var myAjax = new Ajax.Request(url,{
		method:'get',
		parameters:pars,
		onComplete:loginResult
	});
}

function loginResult(re){
	var result = trim(re.responseText);
	if(result == '3'){
		$("errInfo").innerHTML = '<span style="color:#F00">对不起,由于你有过违规操作,您在一段时间内禁止登录</span>';	
		return;
	}
	if(result == '1'){
		location.reload();
	}else{
		$("errInfo").innerHTML = '<span style="color:#F00">对不起，账号或密码有误！</span>';	
	}
}

//--------------------用户留言--------------------//

function userComm(commId,userId,otherId){
	if(userId == '0'){
		openWindow('shadowBg','winObj','winContent',TPL_BASE_PATH+'home/loginAjax.php?a');
	}else{
		var content = trim($("content"+commId).value);	
		var flag = '0';
		
		if(content == ''){
			$("contentTip"+commId).innerHTML = '请填写留言内容';
			flag = '1';
		}else{
			$("contentTip"+commId).innerHTML = '';
			flag = '0';
		}
		if(content != ''){
			if(content.length > 500){
				$("contentTip"+commId).innerHTML = '留言内容不能超过500个字符';
				flag = '1';
			}else{
				$("contentTip"+commId).innerHTML = '';
				flag = '0';
			}
		}
		
		if(flag == '1'){
			return false;
		}
		
		var url = "handle/commHandle.php";	
	    var pars = '?commId='+commId+'&otherId='+otherId+'&content='+encodeURIComponent(content)+'&'+new Date().getTime();
	
	    var myAjax = new Ajax.Request(url,{
		   method:'get',
		   parameters:pars,
		   onComplete:result_comm
	    });
	}
}

function commDel(commId){
	if(confirm("确认删除吗？")){
		var url = "handle/commDelHandle.php";	
	    var pars = '?commId='+commId+'&'+new Date().getTime();
	
	    var myAjax = new Ajax.Request(url,{
		   method:'get',
		   parameters:pars,
		   onComplete:result_comm
	    });
	}
}

function commReplyShow(type,commId){
	if(type == 'show'){
		$('commReplyForm'+commId).style.display = '';
	}else{
		$('commReplyForm'+commId).style.display = 'none';
	}
}

//获取浏览器类型
function getBrowser(){  
   if(navigator.userAgent.indexOf("MSIE")>0) { 
        return "MSIE"; 
   } 
   if(isFirefox=navigator.userAgent.indexOf("Firefox")>0){ 
        return "Firefox"; 
   } 
   if(isSafari=navigator.userAgent.indexOf("Safari")>0) { 
        return "Safari"; 
   }  
   if(isCamino=navigator.userAgent.indexOf("Camino")>0){ 
        return "Camino"; 
   } 
   if(isMozilla=navigator.userAgent.indexOf("Gecko/")>0){ 
        return "Gecko"; 
   } 
}
