<?
/*
 * This file is part of a larger software package. Please read the License file 
 * in the root of this installation for terms of usage and distribution.
 * 
 * @author The Web Pro Miami, Inc. <info@thewebpro.com>
 * @copyright The Web Pro Miami, Inc. <info@thewebpro.com>, http://www.thewebpro.com/
 * 
 * CORE: manage/dialog/multiple_edit.php - version 1.0
*/

// Secure (later) and connect
require("../../inc/dbconnect.php");

// Error checking for form submission
$nError = "";

// Dialog config (vars must be unique to page)
$nAddFormName = "multipleEditForm"; // must match on calling page
$nAddDialogID = "dialogMultipleEditForm".rand(); // needs a dynamic id to always keep cache clear
$nAddFormSubmitID = "submitMultipleEditForm";
$nAddDialogLoader = "addDialogLoader";
$nDivToRefresh = "multipleEditFormTable"; // must be ID of main div or tab to refresh, leave blank if not needed

// Get passed-in variables
$nUpdateDB = (isset($_POST["nUpdateDB"]) ? $_POST["nUpdateDB"] : NULL);
$nListShowing = (isset($_POST["nListShowing"]) ? $_POST["nListShowing"] : NULL);
$nSave = (isset($_POST["nSave"]) ? $_POST["nSave"] : NULL);
$nEditField = (isset($_POST["nEditField"]) ? $_POST["nEditField"] : NULL);
$nPageID = (isset($_GET["nPageID"]) ? $_GET["nPageID"] : NULL);
	if(!$nPageID){ $nPageID = (isset($_POST["nPageID"]) ? $_POST["nPageID"] : NULL); }
$nRemove = (isset($_GET["nRemove"]) ? $_GET["nRemove"] : NULL);
	if(!$nRemove){ $nRemove = (isset($_POST["nRemove"]) ? $_POST["nRemove"] : NULL); }
$nEdit = (isset($_GET["nEdit"]) ? $_GET["nEdit"] : NULL);
	if(!$nEdit){ $nEdit = (isset($_POST["nEdit"]) ? $_POST["nEdit"] : NULL); }

// Initialize vars
$nSecureMe = $nModifyType = $nEditTable = $nHelperField = $nHelperFieldAdditional = $nHelperFieldSecondary = $nFieldEditARRAY = "";
$nRequiredARRAY = $nCheckDuplicateARRAY = $nFilterPriceCharsARRAY = $nFilterBadCharsARRAY = "";
$nDisplayAsYesNoARRAY = $nDisplayAsCustomDDARRAY = $nDisplayAsBigTextFieldARRAY = $nDisplayAsTextAreaARRAY = "";
$nRequiredARRAY = $nCheckDuplicateARRAY = $nCheckEmailARRAY = $nCheckPassARRAY = "";

