var isDebug = true;
//document.domain = "yaoguai.com";
if(typeof $ != 'function')
{
	$import('prototype.js','js');
}
function $import(path,type,title){
	var s,i;
	if(type=="js"){
		var ss=document.getElementsByTagName("script");
		for(i=0;i<ss.length;i++){
			if(ss[i].src && ss[i].src.indexOf(path)!=-1)return ss[i];
		}
		s=document.createElement("script");
		s.type="text/javascript";
		s.src=path;
	}else if(type=="css"){
		var ls=document.getElementsByTagName("link");
		for(i=0;i<ls.length;i++){
			if(ls[i].href && ls[i].href.indexOf(path)!=-1)return ls[i];
		}
		s=document.createElement("link");
		s.rel="alternate stylesheet";
		s.type="text/css";
		s.href=path;
		s.title=title;
		s.disabled=false;
	}
	else return;
	var head=document.getElementsByTagName("head")[0];
	head.appendChild(s);
	return s;
}
function bodySize()
{
	var h1,h2,w1,w2;
	h1 = document.body ? document.body.clientHeight : 0;
	h2 = document.documentElement ? document.documentElement.clientHeight : 0;
	w1 = document.body ? document.body.clientWidth : 0;
	w2 = document.documentElement ? document.documentElement.clientWidth : 0;
	return {"W":Math.max(w1,w2),"H":Math.max(h1,h2)};
}
function initIframe(iframe,w,h)
{
	try{
		var h1,h2,w1,w2;
		var d = iframe.document ? iframe.document : iframe.contentDocument;
		h1 = d.body ? d.body.clientHeight : 0;
		h2 = d.documentElement ? d.documentElement.clientHeight : 0;
		w1 = d.body ? d.body.clientWidth : 0;
		w2 = d.documentElement ? d.documentElement.clientWidth : 0;
		iframe.width = w ? w : Math.max(w1,w2);
		iframe.height = h ? h : Math.max(h1,h2)+30;
		iframe.style.width = iframe.width + 'px';
		iframe.style.height = iframe.height+ 'px';
	}catch(e)
	{
		iframe.width = w ;
		iframe.height = h ;
		iframe.style.width = iframe.width + 'px';
		iframe.style.height = iframe.height+ 'px';
	}
}
function getIframe(src)
{
	try{
		var newIframe = document.createElement('iframe');
		Element.extend(newIframe);
		newIframe.id = 'loginIframe';
		newIframe.src = src;
		newIframe.width = 0;
		newIframe.height = 0;
		newIframe.style.position = "absolute";
		newIframe.frameBorder = '0';
		newIframe.scrolling   = 'no';
		newIframe.style.top = '10%';
		newIframe.style.left = '30%';
		newIframe.style.width = '0px';
		newIframe.style.height = '0px';
		newIframe.style.zIndex = "100";
		return newIframe;
	}catch(e)
	{
		if(isDebug)
		{
			alert(e);
		}
	}
}
function getMask()
{
	try{
		var newMask = document.createElement('div');
		var o = bodySize();
		Element.extend(newMask);
		newMask.id = 'loginMask';
		newMask.style.position = "absolute";
		newMask.style.zIndex = "10";
		newMask.style.top = "0px";
		newMask.style.left = "0px";
		newMask.style.width = o.W + 'px';
		newMask.style.height = o.H + 30 + 'px';
		newMask.style.background = "silver";
		newMask.style.filter = "alpha(opacity=40)";
		newMask.style.opacity = "0.40";
		return newMask;
	}catch(e)
	{
		if(isDebug)
		{
			alert(e);
		}
	}
}
function newLogin(loginurl,tourl)
{
	
	var iframe =  getIframe(loginurl+'&from_url='+encodeURIComponent(tourl),500);
	document.body.appendChild(iframe);
	iframe.onreadystatechange = iframe.onload = function()
	{
		var o = this;
		window.setTimeout(function(){
			initIframe(o,400,360);
		},500);
		window.setTimeout(function(){
			var mask = $('loginMask') ? $('loginMask'):getMask();
			if($('loginMask'))
			{
				var mask = $('loginMask');
			}
			else
			{
				var mask = getMask();
				document.body.appendChild(mask);
			}
			mask.ondblclick = function()
			{
				this.remove();
				iframe.remove();
			}
		},500);

	}
}

