/*获取事件源*/
util.getEvent = function() {
	var e = window.event;
	if (!e) {
		var a = util.getEvent.caller;
		while (a) {
			if (a.arguments) {
				e = a.arguments[0];
				if (e && util.isObject(e) && e.type && e.keyCode)
					break
			}
			a = a.caller
		}
	}
	return e
};
/* 中断事件 */
util.preventEvt = function(e) {
	if (e.charCode != 0) {
		util.isIe() ? event.returnValue = false : e.preventDefault()
	}

};
/* 获取按键事件 */
util.getKeyEvt = function() {
	var e = util.getEvent();
	if (!e.target)
		e.target = e.srcElement;
	e.key = e.keyCode || e.charCode;
	return e
};
/* keypress事件,可以验证数字或电话或邮编 */
function kp_num(t, l) {
	var e = util.getKeyEvt(), c = e.key, v, n;
	if (l) { // 检查长度
		n = e.target.value.length;
		if (l && n >= l)
			util.preventEvt(e);
	}
	t = t || 'int'; // 默认是整数
	if (t == 'int') {
		if (e.charCode != 0 && c < 48 || c > 59)
			util.preventEvt(e)
	} else if (t == 'phone') { // 电话号码
		v = e.target.value;
		n = v.length;
		if (c == 45) {
			if (n < 2 || v.charCodeAt(n - 1) == c)
				util.preventEvt(e)
		} else if (e.charCode != 0 && c < 48 || c > 59)
			util.preventEvt(e);
	} else if (t == 'float') {
		if (e.charCode != 0 && c < 48 || c > 59) {
			if (c != 46 || e.target.value.indexOf(".") != -1)
				util.preventEvt(e)
		}
	}
}
var v = {
	checkNull : function(a, b) { /* 验证空值 */
		return a && a.value.length == 0 ? this.f(a, b) : 1;
	},
	checkLength : function(a, b, c) { /* 验证长度 */
		return a && a.value.length > c ? this.f(a, b) : 1;
	},
	checkRadio : function(a, b) {/* 验证单选按钮是否被选中 */
		if (typeof (a) == "string") {
			a = util.$by(a);
		} else if (typeof (a) == "object") {
			a = util.$by(a.name);
		}
		var count = 0;
		for ( var i = 0; i < a.length; i++) {
			if (a[i].checked) {
				count = count + 1;
			}
		}
		return count == 0 ? this.f(a, b) : 1;
	},
	checkCheckBox : function(a, b) {
		if (typeof (a) == "string") {
			a = util.$by(a);
		} else if (typeof (a) == "object") {
			a = util.$by(a.name);
		}
		var count = 0;
		for ( var i = 0; i < a.length; i++) {
			if (a[i].checked)
				count = count + 1;
		}
		return count == 0 ? this.f(a, b) : 1;
	},
	checkNum : function(a, b) { /* 验证数字 */
		return a.value.length > 0 && a.value.search("[^0-9]") >= 0 ? this.f(a,
				b) : 1;
	},
	checkNumZ : function(a, b) { /* 验证数字和字母 */
		return a.value.length > 0 && a.value.search("[^a-z0-9_]") >= 0 ? this
				.f(a, b) : 1;
	},
	NumCompare : function(a, b, num) {/* 数字之间的比较 */
		return a.value < num ? this.f(a, b) : 1;
	},
	checkMoney : function(a, b) { /* 验证数字 */
		return a.value.length > 0
				&& !a.value
						.match(/^[\-\+]?([0-9]\d*|0|[1-9]\d{0,2}(,\d{3})*)(\.\d+)?$/) ? this
				.f(a, b)
				: 1;
	},
	checkDec : function(a, b) { /* 验证数字 c是数位最大长度，d是小数位最大长度 */
		return a.value.length > 0 && a.value.search("[^0-9\.-]") >= 0 ? this.f(
				a, b) : 1;
	},
	checkDecLen : function(a, b, c, d) { /* 验证数字 */
		var v = a.value, l = v.length, r = 1;
		if (l > 0 && v.search("[^0-9\.]") >= 0)
			r = this.f(a, b);
		if (c && l > c)
			r = this.f(a, '长度必须小于等于' + c + '位!');
		if (d && v.match(/^\d+\.(\d+)$/) && RegExp.$1.length > d)
			r = this.f(a, '小数位必须小于等于' + d + '位!');
		return r;
	},
	checkEmail : function(a, b) { /* 验证Email */
		return a && !a.value.match("^(.+)@(.+)$") ? this.f(a, b) : 1;
	},
	checkIp : function(a, b) { /* 验证Ip */
		return a && !a.value.match(/^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/) ? this
				.f(a, b)
				: 1;
	},
	checkUrl : function(a, b) { /* 验证Url */
		return a
				&& !a.value
						.match(/^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/) ? this
				.f(a, b)
				: 1;
	},
	checkPhone : function(a, b) { /* 验证电话号码 */
		return a && !a.value.match(/\d{3}-\d{7}|\d{4}-\d{7}/) ? this.f(a, b)
				: 1;
	},
	checkPwd : function(a, b, c) {
		return a.value != 'xxxxxx' && b.value == 'xxxxxx' ? this.f(b, c) : 1;
	},
	checkTwopwd : function(a, b, c) {
		return a.value != b.value ? this.f(b, c) : 1;
	},
	check : function(a, b, r) { /* r是正则表达式 */
		return a && !a.value.match(r) ? this.f(a, b) : 1;
	},
	f : function(a, b) {
		if (b) {
			alert(b);
		}// a.focus();
	return 0
},
existsSelect : function(a, b) {
	return a.options.length == 0 ? this.f(a, b) : 1
}
}

