<?php
require_once ("../config.inc.php");
require_once ("../includes/adminlogincheck.inc.php");
require_once ("../includes/mysql.inc.php");
require_once ("../includes/arrays.inc.php");

$page		= isset($_REQUEST['page'])	?	trim($_REQUEST['page'])	:	"1";
$id			= isset($_REQUEST['id'])	?	trim($_REQUEST['id'])	:	"0";
$message	= "";

if (!$objDB) 
{
	$message	= "Unable to Locate Database Server.";
}//end if
else 
{
	$hid = $_GET['hid'];
	
	if (intval($hid) != 0)
	{
		$sqlEdit = " SELECT * FROM ".TABLEPREFIX."Help WHERE hid='$hid'";
		$rsEdit = mysql_query($sqlEdit);
		if($arrEdit= mysql_fetch_array($rsEdit))
		{
 			@extract($arrEdit);
			$txtTitle=$arrEdit['htitle'];
			$txtDes=$arrEdit['hdescription'];
			$chkActive=$arrEdit['active'];
			$ddlCategory=$arrEdit['hcid'];
			$assignedname=$arrEdit['himage'];
		}//end if
	}//end first if

	//if category is blank
	if($ddlCategory=='')
	{
		$ddlCategory=$_GET['ddlCategory'];
	}//end if
        if(isset($_POST['BackNow']) && $_POST['BackNow']=='Back')
        {
	header('location:help_admin.php');
	exit();
        }//     end if

	//add or update category
	if(isset($_POST['Call']) && $_POST['Call']=='save')
	{
			@extract($_POST);
			
                        $hid = $_POST['hid'] ;
                        $ddlCategory = $_POST["ddlCategory"];
                        $txtTitle = $_POST["txtTitle"];
                        $txtDes = $_POST["txtDes"];
                        $chkActive =  $_POST["chkActive"];
                        $assignedname =  $_POST["assignedname"];
			
	
			if($_FILES['hfile']['name']!='')
			{
				//if file already exits remove
				@unlink('../help/'.$assignedname);
				
				$name=$_FILES['hfile']['name'];
				@chmod('../help',0777);
				$assignedname=time().'_'.$name;
				@copy($_FILES['hfile']['tmp_name'],'../help/'.$assignedname);
				
			}//end if
			else
			{
					$assignedname=$assignedname;
			}//end else
	
			if($hid!='')
			{	
				$sqlUpdate = " UPDATE ".TABLEPREFIX."Help SET htitle='".addslashes($txtTitle)."',active='".addslashes($chkActive)."',
								 hcid='".addslashes($ddlCategory)."',hdescription='".addslashes($txtDes)."',
								 himage='".addslashes($assignedname)."' WHERE hid='$hid' ";
								 
				mysql_query($sqlUpdate) or die(mysql_error());
				$message = "<font color='green'>Help updated successfully.</font>";
			}
			else
			{
				 $sql = mysql_query("SELECT MAX(hposition) as max from ".TABLEPREFIX."Help") or die(mysql_error());
				 $rw = mysql_fetch_array($sql);
				 $maxorder = $rw['max']+1;
				 
				$sqlIns = " INSERT INTO ".TABLEPREFIX."Help (hcid,htitle,hdescription,hposition,active,himage) 
							VALUES ('".addslashes($ddlCategory)."','".addslashes($txtTitle)."',
							'".addslashes($txtDes)."','$maxorder','".addslashes($chkActive)."','".addslashes($assignedname)."') ";
							
				mysql_query($sqlIns) or die(mysql_error());
				$message = "<font color='green'>Help added successfully.</font>";
			}

		header('location:help_admin.php?ddlCategory='.$ddlCategory);
		exit();
	}//end if
	//$message
}//end else

