<?php
/* 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( $_GET["smid"]) and $_GET["smid"]!= ""){
	$smid = $_GET["smid"];
}else if(isset( $_POST["smid"]) and $_POST["smid"]!= ""){
	$smid = $_POST["smid"];
}


$txtSMName = $_POST["txtSMName"];
$chkPublished = $_POST["chkPublished"];

if($chkPublished == "on"){
	$chkPublished = "Y";
}else{
	$chkPublished = "N";
}

if($_POST["btnSaveChanges"] == "Save Changes"){
	if(!isNotNull($txtSMName)){
		$message = "* Shipping Method Name cannot be empty!<br>";
	}else {
	     $logofilename = $_FILES['txtSMLogo']['name'];
    	 $logofiletype = $_FILES['txtSMLogo']['type'];
    	 $logotempname = $_FILES['txtSMLogo']['tmp_name'];
		 
		if ($_FILES['txtSMLogo']['name'] != "") {
	        if (!isValidWebImageType($logofiletype,$logofilename,$logotempname)) {
	            $message .= " * Invalid Shipping Logo file ! Upload an image (jpg/gif/bmp/png)" . "<br>";
	            $error = true;
	        } else {

	            $logoimagedest="../products/"."sm_".time().$logofilename;
	            if (move_uploaded_file( $_FILES['txtSMLogo']['tmp_name'], $logoimagedest)) {
			   		chmod($logoimagedest,0777);
		 		}else{
				$logoimagedest="";
				}

	        }
	    }else{
		$logoimagedest="";
		}


	        $qry = "SELECT  shipping_method_name FROM  " . $tableprefix . "shipping_methods WHERE shipping_method_name='" . addslashes($txtSMName) . "' ";
			if(isset($smid) and ( $smid != "")){
				$qry .= " AND shipping_method_id <> '".addslashes($smid)."' ";
			}

	        if (mysql_num_rows(mysql_query($qry)) > 0) {
	            $message .= "* A shipping method with the same name already exists!<br>";
	        }
    }
	if($message== ""){
		if(!isset($smid) || $smid == ""){//insert
			$sql ="INSERT INTO  ".$tableprefix."shipping_methods(shipping_method_name,date_created,shipping_method_logo)
			VALUES('".addslashes($txtSMName)."',now(),'$logoimagedest')
			";
			$res = mysql_query($sql);
			$smid = mysql_insert_id();
			$message = "Shiping Method created successfully!";
		}else{//update
			$sql ="UPDATE  ".$tableprefix."shipping_methods SET
			shipping_method_name = '".addslashes($txtSMName)."',shipping_method_logo = '".addslashes($logoimagedest)."'
			WHERE 	shipping_method_id = '".addslashes($smid)."'
			";
			$res = mysql_query($sql);

			$message = "Changes saved successfully!";
		}
	}else{
		$message = "<br>Please correct the following errors to continue!<br>" . $message;
	}
}else if($_POST["btnDelete"] == "Delete"){
	$sql ="DELETE FROM  ".$tableprefix."shipping_methods
		WHERE 	shipping_method_id = '".addslashes($smid)."'
		";
	$res = mysql_query($sql);
	header("Location:shippingmethods.php?msg=deleted");
	exit;
}

if(isset($_GET["msg"]) and $_GET["msg"]!=""){
	$msg = $_GET["msg"];
	switch($msg){
		case "deleted": $message = MSG_USER_DELETED;break;
		default:
			;
	} // switch

}

$sql = "SELECT * FROM ".$tableprefix."shipping_methods  WHERE shipping_method_id = '".addslashes($smid)."' ";
$result = mysql_query($sql);
if(mysql_num_rows($result) != 0){
	$row = mysql_fetch_array($result);
	$txtSMName = $row["shipping_method_name"];
	$txtSMLogo = $row["shipping_method_logo"];
	$txtDateCreated = $row["date_created"];
	$txtDateCreated = dateFormat($txtDateCreated,"Y-m-d","m/d/Y");

}

include "../includes/htmltop.php";
?>
<SCRIPT>
function confirmDelete(){
	if(confirm("Are you sure you want to delete this record?") ){
		return true;
	}
	return false;
}
</SCRIPT>

<body topmargin="0">
<table width="804" border="0" align="center" cellpadding="0" cellspacing="0">
	<tr>
		<td align="center" >
			<table width="802" border="0" align="center" cellpadding="0" cellspacing="0">
				<tr>
					<td >
						<table width="800" border="0" align="center" cellpadding="0" cellspacing="0">
            				<tr>
              					<td>
									<table width="100%"  border="0" cellspacing="0" cellpadding="0" class="maintext">
                  						<tr>
                    						<td>
												<?php include "../includes/topheader_admin.php";?>
												<table width="100%"  border="0" cellspacing="0" cellpadding="0">
													<tr>
														<td valign="top" bgcolor="#FFFFFF">
															<?php
																include "../includes/admintopmenu.php";
																include "../includes/dotline.php";
																//include "includes/centermain.php";
															?>
																<table width="100%"  border="0" cellspacing="0" cellpadding="0">
																<tr>
																	<td style="width:1px" class="siteborder"><img src="../images/spacer.gif"></td>
																	<td width="100%">
																		<table width="100%"  border="0" cellspacing="0" cellpadding="0">
																			<tr>
																				<td align="center" valign="top">
																					<p>&nbsp;</p>
																						<table width="100%" class="headingbig">
																							<tr><td align="center">Add/Edit Shipping Method</td></tr>
																						</table>
																						<table width="100%">
																							<tr>
																								<td>
																									<!-- ***************************************************** -->
																									<form name="frmFeedback" action="<?php echo $_SERVER["PHP_SELF"]?>" method="POST" enctype="multipart/form-data">
																									<input type="hidden" name="smid" value="<?php echo htmlentities($smid);?>">
																										<table cellpadding="2" cellspacing="2" width="70%" class="heading" border=0>
																											<tr><td colspan="3">&nbsp;</td></tr>
																											<tr><td colspan="3" class="message" align="left"><?php echo $message;?></td></tr>
																											<tr><td colspan="3">&nbsp;</td></tr>
																											<tr><td align="left" >Shipping Method Name&nbsp;<span class="required">*</span></td><td >&nbsp;</td><td align="left"><input class="textbox" type="text" value="<?php echo htmlentities($txtSMName);?>" name="txtSMName" size="30" maxlength="100"></td></tr>
																											<tr><td align="left" >Logo&nbsp;<span class="required"></span></td><td >&nbsp;</td>
																											<td align="left"><input class="textbox" type="file"  name="txtSMLogo" >
																											</td>

																											</tr>
																											<tr>
																											 <td colspan=2>&nbsp;</td>
																											 <td><?php
																											if($txtSMLogo !=""){
																											  echo "<img src=\"$txtSMLogo\">";
																											}
																											?></td>
																											</tr>
																											<tr><td colspan="3">&nbsp;</td></tr>
																											<tr><td colspan="3" align="center">
																												<input type="submit" value="Save Changes" name="btnSaveChanges" class="button" >
																												<?php
																												if(isset($smid) and ( $smid != "")){?>
																												&nbsp;&nbsp;<input type="submit" class="button" value="Delete" name="btnDelete" onClick="return confirmDelete();" >
																												<?
																												}
																												?>

																												&nbsp;&nbsp;<input class="button" type="button" value="Back" name="btnBack" onClick="window.location.href='shippingmethods.php';" >
																												</td>
																											</tr>
																											<tr><td colspan="3">&nbsp;</td></tr>
																										</table>
																									</form>
																									<!-- ***************************************************** -->
																								</td>
																							</tr>
																						</table>


																				</td>
                                											</tr>
                           												</table>
                             										</td>
                              										<td style="width:1px" class="siteborder"><img src="../images/spacer.gif"></td>
                                  								</tr>
                     										</table>
															<?php
																include "../includes/dotline.php";
																include "../includes/adminbottommenu.php";
																include "../includes/bottomband.php";
															?>
														</td>
                          							</tr>
                      							</table>
											</td>
                  						</tr>
									</table>
								</td>
 							</tr>
						</table>
					</td>
				</tr>
			</table>
		</td>
	</tr>
</table>
<?php
include "../includes/footer.php";
?>
</body>
</html>




