<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
// +----------------------------------------------------------------------+
// | PHP version 4/5                                                      |
// +----------------------------------------------------------------------+
// | Copyright (c) 2005-2006 ARMIA INC                                    |
// +----------------------------------------------------------------------+
// | This source file is a part of iScripts EasyCreate 1.1                 |
// +----------------------------------------------------------------------+
// | Authors: jimmy<jimmy.jos@armia.com>        		              	  |
// +----------------------------------------------------------------------+
//Insert a form into the parent page
//User is asked for the name of the form first.
//Then the user can give either (i)ACTION OR (ii) MAILTO
//	(i)ACTION
//		user can give the address of the page the form must be sumitted and the method of submission
//	(ii)MAILTO
//		user can enter the mail address that will be set as the mailto: address of the form. 			   
include "../includes/session.php";
include "../includes/config.php";
$currentloc="../";	  
include "../includes/applicationheader.php";
?>
<script language=JavaScript>
window.onload = this.focus
//user selecting between ACTION/MAILTO
function actionChange() {
	if(document.formForm.rdAction[1].checked == true) {
		document.formForm.form_method.disabled=true;
		document.formForm.form_action.disabled = true;
		document.formForm.form_mail_action.disabled = false;
	}
	else {
		document.formForm.form_method.disabled=false;
		document.formForm.form_action.disabled = false;
		document.formForm.form_mail_action.disabled = true;
	}
}
//function to check for mail validity
function isValidEmail(email){
	var str1=email;
	var arr=str1.split('@');
	var eFlag=true;
	if(arr.length != 2)
	{
		eFlag = false;
	}
	else if(arr[0].length <= 0 || arr[0].indexOf(' ') != -1 || arr[0].indexOf("'") != -1 || arr[0].indexOf('"') != -1 || arr[1].indexOf('.') == -1)
	{
			eFlag = false;
	}
	else
	{
		var dot=arr[1].split('.');
		if(dot.length < 2)
		{
			eFlag = false;
		}
		else
		{
			if(dot[0].length <= 0 || dot[0].indexOf(' ') != -1 || dot[0].indexOf('"') != -1 || dot[0].indexOf("'") != -1)
			{
				eFlag = false;
			}

			for(i=1;i < dot.length;i++)
			{
				if(dot[i].length <= 0 || dot[i].indexOf(' ') != -1 || dot[i].indexOf('"') != -1 || dot[i].indexOf("'") != -1 || dot[i].length > 4)
				{
					eFlag = false;
				}
			}
		}
	}
		return eFlag;	
}
//User clicking on the Insert Form button invokes this function
//It constructs an html string for form and paste it to the parent window
//and paste it to the selected area using PasteHTML() function 
function InsertForm() {
	if(document.formForm.rdAction[1].checked == true) {
		if(document.formForm.form_mail_action.value.length <= 0 || !isValidEmail(document.formForm.form_mail_action.value))
		{
			alert("Please enter a valid email address when using 'Mail To'");
			return false;
		}
	}
	else
	{
		if(document.formForm.form_action.value.length <= 0)
		{
			alert("Please enter a value in the action field");
			return false;
		}
	}
	error = 0
	var sel = window.opener.document.selection;
	if (sel!=null) {
		var rng = sel.createRange();
	   	if (rng!=null) {
			name = document.formForm.form_name.value
			if(document.formForm.rdAction[1].checked == true) {
				action = "mailto:" + document.formForm.form_mail_action.value
				method = "post"
			}
			else {
				action = document.formForm.form_action.value
				method = document.formForm.form_method[formForm.form_method.selectedIndex].text
			}

        		if (error != 1) {

				if (method != "None") {
					method = ' method="' + method + '"'
				} else {
					method = ""
				}

				if (name != "") {
					name = ' name="' + name + '"'
				} else {
					name = ""
				}

				if (action != "") {
					action = ' action="' + action + '"'
				} else {
					action = ""
				}
				
       			HTMLForm = "<form id=ewp_element_to_style" + name + action + method +">&nbsp;</form>"
					rng.pasteHTML(HTMLForm)

					oForm = window.opener.foo.document.getElementById("ewp_element_to_style")

						oForm.runtimeStyle.border = "1px dotted #FF0000"
						
					/*if (window.opener.borderShown == "yes") {
						oForm.runtimeStyle.border = "1px dotted #FF0000"
					}*/

					oForm.removeAttribute("id")


        		}
		}
	
	}
	
	if (error != 1) {
		self.close();
	}
}