switch($nPageID){
	case "order":
		$nSecureMe=2;
		$nModifyType = "Order";
		$nEditTable = "nOrderInfo";
		$nHelperField = "nOrderNum";
		$nHelperFieldAdditional = "nBillName";
		$nHelperFieldSecondary = "";
		$nFieldEditARRAY = array("nStatus" => "Status","nTrackNum" => "Tracking #1","nTrackNum2" => "Tracking #2","nNotes" => "PRIVATE Notes","nPubNotes" => "PUBLIC Notes","nInstructions" => "Special Instructions");
		// Error-checking
		$nRequiredARRAY = array();
		// Filters
		$nFilterBadCharsARRAY = array("nNotes","nPubNotes","nInstructions");
		// Display types
		$nDisplayAsCustomDDARRAY = array("SELECT nID AS nDDValue,nStatus AS nDDText FROM nOrderStatusTable" => "nStatus");
		$nDisplayAsTextAreaARRAY = array("nNotes","nPubNotes","nInstructions");
		break;
	case "prod":
		$nSecureMe=1;
		$nModifyType = "Product";
		$nEditTable = "nProdInfo";
		$nHelperField = "nProdName";
		$nHelperFieldAdditional = "nProdPrice";
		$nHelperFieldSecondary = "nProdSKU";
		$nFieldEditARRAY = array("nProdName" => "Product Name","nCat" => "Category","nProdBriefDesc" => "Brief Description","nProdPrice" => "Price","nSalePrice" => "Sale Price","nShipPrice" => "Extra Shipping Price","nProdWeight" => "Weight (.lbs)","nProdOption1Name" => "Option #1 - Name","nProdOption1Set" => "Option #1 - Options Set","nProdOption2Name" => "Option #2 - Name","nProdOption2Set" => "Option #2 - Options Set","nProdOption3Name" => "Option #3 - Name","nProdOption3Set" => "Option #3 - Options Set","nProdOption4Name" => "Option #4 - Name","nProdOption4Set" => "Option #4 - Options Set","nProdOption5Name" => "Option #5 - Name","nProdOption5Set" => "Option #5 - Options Set","nProdAvail" => "Availability","nAllowPurchase" => "Allow addition to cart?","nNew" => "New?","nBestSeller" => "Best Seller?","nShowAsList" => "Show Options as List of Separate Products?");
		// Error-checking
		$nRequiredARRAY = array("nProdName","nProdSKU","nProdPrice","nCat","nManuID");
		$nCheckDuplicateARRAY = array("nProdName","nProdSKU");
		// Filters
		$nFilterPriceCharsARRAY = array("nProdPrice","nSalePrice","nShipPrice","nCODiscountAmount");
		$nFilterBadCharsARRAY = array("nProdName","nProdSKU","nProdOption1Name","nProdOption1Set","nProdOption1Sku","nProdOption2Name","nProdOption2Set","nProdOption2Sku","nProdOption3Name","nProdOption3Set","nProdOption3Sku","nProdOption4Name","nProdOption4Set","nProdOption4Sku","nProdOption5Name","nProdOption5Set","nProdOption5Sku","nProdPrice","nSalePrice","nShipPrice","nCODiscountAmount");
		// Display types
		$nDisplayAsYesNoARRAY = array("nAllowPurchase","nNew","nBestSeller","nShowAsList");
		$nDisplayAsCustomDDARRAY = array("SELECT nID AS nDDValue,nCatName AS nDDText FROM nCatInfo" => "nCat","SELECT nID AS nDDValue,nAvail AS nDDText FROM nAvailTable" => "nProdAvail");
		$nDisplayAsBigTextFieldARRAY = array("nProdName","nProdOption1Set","nProdOption2Set","nProdOption3Set","nProdOption4Set","nProdOption5Set");
		$nDisplayAsTextAreaARRAY = array("nProdBriefDesc");
		break;
	case "cust":
		$nSecureMe=3;
		$nModifyType = "Customer";
		$nEditTable = "nCustomerInfo";
		$nHelperField = "nContactName";
		$nHelperFieldAdditional = "nCustID";
		$nHelperFieldSecondary = "nCustID";
		$nFieldEditARRAY = array("nCustID" => "Customer ID","nCompanyName" => "Company Name","nContactName" => "Contact Name","nContactPhone" => "Contact Phone","nLoginUser" => "Login Username","nLoginPass" => "Login Password","nCustTypeID" => "Customer Type","nNotes" => "Notes","nBillName" => "Billing Name","nBillAddress" => "Billing Address","nBillCity" => "Billing City","nBillState" => "Billing State","nBillZip" => "Billing ZIP Code","nBillCountry" => "Billing Country","nBillPhone" => "Billing Phone #","nBillFax" => "Billing Fax #","nBillEmail" => "Billing Email Address","nShipName" => "Shipping Name","nShipAddress" => "Shipping Address","nShipCity" => "Shipping City","nShipState" => "Shipping State","nShipZip" => "Shipping ZIP Code","nShipCountry" => "Shipping Country","nShipPhone" => "Shipping Phone #","nShipFax" => "Shipping Fax #","nShipEmail" => "Shipping Email Address","nEmailOptOut" => "Promotion Opt-Out","nActive" => "Active Status");
		// Error-checking
		$nRequiredARRAY = array("nActive","nCustID","nCustTypeID","nContactName","nContactPhone","nLoginUser","nLoginPass");
		$nCheckDuplicateARRAY = array("nCustID","nLoginUser");
		$nCheckEmailARRAY = array("nBillEmail","nShipEmail");
		$nCheckPassARRAY = array("nLoginPass");
		// Filters
		$nFilterBadCharsARRAY = array("nCustID","nContactName","nCompanyName","nContactPhone","nBillName","nBillAddress","nBillCity","nBillPhone","nBillFax","nBillEmail","nShipName","nShipAddress","nShipCity","nShipPhone","nShipFax","nShipEmail");
		// Display types
		$nDisplayAsYesNoARRAY = array("nEmailOptOut","nActive");
		$nDisplayAsCustomDDARRAY = array("SELECT nID AS nDDValue,nNameType AS nDDText FROM nCustomerTypeInfo" => "nCustTypeID",
		"SELECT nState AS nDDText,nAbbrev AS nDDValue FROM nStateInfo ORDER BY nState" => "nBillState",
		"SELECT nAbbrev AS nDDValue,nState AS nDDText FROM nStateInfo ORDER BY nState" => "nShipState",
		"SELECT nCountry AS nDDText,nAbbrev AS nDDValue FROM nCountryInfo" => "nBillCountry",
		"SELECT nAbbrev AS nDDValue,nCountry AS nDDText FROM nCountryInfo" => "nShipCountry");
		$nDisplayAsBigTextFieldARRAY = array("nBillAddress","nShipAddress");
		$nDisplayAsTextAreaARRAY = array("nNotes");
		break;
	case "custtype":
		$nSecureMe=3;
		$nModifyType = "Customer Type";
		$nEditTable = "nCustomerTypeInfo";
		$nHelperField = "nNameType";
		$nHelperFieldAdditional = "nID";
		$nHelperFieldSecondary = "nNameType";
		$nFieldEditARRAY = array("nNameType" => "Type","nTaxExempt" => "Tax Exempt","nAllowPO" => "Allow Purchase Orders");
		// Error-checking
		$nRequiredARRAY = array("nNameType");
		$nCheckDuplicateARRAY = array("nNameType");
		$nCheckEmailARRAY = array();
		$nCheckPassARRAY = array();
		// Filters
		$nFilterBadCharsARRAY = array("nNameType");
		// Display types
		$nDisplayAsYesNoARRAY = array("nTaxExempt","nAllowPO");
		$nDisplayAsCustomDDARRAY = array();
		$nDisplayAsBigTextFieldARRAY = array();
		$nDisplayAsTextAreaARRAY = array();
		break;
	case "disc": // just for removal
		$nSecureMe=5;
		$nModifyType = "Discount";
		$nEditTable = "nDiscountInfo";
		$nHelperField = "nDiscountType";
		$nHelperFieldAdditional = "nCode";
		$nHelperFieldSecondary = "nCode";
		$nFieldEditARRAY = array("nDiscountType" => "Type");
		// Error-checking
		$nRequiredARRAY = array("nDiscountType");
		$nCheckDuplicateARRAY = array();
		$nCheckEmailARRAY = array();
		$nCheckPassARRAY = array();
		// Filters
		$nFilterBadCharsARRAY = array("nDiscountType");
		// Display types
		$nDisplayAsYesNoARRAY = array();
		$nDisplayAsCustomDDARRAY = array();
		$nDisplayAsBigTextFieldARRAY = array();
		$nDisplayAsTextAreaARRAY = array();
		break;
	case "usr": // just for removal
		$nSecureMe=5;
		$nModifyType = "User";
		$nEditTable = "nEditor";
		$nHelperField = "nUser";
		$nHelperFieldAdditional = "nID";
		$nHelperFieldSecondary = "nUser";
		$nFieldEditARRAY = array("nUser" => "Username","nEmail" => "E-mail Address");
		// Error-checking
		$nRequiredARRAY = array("nUser","nEmail");
		$nCheckDuplicateARRAY = array("nUser");
		$nCheckEmailARRAY = array();
		$nCheckPassARRAY = array();
		// Filters
		$nFilterBadCharsARRAY = array("nUser","nEmail");
		// Display types
		$nDisplayAsYesNoARRAY = array();
		$nDisplayAsCustomDDARRAY = array();
		$nDisplayAsBigTextFieldARRAY = array("nUser","nEmail");
		$nDisplayAsTextAreaARRAY = array();
		break;
}

