<?php
	if($_SERVER['REQUEST_METHOD'] == "POST")
	{
		$filename = $_FILES['templateUpload']['name'];
		$target_path = "uploadsDir/templatefiles/";
		$target_path = $target_path . $filename; 
		move_uploaded_file($_FILES['templateUpload']['tmp_name'], $target_path);
	}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Insert Template</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="EditorStyle.css" rel="stylesheet" type="text/css" />

    <script language="javascript">
        function chkFile()
        {
            var filupload = document.getElementById("templateUpload").value;
            if(filupload == "")
            {
                alert("Please select a HTML file");
                return false;
            }    
            else
            {
                if(filupload.lastIndexOf(".htm") == -1 && filupload.lastIndexOf(".HTM") == -1 && filupload.lastIndexOf(".html") == -1 && filupload.lastIndexOf(".HTML") == -1)
                {
                    alert("The File you selected is not a HTML File");
                    return false;
                }
            }
        }
    
        var tempSrc;
        function loadTemplate(tmpName)
        {           
            tempSrc = "uploadsDir/templatefiles/" + tmpName;
            document.getElementById("tempIFrame").src = tempSrc;
        }
        
        function setTemplate()
        {
            if(tempSrc)
            {
                var topWind = window.opener;
                topWind.editFrame.focus();
                var content = tempIFrame.document.body.innerHTML;
                var selectedRegion = topWind.editFrame.document.selection.createRange();
                selectedRegion.pasteHTML(content);
                window.close();
            }
            else
            {
                alert("You have not selected any HTML file");
            }
            
        }
        
    </script>

</head>
<body topmargin="0" bottommargin="0" rightmargin="0" leftmargin="0" class='txtStyle'
    bgcolor='#99ccFF'>
    <form name="form1" method="post" enctype="multipart/form-data">
    <table align="Center" cellpadding="2" cellspacing="2">
        <tr>
            <td colspan="2" align="center">
                INSERT TEMPLATES
            </td>
        </tr>
        <tr>
            <td colspan="2" align="center">
                <input type="file" id="templateUpload" name="templateUpload" width="250" class="otherstyle">
                &nbsp;
				<input type="submit" class="buttonStyle2" value="Upload" onclick="return chkFile()">
            </td>
        </tr>
        <tr>
            <td align="center" class="txtStyle">
                List of Templates
            </td>
            <td align="Center" class="txtStyle">
                Preview
            </td>
        </tr>
        <tr>
            <td align="center">
                <div id="listDiv" class="newDivStyle">
                    <table width="100%" cellpadding="0" cellspacing="0">
                    <?php 
						$dir = opendir("uploadsDir/templatefiles/");
						while (($file = readdir($dir)) !== false)
						{
							if($file != "." && $file!= "..")
							  	echo "<tr><td class='norStyle' onfocusout=\"this.className = 'norStyle'\" onclick=\"loadTemplate('" . $file . "'); this.className = 'selStyle';\" style='cursor:default'>" . $file . "</td></tr>";
						}
						closedir($dir);
					 ?>
					 </table>
                </div>
            </td>
            <td align="Center">
                <div class="newDivStyle">
                    <iframe id="tempIFrame" frameborder="0" width="150" height="150"></iframe>
                </div>
            </td>
        </tr>
        <tr>
            <td colspan="2" align="right">
                <button id="btnOK" class="buttonStyle2" onclick="setTemplate()">
                    OK</button>&nbsp;
                <button id="btnCancel" class="buttonStyle2" onclick="window.close()">
                    Cancel</button>
            </td>
        </tr>
    </table>
    </form>
</body>
</html>
