<?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;
}


include "../includes/htmltop.php";

	$select_template_edit = "SELECT vtemplate_name,vcss_name,vcss_original FROM ".$tableprefix."template_master 
						 WHERE ntemplate_id = ".GetSQLValueString($_GET['edit_id'],"text");
						
    $result_template_edit = mysql_query($select_template_edit) or die(mysql_error()); 
	
	
			
if(isset($_POST['update']))
{


	/*check whether template with the same name exists in the database*/
		
	$select_template = "SELECT * FROM ".$tableprefix."template_master 
						WHERE LOWER(vtemplate_name) = ".GetSQLValueString(strtolower($_POST['txtTemplateName']),"text").
						" AND ntemplate_id != ".GetSQLValueString($_GET['edit_id'],"text");
	$result_template = mysql_query($select_template) or die(mysql_error());
	
	if(mysql_num_rows($result_template)>0)
	{
	
		$message = TNAME_EXIST;
	}
	else if($_POST['file_content']=="")
	{
		$message = CSS_EMPTY;
	}
	else
	{	
	
		
		
			/*$update_template = "UPDATE ".$tableprefix."template_master
								SET vtemplate_name = ".GetSQLValueString($_POST['txtTemplateName'],"text").
								" WHERE ntemplate_id = ".GetSQLValueString($_GET['edit_id'],"text");
			mysql_query($update_template) or die(mysql_error());*/								  

			header("Location:templates.php?".$_GET['qstring']."&txtSearch=".$_GET['txtSearch']."&msg=S");								  
	}						  

}

