var Util = new Object();
Util.openWindow = function (url, width, height, left, top) {
	if (left == null) {
		left = "250px";
	}
	if (top == null) {
		top = "150px";
	}
	if (width == null) {
		width = "500px";
	}
	if (height == null) {
		height = "500px";
	}
	var param = "height=" + height + ",width=" + width + ",left=" + left + ",top=" + top + ",resizable=yes,scrollbars=yes,status=no,toolbar=no,menubar=no,location=no";
	window.open(url, "", param);
};
Util.htmlEncode = function (text) {
	return text.replace(/&/g, "&amp;").replace(/"/g, "&quot;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
};
Util.trim = function (text) {
	if (typeof (text) == "string") {
		return text.replace(/^\s*|\s*$/g, "");
	} else {
		return text;
	}
};
Util.isEmpty = function (val) {
	switch (typeof (val)) {
	  case "string":
		return Util.trim(val).length == 0 ? true : false;
		break;
	  case "number":
		return val == 0;
		break;
	  case "object":
		return val == null;
		break;
	  case "array":
		return val.length == 0;
		break;
	  default:
		return true;
	}
};
/**
 * 检测字符串中是否包含中文，如果包含返回true，否则false
 * @param str 字符串
 */
var specialCharacter = new Array("≤","≥","ºC","℃","°");
Util.isChinese = function (str){
	for(var i=0;i<specialCharacter.length;i++){
		reg = new RegExp(specialCharacter[i],"g");
		str = str.replace(reg,"");
	}
	var re = /^[\x00-\x7F]*$/; 
	if (!re.test(str)) {
		return true;
	} else {
		return false;
	}
};
 
Util.isNumber = function (val) {
	if (val == null) {
		return false;
	}
	reg = /^[\d|\.|,]+$/;
	return reg.test(val);
};
Util.isFloat = function (val) {
	if (val == null) {
		return false;
	}
	reg = /^[\d\.]+$/;
	return reg.test(val);
};
Util.isInt = function (val) {
	if (val == null) {
		return false;
	}
	reg = /\D+/;
	return !reg.test(val);
};
Util.isEmail = function (email) {
	if (val == null) {
		return false;
	}
	reg1 = /([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)/;
	return reg1.test(email);
};
Util.isTel = function (tel) {
	if (val == null) {
		return false;
	}
	reg = /^[\d|\-|\s|\_]+$/; //只允许使用数字-空格等
	return reg.test(tel);
};
Util.isDateTime = function (val) {
	if (val == null) {
		return false;
	}
	reg = /^\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}$/;
	return reg.test(val);
};
Util.isDate = function (val) {
	if (val == null) {
		return false;
	}
	reg = /^\d{4}-\d{2}-\d{2}$/;
	return reg.test(val);
};
Util.isUrl = function (val) {
	if (val == null) {
		return false;
	}
	reg = /^https?:\/\/.+$/i;
	return reg.test(val);
};
Util.fixEvent = function (e) {
	evt = (typeof e == "undefined") ? window.event : e;
	return evt;
};
Util.getQueryParams = function () {
	var startIndex = location.href.lastIndexOf("?");
	var endIndex = location.href.length;
	var params = {};
	if (startIndex != -1) {
		var tmps = location.href.substring(startIndex + 1, endIndex).split("&");
		$.each(tmps, function (i, n) {
			v = n.split("=");
			params[v[0]] = v[1];
		});
	}
	return params;
};
Util.srcElement = function (e) {
	if (typeof e == "undefined") {
		e = window.event;
	}
	src = document.all ? e.srcElement : e.target;
	return src;
};
/**
 * 设置Cookie
 * @param name 
 * @param value 
 * @param minutes 有效时间(分钟)，默认当前浏览器会话时间
 * @param path 站点路径，默认"/"
 */
Util.setCookie = function (name, value, minutes, path) {
	var cookie = new Array();
	cookie.push(name + "=" + encodeURIComponent(value));
	if (minutes & Util.isInt(minutes)) {
		var expires = new Date((new Date()).getTime() + minutes * 60000);
		cookie.push("expires=" + expires.toGMTString());
	}
	if (path) {
		cookie.push("path=" + path);
	} else {
		cookie.push("path=/");
	}
	document.cookie = cookie.join("; ");
};
Util.getCookie = function (name) {
	cookies = document.cookie.split("; ");
	for (i = 0; i < cookies.length; i++) {
		cookie = cookies[i].split("=");
		if (name == cookie[0]) {
			return decodeURIComponent(cookie[1]);
		}
	}
	return "";
};
Util.removeCookie = function (name) {
	document.cookie = name + "=; expires=Fri, 31 Dec 1999 23:59:59 GMT;";
};
/**
 * 调节图片的长度和宽度做限制,自定义高宽
 * 长度和宽度哪个超出就限制哪个，如果都不超出则原尺寸
 * 例：<img src="image.jpg" onload="setImgWH(this,imgW,imgH) >
 * @param img img object
 * @param imgW 限制宽度
 * @param imgH 限制高度
 */
Util.setImgWH = function (img, imgW, imgH) {
	var imgObj = new Image();
	imgObj.src = img.src;
	if (imgObj.width != 0 && imgObj.height != 0) {
		if (imgObj.width >= imgW || imgObj.height >= imgH) {
			var iHeight = imgObj.height * imgW / imgObj.width;
			if (iHeight <= imgH) {
				img.width = imgW;
				img.height = iHeight;
			} else {
				var iWidth = imgObj.width * imgH / imgObj.height;
				img.width = iWidth;
				img.height = imgH;
			}
		} else {
		}
	} else {
		img.width = imgW;
		img.height = imgH;
	}
};
/**
 * checkbox 全选和取消全选
 * @param toggleObj 开关对象
 * @param checkboxName 
 *
 * 09-12-18 鲍智敏: 不对含有disabled属性的复选框进行操作
*/
Util.checkAll = function (toggleObj, checkboxName) {
	if ($(toggleObj).attr("checked")) {
		$(":checkbox[name='" + checkboxName + "']:not(:disabled)").attr("checked", "checked");
	} else {
		$(":checkbox[name='" + checkboxName + "']:not(:disabled)").removeAttr("checked");
	}
};
/**
 * 检测是否至少选中了一个复选框
 * @param name
 */
Util.isChecked = function (name) {
	if($("[name='" + name + "']:checked").size() >0){
		return true;
	}else {
		return false;
	}
};

/**
 * 加载验证码
 * @param form 
 * @param imgId 
 * @param url 
 */
Util.loadValidateCode = function (form, imgId, url) {
	if (form && form.validateCodeKey && document.getElementById(imgId)) {
		var date = new Date();
		var key = date.getTime() + Math.random();
		form.validateCodeKey.value = key;
		document.getElementById(imgId).src = url + "?hi_vc_key=" + key;
	}
};

