<%@page contentType="text/html; charset=UTF-8"%>
<%@page import="com.csc.library.utilities.CheckNull,com.csc.library.mail.SendMail"%>
<%@page import="com.csc.library.utilities.*"%>
<%request.setCharacterEncoding("UTF-8");%>
<%
CheckNull  chk = new CheckNull();
String cmd = chk.chkNullString(request.getParameter("__cmd"));
int result=0;
if(cmd.equalsIgnoreCase("send")){
  String from = chk.chkNullString(request.getParameter("__mail_from"));
  String to = chk.chkNullString(request.getParameter("__mail_to"));
  String cc = chk.chkNullString(request.getParameter("__mail_cc"));
  String subject = new ThaiUtilities().ASCII2Unicode(chk.chkNullString(request.getParameter("__mail_subject")));
  String message = new ThaiUtilities().ASCII2Unicode(chk.chkNullString(request.getParameter("__mail_message")).replaceAll("\n","<br>"));
  if(!to.equals("")){
    SendMail mail = new SendMail( "", from, to, cc, subject, message);
    result = mail.send();
  }
}
%>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link href="../CSS/TISCO.css" rel="stylesheet" type="text/css">
<script type='text/javascript' src='../JS/SCREEN.js'></script>
<script type='text/javascript' src='../JS/DATEINPUT.js'></script>
<script type='text/javascript' src='../JS/VIEWCALENDAR.js'></script>
<script type='text/javascript' src='../JS/ICONSMENU.js'></script>

