//去除2边空格
String.prototype.trim = function() { 
   return this.replace(/(^\s*)|(\s*$)/g, ""); 
}
// 去除字符串左侧空格
String.prototype.trimL = function() { 
   return this.replace(/(^\s*)/g, ""); 
}
// 去除字符串右侧空格
String.prototype.trimR = function() { 
   return this.replace(/(\s*$)/g, ""); 
}
// 字符转换日期 如:"2010-10-10 00:00:00" 转换成 Date
String.prototype.stringToDate = function() { 
	   return new Date(Date.parse(this.replace(/-/g,   "/")));; 
}

// 字符转换日期 如:"2010-10-10 00:00:00" 转换成 Date
function stringToDate(s)
{
	return new Date(Date.parse(s.replace(/-/g,   "/")));
}


/**
 * leon options : width, height, top, left, toolbar, menubar, scrollbars,
 * resizable,location,status,name example : popWin( url,
 * {width:400,height:500,status:no} ; args : 传递给弹出窗口的参数，如：{id:1,name:2}
 */
function popWin(openurl, options, isModal, args) {
	
	var width = undefined!=options && options.width ? options.width : 1024;// 窗口高度；
  	var height = undefined!=options && options.height ? options.height : 768;  // 窗口宽度；
  	var top = undefined!=options && options.top ? options.top :(screen.availHeight-height)/2;  // 窗口距离屏幕上方的象素值；
  	var left= undefined!=options && options.left ? options.left : (screen.availWidth-width)/2; // 窗口距离屏幕左侧的象素值；
  	var toolbar = undefined!=options && options.toolbar || "no";  // 是否显示工具栏，yes为显示；
  	var menubar = undefined!=options && options.menubar || "no";// 是否显示菜单栏，yes为显示；
  	var scrollbars = undefined!=options && options.scrollbars || "yes"; // 是否显示滚动栏，yes为显示；
  	var resizable = undefined!=options && options.resizable || "no";// 是否允许改变窗口大小，yes为允许；
	var location = undefined!=options && options.location || "no";// 是否显示地址栏，yes为允许；
	// var status = options.status || "yes";//是否显示状态栏内的信息（通常是文件已经打开），yes为允许；
	var status = "yes";
	if(undefined==options || options.status==="no") {
		height += 20;
	}
	var name = undefined==options?'default':options.name;
	if( isModal ) {		 
		var features = "help=no;dialogHeight=" + height +
				         "px;dialogWidth=" + width +
				         "px;scroll=" + scrollbars + 
				         ";resizable=" + resizable + 
				         ";location=" + location + 
				         ";status=" + status;
		var val = window.showModalDialog( openurl, [window, args], features );
		return val;
	}else{
		var features = "help=no,height=" + height +
			         "px,width=" + width +
			         "px,scroll=" + scrollbars + 
			         ",scrollbars=" + scrollbars + 
			         ",resizable=" + resizable + 
			         ",location=" + location + 
			        ",top=" + top + 
			         ",left=" + left + 
			         ",toolbar=" + toolbar + 
			         ",menubar=" + menubar + 
			         ",status=" + status;
		var win = window.open(openurl, name, features );
  		win.focus();
		return win;
  	}
}


