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;";
};
/**
 * 2010-1-14 鲍智敏：增加判断图片加载完成后，来伸缩图片
 *
 * 调节图片的长度和宽度做限制,自定义高宽
 * 长度和宽度哪个超出就限制哪个，如果都不超出则原尺寸
 * 例：<img src="image.jpg" onload="Util.setImgWH(this,100,100) >
 * @param img img object
 * @param imgW 限制宽度
 * @param imgH 限制高度
 */
Util.setImgWH = function (img, imgW, imgH) {
	var imgObj = new Image();

    imgObj.onload = function (){
		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;
		}
	}
    imgObj.src = img.src;
};
/**
 * 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;
	}
};

/**
 * 2010-1-11 鲍智敏：添加参数判断保存地址
 */
Util.addBookMark = function(){
	var title="中国海商网";
	var url="http://cn.hisupplier.com";

	// 带参数的话，使用参数值作为地址
	if(arguments[0]){
		url = arguments[0];
	}
	
	if (window.sidebar) {
		window.sidebar.addPanel(title, url, "");
	} else {
		if (window.external) {
			window.external.AddFavorite(url, title);
		} else {
			return true;
		}
	}
}

Util.setHome = function(obj){
	var url="http://cn.hisupplier.com";
	
	try{
		obj.style.behavior='url(#default#homepage)';obj.setHomePage(url);
	}catch(e){
		if(window.netscape) {
			 try {
			 	netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
			 }catch (e){
			 	alert("此操作被浏览器拒绝！\n请在浏览器地址栏输入'about:config' 并回车\n然后将 [signed.applets.codebase_principal_support]设置为'true'");
			 }
			 
			 var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);
	         prefs.setCharPref('browser.startup.homepage', url);
	         alert("设置成功");
		}
	}
}


/**
 * 按比例将图片放大或缩小,该方法需要Browser支持onmousewheel(鼠标滚轮)事件
 * 例：<img src="image.jpg" onmousewheel="return imgZoom(this,20,150)" >
 * @param img 图片 object
 */
Util.imgZoom = function (img) {
	var zoom = parseInt(img.style.zoom, 10) || 100;
	zoom += event.wheelDelta / 12;
	if (zoom > 0) {
		img.style.zoom = zoom + "%";
	}
	return false;
}
