<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
	<xsl:output method="xml" indent="yes" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"/>
	<xsl:param name="message"/>
	<xsl:param name="pageTitle">IP Destinations</xsl:param>
	<xsl:param name="function"/>
	<xsl:param name="basedestipaddr"/>
	<xsl:param name="defaultdestport"/>
	<xsl:param name="defaultsrcport"/>
	<xsl:param name="defaultoptipttl"/>

	<xsl:param name="destination">destinationx.py</xsl:param>
	<xsl:param name="script">ip_destinations.py</xsl:param>
	
	<xsl:include href="namedtemplates.xsl"/>


	<xsl:template name="next_ip_port">
		<xsl:param name="ip" />
		<xsl:param name="port" />
		<xsl:value-of select="$destination"/>
		<xsl:text>?function=add&amp;destipaddr=</xsl:text>
		<xsl:choose>
			<xsl:when test="$ip">
				<xsl:value-of select="$ip"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:call-template name="last_dest_ip_addr" />
			</xsl:otherwise>
		</xsl:choose>
		<xsl:text>&amp;destport=</xsl:text>
		<xsl:choose>
			<xsl:when test="$port">
				<xsl:value-of select="$port"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:call-template name="last_port" />
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<xsl:template name="next_ip_port_esc">
		<xsl:param name="ip" />
		<xsl:param name="port" />
		<xsl:value-of select="$destination"/>
		<xsl:text>?function=add%26destipaddr=</xsl:text>
		<xsl:choose>
			<xsl:when test="$ip">
				<xsl:value-of select="$ip"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:call-template name="last_dest_ip_addr" />
			</xsl:otherwise>
		</xsl:choose>
		<xsl:text>&amp;destport=</xsl:text>
		<xsl:choose>
			<xsl:when test="$port">
				<xsl:value-of select="$port"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:call-template name="last_port" />
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<!-- get the last port out of the list -->
	<xsl:template name="last_dest_ip_addr">
		<xsl:value-of select="/configuration/iptv/iptv_destination[last()]/dest_ip_addr"/>
	</xsl:template>

	<!-- get the last port out of the list -->
	<xsl:template name="last_port">
		<xsl:value-of select="/configuration/iptv/iptv_destination[last()]/dest_port"/>
	</xsl:template>

	<xsl:template  match="/configuration">
		<html>
			<head>
				<title>
					<xsl:value-of select="$web_title"/>
				</title>
				<xsl:call-template name="common_imports"/>
				<script language="JavaScript" type="text/javascript" src="/javascript/ip_decimal.js"/>
				<script type="text/javascript">
					var iptv_ip = new Array();
					<xsl:for-each select="/configuration/iptv/iptv_destination" >
						dec = convert_to_decimal(&quot;<xsl:value-of select="dest_ip_addr" />&quot;);
						iptv_ip.push( dec );
					</xsl:for-each>
					function update_addnew() {
						<xsl:if test="/configuration/iptv/iptv_destination" >
							var next_ip = decimal_to_ip(find_hole(iptv_ip));
							var last_port = <xsl:call-template name="last_port" />;
							var last = &quot;destinationx.py?function=add\x26destipaddr=&quot;+next_ip+&quot;\x26destport=&quot;+last_port;
							document.getElementById("addnew").href = last;
						</xsl:if>
					}
				</script>
			</head>
			<body onload="window.defaultStatus=''; StartFloat(); update_addnew();">
				<xsl:call-template name="initPage">
					<xsl:with-param name="pageName" select="$script"/>
					<xsl:with-param name="message" select="$message"/>
				</xsl:call-template>
				<div id="displayresults">
					<form action="ip_destinations.py" method="post" name="applyform">
						<input type="hidden" name="action" value="apply"/>	
						<xsl:element name="input">
							<xsl:attribute name="type">hidden</xsl:attribute>
							<xsl:attribute name="name">function</xsl:attribute>
							<xsl:attribute name="value">globaledit</xsl:attribute>
						</xsl:element>	
						<div id="row">
							<h1 class="display">IP Destinations</h1>
							<table>
								<tr>
									<td>
										<div class="blacktab">Global Settings</div>
									</td>
								</tr>
								<tr>
									<td>
										<table class="boxholder">
											<tr>
												<td>IPA:</td>
												<td>
													<input type="text" name="basedestipaddr" value="{/configuration/iptv/global_settings/base_dest_ip_addr}" size="15"/>
												</td>
												<td>Port:</td>
												<td>
													<input type="text" class="mediumtext" name="defaultdestport" value="{/configuration/iptv/global_settings/default_dest_port}" size="15"/>
												</td>
												<td>Src Port:</td>
												<td>
													<input type="text" class="mediumtext" name="defaultsrcport" value="{/configuration/iptv/global_settings/default_src_port}" size="15"/>
												</td>
												<td>IP TTL:</td>
												<td>
													<input type="text" class="shorttext" name="defaultoptipttl" value="{/configuration/iptv/global_settings/default_opt_ip_ttl}" size="15"/>
												</td>
											</tr>
										</table>
									</td>
								</tr>
							</table>
							<br/>
							<div class="adddest"> 
								<a id="addnew">
									<xsl:attribute name="href">destinationx.py?function=add&amp;destipaddr=<xsl:value-of select="iptv/global_settings/base_dest_ip_addr"/>&amp;destport=<xsl:value-of select="iptv/global_settings/default_dest_port"/>&amp;defaultsrcport=<xsl:value-of select="iptv/global_settings/default_src_port"/>&amp;defaultipttl=<xsl:value-of select="iptv/global_settings/default_opt_ip_ttl" /></xsl:attribute>
									<xsl:text>Add New</xsl:text>
								</a>
							</div>
							<br />
							<table width="960px">
								<tr class="headerrow">
									<td class="destheadercell">Name</td>
									<td class="destheadercell">Destination</td>
									<td class="destheadercellshort">Type</td>
									<td class="destheadercellshort">Status</td>
									<td class="destheadercell" style="text-align:right;">Action</td>
								</tr>
								<xsl:apply-templates/>
							</table>
						</div>
					</form>
				</div>
			</body>
		</html>
	</xsl:template>

	<xsl:template match="iptv">
		<xsl:for-each select="iptv_destination">
			<xsl:sort data-type="number" select="substring-before(dest_ip_addr,'.')"/>
			<xsl:sort data-type="number" select="substring-before(substring-after(dest_ip_addr,'.'),'.')"/>
			<xsl:sort data-type="number" select="substring-before(substring-after(substring-after(dest_ip_addr,'.'),'.'),'.')"/>
			<xsl:sort data-type="number" select="substring-after(substring-after(substring-after(dest_ip_addr,'.'),'.'),'.')"/>
			<xsl:sort data-type="number" select="dest_port"/>
			<tr class="destrow">
				<td class="destcell">
					<xsl:choose> 
						<xsl:when test="name = ''">
							<xsl:value-of select="dest_ip_addr" />:<xsl:value-of select="dest_port" />
						</xsl:when>
						<xsl:otherwise>
							<xsl:value-of select="name"/>
						</xsl:otherwise>
					</xsl:choose>
				</td>
				<td class="destcell">
					<xsl:value-of select="dest_ip_addr" />:<xsl:value-of select="dest_port" />
				</td>
				<td class="destcellshort">
					<xsl:choose> 
						<xsl:when test="count(programs/program_reference) = 0">
							None
						</xsl:when>
						<xsl:when test="count(programs/program_reference) = 1">
							SPTS
						</xsl:when>
						<xsl:otherwise>
							MPTS
						</xsl:otherwise>
					</xsl:choose>
				</td>
				<td class="destcellshort">
					<xsl:value-of select="@state"/>
				</td>
				<td class="destcell" style="text-align:right;" >
					[ 
					<a>
						<xsl:attribute name="href">
							destinationx.py?function=edit&amp;destipaddr=<xsl:value-of select="dest_ip_addr"/>&amp;destport=<xsl:value-of select="dest_port"/>
						</xsl:attribute>
						Edit
					</a>
					|
					<a>
						<xsl:attribute name="href">
							destinationx.py?function=delete&amp;destipaddr=<xsl:value-of select="dest_ip_addr"/>&amp;destport=<xsl:value-of select="dest_port"/>
						</xsl:attribute>
						Delete
					</a>
					]
				</td>
			</tr>
			<tr class="destrow">
				<td colspan="4" style="color:black;background:black;height:1px;width:800px;"></td>
			</tr>
		</xsl:for-each>
	</xsl:template>
	<xsl:template match="text()"></xsl:template>
</xsl:stylesheet>
