<?xml version="1.0" encoding="UTF-8"?>
<!--
/*
Copyright (c) 2004-2008, Dave Gullo <dave@gullo.tv>
{{{
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are prohibited.

Neither the name of the Dave Gullo nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-->
<xsl:transform version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  xmlns:php="http://snowboards-for-sale.com/ns/phpxb"
  xmlns:mysql="http://snowboards-for-sale.com/ns/mysqlxb"
  >
<xsl:output
  method="xml"
  omit-xml-declaration="no"
  encoding="UTF-8"
  indent="yes"
  doctype-system="http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"
  doctype-public="-//W3C//DTD XHTML 1.1//EN"
/>
<xsl:param name="class"/>
<xsl:param name="mode"/>
<xsl:param name="classfile"/>
<xsl:param name="saveuri"/>
<xsl:param name="saved"/>
<xsl:param name="refresh"/>
<xsl:variable name="schema" select="document($classfile)"/>
<xsl:include href="xsl/border.xsl"/>
<xsl:template match ="/">
<xsl:variable name="doc" select="/"/>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <meta http-equiv="Author" content="Dave Gullo; dave@gullo.tv"/>
    <title><xsl:value-of select="$mode"/><xsl:text> </xsl:text><xsl:value-of select="$class"/> : forminator 2.0</title>
    <link rel="stylesheet" type="text/css" href="/border.css"/>
    <style type="text/css">
    td {
      font-size: 10px;
      color: #000000;
    }
    input, textarea, select {
      font-size: 11px;
    }
    </style>
    <script src="/js/util.js" type="text/javascript"><xsl:text> </xsl:text></script>
  </head>
  <body style="border: 0px; margin: 0px; background-image: url(/pics/zen-retreat2.jpg); background-repeat: no-repeat; text-align: left;">
    <div style="margin-top: 12px; text-align: left; margin-left: 18px;">
    <xsl:call-template name="border">
      <xsl:with-param name="body">
    <table cellpadding="1" cellspacing="0" style="width: 600px; font-size: 12px; height: 100%;" id="table">
      <tr>
        <td><br/></td>
        <td valign="top" style="width: 100%;">
<xsl:if test="$saved = 1">
<div id="saved" style=" font-size: 18px;text-align: left; position: absolute; color: #ff0000; background: #ffff00; opacity: 0.7; padding: 4px; border: 1px #0000ff dashed; width: 490px; filter: alpha(opacity=70)">
  <xsl:value-of select="$class"/> Saved
</div>
<script type="text/javascript">
setTimeout('hide()', 3000);
function hide() {
  var span = document.getElementById('saved');
  span.style.visibility = 'hidden';
}
var loaded = false;
</script>
</xsl:if>
<xsl:if test="$refresh = 1 and $saved = 1">
<script type="text/javascript">
  var cancel = false;
  setTimeout('refresh()', 100);
  function refresh() {
    try {
      var topframe = findTop();
      if (topframe == null) {
        return;
      }
      var myparent = topframe.docOpener(this);
      myparent.location.reload(); // = myparent.location;
      setTimeout('closeMe()', 2000);
    } catch (e) {
    }
  };
  function closeMe() {
    if (!cancel) {
      self.close();
    }
  }
  window.onclick = function () {
    //-= avoid auto-closing of this window...
    cancel = true; 
  };
</script>
</xsl:if>

<xsl:call-template name="form"/>
        <br/>
        <div align="center" style="visibility: hidden;">
        <!-- Handy for generating arrays of all our element names -->
        <xsl:for-each select="$schema/xsd:schema/xsd:element[1]/xsd:complexType/xsd:all/xsd:element">
        '<xsl:value-of select="@name"/>',
        </xsl:for-each>
        </div>
        </td>
        <td><br/></td>
      </tr>
    </table>
<script type="text/javascript">
function updateCell(input) {
  String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ""); };
  var value;
  if (input.type.toString().match(/^select/)) {
    value = input[input.selectedIndex].value;
  } else if (input.type.toString().match(/text/)) {
    value = input.value.trim();
  }
  //-= Scrub out non-numerics
  switch (input.name) {
<!--
  enumerate all XSD integer types here and strip them down to numeric values
eg:
        <xsd:element name="customerid" minOccurs="1" maxOccurs="1" type="integerOrNULL">
          <xsd:annotation>
            <xsd:appinfo>
              <mysql:columnName><![CDATA[customerid]]></mysql:columnName>
              <mysql:datatype><![CDATA[int(11) unsigned]]></mysql:datatype>
              <php:visibility><![CDATA[public]]></php:visibility>
            </xsd:appinfo>
          </xsd:annotation>
        </xsd:element>
-->
<xsl:for-each select="$schema/xsd:schema/xsd:element[1]/xsd:complexType/xsd:all/xsd:element[contains(xsd:annotation/xsd:appinfo/mysql:datatype, 'int')]">
     case '<xsl:value-of select="@name"/>':
</xsl:for-each>
    case 'foo':
      value = value.replace(/[^0-9\.]/g, '');
  }
<!--
  enforce XSD regexs here... do not update if not valid, leave cell in a 'red' state demonstrating unsaved items
eg:
        <xsd:element name="attention" minOccurs="0" maxOccurs="1" type="xsd:string">
          <xsd:annotation>
            <xsd:appinfo>
              <mysql:columnName><![CDATA[attention]]></mysql:columnName>
              <mysql:datatype><![CDATA[varchar(35)]]></mysql:datatype>
              <php:visibility><![CDATA[public]]></php:visibility>
            </xsd:appinfo>
          </xsd:annotation>
          <xsd:restriction base="xsd:string">
            <xsd:pattern value="[0-9]*"/>
          </xsd:restriction>
        </xsd:element>
-->
<xsl:for-each select="$schema/xsd:schema/xsd:element[1]/xsd:complexType/xsd:all/xsd:element[xsd:restriction/xsd:pattern]">
  if (input.name == '<xsl:value-of select="@name"/>') {
    if (value.match(/^<xsl:value-of select="xsd:restriction/xsd:pattern/@value"/>$/) != value) {
      input.focus();
      input.style.background = '#ffcccc';
      alert('Invalid value for '+ input.name);
      return false;
    } else {
      input.style.background = '#ffffff';
    }
  }
</xsl:for-each>
  var url = '/user/save/<xsl:value-of select="$class"/>/<xsl:value-of select="$doc//@id"/>/ifra/';
  var data = input.name +'='+ encodeURIComponent(value); 
  httpRequest.field = input;
  if (httpRequest.readyState != 4) {
    //-= Don't bother till the last request is done..
    //return;
  }
  window.status = 'Please wait updating "'+ input.name +'" ....';
  httpRequest.onreadystatechange = function() { updateCallback(httpRequest); };
  httpRequest.open('POST', url, true);
  httpRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  httpRequest.setRequestHeader("Content-length", data.length);
  httpRequest.setRequestHeader("Connection", "close");
  httpRequest.send(data);
}

  </script>
  <script src="/js/forminator.js">&#160;</script>
      </xsl:with-param>
    </xsl:call-template>
    </div>
  <span id="save"/>
  </body>
</html>
</xsl:template>

<xsl:template name="form">
<xsl:variable name="doc" select="/"/>
<form action="{$saveuri}" method="post" style="margin-top: 0px; margin-bottom: 0px;">
<input type="hidden" name="id" value="{$doc//@id}"/>
<input type="hidden" name="refresh" value=""/>
<table border="0" cellpadding="1" cellspacing="0" style="width: 100%;">
<input type="hidden" name="{$schema/xsd:schema/xsd:element[1]//mysql:primaryKey}" value="{$doc//@id}"/>
<xsl:for-each select="$schema/xsd:schema/xsd:element[1]/xsd:complexType/xsd:all/xsd:element[not(xsd:annotation/xsd:appinfo/php:transient)]">
  <xsl:variable name="element" select="@name"/>
  <xsl:variable name="value" select="$doc/*/*[name() =  $element]"/>
  <tr>
    <td style="width: 100px; font-weight: 800; text-align: right; vertical-align: top;">
    <xsl:value-of select="@name"/>
    </td>
    <td style="padding-right: 4px;">
    <xsl:choose>
      <xsl:when test="./xsd:annotation/xsd:appinfo[mysql:datatype = 'text']">
        <xsl:variable name="height">
          <xsl:choose>
            <xsl:when test="string-length($value) &gt; 800">
              <xsl:text>160px</xsl:text>
            </xsl:when>
            <xsl:when test="string-length($value) &gt; 400">
              <xsl:text>100px</xsl:text>
            </xsl:when>
            <xsl:when test="string-length($value) &gt; 100">
              <xsl:text>80px</xsl:text>
            </xsl:when>
            <xsl:otherwise>
              <xsl:text>28px</xsl:text>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:variable>
        <textarea name="{@name}" cols="30" rows="4" style="width: 100%; height: {$height}; border: 1px #000000 solid;" id="{@name}" onchange="updateCell(this);"><xsl:value-of select="$value"/><xsl:text>