<script language='javascript'>setLang(window.parent.document.cscform.__language.value);</script>
<script language="javascript">getTitleName();</script>
<script language='javascript' src='../JS/HOTKEY.js'></script>
<script language="JavaScript" type="text/JavaScript">
var lang = window.parent.document.cscform.__language.value;
function GoSave(){}
function GoUpdate(){}
function GoDelete(){}
function GoSearch(){}
function goBack(){
	close();
}
function searchEmail(){
	window.open("ASV2U002H.jsp","","left=150,top=20,width=1000,height=600,toolbar=no,status=yes,scrollbars=yes,resizable=no");
}
function sendMailComp(status) {
	with(document.cscform){
		if(status=="0"){
			if(oldemp.value!=""){
				setStatusS(oldemp.value);
			}
			if(sendall.value=="1"){
				sendMailAll();
			}else{
				alert("การส่งเมล์ล้มเหลว");
        nextmail.value = "0";
			}
		}else{
			if(oldemp.value!=""){
				setStatusF(oldemp.value);
			}
			if(sendall.value=="1"){
				sendMailAll();
			}else{
				if(thisemp.value!=""){
					alert("ส่งเมล์เรียบร้อยแล้ว");
				}else{
					window.parent.document.getElementById("mframe").height = "0";
					alert("เสร็จสิ้นการส่งเมลล์");
				}
			}
		}
	}
}
function sendMailAll(){
	with(document.cscform){
    if (thisemp.value!="" && window.parent.document.cscform.send_type.value === "2"){
			__cmd.value = "send";
      nextmail.value = "1";
			submit();
		} else {
			window.parent.document.getElementById("mframe").height = "0";
			alert("เสร็จสิ้นการส่งเมลล์");
		}
	}
}
function setDefMail(mailstatus){
	with(document.cscform){
    if(mailstatus === "1" && nextmail.value === "1") {
  		if(emp_id.value!=""){
  			var tmp_emp_id = emp_id.value.split(",");
  			if(tmp_emp_id.length>0){
  				thisemp.value = tmp_emp_id[0];
  			}
  			emp_id.value = "";
  			if(tmp_emp_id.length>1){
  				for(i=1;i<tmp_emp_id.length;i++){
  					if(emp_id.value!=""){
  						emp_id.value += ",";
  					}
  					emp_id.value += tmp_emp_id[i];
  				}
  			}
  		}
    } else {
      thisemp.value = oldemp.value;
    }

		if(thisemp.value!=""){
			var tmp_thisemp = thisemp.value;
			//alert(tmp_thisemp);
			__mail_to.value = getThisMailTo(tmp_thisemp);
			__mail_cc.value = getThisMailCc(tmp_thisemp);
			__mail_subject.value = getThisSubject();
			__mail_message.value = getThisMass(tmp_thisemp);
			window.parent.document.cscform.mail_cctmp.value = __mail_cc.value;
			window.parent.document.cscform.mail_tmp.value = __mail_to.value;
			window.parent.chkSend(window.parent.document.cscform.send_type.value);
			setStatusW(tmp_thisemp);
		}
	}
}
function setSubject_Massage(){
	with(document.cscform){
		__mail_subject.value = getThisSubject();
		__mail_message.value = getThisMass(thisemp.value);
	}
}
function getThisMailTo(empid){
	return window.parent.document.getElementById("empmail$"+empid).value;
}
function getThisMailCc(empid){
	if(window.parent.document.getElementById("type_cc").checked){
		return window.parent.document.getElementById("bossmail$"+empid).value;
	}else{
		return "";
	}
}
function getThisSubject(){
	return window.parent.document.cscform.subject.value;
}
function getThisMass(empid){
	var tmp = window.parent.document.cscform.massage.value;
        var empname = window.parent.document.getElementById("empname$"+empid).value;
		if(document.cscform["sendall"].value == "1"){
                    if(lang === "tha"){
                        tmp = tmp.replace("empname", "ผู้เข้าอบรม");
                    } else {
                        tmp = tmp.replace("empname", "all participants,");
                    }
		}else{
                    if(lang === "tha"){
			tmp = tmp.replace("empname", "คุณ" + empname);
                    } else {
                        tmp = tmp.replace("empname", empname);
                    }
		}
	return tmp;
}
function setStatusW(empid){
	window.parent.document.getElementById("empid$"+empid).innerHTML = "<font class='wait'>"+window.parent.document.getElementById("empid$"+empid).value+"</font>";
	//alert(window.parent.document.getElementById("empid$"+empid).value);
}
function setStatusF(empid){
	window.parent.document.getElementById("empid$"+empid).innerHTML = "<font class='free'>"+window.parent.document.getElementById("empid$"+empid).value+"</font>";
	//alert(window.parent.document.getElementById("empid$"+empid).value);
}
function setStatusS(empid){
	window.parent.document.getElementById("empid$"+empid).innerHTML = "<font class='stop'>"+window.parent.document.getElementById("empid$"+empid).value+"</font>";
	//alert(window.parent.document.getElementById("empid$"+empid).value);
}
function setStatusB(empid){
	window.parent.document.getElementById("empid$"+empid).innerHTML = "<font class=''>"+window.parent.document.getElementById("empid$"+empid).value+"</font>";
	//alert(window.parent.document.getElementById("empid$"+empid).value);
}
</script>
<STYLE type=text/css>
	FONT.wait {
	FONT-FAMILY: "MS Sans Serif", sans-serif, "Microsoft Sans Serif";
	font-size: 12px;
	color: #E85C00;
	}
	FONT.free {
	FONT-FAMILY: "MS Sans Serif", sans-serif, "Microsoft Sans Serif";
	font-size: 12px;
	color: #339900;
	}
	FONT.stop {
	FONT-FAMILY: "MS Sans Serif", sans-serif, "Microsoft Sans Serif";
	font-size: 12px;
	color: #FF0000;
	}
</STYLE>
<%
	String tmpMail = new ThaiUtilities().ASCII2Unicode(chk.chkNullString(request.getParameter("__tmpMail")));
	tmpMail = tmpMail.replace("$"," ");
	String tmpHead = new ThaiUtilities().ASCII2Unicode(chk.chkNullString(request.getParameter("__tmp_head")));
	tmpHead = tmpHead.replace("$"," ");
