var Aginfo=navigator.userAgent.toLowerCase();
var timer;
var errorAction;
_getObj=function(id){return document.getElementById(id)?document.getElementById(id):false;}
String.prototype.trim = function(){return this.replace(/(^\s*)|(\s*$)/g, "");}
msgBoxs=new showmessage();
function showmessage()
{
	var	input,type,message;
	this.isIE=(Aginfo.indexOf("msie")!=-1&&document.all)?1:0;
	this.Height		= 80;
	this.filter		= 100;
	this.position=findPosition;
	this.checkBox=checkBox;
	this.getCharlen=getCharlen;
	this.makeBox=makeBox;
	this.showBox=showBox;
	//this.trim=function(c){return c.replace(/(^\s*)|(\s*$)/g, "");}
	//this.hidden=hidden;
}
function checkBox()
{
	this.checkBoxs=_getObj(this.divName)?_getObj(this.divName):false;
	this.getCharlen();
}
function getCharlen()
{
	var i;
	this.clength=0;
	for (i=0;i<this.msg.length;i++) 
		this.clength+=(this.msg.charCodeAt(i)>255)?3:1;  
	this.charlen=this.clength*4+20;
}
function findPosition( )
{
	this.obj = this.input.parentNode;
	this.Width		= this.obj.offsetWidth;
	this.Left		= this.obj.offsetLeft;
	this.Top		= this.obj.offsetTop;
	this.Color		= this.type?"#cccccc":"#fff";
	this.cssName=this.type?"sysmsg_":"error_";
	this.divName=this.type?"showMessage":"showError";
	this.msgType=this.type?"小提示:":"<font color=red>出错啦!</font>";
	//this.input.style.border="1px solid";
	this.input.style.backgroundColor=this.type==1?"":"#FFD9D9";
	var x2 = 0;
	var y2 = 0;
	var width = this.input.offsetWidth;
	var height = this.input.offsetHeight;
	if( typeof( this.input.offsetParent ) != 'undefined' )
	{
		for( var posX = 0, posY = 0; this.input; this.input = this.input.offsetParent )
		{
			posX += this.input.offsetLeft;
			posY += this.input.offsetTop;     
		}
		this.Left = posX + width;
		this.Top = posY + height;
    }
    else{
		this.Left = this.input.x + width;
		this.Top = this.input.y + height;
	}
}
function showBox()
{
	if (!this.checkBoxs) this.makeBox();
	else this.msgBox=this.checkBoxs;
	this.msgBox.style.width=this.charlen+40+"px";
	this.msgBox.style.top=this.Top-(this.Height+24)+"px";
	this.msgBox.style.left=(this.Left-56)+"px";
	this.msgBox.style.visibility="visible";
	
}
function makeBox()
{
	this.msgBox = document.createElement('div');
		this.msgBox.className=this.cssName+"mainbox";
		this.msgBox.id  = this.divName;
		this.msgBox.style.zIndex="999";
		this.isIE?this.msgBox.style.filter="Alpha(Opacity="+this.filter+")":this.msgBox.style.opacity=this.filter/100;
	this.obj.appendChild(this.msgBox);
	var title=document.createElement('div');
		title.className=this.cssName+"left";
	this.msgBox.appendChild(title);
	var titles=document.createElement('div');
		titles.className="centext";
	this.msgBox.appendChild(titles);
		var title=document.createElement('div');
			title.className=this.cssName+"tops";
			title.style.width=this.charlen+"px";
			title.innerHTML=this.msgType;
			title.style.lineHeight="19px";
		titles.appendChild(title);
		this.context=document.createElement('div');
			this.context.className=this.cssName+"context";
			this.context.innerHTML=this.msg;
			this.context.style.width=this.charlen+"px";
			this.context.style.lineHeight="20px";
		titles.appendChild(this.context);
		var title=document.createElement('div');
			title.className=this.cssName+"point";
		titles.appendChild(title);
		var title=document.createElement('div');
			title.className=this.cssName+"line";
			title.style.width=this.charlen-20+"px";
		titles.appendChild(title);
	var title=document.createElement('div');
		title.className=this.cssName+"right";
		title.style.zIndex="999";
		title.title="点击关闭";
	this.msgBox.appendChild(title);
	if(window.addEventListener){
        title.addEventListener('click', hiddenMsg, false);
        title.addEventListener('click', hiddenMsg, false);
    } else {
        title.attachEvent('onclick',  function(){hiddenMsg();});
        title.attachEvent('onclick',  function(){hiddenMsg();});
    }

}

function showMsg( Object , type , message )
{
	clearTimeout(timer);
	var Obj=getId(Object);
	if (errorAction==true&&type==1)	return false;
	if (typeof Obj=="object" && typeof msgBoxs=="object" )
	{
		hiddenMsg();
		Obj.value=Obj.value.trim();
		if (Obj.value.length>=3) {return;}
		if (Obj.value.length==0) {type=1;}
		msgBoxs=new showmessage();
		msgBoxs.filter=85;
		msgBoxs.input=Obj;
		msgBoxs.type=type;
		msgBoxs.msg=message;
		msgBoxs.checkBox();
		msgBoxs.position();
		msgBoxs.showBox();
		if (type==1) timer=setTimeout("hiddenss()",3000);
	}
	else
	{
		alert("元素"+Object+"没有找到!");
		return false;
	}
}
function hiddenss()
{
	note=msgBoxs.msgBox;
	if (msgBoxs.filter>=5)
	{
		clearTimeout(timer);
		msgBoxs.filter-=20;
		msgBoxs.isIE?note.style.filter="Alpha(Opacity="+msgBoxs.filter+")":note.style.opacity=msgBoxs.filter/100;
		timer=setTimeout("hiddenss()",100);
	}
	else
	{
		hiddenMsg();
		clearTimeout(timer);
	}
}
function hiddenMsg()
{
	var note;
	note=_getObj("showMessage")?_getObj("showMessage"):_getObj("showError");
	if (typeof note=="object"){
		obj=note.parentNode;
		if(typeof obj=="object"){
			obj.removeChild(note);
			return true;
		}
	}
	return false;
}
