<!-- !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" -->
<html>
<head>
<title>DataSnap REST Project</title>
<link rel="stylesheet" type="text/css" href="css/main.css" />
<script type="text/javascript" src="js/base64.js"></script>
<script type="text/javascript" src="js/json2.js"></script>
<script type="text/javascript" src="js/serverfunctionexecutor.js"></script>
<script type="text/javascript" src="js/connection.js"></script>
<script type="text/javascript" src="<#serverfunctionsjs>"></script>
<script type="text/javascript">

var loginRequired = false;

function onLoad()
{
  showTime();
  loginRequired = <#loginRequired>;
  setConnection('<#host>', '<#port>', '<#urlpath>');
  if (loginRequired)
  {
    showLogin(true);
  }
  else
  {
    showLogin(false);
  }
}

function onLogin()
{
  if (loginRequired)
  {
    if (AdminInst == null)
    {
        if (!setCredentials(document.getElementById('userField').value, document.getElementById('passwrdField').value))
        {
          loginCorrect(false);
          return;
        }
        else
        {
          loginCorrect(true);
          showLogin(false);
        }
    }
  }
  else
    showLogin(false);
}

function loginCorrect(isCorrect)
{
  var errorDiv = document.getElementById('loginError');
  if ( errorDiv != null )
  {
    errorDiv.innerHTML= isCorrect ? "" : "login incorrect";
  }
}

function showLogin(show)
{
  var loginDiv = document.getElementById('logindiv');
  var contentDiv = document.getElementById('contentdiv');
  if (show)
  {
      // show div
      loginDiv.style.display="block";
      contentDiv.style.display="none";
  }
  else
  {
      // show div
      loginDiv.style.display="none";
      contentDiv.style.display="block";
  }
}

function showTime()
{
  var d = new Date();
  var h = d.getHours();
  var m = d.getMinutes();
  var s = d.getSeconds();
  var timeElement = document.getElementById('timeElement');
  if ( timeElement != null )
  {
    timeElement.innerText=
      (h <= 9 ? "0" : "") + h + ":" +
      (m <= 9 ? "0" : "") + m + ":" +
      (s <= 9 ? "0" : "") + s;
  }
}

function serverMethods()
{
  return new <#classname>(connectionInfo);
}

function onReverseStringClick()
{
  if (loginRequired && (AdminInst == null))
  {
    showLogin(true);
    return;
  }
  var valueField = document.getElementById('valueField');
  var s = serverMethods().ReverseString(valueField.value);
  valueField.value = s.result;
}

</script>
</head>
<body onload="onLoad()">
  <#serverfunctioninvoker>
    <h1>DataSnap REST Project</h1>
    <div> Page loaded at <span id="timeElement"></span>
    </div>
    <div id="logindiv" style="display:none">
      <p class="divlabel">Login</p><br />
      <form onsubmit="onLogin(); return false;">
        <table class="authtable">
          <tr>
            <td>User Name:</td>
            <td><input id="userField" class="loginField" type="text" /></td>
          </tr>
          <tr>
            <td>Password:</td>
            <td><input id="passwrdField" class="loginField" type="password" /></td>
          </tr>
        </table>
        <div id="loginError" class="errorMsg"></div><br /><input id="loginButton" type="submit" value="LOG IN" />
      </form>
    </div>
    <div id="contentdiv" class="contentdiv" style="display:none">
      <table>
        <tr>
          <td><input id="valueField" class="loginField" type="text" value="A B C" /></td>
          <td><button onclick='javascript:onReverseStringClick();'>ReverseString</button></td>
        </tr>
      </table>
    </div>
</body>
</html>
