<?php
//DB wrapper functions

function dbConnect($host, $user, $pword, $name){
    mysql_connect($host, $user, $pword);
    mysql_select_db($name);
}

function executeQuery($query){
    return mysql_query($query);   
}

function totalRecords($result){
   return mysql_num_rows($result);
}

function getRecordId(){
   return mysql_insert_id();
}

function getAssoc($result){
   return mysql_fetch_assoc($result);
}
function getRow($result){
   return mysql_fetch_row($result);
}
function showError($query){
   die(mysql_error()."<br /> Query: ".$query);
}

function dbClose(){
   mysql_close();
}
function getRecord($key,$table,$value,$att){
  $sel="select * from $table where $key='$value'";
  $res=mysql_query($sel);
  $rec= mysql_fetch_assoc($res);
  return $rec[$att];
}
function getArabic($key){
  $sel="select * from library where english='$key'";
  $res=mysql_query($sel);
  if(mysql_num_rows($res)!=0 && LANG=='ar'){
  $rec= mysql_fetch_assoc($res);
  return $rec['arabic'];
  }else{
  return $key;
  }
}

function getRecordTotal($key,$table,$value,$att){
  $sel="select * from $table where $key='$value'";
  $res=mysql_query($sel);
  $tot=0;
  while($rec= mysql_fetch_assoc($res)){
  $tot+= $rec[$att];
  }
  return $tot;
}

function getRecordTotalCon($key,$table,$value,$att,$key1,$value1){
  $sel="select * from $table where $key='$value' and $key1='$value1'";
  $res=mysql_query($sel);
  $tot=0;
  while($rec= mysql_fetch_assoc($res)){
  $tot+= $rec[$att];
  }
  return $tot;
}

function getRecordTotalConSp($key,$table,$value,$att,$key1,$value1){
  $sel="select * from $table where $key='$value' and $key1 < '$value1'";
  $res=mysql_query($sel);
  $tot=0;
  while($rec= mysql_fetch_assoc($res)){
  $tot+= $rec[$att];
  }
  return $tot;
}

function getRecordTotalConSp2($key,$table,$value,$att,$key1,$value1,$key2,$value2){
  $sel="select * from $table where $key='$value' and $key1 = '$value1'";
  $res=mysql_query($sel);
  $tot=0;
  while($rec= mysql_fetch_assoc($res)){
  $tot+= $rec[$att];
  }
  return $tot;
}

function getRecordTotalConSp3($key,$table,$value,$att,$key1,$value1,$value2){
  $sel="select * from $table where $key='$value' and $key1 between '$value1' and '$value2'";
  $res=mysql_query($sel);
  $tot=0;
  while($rec= mysql_fetch_assoc($res)){
  $tot+= $rec[$att];
  }
  return $tot;
}

