<script type="text/javascript">
	var showStockLabel = null;
	var showButton = false;

    <?php if($this->stockLevel > 0) { ?>
    	showStockLabel = 'divStockAvailable'; 
    <?php } elseif($this->stockLevel == 0 && $this->stockControl == 0) { ?>
    	showStockLabel = 'divUnderStockAvailable'; 
    <?php } elseif($this->stockLevel == 0 && $this->stockControl == 1) { ?>
    	showStockLabel = 'divNoStockAvailable';      
    <?php } ?>
    
    <?php if($this->validPricing && $this->stockControl == 0) { ?>
    	showButton = true;
    <?php } ?>
    		    
	$(document).ready(function() {
		setInterval("getProductStockCount(false)", 5000);    
		if (showStockLabel!=null) { $('#'+showStockLabel).show(); }
		if (showButton) {$('#continuebutton').show();}		
	});
	
	function toggleStockLabels(activeLabel) {
		$('#divNoStockAvailable').hide();
		$('#divUnderStockAvailable').hide();
		$('#divStockAvailable').hide();
		if (activeLabel!="") $('#'+activeLabel).show();
	}
	
	function getProductStockCount(formSubmit)
	{
	
	   $.ajax({
		  url: "index.php?action=getProductStockLevel&fuse=admin",
		  context: document.body,
		  data : {productId:<?php echo $this->product;?>},
		  success: function(data, textStatus, jqXHR)
		  {
		  	obj = ce.parseAjaxResponse(jqXHR);
		  	//submit form check
		  	if (formSubmit) {
			  	if ((obj.stockLevel > 0) || (obj.stockControl == 0) ) { 
			  		$('#step1form').submit();
			  	} else {
					Ext.MessageBox.alert(lang('Stock Notice'),lang('We are sorry the selected product has sold out'));
				}
			} else {
				//need to update labels			
				if (obj.stockLevel > 0) {
					toggleStockLabels("divStockAvailable");	
					$('#remainingstocklevel').html(obj.stockLevel);
					$('#remainingstockexpiretext').html(obj.timeExpireText);
				} else if (obj.stockLevel == 0 && obj.stockControl == 0) {
					toggleStockLabels("divUnderStockAvailable");							
				} else if (obj.stockLevel == 0 && obj.stockControl == 1) {
					toggleStockLabels("divNoStockAvailable");				
				} else {
					toggleStockLabels("");
				}
				
				if (obj.stockControl == 0) {$('#continuebutton').show();}
				else {$('#continuebutton').hide();}
			}
		  }
		});
		
	}
</script>

<!-- Progress Bar -->
<?php echo $this->progressBar;?>

<!-- Cart Summary -->
<?php echo $this->cartSummary;?>

<!-- Main Page -->
<div style="margin-right:220px; margin-bottom:20px;">
    <form id="step1form" action="<?php echo $this->nextUrl;?>" method="POST">
    <input type="hidden" name="productGroup" value="<?php echo htmlentities($this->productGroup);?>">
    <input type="hidden" name="product" value="<?php echo htmlentities($this->product);?>">
    <input type="hidden" name="formId" value="packageSelect">
