/*
 * This function is used to append onLoad events to a page
 * IE only!
 * 
 * It works like this:
 * addLoadEvent(func(){alert('hey it freakin works!')});
 */
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}


/*================================================================================================
  Focus
================================================================================================*/

function setFocus(id)
{
  document.getElementById(id).focus();
}


function handleKeyDown(btn, evt)
{
  if (document.all)
  {
    if (evt.keyCode == 13)
    {
      evt.returnValue=false;
      evt.cancel = true;
      btn.click();
    }
  }
  else if (document.getElementById)
  {
    if (evt.which == 13)
    {
      evt.returnValue=false;
      evt.cancel = true;
      btn.click();
    }
  }
  else if (document.layers)
  {
    if(evt.which == 13)
    {
      evt.returnValue=false;
      evt.cancel = true;
      btn.click();
    }
  }
}



/*================================================================================================
  Window opening
================================================================================================*/

function getWindowCoords(width, height)
{
  var screenW = 800, screenH = 600;

  if (document.all  ||  document.layers)
  {
    screenW = screen.availWidth;
    screenH = screen.availHeight;
  }
  
  var leftPos = (screenW-width)/2;
  var topPos = (screenH-height)/2;
  
  return "top=" + topPos + ",screenY=" + topPos + ",left=" + leftPos + ",screenX=" + leftPos
         + ",height=" + height + ",width=" + width;
}


function openWindowInCenter(url, windowName, width, height, otherInfo)
{
  return window.open(url, windowName, getWindowCoords(width,height)+otherInfo);
}


// Positions the current (already opened) window in the center.
function positionWindowInCenter()
{
  var screenW = 800, screenH = 600;

  if (document.all  ||  document.layers)
  {
    screenW = screen.availWidth;
    screenH = screen.availHeight;
  }

  var width = 800, height = 600;
  
  if (document.layers)
  {
      width = window.innerWidth;
      height = window.innerHeight;
  }
  else if (document.all)
  {
      width = document.body.clientWidth;
      height = document.body.clientHeight;
  }

  var leftPos = (screenW-width)/2;
  var topPos = (screenH-height)/2;
  
  window.moveTo(leftPos, topPos);
}


/*================================================================================================
  Secure single submission
================================================================================================*/
var formSubmitted = false;

function secureSingleSubmission() {
  if(!formSubmitted){
    formSubmitted = true;
    return true;
  }else{
    return false;
  }
}


/*================================================================================================
  Organizational selectors
  - This code is used for the first generation search windows. It is still in use, but might be
    outphased some day. Currently they are used in the CMS windows.
================================================================================================*/

// Paste selected value into destination input element in opener window
function select(value)
{
  // Note: depends on html generation to have "destinationId" defined
  if (isArray(value))
  {
    var destinationIds = destinationId.split(":");
    for (var i = 0; i < destinationIds.length; i++)
    {
      var inputElement = window.opener.document.getElementById(destinationIds[i]);
      inputElement.value = value[i];
    }
  }
  else{
    var inputElement = window.opener.document.getElementById(destinationId);
    inputElement.value = value;
  }
  window.opener.focus();
  window.close();
}


// Paste selected value into destination input element in opener window
// and then call the named function in that window also
function selectAndActivate(value, functionName)
{
  // Note: depends on html generation to have "destinationId" defined
  
  var inputElement = window.opener.document.getElementById(destinationId);
  inputElement.value = value;
  window.opener[functionName]();
  window.opener.focus();
  window.close();
}


/*==[ Trust code selection ]============================================================*/

function findTrustCode(destinationId, dummyParam)
{
  openWindowInCenter("memberselector.aspx?select=trust&destinationId="+destinationId, "", 
                     600, 600, "resizable=1,scrollbars=1");
}


/*==[ Organization code selection ]=====================================================*/

function findOrgCode(destinationId, param)
{
  var typeInput = document.getElementById(param);
  var orgType = (typeInput == null
                 ? param
                 : escape(document.getElementById(param).value));

  openWindowInCenter("memberselector.aspx?select=org&destinationId="+destinationId+"&orgtype="+orgType, "", 
                     600, 600, "resizable=1,scrollbars=1");
}


/*==[ Member number selection ]=========================================================*/

function findMember(destinationId, dummyParam)
{
  openWindowInCenter("memberselector.aspx?select=member&destinationId="+destinationId, "", 
                     400, 600, "resizable=1,scrollbars=1");
}


/*==[ Forum selection ]=================================================================*/

function findForum(destinationId, dummyParam)
{
  var paramTxt = "";
  if( dummyParam != null )
  {
    paramTxt = "&param="+document.getElementById(dummyParam).value;
  }  
  openWindowInCenter("memberselector.aspx?select=forum&destinationId="+destinationId+paramTxt, "", 
                     500, 600, "resizable=1,scrollbars=1");
}


/*================================================================================================
  Search handlers
  - These are the second generation search handlers. Currently these are used in the Member System
================================================================================================*/

var MarvinSearch = {};

MarvinSearch.membernumber = function(destinationId, param)
{
  marvin.openSearchWindow("member", destinationId, param);
}


MarvinSearch.organization = function(destinationId, param)
{
  // "Param" may be either an input reference or a constant value
  var typeInput = document.getElementById(param);
  var orgType = (typeInput == null
                 ? param
                 : escape(document.getElementById(param).value));

  marvin.openSearchWindow("organization", destinationId, (orgType == null ? "" : "orgtype="+orgType));
}


MarvinSearch.postalcode = function(destinationId, dummy)
{
  marvin.openSearchWindow("postalcode", destinationId, "");
}


MarvinSearch.countrycode = function(destinationId, dummy)
{
  marvin.openSearchWindow("countrycode", destinationId, "");
}

MarvinSearch.street = function(destinationId, dummy)
{
  marvin.openSearchWindow("street", destinationId, "");
}

MarvinSearch.trustcode = function(destinationId, dummy)
{
  marvin.openSearchWindow("trustcode", destinationId, "");
}

/*=====================================================================================================
  Type checking
======================================================================================================*/

//Since typeof always returns "object" when type checking an array, we have to check if the constructor 
//contains "array"
function isArray(obj) 
{
  if (typeof obj == 'object') 
  {  
    var criterion = obj.constructor.toString().match(/array/i); 
    return (criterion != null);  
  }
  return false;
}