<?php

session_start();
if($_SESSION['dbName']==""){
header("location:../index.php");
}


include("config.inc.php");

$connection = mysql_connect($config['dbServer'],$config['dbUser'],$config['dbPass']) or die("Could not connect to DB");
mysql_select_db($_SESSION['dbName'],$connection) or die("Could not find DB");
	



$FinancialTot_bgcolor = "#FFFFFF";
if( $FinancialTot_fontcolor == '') $FinancialTot_fontcolor = "#000000";
if( $FinancialTot_fontsize == '') $FinancialTot_fontsize = 15;
		
//Read From Setup Table							
$querysetup= " select grid_color1, grid_color2 from setup ";
$resultsetup=mysql_query($querysetup,$connection) or die(mysql_error());
while($rowsetup=mysql_fetch_array($resultsetup)){
	extract($rowsetup);
}

//// Reading Main Label info

if ($financial_code != "" && $mode!='insertdt'){

	
	$query_mainLabel= " select FinancialTot_Label, FinancialTot_RelatedCode ,FinancialTot_Sign,FinancialTot_ShowResult,
						FinancialTot_Level,
						FinancialTot_LevelCode, ifnull(FinancialTot_bgcolor,'#FFFFFF'), ifnull(FinancialTot_fontcolor,'#000000'), ifnull(FinancialTot_fontsize,15),
						FinancialTot_order, FinancialTot_RelatedPer
						from financialtotlabel
						where FinancialTot_code='".$financial_code."'";
	//echo $query_mainLabel;
	$result_mainLabel = mysql_query($query_mainLabel, $connection);
	$row_mainLabel=mysql_fetch_row($result_mainLabel);
		
		$financial_label=$row_mainLabel[0];
		$financial_relatedcode=$row_mainLabel[1];
		$financialtot_sign=$row_mainLabel[2];
		$financialtot_showresult=$row_mainLabel[3];
		$financialtot_level=$row_mainLabel[4];
		$FinancialTot_bgcolor=$row_mainLabel[6];
		$FinancialTot_fontcolor=$row_mainLabel[7];
		$FinancialTot_fontsize=$row_mainLabel[8];
		$FinancialTot_order=$row_mainLabel[9];
		$financial_relatedPer=$row_mainLabel[10];
}


/// Drop down of related labels
$financialtot_leveltop=$financialtot_level-1;
$queryfinancialrelated = "select FinancialTot_Code, FinancialTot_Label from FinancialTotLabel 
						where FinancialTot_Code not in (
						select distinct FinancialTotLabel.FinancialTot_Code
						from Financialtemplate ,FinancialTotLabel
						where Financialtemplate.FinancialTot_code=FinancialTotLabel.FinancialTot_code)
						and FinancialTot_TreeCode='".$financial_treecode."'";
					
	if($mode=='editdt')						
		$queryfinancialrelated = $queryfinancialrelated." and financialtot_level='".$financialtot_leveltop."'";
	$queryfinancialrelated = $queryfinancialrelated." order by FinancialTot_Label	";

$resultfinancialrelated = mysql_query($queryfinancialrelated, $connection) or die(mysql_error());

$queryfinancialrelated2 = "select FinancialTot_Code, FinancialTot_Label from FinancialTotLabel 
						where   FinancialTot_TreeCode='".$financial_treecode."'  order by FinancialTot_Label	";

/// Initializations
if ($mode=='') $mode = 'insert';

print <<<HERE
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1256" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> 
		<meta name="viewport" content="width=device-width, initial-scale=1.0"> 
<script type="text/javascript" src="simpletreemenu.js">

/***********************************************
* Simple Tree Menu- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
***********************************************/

</script>


<script type="text/javascript" language="javascript">
  
function setfinancialtot_sign(){
	var x = document.financialreport;
	if (x.chckfinancialtot_sign.checked==true)
		x.financialtot_sign.value=1;
	else	
	x.financialtot_sign.value=-1;
}

function setfinancialtot_showresult(){
	var x = document.financialreport;

	if (x.chckfinancialtot_showresultstatus.checked==true)
		x.financialtot_showresult.value=1;
	else	
	x.financialtot_showresult.value=0;
}

function setfinancialtot_skiprows(){

	var x = document.financialreport;
	if (x.chckfinancialtot_skiprowsstatus.checked==true)
		x.financialtot_skiprows.value=1;
	else	
		x.financialtot_skiprows.value=0;
}

