var IFAWUtil = new Object();
/* General functions */
  IFAWUtil.xVerseNode = function(root, handler)
  /* handler is object w/ method handler.handleXVerse(node)
     - return false to teminate the traversal
     - true to continue */
  {
    var i, c, node;
  
    if (root && root.hasChildNodes())
    {
      for (i = 0, c = root.childNodes.length; i < c; i++)
      {
        node = root.childNodes[i];
        if (node)
        {
          if (handler.handleXVerse(node))
          {
            IFAWUtil.xVerseNode(node, handler);
          }
          else
          {
            return;
          }
        }
      }
    }
  }
  
  IFAWUtil.getXYOffset = function(obj)
  {
    var off;
  
    if (obj.offsetParent != null)
    {
      off = IFAWUtil.getXYOffset(obj.offsetParent);
    }
    else
    {
      off = new Array();
      off['x'] = 0;
      off['y'] = 0;
    }
    off['x'] += parseInt(obj.offsetLeft);
    off['y'] += parseInt(obj.offsetTop);
    return off;
  }

  IFAWUtil.classNameAdd = function(className, selector)
  {
    if (!className)
    {
      return selector;
    }
    else if (className.match(new RegExp("^"+selector+"$")))
    {
      return className;
    }
    else if (className.match(new RegExp("^"+selector+"\\s")))
    {
      return className;
    }
    else if (className.match(new RegExp("\\s"+selector+"\\s")))
    {
      return className;
    }
    else if (className.match(new RegExp("\\s"+selector+"$")))
    {
      return className;
    }
    else
    {
      return className+" "+selector;
    }
  }
  
  IFAWUtil.classNameDel = function(className, selector)
  {
    if (!className)
    {
      return "";
    }
    else if (className.match(new RegExp("^"+selector+"$")))
    {
      return "";
    }
    else if (className.match(new RegExp("^"+selector+"\\s")))
    {
      return className.replace(new RegExp("^"+selector+"\\s"), "");
    }
    else if (className.match(new RegExp("\\s"+selector+"\\s")))
    {
      return className.replace(new RegExp("\\s"+selector+"\\s"), " ");
    }
    else if (className.match(new RegExp("\\s"+selector+"$")))
    {
      return className.replace(new RegExp("\\s"+selector+"$"), "");
    }
    else
    {
      return className;
    }
  }
  
  
  IFAWUtil.classNameMatch = function(className, selector)
  {
    if (!className)
    {
      return false;
    }
    else if (className.match(new RegExp("^"+selector+"$")))
    {
      return true;
    }
    else if (className.match(new RegExp("^"+selector+"\\s")))
    {
      return true;
    }
    else if (className.match(new RegExp("\\s"+selector+"\\s")))
    {
      return true;
    }
    else if (className.match(new RegExp("\\s"+selector+"$")))
    {
      return true;
    }
    else
    {
      return false;
    }
  }
  
  
