<?php
ob_start();
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
// +----------------------------------------------------------------------+
// | PHP version 4/5                                                      |
// +----------------------------------------------------------------------+
// | This source file is a part of iScripts Multicart                     |
// +----------------------------------------------------------------------+
// | Authors: Programmer<programmer@armia.com>        		              |
// +----------------------------------------------------------------------+
// | Copyrights Armia Systems, Inc and iScripts.com � 2005                |
// | All rights reserved                                                  |
// +----------------------------------------------------------------------+
// | This script may not be distributed, sold, given away for free to     |
// | third party, or used as a part of any internet services such as      |
// | webdesign etc.                                                       |
// +----------------------------------------------------------------------+
?>
<?php
include "../includes/config.php";
include "../includes/session.php";
include "../includes/functions.php";

if (get_magic_quotes_gpc()) {
    $_POST = array_map('stripslashes_deep', $_POST);
    $_GET = array_map('stripslashes_deep', $_GET);
    $_COOKIE = array_map('stripslashes_deep', $_COOKIE);
}

if(isset( $_SESSION["sess_adminname"]) and $_SESSION["sess_adminname"]!= ""){
	$adminname = $_SESSION["sess_adminname"];
}else{
	header("Location:login.php");
	exit;
}


if(isset($_POST['update']))
{


	/*check whether custom fields with the same name exists in the database*/
		
	$select_custom_fields = "SELECT * FROM ".$tableprefix."custom_fields 
							 WHERE LOWER(vcustom_field_name) = ".GetSQLValueString(strtolower($_POST['txtfield_name']),"text").
							 " AND ncustom_id != ".$_GET['edit_id'];
	$result_custom_fields = mysql_query($select_custom_fields) or die(mysql_error());
	
	if(mysql_num_rows($result_custom_fields)>0)
	{
	
		$message = CUS_EXIST;
	}
	else
	{	

		$update_custom_fields = "UPDATE ".$tableprefix."custom_fields
								 SET vcustom_field_name = ".GetSQLValueString($_POST['txtfield_name'],"text").",
							  		 vcustom_field_type = ".GetSQLValueString($_POST['txtfield_type'],"text").",
							  		 vrequired_custom_field =".GetSQLValueString($_POST['txtfield_required'],"text").",
									 vcustom_sort_order = ".GetSQLValueString($_POST['txtfield_sortorder'],"text").",
									 vcustom_display_field = ".GetSQLValueString($_POST['txtfield_displayfield'],"text").
								 " WHERE ncustom_id = ".$_GET['edit_id'];	 
							  	
		mysql_query($update_custom_fields) or die(mysql_error());
	
	//$message = "Custom Field Updated Successfully !!";
	
	header("Location:custom_fields.php?".$_GET['qstring']."&txtSearch=".$_GET['txtSearch']."&msg=S");
	}
	

}

include "../includes/htmltop.php";
?>
<script>
function checkCustomFields()
{

  var form_name = new Array('txtfield_name','txtfield_sortorder');		
  var display_name = new Array('<?php echo FIELD_NAME;?>','<?php echo SORT_ORDER;?>');
  var count = 2;	

  if(check_fields('frmEditCustomFields',form_name,display_name,count))	
  {
  
  	
	 
   
  }
  else
  {
  
  		return false;
  
  }
   
}

</script>

		<body>
		
		<center>
		
		<div align="left" id="layout">
		
		
				
				
				<!--header area start -->
<?php

	include_once("../includes/topheader_admin.php")

?>
				
				<!--header area end -->
				
				
				
				
				<div align="left" class="main_body">
				<!--main body start -->
				
				
				
<?php
		include_once("../includes/admintopmenu.php")
