function isEmail(email) 
{ 
	invalidChars = " /;,:{}[]|*%$#!()`<>?"; 
	if (email == "") { 
		return false; 
	}
	for (i=0; i< invalidChars.length; i++)  { 
		badChar = invalidChars.charAt(i) 
		if (email.indexOf(badChar,0) > -1)  { 
			return false; 
		}
    } 
	atPos = email.indexOf("@",1) 
	if (atPos == -1)  {   return false;  } 
	if (email.indexOf("@", atPos+1) != -1) {   return false;  } 
	periodPos = email.indexOf(".",atPos) 
	if(periodPos == -1) {
		return false;  // and at least one "." after the "@" 
	} 
	if ( atPos +2 > periodPos)  { 
		return false;  // and at least one character between "@" and "." 
	}
	if ( periodPos +3 > email.length)  {   return false;  } 
	return true; 
} 

function Mailadd(){
	if (window.event.keyCode==13){
		addmail();
		return false;
	}else return true;
}

function addmail(){
	var mailadd,sendcount,addflag;
	var optmail;
	addflag=0;
	if(!isEmail(document.sendmail.mailfrom.value)){
		alert("请检查发件人Email地址！");
		document.sendmail.mailfrom.focus();
		addflag=1;
		return false;
	}
	if(document.sendmail.mailadd.value.length==0){
		alert("你还没有输入要添加的收件人Email！");
		document.sendmail.mailadd.focus();
		addflag=1;
		return false;
	}else{
		mailadd=document.sendmail.mailadd.value;
		if(!isEmail(document.sendmail.mailadd.value)){
			alert("请检查收件人Email地址！");
			document.sendmail.mailadd.focus();
			addflag=1;
			return false;
		} else {
			sendcount=document.sendmail.maillist.length;
			for(j=0;j<sendcount;j++){
				if(mailadd==document.sendmail.maillist[j].value){
					alert("您添加的收信人Email已经存在了！");
					document.sendmail.mailadd.focus();
					addflag=1;
					return false;
				}
			}
		}
	}
	if(addflag==0){
		optmail=document.createElement("option");
		optmail.text=mailadd;
		optmail.value=mailadd;
		document.sendmail.maillist.add(optmail);
		document.sendmail.mailadd.value="";
	}
}

function delmail(){
	var sendcount,delflag,delcount;
	delflag=0;
	delcount=0;
	sendcount=document.sendmail.maillist.length;
	for(i=0;i<sendcount;i++){
		if(document.sendmail.maillist[i].selected==true){
			delflag=1;
			for(j=i;j>0;j--){
				document.sendmail.maillist[j].value=document.sendmail.maillist[j-1].value;
				document.sendmail.maillist[j].text=document.sendmail.maillist[j-1].text;
			}
			delcount=delcount+1;				
		}			
	}
	if (delflag==0){
		alert("请在收件人列表中选择要删除的Email！");
	}
	for(i=0;i<delcount;i++){
		document.sendmail.maillist.remove(0);
	}
	for(i=0;i<document.sendmail.maillist.length;i++){
		document.sendmail.maillist[i].selected=false;
	}
}

function submit_invitemail(){
	if(document.sendmail.subject.value.length==0){
		alert("邮件主题不能为空！");
		document.sendmail.subject.focus();
		return false;
	}
	var count=document.sendmail.maillist.length;
	if (count==0){
		alert("收件人列表不能为空，请添加收件人Email！");
		document.sendmail.mailadd.focus();
		return false;
	}
	if(document.sendmail.content.value.length==0){
		alert("邀请函正文不能为空！");
		document.sendmail.content.focus();
		return false;
	}
	for (i=0;i<count;i++){
		document.sendmail.maillist[i].selected=true;
	}

	document.sendmail.submit.disabled=true;
	//initAd();
}