/** ----------------XML解析兼容其他浏览器start 重载-----------* */
if(document.implementation && document.implementation.createDocument)
{
	XMLDocument.prototype.loadXML = function(xmlString)
	{
		var childNodes = this.childNodes;
		for (var i = childNodes.length - 1; i >= 0; i--)
		   this.removeChild(childNodes[i]);
		var dp = new DOMParser();
		var newDOM = dp.parseFromString(xmlString, "text/xml");
		var newElt = this.importNode(newDOM.documentElement, true);
		this.appendChild(newElt);
		
	};
	if( document.implementation.hasFeature("XPath", "3.0") )
	{
	    XMLDocument.prototype.selectNodes = function(cXPathString, xNode)
	    {
		    if( !xNode ) 
		    	xNode = this; 
		    var oNSResolver = this.createNSResolver(this.documentElement);
		    var aItems = this.evaluate(cXPathString, xNode, oNSResolver,XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null)
		    var aResult = [];
		    for( var i = 0; i < aItems.snapshotLength; i++)
		    	aResult[i] = aItems.snapshotItem(i);
		    return aResult;
	    }
	    Element.prototype.selectNodes = function(cXPathString)
	    {
		    if(this.ownerDocument.selectNodes)
		    	return this.ownerDocument.selectNodes(cXPathString, this);
		    else
		    	throw "For XML Elements Only";
	    }
	}
	if( document.implementation.hasFeature("XPath", "3.0") )
	{
	    XMLDocument.prototype.selectSingleNode = function(cXPathString, xNode)
	    {
		    if( !xNode ) 
		    	xNode = this; 
		    var xItems = this.selectNodes(cXPathString, xNode);
		    if( xItems.length > 0 )
		    	return xItems[0];
		    else
		    	return null;
	    }
	    // prototying the Element
	    Element.prototype.selectSingleNode = function(cXPathString)
	    {  
		    if(this.ownerDocument.selectSingleNode)
		    	return this.ownerDocument.selectSingleNode(cXPathString, this);
		    else
		    	throw "For XML Elements Only";
	    }
	}
}
/** ----------------XML解析兼容-end----------* */

