﻿function isInt(num) {
	if (isNaN(parseInt(num))) {
		return false;
	}

	return parseInt(num) == num;
}

function isEmail(Expression) {
	if (Expression == null)
		return (false);

	var supported = 0;
	if (window.RegExp) {
		var tempStr = "a";
		var tempReg = new RegExp(tempStr);
		if (tempReg.test(tempStr)) supported = 1;
	}
	if (!supported) {
		return (Expression.indexOf(".") > 2) && (Expression.indexOf("@") > 0);
	}
	var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
	var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
	return (!r1.test(Expression) && r2.test(Expression));
}

function isPhoneNo(Expresstion) {
	if (Expresstion.charAt(0) != '1') {
		return false;
	}
	if (Expresstion.length != 11) {
		return false;
	}
	for (var i = 0; i < Expresstion.length; i++) {
		if (!isInt(Expresstion.substr(i, 1))) {
			return false;
		}
	}
	return true;
}

function isDate(Expression) {
	var reg = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/;
	var r = Expression.match(reg);
	if (r == null) {
		return false;
	}
	var d = new Date(r[1], r[3] - 1, r[4], r[5], r[6], r[7]);
	return (d.getFullYear() == r[1] && (d.getMonth() + 1) == r[3] && d.getDate() == r[4] && d.getHours() == r[5] && d.getMinutes() == r[6] && d.getSeconds() == r[7]);
}

function isChn(str) {
	var reg = /^[u4E00-u9FA5]+$/;

	if (!reg.test(str)) {
		return true;
	}
	return false;
}


function doAjaxAction(id) {
	var obj = document.getElementById(id);
	if (typeof (obj.fireEvent) == "object") {
		obj.fireEvent("onClick");
	}
	else {
		var evt = document.createEvent('HTMLEvents');
		evt.initEvent('click', true, true);
		obj.dispatchEvent(evt);
	}
}

function showPopDiv(id, parent_id, srcObj, content) {
	var obj = document.getElementById(id);
	var offsetLeft = getPos(srcObj, "Left");
	var offsetTop = getPos(srcObj, "Top") + 25;
	if (obj != null) {
		obj.style.pixelLeft = offsetLeft;
		obj.style.pixelTop = offsetTop;
		obj.style.display = "block";
	}
	else {
		var newChild = document.createElement("div");
		newChild.id = id;
		newChild.innerHTML = (content == "" ? "无修改记录" : content);
		newChild.style.display = "block";
		newChild.style.position = "absolute";
		newChild.style.top = offsetTop;
		newChild.style.left = offsetLeft;
		newChild.style.border = "1px solid #d45668";
		newChild.style.width = (content == "" ? 100 : 500);
		newChild.style.padding = 5;
		newChild.style.backgroundColor = "#ffffff";
		document.getElementById(parent_id).appendChild(newChild);
	}
}

function hidePopDiv(id) {
	var obj = document.getElementById(id);
	if (obj != null) {
		obj.style.display = "none";
	}
}

function getPos(el, ePro)				/// Get Absolute Position
{
	var ePos = 0;
	while (el != null) {

		if (el.style.position == "relative") {
			break;
		}
		ePos += el["offset" + ePro];
		el = el.offsetParent;
	}
	return ePos;
}

function checkID(idstr) {
	// 身份证验证 18 位数字
	// 1. 18位
	if (idstr.length != 18) {
		return false;
	}
	// 2. 确保前17位每一位都是数字
	for (i = 0; i < idstr.length - 1; i++) {
		// 如何判断一个字母是数字
		if (isNaN(parseInt(idstr.charAt(i)))) {
			return false;
		}
	}

	// 3. 确保最后一位是数字或者X
	var lastIDNum = idstr.charAt(17);
	if (isNaN(parseInt(idstr.charAt(i))) && lastIDNum.toLowerCase() != 'x') {
		return false;
	}

	return true;
}

function copyLink() {
	window.clipboardData.setData('text', document.URL);
	alert("复制成功，您可以粘贴（Ctrl+V）到QQ或MSN上推荐给好友。");
}

function drawImage(ImgD, ImgHeight, ImgWidth)
{
	var width = ImgD.width;
	var height = ImgD.height;

	if(ImgD.width > 0 && ImgD.height > 0)
	{	
		if (ImgD.width > ImgWidth)
		{
			ImgD.width = ImgWidth; 
    	ImgD.height = (height * ImgWidth) / width;
		}

		if (ImgD.height > ImgHeight)
		{
			ImgD.height = ImgHeight; 
    	ImgD.width = (width * ImgHeight) / height; 
		}
	}
}