/*
===========================================
//是否是正确的IP地址
===========================================
*/
String.prototype.isIP = function()
{

	var reSpaceCheck = /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/;

	if (reSpaceCheck.test(this))
	{
		this.match(reSpaceCheck);
		if (RegExp.$1 <= 255 && RegExp.$1 >= 0
		&& RegExp.$2 <= 255 && RegExp.$2 >= 0
		&& RegExp.$3 <= 255 && RegExp.$3 >= 0
		&& RegExp.$4 <= 255 && RegExp.$4 >= 0)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	else
	{
		return false;
	}

}
/*
===========================================
//是否是正确的长日期
===========================================
*/
String.prototype.isDate = function()
{
	var r = str.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/);
	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]);

}
/*
===========================================
//是否是手机
===========================================
*/
String.prototype.isMobile = function()
{
	return /^0{0,1}13[0-9]{9}$/.test(this);
}
/*
===========================================
//是否是邮件
===========================================
*/
String.prototype.isEmail = function()
{
	return /^[a-zA-Z0-9\-]+@[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3})$/.test(this);
}

/*
===========================================
//是否是邮编(中国)
===========================================
*/
String.prototype.isZipCode = function()
{
	return /^[\\d]{6}$/.test(this);
}
/*
===========================================
//是否是有汉字
===========================================
*/
String.prototype.existChinese = function()
{
	//[\u4E00-\u9FA5]為漢字﹐[\uFE30-\uFFA0]為全角符號
	return /^[^\x00-\xFF]*$/.test(this);
}
/*
===========================================
//返回字符半角长度
===========================================
*/
String.prototype.charLength = function()
{
	var len = 0;
	for(var i=0;i<this.length;i++)
	{
		if(this.charAt(i).existChinese())
		len +=2;
		else
		len +=1;
	}
	return len;
}

/*
===========================================
//是否是合法的文件名/目录名
===========================================
*/
String.prototype.isFileName = function()
{
	return !/[\\\/\*\?\|:"<>]/g.test(this);
}

/*
===========================================
//是否是有效链接
===========================================
*/
String.prototype.isUrl = function()
{
	return /^http:\/\/([\w-]+\.)+[\w-]+(\/[\w-.\/?%&=]*)?$/.test(this);
}

/*
===========================================
//是否是有效的身份证(中国)
===========================================
*/
String.prototype.isIDCard = function()
{
	var Errors=new Array(true,false,false,false,false);
	var area={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:"国外"}
	var idcard = this;
	var Y,JYM;
	var S,M;
	var idcard_array = new Array();
	idcard_array = idcard.split("");
	if(area[parseInt(idcard.substr(0,2))]==null) return Errors[4];
	switch(idcard.length){
		case 15:
		if ((parseInt(idcard.substr(6,2))+1900) % 4 == 0 || ((parseInt(idcard.substr(6,2))+1900) % 100 == 0 && (parseInt(idcard.substr(6,2))+1900) % 4 == 0 )){
			ereg = /^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}$/;//测试出生日期的合法性
		}
		else{
			ereg = /^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}$/;//测试出生日期的合法性
		}
		if(ereg.test(idcard))
		return Errors[0];
		else
		return Errors[2];
		break;
		case 18:
		if ( parseInt(idcard.substr(6,4)) % 4 == 0 || (parseInt(idcard.substr(6,4)) % 100 == 0 && parseInt(idcard.substr(6,4))%4 == 0 )){
			ereg = /^[1-9][0-9]{5}19[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}[0-9Xx]$/;//闰年出生日期的合法性正则表达式
		}
		else{
			ereg = /^[1-9][0-9]{5}19[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}[0-9Xx]$/;//平年出生日期的合法性正则表达式
		}
		if(ereg.test(idcard)){
			S = (parseInt(idcard_array[0]) + parseInt(idcard_array[10])) * 7 + (parseInt(idcard_array[1]) + parseInt(idcard_array[11])) * 9 + (parseInt(idcard_array[2]) + parseInt(idcard_array[12])) * 10 + (parseInt(idcard_array[3]) + parseInt(idcard_array[13])) * 5 + (parseInt(idcard_array[4]) + parseInt(idcard_array[14])) * 8 + (parseInt(idcard_array[5]) + parseInt(idcard_array[15])) * 4 + (parseInt(idcard_array[6]) + parseInt(idcard_array[16])) * 2 + parseInt(idcard_array[7]) * 1 + parseInt(idcard_array[8]) * 6 + parseInt(idcard_array[9]) * 3 ;
			Y = S % 11;
			M = "F";
			JYM = "10X98765432";
			M = JYM.substr(Y,1);
			if(M == idcard_array[17])
			return Errors[0];
			else
			return Errors[3];
		}
		else
		return Errors[2];
		break;
		default:
		return Errors[1];
		break;
	}

} 
/*
===========================================
//是否是有效的电话号码(中国)
===========================================
*/
String.prototype.isPhoneCall = function()
{
	return /(^[0-9]{3,4}\-[0-9]{3,8}$)|(^[0-9]{3,8}$)|(^\([0-9]{3,4}\)[0-9]{3,8}$)|(^0{0,1}13[0-9]{9}$)/.test(this);
}