function CommonUtil()
{
	//AJAX请求方法
	this.webAjax;
	
	//弹出框方法
	this.webMsgBox;
	/**
	 * 采用ajax访问服务器
	 * @param action: 所访问的action-path, 缺省为dynamicAction
	 * @param method: 所调用service中的方法名称
	 * @param params: 符带参数
	 * @param callback: 访问服务器后返回后所执行的方法callback(result)
	 * @param errorcallback: 服务器出现异常时所调用的方法errorcallback(response), 如果找不到此方法则打印:'访问服务器失败! 可能存在网络故障或其他未知原因!'
	 * @param msg: 访问服务器时所显示的加载框, msg=false表示不显示, msg=true表示显示默认框, msg=string表示显示框中的提示信息为此string, 缺省为true
	 */
	this.ajax = function(config)
	{
		if(this.isEmpty(CommonUtil.webAjax)) 
			CommonUtil.webAjax = new WebAjax();
		CommonUtil.webAjax.ajax(config);
	}
	
	
	/**
	 * 弹出窗口
	 * @param {} config
	 * title: 标题
	 * msg: 文本或者HTML内容
	 * option: -1=OK  0=YES NO  1=YES NO CANCEL  2=OK CANCEL  5=Wait
	 * callback : 当点击选项按钮之后所执行的方法
	 */
	this.showMsg = function(config) {
		if(this.isEmpty(CommonUtil.webMsgBox)) 
			CommonUtil.webMsgBox = new WebMessageBox();
		CommonUtil.webMsgBox.show(config);
	}
	
	
	/**
	 * 等待窗口
	 * @param {} msg
	 * @param {} title
	 * @param {} callback
	 */
	this.wait = function() 
	{
		if(this.isEmpty(CommonUtil.webMsgBox)) 
			CommonUtil.webMsgBox = new WebMessageBox();
		CommonUtil.webMsgBox.show({title:'Please Wait...',msg:'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;正在访问服务器, 请稍候...',option:5});
	}
	
	/**
	 * 关闭窗口
	 */
	this.hide = function() {
		if(this.isEmpty(CommonUtil.webMsgBox)) 
			CommonUtil.webMsgBox = new WebMessageBox();
		CommonUtil.webMsgBox.hide();
	}
	
	
	/**
     * 判断v是否是function
     */
    this.isFunction = function(v)
    {
        return typeof v == "function";
    }
    
    /**
	 * 判断v是否为空
	 * @param {} v
	 * @param {} allowBlank : 是否允许空字符串 缺省为false 即:空字符串也认为是空
	 * @return {}
	 */
    this.isEmpty = function(v, allowBlank) {
        return v === null || v === undefined || ((this.isArray(v) && !v.length)) || (!allowBlank ? v === '' : false);
    }
    
    /**
     * 判断v是否是Array
     * @param {} v
     * @return {}
     */
    this.isArray = function(v){
        return Object.prototype.toString.apply(v) === '[object Array]';
    }
    
    /**
     * 判断v是否是object
     * @param {} v
     * @return {}
     */
    this.isObject = function(v){
    	return v && typeof v == "object";
    }
    
    /**
     * 过滤掉undefined
     * @param {} value
     * @return {}
     */
    this.undef = function(value) {
        return value !== undefined ? value : "";
    }
    
    
    /**
	 * 获取正则表达式
	 * 
	 * @param {} type 
	 * 		1: 5-20位首字符为字母
	 *      2: 邮箱
	 *      3: 字母和数字的组合
	 *      4: 数字
	 *      5: 4位数字
	 *      6: 身高
	 *      7: 邮编
	 *      8: 数字
	 *      9: 钱, 保留2位小数位
	 *      10:钱, 保留4位小数位
	 *      11:20以内字母、数字、汉字组合
	 *      12:20以内字母、数字、下划线组合
	 */
	this.getRegexPattern = function(type) {
		switch (type) {
			case 1 : return /^[a-zA-Z]{1}(.){4,19}$/;
			case 2 : return /^.+@.+[.].+$/;
			case 3 : return /^[a-zA-Z0-9]+$/;
			case 4 : return /^[0-9]+$/;
			case 5 : return /^[0-9]{4}$/;
			case 6 : return /^[0-9]{0,3}$/;
			case 7 : return /^[0-9]{6}$/;
			case 8 : return /^[0-9]*$/;
			case 9 : return /^[0-9]+(([.][0-9]{1,2})|([0-9]*))$/;
			case 10 : return /^[0-9]+(([.][0-9]{1,4})|([0-9]*))$/;
			case 11 : return /^([a-zA-Z0-9]|[\u4e00-\u9fa5]){0,20}$/;
			case 12 : return /^([a-zA-Z0-9]|[_]){0,20}$/;
		}
	}
	
	
	/**
	 * 却除字符串中所有空格
	 * @param {} str
	 * @return {}
	 */
	this.trimAll = function(str) {
		var length = str.length;
		var str2 = "";
		var temp;
		for(var i=0; i<length; i++) {
			temp = str.charAt(i);
			if(temp!=" ") {
				str2 += temp;
			}
		}
		return str2;
	}
	
	
	/**
	 * 却除字符串中前空格
	 * @param {} str
	 * @return {}
	 */
	this.trimLeft = function(str) {
		var length = str.length;
		var i = 0;
		var temp;
		for(; i<length; i++) {
			temp = str.charAt(i);
			if(temp != " ") {
				break;
			}
		}
		return str.substring(i, length);
	}
	
	/**
	 * 却除字符串中后空格
	 * @param {} str
	 * @return {}
	 */
	this.trimRight = function(str) {
		var length = str.length;
		var i = length-1;
		var temp;
		for(; i>=0; i--) {
			temp = str.charAt(i);
			if(temp != " ") {
				break;
			}
		}
		return str.substring(0, i+1);
	}
	
	/**
	 * 却除字符串中前后空格
	 * @param {} str
	 * @return {}
	 */
	this.trim = function(str) {
		if(str==undefined || str==null) return str;
		var length = str.length;
		var i;
		var temp;
		var tempStr;
		for(i=0; i<length; i++) {
			temp = str.charAt(i);
			if(temp != " ") {
				break;
			}
		}
		tempStr = str.substring(i, length);
		for(i=tempStr.length-1; i>=0; i--) {
			temp = tempStr.charAt(i);
			if(temp != " ") {
				break;
			}
		}
		return tempStr.substring(0, i+1);
	}
	
	
	/**
	 * 获取当前日期  格式: yyyy-MM-dd HH:mm:ss
	 * @return {}
	 */
	this.getIntraDate = function() {
		var date = new Date();
		var year = date.getFullYear();
		var month = date.getMonth()+1;
		if(month < 10) month = "0"+month;
		var day = date.getDate();
		if(day < 10) day = "0"+day;
		var hour = date.getHours();
		if(hour < 10) hour = "0"+hour;
		var minute = date.getMinutes();
		if(minute < 10) minute = "0"+minute;
		var second = date.getSeconds();
		if(second < 10) second = "0"+second;
		return year+"-"+month+"-"+day+" "+hour+":"+minute+":"+second;
	}
	
	/**
	 * 获取当前日期  格式: yyyy-MM-dd
	 * @return {}
	 */
	this.getShortIntraDate = function() {
		var date = new Date();
		var year = date.getFullYear();
		var month = date.getMonth()+1;
		if(month < 10) month = "0"+month;
		var day = date.getDate();
		if(day < 10) day = "0"+day;
		return year+"-"+month+"-"+day;
	}

	/**
	 * 获取当前时间 HH:mm:ss
	 * @return {}
	 */
	this.getTime = function() {
		var date = new Date();
		var hour = date.getHours();
		if(hour < 10) hour = "0"+hour;
		var minute = date.getMinutes();
		if(minute < 10) minute = "0"+minute;
		var second = date.getSeconds();
		if(second < 10) second = "0"+second;
		return hour+":"+minute+":"+second;
	}
	
	/**
	 * 将日期转换成字符串 yyyy-MM-dd HH:mm:ss
	 * @param {} date
	 * @return {}
	 */
	this.toDateString = function(date) {
		var year = date.getFullYear();
		var month = date.getMonth()+1;
		if(month < 10) month = "0"+month;
		var day = date.getDate();
		if(day < 10) day = "0"+day;
		var hour = date.getHours();
		if(hour < 10) hour = "0"+hour;
		var minute = date.getMinutes();
		if(minute < 10) minute = "0"+minute;
		var second = date.getSeconds();
		if(second < 10) second = "0"+second;
		return year+"-"+month+"-"+day+" "+hour+":"+minute+":"+second;
	}
	
	/**
	 * 将日期转换成字符串 yyyy-MM-dd
	 * @param {} date 
	 * @return {}
	 */
	this.toShortDateString = function(date) {
		var year = date.getFullYear();
		var month = date.getMonth()+1;
		if(month < 10) month = "0"+month;
		var day = date.getDate();
		if(day < 10) day = "0"+day;
		return year+"-"+month+"-"+day;
	}
	
	/**
	 * 获取两时间之差(天)
	 * @param {} small
	 * @param {} big
	 * @return {}
	 */
	this.getDateDiff = function(small, big) {
	    return (big-small)/1000/60/60/24;
	}
	
	/**
	 * 获取即当前之前frontcount天的时期  frontcount不能大于28
	 * @param {} frontcount
	 * @return {}
	 */
	this.getFrontDate = function(frontcount) {
		var date = new Date();
		var year = date.getFullYear();
		var month = date.getMonth()+1;
		var day = date.getDate();
		if(day > frontcount) {
			day = day - frontcount;
		}else {
			if(month == 1) {
				year -- ;
				month = 13;
			}
			month -- ;
			day = this.getDaysInMonth(year,month)+day-frontcount;
		}
		if(month < 10) month = "0"+month;
		if(day < 10) day = "0"+day;
		return year+"-"+month+"-"+day;
	}
	
	/**
	 * 获取指定月份的天数
	 * @param {} year
	 * @param {} month
	 * @return {Number}
	 */
	this.getDaysInMonth = function(year, month) {
		switch (month) {
			case 1: 
			case 3: 
			case 5: 
			case 7: 
			case 8: 
			case 10: 
			case 12: return 31;
			case 4: 
			case 6: 
			case 9: 
			case 11: return 30;
			case 2: return ((year%4==0 && year%100!=0) || (year%400==0)) ? 29 : 28;
			default: return -1;
		}
	}
	
	/**
	 * 获取日期的大写周
	 * @param {} dateStr '2010-10-10'
	 * @return {(日) (一) ...}
	 */
	this.getWeekUpperCase = function(dateStr) 
	{      
		var myweekday="";        
		var re = /-/g;    
		mydate=new Date(dateStr.replace(re,"/"));         
		myweekday=mydate.getDay();         
		if(myweekday == 0)         
		weekday="(日)";         
		else if(myweekday == 1)         
		weekday="(一)";         
		else if(myweekday == 2)         
		weekday="(二)";         
		else if(myweekday == 3)         
		weekday="(三)";         
		else if(myweekday == 4)         
		weekday="(四)";         
		else if(myweekday == 5)         
		weekday="(五)";         
		else if(myweekday == 6)         
		weekday="(六)";         
		return weekday;
	} 
	
	/**
	 * 明天日期  返回字符串 yyyy-MM-dd
	 * @param {} sd 字符串 yyyy-MM-dd
	 * @return {}
	 */
	this.tomorrow = function(sd) {
		var arr = sd.split("-");
		var year = parseInt(arr[0],10);
		var month = parseInt(arr[1],10);
		var day = parseInt(arr[2],10);
		day ++ ;
		var ms = this.getDaysInMonth(year,month);
		if(day > ms) {
			day = 1;
			month ++ ;
			if(month > 12) {
				month = 1;
				year ++ ;
			}
		}
		if(month < 10) month = "0"+month;
		if(day < 10) day = "0"+day;
		return year+"-"+month+"-"+day;
	}
	
	/**
	 * 获取昨天日期 返回字符串 yyyy-MM-dd
	 * @param {} sd 字符串 yyyy-MM-dd
	 * @return {}
	 */
	this.yesterday = function(sd) {
		var arr = sd.split("-");
		var year = parseInt(arr[0],10);
		var month = parseInt(arr[1],10);
		var day = parseInt(arr[2],10);
		day -- ;
		if(day == 0) {
			month --;
			if(month == 0) {
				month = 12;
				year -- ;
			}
			day = this.getDaysInMonth(year,month);
		}
		if(month < 10) month = "0"+month;
		if(day < 10) day = "0"+day;
		return year+"-"+month+"-"+day;
	}
	
	
	/**
	 * 刷新当前页面
	 */
	this.refreshPage = function() {
		window.location.reload();
	}
	
	this.addEvent = function(event,method)
	{
		if (document.all) 
			window.attachEvent(event, method);
		else 
		    window.addEventListener(event, method, false);
		
		
		
	}
	
	
	/**
	 * 装载事件
	 */
	this.loadListener = function(fn)
	{
		if (typeof window.addEventListener != 'undefined')
		{
		    window.addEventListener('load', fn, false);
		}
		else if (typeof document.addEventListener != 'undefined')
		{
		    document.addEventListener('load', fn, false);
		}
		else if (typeof window.attachEvent != 'undefined')
		{
			window.attachEvent('onload', fn);
		}
		else
		{
			var oldfn = window.onload;
			if (typeof window.onload != 'function')
			{
			    window.onload = fn;
			}
			else
			{
			    window.onload = function()
			    {
			        oldfn();
			        fn();
			     };
			
			 }
		}
	}
	/**
	 * 卸载事件
	 */
	this.unLoadListener = function(fn)
	{
		if (typeof window.addEventListener != 'undefined')
		{
		    window.removeEventListener('load', fn, false);
		}
		else if (typeof document.addEventListener != 'undefined')
		{
		    document.removeEventListener('load', fn, false);
		}
		else if (typeof window.attachEvent != 'undefined')
		{
			window.detachEvent('onload', fn);
		}
		else
		{
			var oldfn = window.onload;
			if (typeof window.onload != 'function')
			{
			    window.onload = fn;
			}
			else
			{
			    window.onload = function()
			    {
			        oldfn();
			        fn();
			     };
			
			 }
		}
	}
}
//提示 [是] [否] 后提交
/**
 * subBtn提交BUTTON  msg 提示信息
 */
function saveConfirm(subBtn,msg)
{
	var conBtn  = confirm(msg);
	if(conBtn==true)
	{
		document.getElementById(subBtn).click();
	}
	
}


//初始化 公用JS
var JU =new CommonUtil();
var Context;//项目全局管理对象






