//================================================
//= 文 件 名： init.js
//= 功    能： javascript脚本，网站专用函数库
//= 负 责 人： Kan
//================================================

//================= 常见的字符串处理函数 =================
// 返回字符的长度，一个中文算2个
String.prototype.ChineseLength=function(){
	return this.replace(/[^\x00-\xff]/g, "**").length;
};
// 去所有空格   
String.prototype.TrimAll = function(){   
    return this.replace(/(^\s*)|(\s*)|(\s*$)/g, "");   
};
// 去掉字符串两端的空白字符
String.prototype.Trim=function(){
	return this.replace(/(^\s*)|(\s*$)/g, "");
};
// 去掉字符左端的的空白字符
String.prototype.LeftTrim=function(){
	return this.replace(/(^[\\s]*)/g, "");
};
// 去掉字符右端的空白字符
String.prototype.RightTrim=function(){
	return this.replace(/([\\s]*$)/g, "");
};
/* 忽略大小写比较字符串是否相等
注：不忽略大小写比较用 == 号 */
String.prototype.IgnoreCaseEquals=function(str){
	return this.toLowerCase()==str.toLowerCase();
};
/* 不忽略大小写比较字符串是否相等 */
String.prototype.Equals=function(str){
	return (this==str);
};
/* 比较字符串，根据结果返回 -1, 0
返回值 相同：0 不相同：-1 */
String.prototype.CompareTo=function(str){
	if(this==str){ return 0; }else{ return -1; }
};
// 字符串替换
String.prototype.Replace=function(oldValue,newValue){
	var reg=new RegExp(oldValue,"g");
	return this.replace(reg,newValue);
};
// 检查是否以特定的字符串结束
String.prototype.EndsWith=function(str){
	return this.substr(this.length-str.length)==str;
};
// 判断字符串是否以指定的字符串开始
String.prototype.StartsWith=function(str){
	return this.substr(0,str.length)==str;
};
// 从左边截取n个字符
String.prototype.LeftSlice=function(n){
	return this.substr(0,n);
};
// 从右边截取n个字符
String.prototype.RightSlice=function(n){
	return this.substring(this.length-n);
};
// 统计指定字符出现的次数
String.prototype.Occurs=function(ch){
	//var re=eval("/[^"+ch+"]/g");
	//return this.replace(re,"").length;
	return this.split(ch).length-1;
};

/* 构造特定样式的字符串，用 <span></span> 包含 */ 
// document.write(testStr2.Style("color:red;width:100px")); 
String.prototype.Style=function(style){
	return "<span style=\"".concat(style,"\">",this,"</span>");
};
// 构造类似StringBuilder的函数(连接多个字符串时用到，很方便)
/* StringBuilder测试 */ 
// var testStr3 = new StringBuilder(""); 
// testStr3.Append("test3rn"); 
// testStr3.Append("test3test3rn"); 
// testStr3.Append("test3"); 
// alert(testStr3.toString()); 
// testStr3.Clear(); 
// alert(testStr3.toString()); 
function StringBuilder(str){
	this.tempArr=new Array();
}
StringBuilder.prototype.Append=function(value){
	this.tempArr.push(value);
	return this;
};
StringBuilder.prototype.Clear=function(){
	this.tempArr.length=0;
};
StringBuilder.prototype.toString=function(){
	return this.tempArr.join('');
};

//================= 公共函数(字符类) =================

//替换字符串
function ReplaceAll(str, sptr, sptr1){
	while (str.indexOf(sptr) >= 0){
		str = str.replace(sptr, sptr1);
	}
	return str;
}

//判断是否日期字符串
function isDate(str){
  var reg = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/;
  result = str.match(reg);
  if(result == null)return false;
  var d = new Date(result[1],result[3]-1,result[4])
  var newStr = d.getFullYear()+result[2]+(d.getMonth()+1)+result[2]+d.getDate();
  return str == newStr;
}

//复制字符串
function ExampleCopy(copy){window.clipboardData.setData('text',copy);alert('已复制到剪贴板了！');}

//显示消息框
function msgShow(str){
	$j("body").append("<div id=\"MessageBox\" class=\"MessageBox\"><img style=\"margin:3px 0px -3px 5px;\" src=\"images/ajax_msg.gif\" alt=\"Working o&gt;﹏&lt;o\"/><div id=\"Messages\">Updateing...</div></div>");
	$j("#Messages").html(str);
	$j("#MessageBox img").show();
	$j("#MessageBox").fadeIn();
}
//隐藏消息框
function msgHide(str){
	$j("#MessageBox img").hide();
	$j("#Messages").html(str);
	setTimeout(function(){$j("#MessageBox").fadeOut();$j("#MessageBox").remove();},500);
}

