/*****************************************************************
* 功能：搜索提示字符串
* 参数：
	 elementId:要提示的元素id
	 text:提示文字
******************************************************************/
function initInputText(elementId, text) {
	var obj = $("#" + elementId);
	if($.trim(obj.val()) == "") {
		obj.val(text);
	}
	obj.focus(function() {
		if($.trim(obj.val()) == text) {
			$(this).val("");
		}
	});	
	obj.blur(function() {
		if($.trim(obj.val()) == "") {
			$(this).val(text);
		}
	});
}
/*****************************************************************
* 功能：选择所有checkbox
* 参数：
	  elementId:触发全选事件的元素
	  obj:包含checkbox的容器
******************************************************************/
function checkAll(elementId, obj) {
	$("#" + elementId).click(function() {
		if($(this).attr("checked")) {
			$(obj + " input[type=checkbox]").attr("checked", "true");
		} else {
			$(obj + " input[type=checkbox]").removeAttr("checked");
		}
	});
}

/*****************************************************************
* 功能：测试元素的值是否为空
* 参数：
	  elementId:触发全选事件的元素
	  msg:提示信息
******************************************************************/
function isEmpty(elementId, msg) {
	if($.trim($("#" + elementId).val()) == "") {
		$("#" + elementId).focus();
		alert(msg);
		return true;
	} else {
		return false;
	}
}

/*****************************************************************
* 功能：测试元素的值是否为0
* 参数：
	  elementId:触发全选事件的元素
	  msg:提示信息
******************************************************************/
function isZero(elementId, msg) {
	if($.trim($("#" + elementId).val()) == 0) {
		$("#" + elementId).focus();
		alert(msg);
		return true;
	} else {
		return false;
	}
}

/*****************************************************************
* 功能：判断非负数
******************************************************************/
function isNumber1(val) {
	if (/(^0(.\d+)?$)|(^[1-9](\d+)?(\.\d+)?$)/.test(val)) {
		return true;
	} else {
		return false;
	}
}

/*****************************************************************
* 功能：@ww格式化数字
* 参数：
		val: 要格式的数字
		numDigits:保留小数位数
******************************************************************/
function formatNumber(val, numDigits) {
	if (typeof(numDigits) != "number") {
		numDigits = 2;
	}
	var num = Math.round(val * Math.pow(10, numDigits + 1) + 1) / Math.pow(10, numDigits + 1);
	return num.toString().substr(0, num.toString().length - 1);
}

/*****************************************************************
* 功能：获取文件的扩展名
* 参数：
	  path:文件路径
******************************************************************/
function getFileExtension(path) {
	return path.substring(path.lastIndexOf("."), path.length).toLowerCase();
}

/*****************************************************************
* 功能：缩放图片
******************************************************************/
jQuery.fn.resizeImg = function(options) {
	options = $.extend({
		src: $(this).attr("src"),
		width: 120,
		height: 90
	}, options);
	var _self = this;
	var img = new Image();
	$(img).attr("src", options.src);
	
	if (img.width > options.width || img.height > options.height) {
		if (img.width / options.width > img.height / options.height) {
			$(this).width(options.width);
			$(this).height(img.height / (img.width / options.width));
		} else {
			$(this).height(options.height);
			$(this).width(img.width / (img.height / options.height));
		}
	} else {
		$(this).width(img.width);
		$(this).height(img.height);
	}
};