<?
if($_REQUEST['reset']==1){
	$GLOBALS['db']->query("update ecmpricebooks_ecmproducts set position=0 where ecmpricebook_id='".$_REQUEST['record']."'");
	header("Location: index.php?module=EcmPriceBooks&action=productsOrder&record=".$_REQUEST['record']);
}
if($_REQUEST['save']){
	$b=explode("|",$_REQUEST['sort_order']);
	$i=1;
	foreach($b as $box){
		if(strlen($box)>=1 && $box!="" && $box!=" ")$boxy[]=$box;
	}
	for($i=1;$i<=count($boxy);$i++){
		$z="update ecmpricebooks_ecmproducts set position='".$i."' where deleted='0' and ecmproduct_id='".$boxy[$i-1]."' and ecmpricebook_id='".$_REQUEST['record']."'";
		$GLOBALS['db']->query($z);
	}
	header("Location: index.php?module=EcmPriceBooks&action=DetailView&record=".$_REQUEST['record']);
}
$z="select id from ecmpricebooks_ecmproducts where ecmpricebook_id='".$_REQUEST['record']."' and deleted='0'";
$w=$GLOBALS['db']->query($z);
$ile=mysql_num_rows($w);
?>
<script type="text/javascript" src="modules/EcmPriceBooks/moo1.2.js"></script>
<script language="javascript">
/* when the DOM is ready */
window.addEvent('domready', function() {

/* create sortables */
  var sb = new Sortables('sortable-list', {
    /* set options */
    clone:true,
    revert: true,
    /* initialization stuff here */
    initialize: function() { 
      
    },
    /* once an item is selected */
    onStart: function(el) { 
      el.setStyle('background','#add8e6');
    },
    /* when a drag is complete */
    onComplete: function(el) {
      el.setStyle('background','#ddd');
      //build a string of the order
      var sort_order = '';
      $$('#sortable-list li').each(function(li) { sort_order = sort_order +  li.get('alt')  + '|'; });
      $('sort_order').value = sort_order;
    }
  });
});
</script>

<style type="text/css">
li.sortme {
	padding:2px 2px; 
	font-size: 9px;
	color:#000; 
	cursor:move; 
	list-style:none; 
	width:700px; 
	background:#ddd; 
	margin:10px 0; 
	border:1px solid #999; 
}
#sortable-list{
   clear: none;
   float: left;
   margin: 2px;
   padding: 4px;
}
</style>
<table width='100%' cellpadding='0' cellspacing='0' border='0' class='moduleTitle'><tr><td valign='top'>
</td><td width='100%'><h2>Set Products Order:
<?php
$z="select name from ecmpricebooks where id='".$_REQUEST['record']."'";
$w=$GLOBALS['db']->query($z);
$r=$GLOBALS['db']->fetchByAssoc($w);
print "&nbsp;".$r['name'];
?>
</h2></td>
</tr></table>
<form action="index.php?module=EcmPriceBooks&action=productsOrder&record=<?php echo $_REQUEST['record'];?>" method="post">
<table cellpadding="0" cellspacing="0" border="0" width="100%">
 <tr>
  <td style="padding-bottom: 2px;">
   <input type="submit" value="Save" name="save" class="button">
   <input type="button" value="Reset" name="reset" class="button" onclick="location.href='index.php?module=EcmPriceBooks&reset=1&action=productsOrder&record=<?php echo $_REQUEST['record'];?>';">
   <input type="button" value="Categories Order" name="cat_order" class="button" onclick="location.href='index.php?module=EcmPriceBooks&action=categoryOrder&record=<?php echo $_REQUEST['record'];?>';">
  </td>
 </tr>
</table>
<table width="100%" border="0" cellspacing="0" cellpadding="0" class="tabForm">
 <tr>
 	<td class="dataLabel">
<ul id="sortable-list">
<?php
if(mysql_num_rows($GLOBALS['db']->query("select id from ecmpricebooks_ecmproducts_categories_sort where ecmpricebook_id='".$_REQUEST['record']."'"))==0)$zz="select id from ecmproductcategories where deleted='0' order by name asc";
else $zz="select position,category_id as id from ecmpricebooks_ecmproducts_categories_sort where ecmpricebook_id='".$_REQUEST['record']."' order by position asc";
$ww=$GLOBALS['db']->query($zz);
while($rr=$GLOBALS['db']->fetchByAssoc($ww)){
?>
<?
$z="select e.id,e.position,e.ecmproduct_id,p.product_category_name,p.name,p.code from ecmpricebooks_ecmproducts as e inner join ecmproducts as p on p.id=e.ecmproduct_id where e.ecmpricebook_id='".$_REQUEST['record']."' and e.deleted='0' and p.product_category_id='".$rr['id']."' order by e.position asc,p.product_category_name asc,p.code asc";
$w=$GLOBALS['db']->query($z);
while($r=$GLOBALS['db']->fetchByAssoc($w)){
	print '<li class="sortme" alt="'.$r['ecmproduct_id'].'"><table><tr><td width="100"><b>'.$r['code'].'</b></td><td width="150">'.$r['product_category_name'].'</td><td>'.$r['name'].'</td></tr></table></li>';
	$sort_order[] = $r['ecmproduct_id'];
}
?>

<?php echo '<br>';} ?>
</ul>
<input type="hidden" name="sort_order" id="sort_order" value="<?php echo implode($sort_order,'|'); ?>" />


		</td>
	</tr>
</table>
</form>
