<?php
	if($_SERVER['REQUEST_METHOD'] == "POST")
	{
		$filename = $_FILES['flashUpload']['name'];
		$target_path = "uploadsDir/flashfiles/";
		$target_path = $target_path . $filename; 
		move_uploaded_file($_FILES['flashUpload']['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 Flash</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("flashUpload").value;
            if(filupload == "")
            {
                alert("Please select a file");
                return false;
            }    
            else
            {
                if(filupload.lastIndexOf(".swf") == -1 && filupload.lastIndexOf(".SWF") == -1)
                {
                    alert("The File you selected is not a FLASH");
                    return false;
                }
            }
        }
    
        var flashTag;
        function loadFlash(flashName)
        {            
            flashTag = "<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0' width='150' height='150'>";
			flashTag += "<param name='movie' value='uploadsDir/flashfiles/" + flashName +"'>";
            flashTag += "<param name='quality' value='High'>";
			flashTag += "</object>";
            document.getElementById("flashDiv").innerHTML = flashTag;
        }
        
        function setFlash()
        {
            if(flashTag)
            {
                var topWind = window.opener;
                topWind.editFrame.focus();
                var iWid = document.getElementById("flaWidth").value;
                var iHei = document.getElementById("flaHeight").value;
                if(iWid)
                {
                    var widStr = "width='"+ iWid + "'";
                    flashTag = flashTag.replace("width='150'",widStr);
                }
                if(iHei)
                {
                    var heiStr = "height='"+ iHei + "'";
                    flashTag = flashTag.replace("height='150'",heiStr);
                }
                var selectedRegion = topWind.editFrame.document.selection.createRange();
                selectedRegion.pasteHTML(flashTag);
                window.close();
            }
            else
            {
                alert("You have not selected any flash");
            }
            
        }
        
    </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 FLASH
            </td>
        </tr>
        <tr>
            <td colspan="2" align="center">
                <input type="file" id="flashUpload" name="flashUpload" 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 Flashs
            </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/flashfiles/");
						while (($file = readdir($dir)) !== false)
						{
							if($file != "." && $file!= "..")
						  		echo "<tr><td class='norStyle' onfocusout=\"this.className = 'norStyle'\" onclick=\"loadFlash('" . $file . "'); this.className = 'selStyle';\" style='cursor:default'>" . $file . "</td></tr>";
						}
						closedir($dir);
					 ?>
					 </table>
                </div>
            </td>
            <td align="Center">
                <div id="flashDiv" class="newDivStyle">
                </div>
            </td>
        </tr>
        <tr>
            <td colspan="2" class="txtStyle">
                <table width="20%" cellpadding="2" cellspacing="1">
                    <tr>
                        <td align="left">
                            Width:
                        </td>
                        <td align="left">
                            <input type="text" class="txtStyle" id="flaWidth" style="width: 25px" value="150" />
                        </td>
                    </tr>
                    <tr>
                        <td>
                            Height:
                        </td>
                        <td align="left">
                            <input type="text" class="txtStyle" id="flaHeight" style="width: 25px" value="150" />
                        </td>
                    </tr>
                </table> </td> </tr>
        <tr>
            <td colspan="2" align="right">
                <button id="btnOK" class="buttonStyle2" onclick="setFlash()">
                    OK</button>&nbsp;
                <button id="btnCancel" class="buttonStyle2" onclick="window.close()">
                    Cancel</button>
            </td>
        </tr>
    </table>
    </form>
</body>
</html>
