var images = new Array(); //the images collection
var navs = new Array(); //the navigation URL collection
var currNav = "nav_home"; //The currently selected navigation item

//only runs if the current browser supports dynamic imaging (NE 3.0 or IE 4.0 and up)
if (document.images)
{
    loadRolloverImages(images, "nav_home", "gif");
	loadRolloverImages(images, "nav_news", "gif");
	loadRolloverImages(images, "nav_missionStatement", "gif");
	loadRolloverImages(images, "nav_makeADonation", "gif");
	loadRolloverImages(images, "nav_becomeAMember", "gif");
	loadRolloverImages(images, "nav_projects", "gif");
    loadRolloverImages(images, "nav_contactUs", "gif");
	loadRolloverImages(images, "nav_links", "gif");
}

function loadRolloverImages(images, id, ext)
{
	var img = new Image();
	
	//blur image
	img.src="images/" + id + "_blur." + ext;
	images[id + "_blur"] = img;
	
	//hover image
	img = new Image();
	img.src="images/" + id + "_hover." + ext;
	images[id + "_hover"] = img;
	
	//focus image
	img = new Image();
	img.src="images/" + id + "_focus." + ext;
	images[id + "_focus"] = img;
}

function mouseOver(id)
{
	//only do rollovers for nav items other than the current one
	//if (currNav != id)
		document.images[id].src = images[id + "_hover"].src;
}

function mouseOut(id)
{
	//only do rollovers for nav items other than the current one
	//if (currNav != id) 
		document.images[id].src = images[id + "_blur"].src;
}

function doNav(id)
{
	//cannot reselect the currently selected nav item
    if (currNav != id) 
	{
		//blur the previously selected navigation item
		document.images[currNav].src = images[currNav + "_blur"].src;
		//set the new navigation item as current
		currNav = id;
		//focus the new navigation item
		document.images[currNav].src = images[currNav + "_focus"].src;
		//navigate to the selected item
		window.location = navs[currNav];
	}
}

function constrainImage(src, x, y, attribs)
{
    var img = new Image();
    img.src = src;
    var height = img.height;
    var width = img.width;
    var xr = width / x;
    var yr = height / y;
    if (xr >= yr)
    {
        width /= xr;
        height /= xr;
    }
    else
    {
        width /= yr;
        height /= yr;
    }
    
    document.write("<img src='" & src & "' height='" + height + "' width='" + 
                   width + "' " + attribs + ">");    
}

//***************************
//***** form validation *****
//***************************

function ValidTextarea(field)
{
     var valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!:;?@#%&*_+=.,\"'-/()$\n\r[] "
     var ok = "yes";
     var temp;
     for (var i=0; i<field.value.length; i++)
     {
          temp = "" + field.value.substring(i, i+1);
          if (valid.indexOf(temp) == "-1")
              ok = "no";
     }
          if (ok == "no")
          {
               alert("An invalid character has been detected.");
               field.focus();
               field.select();
               return false
          }
    return true
}

function ValidNumChar(field)
{
     var valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!.'&#,-/[] "
     var ok = "yes";
     var temp;
     for (var i=0; i<field.value.length; i++)
     {
          temp = "" + field.value.substring(i, i+1);
          if (valid.indexOf(temp) == "-1")
              ok = "no";
     }
          if (ok == "no")
          {
               alert("An invalid character has been detected.");
               field.focus();
               field.select();
               return false
          }
    return true
}

function ValidChar(field)
{
     var valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.,'&#-/[] "
     var ok = "yes";
     var temp;
     for (var i=0; i<field.value.length; i++)
     {
          temp = "" + field.value.substring(i, i+1);
          if (valid.indexOf(temp) == "-1")
              ok = "no";
     }
          if (ok == "no")
          {
               alert("An invalid character has been detected.");
               field.focus();
               field.select();
               return false
          }
    return true
}

function ValidNum(field)
{
     var valid = "0123456789."
     var ok = "yes";
     var temp;
     for (var i=0; i<field.value.length; i++)
     {
        temp = "" + field.value.substring(i, i+1);
        if (valid.indexOf(temp) == "-1")
            ok = "no";
     }
     if (ok == "no")
     {
          alert("Invalid entry! Only numbers are allowed.");
          field.focus();
          field.select();
          return false
     }
     return true
}

function ValidURL(field)
{
     var valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_.@-/?+=,:\\"
     var ok = "yes";
     var temp;
     for (var i=0; i<field.value.length; i++)
     {
          temp = "" + field.value.substring(i, i+1);
          if (valid.indexOf(temp) == "-1")
            {
                alert("An invalid character has been detected.");
                field.focus();
                field.select();
                return false
            }
     }
     return true
}

function ValidEmail (field)
{
     var emailStr= field.value
     
     if (emailStr == "")
     {
         return true;
     }
     
     var emailPat=/^(.+)@(.+)$/
     var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
     var validChars="\[^\\s" + specialChars + "\]"
     var quotedUser="(\"[^\"]*\")"
     var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
     var atom=validChars + '+'
     var word="(" + atom + "|" + quotedUser + ")"
     var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
     var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
     var matchArray=emailStr.match(emailPat)
     if (matchArray==null) {
     	alert("Email address seems incorrect (check @ and .'s)")
     	field.focus()
     	field.select()
     	return false
     }
     var user=matchArray[1]
     var domain=matchArray[2]
     
     // See if "user" is valid 
     if (user.match(userPat)==null) {
         // user is not valid
         alert("The username doesn't seem to be valid.")
     	field.focus()
     	field.select()
         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!")
     		field.focus()
     		field.select()
     		return false
     	    }
         }
         return true
     }
     
     // Domain is symbolic name
     var domainArray=domain.match(domainPat)
     if (domainArray==null) {
     	alert("The domain name doesn't seem to be valid.")
     	field.focus()
     	field.select()
         return false
     }
     
     /* domain name seems valid, but now make sure that it ends in a
        three-letter word (like com, edu, gov) or a two-letter word,
        representing country (uk, nl), and that there's a hostname preceding 
        the domain or country. */
     
     /* Now we need to break up the domain to get a count of how many atoms
        it consists of. */
     var atomPat=new RegExp(atom,"g")
     var domArr=domain.match(atomPat)
     var len=domArr.length
     if (domArr[domArr.length-1].length<2 || 
         domArr[domArr.length-1].length>3) {
        // the address must end in a two letter or three letter word.
        alert("The address must end in a three-letter domain, or two letter country.")
     	field.focus()
     	field.select()
        return false
     }
     
     // Make sure there's a host name preceding the domain.
     if (len<2) {
        var errStr="This address is missing a hostname!"
        alert(errStr)
     	field.focus()
     	field.select()
        return false
     }
     
     // If we've gotten this far, everything's valid!
     return true;
}