?>		
																					
								
					<div align="left" class="content_area">
					
					<!--content area start -->
							
					<div align="left" class="page_title"><?php echo EDIT_REGN;?></div>
					<div align="center" class="text_information grey_tr"><?php echo $message?></div>
            		<br>
											
					<div align="left">				
																				
					<form name="frmEditCustomFields" method="post" onSubmit="return checkCustomFields();">

					<table width="100%" border="0" cellpadding="0" cellspacing="0" class="manage_table">
						<tr><td colspan="3">&nbsp;</td></tr>
						<tr><td colspan="3" align="right"><span class="required">*</span>&nbsp;<?php echo FIELDS_ARE_MANDATORY;?></td></tr>
						<tr>
                                                    <td colspan="2"><?php echo MSG_REGFORM;?></td>
                                                </tr>
                                                <tr class="">
							<td width="12%" class="" valign="top"><?php echo FIELD_NAME;?>&nbsp;<span class="required">*</span></td>
		
<?php

							$select_custom_edit = "SELECT * FROM ".$tableprefix."custom_fields WHERE ncustom_id = ".$_GET['edit_id'];
							$result_custom_edit = mysql_query($select_custom_edit) or die(mysql_error());
							
							$custom_field_row = mysql_fetch_assoc($result_custom_edit);
																							
?>		
																									
						
						<td width="88%" valign="top"><input type="text" class="textarea2" name="txtfield_name" value="<?php echo $custom_field_row['vcustom_field_name'];?>"></td>
					</tr>
					<tr class="">
						<td width="12%" class="" valign="top"><?php echo FIELD_TYPE;?></td>
						<td width="88%" valign="top">
<?php
	
							switch($custom_field_row['vcustom_field_type'])
							{
							
								case "text_box":$text_box = "selected";
								break;
								case "text_area":$text_area = "selected";
								break;
								
							}
							
						
							$input_text = "<select name='txtfield_type' class='textarea2'>
												<option {$text_box} value='text_box'>".TEXTBOX."</option>	
												<option {$text_area} value='text_area'>".TEXTAREA."</option>	
											</select>";
																							
							
							echo $input_text;
?>		
							
							</td>
						</tr>
						<tr>
							<td width="12%" valign="top"><?php echo SORT_ORDER;?>&nbsp;<span class="required">*</span></td>
							<td width="88%" valign="top">
								<select name="txtfield_sortorder" class="textarea2">
																										
<?php

							for($i=1;$i<=100;$i++)
							{
							
								if($custom_field_row['vcustom_sort_order'] == $i)
								{
								
									$sort_checked = "selected";
								
								}
								else
								{
								
									$sort_checked = "";
								
								}
			
			
?>			
									<option value="<?php echo $i?>" <?php echo $sort_checked?>><?php echo $i?></option>
			

<?php			
						}


?>			
																										
									
									</select>
								</td>
							</tr>	
							<tr>
								<td width="12%" valign="top"><?php echo ACTION;?></td>
								<td width="88%">
<?php

						if($custom_field_row['vrequired_custom_field'] == 'Y')
						{
						
							$required_checked = "checked";
						
						}
						else
						{
						
							$required_checked = "";
						
						}
						
						if($custom_field_row['vcustom_display_field'] == 'Y')
						{
						
							$display_checked = "checked";
						
						}
						else
						{
						
							$display_checked = "";
						
						}

?>		
																									
						
							<input type="checkbox" name="txtfield_required" value="Y" <?php echo $required_checked;?>>&nbsp;[ <?php echo ENABLE_CHKBOX;?> ]<br>
							<input type="checkbox" name="txtfield_displayfield" value="Y" <?php echo $display_checked;?>>&nbsp;[ <?php echo ENABLE_CHKBOX_CUSTMFLD;?> ]
						</td>
					</tr>
					<tr>
						<td align="center" colspan="6">
							
							<input type="button" name="back" class="button" value="&nbsp;&nbsp;<?php echo BACK;?>&nbsp;&nbsp;" onClick="window.location='custom_fields.php'">
                                                        <input type="submit" class="button" name="update" value="<?php echo UPDATE;?>">
						</td> 
					</tr>				
				</table>	
			
				</form>
																	
																					
			</div>
						
						
				<!--content area end-->
								
				</div>
				
								
					<div align="left" class="clear_float"><img src="<?php echo $imagefolder;?>/clear.gif" alt="" width="1" height="1" /></div>
				<!--main body end -->
				</div>
				
				
<?php
	include "../includes/footer.php";
	
	ob_end_flush();
?>