function setfinancialtot_bold(){
	var x = document.financialreport;
	if (x.chckfinancialtot_boldstatus.checked==true)
		x.financialtot_bold.value=1;
	else	
		x.financialtot_bold.value=0;
}

function setfinancialtot_background(){
	var x = document.financialreport;
	if (x.chckfinancialtot_backgroundstatus.checked==true)
		x.financialtot_background.value=1;
	else	
		x.financialtot_background.value=0;
}

function set_related(){
	var x = document.financialreport;
	x.financial_relatedcode.value=x.financial_relateddesc.value;
}


//function SearchForDetail(){
//	x = document.financialreport;
 //   win = window.open('Form_FinancialAccountsSearch.php', 'Form_FinancialAccountsSearch', 'toolbar=no,location=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width=775,height=450,top=100,left=100');		
//	win.creator = self;
//	win.focus();		
//}

function SubmitRecord() {
	
 	x = document.financialreport;
	
	 if (x.financial_label.value =="")
	{
		alert("Label is mandatory!");
		x.financial_label.focus();
		return 0;
	}
	
	else if(x.mode.value=='editdt' && x.financial_relatedcode.value==''){
		alert("Related is mandatory!");
		x.financial_relateddesc.focus();
		return 0;
	}

	else{
	x.action="Form_FinancialAccounts_Actions.php";
	x.submit();
	}
}

function ClearPage(){

        if (confirm ( "Are you sure you want to Clear Page?"))
	{
		ClearForm('All');
		
		x.action='Form_FinancialAccounts.php';
		x.submit();
	}	
}

function ClearForm(type){

		x = document.financialreport;
		x.financial_code.value='';
		x.financial_label.value='';
		x.financialtot_level.value='';
		x.financialtot_sign.value='';
		x.financialtot_showresult.value='';
		x.FinancialTot_bgcolor.value='';
		x.FinancialTot_fontcolor.value='';
		x.FinancialTot_fontsize.value='';
		//x.financialtot_skiprows.value='';
		///x.financialtot_bold.value='';
		///x.financialtot_background.value='';
	
		x.message.value='';
		
		if(type=='All')
			x.mode.value='';

}

function AddDetail(code1,code2) {
	
    x = document.financialreport;
	x.mode.value= 'insertdt';
	ClearForm('notall');
	x.financial_relatedcode.value=code1;
	x.action="Form_FinancialAccounts.php";
	x.submit();
}	

function AddDetailLedger(Code) {
	
	financialdt_code= Code;

	win = window.open('Form_FinancialAccountsDetailLedgers.php?financialdt_code='+financialdt_code, 'Form_FinancialAccountsDetail', 'toolbar=no,location=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width=750,height=500,top=100,left=100');		
	win.creator = self;
	win.focus();
}	

 function UpdateDetail(code,code1){
 	x = document.financialreport;
	x.mode.value= 'editdt';
	x.financial_treecode.value=code1;
	x.financial_code.value=code;
	x.action="Form_FinancialAccounts.php?financialdt_code="+code;
	x.submit();
}

 function UpdateMaster(code){
 	x = document.financialreport;
	x.mode.value='edit';
	x.financial_code.value=code;
	x.action="Form_FinancialAccounts.php?financialdt_code="+code;
	x.submit();
}


function DeleteDetail(code){
	if (confirm ( "Are you sure you want to Delete This Record ?")){
		x = document.financialreport;
		x.mode.value='deletedt';
		x.financial_code.value=code;
		
		x.action="Form_FinancialAccounts_Actions.php";
		x.submit();
	}
}

function DeleteMaster(code){
	if (confirm ( "Are you sure you want to Delete This Label and All its Details?")){
		x = document.financialreport;
		x.mode.value='deletetop';
		x.financial_code.value=code;
		x.action="Form_FinancialAccounts_Actions.php";
		x.submit();
	}
}

HERE;


print <<<HERE
</script>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<LINK REL="stylesheet" TYPE="text/css" MEDIA="all" HREF="css/tabs.css">

<link rel="stylesheet" type="text/css" media="all" href="css-v2/style.css">
<title>Financial Accounts</title>


<script type="text/javascript" src="dropdown.js"></script>
<link rel="stylesheet" type="text/css" href="simpletree.css" />

<style type="text/css">

