<%@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.*"%>
<%@ include file="../CHECKPROFILE.jsp" %>
<jsp:useBean id="hmul" class="com.csc.upload.HttpMultiPartParser" scope="page"/>
<jsp:useBean id="PRU0392" class="com.csc.library.system.Task" scope="page"/>
<%

	PRU0392.setChannel(request,response);
	PRU0392.checkProfile();
	UProfile u=PRU0392.getUProfile();
	session.setAttribute("finishUpload","false");
	
	CheckNull chk =new CheckNull();
 	InitialEnvironment en = new InitialEnvironment("GLOBAL");
	String tempdir = ".";
	String uploadDir = en.getValue("RECRUIT_UPLOAD-dir");
	String fileDir = chk.chkNullString(request.getParameter("__uploadType"));
    String fieldName = chk.chkNullString(request.getParameter("__fieldName"));

	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);
			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){ }
	}

	FileDownloadUtils downloadutils = new FileDownloadUtils("RECRUIT_UPLOAD", "dir", (String)session.getAttribute("uploadFileName"), "PICTURE");
	downloadutils.setDefaultFileName("defaultperson.jpg");
	String path = downloadutils.getViewURL();
%>
<html ng-app="myHR">
	<head>
		<title></title>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		
		<link rel="stylesheet" href="<%=request.getContextPath()%>/CSS/font-awesome.css">
		<link rel="stylesheet" type="text/css" href="../CSS/Bootstrap/css/bootstrap.css" />
		<link rel="stylesheet" type="text/css" href="../CSS/EMPVIEW_LAYOUT.min.css" />
		
		<script type="text/javascript" src="../JS/angular-1.3.15/angular.js"></script>
		<script type="text/javascript" src="../JS/ANGULAR/SCREEN_ANGULAR.js"></script>
		<script type="text/javascript" src="../JS/jquery-2.1.3.min.js"></script>
		
		<script src="../JS/SCREEN.js"></script>
		<script>setLang("<%=u.get("lang")%>");</script>
		
		<script>

			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(__uploadType.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"){
					closePage();
				 }
			}

			function closePage(){
			   with(document.cscform){
					if(__uploadType.value.toLowerCase()== "picture"){
						window.opener.document.cscform.employeePic.src = "<%=path %>";
						window.opener.document.cscform.__picture.value="<%=session.getAttribute("uploadFileName")%>";
					}else{
						var file = eval("window.opener.document.cscform.<%=fieldName%>")
						file.value="<%=session.getAttribute("uploadFileName")%>";
					}
					window.close();
					
				}
			}
		</script>
		
	</head>
	<body>
		<div class="bg-com"></div>
		<div class="container csc-content">
			<form name="cscform" class="form-horizontal" method="post" enctype="multipart/form-data">
				<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="__uploadType" value="<%=fileDir %>">
				
				<div class="csc-block">
					<div class="center">
						<h3 class="csc-block-head" swlang code='SW000746'> 
							File Insertion Page 
						</h3>
					</div>
					<div class="form-horizontal">
						<div class="row">
							<div class="form-group">
								<div class="col-md-4 col-sm-4 col-xs-4"></div>
								<div class="col-md-4 col-sm-4 col-xs-4">
									<label class="control-label"><input type="file" name="myFile" /></label>
								</div>
							</div>
						</div>
						
						<div class="center">
							<a class="btn btn-primary" swlang code='SW012170' href="javascript:uploadfile();">Upload</a> 
						</div>
						<div class="center">
							<label class="control-label" swlang code='SW005013'>Please wait for a moment while the picture is uploaded</label>
							</br>
							<label class="control-label" swlang code='SW005014'>automatically closed after the picture is finishing uploaded</label>
						</div>
					</div>
				</div>	
			</form>
			<script>closeUpload();</script> 
		</div>
	</body>
</html>