?>
<script>
function checkAddTemplateForm()
{

  var form_name = new Array('txtTemplateName');		
  var display_name = new Array('<?php echo TEMP_NAME;?>');
  var count = 1;	

  if(check_fields('frmEditTemplate',form_name,display_name,count))	
  {
  
  	
	 //document.frmAddBanner.submit();	
   
  }
  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 -->
                                        <?php
                                        /*
					<div align="left"><?php include("../includes/templatemenu.php");?></div>
                                         */
                                        ?>
							
					<div align="left" class="page_title"><?php echo EDIT_TEMP;?></div>
					<div align="center" class="text_information grey_tr"><?php echo $message?></div>

            		<br>
											
					<div align="left">																			
																									
					
																									
					<table cellpadding="0" cellspacing="0" width="100%" class="manage_table">
					<form name="frmEditTemplate" action="#" method="POST" onSubmit="return checkAddTemplateForm();">
						<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>
						
<?php

if(mysql_num_rows($result_template_edit) > 0)
{
    
	$template_edit_row = mysql_fetch_assoc($result_template_edit);
	$filename_original = "../themes/".$template_edit_row['vtemplate_name']."/style_original.css";
	$filename_restore = "../themes/".$template_edit_row['vcss_name'];
	
	 $themesFolder	="../themes/".$template_edit_row['vtemplate_name']."/style.css";
	 
	 if (!is_readable($themesFolder) || !is_writable($themesFolder) || !is_executable($themesFolder)) {
        $error = true;
		$path= "themes/".$template_edit_row['vtemplate_name']."/style.css";
        
		$message = PERMISSION_NOTICE." <br>";
		$message = str_replace("PATH", $path, $message);
        // $message = " * Change the permission of  themes/".$template_edit_row['vtemplate_name']." in the root   folder  to 777 ,  Once you finished editing style sheet   set  permission of  themes/".$template_edit_row['vtemplate_name']." in the root   folder  to 644 <br><br>";
 

 		 
 
    }
	
	
	if(!file_exists($filename_original))
	{
	   	
		@copy($filename_restore,$filename_original);
		
	}
	
	if(isset($_POST['cssRestore']))
	{
		
		$filename_original = "../themes/".$template_edit_row['vtemplate_name']."_original.css";
		$filename_restore = "../themes/".$template_edit_row['vcss_name'];
		if(file_exists($filename_original)&&file_exists($filename_restore))
		{
			$handle_original = fopen($filename_original,'r+');
			$handle_restore = fopen($filename_restore,'w+');
			
			if(filesize($filename_original)>0)
			{
					
				$contents_original = fread($handle_original, filesize($filename_original));			
								
				@fwrite($handle_restore,$contents_original);
				@rewind($handle_restore);
				
				$restore_message = CSS_RESTORED;	
			}	
							
			
			
		}
		
		
	
	}

?>						
						
				
				<?php if($restore_message!='')
				{
				?>		
						<tr>
							<td colspan="3" class="text_information grey_tr" align="center"><?php echo $restore_message?></td>
						</tr>
				<?php } ?>		
				<?php if($message!='')
				{
				?>		
						<tr>
							<td colspan="3" class="text_information grey_tr" align="center"><?php echo $message?></td>
						</tr>
				<?php } ?>		
				
				
						<tr>
							<td colspan="3">&nbsp;</td>
						</tr>
						<tr>
							<td width="17%" align="left" valign=top><?php echo TEMP_NAME;?>&nbsp;<span class="required">*</span></td>
							<td width="2%" >&nbsp;</td>
							<td width="81%" align="left" valign=top>
								<input class="textbox" type="text" name="txtTemplateName" value="<?php echo $template_edit_row['vcss_name'];?>" maxlength="50">
							</td>
						</tr>
						
<?php
    
	 $filename = "../themes/".$template_edit_row['vcss_name'];
	 
	
	if(file_exists($filename))
	{
		$handle = @fopen($filename,'r+');
				
		if(isset($_POST['update']))
		{
		
				
			@fwrite($handle,$_POST['file_content']);
			@rewind($handle);		
		}
		
		if(filesize($filename)>0)
		{
				
			$contents = @fread($handle, filesize($filename));			
		}	
				
		@fclose($handle);
	
	}
?>						
						
						
						<tr>
							<td width="17%" align="left" valign=top><?php echo CSS_CONTENT;?>&nbsp;<span class="required">*</span></td>
							<td width="2%" >&nbsp;</td>
							<td width="81%" align="left" valign="top">
								<textarea name="file_content" rows="20" cols="40"><?php echo $contents?></textarea>
							</td>
						</tr>
						
						
						
						
						
<!--						
						
						<tr>
						  <td align="left" valign=top>Top File Name&nbsp;<span class="required">*</span></td>
						  <td >&nbsp;</td>
							<td valign=top align="left"><input class="textbox" type="text" name="txtTopFile" value="" size="25" maxlength="50"></td>
						</tr>
						<tr><td align="left" valign=top>Left File Name&nbsp;<span class="required">*</span></td><td >&nbsp;</td>
							<td valign=top align="left"><input class="textbox" type="text" name="txtLeftFile" value="" size="25" maxlength="50">&nbsp;
							</td>
						</tr>
						<tr><td align="left" valign=top>Bottom File Name&nbsp;<span class="required">*</span></td><td >&nbsp;</td>
							<td valign=top align="left"><input class="textbox" type="text" name="txtBottomFile" value="" size="25" maxlength="50">&nbsp;
							</td>
						</tr>
						
-->						

<?php

}

?>
						
						<tr><td colspan="3">&nbsp;</td></tr>
						<tr>
							<td colspan="3" align="center">
                                                            <input type="button" class="button" name="back" value="&nbsp;<?php echo BACK;?>&nbsp;" onClick="window.location='templates.php'">
							<input type="submit" value="<?php echo UPDATE;?>" name="update" class="button">
							
							</td>
						</tr>
						</form>
						<form name="frmRestore" method="post">
						<tr>
							<td width="17%" align="left"><?php echo RESTORE_CSS;?></td>
							<td width="2%" >&nbsp;</td>
							<td><a style="cursor:pointer" onClick="Javascript:document.frmRestore.submit();"><img title="Restore" src="../images/restore_css.jpg"></a>
								<input type="hidden" name="cssRestore">
							</td>
						</tr>
						</form>
						<tr><td colspan="3">&nbsp;</td></tr>
					</table>
				
				
				</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();
?>