/*
===========================================
//是否是数字
===========================================
*/
String.prototype.isNumeric = function(flag)
{
	//验证是否是数字
	if(isNaN(this))
	{

		return false;
	}

	switch(flag)
	{

		case null://数字
		case "":
		return true;
		case "+"://正数
		return/(^\+?|^\d?)\d*\.?\d+$/.test(this);
		case "-"://负数
		return/^-\d*\.?\d+$/.test(this);
		case "i"://整数
		return/(^-?|^\+?|\d)\d+$/.test(this);
		case "+i"://正整数
		return/(^\d+$)|(^\+?\d+$)/.test(this);
		case "-i"://负整数
		return/^[-]\d+$/.test(this);
		case "f"://浮点数
		return/(^-?|^\+?|^\d?)\d*\.\d+$/.test(this);
		case "+f"://正浮点数
		return/(^\+?|^\d?)\d*\.\d+$/.test(this);
		case "-f"://负浮点数
		return/^[-]\d*\.\d$/.test(this);
		default://缺省
		return true;
	}
}


/*
===========================================
//转换成全角
===========================================
*/
String.prototype.toCase = function()
{
	var tmp = "";
	for(var i=0;i<this.length;i++)
	{
		if(this.charCodeAt(i)>0&&this.charCodeAt(i)<255)
		{
			tmp += String.fromCharCode(this.charCodeAt(i)+65248);
		}
		else
		{
			tmp += String.fromCharCode(this.charCodeAt(i));
		}
	}
	return tmp
}

/*
===========================================
//对字符串进行Html编码
===========================================
*/
String.prototype.toHtmlEncode = function()
{
	var str = this;

	str=str.replace("&","&amp;");
	str=str.replace("<","&lt;");
	str=str.replace(">","&gt;");
	str=str.replace("'","&apos;");
	str=str.replace("\"","&quot;");

	return str;
}
/*
===========================================
//字符由大小写字母数字或者_组成
===========================================
*/
String.prototype.isAvialable = function()
{
	return (/[^0-9a-z]/g.test(this) || /^[^a-z]/g.test(this));
}
function Enter2Tab()
{
	var e = document.activeElement;
	if(e.tagName == "INPUT" &&
	(
	e.type == "text"     ||
	e.type == "password" ||
	e.type == "checkbox" ||
	e.type == "radio"
	)   ||
	e.tagName == "SELECT")

	{
		if(window.event.keyCode == 13)
		{
			window.event.keyCode = 9;
		}
	}
}
function checkBrowser()
{
	this.ver=navigator.appVersion
	this.dom=document.getElementById?1:0
	this.ie7=(this.ver.indexOf("MSIE 7")>-1 && this.dom)?1:0;
	this.ie6=(this.ver.indexOf("MSIE 6")>-1 && this.dom)?1:0;
	this.ie5=(this.ver.indexOf("MSIE 5")>-1 && this.dom)?1:0;
	this.ie4=(document.all && !this.dom)?1:0;
	this.ns5=(this.dom && parseInt(this.ver) >= 5) ?1:0;
	this.ns4=(document.layers && !this.dom)?1:0;
	this.mac=(this.ver.indexOf('Mac') > -1) ?1:0;
	this.ope=(navigator.userAgent.indexOf('Opera')>-1);
	this.ie=(this.ie7 || this.ie6 || this.ie5 || this.ie4)
	this.ns=(this.ns4 || this.ns5)
	this.bw=(this.ie7 || this.ie6 || this.ie5 || this.ie4 || this.ns5 || this.ns4 || this.mac || this.ope)
	this.nbw=(!this.bw)

	return this;
}
function getXY(obj)
{
	var oRect = {x:0,y:0};;
	while (obj)
	{
		oRect.x += obj.offsetLeft;
		oRect.y += obj.offsetTop;
		obj = obj.offsetParent;
	}
	return oRect;
}
function alertDIV(msg,obj)
{
	var browser = checkBrowser();
	var mydiv = $('alertdiv');
	if(mydiv != null)
	{
		document.body.removeChild(mydiv);
	}
	mydiv = document.createElement('DIV');
	mydiv.style.position = 'absolute';
	mydiv.style.width = document.body.clientWidth/4+'px';;
	mydiv.style.height = 'auto';
	mydiv.style.background = 'white';
	if(browser.ie)
	{
		mydiv.style.filter = 'alpha(opacity=90)';
		mydiv.style.opacity = 1;

	}
	else
	{
		mydiv.style.MozOpacity = 1;
	}
	mydiv.style.color = 'red';
	if("undefined" == typeof(obj))
	{
		mydiv.style.top = document.body.clientHeight/2+'px';
		mydiv.style.left = document.body.clientWidth/2+'px';

	}
	else
	{
		var x = obj.clientWidth+getXY(obj).x;
		mydiv.style.top = getXY(obj).y + 'px';
		mydiv.style.left = x + 'px';
	}
	//mydiv.onclick = function(){drag(this);};
	mydiv.id = 'alertdiv';
	var mybtn = document.createElement('input');
	mybtn.type = 'button';
	mybtn.value = "关闭";
	mybtn.onclick = function()
	{
		document.body.removeChild(mydiv);
	}
	mydiv.innerHTML = "<table width='100%' height='100%' border='0' align='center' cellpadding='0' cellspacing='1' bgcolor='#feb902'>"
	+"<tr>"
	+"<td align='center' valign='top' bgcolor='#f8feee'>"
	+" <div style='border-bottom:1px dashed #afdd1c; height:25px;'><div style=' width:15px; float:right;color:#afdd1c; border: 1px solid red; margin:3px;'><strong><a href='#' onclick=\"document.body.removeChild($('alertdiv'))\">×</a></strong></div>系统消息</div>"
	+"<div style='padding:5px;'>"+msg+"</div></td>"
	+"</tr>"
	+"</table>";
	document.body.appendChild(mydiv);
}
function hiddenDIV()
{
	$('alertdiv').parentNode.removeChild($('alertdiv'));
}
function drag(o){
	o.onmousedown=function(a){
		var d=document;if(!a)a=window.event;
		var x=a.layerX?a.layerX:a.offsetX,y=a.layerY?a.layerY:a.offsetY;
		if(o.setCapture)
		o.setCapture();
		else if(window.captureEvents)
		window.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);

		d.onmousemove=function(a){
			if(!a)a=window.event;
			if(!a.pageX)a.pageX=a.clientX;
			if(!a.pageY)a.pageY=a.clientY;
			var tx=a.pageX-x,ty=a.pageY-y;
			o.style.left=tx;
			o.style.top=ty;
		};

		d.onmouseup=function(){
			if(o.releaseCapture)
			o.releaseCapture();
			else if(window.captureEvents)
			window.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);
			d.onmousemove=null;
			d.onmouseup=null;
		};
	};
}
//CharMode函数
//测试某个字符是属于哪一类.
function CharMode(iN){
	if (iN>=48 && iN <=57) //数字
	return 1;
	if (iN>=65 && iN <=90) //大写字母
	return 2;
	if (iN>=97 && iN <=122) //小写
	return 4;
	else
	return 8; //特殊字符
}

