/**
 * 公用js。主要是用于基本操作。比如截取字符串，格式化时间，字符串转换为时间
 * @author wangyouyi
 * @email wangyouyi1983@hotmail.com
 * NOTE：请勿删除或者随意修改此js内的方法。
 */
var g_s_regs = {};

/**
 * String原型函数，去空格
 */ 
String.prototype.trim = function(){
	var reg = g_s_regs["(^\s*)|(\s*$)"];
	if(reg===undefined){
		reg = /(^\s*)|(\s*$)/g;
		g_s_regs["(^\s*)|(\s*$)"] = reg;
	}
	return this.replace(/　/g,"  ").replace(reg, "");
	
	//return this.replace(/(^s*)|(s*$)/g, "");
}

String.prototype.replaceBase=function(){
 	return str.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/\"/g,"&quot;").replace(/\n/g,"<br/>");	
}

/**
 * 替换html
 */
function replaceBase(str){
	return str.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/\"/g,"&quot;").replace(/\n/g,"<br/>");
}
 
/**
 * 全选或者反选
 * @obj 全选或者反选的checkbox对象
 * @chkname 要全选或者反选的checkbox的name
 */
function doAllCheck(obj, chkname){
	var oc = document.getElementsByName(chkname);
	if(typeof(oc) != 'undefined' ){ 
		for(var i = 0; i < oc.length; i++)
			oc[i].checked = obj.checked;
	}
}

/**
 * 全选，反选
 * @param chkname checkbox的名称
 * @param checkall
 */
function check(chkname, checkall){
	var oc = document.getElementsByName(chkname);
	if(typeof(oc) != 'undefined' ){ 
		for(var i = 0; i < oc.length; i++){
			if(checkall)
				oc[i].checked = true;
			else{
				if(oc[i].checked == true)
					oc[i].checked = false;
				else
					oc[i].checked = true;
			}// end if
		}//end for
	}// end of
}

/**
 * 字符串转换时间，用正则表达式来匹配
 * @str 要转换为时间的字符串
 * @return 返回格式化好的时间
 */
function parseDate(str){
	if(typeof str == 'string'){   
	   	var results = str.match(/(\d{4})-(\d{1,2})-(\d{1,2}).*(\d{2}):(\d{2}):(\d{2}).*/);   
	   	if(results && results.length>6)   
	   		return new Date(parseInt(results[1]),parseInt(results[2].replace(/0(\d)/,"$1")) -1,parseInt(results[3].replace(/0(\d)/,"$1")),parseInt(results[4].replace(/0(\d)/,"$1")),parseInt(results[5].replace(/0(\d)/,"$1")),parseInt(results[6].replace(/0(\d)/,"$1")));    
	    if(results && results.length>3)
	    	return new Date(parseInt(results[1]),parseInt(results[2].replace(/0(\d)/,"$1")) -1,parseInt(results[3].replace(/0(\d)/,"$1")));
	}   
  	return null;   
} 

/**
 * 时间的原型，格式化时间
 * @format 要格式化时间的格式，比如"yyyy-MM-dd"
 */
Date.prototype.format = function(format){
	var o = {
		"M+" : this.getMonth()+1, //month
		"d+" : this.getDate(),    //day
		"h+" : this.getHours(),   //hour
		"m+" : this.getMinutes(), //minute
		"s+" : this.getSeconds(), //second
		"q+" : Math.floor((this.getMonth()+3)/3),  //quarter
		"S" : this.getMilliseconds() //millisecond
	}
	if(/(y+)/.test(format)) format=format.replace(RegExp.$1,
	(this.getFullYear()+"").substr(4 - RegExp.$1.length));
	for(var k in o)if(new RegExp("("+ k +")").test(format))
	format = format.replace(RegExp.$1,
	RegExp.$1.length==1 ? o[k] :
	("00"+ o[k]).substr((""+ o[k]).length));
	return format;
}

/**
 * 在页面上直接显示截取后的字符串。超过的部门用"..."显示
 * @str 要截取的字符串
 * @len 截取的长度
 */
function substrlen(str, len){
	var strlen = 0; 
 	var s = "";
	for(var i = 0; i < str.length; i++){
        if(str.charCodeAt(i) > 128)
        	strlen += 2;
        else 
        	strlen++;
        s += str.charAt(i);
        if(strlen >= len) {
        	s = s + "...";
        	break;
        }	
	}
	document.write(s);
}

/**
 * 首页截取字符串用，都在字符串后面加...
 */
function index_substrlen(str, len){
	if(str == "&nbsp;"){
		return;
	}
	var strlen = 0; 
 	var s = "";
	for(var i = 0; i < str.length; i++){
        if(str.charCodeAt(i) > 128)
        	strlen += 2;
        else 
        	strlen++;
        s += str.charAt(i);
        if(strlen >= len) {
        	//s = s + "...";
        	break;
        }
	}
	s = s + "...";
	document.write(s);
}

/**
 * 返回截取后的字符串。超过的部分用"..."显示
 * @param str 截取前字符串
 * @param len 要截取的长度
 */
function return_substrlen(str, len){
	var strlen = 0; 
 	var s = "";
	for(var i = 0; i < str.length; i++){
        if(str.charCodeAt(i) > 128)
        	strlen += 2;
        else 
        	strlen++;
        s += str.charAt(i);
        if(strlen >= len){
        	s = s + "...";
        	break;
        }	
	}
	return s;
}

/**
 * 截取字符串，不论汉英，都统计截取长度。超过部门不用任何字符来替代
 * @param str 要截取的字符串
 * @param len 要截取的字符串长度
 */
function substr(str, len){
	var strlen = str.length;
	if( strlen > len )
		str = str.substring(0, len)
	document.write(str);
}