<?php if(isset($_GET["domainName"])) echo "<input type=\"hidden\" name=\"domainName\" value=\"" . $_GET["domainName"] . "\" />"; ?>
<?php if(isset($_GET["tld"])) echo "<input type=\"hidden\" name=\"tld\" value=\"" . $_GET["tld"] . "\" />"; ?>    

    <?php if($this->packageCount > 0) { ?>

        <div style="border:2px solid #ddd; background:#f6f6f6; padding:15px; margin-bottom:20px;-moz-border-radius: 5px;-webkit-border-radius: 5px;">
            <?php if($this->dropdownCount > 1) { ?>
                <?php echo $this->lang('Choose Product');?>: <select name="product" onchange = "window.location.href='order.php?step=1&productGroup=<?php echo $this->productGroup;?>&product='+this.options[this.selectedIndex].value">
                  <?php foreach($this->packages AS $package) { ?>
                    <?php if(!$package['showDropdown']) { continue; } ?>
                    <option value="<?php echo $package['id'];?>" <?php if($package['selected']){ echo "selected";}?>><?php echo $package['planname'];?></option>
                  <?php } //end foreach ?>
                </select>
                <br><br>
            <?php } ?>
            <h3><?php echo $this->planname;?></h3>
            <br>
            <div id="productDesc">
            	<?php echo $this->description;?>
            </div>
        </div>
    	
           <div id='divStockAvailable' style='display:none;border:1px solid #efefbb; background:#FEFFED; font-weight:bold; margin-bottom:20px; padding:10px;-moz-border-radius: 5px;-webkit-border-radius: 5px;'><?php echo $this->lang('Remaining Stock');?>: <span id='remainingstocklevel'><?php echo $this->stockLevel;?></span> <?php echo "<span id='remainingstockexpiretext' style='font-weight:normal;'>".$this->timeExpireText."</span>"; ?></div>
           <div id='divUnderStockAvailable' style='display:none;border:1px solid #900; background:#ffeded; color:#900; font-weight:bold; margin-bottom:20px; padding:10px;-moz-border-radius: 5px;-webkit-border-radius: 5px;'><?php echo $this->lang('This product is sold out and there may be a delay in processing your order!');?></div>
           <div id='divNoStockAvailable' style='display:none;border:1px solid #900; background:#ffeded; color:#900; font-weight:bold; margin-bottom:20px; padding:10px;-moz-border-radius: 5px;-webkit-border-radius: 5px;'><?php echo $this->lang('This product is sold out and no new orders are being accepted!');?></div>

        <table class="ts2" cellspacing="0">
          <tr style="background:#E8F3F8;">
            <td>&nbsp;</td>
            <td><strong><?php echo $this->lang('Term Length');?></strong></td>
            <td><strong><?php echo $this->lang('Price Per Term');?></strong></td>
            <td><strong><?php echo $this->lang('Setup Fee');?></strong></td>
            <td><strong><?php if($this->showSaved) { echo $this->lang("You Save");}?></strong></td>
          </tr>
       <?php foreach($this->pricing AS $price) { ?>
          <tr>
            <td><input name="priceTerm" type="radio" value="<?php echo $price['termId'];?>" <?php if($this->stockControl == 0 && $price['selected']) { echo "checked";} elseif($this->stockControl == 1){ echo "disabled";}?> /></td>
            <td><?php echo $price['term'];?></td>
            <td><?php echo $price['price'];?></td>
            <td><?php echo $price['setup'];?></td>
            <td><?php if($this->showSaved) { echo $price['save'];}?></td>
          </tr>
       <?php } //end foreach ?>

       <?php if(!$this->validPricing) { ?>
          <tr>
            <td align="center" colspan="5" style="font-weight:bold; color:#900;"><?php echo $this->lang("There are no valid pricing terms for this product.");?></td>
          </tr>
       <?php } //end if ?>
        </table>

         <!-- if stocklevel is above 1 then we need to check that when we submit the stock level is still one -->
       	 <a id="continuebutton" class="button small white" style="display:none;cursor:pointer;" onclick="<?php if($this->stockLevel > 0) { echo "getProductStockCount(true);"; } else { echo "$('#step1form').submit();";} ?>"> <?php echo $this->lang('Continue');?> &raquo; </a>

   <?php } else { ?>

    <div style='border:1px solid #900; background:#ffeded; color:#900; font-weight:bold; margin-bottom:20px; padding:10px;-moz-border-radius: 5px;-webkit-border-radius: 5px;'><?php echo $this->lang("An Invalid Product was Specified");?></div>

   <?php } ?>

</form>

</div>

<div class="clear"></div>