// JavaScript Document

/**
 * @file util.js
 * @brief 辅助函数库
 */

/**
 * 检测是否是合法的 IPv4 格式的 IP 地址。
 * @param ip 指定待检测的地址字符串
 * @return 如果输入的地址合法返回经过整理的 IP 地址，否则返回 false 。
 * @note 应用时，应当使用该函数返回的字符串作为 IP 地址进行操作。
 */
function $checkIPv4(ip)
{
	if (ip == null || ip == "")
		return false;
	
	if (typeof(ip) != "string")
		return false;
	
	// 先判断是否是整数
	var re = /(\d+)\.(\d+)\.(\d+)\.(\d+)/g
	if (!re.test(ip))
	{
		return false;
	}

	var exp = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/;
	var reg = ip.match(exp);

	if (reg == null)
	{
		return false;
	}
	else
	{
		var output = parseInt(RegExp.$1) + "." + parseInt(RegExp.$2) + "." + parseInt(RegExp.$3) + "." + parseInt(RegExp.$4);
		return output;
	}
};


/**
 * 如果输入是空串或者空格串返回 true
 */
function $checkNull(str)
{
	str = str.trim();
	if(str.length == 0)
		return true;
	
	return false;
};


/**
 * 判断字符全部由a-Z或者是A-Z的字字母组成
 */
function $checkLetter(str)
{
	if (!$checkStringType(str))
		return false;

	var reg = /^[a-zA-Z]+$/g;
	if (reg.test(str))
		return true;

	return false;
};


/**
 * 判断字符由字母和数字组成
 */
function $checkLetterAndNum(str)
{
	if (!$checkStringType(str))
		return false;

	var reg = /^[0-9a-zA-Z]+$/g;
	if (reg.test(str))
		return true;
	
	return false;
};


/**
 * 判断字符串是否是整数型
 */
function $checkInteger(str)
{
	if (str.length != 0)
	{
		reg=/^[-+]?\d*$/;
		if (reg.test(str))
		{
			return true;
		}
	}

	return false;
}

/**
 * 判断是否身份证号码，即位数15或18
 */
/*
var vcity={ 11:"北京",12:"天津",13:"河北",14:"山西",15:"内蒙古",
           21:"辽宁",22:"吉林",23:"黑龙江",31:"上海",32:"江苏",
           33:"浙江",34:"安徽",35:"福建",36:"江西",37:"山东",41:"河南",
           42:"湖北",43:"湖南",44:"广东",45:"广西",46:"海南",50:"重庆",
           51:"四川",52:"贵州",53:"云南",54:"西藏",61:"陕西",62:"甘肃",
           63:"青海",64:"宁夏",65:"新疆",71:"台湾",81:"香港",82:"澳门",91:"国外"}
*/
function $checkIDCard(str)
{
	if (str.length != 0)
	{
		reg=/(^\d{15}$)|(\d{17}(?:\d|x|X)$)/;
		if (reg.test(str))
		{
			return true;
		}
	}

	return false;
	/*
	var isum=0;
	var re=/^\d{17}(\d|X)$/i;
	if(str!=null && str.length>0){
		if(!re.test(str)){
	        alert("卡号不符合要求!");
			return false;
		}

		//检查地址是否符合要求
		if(vcity[parseInt(str.substr(0,2))]==null){
			alert("身份地址不符合要求!");
			return false;
		}
		
		//检查出生日期是否合法
		sbirthday=str.substr(6,4)+"-"+Number(str.substr(10,2))+"-" + Number(str.substr(12,2));
		var date=new Date(sbirthday.replace(/-/g,"/"));
		if(sbirthday!=(date.getFullYear()+"-"+date.getMonth()+"-"+date.getDate())){
			//alert("出生日期非法!"+date.getFullYear()+"-"+date.getMonth()+"-"+date.getDate() +str.substr(10,2));
			alert("出生日期非法!"+date.getFullYear()+"-"+date.getMonth()+"-"+date.getDate());
		    return false;
		}
		
		//检查验证码是否合法
		for(var i=17;i>=0;i--){
		    isum+=(Math.pow(2,i)%11)*parseInt(str.charAt(17-i),11);
		}
		if(isum%11!=1){
		    alert("验证码非法!");
		    return false;
		}
		
		//alert('身份证地址:'+vcity[parseInt(str.substr(0,2))]+"\n"
		// +"出生日期:"+sbirthday+"\n"
		// +"性别:"+(str.substr(16,1)%2?"男":"女"));
	}

	return false;
	*/
}

/**
 * 判断是否电话号码，即只含有数字和中杠
 */
function $checkTelphone(str)
{
	if (str.length != 0)
	{
		reg=/^[0-9-]+$/g;
		if (reg.test(str))
		{
			return true;
		}
	}

	return false;
}

/**
 * 判断是否字符串，即只含有数字，字母和下划线
 */
function $checkString(str)
{
	if (str.length != 0)
	{
		reg = /^[a-zA-Z0-9_]+$/;
		if (reg.test(str))
			return true;
	}

	return false;
}


/**
 *  判断字符由字母和数字，下划线，点号组成；且开头的只能是下划线和字母
 */
function $checkVarNaming(str)
{
	return (/^([a-zA-z_]{1})([\w]*)$/g.test(str));
};


