/* ===========================================================================
 * Filename: javascript/functions.css
 * Author: Rebecca Skeers, rebecca@webmistress.com.au, www.webmistress.com.au except where indicated
 * Date: 4 August 2008
 * Description: Contains behaviour functions styles for the Salvation Army splash page.
 * =========================================================================== 
 */
 
attachEventListener(window, "load", initPage, false);

function initPage() 
{	
	mapInit();
	buttonInit();
	initLinks();
}

function checkDonateForm() 
{
	var theState = document.getElementById("state_selection");
	if(theState.selectedIndex==0)
	{
		alert ("Please select your location and then click the 'donate online' button.");
		return false;
	}
	return true;
}

function buttonInit()
{
	var theButton = document.getElementById("donationform_submit");
	
	attachEventListener(theButton, "mouseover", mouseoverButton, false);
	attachEventListener(theButton, "mouseout", mouseoutButton, false);
	attachEventListener(theButton, "focus", mouseoverButton, false);
	attachEventListener(theButton, "blur", mouseoutButton, false);

	return true;
}

function mouseoverButton()
{
	var theImage = document.getElementById("donationform_submit");
	theImage.src="./images/donate_button_hover.gif";
	return true;
}


function mouseoutButton(event)
{
	var theImage = document.getElementById("donationform_submit");
	theImage.src="./images/donate_button.gif";
	return true;
}


function mapInit()
{
	var theMap = document.getElementById("regionmap");
	var areas = theMap.getElementsByTagName("area");
	for (var j = 0; j < areas.length; j++)
	{
		attachEventListener(areas[j], "mouseover", mouseoverMap, false);
		attachEventListener(areas[j], "mouseout", mouseoutMap, false);
		attachEventListener(areas[j], "focus", mouseoverMap, false);
		attachEventListener(areas[j], "blur", mouseoutMap, false);
	}
	return true;
}

function mouseoverMap()
{
	var theImage = document.getElementById("regionimage");
	var nav1 = document.getElementById("region1");
	var nav2 = document.getElementById("region2");
	var caption1 = document.getElementById("caption1");
	var caption2 = document.getElementById("caption2");

	if(this==nav1)
	{
		theImage.src="./images/map_eastern.gif";
		caption1.className = "on";
		caption2.className = "off";
	}
	else if(this==nav2)
	{
		theImage.src="./images/map_southern.gif";
		caption1.className = "off";
		caption2.className = "on";
	}
	else
	{
		theImage.src="./images/map_australia.gif";
		caption1.className = "off";
		caption2.className = "off";
	}
	
	return true;
}

function mouseoutMap(event)
{
	var theImage = document.getElementById("regionimage");
	var caption1 = document.getElementById("caption1");
	var caption2 = document.getElementById("caption2");
	
	theImage.src="./images/map_australia.gif";
	caption1.className = "off";
	caption2.className = "off";
	
	return true;
}

function preloadImages()
{
	if (document.images)
	{
		images = new Array();
		images[0]="images/map_eastern.gif";
		images[1]="images/map_southern.gif";
		images[2]="images/donate_button_hover.gif";
		
		imageObj1 = new Image();
		imageObj1.src=images[0];
		
		imageObj2 = new Image();
		imageObj2.src=images[1];
		
		imageObj3 = new Image();
		imageObj3.src=images[2];
	}	
};


function initLinks()
{
	if (!document.getElementsByTagName) 
 		return;
 		
 	var anchors = document.getElementsByTagName("a");
 	for (var i=0; i<anchors.length; i++) 
 	{
   		var anchor = anchors[i];
   		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external")
   		{
     		anchor.target = "_blank";
 		}	
 	}
}

/* The JavaScript Anthology - James Edwards & Cameron Adams */
function attachEventListener(target, eventType, functionRef, capture)
{
	if (typeof target.addEventListener != "undefined")
	{ 
		target.addEventListener(eventType, functionRef, capture);
	}
	else if (typeof target.attachEvent != "undefined")
	{
		var functionString = eventType + functionRef;
		target["e" + functionString] = functionRef;
		target[functionString] = function(event)
		{
			if(typeof event == "undefined")
			{
				event = window.event
			};

			target["e" + functionString](event);
        };
		target.attachEvent("on" + eventType, target[functionString]);
	}
	else
	{
		eventType = "on" + eventType;

		if (typeof target[eventType] == "function")
		{
			var oldListener = target[eventType];
			target[eventType] = function()
			{
				oldListener();
				return functionRef();
			}
		}
		else
		{
			target[eventType] = functionRef;
		}
	}

	return true;
};