include_once('title.inc.php');
?>
<script language="javascript" src="../js/validation.js" type="text/javascript"></script>
<script language="javascript">
function category_chk()
{

		var errorstr = '';
		var msgstr = "Sorry, we cannot complete your request.\nPlease provide us the missing or incorrect information enclosed below.\n\n";
		
		with(document.form1)
		{ 	
			if (trim(document.form1.ddlCategory.value)=="") { errorstr += "- Please enter Category.\n";}
			else if (trim(document.form1.txtTitle.value)=="") { errorstr += "- Please enter Title.\n";}
			else if (trim(document.form1.txtDes.value)=="") { errorstr += "- Please enter Description.\n";}
			<?php if($hid==''){?>
				else if (trim(document.form1.hfile.value)=="") { errorstr += "- Please select a Help Image.\n";}
			<?php }?>
			
			
		}	
		
		if (errorstr != '')
		{
			msgstr = msgstr + errorstr;
			alert(msgstr);
			return false;
		}
		else
		{
			if(document.form1.himage.value!='')
			{
      	 		 UploadFile();
			}//end if
			return true;
		}	
}
function showPrograsBar()
{
	FreezePage();
	document.getElementById('uploadimageloader').style.display='inline';
		//PrograsBar =window.open("prograssbar.php",'welcome','resizable=no,width=0,height=0,screenX=150,screenY=150,top=150,left=150');

}
function closePrograsBar(){
		if (PrograsBar && PrograsBar.open && !PrograsBar.closed) PrograsBar.close();
}

function UploadFile(){
      showPrograsBar();
}
function FreezePage()
{
	document.getElementById("parentDiv").style.width = window.screen.width;
	document.getElementById("parentDiv").style.height = window.screen.height*20;
	document.getElementById("parentDiv").style.display = 'inline';

	ShowIndicator();
}

function ShowIndicator()

{
	document.getElementById("waitIndicatorDiv").style.display = 'block';
}
function change_sitemap(){
	document.form1.img1.src=document.form1.himage.value;
}

</script>
<?php include_once('tophtml.php');?>
<table width="100%"  border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td valign="top" class="white"><table width="100%"  border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td width="72%" valign="top">
			<table width="100%"  border="0" cellspacing="0" cellpadding="5">
          <tr>
            <td><table width="100%"  border="0" cellspacing="0" cellpadding="5">
              <tr>
                <td width="41%" class="headings1"><?php if($hid!=''){?>
              Edit
                <?php }else{?>
                Add New
                <?php }?>
              Admin Help</td>
              </tr>
            </table>
			
			<div class="bordernew padding2">
<div id="parentDiv" class="transparent" style="position:absolute; background-color:Gray; 
width:100%; height:500%; display:none; top:0px; left:0px; z-index:5000;">
</div>


<div id="waitIndicatorDiv" class="Indicator">
<table id="uploadimageloader" width="50%" style="display:none;" align="center">
	<tr>
		<td align="center"><img src="../images/loading.gif"></td>
	</tr>
