//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// JAVASCRIPTS
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// GOOGLE MAPS - loadGoogleMap(< street address >, < name of place >)
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
/*
************************************************************************************************************
:: USAGE ::
************************************************************************************************************
Simple function for displaying a Google Map on a page.
The element that you want the map loaded into should have its ID set to "map".
Further instructions can be found at http://code.google.com/apis/maps/

1)  Include the following on pages using Google Maps functions:
<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=ABQIAAAAUWVWEX6JI_oAnRUQM4BNpBSl0uCdPM94pyHkRG8O_99Iqb0ioBS9pb6HQocP3FGBdS0cBJb4pdmtYA" type="text/javascript"></script>

2)  Be sure to obtain a fresh key here before deployment:
http://code.google.com/apis/maps/signup.html

3)  It must also be activated in the body tag:
<body onload="loadGoogleMap('Dallas, TX', 'Dallas');" onunload="GUnload()">
************************************************************************************************************
*/
function loadGoogleMap(address,location) {
	if (GBrowserIsCompatible()) {
		var map = new GMap2(document.getElementById("map"));
		var geocoder = new GClientGeocoder();
		
		geocoder.getLatLng(
			address,
			function(point) {
				if (!point) {
					alert(address + " not found");
				} else {
					map.setCenter(point, 13);
					var marker = new GMarker(point);
					map.addOverlay(marker);
					marker.openInfoWindowHtml('<h1>'+location+'</h1><p>'+address+'</p>',{maxWidth:150,maxHeight:130, autoScroll:true});
	
					GEvent.addListener(marker, "click", function() {
						marker.openInfoWindowHtml('<h1>'+location+'</h1><p>'+address+'</p>',{maxWidth:150,maxHeight:130, autoScroll:true});
					});
	
					map.addControl(new GSmallZoomControl());
					map.addControl(new GMapTypeControl());
					map.addControl(new GOverviewMapControl());
				}
			}
		);
	}
}

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// GOOGLE MAP DIRECTIONS - loadDirections(< start >, < destination >)
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
/*
************************************************************************************************************
:: USAGE ::
************************************************************************************************************
Simple function for displaying a Google Map on a page, with directions.
The element that you want the map loaded into should have its ID set to "map".
The element that you want the directions loaded into should have its ID set to "directions".
Further instructions can be found at http://code.google.com/apis/maps/

1)  Include the following on pages using Google Maps functions:
<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=ABQIAAAAUWVWEX6JI_oAnRUQM4BNpBSl0uCdPM94pyHkRG8O_99Iqb0ioBS9pb6HQocP3FGBdS0cBJb4pdmtYA" type="text/javascript"></script>

2)  Be sure to obtain a fresh key here before deployment:
http://code.google.com/apis/maps/signup.html

3)  It must also be activated in the body tag:
<body onload="loadDirections('arlington, tx', 'dallas, tx');" onunload="GUnload()">
************************************************************************************************************
*/
function loadDirections(startAddress,endAddress) {
	if (GBrowserIsCompatible()) {
		var map = new GMap2(document.getElementById("map"));
		var directions = new GDirections(map, document.getElementById("directionPane"));
		directions.load("from: "+startAddress+" to: "+endAddress);
	}
}


//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// JS URL VARS - getURLVar(varName)
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
/*
************************************************************************************************************
:: USAGE ::
************************************************************************************************************
Retrieves URL variables passed using GET

var newVariable = getURLVar('urlVarName');
************************************************************************************************************
*/
function getURLVar(urlVarName)
{
	var urlHalves = String(document.location).split('?');
	var urlVarValue = '';
	
	if(urlHalves[1])
	{
		var urlVars = urlHalves[1].split('&');
		
		for(i=0; i<=(urlVars.length); i++)
		{
			if(urlVars[i])
			{
				var urlVarPair = urlVars[i].split('=');
				if (urlVarPair[0] && urlVarPair[0] == urlVarName) 
				{
					urlVarValue = urlVarPair[1];
				}
			}
		}
	}
	return urlVarValue;   
}


