
//a loose url regular expression
reUrlLoose = /^http:\/\/\S+$/;

reCurrency = /^\$?[\d,]+$/;
reNumber = /^[\d,]+$/;
reDecimal = /^[\d,.]+$/;
reEmail = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;




function isValidCurrency(fe)
{
    if ( (fe.value != "") && (! fe.value.match(reCurrency) ) )
        return false;
    else return true;
}

function isValidNumber(fe)
{
    if ( (fe.value != "") && (! fe.value.match(reNumber) ) )
        return false;
    else return true;
}

function isValidDecimal(fe)
{
    if ( (fe.value != "") && (! fe.value.match(reDecimal) ) )
        return false;
    else return true;
}




//----------------------------------------------------------------
// HIDING FUNCTIONS
//----------------------------------------------------------------
function domCollapse(which)
{
  if (document.getElementById && document.createTextNode)
  {
	if (which=="dispall")
    {
      domCollapseAll(1);
    }
    else if (which=="hideall")
    {
      domCollapseAll(0);
    }
	else
    {
	  m = document.getElementById("menu");
	  trig = m.getElementsByTagName("div").item(which).style.display;

      if (trig=="block")
      {
        trig="none";
        switchImage(which, 'Hidden');
      }
      else if (trig=="" || trig=="none")
	  {
		trig="block";
		switchImage(which, 'Visible');
	  }

	  m.getElementsByTagName("div").item(which).style.display=trig;
	}
  }
}


function domCollapseFor(which, forId)
{
  if (document.getElementById && document.createTextNode)
  {
	if (which=="dispall")
    {
      domCollapseAllFor(1, forId);
    }
    else if (which=="hideall")
    {
      domCollapseAllFor(0, forId);
    }
	else
    {
	  m = document.getElementById(forId);
	  trig = m.getElementsByTagName("div").item(which).style.display;

      if (trig=="block")
      {
        trig="none";
        switchImage(which, 'Hidden');
      }
      else if (trig=="" || trig=="none")
	  {
		trig="block";
		switchImage(which, 'Visible');
	  }

	  m.getElementsByTagName("div").item(which).style.display=trig;
	}
  }
}



function domCollapseAll(show)
{
  if (document.getElementById && document.createTextNode)
  {
	m=document.getElementById("menu");
	for (i=0;i<m.getElementsByTagName("div").length;i++)
    {
      if (show==1)
      {
		m.getElementsByTagName("div").item(i).style.display="block";
	  }
	  else
      {
	    m.getElementsByTagName("div").item(i).style.display="none";
	  }
    }
  }
}


function domCollapseAllFor(show, forId)
{
  if (document.getElementById && document.createTextNode)
  {
	m=document.getElementById(forId);
	for (i=0;i<m.getElementsByTagName("div").length;i++)
    {
      if (show==1)
      {
		m.getElementsByTagName("div").item(i).style.display="block";
	  }
	  else
      {
	    m.getElementsByTagName("div").item(i).style.display="none";
	  }
    }
  }
}


function domCollapseAllBut(show)
{
  if (document.getElementById && document.createTextNode)
  {
	m=document.getElementById("menu");
	for (i=0;i<m.getElementsByTagName("div").length;i++)
    {
      if (show==i) {}
	  else
      {
	    m.getElementsByTagName("div").item(i).style.display="none";
	  }
    }
  }
}


function domCollapseAllButFor(show, forId)
{
  if (document.getElementById && document.createTextNode)
  {
	m=document.getElementById(forId);
	for (i=0;i<m.getElementsByTagName("div").length;i++)
    {
      if (show==i) {}
	  else
      {
	    m.getElementsByTagName("div").item(i).style.display="none";
	  }
    }
  }
}


function domCollapseAllButArray(show)
{
  if (document.getElementById && document.createTextNode)
  {
	m=document.getElementById("menu");
	for (i=0;i<m.getElementsByTagName("div").length;i++)
    {
      hideThis = true;
      for (v=0;v<show.length;v++)
        if (show[v] == i)
        {
          hideThis = false;
          break;
        }

      if (hideThis)
	    m.getElementsByTagName("div").item(i).style.display="none";

    }
  }
}


function domShow(which)
{
  if (document.getElementById && document.createTextNode)
  {
	  m = document.getElementById("menu");
	  m.getElementsByTagName("div").item(which).style.display="block";
	}
}

function domShowFor(which, forId)
{
  if (document.getElementById && document.createTextNode)
  {
	  m = document.getElementById(forId);
	  m.getElementsByTagName("div").item(which).style.display="block";
	}
}

