﻿
/* ENTER = TAB from http://blogs.meetandplay.com/WPierce/archive/2006/10/09/Tab_with_Enter_Key.aspx */

  // xBroswer event subscription
  function addListener(a,b,c,d){if(a.addEventListener){a.addEventListener(b,c,d);return true;}else if(a.attachEvent){var e=a.attachEvent("on"+b,c);return e;}else{alert("Handler could not be attached");}}
  function bind(a,b,c,d){return window.addListener(a,b,function(){d.apply(c,arguments)});}
  //

  // Generic dispatcher for keystrokes
  function handleKeystroke(evt) 
  {
    evt = getEvent(evt);
    var asc = getKeyCode(evt);
    var chr = getCharacter(asc);

    for (var i in this)
    {
      if (asc == i)
      {
        this[i](evt);
        break;
      }
    }
  }
  //

  // xBrowser event utilities
  function cancelEvent(evt)
  {
    evt.cancelBubble = true;
    evt.returnValue = false;
    if (evt.preventDefault) evt.preventDefault();
    if (evt.stopPropagation) evt.stopPropagation();
    return false;
  }
  function getEvent(evt)
  {
    if( !evt ) evt = window.event;
    return evt;
  }
  function getTarget(evt)
  {
    var target = evt.srcElement ? evt.srcElement : evt.target;
    return target;
  }
  function getKeyCode(evt)
  {
    var asc = !evt.keyCode ? (!evt.which ? evt.charCode : evt.which) : evt.keyCode;
    return asc;
  }
  function getCharacter(asc)
  {
    var chr = String.fromCharCode(asc).toLowerCase();
    return chr;
  }
  //

  function handleEnterKey(evt)
  {       
    var target = getTarget(evt);
    var targetTabIndex = target.tabIndex;
    var nextTabIndex = targetTabIndex+1;

    var nextElement = getElementByTabIndex(nextTabIndex);
    if( nextElement )
    {
      nextElement.focus();
      return cancelEvent(evt);
    }

    return true;
  }

  function getElementByTabIndex(tabIndex)
  {
    var form = document.forms[0];
    for( var i=0; i<form.elements.length; i++ )
    {
      var el = form.elements[i];
      if( el.tabIndex && el.tabIndex == tabIndex )
      {
        return el;
      }
    }

    return null;
  }
  
// Setup our Key Commands

/*
var keyMap    = new Array();
var ENTER     = 13;
keyMap[ENTER] = handleEnterKey;
*/
 
// Add the keypress listner to the document object for global capture
//bind(document, 'keypress', keyMap, handleKeystroke); 


/* Mediachase Helper Routines */

function ShowProgress()
{
	var w = 300;
	var h = 160;
	var l = (screen.width - w) / 2;
	var t = (screen.height - h) / 2;
	winprops = 'resizable=0, height='+h+',width='+w+',top='+t+',left='+l+'w';
	var f = window.open('Progress.aspx?ID='+document.forms[0].__MEDIACHASE_FORM_UNIQUEID.value, "_blank", winprops);
}

function calendarPicker(strField,idQuestion)
{
    window.open('DatePicker.aspx?field=' + strField + '&questionid=' + idQuestion, 'calendarPopup', 'left=400,top=400,width=180,height=150,location=no,resizable=no,scrollbars=no,status=no,toolbar=no');
}

function openHelp(topic)
{
    var strOptions = 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,width=440, height=600';
    var url = 'Help/' + topic + '.aspx';
    winOnLineHelp = window.open(url, 'SAPOnlineHelp', strOptions);
}

function openDocumentViewer(url)
{
    var strOptions = 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,width=800, height=700';
    var winDocumentViewer;
    winDocumentViewer = window.open(url, '', strOptions);
    return false;
}

/**
 * X-browser event handler attachment and detachment
 * @argument obj - the object to attach event to
 * @argument evType - name of the event - DONT ADD "on", pass only "mouseover", etc
 * @argument fn - function to call
 */
function addEvent(obj, evType, fn){
 if (obj.addEventListener){
    obj.addEventListener(evType, fn, false);
    return true;
 } else if (obj.attachEvent){
    var r = obj.attachEvent("on"+evType, fn);
    return r;
 } else {
    return false;
 }
}

function returnRefresh(returnVal)
{

    var url = unescape(window.location.pathname);   
    /*window.location.href = url;*/
  
    window.location.href = url + '?action=moveto&page=' + returnVal.toString();  
}