/* Flags */

  IFAWUtil.flagImgs = new Array();

  IFAWUtil.flagOnMouseOver = function()
  {
    this.src = this.src.replace(/\/flag([^\/]+)$/, "/o_flag$1");
  }

  IFAWUtil.flagOnMouseOut = function()
  {
    this.src = this.src.replace(/\/o_flag([^\/]+)$/, "/flag$1");
  }

  IFAWUtil.flagDivOnMouseOver = function()
  {
    this.className = IFAWUtil.classNameAdd(this.className, "over");
  }

  IFAWUtil.flagDivOnMouseOut = function()
  {
    this.className = IFAWUtil.classNameDel(this.className, "over");
  }

  IFAWUtil.initFlagsHXV = function(node)
  {
    var img, rgs;
    if (node.nodeType == "1")
    {
      if ((node.nodeName == "DIV") ||
          (node.nodeName == "UL") ||
          (node.nodeName == "LI") ||
          (node.nodeName == "A"))
          
      {
        node.onmouseover = IFAWUtil.flagDivOnMouseOver;
        node.onmouseout = IFAWUtil.flagDivOnMouseOut;
      }

      if ((node.nodeName == "DIV") && 
          IFAWUtil.classNameMatch(node.className, "country_flag"))
      {
        this.inFlagDIV = true;
      }
      else if ((node.nodeName == "IMG") && 
               this.inFlagDIV)
      {
        rgs = node.src.match(/^(.+)\/([^\/]+)$/);
        if (!rgs[2].match(/^o_flag/i) &&
            !rgs[2].match(/^s_flag/i))
        {
          img = new Image();
          img.src = rgs[1]+"/o_"+rgs[2];
          IFAWUtil.flagImgs.push(img);
          node.onmouseover = IFAWUtil.flagOnMouseOver;
          node.onmouseout = IFAWUtil.flagOnMouseOut;
          this.inFlagDIV = false;
        }
      }
    }
    return true;
  }

  IFAWUtil.initFlags = function(rootId)
  {
    var root, handler;
    if (document.getElementById)
    {
      root = document.getElementById(rootId);
      if (root)
      {
        handler = new Object();
        handler.handleXVerse = IFAWUtil.initFlagsHXV;
        handler.inFlagDIV = false;
        IFAWUtil.xVerseNode(root, handler);
      }
    }
  }




  IFAWUtil.globalNavImgOverPrefix = "";
  IFAWUtil.globalNavImgs = new Array();

  IFAWUtil.globalNavMouseOver = function()
  {
    IFAWUtil.globalNavImgs[this.className]["img"].src =
     IFAWUtil.globalNavImgs[this.className]["over"].src;
  }

  IFAWUtil.globalNavMouseOut = function()
  {
    IFAWUtil.globalNavImgs[this.className]["img"].src =
     IFAWUtil.globalNavImgs[this.className]["out"].src;
  }

  IFAWUtil.initGlobalNavHXV = function(node)
  {
    var a, r;
    if (node.nodeName == "IMG")
    {
      if ((this.navType == "home") &&
          this.inLowerRow)
      {
        // Skip
      }
      else
      {
        r = node.src.match(/^(.+)\/([^\/]+)\.([^\.]+)$/);
        a = new Array();
        a["out"] = new Image();
        a["out"].src = node.src;
        a["over"] = new Image();
        a["over"].src = IFAWUtil.globalNavImgOverPrefix+"/"+r[2]+"_o."+r[3];
        a["img"] = node;

        IFAWUtil.globalNavImgs[node.parentNode.className] = a;

        node.parentNode.onmouseover = IFAWUtil.globalNavMouseOver;
        node.parentNode.onmouseout = IFAWUtil.globalNavMouseOut;
      }
    }
    else if ((node.nodeName == "DIV") &&
             (IFAWUtil.classNameMatch(node.className, "lower_row")))
    {
      this.inLowerRow = true;
    }

    return true;
  }

  IFAWUtil.initGlobalNav = function(rootId, prefix, navType)
  {
    IFAWUtil.globalNavImgOverPrefix = prefix;    
    var root, handler;
    if (document.getElementById)
    {
      root = document.getElementById(rootId);
      if (root)
      {
        handler = new Object();
        handler.handleXVerse = IFAWUtil.initGlobalNavHXV;
        handler.navType = navType;
        handler.inLowerRow = false;
        IFAWUtil.xVerseNode(root, handler);
      }
    }
  }



/*
 Send this page
*/
  IFAWUtil.mailpage = function()
  {
    var widgets = 
     "toolbar=no," +
     "location=no," +
     "directories=no," +
     "status=no," +
     "menubar=no," +
     "scrollbars=yes," +
     "resizable=yes," +
     "width=500," +
     "height=500";
    var location = window.location.href.toLowerCase().replace(/^https?:\/\/[^\/]+/, "");
    var url = "/send_this_page/index.php?sendpage="+encodeURIComponent(location)+"&title="+encodeURIComponent(document.title);
    popupWin = window.open(url, "SendThisPage", widgets);
    popupWin.focus();
  }

/*
 Override window.print()

 window.print = function()
{
    var widgets = 
     "toolbar=no," +
     "location=yes," +
     "directories=no," +
     "status=no," +
     "menubar=yes," +
     "scrollbars=yes," +
     "resizable=yes," +
     "width=600," +
     "height=700";
    var url = "/print_this_page/index.php?printpage="+encodeURIComponent(window.location.pathname);
    popupWin = window.open(url, "PrintThisPage", widgets);
    popupWin.focus();
 }
*/

/*
 Cookie for int'l sites
*/

IFAWUtil.intlCookieSet = function(code)
{
var expry = new Date();
expry.setTime(expry.getTime() + (45 * 86400000));

document.cookie =
 "ifawsc="+encodeURIComponent(code)+
 "; expires="+(expry.toUTCString ? expry.toUTCString() : expry.toGMTString())+
 "; path=/"+
"; domain=.ifaw.org";

window.location = "/"+code+"/";
return false;
}

IFAWUtil.intlCookie = function()
{
var rgs;

if (window.location.pathname.match(/^\/splash/))
{
return;
}
else if ((window.location.pathname.match(/^\/$/)) ||
         (window.location.pathname.match(/^\/index\.(html|htm|php)/i)))
{
if (rgs = document.cookie.match(/(^|;\s*)ifawsc=([^;]+)(;|$)/))
{
this.intlCookieSet(rgs[2]);
}
else
{
window.location = "/splash.php";
}
}
}
IFAWUtil.intlCookie();


function openPopup(myurl, width, height)
{
    var newWindow;
    var props = 'scrollBars=yes,resizable=yes,toolbar=no,menubar=no,location=no,directories=no,width=' + width + ',height=' + height;
    newWindow = window.open(myurl, "View", props);
}

function checkForFormScroll()
{
    if (window.location.hash == "#scrollup")
    {
        window.scroll(0, 0);
        window.location.hash = "x";
    }

    setTimeout("checkForFormScroll();", 1000);
}
checkForFormScroll();