/**
 * 判断是是否是 YYYY-MM-DD 格式的日期字符串
 */
function $checkDate(str)
{
	if (str.length != 0)
	{
		var reg = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/;
		var r = str.match(reg);
		if (r == null)
			return false;
		else
			return true;
	}    
};


// 判断输入的EMAIL格式是否正确    
function $checkEmail(str)
{
	if (str.length != 0)
	{
		reg = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
		if (reg.test(str))
			return true;
	}

	return false;
} 


/**
 * 判断输入内容是否有中文
 */
function $checkChinese(str)
{
	if (str == null)
		return false;

	if (str.length != 0)
	{
		reg = /^[\u0391-\uFFE5]+$/;
		if (reg.test(str))
			return true;
	}

	return false;
};




/**
 * 判断输入参数是否是字符串类型的变量
 */
function $checkStringType(input)
{
	if (input == null)
		return false;

	if (typeof(input) == "string")
		return true;
	else
		return false;
};


/**
 * 判断输入参数是否是数字类型的变量，包括整数类型和浮点类型
 */
function $checkNumberType(input)
{
	if (input == null)
		return false;

	if (typeof(input) == "number")
		return true;
	else
		return false;
};

String.prototype.trim = function()
{
	return this.replace(/(^\s*)|(\s*$)/g, "");
};

function disableWindow()
{
	var windowWidth = 0;
	var windowHeight = 0;
	if (document.documentElement && document.documentElement.clientHeight)
	{
		// Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
    }
	else if (document.body)
	{
		// other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
    }

	var layer = document.createElement("div");
	layer.style.position = "absolute";
	layer.style.left = "0px";
	layer.style.top = "0px";
	layer.style.width = windowWidth + "px";
	layer.style.height = windowHeight + "px";
	layer.style.zIndex = 9999;
	layer.style.background = "black";
	layer.style.filter = "alpha(opacity=50)";

	document.body.appendChild(layer);
}


function isIE7()
{
	var ua = window.navigator.userAgent.toLowerCase();
	var ie = (ua.indexOf("msie 7") > -1);
	return ie;
}

function getScroll()
{
	var t, l, w, h;
	if (document.documentElement && document.documentElement.scrollTop)
	{
		t = document.documentElement.scrollTop;
		l = document.documentElement.scrollLeft;
		w = document.documentElement.scrollWidth;
		h = document.documentElement.scrollHeight;
	}
	else if (document.body)
	{
		t = document.body.scrollTop;
		l = document.body.scrollLeft;
		w = document.body.scrollWidth;
		h = document.body.scrollHeight;
	}
	
	return { t: t, l: l, w: w, h: h };
}

function delCode(obj)
{
	try
	{
		obj = obj.replace(new RegExp('x&;', 'gm'),'<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;');
		obj = obj.replace(new RegExp('\\+', 'gm'),'&nbsp;');
		
	}
	catch (ex)
	{
		alert("去除非法字符失败！");
		obj = obj;
	}
	return obj;
}

function forCode(obj)
{
	try
	{
		obj = encodeURI(obj);
		obj = obj.replace(new RegExp('%0D%0A', 'gm'),'x%26%3B');
		obj = obj.replace(new RegExp('%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20', 'gm'),'');
		obj = returnCode(obj);
		obj = delCode(obj);
	}
	catch (ex)
	{
		alert("编码失败！");
		obj = obj;
	}
	return obj;
}

function returnCode(obj)
{
	try
	{
		obj = decodeURIComponent(obj);
	}
	catch (ex)
	{
		alert("解码失败！");
		obj = obj;
	}
	return obj;
}

function replaceCode(obj)
{
	try
	{
		obj = obj.replace(new RegExp('<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;', 'gm'),'\n');
		if(obj.indexOf("&") >= 0)
		{
			obj = obj.replace(new RegExp('&', 'gm'),'&amp;');
		}
		if(obj.indexOf("<") >=0)
		{
			obj = obj.replace(new RegExp('<', 'gm'),'&lt;');
		}
		if(obj.indexOf(">") >=0)
		{
			obj = obj.replace(new RegExp('>', 'gm'),'&gt;');
		}
		if(obj.indexOf("\''") >=0)
		{
			obj = obj.replace(new RegExp("\''", 'gm'),'&quot;');
		}
		
	}
	catch (ex)
	{
		alert("去除非法字符失败！");
		obj = obj;
	}
	return obj;
}

function delCodeForText(obj)
{
	try
	{
		obj = obj.replace(new RegExp('\\+', 'gm'),' ');
		if(obj.indexOf("&amp;") >= 0)
		{
			obj = obj.replace(new RegExp('&amp;', 'gm'),'&');
		}
		if(obj.indexOf("&lt;") >=0)
		{
			obj = obj.replace(new RegExp('&lt;', 'gm'),'<');
		}
		if(obj.indexOf("&gt;") >=0)
		{
			obj = obj.replace(new RegExp('&gt;', 'gm'),'>');
		}
		if(obj.indexOf("&quot;") >=0)
		{
			obj = obj.replace(new RegExp('&quot;', 'gm'),"\''");
		}
	}
	catch (ex)
	{
		alert("去除非法字符失败！");
		obj = obj;
	}
	return obj;
}