%>
</head>
<body leftmargin="0" topmargin="0">
  <div id="toolTipLayer" style="position:absolute; visibility: hidden"></div>
  <form name="cscform" method="post" action="TRA0126MS.jsp">

    <input type="hidden" name="__screen" value="TRA0126MS"/>
    <input type="hidden" name="__cmd" value=""/>
    <input type="hidden" name="__help" value=""/>
    <input type="hidden" name="__goPage" value=""/>
    <input type="hidden" name="__orderBy" value=""/>
    <input type="hidden" name="__helpReturn" value=""/>
    <input type="hidden" name="__helpName" value=""/>
    <input type="hidden" name="__pageCall" value=""/>
    <input type="hidden" name="__fixCon" value=""/>
    <input type="hidden" name="__fixFilter" value=""/>
    <input type="hidden" name="__language" value="<%//=screen.getUProfile().get("lang")%>"/>
    <input type="hidden" name="__companyid" value="<%//=screen.getUProfile().get("companyid")%>"/>
    <input type="hidden" name="mailform" value="<%=chk.chkNullString(request.getParameter("mailform"))%>"/>
    <input type="hidden" name="emp_id" value="<%=chk.chkNullString(request.getParameter("emp_id"))%>"/>
    <input type="hidden" name="thisemp" value=""/>
    <input type="hidden" name="oldemp" value="<%=chk.chkNullString(request.getParameter("thisemp"))%>"/>
    <input type="hidden" name="sendall" value="<%=chk.chkNullString(request.getParameter("sendall"))%>"/>
    <input type="hidden" name="nextmail" value="<%=chk.chkNullString(request.getParameter("nextmail"),"1")%>"/>
    <script language="javascript">getInputFormatDate();</script>
    <script language="javascript">initToolTips();</script>
    <table border="0" align="center" cellpadding="0" cellspacing="0" class="maxsize">
      <tr>
        <td class="header">TRA0126MS,<script language="javascript">swapLang("Detail");</script>&nbsp;</td>
      </tr>
      <tr>
        <td class="blankspace"></td>
      </tr>
      <tr>
        <td>
          <table align="center" border="0" cellpadding="0" cellspacing="0" class="largest" colspan="2">
            <tr>
              <td width="20%" align="right"><script language="javascript">swapLang("To");</script>&nbsp;</td>
              <td width="80%"><input name="__mail_to" type="text" size="70" value=""  ></td>
            </tr>
            <tr>
              <td  align="right"><script language="javascript">swapLang("cc");</script>&nbsp;</td>
              <td><input name="__mail_cc" type="text" size="70" onkeypress="chkNotThaiChaOnly()" value="" ></td>
            </tr>
            <tr>
              <td  align="right"><script language="javascript">swapLang("From");</script>&nbsp;</td>
              <td><input name="__mail_from" type="text" size="70" maxlength="70" onkeypress="chkNotThaiChaOnly()" value="<%=(chk.chkNullString(request.getParameter("__mail_from")).equals(""))?chk.chkNullString(request.getParameter("mailform")):chk.chkNullString(request.getParameter("__mail_from"))%>"></td>
              <!-- onblur="chkEmail(this)" -->
            </tr>
            <tr>
              <td  align="right"><script language="javascript">swapLang("Subject");</script>&nbsp;</td>
              <td><input name="__mail_subject" type="text" size="70"  value=""></td>
            </tr>
            <tr>
              <td valign="top"  align="right"><script language="javascript">swapLang("Message");</script>&nbsp;</td>
              <td><textarea name="__mail_message" cols="70" rows="15"></textarea></td>
            </tr>
          </table>
        </td>
      </tr>
      <tr>
        <td class="blankspace"></td>
      </tr>
    </table>
    <%
    if (request.getParameter("__cmd") != null && request.getParameter("__cmd").equals("send")) {
      %>
      <script language="javascript">
      setDefMail('<%=result%>');
      sendMailComp('<%=result%>');
      </script>
      <%
    } else {
      %>
      <script language="javascript">setDefMail('1');</script>
      <%
    }
    %>
  </form>
</body>
</html>