$nMessage = "";
$nAction = "";
if($nRemove){ $nAction = "Remove"; }
if($nEdit){ $nAction = "Edit"; }
// Secure this page
require("../inc/secure_page.php");
?>
<?
// Build item array from submitted info
$nTable = "";
$nDDOptions = "";
$nRefreshAndClose = "";
$nError = "";
$nShowCloseButton = "";
$nItemAmount = 0;
$nItemArray = array();
$nEditArray = array();
foreach($_GET as $key => $nItemID) {
	$pos = strpos($key,"item_");
	if($pos !== false){
		$nItemAmount++;
		$keyARRAY = explode("_",$key);
		$nItemKey = $keyARRAY[1];
		$nItemArray["$nItemKey"] = $nItemID;
		// Re-build form elements for re-posting
		$nTable.="<input type=\"checkbox\" name=\"$key\" value=\"$nItemID\" checked>";
	}
	$pos = strpos($key,"edit_");
	if($pos !== false){
		$keyARRAY = explode("_",$key);
		$nItemKey = $keyARRAY[1];
		$nEditArray["$nItemKey"] = $nItemID;
	}
}
foreach($_POST as $key => $nItemID) {
	$pos = strpos($key,"item_");
	if($pos !== false){
		$nItemAmount++;
		$keyARRAY = explode("_",$key);
		$nItemKey = $keyARRAY[1];
		$nItemArray["$nItemKey"] = $nItemID;
		// Re-build form elements for re-posting
		$nTable.="<input type=\"checkbox\" name=\"$key\" value=\"$nItemID\" checked>";
	}
	$pos = strpos($key,"edit_");
	if($pos !== false){
		$keyARRAY = explode("_",$key);
		$nItemKey = $keyARRAY[1];
		$nEditArray["$nItemKey"] = $nItemID;
	}
}