body,td,th {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 11px;
}
body {
	margin-top: 2px;
	margin-left: 1px;
}
a:link {
	text-decoration: none;
	color: #0000FF;
}
a:visited {
	text-decoration: none;
	color: #0000FF;
}
a:hover {
	text-decoration: none;
	color: #0000FF;
}
a:active {
	text-decoration: none;
	color: #0000FF;
}

.style12 {
	font-size: 9px;
	color: #999999;
	font-weight: bold;
}

.style15 {
	font-size: 10px;
	color: #404040;
	font-weight: bold;
	font-family: "Verdana, Arial, Helvetica, sans-serif";
}
.style16 {
	font-size: 10px;
	color: #888888;
	font-weight: bold;
	font-family: "Verdana, Arial, Helvetica, sans-serif";
}

.style17 {font-size: 9px}
.style25 {color: #999999; font-weight: bold; font-size: 10px; }
.style26 {color: #FF0000}
.style27 {color: #999999 ;font-size: 10px;}
.style28 {color: #FFFFFF ;font-size: 10px;}

</style>

<link type="text/css" rel="stylesheet" href="dhtmlgoodies_calendar.css?random=20051112" media="screen"></LINK>
<SCRIPT type="text/javascript" src="dhtmlgoodies_calendar.js?random=20060118"></script>

</head>

<body >


<form name='financialreport' method='post'>
<input type='hidden' name='financial_code'  value='$financial_code'/>
<input type='hidden' name='financial_treecode'  value='$financial_treecode'/>
<input type='hidden' name='mode' value='$mode'>
<input type='hidden' name='message' value='$message'>
<input type='hidden' name='financialtot_level' value='$financialtot_level' />
<input type='hidden' name='financial_relatedcode' value='$financial_relatedcode' />
HERE;

//$style= "style=' background-color:#4069A7; color:#FFFFFF;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px;'";

echo"<table cellpadding='0' cellspacing='0' border='0'  align='center' class='layout-width'>";
include_once("header.php");
echo" <tr><td  valign='top'>";

include_once("sidemenu.php");
echo"  </td></tr>";


echo" <tr style='height:26px;'>
		<td  valign='top' id='content-top'>
			<table border='0' cellpadding='0' cellspacing='0' style='table-layout:fixed; height:26px;' class='layout-width'>
			<tr style='height:26px;'>
				<td style='width:38px; height:26px;'>&nbsp;</td>
				<td valign='middle' style='width:300px; height:26px;'><span class='title'>Financial Accounts</span></td>
				<td class='message' valign='middle' style='height:26px;'>".htmlspecialchars($message, ENT_QUOTES)."</td>
			</tr></table>
		</td>
	  </tr>";


echo"<tr>
		<td  height='34'  valign='top' id='content-top2'>
			<table border='0' cellpadding='0' cellspacing='0' class='layout-width'>
			<tr>
				<td width='38'  height='34'>&nbsp;</td>
				<td valign='middle' height='34'>";
echo"		<button type='button' id='btnClear' name='btnClear' onclick='ClearPage(); return false;' title='Clear Page'><span><span>Clear Page</span></span></button>";


echo"		<button type='button' id='btnPost' name='btnPost' onclick='SubmitRecord(); return false;' title='Save Changes' $Insmode><span><span>Save</span></span></button>";


				
echo"   </td></tr>";
echo"	</table>";
echo"</td></tr>";

echo"<tr><td id='content-body'>";		
echo"<table border='0'  align='center' valign='top' width=100% class='tablecontent'>

<col width=90>
<col width=200>";	
echo"<tr bgcolor='#F7F7F7' height='25'><td valign='center' align='left' ><span class='style1'>Label <span class='style26'></span></span></td>";
echo"<td colspan='5' align='left'><input type='text' name='financial_label' value='".htmlspecialchars($financial_label, ENT_QUOTES)."' style='width:350px; ' maxlength='50' /></td>

</tr>";

echo"<tr bgcolor='#F7F7F7' height='25'><td><span class='style1'>Level&nbsp;</span>";
echo"<font face='Verdana' size='2'><b>$financialtot_level</b></font></span></td>";




	if ($financialtot_sign == '') 
		$financialtot_sign = 1;
	
	if ($financialtot_showresult == '') 
		$financialtot_showresult = 0;
	
	//if ($financialtot_skiprows == '') 
	//	$financialtot_skiprows = 0;
	
	//if ($financialtot_bold == '') 
	//	$financialtot_bold = 0;
		
	//if ($financialtot_background == '') 
	//	$financialtot_background = 0;
		
	if ($financialtot_sign == 1) $financialtot_signstatus = 'checked'; 
	if ($financialtot_showresult == 1) $financialtot_showresultstatus = 'checked'; 
	//if ($financialtot_skiprows == 1) $financialtot_skiprowsstatus = 'checked'; 
	//if ($financialtot_bold == 1) $financialtot_boldstatus = 'checked'; 
	//if ($financialtot_background == 1) $financialtot_backgroundstatus = 'checked'; 
	
	
		
echo"<td valign='center' align='left'>";
	echo"<input type='checkbox' name='chckfinancialtot_sign' $financialtot_signstatus onclick='setfinancialtot_sign()';/>
	<span class='style1'>Sign[+ -]&nbsp;<span class='style26'></span></span>
	<input type='hidden' name='financialtot_sign' id='financialtot_sign' value='$financialtot_sign'/>		
	</td>";
	echo"<td colspan='6'>";
		
	if($financialtot_level=='2'){
		echo"<input type='checkbox' name='chckfinancialtot_showresultstatus' $financialtot_showresultstatus onclick='setfinancialtot_showresult()'/>
		<span class='style1'>Show Result[Profit & Loss]&nbsp;<span class='style26'></span></span>";
	}
	echo"<input type='hidden' name='financialtot_showresult' id='financialtot_showresult' value='$financialtot_showresult'/>";
	echo"</td></tr>";
	
	echo "<tr height=30><td><span class='style1'>Background Color</span></td>
			<td><input type='color' name='FinancialTot_bgcolor' id='FinancialTot_bgcolor' value='$FinancialTot_bgcolor' style='width:120px;'/></td>
		</tr>";
	echo "<tr height=30><td><span class='style1'>Font Color</span></td>
			<td><input type='color' name='FinancialTot_fontcolor' id='FinancialTot_fontcolor' value='$FinancialTot_fontcolor' style='width:120px;'/></td>
		</tr>";
	echo "<tr height=30><td><span class='style1'>Font Size</span></td>
			<td><input type='number' name='FinancialTot_fontsize' id='FinancialTot_fontsize' value='$FinancialTot_fontsize' style='width:120px;'/></td>
		</tr>";
		
	///Related code
	if($mode=='editdt'){
		echo"<tr bgcolor='#F7F7F7' height='25'><td><span class='style1'>Related</td>
		<td valign='center' align='left' colspan='7'><select name='financial_relateddesc' style='width: 250px;' onchange=set_related();";
		echo"> ";
		echo"<option></option>";
		while($relatedfin=mysql_fetch_row($resultfinancialrelated)){
			echo"<option ";
			if ($financial_relatedcode==$relatedfin[0])
				echo " selected ";
			echo" value='$relatedfin[0]'>$relatedfin[1] </option>";
		}
		echo"</select></td></tr>";
		echo"<tr bgcolor='#F7F7F7' height='25'><td><span class='style1'>Related %</td>
		<td valign='center' align='left' colspan='7'><select name='financial_relatedPer' style='width: 250px;'  ";
		echo"> ";
		echo"<option></option>";
		$resultfinancialrelated = mysql_query($queryfinancialrelated2, $connection);
		while($relatedfin=mysql_fetch_row($resultfinancialrelated)){
			echo"<option ";
			if ($financial_relatedPer==$relatedfin[0])
				echo " selected ";
			echo" value='$relatedfin[0]'>$relatedfin[1] </option>";
		}
		echo"</select></td></tr>";
		$querylvl = "select FinancialTot_order from financialtotlabel where FinancialTot_RelatedCode=$financial_relatedcode order by FinancialTot_order;";
		echo "<tr><td><span class='style1'>Order</span></td>
				<td><select name='FinancialTot_order' id='FinancialTot_order' >";
		$reslvl = mysql_query($querylvl,$connection);
		while($rowlvl = mysql_fetch_row($reslvl)){
			echo "<option value='$rowlvl[0]' ".($rowlvl[0]==$FinancialTot_order?'selected':'').">$rowlvl[0]</option>";
		}
		echo "</select></td></tr>";
	}	
	else if($mode=='insertdt'){
	
		$query_relatedlabel="select FinancialTot_Label from financialtotlabel
							where FinancialTot_code='".$financial_relatedcode."'";
							
		$result_relatedlabel = mysql_query($query_relatedlabel, $connection);
		$row_related=mysql_fetch_row($result_relatedlabel);
		
	
	echo"<tr bgcolor='#F7F7F7' height='25'><td><span class='style1'>Related</span></td><td colspan='7' align='left'>";
	echo"<font face='Verdana' size='2'><span class='style1'><b>$row_related[0]</b></span></font>";
	echo"</td>";
}	
	
	echo"</tr>";
	
	//echo"<input type='hidden' name='financialtot_skiprows' id='financialtot_skiprows' value='$financialtot_skiprows'/>";
	//echo"<input type='hidden' name='financialtot_bold' id='financialtot_bold' value='$financialtot_bold'/>";
	//echo"<input type='hidden' name='financialtot_background' id='financialtot_background' value='$financialtot_background'/>";		
	//echo"<td>";
	//echo"<input type='checkbox' name='chckfinancialtot_skiprowsstatus' $financialtot_skiprowsstatus onclick='setfinancialtot_skiprows()'/>
	//<span class='style1'>SkipRows&nbsp;<span class='style26'></span></span>
			
	///</td>";
	//echo"<td>";
	//echo"<input type='checkbox' name='chckfinancialtot_boldstatus' $financialtot_boldstatus onclick='setfinancialtot_bold()'/>
	//<span class='style1'>Bold&nbsp;<span class='style26'></span></span>
	
	//</td>";
	//echo"<td>";
	//echo"<input type='checkbox' name='chckfinancialtot_backgroundstatus' $financialtot_backgroundstatus onclick='setfinancialtot_background()'/>
	//<span class='style1'>Background&nbsp;<span class='style26'></span></span>
	
	///</td>
	
	
/*	echo"<tr><td colspan='9'><table border=0 align=left width=100%>";
	echo"<tr bgcolor=#D8D8D8 height='35'> ";				
	echo"<td align=center>";
	echo"<input type='button' id='btnClear' name='BtnClear' value='Clear Page'   onclick='ClearPage();' $style title='Clear Page'>&nbsp;";
	echo"<input type='button' name='btnPost' id='btnPost' value='Save' ";//post
	echo" onclick='SubmitRecord();' $style	title='Save Changes' />&nbsp;";


	echo"</td>";
	echo"  </tr></table></td></tr>";*/
	
print<<<HERE
<tr height='30'><td colspan='3' align='left'>
	<a href="javascript:ddtreemenu.flatten('treemenu1', 'expand')"><font color=#606060  >Expand All</font></a> | <a href="javascript:ddtreemenu.flatten('treemenu1', 'contact')"><font color=606060 >Collapse All</font></a>	
</td></tr>
HERE;




/// Getting all top labels
echo "<tr><td colspan='6' align='left'>";
$query="select FinancialTot_code,FinancialTot_Label from financialtotlabel
			where financialtot_relatedcode is null";
$result = mysql_query($query, $connection);
	echo"<div><ul id='treemenu1' class='treeview'>";
$x=0;
$j=1;

while($rows=mysql_fetch_array($result)){

	$financialtotcode=$rows[0];
	
//Getting max level
$querycount="select max(FinancialTot_Level) from FinancialTotlabel where FinancialTot_TreeCode='".$financialtotcode."'";
$resultdtc = mysql_query($querycount, $connection);
$rowsdtc=mysql_fetch_array($resultdtc);
$numoflevels = $rowsdtc[0];

/// Getting max level with ledger
$query_level="select distinct FinancialTot_Level from FinancialTotLabel where FinancialTot_TreeCode='".$financialtotcode."'
				and FinancialTot_code in(select FinancialTot_code from Financialtemplate)";
$result_level= mysql_query($query_level, $connection);
$rows_level=mysql_fetch_array($result_level);
$maxlevelsledger = $rows_level[0];				
				
	

	if($j%2==0)
	$stylecolor="style='background-color:".$grid_color2."'";
	else			
		$stylecolor="style='background-color:".$grid_color1."'";	
				
	
	$querydt="select a.financialtot_label,a.financialtot_code, a.financialtot_relatedcode,a.FinancialTot_LevelCode,a.FinancialTot_Level,
			if(a.FinancialTot_Sign='1','&#10003;','X')
			,if(a.FinancialTot_ShowResult='1','&#10003;','X'),
			
			(select count(*) from financialtotlabel where financialtot_relatedcode=a.financialtot_code)
			
			from financialtotlabel as A left join FinancialTotlabel as B
	on (B.FinancialTot_Level = a.FinancialTot_Level and B.FinancialTot_Code = a.FinancialTot_Code ) or (B.FinancialTot_Level > a.FinancialTot_Level and B.FinancialTot_LevelCode like concat(a.FinancialTot_levelCode,'%'))
	Where a.FinancialTot_TreeCode = '".$financialtotcode."'
	And a.FinancialTot_Level > 1
	Group By a.FinancialTot_Label, a.FinancialTot_Code
	Order By A.FinancialTot_LevelCode Asc, A.FinancialTot_Code,A.Financialtot_level";
	//echo $querydt;
	$resultdt = mysql_query($querydt, $connection);
	
	if($j!='1')
		echo"<li></li>";
	
	echo"<li $stylecolor><span><img src='img/button_edit.png'  onMouseOver=\"style.cursor='hand'\" onclick=UpdateMaster('".$rows[0]."') title='Edit Record'></span>&nbsp;
					<img src='img/c_delete.png'  onMouseOver=\"style.cursor='hand'\" onclick=DeleteMaster('".$rows[0]."') title='Delete Record'>&nbsp;";

					
					if($maxlevelsledger!=''){
					
						if($maxlevelsledger>$rowsdt[4])
							echo"<img src='img/add.png' width='15' onMouseOver=\"style.cursor='hand'\" onclick=AddDetail('".$rows[0]."','".$rows[0]."') title='Add Detail' >&nbsp;";
					}
					else{
						echo"<img src='img/add.png'  width='15'  onMouseOver=\"style.cursor='hand'\" onclick=AddDetail('".$rows[0]."','".$rows[0]."') title='Add Detail'>&nbsp;";

					}
						
					echo"<span class='style15'><font color=red><b>".$rows[1]."</b></font></span>&nbsp;&nbsp;&nbsp;&nbsp; 
					";
					
					echo "<ul>";
					
	$Vtotcode='';			
	
	$i=1;
	$Ni=0;
	$Nu=0;
	$Vlevel=1;
	while($rowsdt=mysql_fetch_array($resultdt)){		
			
			
			
			if ($rowsdt[4] > $Vlevel) {
			
				echo"&nbsp;&nbsp;&nbsp;&nbsp;<li $stylecolor><img src='img/button_edit.png'  onMouseOver=\"style.cursor='hand'\" onclick=UpdateDetail('".$rowsdt[1]."','".$rows[0]."') title='Edit Record'>&nbsp;
					<img src='img/c_delete.png'  onMouseOver=\"style.cursor='hand'\" onclick=DeleteDetail('".$rowsdt[1]."') title='Delete Record'>&nbsp;";
					$Ni+=1;
					if($maxlevelsledger!=''){
					
						if($maxlevelsledger>$rowsdt[4])
							echo"<img src='img/add.png'  width='15'  onMouseOver=\"style.cursor='hand'\" onclick=AddDetail('".$rowsdt[1]."','".$rows[0]."') title='Add Detail'>&nbsp;";
					}
					else{
						echo"<img src='img/add.png'  width='15'  onMouseOver=\"style.cursor='hand'\" onclick=AddDetail('".$rowsdt[1]."','".$rows[0]."') title='Add Detail'>&nbsp;";
					}
					
					///LEdger detail
					//if($numoflevels==$rowsdt[4])
						echo"<span class='style15'><img src='img/addledger.png'  width='15' onMouseOver=\"style.cursor='hand'\" onclick=AddDetailLedger('".$rowsdt[1]."') title='Add Detail Ledger'  ></span>";

						
					echo"<span class='style15'>(L".$rowsdt[4].")&nbsp;".$rowsdt[0]."</span>&nbsp;&nbsp;&nbsp;&nbsp;
					<span class='style15'></span>";
					
					if ($rowsdt[7] > 0){
						echo"<ul $stylecolor >";
						$Nu+=1;
										}
			}
			else
			if ($rowsdt[4] < $Vlevel) {
					
					$Abs= abs($rowsdt[4] - $Vlevel);
					
					while($Abs>0)
						{
							echo "</ul></li>";	
							$Abs-=1;
							$Nu-=1;
							$Ni-=1;
						}
				
					echo"&nbsp;&nbsp;&nbsp;&nbsp;<li $stylecolor><img src='img/button_edit.png'  onMouseOver=\"style.cursor='hand'\" onclick=UpdateDetail('".$rowsdt[1]."','".$rows[0]."') title='Edit Record'>&nbsp;
								<img src='img/c_delete.png'  onMouseOver=\"style.cursor='hand'\" onclick=DeleteDetail('".$rowsdt[1]."') title='Delete Record'>&nbsp;";
							$Ni+=1;
								if($maxlevelsledger!=''){
									if($maxlevelsledger>$rowsdt[4])
										echo"<img src='img/add.png'  width='15'  onMouseOver=\"style.cursor='hand'\" onclick=AddDetail('".$rowsdt[1]."','".$rows[0]."') title='Add Detail'>&nbsp;";
								}
								else{
									echo"<img src='img/add.png'  width='15'  onMouseOver=\"style.cursor='hand'\" onclick=AddDetail('".$rowsdt[1]."','".$rows[0]."') title='Add Detail'>&nbsp;";
								}
								
								/// Check if this is last level,=>show ledger button
								//if($numoflevels==$rowsdt[4])
									echo"<span class='style15'><img src='img/addledger.png'  width='15' onMouseOver=\"style.cursor='hand'\" onclick=AddDetailLedger('".$rowsdt[1]."') title='Add Detail Ledger'  ></span>";
								
								echo"<span class='style15'>(L".$rowsdt[4].")&nbsp;".$rowsdt[0]."</span>&nbsp;&nbsp;&nbsp;&nbsp; 
								<span class='style15'></span>";
								
					if ($rowsdt[7] > 0){
						echo"<ul $stylecolor >";
						$Nu+=1;
										}
			}
			else if ($rowsdt[4] == $Vlevel) {
						echo"</li>";
						$Ni-=1;
						echo"&nbsp;&nbsp;&nbsp;&nbsp;<li $stylecolor><img src='img/button_edit.png'  onMouseOver=\"style.cursor='hand'\" onclick=UpdateDetail('".$rowsdt[1]."','".$rows[0]."') title='Edit Record'>&nbsp;
							<img src='img/c_delete.png'  onMouseOver=\"style.cursor='hand'\" onclick=DeleteDetail('".$rowsdt[1]."') title='Delete Record'>&nbsp;";
						$Ni+=1;
						if($maxlevelsledger!=''){
						if($maxlevelsledger>$rowsdt[4])
							echo"<img src='img/add.png'  width='15'  onMouseOver=\"style.cursor='hand'\" onclick=AddDetail('".$rowsdt[1]."','".$rows[0]."') title='Add Detail'>&nbsp;";
					}
					else{
						echo"<img src='img/add.png'  width='15'  onMouseOver=\"style.cursor='hand'\" onclick=AddDetail('".$rowsdt[1]."','".$rows[0]."') title='Add Detail'>&nbsp;";
					}	
					
					/// Check if this is last level,=>show ledger button
					//if($numoflevels==$rowsdt[4])
						echo"<span class='style15'><img src='img/addledger.png' width='15' onMouseOver=\"style.cursor='hand'\" onclick=AddDetailLedger('".$rowsdt[1]."') title='Add Detail Ledger'  ></span>";

					echo"<span class='style15'>(L".$rowsdt[4].")&nbsp;".$rowsdt[0]."</span>&nbsp;&nbsp;&nbsp;&nbsp; 
					<span class='style15'></span>";
					
						if ($rowsdt[7] > 0){
						echo"<ul $stylecolor >";
						$Nu+=1;
										}
									}
			
			
			
			$Vlevel=$rowsdt[4];
			}
			
	while ($Nu>0)		
		{
			echo"</ul></li>";
			
			$Nu-=1;
		}
		
	echo"</ul></li>";

	$Nu=0;
	$j=$j+1;
}
echo"</div></td></tr></table></td></tr>";	
 echo" <tr height='30'><td align='center' id='content-bottom' ></td></tr>";
include("footer.php");	

print <<<HERE
<script type="text/javascript">

//ddtreemenu.createTree(treeid, enablepersist, opt_persist_in_days (default is 1))

ddtreemenu.createTree("treemenu1", true)
</script>
HERE;


print <<<HERE

</body>
</html>
HERE;
?>
		  