/**
 * 在页面上直接显示截取后的字符串。
 * @str 要截取的字符串
 * @len 截取的长度
 */
function substr2(str, len){
	var strlen = 0; 
 	var s = "";
	for(var i = 0; i < str.length; i++){
        if(str.charCodeAt(i) > 128)
        	strlen += 2;
        else 
        	strlen++;
        s += str.charAt(i);
        if(strlen >= len) {
        	break;
        }	
	}
	document.write(s);
}

/**
 * 判断是否选择要删除的checkbox
 * @chkname checkbox的name
 */
function isChecked(chkname){
	var sids = document.getElementsByName(chkname);
	for(var i = 0; i < sids.length; i++){
		if( sids[i].checked ){
			return true;
		}
	}
	return false;
}

/**
 * 获取选中的checkbox值
 * 返回被选中的值
 */
function getCheckedValue(chkname){
	var ids = "";
	var sids = document.getElementsByName(chkname);
	for(var i = 0; i < sids.length; i++){
		if(sids[i].checked == true){
			if(ids == "")
				ids += sids[i].value;
			else
				ids += "," + sids[i].value 
		}// end of if
	}// end for
	return ids;
}// end function


/**
 * 获得radio的值
 * @param radioname radio的名称
 */
function getRadioValue(radioname){
	var radio = document.getElementsByName(radioname);
	var radiovalue = "";
	//alert(radio.length);
	for(var i = 0; i < radio.length; i++){
		if(radio[i].checked){
			radiovalue = radio[i].value;
			break;
		}
	}
	return radiovalue;
}

/**
 * 选中radio
 * @param radioname	当前radio的名称
 * @param radiovalue 当前radiovalue值
 */
function checkRadio(radioname, radiovalue){
	var radio = document.getElementsByName(radioname);
	for(var i = 0; i < radio.length; i++){
		if(radiovalue == "")
			radio[i].checked = false;
		else if(radio[i].value == radiovalue)
			radio[i].checked = true;
	}
}

/**
 * 提示是否要删除
 * @form 当前表单
 * @action 当前表单要请求的action
 * @chkname checkbox的name
 */	
function doDelete(form, action, chkname){
	if( !isChecked(chkname) ){
		alert('没有选中要删除的项！');
		return;
	} 
	if(!confirm('确实要删除选中项吗？'))
		return; 
	form.action = action;
	form.submit();
}

/**
 * 提示是否要还原
 * @form 当前表单
 * @action 当前表单要请求的action
 * @chkname checkbox的name
 */	
function doRestore(form, action, chkname){
	if( !isChecked(chkname) ){
		alert('没有选中要恢复的项！');
		return;
	} 
	if(!confirm('确实要恢复选中项吗？'))
		return; 
	form.action = action;
	form.submit();
}

/**
 * 根据value判断哪个select下的option被选中
 * @objSelect 下拉框对象
 * @objItemValue option的value
 */
function SelectItemByValue(objSelect, objItemValue){  
//	alert("objItemValue:"+objItemValue);
    for(var i = 0; i < objSelect.options.length; i++){
        if(objSelect.options[i].value == objItemValue){
  //      	alert("The options[" + i + "].value:" + objSelect.options[i].value);
            objSelect.options[i].selected = true;
            break;
        }
    } 
}

/**
 * 等比缩放图片
 * @param ImgD 图片对象
 * @param height 要缩放的高度
 * @param width 要缩放的宽度
 */
function DrawImage(ImgD, height, width){
	var image = new Image();
	image.src = ImgD.src; 
	// 如果图片的宽、高都大于0
	if(image.width > 0 && image.height > 0){ 
		// 如果宽大于高
		if(image.width / image.height >= width / height){
			// 如果宽大于要缩放的宽，则缩放高，宽置为要缩放大小的宽
			if(image.width > width){
			   ImgD.width = width;
			   ImgD.height = (image.height * width) / image.width;
		   	} else{// 否则不缩放
		       ImgD.width = image.width;
		       ImgD.height = image.height;
		   	}// end if 
		  //ImgD.alt = image.width + '??' + image.height;
	  	}else{// 如果高大于宽，则高置为要缩放的高度，缩放宽
	     	if(image.height > height){
	        	ImgD.height = height;
	        	ImgD.width = (image.width * height) / image.height;
	     	}else{
	        	ImgD.width = image.width;
	        	ImgD.height = image.height; 
	     	}
	     	//ImgD.alt = image.width + '??' + image.height;
	 	}//end of if
	}// end of if
}// end of function

/**
 * 长数字字符添加","进行分割。如"1000"，输出为"1,000"
 * numberStr：数字字符
 */
function addCommaToNumber(numberStr){
	var str = numberStr;
	var subs = new Array();
	var newStr = "";
	for(var i = str.length, j = 0; i > 0; i -= 3, j++)
		subs[j]=str.substring(i, i - 3);
	subs.reverse();
  	for(var i = 0; i < subs.length; i++)
  		newStr += ( i== subs.length-1 ) ? subs[i]: subs[i] + ",";
  	document.write(newStr);
} 

/**
 * 长数字字符添加","进行分割。如"1000"，返回为"1,000"
 * numstr 数字字符串
 */
function addCommaToNumber2(numstr){
	var str = numstr;
	var subs = new Array();
	var newStr = "";
	for(var i = str.length, j = 0; i > 0; i -= 3, j++)
		subs[j]=str.substring(i, i - 3);
	subs.reverse();
  	for(var i = 0; i < subs.length; i++)
  		newStr += ( i== subs.length-1 ) ? subs[i]: subs[i] + ",";
  	return newStr;
}