$nUseTextContainer = 0;
$nShowSaveButton = $nShowCancelButton = "";

if($nUpdateDB){
	if($nRemove){
		// Update DB
		switch($nPageID){
			case "order":
     		case "cust":
			case "custtype":
			case "disc":
			case "usr":
				foreach($nItemArray as $key => $nID) {
					$Query = "DELETE FROM $nEditTable WHERE nID=$nID LIMIT 1";
					mysql_select_db($DBName);
					mysql_query($Query, $Link);
				}
				break;
			case "prod":
				foreach($nItemArray as $key => $nID) {
					$Query = "SELECT nProdImgS,nProdImgL,nProdImgE FROM nProdInfo WHERE nID=$nID";
					mysql_select_db($DBName); $Result = mysql_query($Query,$Link);
					if ($Row = mysql_fetch_array($Result)) {
						// Remove files/images associated with record
						if($Row["nProdImgS"]){ $nFile = $Row["nProdImgS"]; if(file_exists("../$nFile")){ unlink("../$nFile"); } }
						if($Row["nProdImgL"]){ $nFile = $Row["nProdImgL"]; if(file_exists("../$nFile")){ unlink("../$nFile"); } }
						if($Row["nProdImgE"]){ $nFile = $Row["nProdImgE"]; if(file_exists("../$nFile")){ unlink("../$nFile"); } }
					}
					// Remove any related extended product prices
					$Query = "DELETE FROM nProdPriceInfo WHERE nProdID=$nID";
					mysql_select_db($DBName); mysql_query($Query, $Link);
					// Remove records
					$Query = "DELETE FROM nProdInfo WHERE nID=$nID LIMIT 1";
					mysql_select_db($DBName); mysql_query($Query, $Link);
					// Remove child records
					$Query = "DELETE FROM nProdInfo WHERE nCaseParentID=$nID";
					mysql_select_db($DBName); mysql_query($Query, $Link);
					// Remove related checkout special records
					$Query = "DELETE FROM nCheckoutSpecials WHERE nParentProdID=$nID";
					mysql_select_db($DBName); mysql_query($Query, $Link);
					// Remove Invetory Information of product
					$Query = "DELETE FROM nProdInventory WHERE nProdId=$nID";
					mysql_select_db($DBName); mysql_query($Query, $Link);
				}
				break;
		}
		$nRefreshAndClose=1;
		$nShowCloseButton=1;
	}
	if($nEdit){
		foreach($nFieldEditARRAY as $nFieldValue => $nFieldName){ if($nFieldValue==$nEditField){ $nEditKey=$nFieldName; } }
		
		if($nListShowing){
			// Perform any error checking, and save if OK
			$nErrorARRAY = array();
			foreach($nItemArray as $key => $nID) {
				// Check for required fields
				if ($nRequiredARRAY && in_array($nEditField,$nRequiredARRAY)){
					if($nEditArray["$key"]==""){
						$nRequiredFieldError = 1;
						$nErrorARRAY["$key"] = 1;
					}
				}
				// Check for duplicates, if needed
				if ($nCheckDuplicateARRAY && in_array($nEditField,$nCheckDuplicateARRAY)){
					if(nIsDuplicate($nID,$nEditArray["$key"],$nEditField)){
						$nError.="<li>The $nEditKey '".$nEditArray["$key"]."' is already used by another ".strtolower($nModifyType).".  Please try again.";
						$nErrorARRAY["$key"] = 1;
					}
				}
				// Check for a valid email format
				if ($nCheckEmailARRAY && in_array($nEditField,$nCheckEmailARRAY)){
					if($nEditArray["$key"]!="" && !validate_email($nEditArray["$key"])){
						$nError.="<li>The $nEditKey '".$nEditArray["$key"]."' is not a valid email address.  Please try again.";
						$nErrorARRAY["$key"] = 1;
					}
				}
				// Check for a valid password format
				if ($nCheckPassARRAY && in_array($nEditField,$nCheckPassARRAY)){
					if($nEditArray["$key"]!="" && !nIsValidPass($nEditArray["$key"])){
						$nError.="<li>The $nEditKey '".$nEditArray["$key"]."' must be between 5-15 characters and contain at least one number.  Please try again.";
						$nErrorARRAY["$key"] = 1;
					}
				}
				// Filter special characters
				if ($nFilterPriceCharsARRAY && in_array($nEditField,$nFilterPriceCharsARRAY)){
					$nEditArray["$key"] = nCleanPriceChars($nEditArray["$key"]);
				}
				if ($nFilterBadCharsARRAY && in_array($nEditField,$nFilterBadCharsARRAY)){
					$nEditArray["$key"] = nCleanBadChars($nEditArray["$key"]);
				}
				// If no error is found, update DB
				if(!$nError){
					// Make final cleanup before adding to database
					$nEditArray["$key"] = nCleanQuotes($nEditArray["$key"]);
					$Query = "UPDATE $nEditTable SET $nEditField='".$nEditArray["$key"]."' WHERE nID=$nID LIMIT 1";
					mysql_select_db($DBName);
					mysql_query($Query, $Link);
				}
			}
			if(!$nError){
				$nRefreshAndClose=1;
				$nShowCloseButton=1;
			} else {
				if($nRequiredFieldError){ $nError="<li>All fields are required. Please try again.".$nError; }
				// If error found, reset so list shows again, with error
				$nListShowing=0;
			}
		}
		if(!$nListShowing && $nHelperField && $nEditField){
			$nErrorARRAY = array();
			$nListTable = "";
			// Show list to edit
			foreach($nItemArray as $key => $nID) {
				if($nEditField==$nHelperField){ $nHelperField=$nHelperFieldSecondary; $nHelperFieldAdditional=""; }
				if($nHelperFieldAdditional){ $nAddToQuery=",$nHelperFieldAdditional AS nHelper2Row"; } else { $nAddToQuery=""; }
				$Query = "SELECT $nHelperField AS nHelperRow,$nEditField AS nEditRow$nAddToQuery FROM $nEditTable WHERE nID=$nID LIMIT 1";
				mysql_select_db($DBName); $Result = mysql_query($Query,$Link);
				if ($Row = mysql_fetch_array($Result)) {
					$nHelperRow = (isset($Row["nHelperRow"]) ? $Row["nHelperRow"] : NULL);
					$nHelper2Row = (isset($Row["nHelper2Row"]) ? $Row["nHelper2Row"] : NULL);
					if($nHelper2Row){ $nHelperRow = "$nHelperRow <span style=\"color:#898989;\">($nHelper2Row)</span>"; }
					if(count($nEditArray)){ $nEditRow = $nEditArray["$key"]; } else { $nEditRow = $Row["nEditRow"]; }
					if(isset($nErrorARRAY["$key"]) && $nErrorARRAY["$key"]==1){ $nStyle="color:red;"; } else { $nStyle=""; }
					// DISPLAY: Standard field with helper text
					$nAddToList="<tr><td align=\"right\" style=\"padding-right:30px;$nStyle\">$nHelperRow </td><td><input type=\"text\" name=\"edit_$key\" id=\"edit_$key\" value=\"$nEditRow\" class=\"contentForm\" style=\"width:125px;\" maxlength=\"100\"></td></tr>";
					// DISPLAY: Dropdown Yes/No
					if ($nDisplayAsYesNoARRAY && in_array($nEditField,$nDisplayAsYesNoARRAY)){
						$nAddToList=nGetYesNoDD("edit_$key",$nEditRow,$nHelperRow);
					}
					// DISPLAY: Dropdown Custom
					if ($nDisplayAsCustomDDARRAY && in_array($nEditField,$nDisplayAsCustomDDARRAY)){
						$nEditFieldKey = array_search($nEditField,$nDisplayAsCustomDDARRAY);
						$nAddToList=nGetCustomDD("edit_$key",$nEditRow,$nEditFieldKey,$nHelperRow);
					}
					// DISPLAY: Larger text field
					if ($nDisplayAsBigTextFieldARRAY && in_array($nEditField,$nDisplayAsBigTextFieldARRAY)){
						$nAddToList="<tr><td align=\"right\" style=\"padding-right:20px;$nStyle\">$nHelperRow </td><td><input type=\"text\" name=\"edit_$key\" id=\"edit_$key\" value=\"$nEditRow\" class=\"contentForm\" style=\"width:250px;\" maxlength=\"255\"></td></tr>";
					}
					// DISPLAY: Even larger textarea box
					if ($nDisplayAsTextAreaARRAY && in_array($nEditField,$nDisplayAsTextAreaARRAY)){
						$nAddToList="<tr><td align=\"right\" style=\"padding-right:20px;$nStyle\">$nHelperRow </td><td><textarea cols=\"50\" rows=\"4\" name=\"edit_$key\" id=\"edit_$key\" class=\"contentForm\" style=\"width:280px;font-size:11px;\">$nEditRow</textarea></td></tr>";
					}
					// Finally, put item into main display table
					$nListTable.=$nAddToList;
				}
				$nMessage = "<table align=\"center\"><tr><td class=\"content\">&nbsp;</td><td class=\"content\"><strong>$nEditKey</strong></td></tr>$nListTable</table>";
				$nUseTextContainer=0;
			}
			$nShowSaveButton=1;
			$nShowCancelButton=1;
			$nAction = " Save Changes ";
		}
	}
} else {
	if($nRemove){
		if($nItemAmount==1){
			$nMessage = "<strong>Are you sure you want to permanently remove this ".strtolower($nModifyType)."?</strong>";
		} else {
			$nMessage = "<strong>Are you sure you want to permanently remove these $nItemAmount ".strtolower($nModifyType)."s?</strong>";
		}
		$nUseTextContainer=1;
	}
	if($nEdit){
		foreach($nFieldEditARRAY as $nFieldValue => $nFieldName){ $nDDOptions.="<option value=\"$nFieldValue\">$nFieldName</option>"; }
		$nMessage = "<strong>Choose what to edit for these $nItemAmount ".strtolower($nModifyType)."s: <select name=\"nEditField\" class=\"contentForm\">$nDDOptions</select></strong>";
		$nUseTextContainer=1;
	}
	$nShowSaveButton=1;
	$nShowCancelButton=1;
	if($nUseTextContainer){ $nMessage="<div style=\"text-align:center;padding-top:70px;\">$nMessage</div>"; }
}
?>

