<?xml version="1.0" encoding="iso-8859-1"?>

<xsl:stylesheet
	version="1.0"
	xmlns="http://www.w3.org/1999/xhtml"
	xmlns:h="http://www.w3.org/1999/xhtml"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:hdoc="http://cking.be/hdoc"
  extension-element-prefixes="hdoc"
	>

  <hdoc:hdoc xmlns="http://cking.be/hdoc">
    <original url="http://users.telenet.be/cking/webstuff/digiTransformer/digiTransformer.xsl"/>
    <author name="Anton Triest" e-mail="anton@cking.be" url="http://www.cking.be/"/>
    <license name="Creative Commons Attribution/ShareAlike" url="http://creativecommons.org/licenses/by-sa/2.0/"/>
  </hdoc:hdoc>

	<!-- output to XHTML 1.0 Strict -->
	<xsl:output
		method="xml"
		version="1.0"
		encoding="iso-8859-1"
		indent="yes"
		doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"
		doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
		/>

	<xsl:param name="param-dial"/>
	<xsl:param name="param-maxDigits"/>
	<xsl:param name="param-minChars"/>
	<xsl:param name="param-dictionary"/>	<!-- UNDER CONSTRUCTION: allow multiple dictionaries -->
	<xsl:param name="param-dictionaries"/>  <!-- note: this comes in like a string, eg. "'<d>english-basic</d><d>geographic-en</d>'" -->

	<xsl:variable name="upper" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>
	<xsl:variable name="lower" select="'abcdefghijklmnopqrstuvwxyz'"/>
	<xsl:variable name="digit" select="'22233344455566677778889999'"/>
	<xsl:variable name="filter" select="'*#-+/.,?!()_:; '"/>

	<xsl:variable name="maxDigits">
		<!-- if it's passed as param then read it, otherwise take -1 (means no max) -->
		<xsl:choose>
			<xsl:when test="string($param-maxDigits)">
				<xsl:value-of select="number($param-maxDigits)"/>
			</xsl:when>
			<xsl:otherwise>-1</xsl:otherwise>
		</xsl:choose>
	</xsl:variable>

	<xsl:variable name="minChars">
		<!-- if it's passed as param then read it, otherwise take -1 (means no min) -->
		<xsl:choose>
			<xsl:when test="string($param-minChars)">
				<xsl:value-of select="number($param-minChars)"/>
			</xsl:when>
			<xsl:otherwise>-1</xsl:otherwise>
		</xsl:choose>
	</xsl:variable>

	<xsl:variable name="dial" select="translate(translate(translate($param-dial,$upper,$lower),$lower,$digit),$filter,'')"/>

<!--
	<xsl:variable name="dictionaries" select="document($param-dictionary)/dictionary"/>