//获取系统时间
function showtime () {
	var now = new Date();
	var year = now.getYear();
	var month = now.getMonth() + 1;
	var date = now.getDate();
	var hours = now.getHours();
	var minutes = now.getMinutes();
	var seconds = now.getSeconds();
	var day = now.getDay();
	Day = new MakeArray(7);
	Day[0]="星期天";
	Day[1]="星期一";
	Day[2]="星期二";
	Day[3]="星期三";
	Day[4]="星期四";
	Day[5]="星期五";
	Day[6]="星期六";
	var timeValue = "";
	//timeValue += "<b><span style='font-size:20px;'>";
	timeValue += ((hours < 10) ? "0"+hours: hours);
	timeValue += ((minutes < 10) ? ":0" : ":") + minutes;
	timeValue += ((seconds < 10) ? ":0" : ":") + seconds;
	//timeValue += (hours < 12) ? "<span style='font-size:12px;'>上午</span>" : "<span style='font-size:12px;'>下午</span>";
	timeValue += (hours < 12) ? " 上午" : " 下午";
	//timeValue += "</span> "+(Day[day]) + " ";
	timeValue += " "+(Day[day]) + " ";
	timeValue += year + "年";
	timeValue += ((month < 10) ? "0" : "") + month + "月";
	timeValue += date + "日";
	//timeValue += "</b>";
	return timeValue;
	//timerID = setTimeout("showtime()",1000);
}

//================= 公共函数(数字类) =================
//转换为整数
function int(num){return num-num%1}

//保留指定位数小数
function ForDight(Dight,How){
  if(Dight==0){return 0;}
  Dight=Math.round(Dight*Math.pow(10,How)+0.05)/Math.pow(10,How);
  return Dight;
}
//强制保留指定位数小数
function ForDightEnforce(Dight,How){
  if(Dight==0){return 0;}
  Dight=Math.round(Dight*Math.pow(10,How)+0.05)/Math.pow(10,How);
  var strDight=Dight.toString();
  if(How>0){
  var pos_decimal=strDight.indexOf(".");
  if(pos_decimal<0){
	  pos_decimal=strDight.length;
	  strDight+=".";
  }
  while(strDight.length<=pos_decimal+How){
	  strDight+="0";
  }}
  return strDight;
}

//是否为数字(含小数和负数)
function isDouble(s){
	var patrn=/^-?[1-9]+\d*$|^(-?[1-9]+\d*)(\.\d+)$|^(-?[0])(\.\d+)$|^[0]$/;
	if (!patrn.exec(s)) return false;
	return true;
}

//是否为正整数
function isInt(s){
	var patrn=/^\d*$/;
	if (!patrn.exec(s)) return false;
	return true;
}

//去处千分号
function DelQF(str){
	return ReplaceAll(str,",","");
}
//添加千分号
function AddQF(num){
	var str,str1,str2,strTmp;
	str = num.toString().TrimAll();
	if(str != ""){
		if(isDouble(str)){
			str1="";str2="";strTmp="";
			if(str.indexOf(".")!=-1){
				str1 = str.split(".")[0];
				str2 = str.split(".")[1];
			}else{
				str1 = str;
			}
			if(str1.indexOf("-")!=-1){
				str1 = str1.substring(1,str1.length);
			}
			//添加千分号        
			var i = str1.length/3;
			var k = str1.length%3;
			if( k== 0){
				for(var j=0;j<i;j++){            
					strTmp+=str1.substring(j*3,3*j+3)+",";        
				}
			}else{
				var l = int(i);
				strTmp+=str1.substring(0,k)+",";
				for(var j=0;j<l;j++){            
					strTmp+=str1.substring(j*3+k,3*j+3+k)+",";        
				}
			}
			//去除最后一个千分号        
			strTmp=strTmp.substring(0,strTmp.length-1);
			if(str2!=""){strTmp+="."+str2;}
			if(str.indexOf("-")!=-1){
				str = "-"+strTmp;
			}else{
				str = strTmp;
			}
		}else{
			str = "0";
		}         
	}else{
		str = "";
	}
	return str;
}


//================================================================================================

//弹出子窗口
function popWin(url,win,para) {
    var win = window.open(url,win,para);
    win.focus();
}

//设置跳转
function setLocation(url){
    window.location.href = url;
}

//在子窗口设置其父窗口跳转地址
//父窗口刷新的方法：
//window.opener.location.href=window.opener.location.href	直接跳转
//window.opener.location.reload()							跳转需用户确认
function setPLocation(url, setFocus){
    if( setFocus ) {
        window.opener.focus();
    }
    window.opener.location.href = url;
}