//bitTotal函数
//计算出当前密码当中一共有多少种模式
function bitTotal(num){
	modes=0;
	for (i=0;i<4;i++){
		if (num & 1) modes++;
		num>>>=1;
	}
	return modes;
}

//checkStrong函数
//返回密码的强度级别

function checkStrong(sPW){
	if (sPW.length<=6)
	return 0; //密码太短
	Modes=0;
	for (i=0;i<sPW.length;i++){
		//测试每一个字符的类别并统计一共有多少种模式.
		Modes|=CharMode(sPW.charCodeAt(i));
	}

	return bitTotal(Modes);

}
function id2age(idcard)
{
	dd = parseInt(idcard.substr(12,2)) + 1;
	mm = parseInt(idcard.substr(10,2)) + 1;
	yy = idcard.substr(6,4);
	days = new Date();
	gdate = days.getDate();
	gmonth = days.getMonth();
	gyear = days.getYear();
	if (gyear < 2000) gyear += 1900;
	age = gyear - yy;
	if ((mm == (gmonth + 1)) && (dd <= parseInt(gdate))) {
		age = age;
	} else {
		if (mm <= (gmonth)) {
			age = age;
		} else {
			age = age - 1;
		}
	}
	if (age == 0)
	age = age;
	return age;
}

//检测是否为空
function checkempty(id, msg)
{
  if ($(id).value == '')
    $(id+"_error").innerHTML = msg;
  else
    $(id+"_error").innerHTML = '';
}
function checkempty2(id, id2, msg)
{
  if ($(id).value == '')
    $(id2+"_error").innerHTML = msg;
  else
    $(id2+"_error").innerHTML = '';
}

//判断是否为整数
function checkint(id, msg)
{
  var ret = /^[1-9]+[0-9]*]*$/;
  if (!ret.test($(id).value))
    $(id+"_error").innerHTML = msg;
  else
    $(id+"_error").innerHTML = '';
}

