/*
    Begin Drop Down Menu javascript
*/

sfHover = function()
{
    if (document.getElementById("vertical"))
    {
      	var sfEls = document.getElementById("vertical").getElementsByTagName("LI");
      	
        for (var i = 0; i < sfEls.length; i++)
        {
        		sfEls[i].onmouseover = function()
            {
        			 this.className += " sfhover";
        		}
        		
        		sfEls[i].onmouseout = function()
            {
        			 this.className = this.className.replace(new RegExp(" sfhover\\b"), "");
        		}
      	}
  	}
    else
    {
        //alert('Can not find V menu');
    }

    if (document.getElementById("horizontal"))
    {
      	var sfElsH = document.getElementById("horizontal").getElementsByTagName("LI");
      	
        for (var i = 0; i < sfElsH.length; i++)
        {
        		sfElsH[i].onmouseover = function()
            {
        			 this.className += " sfhover";
        		}
        		
        		sfElsH[i].onmouseout = function()
            {
        			 this.className = this.className.replace(new RegExp(" sfhover\\b"), "");
        		}
      	}
  	}
  	else
    {
        //alert('Can not find H menu');
    }
}

if (window.attachEvent) window.attachEvent("onload", sfHover);

/*
    End Drop Down Menu javascript
*/



/*
    Begin: Freecap
*/

function new_freecap()
{
    // loads new freeCap image
    if(document.getElementById)
    {
        // extract image name from image source (i.e. cut off ?randomness)
        thesrc = document.getElementById("freecap").src;
        thesrc = thesrc.substring(0,thesrc.lastIndexOf(".")+4);
        // add ?(random) to prevent browser/isp caching
        document.getElementById("freecap").src = thesrc+"?"+Math.round(Math.random()*100000);
    } 
    else 
    {
        alert("Sorry, cannot autoreload freeCap image\nSubmit the form and a new freeCap will be loaded");
    }
}

/*
    End: Freecap
*/

var thePage, req;
function expandCounty(theElement, theElements, theRedirect)
{
    var elementY;
    
    if (theRedirect)
    	thePage = theRedirect;
    
    if (document.getElementById(theElement))
      elementY = document.getElementById(theElement);
    else
      elementY = false;
    
    if (elementY)
    {
			if (elementY.style.display == '')
				elementY.style.display = 'none';
      else
      {
        closeAll(theElements);
        elementY.style.display = '';
  		}
  		
      if (window.XMLHttpRequest) // Mozilla/Safari
  		{
  			req = new XMLHttpRequest();
  		}
  		else if (window.ActiveXObject) // IE
  		{
  			req = new ActiveXObject("Microsoft.XMLHTTP");
  		}
  		
  		req.open('POST', "set-last-used.php", true);
  		req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  		req.send("lastused=" + theElement);
  	}
}


/*
    Begin: 
    close all the left side navigation boxes before it expands the first elements
*/

function closeAll(theElements)
{ 
    if (theElements)
    {
        // Close the all first
        var breakElements = theElements.split(',');
        
        // If we have an array ;-)
        if (breakElements.length > 0)
        {
            // Loop thruogh all the items of the array 
            for (var i = 0; i < breakElements.length; i++)
            {
                // close the box
                if (document.getElementById(breakElements[i]))
                {
                    document.getElementById(breakElements[i]).style.display = 'none';
                }
            } 
        }
    } 
} 

/*
    End: 
*/

var imageManager =
{
    changeImage: function(whatFile)
    {
        this.smallImage = whatFile.src;
        this.mediumImage = this.smallImage.replace('-small', '-medium');
        this.largeImage = this.smallImage.replace('-small', '-large');
        document.getElementById('largeImage').innerHTML = '<img onClick="imageManager.getLargeImage(\'' + this.largeImage + '\');" src="' + this.mediumImage + '"> <div align="center"><a href="javascript:imageManager.getLargeImage(\'' + this.largeImage + '\');" src="' + this.mediumImage + '">Enlarge Image</a></div>';
    },
    
    getLargeImage: function(imageSource)
    {
        try
        {
            popupWindow = window.open('gallery_popup.php?src=' + imageSource, 'popup', 'height=300,width=300');
        }
        catch(e)
        {
            alert("Error loading popup window: " + e);
        }
    }
}

function checkForm(theForm, theElements, theFieldnames)
{
	var errorDisplay = "";
	if (theForm)
	{	
		if (theElements.length > 0)
		{
			for (var i in theElements)
			{
				if (theElements[i].value == '')
				{
					errorDisplay += '"' + theFieldnames[i] + '" is a mandatory field' + "\n";
				}
			}
			if (errorDisplay.length == 0)
					return true;
			else
			{
				alert("Please correct the errors below to continue\n" + errorDisplay);
				return false;
			}
		}
	}
}
