var EmailCollection=["163.com","126.com","139.com", "189.cn","yahoo.cn","yahoo.com","yahoo.com.cn","yeah.net","live.cn","live.com","qq.cn","qq.com","sohu.com","sina.com","vip.qq.com","gmail.com","hotmail.com","21cn.com","eyou.com","msn.com"];
var EmailDefault=["qq.com","163.com","126.com","yahoo.com.cn","sina.com"];

var lvSelectedIndex=-1; //记录当前选中Email
var lvLastArrayLength=-1;


function ShowEmailTip(pvInput,pvEvent,pvMailDiv,pvMailText)
{
	var lvObj=document.getElementById(pvMailDiv);	
	if (pvInput.value=="" || pvEvent.keyCode==13)
	{	
		lvObj.style.display="none";
		return false;
	}
	if (lvObj.style.display=="none")  //确保每次出提示框时锁定第一条
	{
		for (var i=0;i<lvObj.getElementsByTagName("div").length;i++)
		{
				lvObj.getElementsByTagName("div")[i].className="mes";
		}
		lvObj.getElementsByTagName("div")[0].className="meshover";
		lvObj.style.display="block";
		lvObj.style.visibility="";
	}
	var value=pvInput.value;
	var reg=/@(\w|.)*/;
	
	var lvShowEmalArray=new Array();
	if (value.search("@")!=-1)
	{
	    var lvInputMailServer=value.substring(value.search("@")+1);
	    if(lvInputMailServer=="")
	    {
	        lvShowEmalArray=EmailDefault;
	    }
	    else
	    {
	        lvInputMailServer=lvInputMailServer.toLowerCase();
	        for (var i=0;i<EmailCollection.length;i++)
	        {
	            if(EmailCollection[i].length>=lvInputMailServer.length && lvInputMailServer==EmailCollection[i].substring(0,lvInputMailServer.length))
	            {
	                lvShowEmalArray.push(EmailCollection[i]);
	            }
	        }
	    }
	}
	else
	{
	    lvShowEmalArray=EmailDefault;
	}
	
	if(lvLastArrayLength!=lvShowEmalArray.length)
	{
	    lvLastArrayLength=lvShowEmalArray.length;
	    lvSelectedIndex=-1;
	}
	
	lvObj.innerHTML="";
	var lvDefaultDiv=document.createElement("div");
	lvDefaultDiv.id="mes";
	lvDefaultDiv.setAttribute("type","");
	lvDefaultDiv.className="mes";
	lvDefaultDiv.style.display="none";
	lvDefaultDiv.innerHTML=value;
	lvObj.appendChild(lvDefaultDiv);
	
	for(var i=0;i<lvShowEmalArray.length;i++)
	{
	    var lvDivElement=document.createElement("div");
	    lvDivElement.id="mes"+(i+1);
	    lvDivElement.className=((i+1)==lvSelectedIndex)?"meshover":"mes";
	    var lvEmailServerValue="";
	    if(value.search("@")==-1)
	    {
	        lvEmailServerValue=value+"@"+lvShowEmalArray[i];
	    }
	    else
	    {
	        lvEmailServerValue=value.substring(0,value.search("@")+1)+lvShowEmalArray[i];
	    }
	    lvDivElement.setAttribute("type",lvEmailServerValue);
	    lvDivElement.innerHTML=lvEmailServerValue;
	    
	    lvDivElement.onmouseout=function(){ this.className="mes";}
	    lvDivElement.onmouseover = function(){
			    var obj=document.getElementById(pvMailDiv);
			    for (var i=0;i<obj.getElementsByTagName("div").length;i++)
			    {
				    obj.getElementsByTagName("div")[i].className="mes";
			    }
			     this.className="meshover";
	        }
    	
	    lvDivElement.onclick = function(){    		    
			    document.getElementById(pvMailText).value=this.innerHTML;
			    document.getElementById(pvMailDiv).style.display="none";
		    }
    	
	    lvObj.appendChild(lvDivElement);
	}
}


function HideEmailTip(pvMailDiv)
{
    setTimeout("document.getElementById('"+pvMailDiv+"').style.display='none';",300);
}
function SelectItem(pvMailDiv,pvNewItem,pvOldItem)
{
    var obj=document.getElementById(pvMailDiv);
    if(pvOldItem>0)
    {
        obj.getElementsByTagName("div")[pvOldItem].className="mes";
    }
    obj.getElementsByTagName("div")[pvNewItem].className="meshover";    
}
function pressdownup(pvEvent,pvMailDiv,pvMailText)
{
	var obj=document.getElementById(pvMailDiv);
	if (pvEvent==null || pvEvent=="")
	{
		pvEvent=window.event;
    }
	if (pvEvent.keyCode!=38 && pvEvent.keyCode!=40)
	{
		return false;
    }
	else
	{
		var lvDivLength=obj.getElementsByTagName("div").length;
		if (pvEvent.keyCode==38) //向上
		{
		    var lvUpEventDown=false;
		    for(var loop=lvDivLength-1;loop>0;loop--)
		    {
		        if(obj.getElementsByTagName("div")[loop].className=="meshover")
		        {
		            lvSelectedIndex=loop-1;
		            if(lvSelectedIndex==0)
		            {
		                lvSelectedIndex=lvDivLength-1;
		            }
		            SelectItem(pvMailDiv,lvSelectedIndex,loop);	
		            lvUpEventDown=true;
		            break;
		        }
		    }
		    if(lvUpEventDown==false)
		    {
		        lvSelectedIndex=lvDivLength-1;
		        SelectItem(pvMailDiv,lvSelectedIndex,0);		        
		    }
		}
		else
		{
		    var lvDownEventDown=false;
		    for(var loop=1;loop<lvDivLength;loop++)
		    {
		        if(obj.getElementsByTagName("div")[loop].className=="meshover")
		        {
		            lvSelectedIndex=loop+1;
		            if(lvSelectedIndex==lvDivLength)
		            {
		                lvSelectedIndex=1;
		            }
		            SelectItem(pvMailDiv,lvSelectedIndex,loop);	
		            lvDownEventDown=true;
		            break;
		        }		    
		    }
		    if(lvDownEventDown==false)
		    {
		        lvSelectedIndex=1;
		        SelectItem(pvMailDiv,lvSelectedIndex,0);	
		    }		    
		}
	}
}
function setvalue(pvEvent,pvMailDiv,pvMailText)
{
	var obj=document.getElementById(pvMailDiv);
	if (pvEvent==null || pvEvent=="")
	{
		pvEvent=window.event;
	}
	if (pvEvent.keyCode!=13)
	{
		return false;
	}
	else
	{
		for (var i=0;i<obj.getElementsByTagName("div").length;i++)
		{
			if (obj.getElementsByTagName("div")[i].className=="meshover")
			{
				obj.getElementsByTagName("div")[i].onclick();
				break;
			}
		}
		if(pvEvent.cancelable)
		{
		    pvEvent.preventDefault();
		    pvEvent.returnValue =false;
		    pvEvent.cancel = true;
		}
		else
		{
		    pvEvent.cancelBubble =true;
		    pvEvent.returnValue =false;
		    pvEvent.cancel = true;
		}		
	}
}