-->

	<xsl:variable name="dicts" select="document('data.xml')/data/list/dict[contains($param-dictionaries,@name)]"/>
	<xsl:variable name="dictionaries" select="document($dicts/@name)/dictionary"/>

	<xsl:variable name="words" select="$dictionaries/w[contains($dial,translate(translate(.,$upper,$lower),$lower,$digit))][$minChars &lt; 0 or string-length(.) &gt;= $minChars]"/>

	<xsl:variable name="phrases">
		<xsl:call-template name="collect-phrases"/>
	</xsl:variable>

	<!--
		core algorithm: borrowed from DoCreatePhrases in Frans Bouma's C# program
		http://blogs.msdn.com/the1/archive/2004/04/06/108717.aspx
	-->
	<xsl:template name="collect-phrases">
		<xsl:param name="index" select="1"/>
		<xsl:param name="phrase" select="''"/>
		<xsl:param name="countDigits" select="0"/>
		<xsl:choose>
			<xsl:when test="$index &gt; string-length($dial)">
				<!-- complete phonenumber has been processed: write this phrase to the output tree -->
				<li><xsl:value-of select="$phrase"/></li>
			</xsl:when>
			<xsl:otherwise>
				<xsl:if test="$maxDigits &lt; 0 or $countDigits &lt; $maxDigits">
					<!-- simply use the digit as a 'word' in the phrase and continue with the next digit -->
					<xsl:call-template name="collect-phrases">
						<xsl:with-param name="index" select="$index + 1"/>
						<xsl:with-param name="phrase" select="concat($phrase, substring($dial, $index, 1))"/>
						<xsl:with-param name="countDigits" select="$countDigits + 1"/>
					</xsl:call-template>
				</xsl:if>
				<xsl:for-each select="$words">
					<xsl:if test="($index + string-length(.) - 1) &lt;= string-length($dial)">
						<xsl:variable name="value" select="translate(translate(., $upper, $lower), $lower, $digit)"/>
						<xsl:if test="starts-with(substring($dial, $index), $value)">
							<!-- match found: fill in word and recurse into the rest of the string -->
							<xsl:call-template name="collect-phrases">
								<xsl:with-param name="index" select="$index + string-length(.)"/>
								<xsl:with-param name="phrase" select="concat($phrase, .)"/>
								<xsl:with-param name="countDigits" select="$countDigits"/>
							</xsl:call-template>
						</xsl:if>
					</xsl:if>
				</xsl:for-each>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<xsl:template match="/">
		<!-- input root element: start generating xhtml output -->
		<div id="xslt-result">
			<xsl:call-template name="generate-ui"/>
			<div id="result">
				<!--<div id="dump"><xsl:copy-of select="$param-dictionaries"/></div>-->
				<xsl:choose>
					<xsl:when test="string($dial)">
						<xsl:call-template name="print-input"/>
						<xsl:call-template name="print-dictionary"/>
						<xsl:call-template name="print-words"/>
						<xsl:choose>
							<xsl:when test="string($phrases)">
								<p>
									<xsl:variable name="count" select="string-length($phrases) div string-length($dial)"/>
									<b><xsl:value-of select="$count"/></b>
									<xsl:choose>
										<xsl:when test="$count = 1"> phrase:</xsl:when>
										<xsl:otherwise> phrases:</xsl:otherwise>
									</xsl:choose>
								</p>
								<ul class="phrases">
									<xsl:copy-of select="$phrases"/>
								</ul>
							</xsl:when>
							<xsl:otherwise>
								<p>no phrases found</p>
							</xsl:otherwise>
						</xsl:choose>
					</xsl:when>
					<xsl:otherwise>
						<xsl:call-template name="no-input"/>
					</xsl:otherwise>
				</xsl:choose>
			</div>
			<xsl:call-template name="license"/>
		</div>
	</xsl:template>

  <xsl:template name="generate-ui">
    <div id="ui">
			<xsl:call-template name="generate-map"/>
      <form id="input" action="" onsubmit="return false;">
				<div id="brand">
					<a href="http://users.telenet.be/cking/webstuff/digiTransformer/info.html" title="about the digiTransformer">digiTransformer</a>
					<span> v2 &#0169; </span>
					<a href="mailto:anton@cking.be?subject=[webstuff:digiTransformer] " title="anton@cking.be">cking</a>
				</div>
				<p>
					<input type="text" id="input-dial" onchange="Go();"/>
				</p>
				<p>
					<input type="checkbox" id="input-doMax" onclick="Clicked('input-doMax');"/>
					<span> max digits </span>
					<select id="input-maxDigits" onchange="Go();" style="visibility:hidden;">
						<option value="0">0 &#0160;</option>
						<option value="1">1</option>
						<option value="2">2</option>
						<option value="3">3</option>
						<option value="4">4</option>
						<option value="5">5</option>
					</select>
				</p>
				<p>
					<input type="checkbox" id="input-doMin" onclick="Clicked('input-doMin');"/>
					<span> min chars </span>
					<select id="input-minChars" onchange="Go();" style="visibility:hidden;">
						<option value="2">2 &#0160;</option>
						<option value="3">3</option>
					</select>
				</p>
				<p>
					<select id="input-dictionary" ondblclick="Go();" size="3" multiple="multiple">
						<xsl:apply-templates select="document('data.xml')/data/list/dict" mode="option"/>
					</select>
					<input id="cmd" type="text" readonly="readonly"/>
					<!--<input type="hidden" id="dummy"/>-->
				</p>
      </form>
    </div>
  </xsl:template>

  <xsl:template match="data/list/dict" mode="option">
		<option value="&lt;d&gt;{@name}&lt;/d&gt;"><xsl:value-of select="@id"/></option>
	</xsl:template>

  <xsl:template name="generate-map">
		<xsl:apply-templates select="document('image-map.xml')/image-map/h:div[@id='image-map']/*"/>
  </xsl:template>

  <xsl:template match="h:map | h:img">
		<xsl:copy>
			<xsl:copy-of select="@*"/>
			<xsl:apply-templates select="*"/>
		</xsl:copy>
  </xsl:template>

  <xsl:template match="h:area">
		<xsl:copy>
			<xsl:copy-of select="@*"/>
			<xsl:attribute name="alt"/>
			<xsl:variable name="id" select="@id"/>
			<xsl:variable name="cmd" select="ancestor::image-map/commands/cmd[@id=$id]"/>
			<xsl:if test="string($cmd/@func)">
				<xsl:attribute name="href">javascript:<xsl:value-of select="$cmd/@func"/>;</xsl:attribute>
			</xsl:if>
			<xsl:if test="string($cmd/@msg)">
				<xsl:attribute name="onmouseover">ShowCmd('<xsl:value-of select="$cmd/@msg"/>');</xsl:attribute>
				<xsl:attribute name="onmouseout">ShowCmd('');</xsl:attribute>
			</xsl:if>
		</xsl:copy>
  </xsl:template>

	<xsl:template name="no-input">
		<p class="empty-dial" id="no-input">&#0171; enter phone number or phrase</p>
  </xsl:template>

	<xsl:template name="print-input">
		<p>input: <b><xsl:value-of select="$param-dial"/></b></p>
		<p>dial <span class="digital"><xsl:value-of select="$dial"/></span></p>
  </xsl:template>

	<xsl:template name="print-dictionary">
		<xsl:variable name="nDicts" select="count($dicts)"/>
		<xsl:variable name="nWords" select="count($dictionaries/w)"/>
		<xsl:choose>
			<xsl:when test="$nDicts = 0">
				<p>no dictionary</p>
			</xsl:when>
			<xsl:when test="$nDicts = 1">
				<p>
					dictionary:
					<a href="javascript:OpenDict('{$dicts/@name}', {$nWords});">
						<xsl:value-of select="$dicts/@id"/>
					</a>
					(<xsl:value-of select="$nWords"/> words)
				</p>
			</xsl:when>
			<xsl:otherwise>
				<p>
					dictionaries (total <xsl:value-of select="$nWords"/> words):
					<ul class="dict-list">
						<xsl:for-each select="$dicts">
							<xsl:variable name="count" select="count(document(@name)/dictionary/w)"/>
							<li><a href="javascript:OpenDict('{@name}', {$count});"><xsl:value-of select="@id"/></a></li>
						</xsl:for-each>
					</ul>
				</p>
			</xsl:otherwise>
		</xsl:choose>
  </xsl:template>

	<xsl:template name="print-words">
		<p>
			<xsl:choose>
				<xsl:when test="count($words)">
					subset:
					<b>
						<xsl:for-each select="$words">
							<xsl:value-of select="."/>&#160;
						</xsl:for-each>
					</b>
					(<xsl:value-of select="count($words)"/> words)
				</xsl:when>
				<xsl:otherwise>no words</xsl:otherwise>
			</xsl:choose>
		</p>
  </xsl:template>

	<xsl:template name="license">
    <div id="license">
			<a href="http://creativecommons.org/licenses/by-sa/2.0/">
				<img src="img/somerights.gif" width="88px" height="31px" alt="creativecommons" title="Creative Commons Attribution/ShareAlike License"/>
			</a>
			<span class="note"> built with XHTML, CSS, XSLT &amp; JavaScript</span>
    </div>
	</xsl:template>

</xsl:stylesheet>

