<%@page contentType="text/html; charset=UTF-8"%>
<%@page import="com.csc.library.entry.HelpEntry,com.csc.library.system.*,com.csc.library.utilities.*"%>
<%@ page import="com.csc.library.entry.*,com.csc.library.database.*,com.csc.library.session.*"  %>
<%@page import="java.util.*,java.net.*,java.io.*,com.csc.upload.*"%>
<jsp:useBean id="hmul" class="com.csc.upload.HttpMultiPartParser" scope="page"/>
<jsp:useBean id="TASK" class="com.csc.library.system.Task" scope="page"/>
<%

	TASK.setChannel(request,response);
	TASK.checkProfile();
	UProfile u=TASK.getUProfile();

 	InitialEnvironment en = new InitialEnvironment("GLOBAL");
	String picturePath=en.getValue("WB_UPLOAD-dir");

	String tempdir = ".";

	//request.setAttribute("dir", request.getParameter("dir"));
	request.setAttribute("dir", picturePath);
	String browser_name = request.getRequestURI();
	CheckNull chk =new CheckNull();

%>
<%
    session.setAttribute("finishUpload","false");
	if ((request.getContentType()!=null)&&(request.getContentType().toLowerCase().startsWith("multipart"))){
		response.setContentType("text/html");
		try{
			int bstart = request.getContentType().lastIndexOf("oundary=");
			String bound = request.getContentType().substring(bstart+8);
			Hashtable ht = hmul.processData(request.getInputStream(), bound, picturePath);
        			hmul.uploadFiles(ht);		
			FileInfo fi = (FileInfo)ht.get("myFile");
			File f = fi.file;
			session.setAttribute("TOPIC_FILE", f.getName());
			session.setAttribute("finishUpload","true");
		}
		catch (Exception e){ }
	}
%>

<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link href="../CSS/TISCO.css" rel="stylesheet" type="text/css">
<link href="../CSS/MENU.css" rel="stylesheet" type="text/css">
<script  language="JavaScript" src="../JS/SCREEN.js"></script>
<script src="../JS/VIEWCALENDAR.js"></script>
<script language='javascript'>setLang('<%=u.get("lang")%>');</script>
<script language="JavaScript" >
<!--
/*
function checktype(){
	var stdstr="";
	var exp =/^\S+\.(gif|jpg|jpeg)$/i;
    with(document.cscform){
		if(myFile.value.length>0){
			stdstr=myFile.value.substring(myFile.value.lastIndexOf("\\")+1,myFile.value.length);
			if(stdstr.match(exp)!=null){
				setFileName(); 
			}else{
				alert("กรุณาเลือกไฟล์ (.jpg),ไฟล์ (.jpeg) หรือไฟล์ (.gif) เท่านั้น");
			}
		} else if (myFile.value.length==0)
			alert("กรุณาเลือกไฟล์ก่อน");
	}
}

*/
function checktype(){	
    with(document.cscform){
	if (myFile.value.length==0){
			alert("กรุณาเลือกไฟล์ก่อน");
	}else{
		setFileName();
	}
	}
}
function setFileName(){
	var file1 = document.cscform.myFile.value;
	var  pictureName="";
	if(file1.length>0 && checkFilename(file1)){ 
		pictureName=checkFilename(file1);

		}
	document.cscform.submit();
}

function checkFilename(obj){
	var stdstr="";
		if(obj.length>0){
			stdstr=obj.substring(obj.lastIndexOf("\\")+1,obj.length);
		}
		return stdstr;
}

function  uploadfile(){
		checktype();
}

<!--

//-->

function closeUpload(){
     var flag ="<%=session.getAttribute("finishUpload")%>";
	 if(flag=="true"){
		setTimer();
	 }
}

function closePage(){
		window.opener.document.cscform.__attatch_file.value="<%=session.getAttribute("TOPIC_FILE")%>";
	    window.close();
}
function setTimer(){
	setTimeout("closePage()",5000);
}

</script>
</head>
<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0" >
  <div align="center">
<form  name="cscform"  enctype="multipart/form-data"  method="POST">
  <input type="hidden" name="__cmd"  value="">
  <input type="hidden" name="__screen" value="PRU0392">
  <input type="hidden" name="__companyid" value="<%=u.get("companyid")%>">
  <input type="hidden" name="__language" value ="<%=u.get("lang")%>">
  <input type="hidden" name="dir" value="<%=picturePath%>">
  <br>
  <br>
  <br>
  <div align="center">
    <table  cellpadding="0"  cellspacing="0">
      <tr >
        <td  class="header">เอกสารแนบ
          &nbsp;</td>
      </tr>
      <tr >
        <td  colspan="2"><table width="630" border="0">
            <tr>
              <td colspan="3">
                <input type="hidden"name="__screen"  value="upload"></td>
            </tr>
            <tr>
              <td width="102" class="columnlabel" ><div align="right">
                  <script language="javascript">swapLang("Attach file");</script>
                </div></td>
              <td width="346">&nbsp;
                <input type="file" name="myFile" ></td>
              <td>&nbsp;</td>
            </tr>
            <tr>
              <td colspan="3">&nbsp;</td>
            </tr>
            <tr>
              <td colspan="3"><div align="center"><a href="javascript:uploadfile();"><img src="../IMAGES/BUTTON/UPLOAD.gif" border="0"></a></div></td>
            </tr>
            <tr>
              <td colspan="3" align="center">กรุณารอซักครู่ในระหว่างไฟล์กำลังอัพโหลด</td>
            </tr>
            <tr>
              <td colspan="3" align="center">หน้าต่างนี้จะปิดอัตโนมัติหลังจากอัพโหลดไฟล์เสร็จ</td>
            </tr>
            <tr>
              <td colspan="3">&nbsp;</td>
            </tr>
          </table></td>
      </tr>
      <tr>
        <td  class="bottom"></td>
      </tr>
    </table>
  </div>
</form>
</div>
<script language="javascript">closeUpload();</script>
</body>
</html>