document.onkeydown = function () { 
			if (event.keyCode == 13) {	// ENTER
				InsertForm()
			}
};

document.onkeypress = onkeyup = function () {
	if (event.keyCode == 13) {	// ENTER
	event.cancelBubble = true;
	event.returnValue = false;
	return false;			
	}
};

</script>
<body>
<div class="MainBody" style="width:100%; height:100%">
<table width="96%" height="10%" align="center" border="0" cellspacing="0" cellpadding="0">
<tr>
	<td width="10" height="11"><img src="images/main_lc.gif" width="10" height="11"></td>
	<td background="images/main_tbg.gif" height="11"><font size="1"><img src="images/1x1.gif" width="1" height="1"></font></td>
	<td width="10" height="11"><img src="images/main_rc.gif" width="10" height="11"></td>
</tr>
<tr>
	<td width="10" height="11" background="images/main_lm.gif"></td>
	<td bgcolor="#F7F7F7" height="100%" class="body" style="padding:10px" valign="top">
		<form name=formForm>
		<table width="100%" border="0" cellspacing="0" cellpadding="0">
  		<tr>
			<td width="15"><img src="_images/1x1.gif" width="15" height="8"></td>
	  		<td class="maintext" bgcolor="#CCCCCC"><b>Insert Form</b></td>
	    </tr>
  		<tr>
			<td>&nbsp;</td>
	  		<td class="maintext"><br>Enter the required information and click &quot;Insert Form&quot; to insert a form into your webpage.<br>
				Click the &quot;Cancel&quot; Button to  close this window.<br>&nbsp;</td>
  		</tr>
  		<tr>
			<td>&nbsp;</td>
			<td class="body">
			    <table border="0" cellspacing="0" cellpadding="5" width="98%" class="bevel2">
			    <tr>
		    		<td class="maintext" width="202">Form Name</td>
					<td width="434" colspan="2" class="body">
			  			<input type="text" name="form_name" size="10" class="Text90" maxlength="50">
		  			</td>
		  		</tr>
		  		<tr>
		    		<td colspan="3" class="body">
			  		<fieldset>
						<legend class="maintext"><b>Action:</b>
			  			<input name="rdAction" type="radio" id="rdAction"  value="action" checked="true" onClick="javascript:actionChange();"></legend>
						<table cellpadding="4">
				 		 <tbody>
						 <tr>
					  		<td width="30%" class="maintext">Address</td>	
					  		<td><input type="text" name="form_action" id="form_action" size="30" class="Text250"></td>
						  </tr>
						  <tr>
							  <td width="30%" class="maintext">Method:</td>	
					  		  <td>
							  <select class=text70 name=form_method id="form_method" >
								<option selected>None
								<option>Post
								<option>Get</option>
								</select>
					  		</td>
						</tr>
				  		</tbody>
					</table>
				</fieldset>
			  		  </td>
			</tr>
		  <tr>
		    <td colspan="3" class="body">
			
			 <fieldset>
				<legend class="maintext"><b>Mail To</b>
		      <input name="rdAction" id="rdAction" type="radio" value="mail" onClick="javascript:actionChange();"></legend>
				<table cellpadding="4">
				  <tbody>
					<tr>
					 <td width="30%" class="maintext">Mail</td>	
					  <td><input type="text" name="form_mail_action" id="form_mail_action" size="30" class="Text250"></td>
					</tr>


				  </tbody>
				</table>
				</fieldset>
			
			
					      </td>
		    </tr>
		  
	    </table>
	</td>
  </tr>
  <tr>
	<td colspan="2"><img src="images/1x1.gif" width="1" height="10"></td>
  </tr>
  <tr>
	<td>&nbsp;</td>
	<td align="center">
	    <input type="button" name="insertForm" value="Insert Form" class="editorbutton" onClick="javascript:InsertForm();">
	<input type="button" name="Submit" value="Cancel" class="editorbutton" onClick="javascript:window.close()">
	</td>
  </tr>
</table>
</form>						</td>
						<td width="10" background="images/main_rm.gif" height="11"></td>
					</tr>
					<tr>
						<td width="10" height="11"><img src="images/main_lcb.gif" width="10" height="11"></td>
						<td background="images/main_bbg.gif" height="11"><font size="1"><img src="images/1x1.gif" width="1" height="1"></font></td>
						<td width="10" height="11"><img src="images/main_rcb.gif" width="10" height="11"></td>
					</tr>
				</table>
				
					</div>
					</div>
<?php
  include "./includes/editorfooter.php";
?> 