function domShowFor(which, forId)
{
  if (document.getElementById && document.createTextNode)
  {
	  m = document.getElementById(forId);
	  m.getElementsByTagName("div").item(which).style.display="block";
	}
}

function domShowForTag(which, forId, forTag)
{
  if (document.getElementById && document.createTextNode)
  {
	  m = document.getElementById(forId);
	  m.getElementsByTagName(forTag).item(which).style.display="block";
	}
}

function domHide(which)
{
  if (document.getElementById && document.createTextNode)
  {
	  m = document.getElementById("menu");
	  m.getElementsByTagName("div").item(which).style.display="none";
	}
}

function domHideFor(which, forId)
{
  if (document.getElementById && document.createTextNode)
  {
	  m = document.getElementById(forId);
	  m.getElementsByTagName("div").item(which).style.display="none";
	}
}

function domHideFor(which, forId)
{
  if (document.getElementById && document.createTextNode)
  {
	  m = document.getElementById(forId);
	  m.getElementsByTagName("div").item(which).style.display="none";
	}
}

function domHideForTag(which, forId, forTag)
{
  if (document.getElementById && document.createTextNode)
  {
	  m = document.getElementById(forId);
	  m.getElementsByTagName(forTag).item(which).style.display="none";
	}
}

// Adding backwards compatibility
if (document.getElementById && document.createTextNode)
{
	document.write('<style type="text/css">#menu div{display:none;}</style>')
}









function switchImage(i, whichWay)
{
    image = eval("document.Arrow" + i);


    if (image != null)
    {

      if (whichWay == 'Hidden')
      {
        image.src = '../images/arrow_closed.gif';
      }
      if (whichWay == 'Visible')
      {
        image.src = '../images/arrow_open.gif';
      }

    }

}


//----------------------------------------------------------------
// CENTERED POPUP FUNCTION
//----------------------------------------------------------------
function popupCenter(sUrl, intPopupHeight, intPopupWidth, yesnoDirectories, yesnoLocation, yesnoMenubar, yesnoResizable, yesnoScrollbars, yesnoStatus, yesnoToolbar)
{
	var sw= screen.availWidth/2;
	var sh = screen.availHeight/2;

			//these numbers are based on the size of the window opening.
			//wh is the height
			//ww is the width

	var wh = intPopupHeight/2;
	var ww = intPopupWidth/2;
	xpos=  sw - ww;
	ypos = sh - wh;

	params =          "height=" + intPopupHeight + ",";
	params = params + "width=" + intPopupWidth + ",";
	params = params + "top=" + ypos + ",";
	params = params + "left=" + xpos + ",";
	params = params + "screenX=" + xpos + ",";
	params = params + "screenY=" + ypos + ",";
	params = params + "directories=" + yesnoDirectories + ",";
	params = params + "location=" + yesnoLocation + ",";
	params = params + "menubar=" + yesnoMenubar + ",";
	params = params + "resizable=" + yesnoResizable + ",";
	params = params + "scrollbars=" + yesnoScrollbars + ",";
	params = params + "status=" + yesnoStatus + ",";
	params = params + "toolbar=" + yesnoToolbar;

	newwindow=window.open(sUrl, 'popup', params);
	newwindow.focus();

}


//----------------------------------------------------
//  VALIDATE EMAIL ADDRESSES
//----------------------------------------------------

