// General useful functions

htmlroot = "/";
cgiroot = "/cgi-bin/";

function formtag(scriptName) {
  document.write('<form method="post" action="' + cgiroot + scriptName + '">');
}

function shareLink() {
  document.writeln('<!-- AddThis Button BEGIN -->');
  document.writeln('<a class="addthis_button" href="http://www.addthis.com/bookmark.php?v=250&amp;pub=crispyduck"><img');
  document.writeln('src="http://s7.addthis.com/static/btn/v2/lg-share-en.gif" width="125" height="16" alt="Bookmark and Share"');
  document.writeln('style="border:0"/></a><script type="text/javascript"');
  document.writeln('src="http://s7.addthis.com/js/250/addthis_widget.js?pub=crispyduck"></script>');
  document.writeln('<!-- AddThis Button END -->');
}

function formtagGET(scriptName) {
  document.writeln('<form method="get" target="_top"  action="' + cgiroot + scriptName + '">');
}

function cgiLink(scriptName) {
  document.writeln('<a href="' + cgiroot + scriptName + '">');
}

function cgiLinkNew(scriptName) {
  document.writeln('<a href="' + cgiroot + scriptName + '" target=_blank>');
}

function redLink(scriptName) {
  location.replace(cgiroot + scriptName);
}

function setDefDate(obj) {
  if (obj.value == "dd/mm") {
    today = new Date();
    obj.value = today.getDate() + "/" + (today.getMonth() + 1);
  }
}

function anchortag(scriptName, parameters) {
  document.writeln('<a href="' + cgiroot + scriptName + '?' + parameters + '">');
}

function anchortaghtml(docName) {
  document.writeln('<a href="' + htmlroot + docName + '">');
}

function putDate() {
  today = new Date();
  fullDate = today.toLocaleString();
  dateOnly = fullDate.slice(0, fullDate.lastIndexOf(" "));
  document.write(dateOnly);
}

function header() {
  document.write('<table bgcolor="#000000" width="100%" cols="3" border=0 cellpadding=0><tr>');
  document.write('<td align="left"><a href="' + htmlroot + 'index.htm" target="_parent">');
  document.write('<img border=0 src="' + htmlroot + 'images/logo.jpg"></a></td>');
  document.write('<td align="right" valign="top"><font face="arial,helvetica,sans-serif" color="white"><small><script>putDate();</script></small><br><br>');
  document.write('<a href="' + htmlroot + '" target="_parent">home</a><br>');
  document.write('<a href="' + htmlroot + 'guestbook.htm" target="_parent">guest book</a><br><br>');
  selectPage();
  document.write('</td></tr></table>');
}

// constructor for Product links
function Product(name, page) {
  this.name = name;
  this.page = page;
}

var pArray = new Array();
var i = 0;

pArray[i++] = new Product("Marquee Plus", "marquee.htm");
pArray[i++] = new Product("Scroller", "scroller/index.htm");
pArray[i++] = new Product("Doshometer", "doshometer/index.htm");
pArray[i++] = new Product("Elvis", "elvis/index.htm");
pArray[i++] = new Product("Four Forces", "fourforces/index.htm");
pArray[i++] = new Product("SAS Tips", "sastips/index.htm");
pArray[i++] = new Product("SAS transport file format", "sasxpt/index.htm");
pArray[i++] = new Product("dsread - SAS7BDAT reader", "dsread/index.htm");
pArray[i++] = new Product("SAS7BDAT file format", "sas7bdat/index.htm");

function selectPage() {
  document.write('<select id="pageSelector" onChange="selectorChanged();"><option value="-1">-- Select page --</option>');
  for (i=0; i < pArray.length; i++) {
    document.write('<option value="' + i + '">' + pArray[i].name + '</option>');
  }
  document.write('</select>');
}

function selectorChanged() {
  var newPage = document.getElementById('pageSelector').value;
  if (newPage > -1) {
    window.location = htmlroot + pArray[newPage].page;
  }
}

function footer() {
  var lastmod = new Date();
  lastmod.setTime(Date.parse(document.lastModified));     // get string of last modified date
  datestring = lastmod.toLocaleString();
  dateOnly = datestring.slice(0, datestring.lastIndexOf(" "));  // trim off the time part
  if (lastmod != 0){              // unknown date (or January 1, 1970 GMT)
    document.write("<br>Last update: " + dateOnly); //dateOnly)
  }
}

function makeEmail(name, host, text) {
  var addy = name + '@' + host;
  if (text == null) { text = addy; }
  document.write('<a href="mailto:' + addy + '">' + text + '</a>');
}

var xmlHttp;
var pageID = document.URL.slice(document.URL.lastIndexOf('.uk/')+3);
if (pageID.indexOf('.htm') == -1) pageID += 'index.htm';

function createAjax()
{
  try {
    // Firefox, Opera 8.0+, Safari
    xmlHttp = new XMLHttpRequest();
    return true;
  }
  catch (e) {
    // Internet Explorer
    try {
      xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
      return true;
    }
    catch (e) {
      try {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        return true;
      }
      catch (e) {
        alert("Your browser does not support AJAX!");
        return false;
      }
    }
  }
}

function commentsLoaded() {
  if(xmlHttp.readyState == 4) {
    commdiv = document.getElementById('comments');
    commdiv.innerHTML = xmlHttp.responseText;
  }
}

function loadComments(showStatus) {

  if (createAjax()) {

    if (showStatus) {
      commdiv = document.getElementById('comments');
      commdiv.innerHTML = "Loading comments for page " + pageID + "...";
    }

    xmlHttp.onreadystatechange = commentsLoaded;
    xmlHttp.open("GET", "/cgi-bin/getComments.pl?page=" + pageID, true);
    xmlHttp.send(null);

  } else {

    commdiv = document.getElementById('comments');
    commdiv.innerHTML = "Your browser does not support fetching comments.";

  }

}

function commentSaved() {
  if(xmlHttp.readyState == 4) {
    sp = document.getElementById('savePanel');
    sp.innerHTML = "Comment saved:<br>" + xmlHttp.responseText;
    // reload the comments, including the new one
    loadComments(false);
  }
}

function saveComment() {

  var name    = document.getElementById("addCommentName").value;
  var email   = document.getElementById("addCommentEmail").value;
  var web     = document.getElementById("addCommentWeb").value;
  var comment = document.getElementById("addCommentComment").value;

  if (name != "" && comment != "") {

    if (createAjax()) {

      sp = document.getElementById('savePanel');
      sp.innerHTML = "Posting comment...";

      var params;
      params = "page=" + pageID;
      params += "&name="    + escape(name);
      params += "&email="   + escape(email);
      params += "&web="     + escape(web);
      params += "&comment=" + escape(comment);

      xmlHttp.onreadystatechange = commentSaved;
      xmlHttp.open("POST", "/cgi-bin/saveComment.pl", true);
      xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      xmlHttp.setRequestHeader("Content-length", params.length);
      xmlHttp.setRequestHeader("Connection", "close");
      xmlHttp.send(params);

    } else {

      sp = document.getElementById('savePanel');
      sp.innerHTML = "Your browser does not support saving comments.";

    }

  } else {

    alert("The Name and Comment fields must not be empty!");

  }

}
