<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<?php
include ("../navigation/header.php");
$page_name = "Orders-order edit";
include ("../navigation/access.php");

if ($permission_num==1) {
	include ("../navigation/store_sidebar.php");

require_once ('../include/tc_calendar.php');
	

$order_reference = $_GET['order_reference'];
$error = $_GET['e'];

$table_name = $_SESSION['db_prefix']."_store_items";
$store_item_query = "SELECT store_item_id, item_name, unit FROM $table_name WHERE subCategory = 'Tiles' ORDER BY item_name ASC";

	$store_item_result = execsql($store_item_query);
	$store_item_num = mysql_num_rows($store_item_result);
	$w = 0;
	$arrId2 = array ();
	$arrName2 = array ();
	while ($w < $store_item_num) {
		$arrId2[$w] = mysql_result($store_item_result, $w, "store_item_id");
		$arrName2[$w] = mysql_result($store_item_result, $w, "item_name");
		$w++;
	}

$table_name = $_SESSION['db_prefix']."_store_items";
$store_item_query = "SELECT store_item_id, item_name, unit FROM $table_name WHERE subCategory = 'Accessories' ORDER BY item_name ASC";

	$store_item_result = execsql($store_item_query);
	$store_item_num = mysql_num_rows($store_item_result);
	$w = 0;
	$arrId3 = array ();
	$arrName3 = array ();
	while ($w < $store_item_num) {
		$arrId3[$w] = mysql_result($store_item_result, $w, "store_item_id");
		$arrName3[$w] = mysql_result($store_item_result, $w, "item_name");
		$w++;
	}

$table_name = $_SESSION['db_prefix']."_list_places";
    $position_query = "SELECT item_name FROM $table_name ORDER BY item_name ASC";
	$arrId4 = array ();
	$arrName4 = array ();
	$position_result = execsql($position_query);
	if ($position_result!="") {
	$position_num = mysql_num_rows($position_result);
	$b = 0;
	while ($b < $position_num) {
		$arrId4[$b] = mysql_result($position_result, $b, "item_name");
		$arrName4[$b] = mysql_result($position_result, $b, "item_name");
		$b++;
	}
	}
	
$table_name2 = $_SESSION['db_prefix']."_orders";
$order_query = "SELECT * FROM $table_name2 WHERE order_reference ='".$order_reference."' ";
$order_result = mySQL_query($order_query);
$order_num = mysql_num_rows($order_result);
$order_no  = mysql_result($order_result, 0, "order_no");
$cust_id  = mysql_result($order_result, 0, "cust_id");
$order_date  = mysql_result($order_result, 0, "order_date");
$delivery_date  = mysql_result($order_result, 0, "delivery_date");
$delivery_contact  = mysql_result($order_result, 0, "delivery_contact");
$delivery_phone  = mysql_result($order_result, 0, "delivery_phone");
$project_no  = mysql_result($order_result, 0, "project_no");
$project_place  = mysql_result($order_result, 0, "project_place");
$pallets  = mysql_result($order_result, 0, "pallets");
$del_col  = mysql_result($order_result, 0, "del_col");
$additional_info  = mysql_result($order_result, 0, "additional_info");
$ordered  = mysql_result($order_result, 0, "ordered");
$del_pallets  = mysql_result($order_result, 0, "del_pallets");
$delivered  = mysql_result($order_result, 0, "delivered");

$ord_date = strtotime($order_date);
$d1 = date("d", $ord_date );
$m1 = date("m", $ord_date );
$y1 = date("Y", $ord_date );


if ($delivery_date=='2000-01-01') { 
$delivery_date = $today;
$tba='yes';
 }
else {
$tba='no';
;
}


$require_date = strtotime($delivery_date);
$d = date("d", $require_date );
$m = date("m", $require_date );
$y = date("Y", $require_date );


$table_name3 = $_SESSION['db_prefix']."_customer";
$account_query = "SELECT cust_id, comp_name, bag_box, box_no, postal_place, phone, contact_1, email_1, cell_1 FROM $table_name3 WHERE cust_id = '".$cust_id."' ";
$account_result = mySQL_query($account_query);
$c0 = mysql_result($account_result, 0, "cust_id");
$c1 = mysql_result($account_result, 0, "comp_name");  
$c2 = mysql_result($account_result, 0, "bag_box");
$c3 = mysql_result($account_result, 0, "box_no");
$c4 = mysql_result($account_result, 0, "postal_place");
$c5 = mysql_result($account_result, 0, "phone");
$c6 = mysql_result($account_result, 0, "contact_1");
$c7 = mysql_result($account_result, 0, "email_1");
//$c8 = mysql_result($account_result, 0, "status");
$c9 = mysql_result($account_result, 0, "cell_1");

$accountTT = "<label class=tip_text_label>".$c1."<br>".$c2."<br>".$c4."<br>".$c5."<br>".$c6."<br><label class=tip_click_label>Click for details</label>";

?>

<!--style sheets-->
<link rel="stylesheet" type="text/css" href="../css/tooltip.css" />

<!--jscript-->
<script language="javascript" src="../jscript/jquery.1.4.2.js"></script>
<script language="JavaScript" src="../jscript/tooltip.js"  type="text/javascript"></script> 
<script language="JavaScript" src="../jscript/alert_message.js"  type="text/javascript"></script>
<script language="javascript" src="../jscript/chainedselects.js"></script>
<script language="JavaScript" src="../jscript/cancel_confirmation.js"  type="text/javascript"></script> 
<script language="javascript" src="../jscript/calendar.js"></script>
<script language="JavaScript" src="../jscript/capitalize_textbox.js"  type="text/javascript"></script>  
<script language="JavaScript" src="../jscript/numbers_only.js"  type="text/javascript"></script> 
<script language="JavaScript" src="../jscript/show_hide_layers.js"  type="text/javascript"></script>  
<script language="JavaScript" src="../jscript/go_to_url.js"  type="text/javascript"></script> 
<script language="JavaScript" src="../jscript/confirm_exit.js"  type="text/javascript"></script>  

<script type="text/javascript">
function validate_required(input,alerttxt,maxvalue,orvalue)
{
with (input)
  {
  if (value<maxvalue)
    {
    alertMessage(1, '', 'The entered quantity is less than already delivered.<br>The entry is reversed to the original.<br><br>Please enter a valid quantity.');
	input.value = orvalue;
	return false;
	}
  else
    {
    return true;
    }
  }
}
</script> 

<script type="text/javascript">
function validate_pallets(input,alerttxt,maxvalue,orvalue)
{
with (input)
  {
  if (value<maxvalue)
    {
    alertMessage(1, '', 'The entered quantity is less than already delivered.<br>The entry is reversed to the original.<br><br>Please enter a valid quantity.');
	input.value = orvalue;
	return false;
	}
  else
    {
    return true;
    }
  }
}
</script>

<script>
//var disable_empty_list=true; //uncomment this line to disable empty selection lists

<?php 

$k=0;
while ( $k<10 ) {
?>

addListGroup("Direct materials", "category<?php echo $k ?>", "store_item_id_select<?php echo $k ?>", "items");
//addOption("category0", "", "", "", 1); //Empty starter option
addList("category<?php echo $k ?>", "", "", "");
addList("category<?php echo $k ?>", "Tiles", "Tiles", "Tiles");
addList("category<?php echo $k ?>", "Accessories", "Accessories", "Accessories");

<?php
$k++;
}
?>

		addList("Tiles", "", "", "");
		<?php 
    	for ($w = 0; $w < count($arrId2); $w++) { ?>
			addList("Tiles", "<?php echo $arrName2[$w]?> ", "<?php echo $arrId2[$w]?> ", "");
		<?php
	    }
     ?>
		addList("Accessories", "", "", "");
		<?php 
        for ($w = 0; $w < count($arrId3); $w++) { ?>
			addList("Accessories", "<?php echo $arrName3[$w]?> ", "<?php echo $arrId3[$w]?> ", "");
		<?php
	    }
	?>

</script>

<script language="javascript">
function initCS() {
<?php
$t=0;
while ($t<50) {
?>
initListGroup("Direct materials", form1.category<?php echo $t ?>, form1.store_item_id_select<?php echo $t ?>); 

function initLists() {
  form1.category<?php echo $t ?>.onchange();
  category<?php echo $t ?>.fireEvent("onchange");
}

<?php 
$t++;
}
?>
}

</script>
</head>

<body onload="initCS(); 
<?php if ($ordered=='no') { ?>alertMessage(1, '', 'This ordered is cancelled.<br>You cannot edit the order anymore.'); <?php } ?>  
<?php if ($error==1) { ?> alertMessage(1, '', 'This product is already in the list. Edit the quantity.'); <?php } ?>
<?php if ($tba!='yes') {?> document.getElementById('date_div').hidden=false; document.getElementById('tba_div').hidden=true; <?php } else { ?> document.getElementById('date_div').hidden=true; document.getElementById('tba_div').hidden=false; <?php } ?>
">
<div class="main_frame" style="height:auto; min-height:555px;">
<div class="page_head_frame">
          <table width="690" border="0" cellspacing="0" cellpadding="0">
            <tr>
              <td width="493"><label class="label_page_head">
              Edit order</label>
              </td>
              <td width="197"><label style="font-size:18px; "><?php echo $order_no ?></label></td>
            </tr>
          </table>
        </div>
        <br />
        <br />
        <br />
<br />
<br />
<form action="edit_order_save.php" method="POST" name="form1" id="form1">
      <div class="page_middle_frame">
       <?php if ($ordered=='no') { ?><div class="page_inside_frame"><img src="../images/exclamation_mark_red_small.png" width="20" height="32" />&nbsp;&nbsp;This order is canceled.</div><?php } ?>
       <div class="page_inside_frame"> Order for:  
              <?php 
		include ('../include/customer_block.php');
		echo' <br>';
		?>
         
          <table width="642" id="table_display" >
            <tr>
              <td valign="top">Order date:</td>
              <td colspan="3" valign="top">
			  
			  <?php
		$myCalendar = new tc_calendar("order_date", true);
		$myCalendar->setIcon("../images/iconCalendar.gif");
		$myCalendar->setDate($d1, $m1, $y1);
		$myCalendar->setPath("./");
		$myCalendar->setYearInterval(2015, 2090);
		$myCalendar->writeScript();
?>

</td>
            </tr>
            <tr>
              <td width="128" valign="top"><select name="deliver_collect_select" id="deliver_collect_select" <?php if ($ordered!='yes') { ?> disabled="disabled" <?php } ?>>
                <option value="deliver">Deliver</option>
                <option value="collect">Collect</option>
                <option value="<?php echo $del_col ?>" selected="selected"><?php echo $del_col ?></option>
                
              </select >                
              </td>
              <td colspan="3" valign="top">
	<div id="date_div">		  
			  <?php
		$myCalendar = new tc_calendar("delivery_date", true);
		$myCalendar->setIcon("../images/iconCalendar.gif");
		$myCalendar->setDate($d, $m, $y);
		$myCalendar->setPath("./");
		$myCalendar->setYearInterval(2015, 2090);
		$myCalendar->writeScript();
?>
</div>
<div id="tba_div" style="cursor:pointer" onclick="document.getElementById('date_div').hidden=false; document.getElementById('tba_div').hidden=true; document.getElementById('tba').value='no';">
t.b.a. (Click to change) <input name="tba" id="tba" type="hidden" value="<?php echo $tba ?>" />
</div>
</td>
            </tr>
            
            <tr>
              <td>Delivery address:</td>
              <td colspan="3"><input type="text" name="project_no_text" id="project_no_text" value="<?php echo $project_no ?>" onchange="document.getElementById('edit_monitor').value='yes';" autocomplete="off" onfocus = "this.select();" <?php if ($ordered!='yes') { ?> disabled="disabled" <?php } ?> />
              <select name="project_place_select" id="project_place_select" class="select120" tabindex="4" onchange="document.getElementById('edit_monitor').value='yes'; " <?php if ($ordered!='yes') { ?> disabled="disabled" <?php } ?>>
                <option value="<?php echo $project_place?>" selected="selected"><?php echo $project_place?></option>
                <?php
        	for ($j = 0; $j < count($arrId4); $j++) {
    		echo "<option value=" . "\"" . $arrId4[$j] . "\"" . ">" . $arrName4[$j] . "</option>";
	}
?>
              </select>                
              </td>
            </tr>
            <tr>
              <td>Contact:</td>
              <td width="176">
              <input type="text" name="contact_text" id="contact_text" value="<?php echo $delivery_contact ?>" class="text200" onBlur="capitalize(this)" maxlength="25" autocomplete="off" onfocus = "this.select();" <?php if ($ordered!='yes') { ?> disabled="disabled" <?php } ?>/></td>
              <td width="86">Cell/phone:</td>
              <td width="232" align="left" valign="top">
              <input type="text" name="phone_text" id="phone_text"  value="<?php echo $delivery_phone ?>" class="text150" onkeypress="return numbersonly(this, event)" maxlength="10" autocomplete="off" onfocus = "this.select();" <?php if ($ordered!='yes') { ?> disabled="disabled" <?php } ?>/></td>
            </tr>
            <tr>
              <td>Notes:</td>
              <td colspan="3"><textarea name="notes_text" id="notes_text" cols="45" rows="5" class="text_box" <?php if ($ordered!='yes') { ?> disabled="disabled" <?php } ?>><?php echo $additional_info ?></textarea></td>
            </tr>
          </table>
          <br />
          Order details:<br />
            
            <?php

$table_name = $_SESSION['db_prefix']."_order_lines";
$line_query = "SELECT *  FROM $table_name WHERE  order_no =  '".$order_no."'";
$line_result = mySQL_query($line_query);
$line_num = mysql_num_rows($line_result);

				  ?>
            
            <input name="order_no" type="hidden" value="<?php echo $order_no ?>" />   
          </p>
          <table width="642" id="table_display" >
            <tr>
                  <td width="328" height="26" align="left" valign="top">Item</td>
                  <td width="136" align="left" valign="top">Group</td>
                  <td width="136" align="left" valign="top">Ordered</td>
                  <td width="136" align="left" valign="top">Delivered</td>
            </tr>
                <?php
				$i=0;
				while ($i<$line_num){
					 $a0 = mysql_result($line_result, $i, "order_line_id");
					 $a1 = mysql_result($line_result, $i, "store_item_id");
					 $table_name = $_SESSION['db_prefix']."_store_items";
						$item_query = "SELECT item_name, unit, subCategory FROM $table_name WHERE store_item_id = '".$a1."' ";
						$item_result = mySQL_query($item_query);
						$a1a = mysql_result($item_result, 0, "item_name");
						$a3a = mysql_result($item_result, 0, "unit");
						$a4a = mysql_result($item_result, 0, "subCategory");
					 $a2 = mysql_result($line_result, $i, "quantity");
					 $a3 = mysql_result($line_result, $i, "delivered");
					 
					 
								 
				 ?>
                <tr>
                  <td colspan="2" valign="middle"><div id="store_item_divA<?php echo $i ?>"><img src="../images/line_edit.png" alt="" width="16" height="12" id="add_icon<?php echo $i ?>" title="Click to edit line" 
              <?php if ($ordered=='yes') { ?>    
              onclick="
              document.getElementById('store_item_divA<?php echo $i ?>').hidden=true; 
              document.getElementById('store_item_divB<?php echo $i ?>').hidden=false; 
              document.getElementById('quantity_text<?php echo $i ?>').value=''; 
              document.getElementById('store_item_text<?php echo $i ?>').value =''; 
              calc_M(<?php echo $line_num?>)
              " <?php } ?> />
                    <input name="store_item_text<?php echo $i ?>" id="store_item_text<?php echo $i ?>" type="text" class="text300" value="<?php echo $a1a ?>" readonly="readonly" style="border:hidden; background-color:inherit" />
                    <input name="store_item_id<?php echo $i ?>" id="store_item_id<?php echo $i ?>" type="hidden" value="<?php echo $a1 ?>" />
                  </div>
                    <div id="store_item_divB<?php echo $i ?>" hidden="hidden">
                      <select name="category<?php echo $i ?>" id="category<?php echo $i ?>" class="select160"  onchange="document.getElementById('store_item_divA<?php echo $i ?>').hidden=true; document.getElementById('store_item_divB<?php echo $i ?>').hidden=false; document.getElementById('store_item_id_select<?php echo $i ?>').selectedIndex=-1; document.getElementById('index<?php echo $i ?>').value = document.getElementById('category<?php echo $i ?>').selectedIndex; ">
                      </select>
                      <input type="hidden" name="index<?php echo $i ?>" id="index<?php echo $i ?>" />
                      <select name="store_item_id_select<?php echo $i ?>" id="store_item_id_select<?php echo $i ?>" class="select230" onchange="
                      document.getElementById('store_item_id<?php echo $i ?>').value = document.getElementById('store_item_id_select<?php echo $i ?>').value;
                      
                     unit = document.getElementById('store_item_id_select<?php echo $i ?>').options[document.getElementById('store_item_id_select<?php echo $i ?>').selectedIndex].text;               		unit2 = unit.split('#'); 
              		unit3 = unit2[1];
                    document.getElementById('price_text<?php echo $i ?>').value = unit3;
                    
                    document.getElementById('amount_text<?php echo $i ?>').value = (document.getElementById('quantity_text<?php echo $i ?>').value * unit3).toFixed(2);
                    calc_M(<?php echo $line_num?>)
                    "
              >
                      </select>
                      <input type="hidden" name="line_num" id="line_num" value="<?php echo $line_num?>" />
                    </div></td>
                  <td valign="middle"><input name="quantity_text<?php echo $i ?>" id="quantity_text<?php echo $i ?>" type="text" value="<?php echo number_format($a2, 0, '.', '') ?>" class="text50" width="50px" onKeyUp="document.getElementById('amount_text<?php echo $i ?>').value = parseFloat(document.getElementById('quantity_text<?php echo $i ?>').value * document.getElementById('price_text<?php echo $i ?>').value).toFixed(2); calc_M(<?php echo $line_num?>)" onkeypress="return numbersonly(this, event)" onfocus = "this.select();" autocomplete="off"  onChange="document.getElementById('p_edit').value='yes' " onblur="validate_required(quantity_text<?php echo $i?>, 'The delivery amount exceeds the balance, the entry will be cleared. ' +'\n'+ 'Please enter a valid amount.', <?php echo $a3 ?>, <?php echo $a2 ?>); " style="text-align:right" <?php if ($ordered!='yes') { ?> disabled="disabled" <?php } ?> 
                  /></td>
                  <td valign="middle">
                  <input type="text" name="delivered_text" id="delivered_text" class="text50" width="50px" style="border:hidden;" disabled="disabled" value="<?php echo $a3; ?>" /></td>
                </tr>
                <?php 
			   
				$i++;
				 }
				 ?>
                <tr>
                  <td colspan="2">
                  <?php if ($ordered=='yes') { ?>
                  <a href="" onmouseover="ddrivetip('Click to add a new line.', 150)"; onmouseout="hideddrivetip()" onclick= " 
                  
	if (document.getElementById('edit_monitor').value=='no') {
		  editwindow=dhtmlmodal.open('editBox', 'iframe', '../edit_windows/order_edit_add_line.php?order_reference=<?php echo $order_reference ?>', 'Select product', 'width=450px,height=75px,center=1,resize=0,scrolling=0')
needToConfirm = false;
editwindow.onclose=function(){return window}; return false;
	}
	else {
		alertMessage(1, 'quotation_edit.php', 'Before adding a line, you need to save the changes you made. Click the Save button and try again.'); return false;
	}

                  "><?php } ?><img src="../images/line_edit.png" width="16" height="12" /><font color='#0047D4'> New line</font></a>
                   <input type="hidden" name="edit_monitor" id="edit_monitor" value="no" />
                  </td>
                  <td>&nbsp;</td>
                  <td>&nbsp;</td>
                </tr>
                <tr>
                  <td colspan="2"> Pallets</td>
                  <td><input type="text" name="pallets_text" id="pallets_text" class="text50" onkeypress="return numbersonly(this, event)" style="text-align:right" value="<?php echo $pallets ?>" onfocus = "this.select();" onBlur="validate_pallets(pallets_text, '', <?php echo $del_pallets ?>, <?php echo $pallets ?> )"  <?php if ($ordered!='yes') { ?> disabled="disabled" <?php } ?> onChange="document.getElementById('p_edit').value='yes' "/></td>
                  <td><input type="text" name="pallets_delivered_text" id="pallets_delivered_text" class="text50" width="50px" style="border:hidden;" disabled="disabled" value="<?php echo $del_pallets; ?>" /></td>
                </tr>
         </table>
        </div>
              <br />
              <div class="page_inside_frame">
      <table width="600" border="0">
    <tr>
      <td width="189"><input type="hidden" name="order_reference_text" id="order_reference_text" value="<?php echo $order_reference ?>" />
        <input name="p_edit" id="p_edit" type="hidden" value="no" /></td>
      <td width="401" align="right">
      
      <input name="button2" type="button" class="imagebutton" id="button2" onClick="if (document.getElementById('p_edit').value=='yes') { cancel_confirmation(3, 'store_main.php?date=0');} else {needToConfirm = false; window.location='store_main.php?date=0';}" value="Close"/>
       <?php if ($ordered=='yes') { ?>
        <input name="save_button"  id="save_button" type="submit" class="imagebutton" value="Save" onClick="needToConfirm = false; confirmRefresh();"/>
        <input name="save_close_button"  id="save_close_button" type="submit" class="imagebutton" value="Save &amp; Close" onClick="needToConfirm = false; confirmRefresh();"  />
        
		<?php }  ?>
        </td>
    </tr>
  </table>
</div>
  
 <br />
  </div>
  </form>
  <!--end of tableMain--> 
  
</div>
<?php include ('../navigation/side_bar.php'); ?>
</body>
<?php } else { ?>
<script> needToConfirm = false; </script>
<?php
$_SESSION['denied_return'] = "store/store_main.php?date=";	
include ("../navigation/store_sidebar.php");?>
<body onload="accessdenied(); return false">
<?php include ('../navigation/side_bar.php'); ?>
</body>
<?php }
?>
</html>