<?php
	Require("../../Includes/i_Includes.php");
	Require("../../Includes/Nav/PHP_JK_ADMIN_OPEN.php");
	Require("../../Includes/Nav/PHP_JK_ADMIN_DEFAULT.php");
	
	If ((ACCNT_ReturnRights("PHPJK_MCONF_DELETE")) || (ACCNT_ReturnRights("PHPJK_MCONF_UPDATE"))) {
		HeaderHTML();
		Main();
	}Else{
		WriteScripts();
		DOMAIN_Message("You must login with Domains rights.<br>", "ERROR");
	}
	Require("../../Includes/Nav/PHP_JK_ADMIN_CLOSE.php");
	
	
	//************************************************************************************
	//*																					*
	//*																					*
	//*																					*
	//************************************************************************************
	Function Main()
	{
		$sAction		= Trim(Request("sAction"));
		$sError			= "";
		$sSuccess		= "";
		
		If ( $sAction == "DeleteConfiguration" ) {
			If ( ! ACCNT_ReturnRights("PHPJK_MCONF_DELETE")) {
				$sError = "You must login with Domains rights.<br>";
			}Else{
				ForEach ($_POST as $sCheckbox=>$sValue)
				{
					If ( strpos($sCheckbox, "sDeleteConfConst") !== false ) {
						$sConfConst	= str_replace("sDeleteConfConst", "", $sCheckbox);
						DB_Update ("DELETE FROM Configurations WHERE ConfConst = '" . SQLEncode($sConfConst) . "'");
						If ( $sSuccess == "" ) 
							$sSuccess = "The configuration variables have been deleted.<br>";
					}
				}
			}
		}ElseIf ( $sAction == "UpdateConfiguration" ) {
				If ( ! ACCNT_ReturnRights("PHPJK_MCONF_UPDATE")) {
					$sError = "You must login with Domains rights.<br>";
				}Else{
					ForEach ($_POST as $sTextField=>$sValue)
					{
						If ( strpos($sTextField, "sOldConfConst") !== false ) {
							$sOldConfConst		= FixFormData($sValue);
							$sConfConst			= str_replace("sOldConfConst", "", $sTextField);
							$sOldValue			= Request("sOldValue" . $sConfConst);
							$sNewValue			= Request("sNewValue" . $sConfConst);
							If ( $sOldValue != $sNewValue ) {
								DB_Update ("UPDATE Configurations SET Value = '" . SQLEncode($sNewValue) . "' WHERE ConfConst = '" . SQLEncode($sOldConfConst) . "'");
								If ( $sSuccess == "" )
									$sSuccess = "Your configuration variables have been updated.";
							}
						}
					}
				}
		}
		
		If ( $sError != "" )
			DOMAIN_Message($sError, "ERROR");
		If ( $sSuccess != "" )
			DOMAIN_Message($sSuccess, "SUCCESS");
		
		WriteScripts();
		WriteForm();
		
	}
	//************************************************************************************
	

	//************************************************************************************
	//*																					*
	//*	This is the function to write out the form.										*
	//*																					*
	//************************************************************************************
	Function WriteForm()
	{
		Global $aVariables;
		Global $aValues;
		
		$sBGColor	= $GLOBALS["BGColor2"];
		$sTextColor	= $GLOBALS["TextColor2"];
		
		$sQuery			= "SELECT * FROM Configurations (NOLOCK) ORDER BY ConfConst";
		$rsRecordSet	= DB_Query($sQuery);
		?>
		<center>
		<form name='ManageConfConsts' action='index.php' method='post'>
		<?php 
		$aVariables[0] = "sAction";
		$aValues[0] = "New";
		Echo DOMAIN_Link("P");
		
		DOMAIN_Link_Clear();
		 ?>
		<table cellpadding=0 cellspacing=0 border=0 width=671>
			<tr><td><img src='../../Images/Blank.gif' Width=1 Height=5></td></tr>
			<tr>
				<td>
					<font color='<?=$GLOBALS["PageText"]?>'>
					<font size=+1><b>Manage Configuration Variables</b></font>
					<br><br>
					<table cellpadding=2 cellspacing=0 border=0 width=671>
						<tr>
							<td bgcolor=<?=$sBGColor?>><font color='<?=$GLOBALS["TextColor2"]?>'><b>Configuration Constant</b></td>
							<td bgcolor=<?=$sBGColor?>><font color='<?=$GLOBALS["TextColor2"]?>'><b>Value</b></td>
							<td bgcolor=<?=$sBGColor?>>&nbsp;</td>
							<td align=center bgcolor=<?=$sBGColor?>><font color='<?=$GLOBALS["TextColor2"]?>'><b>Delete</b></td>
						</tr>
						<?php 
						while ( $rsRow = DB_Fetch($rsRecordSet) )
						{
							$sConfConst		= $rsRow["ConfConst"];
							$sDescription	= $rsRow["Description"];
							$sValue			= $rsRow["Value"];
							$sViewRightsLvl	= Trim($rsRow["RightLvlv"]);
							$sEditRightsLvl	= Trim($rsRow["RightLvlc"]);
							$sTempType		= Trim($rsRow["Type"]);

							If (( ACCNT_ReturnRights($sEditRightsLvl) ) || ( $sEditRightsLvl == "" )) {
								If ( $sBGColor == $GLOBALS["BGColor1"] ) {
									$sBGColor	= $GLOBALS["PageBGColor"];
									$sTextColor	= $GLOBALS["PageText"];
									$sLinkColor	= "MediumNavPage";
								}Else{
									$sBGColor	= $GLOBALS["BGColor1"];
									$sTextColor	= $GLOBALS["TextColor1"];
									$sLinkColor	= "MediumNav1";
								}
								 ?>
								<tr>
									<td bgcolor=<?=$sBGColor?>>
										<input type='hidden' name="sOldConfConst<?=htmlentities($sConfConst)?>" value="<?=htmlentities($sConfConst)?>">
										<font color='<?=$sTextColor ?>'><b><?=FixFormData($sConfConst)?>
									</td>
									<td bgcolor=<?=$sBGColor?>>
										<input type='hidden' name="sOldValue<?=htmlentities($sConfConst)?>" value="<?=htmlentities($sValue)?>">
										<input type='text' name="sNewValue<?=htmlentities($sConfConst)?>" value="<?=htmlentities($sValue)?>" size=30 maxlength=250>
									</td>
									<td bgcolor=<?=$sBGColor?>><a href="Edit.php?<?=DOMAIN_Link("G")?>&sConfConst=<?=htmlentities($sConfConst)?>" class='<?=$sLinkColor ?>'>More...</a></td>
									<td align=center bgcolor=<?=$sBGColor?>><input type='checkbox' name="sDeleteConfConst<?=htmlentities($sConfConst)?>" value="<?=htmlentities($sConfConst)?>"></td>
								</tr>
								<tr>
									<td colspan=4 bgcolor=<?=$sBGColor?>><font color='<?=$sTextColor ?>' size=-2><?=$sDescription ?></td>
								</tr>
								<?php 
							}ElseIf (( ACCNT_ReturnRights($sViewRightsLvl) || ( $sViewRightsLvl == "" )) ) {
								If ( $sBGColor == $GLOBALS["BGColor1"] ) {
									$sBGColor	= $GLOBALS["PageBGColor"];
									$sTextColor	= $GLOBALS["PageText"];
								}Else{
									$sBGColor	= $GLOBALS["BGColor1"];
									$sTextColor	= $GLOBALS["TextColor1"];
								}
								?>
								<tr>
									<td bgcolor=<?=$sBGColor?>><font color='<?=$sTextColor ?>'><?=$sConfConst ?><br></td>
									<td bgcolor=<?=$sBGColor?>><font color='<?=$sTextColor ?>'><?=$sValue ?></td>
									<td bgcolor=<?=$sBGColor?>>&nbsp;</td>
									<td align=center bgcolor=<?=$sBGColor?>>&nbsp;</td>
								</tr>
								<tr>
									<td colspan=4 bgcolor=<?=$sBGColor?>><font color='<?=$sTextColor ?>' size=-2><?=$sDescription ?></td>
								</tr>
								<?php 
							}
						}
						?>
					</table>
					<br>
					<font size=-2>
					<b>WARNING: Changing or deleting Configuration Variables can render the system unusable!</b> 
					Please keep a record of all changed or deleted Configuration Variables.
				</td>
			</tr>
			<tr><td><img src='../../Images/Blank.gif' Width=1 Height=5></td></tr>
		</table>
		</form>
		<?php 
	}
	//************************************************************************************
	
	
	//************************************************************************************
	//*																					*
	//*	This writes the JavaScript out even if the Main() Sub isn't called in case the	*
	//*		admin has creation rights, but not edit rights.								*
	//*																					*
	//************************************************************************************
	Function WriteScripts()
	{
		?>
		<script language='JavaScript1.2' type='text/javascript'>
	
			function SubmitForm(sAction){
				document.ManageConfConsts.sAction.value = sAction;
				document.ManageConfConsts.submit();
			}
			
		</script>
		<?php 
	}
	//************************************************************************************
	
	
	//************************************************************************************
	//*																					*
	//*	This checks to see if the configuration variable they are trying to add is 		*
	//*		already in the DB.															*
	//*																					*
	//************************************************************************************	
	Function DoesntExist($sConfConst)
	{
		$sQuery			= "SELECT * FROM Configurations (NOLOCK) WHERE ConfConst = '" . SQLEncode($sConfConst) . "'";
		$rsRecordSet	= DB_Query($sQuery);
		if ( $rsRow = DB_Fetch($rsRecordSet) )
			Return False;
			
		Return True;
	}
	//************************************************************************************	
	
	
	
	//************************************************************************************
	//*																					*
	//*																					*
	//************************************************************************************
	Function HeaderHTML()
	{
		Global $aVariables;
		Global $aValues;
		
		?>
		<table cellpadding=0 cellspacing=0 border=0 width=671>
			<tr>
				<td bgcolor=<?=$GLOBALS["BorderColor2"]?>><img src='../../Images/Blank.gif' Width=1 Height=2></td>
				<td colspan=8 bgcolor=FFFFFF width=23><img src='../../Images/Blank.gif' Width=23 Height=4></td>
				<td bgcolor=<?=$GLOBALS["BorderColor2"]?>><img src='../../Images/Blank.gif' Width=1 Height=2></td>
			</tr>
			<tr>
				<td bgcolor=<?=$GLOBALS["BorderColor2"]?>><img src='../../Images/Blank.gif' Width=1 Height=2></td>
				<td bgcolor=FFFFFF width=23><img src='../../Images/Blank.gif' Width=23 Height=2></td>
				<?php
				If (ACCNT_ReturnRights("PHPJK_MCONF_CREATE_NEW")) {
					Echo "<td bgcolor=FFFFFF width=1><a href='New.php'><img src='../../Images/Administrative/NewConfiguration.gif' Width=22 Height=39 Border=0 Alt='Create a new Configuration Variable.'></a></td>";
				}Else{
					Echo "<td bgcolor=FFFFFF width=1>&nbsp;</td>";
				}
				
				Echo "<td bgcolor=FFFFFF width=50><img src='../../Images/Blank.gif' Width=50 Height=2></td>";
				If (ACCNT_ReturnRights("PHPJK_MCONF_DELETE")) {
					Echo "<td bgcolor=FFFFFF width=1><input type='image' name='delete' SRC='../../Images/Administrative/DeleteConfiguration.gif' ALIGN='absmiddle' Width=27 Height=39 Border=0 Alt='Delete all Configuration Variables with checked checkboxes next to them.' onClick='SubmitForm(\"DeleteConfiguration\")'></td>";
				}Else{
					Echo "<td bgcolor=FFFFFF width=1>&nbsp;</td>";
				}
				
				Echo "<td bgcolor=FFFFFF width=50><img src='../../Images/Blank.gif' Width=50 Height=2></td>";
				If (ACCNT_ReturnRights("PHPJK_MCONF_UPDATE")) {
					Echo "<td bgcolor=FFFFFF width=1><input type='image' name='update' SRC='../../Images/Administrative/UpdateConfiguration.gif' ALIGN='absmiddle' Width=31 Height=44 Border=0 Alt='Update all Configuration Variables with changes on this page.' onClick='SubmitForm(\"UpdateConfiguration\")'></td>";
				}Else{
					Echo "<td bgcolor=FFFFFF width=1>&nbsp;</td>";
				}
				?>
				<td bgcolor=FFFFFF width=50><img src='../../Images/Blank.gif' Width=50 Height=2></td>
				<td bgcolor=FFFFFF width=100%>&nbsp;</td>
				<td bgcolor=<?=$GLOBALS["BorderColor2"]?>><img src='../../Images/Blank.gif' Width=1 Height=2></td>
			</tr>
			<tr>
				<td bgcolor=<?=$GLOBALS["BorderColor2"]?>><img src='../../Images/Blank.gif' Width=1 Height=2></td>
				<td colspan=8 bgcolor=FFFFFF width=23><img src='../../Images/Blank.gif' Width=23 Height=2></td>
				<td bgcolor=<?=$GLOBALS["BorderColor2"]?>><img src='../../Images/Blank.gif' Width=1 Height=2></td>
			</tr>
			<tr>
				<td colspan=10 bgcolor=<?=$GLOBALS["BorderColor2"]?>><img src='../../Images/Blank.gif' Width=2 Height=1></td>
			</tr>
		</table>
		<?php
	}
	//************************************************************************************
 ?>