//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// PAD - pad(number, desired length)
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
/*
************************************************************************************************************
:: USAGE ::
************************************************************************************************************
Adds 0's to the beginning of a number to ensure it's a certain length

var zeroZeroFive = pad(5, 3);
************************************************************************************************************
*/

function pad(number, desiredLength)
{
	var str = ''+number;
	
	while(str.length < desiredLength) {
		str = '0'+str;
	}
	return str;
}


//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// INCLUDE SCRIPT - includeScript(filename)
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
/*
************************************************************************************************************
:: USAGE ::
************************************************************************************************************
Includes a JavaScript file

includeScript('filename.js');
************************************************************************************************************
*/

function includeScript(file)
{
	document.write('<script type="text/javascript" src="'+ file + '"></script>'); 
}


//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// SWITCH BACKGROUND - switchBackground(element ID, image URL)
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
/*
************************************************************************************************************
:: USAGE ::
************************************************************************************************************
Replaces the background-image property on the
specified element with the specified URL


************************************************************************************************************
*/

function switchBackground(elementID,imageURL)
{
	document.getElementById(elementID).style.background = 'url('+imageURL+')';
}

function toggleVisibility(elementID,visibility)
{
	document.getElementById(elementID).style.visibility = visibility;
}

function widgetButtonClicked(buttonID)
{
	// reset visibility
	toggleVisibility('worshipTimes','hidden')
	toggleVisibility('thisWeek','hidden')
	toggleVisibility('directions','hidden')
	// reset button frames
	switchBackground('btn_worshipTimes', '/themes/default/img/pageStructure/homeWidget/btnInactive.gif');
	switchBackground('btn_thisWeek', '/themes/default/img/pageStructure/homeWidget/btnInactive.gif');
	switchBackground('btn_directions', '/themes/default/img/pageStructure/homeWidget/btnInactive.gif');
	// switch active button
	switchBackground(buttonID, '/themes/default/img/pageStructure/homeWidget/btnActive.gif');
	// toggle visibility on relevant content
	switch(buttonID) {
		case 'btn_worshipTimes':
			toggleVisibility('worshipTimes','visible')
			break;
		case 'btn_thisWeek':
			toggleVisibility('thisWeek','visible')
			break;
		case 'btn_directions':
			toggleVisibility('directions','visible')
			break;
	}
}

function writeCurrentYear()
{
	todaysDate = new Date();
	document.write(todaysDate.getFullYear());
}



function staffToggle(id,total)
{
	visibilityToggle = '';
	for(i = 0; i < total; i++)
	{
		if(id == i)
		{
			visibilityToggle = 'visible';
		}
		else
		{
			visibilityToggle = 'hidden';
		}
		toggleVisibility('staffPhoto'+i,visibilityToggle);
		toggleVisibility('staffInfo'+i,visibilityToggle);
	}
}

function validate_required(field,alerttxt)
{
	with (field)
	{
		if (value == null || value == "")
		{
			alert(alerttxt);
			return false;
		}
		else
		{
			return true;
		}
	}
}

function validate_donationForm(targetForm)
{
	if(
		validate_required(document.getElementById('name'), 'Please enter your name') == false
		||
		validate_required(document.getElementById('phone'), 'Please enter your phone number') == false
		||
		validate_required(document.getElementById('email'), 'Please enter your eMail address') == false
		||
		validate_required(document.getElementById('address1'), 'Please enter your address') == false
		||
		validate_required(document.getElementById('state'), 'Please select your state') == false
		||
		validate_required(document.getElementById('zip'), 'Please enter your zip code') == false
	)
	{
		return false;
	}
	else
	{
		return true;
	}
}
function sfHover() {
	var sfEls = document.getElementById("nav_main").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"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);
