<%@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="UPLOADFILE" class="com.csc.library.system.Task" scope="page"/>
<%

	UPLOADFILE.setChannel(request,response);
	UPLOADFILE.checkProfile();
	UProfile u=UPLOADFILE.getUProfile();
	session.setAttribute("finishUpload","false");
	
	CheckNull chk =new CheckNull();
 	InitialEnvironment en = new InitialEnvironment("GLOBAL");
	String tempdir = ".";
	String uploadDir = en.getValue("WELFARE_UPLOAD-dir");
	String fieldName = chk.chkNullString(request.getParameter("__fieldName"));
	String fileName = chk.chkNullString(request.getParameter("__fileName"));
	String fileType = chk.chkNullString(request.getParameter("__uploadType"));
    String fileDir=chk.chkNullString(request.getParameter("__fileDir"));
	String fileSrc=chk.chkNullString(request.getParameter("__fileSrc"));

	String browser_name = request.getRequestURI();
	String filePath = uploadDir+""+fileDir;
	
	request.setAttribute("dir", filePath);
    
	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);
			System.out.println("==========" +bstart);
			System.out.println("==========" +bound);
			
			Hashtable ht = hmul.processData(request.getInputStream(), bound, filePath);
			ht.put("dir",filePath);
        	hmul.uploadFiles(ht,u);		
			session.setAttribute("uploadFileName", hmul.getStringFile());
			session.setAttribute("finishUpload","true");
		}
		catch (Exception e){ }
	}
%>
<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 language="JavaScript" src="../JS/SCREEN.js"></script>
<script language="javascript">setLang("<%=u.get("lang")%>");</script>
<script language="JavaScript" >

function uploadfile(){
	if(checkFile()){
		document.cscform.submit();
	}
}

function checkFile(){
	var chk = false;
	var exp = /\.(gif|jpg|jpeg|png)$/i;
    with(document.cscform){
		if(myFile.value.length>0){
			stdstr=myFile.value.substring(myFile.value.lastIndexOf("\\")+1,myFile.value.length);
			if(__fileType.value.toLowerCase()== "picture"){
				if(stdstr.match(exp) != null){
					chk = true;
				}else{
					alert("กรุณาเลือกไฟล์ (.jpg), ไฟล์ (.jpeg), ไฟล์ (.png) หรือไฟล์ (.gif) เท่านั้น");
				}
			}else{
			    chk = true;
			}
		} else if (myFile.value.length==0){
			alert("กรุณาเลือกไฟล์ก่อน");
		}
	}
	return chk;
}


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

function setTimer(){
	setTimeout("closePage()",5000);
}

function closePage(){
   with(document.cscform){
		if(__fileType.value.toLowerCase()== "picture"){
			var sr = eval("window.opener.document.cscform.<%=fileSrc%>");
			sr.src = "./UPLOAD/<%=fileDir%>"+__picName.value;
			var field = eval("window.opener.document.cscform.__<%=fileSrc%>");
			field.value=__picName.value;
		}else{
			var file = eval("window.opener.document.cscform.__<%=fileSrc%>")
			file.value=__picName.value;
		}
		window.close();
		
	}
}


</script>
</head>
<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0" >
  <div align="center">
<form  name="cscform"  enctype="multipart/form-data"  method="POST" action = "#">
  <input type="hidden" name="__cmd"  value="">
  <input type="hidden" name="__screen" value="UPLOADFILE">
  <input type="hidden" name="__companyid" value="<%=u.get("companyid")%>">
  <input type="hidden" name="__language" value ="<%=u.get("lang")%>">

  <input type="hidden" name="__fieldName" value="<%=chk.chkNullString(request.getParameter("__fieldName"))%>">
  <input type="hidden" name="__fileType" value="<%=fileType%>">
  <input type="hidden" name="__fileDire" value="<%=fileDir%>">
  <input type="hidden" name="__picName" value="<%=session.getAttribute("uploadFileName")%>">

  <br>
  <br>
  <div align="center">
    <table  cellpadding="0" cellspacing="0" class = "inner">
      <tr >
        <td  class="header">UPLOADFILE,&nbsp;
          <script language="javascript">swapLang("File Insertion Page");</script>
          &nbsp;</td>
      </tr>
      <tr >
        <td  colspan="2">
		<table class="inner" 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"><script language="javascript">swapLang("Please wait for a moment while the file is uploaded");</script></td>
            </tr>
            <tr>
              <td colspan="3" align="center"><script language="javascript">swapLang("This window will automatically closed after the file is finishing uploaded");</script></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>