<?php if(!$nRefreshAndClose){ ?>

<div id="<?=$nAddDialogLoader;?>">
<form action="<?=$_SERVER['PHP_SELF']?>" name="<?=$nAddFormName;?>" id="<?=$nAddFormName;?>" method="post" style="margin:0px;">
	<input type="hidden" name="nUpdateDB" value="1">
	<input type="hidden" name="nPageID" value="<?php echo $nPageID; ?>">
	<input type="hidden" name="nRemove" id="nRemove" value="<?php echo $nRemove; ?>">
	<input type="hidden" name="nEdit" id="nEdit" value="<?php echo $nEdit; ?>">
	<?php if($nShowSaveButton){ ?>
	<input type="hidden" name="nSave" id="nSave" value="1">
	<?php } ?>
	<?php if($nSave){ ?>
	<input type="hidden" name="nEditField" value="<?php echo $nEditField; ?>">
	<input type="hidden" name="nListShowing" value="1">
	<?php } ?>
	<div id="content" style="padding:10px 0px 0px 10px;">

		 
		  
		  <div style="height:186px; overflow: auto;">
			<? if($nError) { ?>
		  		<div style="border:1px solid #FFDA8A;background-color:#FFF2D7;padding:6px;" class="contentError"><ul style="margin-left:16px;"><?=$nError?></ul></div><BR>
			<? } ?>
		  
			<?php echo $nMessage; ?>
			<BR><BR>
		  </div>
		  
	</div>
	<div style="display:none;"><? echo $nTable; ?></div>
	<div style="text-align:center;">
		<?php if($nShowSaveButton){ ?><input type="button" name="<?=$nAddFormSubmitID;?>" id="<?=$nAddFormSubmitID;?>" value="<?php echo $nAction; ?>" class="blue button"> <?php } ?>
		<?php if($nShowCancelButton){ ?><input type="button" name="nCancel" id="nCancel" value=" Cancel " class="button" onClick="dialogClose();"> <?php } ?>
		<?php if($nShowCloseButton){ ?><input type="button" name="nClose" value=" Close " class="button" onClick="refreshParentAndClose();"> <?php } ?>
	</div>
</form>
</div>

<script>
// On form submission...
$('#<?=$nAddFormSubmitID;?>').click(function () { 
	var form = $('#<?=$nAddFormName;?>');
	// Process form
	var formData = $(form).serializeObject();
	// Save form via AJAX
	$("#<?=$nAddDialogLoader;?>").html(loaderImgS).load("dialog/multiple_edit.php", formData, function(response, status, xhr) {
		if (status != "error") {
			//if(response == "1"){
			if(response){
				$("#<?=$nAddDialogLoader;?>").html(response);
				// additional processing, if needed
				<? if($nRemove || ($nEdit && $nUpdateDB)){ ?>
					try{ nLoadCustomersTable(); }catch(e){}
					try{ nLoadCategoriesTable(); }catch(e){}
					try{ nLoadCustomerTypesTable(); }catch(e){}
					try{ nLoadOrdersTable(); }catch(e){}
					try{ nLoadProductsTable(); }catch(e){}
					try{ nLoadDiscountsTable(); }catch(e){}
					try{ nLoadUsersTable(); }catch(e){}
					try{ showNotify('The <strong><?php echo $nModifyType;?></strong> list has been updated with your changes.','done') }catch(e){};
					dialogClose();
				<? } ?>
			} else { alert("There was an error saving your changes: "+response); }
		} else {
			 alert("An error occurred while performing your request: " + xhr.status + " " + xhr.statusText);
		}
	});
	return false;
});
</script>

<?php } ?>

