<?php
/**
 * fedex_popup_laser.php
 *
 * @package fedex automated shipping labels
 * @copyright Copyright 2007-2008 Numinix Technology http://www.numinix.com
 * @copyright Portions Copyright 2003-2006 Zen Cart Development Team
 * @copyright Portions Copyright 2003 osCommerce
 * @license http://www.zen-cart.com/license/2_0.txt GNU Public License V2.0
 * @version $Id: fedex_popup_laser.php 33 2010-03-11 00:13:12Z numinix $
 * @author Numinix Technology
 */

	$oID = $_GET['oID'];
	$active = $_GET['active'];
	
	// check to see if there are multiple packages in this shipment
	$packages_query = "select multiple as multiple from " . TABLE_SHIPPING_MANIFEST . " where orders_id = " . $oID . " order by multiple asc";
	
	// if there's no other indication, first label should be active label
	if (!$active) {
		$active = 1;
		}
	
	$multiple = array();
	$packages = $db->Execute($packages_query);
	while (!$packages->EOF) {
		// if 'multiple' is populated, get all values into an array
/*		
		echo '<pre>';
		echo 'packages is <br>';
		print_r($packages);
		echo '</pre>';
*/		
		if ($packages->fields['multiple']) {
			$multiple[] = $packages->fields['multiple'];
			}
    $packages->MoveNext();
		}
	                    
	// get the highest value from $multiple, it's the last label
	if ($multiple) {
		$last = max($multiple);
		}
	
	// now get the tracking number for the selected (active) label
	if ($multiple) {
		$tracking_num = fedex_manifest_data($oID,$active);
		}
	elseif (!$multiple) {
		$tracking_num = $_GET['num'];
		}

?>
	
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>

    <style type="text/css">
    <!-- 
		table { 
			width: 675; 
			border-top: 1px dotted black; 
			}
		td {
			font-family: Verdana, Arial, sans-serif; 
			font-size: 12px; 
			}
		-->
    </style>
    <script language="JavaScript1.1" type="text/javascript">
    var NS4 = (document.layers) ? true : false ;var resolution = <?php echo (int)MODULE_SHIPPING_FEDEX_RESOLUTION; ?>;if (NS4 && navigator.javaEnabled()){var toolkit = java.awt.Toolkit.getDefaultToolkit();resolution = toolkit.getScreenResolution();}
    </script>
    <script language="JavaScript" type="text/javascript">
    document.write('<img WIDTH=' + (675 * resolution )/100 + '<img HEIGHT=' + (467 * resolution )/100 + ' alt="ASTRA Barcode" src="<?php echo DIR_WS_FEDEX_LABELS . $tracking_num; ?>.png">');
    </script>
    <title></title>
  </head>
  <body>
    <table border="0" width="100%">
      <tr>
        <td colspan="4">
          <img src="images/pixel_trans.gif" border="0" alt=""
          width="1" height="100">
        </td>
      </tr>
      <tr>
        <td align="center">
          <a href="#" onclick=
          "window.print(); return false"><img src=
          "includes/languages/english/images/buttons/button_print.gif"
               border="0" alt="IMAGE_ORDERS_PRINT" title=
               " IMAGE_ORDERS_PRINT "></a>
        </td>
				<td align="center">
<?php

// links for multiple packages

	if ($multiple) {
		if ($active != 1) {
			echo '<a href="?oID=' . $oID . '&active=' . ($active-1) . '">&lt;-- previous</a> &nbsp; ';
			}
		else {
			echo '&lt;-- previous';
			}
		foreach ($multiple as $package_num) {
			if ($active != $package_num) {
				echo ' &nbsp; <a href="?oID=' . $oID . '&active=' . $package_num . '">' . $package_num . '</a> &nbsp; ';
				}
			else {
				echo ' <b>' . $package_num . '</b> ';
				}
			}
		if ($active != $last) {
			echo ' &nbsp; <a href="?oID=' . $oID . '&active=' . ($active+1) . '">next --&gt;</a>';
			}
		else {
			echo ' &nbsp; next --&gt;';
			}
		}		
?>
        </td>
				<td align="center">
          <a href="orders.php?oID=<?php echo $oID; ?>"><img src=
          "includes/languages/english/images/buttons/button_back.gif"
               border="0" alt="IMAGE_ORDERS_BACK" title=
               " IMAGE_ORDERS_BACK "></a>
        </td>
        <td>
          <button  class="navigation" type="button" style="cursor:pointer;"onClick="javascript:location.href = '<?php echo zen_href_link(FILENAME_EMAIL_FEDEX, 'oID=' .$_GET['oID'] . '&num=' . $_GET['num']); ?>';">Email Label</button>
        </td>
      </tr>
    </table>
  </body>
</html>

<?php

function fedex_manifest_data($oID, $active) {
  global $db;
	if (!$active) {
		$packages_query = "select tracking_num as tracking_num from " . TABLE_SHIPPING_MANIFEST . " where orders_id = " . $oID . "";
		}
	else {	
		$packages_query = "select tracking_num as tracking_num from " . TABLE_SHIPPING_MANIFEST . " where orders_id = " . $oID . " and multiple = '" . $active . "'";
		}
  $val = $db->Execute($packages_query);
	while (!$val->EOF) {
		$tracking_num = $val->fields['tracking_num'];
    $val->MoveNext();
	}
	return $tracking_num;
	}
?>