//得到去除左右空格后的字符串
String.prototype.trim = function() 
{   
    return this.replace(/(^\s*)|(\s*$)/g,"");   
}

//验证密码有效性
function isValuse(s) {
	if (s == null || s.length < 8) {
	   return false;
	}
	 
	var patrn = /^(?![a-zA-Z]+$)(?![A-Z0-9]+$)(?![A-Z\\W_!@#$%^&*`~.()-+=]+$)(?![a-z0-9]+$)(?![a-z\\W_!@#$%^&*`~.()-+=]+$)(?![0-9\\W_!@#$%^&*`~.()-+=]+$)[a-zA-Z0-9\\W_!@#$%^&*`~.()-+=]{8,30}$/
	if (!patrn.exec(s)){
		return false;
	}
return true;
}

//验证是否为正整数
function isInt(s){
	if(s==null || s==""){
		return false;
	}
	
	var patrn= /^([1-9]\d*|[0]{1,1})$/;
	if (!patrn.exec(s)){
		return false;
	}
	return true;
}

//验证是否为正整数
function checkPrice(s){
	if(s==null || s==""){
		return false;
	}
	var patrn= /^(([1-9][0-9]*)|(([0]\.\d{1,2}|[1-9][0-9]*\.\d{1,2})))$/;
	if (!patrn.exec(s)){
		return false;
	}
	return true;
}

/**
 * ajax的post方法：异步
 * @param url
 * @param param
 * @param callback
 * @return
 */
function AjaxPost(url,param,callback){
	$.ajax({
		type: "post",
		url: url,
		data:param,
		beforeSend: function(XMLHttpRequest){
			loadProgress();
		},
		contentType: "application/x-www-form-urlencoded; charset=utf-8",
		success: function(data, textStatus){
			callback(data);
		},
		complete: function(XMLHttpRequest, textStatus){
			hiddenProgress();
		},
		error: function(data){
			//loadError(data);
		}
	});
}
/**
 * ajax的post方法：异步方式json
 * @param url
 * @param param
 * @param callback
 * @return
 */
function AjaxPostJson(url,param,callback){
	$.ajax({
		type: "post",
		url: url,
		data:JSON.stringify(param),
		beforeSend: function(XMLHttpRequest){
			//loadProgress();
		},
		contentType: "application/json; charset=UTF-8",
		success: function(data, textStatus){
			callback(data);
		},
		complete: function(XMLHttpRequest, textStatus){
			//hiddenProgress();
		},
		error: function(data){
			//loadError(data);
		}
	});
}
/**
 * ajax的post方法：非异步方式非json
 * @param url
 * @param param
 * @param callback
 * @return
 */
function AjaxPostSync(url,param,callback){
	$.ajax({
		type: "post",
		url: url,
		data:param,
		cache: false,
		async: false,
		beforeSend: function(XMLHttpRequest){
			loadProgress();
		},
		contentType: "application/x-www-form-urlencoded; charset=utf-8",
		success: function(data, textStatus){
			callback(data);
		},
		complete: function(XMLHttpRequest, textStatus){
			hiddenProgress();
		},
		error: function(data){
			//loadError(data);
		}
	});
}

/**
 * ajax的post方法：非异步方式json
 * @param url
 * @param param
 * @param callback
 * @return
 */
function AjaxPostSyncJson(url,param,callback){
	$.ajax({
		type: "post",
		url: url,
		data:param,
		cache: false,
		async: false,
		beforeSend: function(XMLHttpRequest){
			loadProgress();
		},
		contentType: "application/x-www-form-urlencoded; charset=utf-8",
		success: function(data, textStatus){
			callback(data);
		},
		complete: function(XMLHttpRequest, textStatus){
			hiddenProgress();
		},
		error: function(data){
			//loadError(data);
		}
	});
}



/**********************************数据获取函数**********************************************************/
/**
 * 读取form内所有数据
 * 支持input、selectformValue、checkbox
 * @param name
 * @return
 */
function getFormValue(name){
	var obj = name;
	if(typeof(name) == 'string'){
		obj = '#'+name;
	}
	var param = {};
	$(obj).find('select,input,textarea').each(function(i,item){
		if(item.id){
			if($(item).attr('calendar') || $(item).attr('date')){
				param[item.id] = dateStringFormat($(item).val());
			}
			else if($(item).attr('type') == "checkbox"){
				if(item.checked){param[item.id] = "1";}
				else{param[item.id] = "0";}
			}
			else if($(item).attr('type') == "button"){
				//如果为按钮则跳过
			}
			
			else if($(item).attr('class') == "easyui-combotree"){
				//easyui 下拉树特殊处理
				param[item.id] = $('#'+item.id).combotree('getValue');
			}
			
			else if($(item).attr('ct') == "combotree"){ //easyui 下拉树特殊处理
				
				param[item.id] = $('#'+item.id).combotree('getValues').join(",");
			}
			
			else if($(item).attr('type') != "file"){
				if($(item).val() ==  null){param[item.id] = "";}
				else{$(item).val(jQuery.trim($(item).val()));param[item.id] = jQuery.trim($(item).val());}
			}
		}
	});
	return param;
}

//通过text设置指定select的选中项
function setSelectedOptByText(id, text) {
	var cnt = $("#" + id +" option").length;
	for(var i=0; i<cnt; i++) {
		if($("#"+id).get(0).options[i].text == text) {
			$("#"+id).get(0).options[i].selected = true;
			break;
		}
	}
}

var public_fn = {};
public_fn.openWin = function(param){
	if(param){
		//按屏幕比例显示
		if(param.percents){
			param.height = parseInt($(document).height() * param.percents);
			param.width = parseInt($(document).width() * param.percents);
			delete param.percents;
		}
		var win = new WindowOpen(param);
	}
};

/*
 * 	获得指定日期的星期数，1-6为星期一到星期六，0为星期天
 *	@y 年份
 *	@m 月份
 *	@d 日
 */
function getWeek(y, m, d) {
	var _int = parseInt,c = _int(y/100);
	y = y.toString().substring(2, 4);
	y = _int(y, 10);
	if(m === 1) {
		m = 13;
		y--;
	} else if(m === 2) {
		m = 14;
		y--;
	}
	var w = y + _int(y/4) + _int(c/4) - 2*c + _int(26*(m+1)/10) + d - 1;
	w = w%7;
	return w >= 0 ? w : w+7;
}

/**
*	验证指定的日期字符串是否为有效的日期
*	@s 格式:2011-12-02
*/
function isDate(s) { 
  var r = s.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/); 
  if(r==null)
	return false; 
  var d = new Date(r[1], r[3]-1,r[4]); 
  return (d.getFullYear()==r[1]&&(d.getMonth()+1)==r[3]&&d.getDate()==r[4]); 
}

/**
 * 获取当月最后一天
 * @param format日期格式（如：yyyy-MM-dd）
 * @return
 */
function getLastDateForMonth(format){
	var d = new Date(); 
	d = new Date(d.getFullYear(), d.getMonth()+1, 0); 
	return d.format(format);
}

/**
 * 设置form内控件为不可用
 * @param name
 * @return
 */
function setFormDisabled(name){
	var obj = name;
	if(typeof(name) == 'string'){
		obj = '#'+name;
	}
	$(obj).attr("disabled","true");
	$(obj).find('input,select,textarea').attr("disabled","true");
}

/**
 * 设置form内控件为可用
 * @param name
 * @return
 */
function setFormEnabled(name){
	var obj = name;
	if(typeof(name) == 'string'){
		obj = '#'+name;
	}
	$(obj).attr("disabled","");
	$(obj).find('input,select').attr("disabled","");
}

/**
 * select 控件加载json数组数据
 * @param name
 * @param jsonList
 * @param param 对应参数{value:"code",text:"name"}将code对应到value，name对应到text上{addnull:true}
 * @return
 */
function comboxLoad(name,jsonList,param){
	var obj = name;
	if(typeof(name) == 'string'){		
		obj = 'select[id='+name+']';
	}
	$(obj).children().remove();

	var idStr = "value";
	var textStr = "text";
	if(param){
		if(param.value){idStr = param.value;}
		if(param.text){textStr = param.text;}
		if(param.addnull){$('<option value=""></option>').appendTo(obj);}
	}
	$.each(jsonList,function(i,json){
		$('<option value='+json[idStr]+'>'+json[textStr]+'</option>').appendTo(obj);
	});
}

/**
 * select 控件加载json MAP对象数据
 * @param name 
 * @param hm MAP对象数据
 */
function comboxLoadHM(name,hm){
	
	var obj = name;
	if(typeof(name) == 'string'){		
		obj = 'select[id='+name+']';
	}
	$(obj).children().remove();
	
	$.each(hm,function(key,value){
		$('<option value='+key+'>'+value+'</option>').appendTo(obj);
	});
}

/**
 * 读取项目名称
 * @return
 */
function Get_ProjectName(){
    var jspath=location.href;
    jspath=jspath.substring(jspath.indexOf("//")+2);
    jspath=jspath.substring(jspath.indexOf("/")+1);
    var pro_name=jspath.substring(0,jspath.indexOf("/"));
    return pro_name;
}

/**
 * 获取请求url中的参数信息
 * @param {} names
 */
function getUrlParam(name)
{
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if(r!=null)
        return unescape(r[2]);
    return '';
}


/**
 * 按json中的key读取值
 * @param name
 * @param json
 * @return
 */
function get_jsonForName(name,json){
	for(var key in json){
		if(key == name){
			return json[key];
		}
	}
}

/**
 * 获取展开页面body的height
 * @return
 */
function getBodyHeight(){
	return $(document).height() - 4;
}


/**
 * 获取展开页面body的width
 * @return
 */
function getBodyWidth(){
	return $(document).width() - 4;
}

/**********************************对象增减函数**********************************************************/
/**
 * 显示进度条
 * @return
 */
function loadProgress(msg){
	if(!msg){		
		msg = "数据查询中...";
	}
	$('<div class="datagrid-mask loadProgress" style="z-index: 9998;position:absolute;left:0;top:0;background:#ccc;opacity:0.3;filter:alpha(opacity=30);"></div>').css('display','block').css('height',$(document).height() - 4 + "px").css('width','100%').appendTo(document.body);
	$('<div class="load-progress" style="z-index: 9999;width:200px;background-color:#fff;position:absolute;top:50%;left:50%;margin-left:-80px;margin-top:-30px;border:1px solid #99BBE8;padding:10px;"><div>' + msg + '</div><marquee style="border:1px solid #000000" direction="right" width="200" scrollamount="5" scrolldelay="10" bgcolor="#ECF2FF"><table cellspacing="1" cellpadding="0"><tr height=8><td bgcolor=#99BBE8 width=8></td><td></td><td bgcolor=#99BBE8 width=8></td><td></td><td bgcolor=#99BBE8 width=8></td><td></td><td bgcolor=#99BBE8 width=8></td><td></td></tr></table></marquee></div>').appendTo(document.body);	
}
/**
 * 隐藏进度条
 * @return
 */
function hiddenProgress(){
	$('.loadProgress').remove();
	$('.load-progress').remove();
}
/**
 * 显示进遮挡
 * @return
 */
function loadDodging(){
	$('<div class="datagrid-mask loadDodging"></div>').css('display','block').css('height',$(document).height() - 20 + "px").css('width',$(document).width() - 20 + "px").appendTo(document.body);
}
/**
 * 隐藏进遮挡
 * @return
 */
function hiddenDodging(){
	$('.loadDodging').remove();
}

/**********************************string格式化函数**********************************************************/

/**
 * 消息类型的格式化
 */
function messageTypeFormat(type){
	if(null == type){
		return type;
	} else if(2 == type) {
		return "重要消息";
	} else if(1 == type) {
		return "普通消息";
	} else if(0 == type) {
		return "系统消息";
	} else if(3 == type) {
		return "通知";
	}
};

/**
 * 消息状态的格式化
 * @param status
 * @returns
 */
function messageStatusFormat(status){
	if(null == status){
		return status;
	} else if(0 == status) {
		return "未查看";
	} else if(1 == status) {
		return "已查看";
	}
};

/**
 * 简单日期格式化
 * 20100703 -> 2010-07-03
 * 2010-07-03 -> 20100703
 * 20100703123021 -> 2010-07-03 12:30:21
 * 2010-07-03 12:30:21 -> 20100703123021
 * @return
 */
function dateStringFormat(strDate){
	if(strDate == null){return strDate;}
	else if(strDate.length == 6){
		return strDate.substr(0,4) + "-"  + strDate.substr(4,2);
	}
	else if(strDate.length == 7){
		return strDate.replaceAll("-","");
	}
	else if(strDate.length == 8){
		return strDate.substr(0,4) + "-"  + strDate.substr(4,2) + "-"+strDate.substr(6,2);
	}
	else if(strDate.length == 10){
		return strDate.replaceAll("-","");
	}
	else if(strDate.length == 14){
		return strDate.substr(0,4) + "-"  + strDate.substr(4,2) + "-"+strDate.substr(6,2) + " " + strDate.substr(8,2) + ":" + strDate.substr(10,2) + ":" + strDate.substr(12,2);
	}
	else if(strDate.length == 19){
		return strDate.replaceAll("-","").replaceAll(" ","").replaceAll(":","");
	}
	return strDate;
}

/**
 * 启用、禁用格式化
 * 1 -> 启用、 0 -> 禁用、 “” -> 全部
 * 启用 -> 1、 禁用 -> 0、 全部 -> “” 
 * @param str
 * @return
 */
function enableStringFormat(str){
	if(str == null){return str;}
	else if(str == '1'){return "启用";}
	else if(str == '0'){return "禁用";}
	else if(str == ''){return "全部";}
	else if(str == '启用'){return "1";}
	else if(str == '禁用'){return "0";}
	else if(str == '全部'){return "";}
	else{return str;}
}

/**
 * 是、否格式化
 * 1 -> 是、 0 -> 否、 “” -> 全部
 * 是 -> 1、 否 -> 0、 全部 -> “” 
 * @param str
 * @return
 */
function yesStringFormat(str){
	if(str == null){return str;}
	else if(str == '1'){return "是";}
	else if(str == '0'){return "否";}
	else if(str == ''){return "全部";}
	else if(str == '是'){return "1";}
	else if(str == '否'){return "0";}
	else if(str == '全部'){return "";}
	else{return str;}
}

/**
 * 秒格式化。 
 * 3723 -> 1小时2分3秒
 * 63 -> 1分3秒
 * 53 -> 53秒
 * @param sec
 * @return
 */
function secondFormat(sec){
	if(sec == "" || sec == null){return sec;}
	var hour = Math.floor(sec/3600);
	var min = Math.floor((sec - hour*3600)/60);
	var second = sec - hour*3600 - min*60;
	var ret = "";
	if(hour != 0){ret += hour + "小时";}
	if(min != 0){ret += min + "分";}
	if(second != 0){ret += second + "秒";}
	return ret;
}

function weekStringFormat(str)
{
	if(str.length == 8){
		var YEAR=1901,MONTH=1,DAY=7;
		var i,j,total,leapcount=0,ordcount=0,day=0;
		var y = parseFloat(str.substr(0,4));
		var m = parseFloat(str.substr(4,2));
		var d = parseFloat(str.substr(6,2));
	     for(i=1;i<m;i++)                      //所求年的天数
	         switch(i){
	          case 1:
	          case 3:
	          case 5:
	          case 7:
	          case 8:
	          case 10:
	          case 12:day=day+31;
	                  break;
	          case 4:
	          case 6:
	          case 9:
	          case 11:day=day+30;
	                  break;
	          case 2:day=day+28;
	                 break;
	          }
	       day=day+d;
	       for(j=YEAR;j<y;j++)                  //计算平闰年个数
	          if(j%400==0||j%100!=0&&j%4==0)
	             leapcount++;
	          else
	             ordcount++;
	       total=2*leapcount+ordcount+day-DAY;
	       if(d>2)
	         if(y%400==0||y%100!=0&&y%4==0)
	            total+=1;
	       week=total%7 + 1;
	       return week;
	}else{return str;}
}
/**********************************注册为prototype函数**********************************************************/
/**
 * 实现replaceAll方法
 * @param s1
 * @param s2
 * @return
 */
String.prototype.replaceAll  = function(s1,s2){   
	return this.replace(new RegExp(s1,"gm"),s2);   
};

/**
 * 日期格式化
 * alert(new Date().format("yyyy-MM-dd")); 
	alert(new Date("january 12 2008 11:12:30").format("yyyy-MM-dd hh:mm:ss")); 
 * @param format
 * @return
 */
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; 
};