<?php
function nIsDuplicate($nID,$nDupValue,$nEditField){
	global $DBName, $Link;
	global $nEditTable;
	$isDuplicate = 0;
	$Query = "SELECT nID FROM $nEditTable WHERE $nEditField='$nDupValue' AND nID!=$nID AND nCaseParentID=0";
	mysql_select_db($DBName); $Result = mysql_query($Query,$Link);
	while ($Row = mysql_fetch_array($Result)) {
		$isDuplicate = 1;
	}
	return $isDuplicate;
}
function validate_email($email) { 
	$nIsValid = TRUE;
	if(preg_match("/^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$/i", $email)) {
		$nIsValid = TRUE;
	} else {
		$nIsValid = FALSE;
	}
	return $nIsValid; 
}
function nIsValidPass($nPassValue){
	if(strlen($nPassValue)<5 || strlen($nPassValue)>15){ return 0; }
	$nNumExists = 0;
	for($x=0;$x<=9;$x++){ if(strpos($nPassValue,"$x")!==false){ $nNumExists = 1; } }
	if(!$nNumExists){ return 0; }
	return 1;
}
function nGetYesNoDD($nSelectName,$nSelectedItem,$nHelperRow){
	$nYesNoDDARRAY = array("1" => "Yes","0" => "No");
	$nAddToList="<tr><td align=\"right\" style=\"padding-right:20px;\">$nHelperRow </td><td><select name=\"$nSelectName\" class=\"contentForm\">";
	foreach($nYesNoDDARRAY as $nYesNoValue => $nYesNoText) {
		if($nSelectedItem==$nYesNoValue){ $nSelected="SELECTED"; } else { $nSelected=""; }
		$nAddToList.="<option value=\"$nYesNoValue\" $nSelected>$nYesNoText</option>";
	}
	$nAddToList.="</select></td></tr>";
	return $nAddToList;
}
function nGetCustomDD($nSelectName,$nSelectedItem,$nSelectQuery,$nHelperRow){
	global $DBName, $Link;
	$nAddToList="<tr><td align=\"right\" style=\"padding-right:20px;\">$nHelperRow </td><td><select name=\"$nSelectName\" class=\"contentForm\">";
	if($nSelectedItem==""){ $nAddToList.="<option value=\"\" SELECTED></option>"; }
	mysql_select_db($DBName); $Result = mysql_query($nSelectQuery,$Link);
	while ($Row = mysql_fetch_array($Result)) {
		if($nSelectedItem==$Row["nDDValue"]){ $nSelected="SELECTED"; } else { $nSelected=""; }
		$nAddToList.="<option value=\"".$Row["nDDValue"]."\" $nSelected>".$Row["nDDText"]."</option>";
	}
	$nAddToList.="</select></td></tr>";
	return $nAddToList;
}
function nCleanQuotes($nItemValue){
	$nItemValue = str_replace("\"","&quot;",$nItemValue);
	return $nItemValue;
}
function nCleanPriceChars($nItemValue){
	$notAllowed = array("$",",");
	$nItemValue = str_replace($notAllowed,"",$nItemValue);
	return $nItemValue;
}
function nCleanBadChars($nItemValue){
	$notAllowed = array("<",">","~");
	$nItemValue = str_replace($notAllowed,"",$nItemValue);
	return $nItemValue;
}
?>
<?
// disconnect from mysql
mysql_close($Link);
?>