function emailCheck (emailStr)
{

  /* The following variable tells the rest of the function whether or not
  to verify that the address ends in a two-letter country or well-known
  TLD.  1 means check it, 0 means don't. */

  var checkTLD=1;

  /* The following is the list of known TLDs that an e-mail address must end with. */

  var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;

  /* The following pattern is used to check if the entered e-mail address
  fits the user@domain format.  It also is used to separate the username
  from the domain. */

  var emailPat=/^(.+)@(.+)$/;

  /* The following string represents the pattern for matching all special
  characters.  We don't want to allow special characters in the address.
  These characters include ( ) < > @ , ; : \ " . [ ] */

  var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";

  /* The following string represents the range of characters allowed in a
  username or domainname.  It really states which chars aren't allowed.*/

  var validChars="\[^\\s" + specialChars + "\]";

  /* The following pattern applies if the "user" is a quoted string (in
  which case, there are no rules about which characters are allowed
  and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
  is a legal e-mail address. */

  var quotedUser="(\"[^\"]*\")";

  /* The following pattern applies for domains that are IP addresses,
  rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
  e-mail address. NOTE: The square brackets are required. */

  var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;

  /* The following string represents an atom (basically a series of non-special characters.) */

  var atom=validChars + '+';

  /* The following string represents one word in the typical username.
  For example, in john.doe@somewhere.com, john and doe are words.
  Basically, a word is either an atom or quoted string. */

  var word="(" + atom + "|" + quotedUser + ")";

  // The following pattern describes the structure of the user

  var userPat=new RegExp("^" + word + "(\\." + word + ")*$");

  /* The following pattern describes the structure of a normal symbolic
  domain, as opposed to ipDomainPat, shown above. */

  var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");

  /* Finally, let's start trying to figure out if the supplied address is valid. */

  /* Begin with the coarse pattern to simply break up user@domain into
  different pieces that are easy to analyze. */

  var matchArray=emailStr.match(emailPat);

  if (matchArray==null)
  {

    /* Too many/few @'s or something; basically, this address doesn't
    even fit the general mould of a valid e-mail address. */

    // alert("Email address seems incorrect (check @ and .'s)");
    return false;
  }

  var user=matchArray[1];
  var domain=matchArray[2];

  // Start by checking that only basic ASCII characters are in the strings (0-127).

  for (i=0; i<user.length; i++)
  {
    if (user.charCodeAt(i)>127)
    {
      // alert("Ths username contains invalid characters.");
      return false;
    }
  }

  for (i=0; i<domain.length; i++)
  {
    if (domain.charCodeAt(i)>127)
    {
      // alert("Ths domain name contains invalid characters.");
      return false;
    }
  }

  // See if "user" is valid

  if (user.match(userPat)==null)
  {

    // user is not valid

    // alert("The username doesn't seem to be valid.");
    return false;

  }

  /* if the e-mail address is at an IP address (as opposed to a symbolic
  host name) make sure the IP address is valid. */

  var IPArray=domain.match(ipDomainPat);

  if (IPArray!=null)
  {

    // this is an IP address

    for (var i=1;i<=4;i++)
    {
      if (IPArray[i]>255)
      {
        // alert("Destination IP address is invalid!");
        return false;
      }
    }

    return true;
  }

  // Domain is symbolic name.  Check if it's valid.

  var atomPat=new RegExp("^" + atom + "$");
  var domArr=domain.split(".");
  var len=domArr.length;

  for (i=0;i<len;i++)
  {
    if (domArr[i].search(atomPat)==-1)
    {
      // alert("The domain name does not seem to be valid.");
      return false;
    }
  }

  /* domain name seems valid, but now make sure that it ends in a
  known top-level domain (like com, edu, gov) or a two-letter word,
  representing country (uk, nl), and that there's a hostname preceding
  the domain or country. */

  if (checkTLD && domArr[domArr.length-1].length!=2 &&
      domArr[domArr.length-1].search(knownDomsPat)==-1)
  {
    // alert("The address must end in a well-known domain or two letter " + "country.");
    return false;
  }

  // Make sure there's a host name preceding the domain.

  if (len<2)
  {
    // alert("This address is missing a hostname!");
    return false;
  }


  // If we've gotten this far, everything's valid!

  return true;
}


//----------------------------------------------------
//  REPLACE ALL STRING UTIL FUNCTION
//----------------------------------------------------

function replaceAll(inputString, fromString, toString)
{
   // Goes through the inputString and replaces every occurrence of fromString with toString
   var temp = inputString;
   if (fromString == "") {
      return inputString;
   }
   if (toString.indexOf(fromString) == -1) { // If the string being replaced is not a part of the replacement string (normal situation)
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } else { // String being replaced is part of replacement string (like "+" being replaced with "++") - prevent an infinite loop
      var midStrings = new Array("~", "`", "_", "^", "#");
      var midStringLen = 1;
      var midString = "";
      // Find a string that doesn't exist in the inputString to be used
      // as an "inbetween" string
      while (midString == "") {
         for (var i=0; i < midStrings.length; i++) {
            var tempMidString = "";
            for (var j=0; j < midStringLen; j++) { tempMidString += midStrings[i]; }
            if (fromString.indexOf(tempMidString) == -1) {
               midString = tempMidString;
               i = midStrings.length + 1;
            }
         }
      } // Keep on going until we build an "inbetween" string that doesn't exist
      // Now go through and do two replaces - first, replace the "fromString" with the "inbetween" string
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + midString + toTheRight;
      }
      // Next, replace the "inbetween" string with the "toString"
      while (temp.indexOf(midString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(midString));
         var toTheRight = temp.substring(temp.indexOf(midString)+midString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } // Ends the check to see if the string being replaced is part of the replacement string or not

   return temp; // Send the updated string back to the user

} // Ends the "replaceSubstring" function