</xsl:text></textarea>
      </xsl:when>
      <xsl:when test="starts-with(./xsd:annotation/xsd:appinfo/mysql:datatype, 'enum')">
        <select name="{@name}" id="{@name}" onchange="updateCell(this);">
        <xsl:for-each select="./xsd:annotation/xsd:appinfo/mysql:enum">
          <option value="{.}">
            <xsl:if test="$value = .">
              <xsl:attribute name="selected">
                <xsl:text>SELECTED</xsl:text>
              </xsl:attribute>
            </xsl:if>
            <xsl:value-of select="."/>
          </option>
        </xsl:for-each>
        </select>
      </xsl:when>
      <xsl:when test="./xsd:annotation/xsd:appinfo[mysql:datatype != 'text']">
        <input type="text" name="{@name}" value="{$value}" style="width: 100%; border: 1px #000000 solid;" id="{@name}" onchange="updateCell(this);">
          <xsl:if test="@name = 'query' or @name = 'clientid' or count(xsd:annotation/xsd:appinfo/php:disabled) &gt; 0"><!-- cheap hack cause these are immutable -->
            <xsl:attribute name="disabled"><xsl:text>disabled</xsl:text></xsl:attribute>
          </xsl:if>
        </input>
      </xsl:when>
      <xsl:when test="not(./xsd:annotation/xsd:appinfo/mysql:datatype)">
        <input type="text" name="{@name}" value="{$value}" style="width: 100%; border: 1px #000000 solid;" disabled="disabled"/>
      </xsl:when>
    </xsl:choose>
    </td>
  </tr>
</xsl:for-each>
</table>
</form>
</xsl:template>
</xsl:transform>