function getRecordCount($key,$table,$value){
  $sel="select * from $table where $key='$value'";
  $res=mysql_query($sel);
    return mysql_num_rows($res);
}
function getOptions($key,$table,$value,$action,$parent,$i,$par){?>

	<option <?php if($action=='edit'){if($par==$value){echo 'selected';}}?> value="<?php echo $value;?>"><span style="font-size:<?php echo 26;?>px;"><?php 
	$j=$i;
	while($j>0){echo " ->";$j--;}echo getRecord('id','ven_categories',$value,'name') ?></span></option>
    
    <?php 
		$pid=$value;
									$s="select * from ven_categories where parent='$pid'";
									$su=executeQuery($s);
									$cnt=totalRecords($su);
									if($cnt!=0){
										$i++;
									}else{?>
										
										<?php }
									while($sub=getAssoc($su)){
										
									
										getOptions('id','ven_categories',$sub['id'],$action,$sub['parent'],$i,$par);
										}
											
	 }
	 function getOptionsprod($key,$table,$value,$action,$parent,$i,$par){
		  $cnt=getRecordCount('parent','ven_categories',$value);
		  if($cnt!=0){
		 ?>
     <optgroup label="<?php 
	$j=$i;
	while($j>0){echo " ->";$j--;}echo getRecord('id','ven_categories',$value,'name') ?>"></optgroup>
    <?php }else{?>

	<option <?php if($action=='edit'){if($par==$value){echo 'selected';}}?> value="<?php echo $value;?>"><span style="font-size:<?php echo 26;?>px;"><?php 
	$j=$i;
	while($j>0){echo " ->";$j--;}echo getRecord('id','ven_categories',$value,'name') ?></span></option>
    
    <?php }
		$pid=$value;
									$s="select * from ven_categories where parent='$pid'";
									$su=executeQuery($s);
									$cnt=totalRecords($su);
									if($cnt!=0){
										$i++;
									}else{?>
										
										<?php }
									while($sub=getAssoc($su)){
										
									
										getOptionsprod('id','ven_categories',$sub['id'],$action,$sub['parent'],$i,$par);
										}
											
	 }
	 function multi_level_category($pid,$table,$typ,$select,$val){
	if(isset($_GET['id'])){$select=$_GET['id'];}else{$select=0;}
	$ca=mysql_query("select * from ".$table." where status=1 and parent='$pid' and id!='".$select."'");
	if(totalRecords($ca)>0){
	while($cat=getAssoc($ca)){
	$aa=''; for($i=0;$i<$val;$i++)	{$aa.=' - ';}?>
	<option <?php if($typ=='edit'){if(getRecord('id',$table,$select,'parent')==$cat['id']){echo 'selected="selected"';}}?>  value="<?php echo $cat['id'];?>"><?php echo $aa.$cat['title'];?></option>
	<?php multi_level_category($cat['id'],$table,$typ,$select,($val+1));  }}}
	
function multi_level_category_pro($pid,$table,$typ,$val){
	if(isset($_GET['id'])){$select=$_GET['id'];}else{$select=0;}
	$ca=mysql_query("select * from ".$table." where status=1 and parent='$pid'");
	if(totalRecords($ca)>0){
	while($cat=getAssoc($ca)){
	$aa=''; for($i=0;$i<$val;$i++)	{$aa.=' - ';}?>
	<option <?php if($typ=='edit'){if(getRecord('id','products',$select,'category')==$cat['id']){echo 'selected="selected"';}}?>  value="<?php echo $cat['id'];?>"><?php echo $aa.$cat['title'];?></option>
	<?php multi_level_category_pro($cat['id'],$table,$typ,$select,($val+1));  }}}
	
function multi_level_category_pro_multi($pid,$table,$typ,$select,$val){
	if($typ=='edit'){$selectt=explode(',',$select);}
	$ca=mysql_query("select * from ".$table." where status=1 and parent='$pid'");
	if(totalRecords($ca)>0){
	while($cat=getAssoc($ca)){
	$aa=''; for($i=0;$i<$val;$i++)	{$aa.=' - ';}?>
	<option <?php if($typ=='edit'){if(in_array($cat['id'], $selectt)){echo 'selected="selected"';}}?>  value="<?php echo $cat['id'];?>"><?php echo $aa.$cat['title'];?></option>
	<?php multi_level_category_pro_multi($cat['id'],$table,$typ,$select,($val+1));  
	}}}

function multi_level_products($pid,$table,$typ,$select){
	if(isset($_GET['id'])){$select=$id;}else{$select=0;}
	$ca=mysql_query("select * from ".$table." where status=1 and parent='$pid' and id!='".$select."'");
	while($cat=getAssoc($ca)){
	$su=mysql_query("select * from ".$table." where status=1 and parent='$cat[id]'");
	if(totalRecords($su)>0){
	?>
    <optgroup label="<?php echo $cat['title'];?>">
	<?php multi_level_products($cat['id'],$table,$typ,$select);?>
    </optgroup>
	<?php } else{?>
	<option <?php if($typ=='edit'){if(getRecord('id','products',$_GET['id'],'category')==$cat['id']){echo 'selected="selected"';}}?>  value="<?php echo $cat['id'];?>"><?php echo $cat['title'];?></option>
    <?php }}}?>
