// survey scripts go here
function setCancel() {
  var c = document.getElementById('cancel');
  c.onclick = function(){ return customCancel(); };
}
//window.onload.actions.push(setCancel);
function show(what)
{
  what = document.getElementById(what);
  what.className = '';
}
function hide(what)
{
  what = document.getElementById(what);
  what.className = 'hidden';
}
function customCancel()
{
  var width = 200;
  var height = 200;

  // create the cancel div
  var newDiv = document.createElement("div");
  newDiv.setAttribute("id","cancelDiv");

  // create the paragraph
  var newDivParagraph = document.createElement("p");
  var divText = document.createTextNode("Choose yes to cancel the survey and return to the beginning (all survey data will be lost). Choose no to close this box and continue.");
  newDivParagraph.appendChild(divText);
  newDiv.appendChild(newDivParagraph);

  // create yes link
  var newDivLink1 = document.createElement("a");
  newDivLink1.setAttribute("href","cancel.php?cancel=yes");
  newDivLink1.setAttribute("title","Cancel this survey");
  newDivLink1.className = "button";
  var newDivLinkText1 = document.createTextNode("Yes");
  newDivLink1.appendChild(newDivLinkText1);
  newDiv.appendChild(newDivLink1);

  // create no link
  var newDivLink2 = document.createElement("a");
  newDivLink2.setAttribute("href","#");
  newDivLink2.onclick = function(){ return destroyCancel(); };
  newDivLink2.setAttribute("title","Do not cancel this survey");
  newDivLink2.className = "button";
  var newDivLinkText2 = document.createTextNode("No");
  newDivLink2.appendChild(newDivLinkText2);
  newDiv.appendChild(newDivLink2);

  // attach it all to the page
  document.getElementById("otherBody").appendChild(newDiv);
  newDivLink2.focus();
  return false;
}

var isNN = (navigator.appName.indexOf("Netscape")!=-1);
function autoTab(input,len, e)
{
  var keyCode = (isNN) ? e.which : e.keyCode;
  var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
  if (input.value.length >= len && !containsElement(filter,keyCode))
  {
    input.value = input.value.slice(0, len);
    input.form[(getIndex(input)+1) % input.form.length].focus();
  }
  function containsElement(arr, ele)
  {
    var found = false, index = 0;
    while (!found && index < arr.length)
      if(arr[index] == ele)
        found = true;
      else
        index++;
      return found;
  }
  function getIndex(input)
  {
    var index = -1, i = 0, found = false;
    while (i < input.form.length && index == -1)
      if (input.form[i] == input)
        index = i;
      else
        i++;
      return index;
    }
  return true;
}
function destroyCancel() {
  var div = document.getElementById('cancelDiv');
  div.parentNode.removeChild(div);
  return false;
}

// moves to the next field
function moveOn(what) {
  if (what.value.length == what.getAttribute('maxlength')) {
    if (what.nextSibling) {
      // jump to the next input, not the text
      what.nextSibling.nextSibling.focus();
    }
  }
}

