
/*
 *
 * File:      $RCSfile: formctrl.js,v $
 * Version:   $Revision: 1.2 $
 * Revised:   $Date: 2005/11/18 14:20:18 $
 *            $Author: ma-ran $
 *
 * Created:   2004-06-01
 * Author:    Martin Bostrom
 * Project:   E-mail DICOM viewer
 *
 *
 * Description:
 *
 */

// Global variables:
GLOBAL = {
  hasDownloadedApp: false // Flag set on begin of download
}


/*****************************************************************************
 * 
 * Support functions
 * 
 *
 * getCurStyle()
 *   Returns the current style of an element. 
 *
 * In:
 *   elem: element to get the style for
 *
 */


function getCurStyle(elem)
{
  var curStyle;
  if(elem.currentStyle!=null)
    return(elem.currentStyle);
  else
    return(document.defaultView.getComputedStyle(elem, ''));
}


/* 
 * swapColors()
 *   Swaps the requested color styles of the given element.
 *
 * In:
 *   elem:     element to affect
 *   colorSet: names of the color properties to swap
 */

function swapColors(elem, colorSet)
{
  for(var prop in colorSet)
  {
    if(getCurStyle(elem)[prop].indexOf('#')!=-1)
      elem.style[prop]=getCurStyle(elem)[prop]
        .replace(/([0-9A-F]{2})([0-9A-F]{2})([0-9A-F]{2})/i, '$3$2$1');
    else
      elem.style[prop]=getCurStyle(elem)[prop]
        .replace(/rgb\(([0-9]*), ([0-9]*), ([0-9]*)\)/i, 'rgb($3, $2, $1)');
  }
}


/* 
 * swapBorderColors()
 *   Swaps the border colors of the given element
 *
 * In:
 *   elem: element to affect
 */

function swapBorderColors(elem)
{
  swapColors(elem, 
             {"borderTopColor": true, 
              "borderRightColor": true, 
              "borderBottomColor": true, 
              "borderLeftColor": true});
}


/*****************************************************************************
 * 
 * Initialization functions
 * 
 * 
 * initFields()
 *   Initializes input fields:
 *   - Sets focus on the first available field
 *   - Adjusts positioning for IE 5
 */

function initFields()
{
  var inputs=document.getElementsByTagName("INPUT");
  var fwdButt;
  var backButt;
  var firstField;

  for(var i=0; i<inputs.length; i++)
  {
    if(inputs[i].type!="hidden")
    {
      if(inputs[i].value.indexOf("<")!=-1)
        backButt=inputs[i];
      else 
      {
        if(firstField==null)
          firstField=inputs[i];
        if(inputs[i].value.indexOf(">")!=-1)
          fwdButt=inputs[i];
      }
    }
  }

  if(navigator.userAgent.indexOf('MSIE 5.0')!=-1)
  {
    if(backButt)
      backButt.style.styleFloat="none";
    if(fwdButt)
      fwdButt.style.styleFloat="right";
  }

  if(firstField)
    firstField.focus();
}



/*
 * initHoverImages()
 *   Initializes hover effects on link-images.
 */

function initHoverImages()
{
  var links=document.getElementsByTagName("A");
  for(var li=0; li<links.length; li++)
  {
    links[li].onmouseover=function()
    {
      var imgs=this.getElementsByTagName("IMG");
      for(var ii=0; ii<imgs.length; ii++)
      {
        imgs[ii].src=imgs[ii].src
          .replace(/(.*)\/([^\/]*)\.([^\/\.]*)/i, '$1/high/$2.$3');
      }
    }
    links[li].onmouseout=function()
    {
      var imgs=this.getElementsByTagName("IMG");
      for(var ii=0; ii<imgs.length; ii++)
      {
        imgs[ii].src=imgs[ii].src.replace(/\/high\//, '/');
      }
    }
  }
}


/*
 * initHoverInputs()
 *   Initializes hover effects on input fields.
 */

function initHoverInputs()
{
  var inputs=document.getElementsByTagName("INPUT");
  for(var fi=0; fi<inputs.length; fi++)
  {
    if(inputs[fi].type!="hidden")
    {
      inputs[fi].onmouseover=function()
      {
        this.style.backgroundImage=getCurStyle(this).backgroundImage
          .replace(/(.*)\/([^\/]*)\.([^\/\.]*)/i, '$1/high/$2.$3');
        swapBorderColors(this);
      }
      inputs[fi].onmouseout=function()
      {
        this.style.backgroundImage=this.style.backgroundImage
          .replace(/\/high\//, '/');
        swapBorderColors(this);
      }
    }
  }
}


/*****************************************************************************
 * 
 * Event functions
 * 
 * 
 * whenDownloadApp()
 *   Called on download begin; flags that a download is started (or, at least,
 *   attempted).
 */

function whenDownloadApp()
{
  GLOBAL.hasDownloadedApp=true;
  return(true);
}


/*
 * whenNextAfterDownload()
 *   Called on click at the [Next]-button of the download page. Tests if a 
 *   download was ever attempted, and if not, asks the user to start a 
 *   download.
 * 
 * In:
 *   warningMsg: prompt message to the user.
 */

function whenNextAfterDownload(warningMsg)
{
  var links=document.getElementsByTagName("A");

  if(!GLOBAL.hasDownloadedApp 
     && 
     links.length>=1 
     && 
     links[0].href.indexOf('.exe')!=-1)
  {
    if(confirm(warningMsg))
    {
      GLOBAL.hasDownloadedApp=true;
      window.location=links[0].href;
      return(false);
    }
    else
      return(true);
  }
  else
    return(true);
}


/*
 * whenInitForm()
 *   Called on form (page) init.
 */

function whenInitForm()
{
  initFields();
  initHoverImages();
  initHoverInputs();
}

// EoF