</table>
</div>

		<form name="form1" method="POST" action="<?php echo $PHP_SELF?>" enctype="multipart/form-data">
  <input type=hidden name='Call' value="save">
  <input name='hid' type=hidden id="hid" value="<?php echo $hid?>">
  <input name='assignedname' type=hidden id="assignedname" value="<?php echo $assignedname?>">
  <input name='ddlCategory' type=hidden id="ddlCategory" value="<?php echo $ddlCategory;?>">
          <table width="100%"  border="0" cellspacing="0" cellpadding="5">
            <tr>
              <td><table width="100%"  border="0" cellspacing="0" cellpadding="0" class="tblestyle_admin2">
				<?php if ($objDB) 
					  { 
				?>
					<?php 	if(isset($message) && $message!='')
							{
					?>
                <tr align="left">
                  <td align="center" class="maintext2">&nbsp;</td>
                  <td colspan="3" class="maintext2 warning"><?php echo $message;?></td>
                  </tr>
					<?php  	}//end if?>
  				<tr align="left">
                  <td width="3%" class="maintext2">&nbsp;</td>
                  <td width="27%" valign="top" class="maintext2">Category <span class="warning">*</span></td>
                  <td colspan="2" valign="top" class="maintext2"><select name="ddlCategory" class="textbox">
		<?php
			//fectch category from Help category table
			$category=mysql_query("select * from ".TABLEPREFIX."HelpCategory where htype='admin' and active='1' order by hcposition desc") or die(mysql_error());
			if(mysql_num_rows($category)>0){
				while($arr=mysql_fetch_array($category))
				{?>
				<option value="<?php echo $arr['hcid'];?>" <?php if($ddlCategory==$arr['hcid']){echo 'selected';}?>><?php echo $arr['hctitle'];?></option>
				<?php
					}//end while
				}//end if
				else{
					echo '<option value="Nil">NIL</option>';
				}//end if?>
				</select></td>
  				</tr>
                 <tr align="left">
                  <td class="maintext2">&nbsp;</td>
                  <td valign="top" class="maintext2">Title <span class="warning">*</span></td>
                  <td colspan="2" valign="top" class="maintext2"><input name="txtTitle" type="text" class="textbox3" id="txtTitle" value="<?php echo $txtTitle?>" size="35" maxlength="100"></td>
                 </tr>
                 <tr align="left">
                   <td class="maintext2">&nbsp;</td>
                   <td valign="top" class="maintext2">Description <span class="warning">*</span></td>
                   <td colspan="2" valign="top" class="maintext2"><textarea name="txtDes" cols="65" rows="15" class="textbox_textarea txt-area-big"><?php echo htmlentities($txtDes);?></textarea></td>
                 </tr>
                 <tr align="left">
                   <td class="maintext2">&nbsp;</td>
                   <td valign="top" class="maintext2">Help Icon <span class="warning">*</span></td>
                   <td width="31%" valign="top" class="maintext2"><input type="file" name="hfile" class="textbox3" onChange="change_sitemap();">
	  <br><br><span class="warning">Image size should be 22 x 20</span></td>
                   <td width="39%" valign="top" class="maintext2"><?php if($assignedname!=''){
	  echo ' <img src="../help/'.$assignedname.'" name="img1" width="22" height="20">';}//end if
	  else{echo ' <img src="images/default_album.gif" name="img1" width="22" height="20">';}//end else?></td>
                 </tr>
                <tr align="left">
                  <td class="maintext2">&nbsp;</td>
                  <td valign="top" class="maintext2">Active</td>
                  <td colspan="2" valign="top" class="maintext2"><input name="chkActive" type="radio" value="1" <?php if($chkActive=='1'){echo 'checked';}if($chkActive==''){echo 'checked';}?>>
						      Yes <input type="radio" name="chkActive" value="0" <?php if($chkActive=='0'){echo 'checked';}?>>No</td>
                </tr>
				<?php 			
						}//end first if
				?>
              </table>
                <table width="100%"  border="0" cellspacing="0" cellpadding="0">
                  <tr>
                    <td align="right"><table width="100%"  border="0" cellspacing="0" cellpadding="0">
                      <tr>
                        <td>&nbsp;</td>
                      </tr>
                    </table>
                      <?php if ($objDB) 
					  { 
					?>
                      <table width="100%"  border="0" cellspacing="0" cellpadding="5">
                      <tr>
                        <td align="left"><table width="100%"  border="0" cellspacing="0" cellpadding="0">
                            <tr>
								<td width="31%">&nbsp;</td>
								<td width="5%"><input name="BackNow" type="submit" class="back-btn" value="Back"></td>
                              <td><input class="loginbutton" type="submit" value="<?php if($hid!=''){?>Edit<?php }else{?>Add<?php }?> Admin Help"  onclick="return category_chk()" name="submit2"></td>
                              <?php //if ($hcid > 0) { ?>
                              
								<?php //}//end if?>
                              </tr>
                        </table></td>
                      </tr>
                    </table>
					<?php 		
						}//end first if
				?></td>
                  </tr>
                </table></td>
            </tr>
          </table></form>
		  </div>
			
			</td>
          </tr>
        </table>

		  </td>
        </tr>
    </table>      
    </td>
  </tr>
</table>
<?php include_once('bottomhtml.php');?>