// <script>
var KEY_ENTER = 13;
var oElements = new Object();

function Cookie(sName, sValue, iHours, sPath, sDomain, bSecure, bUnescape)
{
  this._name = escape(sName).replace(/\./, "%2E");
  this.Value = (sValue)?sValue:"";

	this._unescape=(bUnescape==true)?true:false;

  if (iHours)
    this._expiration = new Date((new Date()).getTime() + (iHours*3600000))
  else
    this._expiration = null;
  
  this._path = (sPath) ? sPath : null;
  this._domain = (sDomain) ? sDomain : null;
  this._secure = (bSecure) ? bSecure : null;

  this.Load = _Cookie_load;
  this.Store = _Cookie_store;
  this.Load = _Cookie_load;
  this.Remove = _Cookie_remove;
  this.HasKeys = _Cookie_HasKeys;
  this.Load();
}

function _Cookie_HasKeys()
{
  for (var prop in this)
  {
    if ((prop.charAt(0) == '_') || 
        (typeof(this[prop]) == 'function') ||
        (prop.search(/^value$/i) > -1)) continue;
    return true;
  }
  return false;
}

// this function is the "private" implementation of the "public" store method
function _Cookie_store()
{
  var sCookieVal = '';
  for (var prop in this)
  {
    if ((prop.charAt(0) == '_') || (typeof(this[prop]) == 'function')) continue;
    if (sCookieVal != '') sCookieVal += '&';
    sCookieVal += prop + '=' + ((this._unescape)?escape(this[prop]):this[prop]);
  }
  var sCookie = (sCookieVal) ? this._name + '=' + sCookieVal : this._name + '=' + escape(this.Value);
  if (this._expiration) sCookie += '; expires=' + this._expiration.toGMTString();
  if (this._path) sCookie += '; path=' + this._path;
  if (this._domain) sCookie += '; domain=' + this._domain;
  if (this._secure) sCookie += '; secure';

  document.cookie = sCookie;
}

function _Cookie_load()
{
  var sAllCookies = document.cookie;
  if (sAllCookies == '') return false;
  
  var iStart = sAllCookies.indexOf(this._name + '=');
  if (iStart == -1) return false;
  iStart += this._name.length + 1;
  var iEnd = sAllCookies.indexOf(';', iStart);
  if (iEnd == -1) iEnd = sAllCookies.length;
  
  var sCookieVal = sAllCookies.substring(iStart, iEnd);
  
  if (sCookieVal.indexOf('&') > -1)
  {
    var aNameValues = sCookieVal.split('&');
    for (var i = 0; i < aNameValues.length; ++i)
      aNameValues[i] = aNameValues[i].split('=');
  
    for (var i = 0; i < aNameValues.length; ++i)
      this[aNameValues[i][0]] = (this._unescape) ? unescape(aNameValues[i][1]) : aNameValues[i][1];
    this.Value = "";
  }
  else
  {
		if (sCookieVal && sCookieVal.indexOf("=")>=0)
		{
			if (this._unescape)
				this.Value = (sCookieVal) ? unescape(sCookieVal.split("=")[1]) : "";
			else
				this.Value = (sCookieVal)?sCookieVal.split("=")[1]:"";
		}
		else
		{
			this.Value = (sCookieVal) ? sCookieVal : "";
		}
  }
  
  return true;
}

function _Cookie_remove()
{
  var sCookie;
  sCookie = this._name + '=';
  if (this._path) sCookie += '; path=' + this._path;
  if (this._domain) sCookie += '; domain=' + this._domain;
  sCookie += '; expires=Fri, 02-Jan-1970 00:00:00 GMT';
  
  document.cookie = sCookie;
}

function InitAnswerSheet()
{
  if (document.getElementById && Ctrl.mode == "I") 
    document.body.style.overflow='hidden';
  if(document.layers)
    document.captureEvents(Event.KEYPRESS);
  
  document.onkeypress = CheckEnter;

  if (document.all||document.getElementById)
  {
    for (var i = 0, len = document.forms.length; i < len; ++i)
    {
      if (document.all)
      {
        oElements['answer' + (i+1)] = false;
        document.forms[i]['answer' + (i+1)].onblur = InputBlur;
      }
      
      document.forms[i].onsubmit=(document.all&&!document.getElementById) ? function(){CheckEnter();return false;} : function(){return false;};
      //document.forms[i].onsubmit=function(){return false;};
    }
  }
}

function InputBlur()
{
  if (oElements[this.name] == true)
  {
    oElements[this.name] = false;
    this.focus();
  }
}

function CheckEnter(evt)
{
  if(document.all)
  {
		//IE4 hack alert!
    if(event.keyCode == KEY_ENTER || (document.all && !document.getElementById && event.type=="submit"))
    {
      if (Ctrl.mode == "I")
      {
        var frmNav = GetFrame("nav");
        if (!frmNav.bReady) return false;
        frmNav.MoveQuestion(1);
        oElements[event.srcElement.name] = false;
      }
      else
      {
        RotateFocus(event.srcElement.name);
      }
      return false; // stop processing this event
    }
  }
  else
  {
    if(evt.which != KEY_ENTER)
    {
      routeEvent(evt); // don't stop processing
    }
    else
    {
      if (Ctrl.mode == "I")
      {
        var frmNav = GetFrame("nav");
        if (!frmNav.bReady) return false;
        frmNav.MoveQuestion(1);
      }
      else
      {
        RotateFocus(evt.target.name);
      }
      return false;
    }
  }
}

function RotateFocus(sAnswerInput)
{
  var iAnswer = sAnswerInput.substr(6);
  if (!isNaN(iAnswer))
  {
    iAnswer = parseInt(iAnswer);
    if (iAnswer == document.forms.length)
    {
      var frmNav = GetFrame("nav");
	  //frmNav.FinishPaperTest();
		  frmNav.document.DoneForm.DoneButton.focus();
    }
    else
    {
      document.forms["form" + (iAnswer+1)]["answer" + (iAnswer+1)].focus();
    }
  }
}

function GetFrame(frameName, level)
{
  var theFrame = null;
  
  if(!level)
    level = top;
  
  var i;
  for(i = 0; i < level.frames.length; i++)
  {
    if(level.frames[i].name == frameName)
      theFrame = level.frames[i];
    else
      theFrame = GetFrame(frameName, level.frames[i]);
    
    if(theFrame != null)
      break;
  }
  
  return theFrame
}

function ExpireCookie(sName)
{
  var cookie = new Cookie(sName, "", 1, "/");
  cookie.Load();
  cookie.Remove();
}