<?php
	/*
	 *
	 *  Lennox.com Product API v1.0 Example
	 * 	PRODUCT LISTING BY CATEGORY
	 *
	 *	XML Source: http://lennox.poweractive.com/api/v1/category/<categorynumber>/
	 *
	 *  PHP 5 w/ XML transport
	 *  Author: Ben Wilson (bwilson@powercreative.com)
	 *  2008/10/09
	 *
	 *  Version 1.0 - Initial write
	 *
	 *
	 *  Note: the "allow_url_fopen" directive in your php.ini file must be
	 *        set to "On" for this demo to work.
	 */

//$APIKEY = $_GET['apikey'];

//fetch CATEGORY NUMBER from querystring													'
//if (!empty($_GET['cat'])) { $category_id = $_GET["cat"]; }
//else { echo "This script requires a valid category ID."; exit(); }

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<head>
    <title>Product List by Category - PHP 5 - Lennox.com API Prototype</title>
	<style type="text/css">
		h2, div { margin: 0; padding: 0; }
		table.Products th {
			border-bottom:		8px solid white;
			background-color:	black;
			color:				white;
			text-align:			left;
			padding:			1px 10px 2px 6px;
		}
			table.Products th a {
				color:			#DDF;
			}
		tr.Odd {
			background-color: #F0F0F0;
		}
		tr.PadBot td {
			border-bottom: 8px solid white;
		}
		td {
			padding: 0 4px 2px 6px;
		}
	</style>


</head>

    <body>
	<script type="text/javascript">
	function openwin(url, width, height) {
	var name = "popup"; // popup name
	var width = width; // popup width
	var height = height; // popup height
	var left = (screen.width - width) / 2 ;
	var top = (screen.height - height) / 2;
	var windowproperties = "width="+ width +",height="+ height +",left="+ left +",top="+ top +",scrollbars=1";
	window.open(url, name, windowproperties);
	}
	</script>
<?php

	//Lennox.com Product API URL for Product Categories
	$api_url = "http://www.lennox.com/api/v1/".$APIKEY."/category/".$category_id;

	//use the handy SimpleXML file loader
	$xml = simplexml_load_file($api_url);

	//SimpleXML will either return FALSE is unsuccessful
	//or an XML object if successful.
	if ($xml) {

		if ($xml->Category) {
?>
			<!--<h2><?php //echo $xml->Category ?></h2> hide this for now since it usually matches the sub category name and looks redundant... -->

<?php
			foreach ($xml->SubCategory as $subcat) {
			if ($subcat->SubCategoryName <> "Oil Furnaces"){	//display if category is not oil furnaces (liberty doesn't carry those)
?>
				<h3><?php echo $subcat->SubCategoryName ?></h3>
				<!--<div class="Description">Short: <?php //echo $subcat->ShortDescription ?></div>
				<div class="Description">Long: <?php //echo $subcat->LongDescription ?></div>-->

				<!--START Product Table-->
				<table class="Products" cellpadding="0" cellspacing="0" width="100%">
					<thead>
						<tr valign="bottom">
							<th>

<?php
				if ($subcat->ContainsEnergyStar == "Yes") {
?>
								<img src="http://lennox.com/res/skins/2007/images/icon_estar.gif" alt="ESTAR ICON" height="12" width="12"> ENERGY STAR<sup>&reg;</sup> Product

<?php
				} else {
?>
								Products
<?php
				}
?>
							</th>
							<th>&nbsp;</th>

<?
				$columns = 3;

				if (count($subcat->CategoryRatings->CategoryRating)) {
					foreach ($subcat->CategoryRatings->CategoryRating as $rating) {
						$columns++;
?>
							<th><?php echo $rating->Name ?></th>
<?php
					} //foreach Rating
				} // if Ratings
?>


							<th><a href="http://www.lennox.com/popups/disclaimer.asp#priceguide" class="Popup w350 h175">Price<br>Guide<sup>*</sup></a></th>
						</tr>
					</thead>
					<tbody>
<?php
				$j = 0;

				//now, list each PRODUCT in our XML feed
				foreach ($subcat->Product as $product) {

?>
						<tr class="<?php echo ($j%2) ? ' Odd':' Even'?>" valign="bottom">
							<td class="<?php echo ($product->attributes()->EnergyStar=="Y") ? 'energyStar':'' ?>">
<?php
					if ($product->attributes()->EnergyStar == "Y") {
?>
								<img src="http://lennox.com/res/skins/2007/images/icon_estar.gif" alt="ESTAR ICON" height="12" width="12">
<?php
					}
?>

								<a href="custom/lennox-api/product-details.php?product=<?php echo $product->ModelNumber ?>"  onClick="openwin(this.href,'800','600'); return false;" class="NoUnderline">
								<?php echo $product->ModelName ?>
								</a>
							</td>
							<td><?php echo ($product->attributes()->New == "Y") ? 'NEW':'&nbsp;' ?></td>

<?php
					//if we have ratings to display
					if (count($product->Ratings->Rating)) {

						foreach ($product->Ratings->Rating as $rating) {

?>
							<td><?php echo $rating ?></td>
<?php
						} //foreach Rating

					//if this product does not have the ratings that are specified
					//by the Category, then show a blank table cell
					} else {
?>
							<td colspan="<?php echo count($subcat->CategoryRatings->CategoryRating) ?>">&nbsp;</td>
<?php

					}//if Product Ratings
?>

							<td><?php echo ($product->PriceGuide) ? $product->PriceGuide : '&nbsp;' ?></td>
						</tr>
						<tr class="PadBot<?php echo ($j%2) ? ' Odd':' Even' ?>" valign="top">
							<td colspan="<?php echo $columns ?>" class="OneLiner"><?php echo $product->OneLiner ?>&nbsp;</td>
						</tr>
<?php
					$j++;
				} //foreach product
?>
						</tbody>
					</table>
<?php
			}	//end of check for category #11
			} //foreach SubCat


		} //if Category

	} else {
		echo "There has been an error retrieving the XML.";
		exit();
	}

?>

