<?xml version="1.0"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
	<xsl:output method="html" 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="function"/>
	<xsl:param name="pageTitle">
		<xsl:if test="$function = 'add'">
			Add
		</xsl:if>
		IP Destination
	</xsl:param>
	<xsl:param name="destipaddr"/>
	<xsl:param name="destport"/>
	<xsl:include href="namedtemplates.xsl"/>
	<xsl:template match="/configuration/iptv/iptv_destination[dest_ip_addr=$destipaddr and dest_port=$destport]/programs" mode="iptv">
		<xsl:for-each select="program_reference">
			iptv_assign.push(new TIptvAssignment(<xsl:value-of select="source_port" />,<xsl:value-of select="source_program_number"/>));
		</xsl:for-each>
	</xsl:template>
	<xsl:template name="head">
		<head>
			<title>
				<xsl:value-of select="$web_title"/>
			</title>
			<xsl:if test="configuration/user_data/auto_logout_time != 0">
				<xsl:element name="meta">
					<xsl:attribute name="http-equiv">refresh</xsl:attribute>
					<xsl:attribute name="content"><xsl:value-of select="(configuration/user_data/auto_logout_time * 60)+ 5"/>;URL=ip_destinations.py</xsl:attribute>
				</xsl:element>
			</xsl:if>
			<xsl:call-template name="common_imports"/>
			<script type="text/javascript" src="/javascript/destination.js"/>
			<script type="text/javascript" src="/javascript/inputs.js"/>
			<script type="text/javascript" src="/javascript/selectbox.js"/>
			<script type="text/javascript" src="/javascript/add_validate.js"/>
			<script type="text/javascript">
				var iptv_assign = new Array();   // list of TIptvDestination
				<xsl:apply-templates mode="iptv"/>
			</script>
		</head>
	</xsl:template>
	<xsl:template match="/">
		<html>
		<xsl:call-template name="head"/>
			<body onload="StartFloat(); load();">
				<xsl:call-template name="initPage">
					<xsl:with-param name="pageName">ip_destinations.py</xsl:with-param>
					<xsl:with-param name="reloadPage">destinationx.py</xsl:with-param>
					<xsl:with-param name="message" select="$message"/>
					<xsl:with-param name="applyClick">val_form();</xsl:with-param>
					<xsl:with-param name="params">?function=<xsl:value-of select="$function"/>&amp;destipaddr=<xsl:value-of select="$destipaddr"/>&amp;destport=<xsl:value-of select="$destport"/></xsl:with-param>
				</xsl:call-template>
				<div id="displayresults">
					<form action="ip_destinations.py" method="post" name="applyform">
					<input type="hidden" name="action" value="apply"/>
					<input type="hidden" name="destipaddr" value="{$destipaddr}"/>
					<input type="hidden" name="destport" value="{$destport}"/>
					<input type="hidden" name="function" value="{$function}"/>
					<h1 class="display">IP Destination:</h1> 
					<xsl:variable name="activeName">
						<xsl:choose>
							<xsl:when test="$function = 'add' or /configuration/iptv/iptv_destination[dest_ip_addr=$destipaddr and dest_port=$destport][1]/name = ''">
								<xsl:value-of select="$destipaddr"/>:<xsl:value-of select="$destport"/>
							</xsl:when>
							<xsl:otherwise>
								<xsl:value-of select="/configuration/iptv/iptv_destination[dest_ip_addr=$destipaddr and dest_port=$destport][1]/name"/>
							</xsl:otherwise>
						</xsl:choose>
					</xsl:variable>
					<div class="addeditdelete">
						<xsl:if test="$function = 'add'">
							Adding New Destination
						</xsl:if>
						<xsl:if test="$function = 'edit'">
							Editing:
							<select name="ip_dest" onchange="window.location = 'destinationx.py?function=edit' + this.value">
								<xsl:for-each select="/configuration/iptv/iptv_destination">
									<xsl:sort select="name" order="ascending"/>
									<xsl:variable name="value">
										<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>
									</xsl:variable>
									<option value="&amp;destipaddr={dest_ip_addr}&amp;destport={dest_port}">
										<xsl:if test="$value = $activeName">
											<xsl:attribute name="selected">selected</xsl:attribute>
										</xsl:if>
										<xsl:value-of select="$value"/>
									</option>
								</xsl:for-each>
							</select>
						</xsl:if>
						<xsl:if test="$function = 'delete'">
							You are deleting <xsl:value-of select="$activeName"/>. Click Apply Changes button to the left to delete this destination.
						</xsl:if>
					</div>
					<div class="adddest">
						<a href="ip_destinations.py">Cancel</a>
					</div>
					<table width="700">
						<tr>
							<td>
								<table>
									<tr>
										<td>
											<div class="blacktab">General</div>
										</td>
									</tr>
									<tr>
										<td>
											<table class="boxholder">
												<tr>
													<td>Name:</td>
													<td>
														<input type="text" name="name" value="{$activeName}" size="15">
															<xsl:if test="$function = 'delete'">
																<xsl:attribute name="disabled">disabled</xsl:attribute>
															</xsl:if>
														</input>
													</td>
													<td>IPA:</td>
													<td>
														<input type="text" class="mediumtext" name="dest_ip_addr" value="{$destipaddr}" size="15" id="dest_ip"/>
													</td>
													<td>Port:</td>
													<td>
														<input type="text" class="shorttext" name="dest_port" value="{$destport}" size="15"/>
													</td>
													<td>Src Port:</td>
													<td>
														<input type="text" class="shorttext" name="src_port" size="15">
															<xsl:attribute name="value">
																<xsl:choose>
																	<xsl:when test="$function != 'add'">
																		<xsl:value-of select="/configuration/iptv/iptv_destination[dest_ip_addr=$destipaddr and dest_port=$destport][1]/src_port"/>
																	</xsl:when>
																	<xsl:otherwise>
																		<xsl:value-of select="/configuration/iptv/global_settings/default_dest_port"></xsl:value-of>
																	</xsl:otherwise>
																</xsl:choose>
															</xsl:attribute>
														</input>
													</td>
												</tr>
												<tr>
													<td>Modulation Target:</td>
													<td>
														<!-- I pulled this from outputs. There has to be a more elegant way of showing current setting, but this works for now. ahp-->
														<select name="modulation_target" onchange="whichMR(this.form);">
															<xsl:call-template name="printOptions">
																<xsl:with-param name="optionName">modulation_target</xsl:with-param>
																<xsl:with-param name="defaultValue">
																	<xsl:choose>
																		<xsl:when test="$function != 'add'">
																			<xsl:value-of select="/configuration/iptv/iptv_destination[dest_ip_addr=$destipaddr and dest_port=$destport][1]/modulation_target"/>
																		</xsl:when>
																		<xsl:otherwise>
																			<xsl:text>Manual</xsl:text>
																		</xsl:otherwise>
																	</xsl:choose>
																</xsl:with-param>
																<xsl:with-param name="ignoredValue">8PSK</xsl:with-param>
															</xsl:call-template>
														</select>
														<input type="hidden" name="framing_mode" value="188"/>
														<input type="hidden" name="modulation_type" value="{/configuration/iptv/iptv_destination[dest_ip_addr=$destipaddr and dest_port=$destport][1]/nit/transport_stream/modulation_type}"/>
														<input type="hidden" name="delivery_descriptor" value="{/configuration/iptv/iptv_destination[dest_ip_addr=$destipaddr and dest_port=$destport][1]/nit/transport_stream/delivery_descriptor}"/>
														<xsl:if test="$function = 'edit'">
															<input type="hidden" name="polarization" value="{/configuration/iptv/iptv_destination[dest_ip_addr=$destipaddr and dest_port=$destport][1]/nit/transport_stream/polarization}"/>
															<input type="hidden" name="east_west" value="{/configuration/iptv/iptv_destination[dest_ip_addr=$destipaddr and dest_port=$destport][1]/nit/transport_stream/east_west}"/>
															<input type="hidden" name="orbital_position" value="{/configuration/iptv/iptv_destination[dest_ip_addr=$destipaddr and dest_port=$destport][1]/nit/transport_stream/orbital_position}"/>
															<input type="hidden" name="center_frequency" value="{/configuration/iptv/iptv_destination[dest_ip_addr=$destipaddr and dest_port=$destport][1]/nit/transport_stream/center_frequency}"/>
															<input type="hidden" name="bandwidth" value="{/configuration/iptv/iptv_destination[dest_ip_addr=$destipaddr and dest_port=$destport][1]/nit/transport_stream/bandwidth}"/>
															<input type="hidden" name="constellation" value="{/configuration/iptv/iptv_destination[dest_ip_addr=$destipaddr and dest_port=$destport][1]/nit/transport_stream/constellation}"/>
															<input type="hidden" name="hierarchy_information" value="{/configuration/iptv/iptv_destination[dest_ip_addr=$destipaddr and dest_port=$destport][1]/nit/transport_stream/hierarchy_information}"/>
															<input type="hidden" name="code_rate_HP_stream" value="{/configuration/iptv/iptv_destination[dest_ip_addr=$destipaddr and dest_port=$destport][1]/nit/transport_stream/code_rate_HP_stream}"/>
															<input type="hidden" name="code_rate_LP_stream" value="{/configuration/iptv/iptv_destination[dest_ip_addr=$destipaddr and dest_port=$destport][1]/nit/transport_stream/code_rate_LP_stream}"/>
															<input type="hidden" name="guard_interval" value="{/configuration/iptv/iptv_destination[dest_ip_addr=$destipaddr and dest_port=$destport][1]/nit/transport_stream/guard_interval}"/>
															<input type="hidden" name="transmission_mode" value="{/configuration/iptv/iptv_destination[dest_ip_addr=$destipaddr and dest_port=$destport][1]/nit/transport_stream/transmission_mode}"/>
															<input type="hidden" name="other_frequency_flag" value="{/configuration/iptv/iptv_destination[dest_ip_addr=$destipaddr and dest_port=$destport][1]/nit/transport_stream/other_frequency_flag}"/>
														</xsl:if>
													</td>
													<td>Bit Rate:</td>
													<td>
														<input class="mediumtext" type="text" name="constant_bit_rate" size="15">
															<xsl:attribute name="value">
																<xsl:choose>
																	<xsl:when test="$function != 'add'">
																		<xsl:value-of select="/configuration/iptv/iptv_destination[dest_ip_addr=$destipaddr and dest_port=$destport][1]/constant_bit_rate"/>
																	</xsl:when>
																	<xsl:otherwise>
																		<xsl:text>0</xsl:text>
																	</xsl:otherwise>
																</xsl:choose>
															</xsl:attribute>
														</input>
													</td>
													<td>IP TTL:</td>
													<td>
														<input type="text" class="shorttext" name="opt_ip_ttl" size="15">
															<xsl:attribute name="value">
																<xsl:choose>
																	<xsl:when test="$function != 'add'">
																		<xsl:value-of select="/configuration/iptv/iptv_destination[dest_ip_addr=$destipaddr and dest_port=$destport][1]/opt_ip_ttl"/>
																	</xsl:when>
																	<xsl:otherwise>
																		<xsl:value-of select="/configuration/iptv/global_settings/default_opt_ip_ttl"/>
																	</xsl:otherwise>
																</xsl:choose>
															</xsl:attribute>
														</input>
													</td>
													<td>Enable:</td>
													<td>
														<select name="dest_enable">
															<xsl:call-template name="printOptions">
																<xsl:with-param name="optionName">enabledisable</xsl:with-param>
																<xsl:with-param name="defaultValue">
																	<xsl:if test="$function != 'add'">
																		<xsl:value-of select="/configuration/iptv/iptv_destination[dest_ip_addr=$destipaddr and dest_port=$destport][1]/@state"/>
																	</xsl:if>
																</xsl:with-param>
															</xsl:call-template>
														</select>
													</td>
												</tr>
												<tr>
													<td>
														Transmit:
													</td>
													<td>
														<select name="enable_rtp">
															<xsl:call-template name="printOptions">
																<xsl:with-param name="optionName">enable_rtp</xsl:with-param>
																<xsl:with-param name="defaultValue">
																	<xsl:if test="$function != 'add'">
																		<xsl:value-of select="/configuration/iptv/iptv_destination[dest_ip_addr=$destipaddr and dest_port=$destport][1]/enable_rtp"/>
																	</xsl:if>
																</xsl:with-param>
															</xsl:call-template>
														</select>
													</td>
													<td>
														Interface:
													</td>
													<td>
														<select name="network_if">
															<xsl:call-template name="printOptions">
																<xsl:with-param name="optionName">net_interface</xsl:with-param>
																<xsl:with-param name="defaultValue">
																	<xsl:choose>
																		<xsl:when test="$function != 'add'">
																			<xsl:value-of select="/configuration/iptv/iptv_destination[dest_ip_addr=$destipaddr and dest_port=$destport][1]/network_if"/>
																		</xsl:when>
																		<xsl:otherwise>eth1</xsl:otherwise>
																	</xsl:choose>
																</xsl:with-param>
															</xsl:call-template>
														</select>
													</td>
												</tr>
											</table>
										</td>
									</tr>
								</table>
							</td>
						</tr>
						<tr>
							<td>
								<table>
									<tr>
										<td>
											<div class="blacktab">Programs</div>
										</td>
									</tr>
									<tr>
										<td>
											<table class="boxholder">
												<tr>
													<td>
														Available:<br/>
														<select name="opt_source" size="15" style="width:300px;" multiple="multiple" id="available_programs" ondblclick="moveSelectedOptions(document.getElementById('available_programs'), document.getElementById('assigned_programs'));">
															<xsl:for-each select="/configuration/port/program_remap/program">
																<xsl:sort select="program_number_out" data-type="number" order="ascending"/>
																<!-- xsl:for-each select="/configuration/status/port/routed_programs/program" -->
																<xsl:variable name="curIndex" select="../../@index"/>
																<xsl:variable name="progNumIn" select="@program_number_in"/>
																<xsl:variable name="activity">
																	<xsl:if test="/configuration/status/port[@index=$curIndex]/routed_programs/program[@program_number_in=$progNumIn]/@status = 'inactive'">
																		<xsl:text> (inactive)</xsl:text>
																	</xsl:if>
																	<xsl:if test="../../portstate = 'Disabled'">
																		<xsl:text> (disabled)</xsl:text>
																	</xsl:if>
																</xsl:variable>
																<option value="{$curIndex},{@program_number_in}">
																	<xsl:if test="$activity != ''">
																		<xsl:attribute name="class">inactive-program</xsl:attribute>
																	</xsl:if>
																	[<xsl:value-of select="$curIndex"/>-<xsl:value-of select="$progNumIn" />-<xsl:value-of select="program_number_out" />]
																	<xsl:choose>
																		<xsl:when test="contains(name_out,'(')">
																			<xsl:value-of select="substring-before(name_out, ' (')"/><xsl:value-of select="$activity"/>
																		</xsl:when>
																		<xsl:otherwise>
																			<xsl:value-of select="name_out"/><xsl:value-of select="$activity"/>
																		</xsl:otherwise>
																	</xsl:choose>
																</option>
															</xsl:for-each>
														</select>
													</td>
													<td>
														<table>
															<tr>
																<td>
																	<div class="button" onclick="moveSelectedOptions(document.getElementById('available_programs'), document.getElementById('assigned_programs'));">&gt;</div>
																</td>
															</tr>
															<tr>
																<td>
																	<div class="button" onclick="moveSelectedOptions(document.getElementById('assigned_programs'), document.getElementById('available_programs'));" >&lt;</div>
																</td>
															</tr>
															<tr>
																<td>
																	<div class="button" onclick="moveAllOptions(document.getElementById('available_programs'), document.getElementById('assigned_programs'));">&gt;&gt;</div>
																</td>
															</tr>
															<tr>
																<td>
																	<div class="button" onclick="moveAllOptions(document.getElementById('assigned_programs'), document.getElementById('available_programs'));">&lt;&lt;</div>
																</td>
															</tr>
														</table>
													</td>
													<td>
														Assigned:<br/>
														<select name="dest*" size="15" style="width:300px;" id="assigned_programs" multiple="multiple" ondblclick="moveSelectedOptions(document.getElementById('assigned_programs'), document.getElementById('available_programs'));">
															<!--xsl:for-each select="/configuration/iptv/iptv_destination[dest_ip_addr='226.0.1.2' and dest_port='2000']/programs/program_reference">
																<option><xsl:attribute name="value"><xsl:value-of select="source_port"/>,<xsl:value-of select="source_program_number" /></xsl:attribute>
																<xsl:value-of select="source_port"/>,<xsl:value-of select="source_program_number" /></option>
															</xsl:for-each-->
														</select>
													</td>
												</tr>
											</table>
										</td>
									</tr>
								</table>
							</td>
						</tr>
						<tr>
							<td>
								<table>
									<tr>
										<td>
											<div class="blacktab">Tables</div>
										</td>
									</tr>
									<tr>
										<td>
											<table class="boxholder">
												<tr>
													<td valign="top">Type: </td>
													<td valign="top" width="350px">
														<!-- No support for active table in asimux.xml, need atsc/enable -->
														<select name="active_table" id="active_table" onchange="show_activeTable()">
															<option value="MPEG"><xsl:if test="/configuration/iptv/iptv_destination[dest_ip_addr=$destipaddr and dest_port=$destport][1]/nit/enable = 'No'"><xsl:attribute name="selected">selected</xsl:attribute></xsl:if>MPEG</option>
															<option value="DVB"><xsl:if test="/configuration/iptv/iptv_destination[dest_ip_addr=$destipaddr and dest_port=$destport][1]/nit/enable = 'Yes'"><xsl:attribute name="selected">selected</xsl:attribute></xsl:if>DVB</option>
														</select>
														<!-- BR/>(DVB-NIT only at this time) -->
													</td>
													<td style="border-left:1px solid black;"></td>
													<td>
														<!-- this span needs to appear ONLY if table type is MPEG or DVB -->
														<span id="NONE" name="NONE">
															<table width="450px">
																<tr>
																	<td></td>
																</tr>
															</table>
														</span>
														<span id="MPEG" name="MPEG">
															<table width="450px">
																<tr>
																	<td>
																		PAT Rate:
																		<select name="pat_rate">
																			<xsl:call-template name="printOptions">
																				<xsl:with-param name="optionName">redundancy_group</xsl:with-param>
																				<xsl:with-param name="defaultValue">
																					<xsl:if test="$function != 'add'">
																						<xsl:value-of select="/configuration/iptv/iptv_destination[dest_ip_addr=$destipaddr and dest_port=$destport][1]/pat_rate"/>
																					</xsl:if>
																				</xsl:with-param>
																				<xsl:with-param name="ignoredValue">disabled</xsl:with-param>
																			</xsl:call-template>
																		</select>
																		tenths
																	</td>
																	<td>
																		Transport Stream ID:
																		<input type="text" name="tsid" value="{nit/transport_stream/@id}" size="15">
																			<xsl:attribute name="value">
																				<xsl:if test="$function != 'add'">
																					<xsl:value-of select="/configuration/iptv/iptv_destination[dest_ip_addr=$destipaddr and dest_port=$destport][1]/nit/transport_stream/@id"/>
																				</xsl:if>
																			</xsl:attribute>
																		</input>
																		<input type="hidden" name="network_id" value="{nit/network_id}">
																			<xsl:attribute name="value">
																				<xsl:if test="$function != 'add'">
																					<xsl:value-of select="/configuration/iptv/iptv_destination[dest_ip_addr=$destipaddr and dest_port=$destport][1]/nit/network_id"/>
																				</xsl:if>
																			</xsl:attribute>
																		</input>
																	</td>
																</tr>
															</table>
														</span>
														<!-- end of active table span -->
													</td>
												</tr>
											</table>
										</td>
									</tr>
								</table>
							</td>
						</tr>
						<tr>
							<td>
								<!--Span DVB should be shown only when Table TYpe DVB is selected. -->
								<span id="DVB" name="DVB">
									<table>
										<tr>
											<td>
												<div class="blacktab">DVB-NIT</div>
											</td>
										</tr>
										<tr>
											<td>
												<span id="nitfields" name="nitfields">
													<table class="boxholder">
														<tr>
															<td>
																<div class="quartercell">
																	<b>Network Name: </b>
																</div>
																<div class="quartercell">
																	<input name="nit_network_name" type="text">
																		<xsl:attribute name="value">
																			<xsl:if test="$function != 'add'">
																				<xsl:value-of select="/configuration/iptv/iptv_destination[dest_ip_addr=$destipaddr and dest_port=$destport][1]/nit/network_name"/>
																			</xsl:if>
																		</xsl:attribute>
																	</input>
																</div>
															</td>
															<td>
																<div class="quartercell">
																	Nit Rate:
																</div>
																<div class="quartercell">
																	<input class="mediumtext" name="rate" type="text">
																		<xsl:attribute name="value">
																			<xsl:if test="$function != 'add'">
																				<xsl:value-of select="/configuration/iptv/iptv_destination[dest_ip_addr=$destipaddr and dest_port=$destport][1]/nit/rate"/>
																			</xsl:if>
																		</xsl:attribute>
																	</input>
																</div>
															</td>
														</tr>
														<tr>
															<td>
																<div class="quartercell">
																	<b>Network ID: </b>
																</div>
																<div class="quartercell">
																	<input class="mediumtext" name="nit_network_id" type="text">
																		<xsl:attribute name="value">
																			<xsl:if test="$function != 'add'">
																				<xsl:value-of select="/configuration/iptv/iptv_destination[dest_ip_addr=$destipaddr and dest_port=$destport][1]/nit/network_id"/>
																			</xsl:if>
																		</xsl:attribute>
																	</input>
																</div>
															</td>
															<td>
																<div class="quartercell">
																	<b>Symbol Rate: </b>
																</div>
																<div class="quartercell">
																	<input class="mediumtext" name="symbol_rate" type="text">
																		<xsl:attribute name="value">
																			<xsl:if test="$function != 'add'">
																				<xsl:value-of select="/configuration/iptv/iptv_destination[dest_ip_addr=$destipaddr and dest_port=$destport][1]/nit/transport_stream/symbol_rate"/>
																			</xsl:if>
																		</xsl:attribute>
																	</input>
																</div>
															</td>
														</tr>
														<tr>
															<td>
																<div class="quartercell">
																	<b>Original Network ID: </b>
																</div>
																<div class="quartercell">
																	<input class="mediumtext" name="nit_orig_network_id" type="text">
																		<xsl:attribute name="value">
																			<xsl:if test="$function != 'add'">
																				<xsl:value-of select="/configuration/iptv/iptv_destination[dest_ip_addr=$destipaddr and dest_port=$destport][1]/nit/original_network_id"/>
																			</xsl:if>
																		</xsl:attribute>
																	</input>
																</div>
															</td>
															<td>
																<div class="quartercell">
																	<b>RF Frequency: </b>
																</div>
																<div class="quartercell">
																	<input class="mediumtext" name="frequency" type="text">
																		<xsl:attribute name="value">
																			<xsl:if test="$function != 'add'">
																				<xsl:value-of select="/configuration/iptv/iptv_destination[dest_ip_addr=$destipaddr and dest_port=$destport][1]/nit/transport_stream/frequency"/>
																			</xsl:if>
																		</xsl:attribute>
																	</input>
																</div>
															</td>
														</tr>
													</table>
												</span>
											</td>
										</tr>
									</table>
								</span>
								<!-- end of DVB span -->
							</td>
						</tr>
					</table>
					</form>
				</div>
			</body>
		</html>
	</xsl:template>
	<xsl:template match="text()">
	</xsl:template>
	<xsl:template match="text()" mode="iptv">
	</xsl:template>
</xsl:stylesheet>