/* 是否是小数子 */
function isDecimal(str, f, n) {
	var p = str.indexOf(".");
	var int, flt;

	if (str == "")
		return true;
	if (p < 0) {
		p = str.length;
	}
	int = str.substr(0, p);
	flt = str.substr(p + 1);
	if (isInt(int) == false) {
		return false;
	}
	if (flt != '') {
		if (isInt(flt) == false) {
			return false;
		}
	}
	if ((int.length > f - n) || (flt.length > n)) {
		return false;
	}
	return true;
}
// 验证输入时间是否在当前时间以前/**比如当前时间是：2009－08－10，输入时间是2009－08－01
// ************添加日期时用*************/
function isBeforeNow(oldDate) {
	if (oldDate == null) {
		return false;
	}
	var startDate = new Date();
	var endDate = new Date(getFormatDate(oldDate));

	var sy = startDate.getFullYear() * 10000;
	var sm = (startDate.getMonth() + 1) * 100;
	var sd = startDate.getDate();

	var ey = endDate.getFullYear() * 10000;
	var em = (endDate.getMonth() + 1) * 100;
	var ed = endDate.getDate();

	var s = sy + sm + sd;
	var e = ey + em + ed;
	if ((e - s) < 0) {
		return true;
	}
	return false;
}

// 验证输入时间是否在当前时间以后/**比如当前时间是：2009－08－10，输入时间是2009－08－20
// ***********查询时常用××××××××××××××/
function isAfterNow(oldDate) {
	if (oldDate == null) {
		return false;
	}
	var startDate = new Date();
	var endDate = new Date(getFormatDate(oldDate));

	var sy = startDate.getFullYear() * 10000;
	var sm = (startDate.getMonth() + 1) * 100;
	var sd = startDate.getDate();

	var ey = endDate.getFullYear() * 10000;
	var em = (endDate.getMonth() + 1) * 100;
	var ed = endDate.getDate();

	var s = sy + sm + sd;
	var e = ey + em + ed;
	if ((e - s) > 0) {
		return true;
	}
	return false;
}

// 比较前一时间是否在后一时间前面
function isBeforeDate(start, end) {
	if (start == null || end == null) {
		return true;
	}
	var startDate = new Date(getFormatDate(start));
	var endDate = new Date(getFormatDate(end));

	var sy = startDate.getFullYear() * 10000;
	var sm = (startDate.getMonth() + 1) * 100;
	var sd = startDate.getDate();

	var ey = endDate.getFullYear() * 10000;
	var em = (endDate.getMonth() + 1) * 100;
	var ed = endDate.getDate();

	var s = sy + sm + sd;
	var e = ey + em + ed;
	if ((e - s) < 0) {
		return false;
	}
	return true;
}
function getFormatDate(strDate) {
	var linkStr = "";
	if (strDate.indexOf("-") != -1) {
		linkStr = "-";
	}
	if (strDate.indexOf("/") != -1) {
		linkStr = "/";
	}
	var arrDate = strDate.split(linkStr);
	return arrDate[1] * 1 + "-" + arrDate[2] * 1 + "-" + arrDate[0] * 1;
}

// 全选
function checkAll(e, itemName) {
	var name = document.getElementsByName(itemName);
	for ( var i = 0; i < name.length; i++)
		name[i].checked = e.checked;
}

// 选择单个并取消全选
function checkItem(e, allName) {
	var all = document.getElementsByName(allName)[0];
	if (!e.checked)
		all.checked = false;
	else {
		var name = document.getElementsByName(e.name);
		for ( var i = 0; i < name.length; i++) {
			if (!name[i].checked) {
				all.checked = false;
				return;
			} else {
				all.checked = true;
			}
		}
	}
}

//全角逗号转半角逗号
function fullChar2halfChar(str)
{
	var result = '';
	
	for (i=0 ; i<str.length; i++)
	{
		code = str.charCodeAt(i);//获取当前字符的unicode编码
		if (code >= 65281 && code <= 65373)//在这个unicode编码范围中的是所有的英文字母已经各种字符
		{
			result += String.fromCharCode(str.charCodeAt(i) - 65248);//把全角字符的unicode编码转换为对应半角字符的unicode码
		}else
		{
			result += str.charAt(i);
		}
	}
	return result;
}

function checkIsFloat(str,prompt){
	var pattern = /^\d+\.\d+$/;
	    if (pattern.test(str)) {
	       var num = parseFloat(str);
		    if(num < 0.0001){
		    	alert(prompt);
		       return false;
		    }else{
		       return true;
		    }
	    } else {
	    	alert(prompt);
	        return false;
	    }
}


function checkKey(key){
   alert(key);
   key = fullChar2halfChar(key);
   key = key.replace(/\s/g, ",");
   key = key.replace(/\n|\r/g,",");
   key = key.replace(/,{2,}/g,",");
   
   return key;
}