<?
if($_REQUEST['reset']==1){
	$GLOBALS['db']->query("delete from ecmprices_ecmproducts_categories_sort where ecmprice_id='".$_REQUEST['record']."'");
	header("Location: index.php?module=EcmPrices&action=categoryOrder&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;
	}
	$GLOBALS['db']->query("delete from ecmprices_ecmproducts_categories_sort where ecmprice_id='".$_REQUEST['record']."'");
	for($i=1;$i<=count($boxy);$i++){

		$z="insert into ecmprices_ecmproducts_categories_sort set id='".create_guid()."',position='".$i."',ecmprice_id='".$_REQUEST['record']."',category_id='".$boxy[$i-1]."'";
		$GLOBALS['db']->query($z);
		//echo $z.'<br>';;
	}
	header("Location: index.php?module=EcmPrices&action=productsOrder&record=".$_REQUEST['record']);
}
?>
<script type="text/javascript" src="modules/EcmPrices/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 Categories Order:
<?php
$z="select name from ecmprices 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=EcmPrices&action=categoryOrder&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=EcmPrices&reset=1&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
$w = $GLOBALS['db']->query("select id from ecmprices_ecmproducts_categories_sort where ecmprice_id='".$_REQUEST['record']."'");
if ($w->num_rows==0)
	$z="select id from ecmproductcategories where deleted='0' order by name asc";
else 
	$z="select position,category_id as id from ecmprices_ecmproducts_categories_sort where ecmprice_id='".$_REQUEST['record']."' order by position asc";
$w=$GLOBALS['db']->query($z);
while($r=$GLOBALS['db']->fetchByAssoc($w)){
	$rr=$GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("select name from ecmproductcategories where id='".$r['id']."'"));
	print '<li class="sortme" alt="'.$r['id'].'">'.$rr['name'].'</li>';
	$sort_order[] = $r['id'];
}
?>
</ul>
<input type="hidden" name="sort_order" id="sort_order" value="<?php echo implode($sort_order,'|'); ?>" />


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