/*
 * 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) 
{
  addEvent(window, 'load', func);
}

// OBS: duplicate from marvin.js
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; 
  } 
}


/*================================================================================================
  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 (screen.availWidth  &&  screen.availHeight)
  {
    screenW = screen.availWidth;
    screenH = screen.availHeight;
  }
  
  var leftPos = (screenW-width)/2;
  var topPos = (screenH-height)/2;
  
  return "top=" + topPos + "px,screenY=" + topPos + "px,left=" + leftPos + "px,screenX=" + leftPos
         + "px,height=" + height + "px,width=" + width + "px";
}


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 (screen.availWidth  &&  screen.availHeight)
  {
    screenW = screen.availWidth;
    screenH = screen.availHeight;
  }

  var width = 800, height = 600;
  
  if (window.innerWidth  &&  window.innerHeight)
  {
      width = window.innerWidth;
      height = window.innerHeight;
  }
  else if (document.body)
  {
      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;
  }
}

function enableFormSubmitDelayed(delay) {
  setTimeout("enableFormSubmit()", delay);
}

function enableFormSubmit()
{
  formSubmitted = 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]);
      selectIntoElement(inputElement, value[i]);

      if (typeof inputElement.oninput != "undefined" && inputElement.oninput != null)
        inputElement.oninput(inputElement);
      else if (typeof inputElement.onpropertychange != "undefined" && inputElement.onpropertychange != null)
        inputElement.onpropertychange(inputElement);
    }
  }
  else
  {
    var inputElement = window.opener.document.getElementById(destinationId);
    selectIntoElement(inputElement, value);
  
    if (typeof inputElement.oninput != "undefined" && inputElement.oninput != null)
      inputElement.oninput(inputElement);
    else if (typeof inputElement.onpropertychange != "undefined" && inputElement.onpropertychange != null)
      inputElement.onpropertychange(inputElement);
  }
    
  window.opener.focus();
  window.close();
}


function selectIntoElement(element, value)
{
  if (value.substring(0,3) == "#A#")
  {
    element.href = value.substring(3);
  }
  else if (typeof element.value != "undefined")
    element.value = value;
  else
    element.innerHTML = value;
}


// 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;
}
