// JavaScript Document

// Global Varaibles ---------------------------------------------

var cursorX = "";
var cursorY = "";
var IE = document.all ? true : false;
var currentPastor = "sherman"; // Default Pastor


	function getMouseX(evt)
	{
		if (!IE)
		{
			return evt.pageX
		}
		else
		{
		   return event.clientX
		}
		
	}
	
	function getMouseY(evt)
	{
		if (!IE)
		{
			return evt.pageY
		}
		else
		{
		   return event.clientY
		}
	}
	
/*  Get Mouse Position * *******************************************************
 *
 *	TAKES:		Event Object
 * 	RETURNS:	NOTHING
 *	NOTES:		IE by default does not calculate the page scrolling in the
 *				mouse postion, but the screen position.  Since FF and NS include
 *				page scroll in the mouse position, we're adding to IE as well.
 *
 *************************************************************************/
	
	function updateCursorPosition(e)
	{
		var posX = "";
		var posY = "";
		
		if (IE)
		{ 
			// grab the y pos.s if browser is IE
			posX = event.clientX - 2 + getPageXOffset();
			posY = event.clientY - 2 + getPageYOffset();
	  	}
		else
		{ 
			// grab the y pos.s if browser is NS
			posX = e.pageX;
			posY = e.pageY;
		}  
	  
	   // catch possible negative values in NS4
	   if ((posX < 0)||(posY < 0))
	   {
		  posX = 0;
		  posY = 0;
	   }  
	  
	 	cursorX = posX;
	 	cursorY = posY;
		
	}
	function getWindowWidth()
	{
		return document.all?document.body.clientWidth:window.innerWidth; 	
	}
	
	function getWindowHeight()
	{
		return document.all?document.body.clientHeight:window.innerHeight; 	
	}
	
	function containsMouse(element)
	{
		if (element.style.border != "")
		{
			var borderOffset	= 2;
		}
		else
		{
			var borderOffset	= 0;
		}
		
		var elementLeftX 	= getPosX(element);
		var elementRightX 	= getPosX(element) + element.offsetWidth;
		var elementTopY 	= getPosY(element);
		var elementBottomY 	= getPosY(element) + element.offsetHeight;
		
		//alert("mouse: " + cursorX + ", " + cursorY + "\nmenu: (" + elementLeftX + ", " + elementRightX + ") (" + elementTopY + ", " + elementBottomY + ")");
		
		if ((cursorX >= (elementLeftX + borderOffset)) & (cursorX <= (elementRightX - borderOffset)) & (cursorY >= (elementTopY + borderOffset)) & (cursorY <= (elementBottomY - borderOffset)))
		{
			return true;	
		}
		else
		{
			return false;	
		}
	}
	
	function checkIfMenuContainsMouse()
	{
		if (!( (containsMouse(headerMenu)) || (containsMouse(getElement("headerLinkContainer"))) ))
		{
			hideMenu();
		}
	}
	
	
	function displayPastor(pastor)
	{
		// Hide the current pastor profile
		hideElement(getElement(currentPastor));
		
		// Set the new pastor as the current pastor
		currentPastor = pastor;
		
		// Display the new pastor profile
		showElement(getElement(currentPastor));
	}
	
/* Is Array * *******************************************************
 *
 *	TAKES:		Array Object
 * 	RETURNS:	True if the array object is actually an array. False if the 
 *				array object is not an array.
 *	NOTES:		Usage: [Array Obj].isArray();
 *
 *************************************************************************/
 
 	Object.prototype.isArray = function()
	{
		return this.constructor == Array;
	}
	
/* Data Is Valid **********************************************************
*
*	TAKES:		Data, Data Type
*	RETURNS:	True if the data is valid, False if the data is not valid
*				based on the data type. Data types include:
*
*					1. String	- Ex: "abc123"
*					2. Integer	- Ex: 123
*					3. Float	- Ex: 25.99
*					4. Email	- Ex: test@email.com
*					5. Password - Ex: *******
*					6. Phone	- Ex: 123-444-5555
*
*	NOTES:		This function genreally doesn't enforce the maximum number
*				of characters for given data, this should be handled with 
*				character limits using HTML forms.
*
**************************************************************************/

	function dataIsValid(data, dataType)
	{
		var regExp;
		var errorMessage
		data = String(data);
		
		switch(dataType)
		{
			case "string":
				
				regExp = /^\w+$/;
				errorMessage = "Invalid characters were used. (Ex: <, >, &, %, \", ', #).";
				break;
				
			case "integer":
				
				regExp = /^\d+$/;
				errorMessage = "This field can only contain whole numbers (Ex: 1, 23, 675).";
				break;
				
			case "float":
			
				regExp = /^\d+.\d+$/;
				errorMessage = "This field can only contain decimal numbers (Ex: 1.0, 23.5, 67.5).";
				break;
				
			case "fraction":
			
				regExp = /^(|[0-9]+ )[0-9]+\/[0-9]+$/;
				errorMessage = "This field can only contain fractions (Ex: 12 3/4, 1/2).";
				break;
				
			case "email":
			
				regExp = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
				errorMessage = "This field can only contain valid email addresses (Ex: johndoe@emailexample.com).";
				break;
				
			case "password":
			
				regExp = /^.{6}/
				errorMessage = "This field must be at least six characters long and not contain any invalid characters. (Ex: <, >, &, %, \", ', #)";
				break;
			
			case "phone":
			
				regExp = /^((\(\d{3}\)( |))|(\d{3}( |-|.|)))\d{3}( |-|.|)\d{4}(,| |x|e)*/;
				errorMessage = "This field can only contain phone numbers (Ex: 111-555-4444).";
				break;
			
			case "zip":
			
				regExp = /^\d{5}([\-]\d{4})?$/;
				errorMessage = "This field can only contain zip codes (Ex: 12345-5555).";
				break;
			
			case "text":
			
				regExp = /^.*$/;
				errorMessage = "Invalid characters were used. (Ex: <, >, &, %, \", ', #).";
				break;
			
			case "date":
			
				regExp = /^([1-9]|1[012])\/([1-9]|[12][0-9]|3[01])\/\d{4}$/;
				errorMessage = "This field can only contain valid date values in the form: <b>MM/DD/YYYY</b>";
				break;
			
			case "time":
			
				regExp = /^(0[1-9]|1[012]):([012345][0-9]) (A|P)M$/;
				errorMessage = "This field can only contain valid time values in the form: <b>HH:MM AM</b>";
				break;
				
			default:
				
				return false;
		}
		
		if ( (data.search(regExp) != -1) & isValid(data) )
		{
			return true;	
		}
		else
		{
			return errorMessage;	
		}
	}
	
/* Is Valid **********************************************************
*
*	TAKES:		Data
*	RETURNS:	True if the data is valid, False if the data is not valid
*				based on the data type. Data types include:
*
**************************************************************************/

	function isValid(data)
	{
		var regExp = /(<|>|"|%|&|'|#)/;
		
		if (data.search(regExp) != -1)
		{
			return false;	
		}
		else
		{
			return true;	
		}
		
	}

/* Right **********************************************************
*
*	TAKES:		str - the string we are RIGHTing
*               n - the number of characters we want to return
*	RETURNS:	n characters from the right side of the string
*
**************************************************************************/

	function Right(str, n)
    {
		if (n <= 0)     // Invalid bound, return blank string
		{
			return "";
		}
		else if (n > String(str).length)   // Invalid bound, return
		{
			return str;                     // entire string
		}
		else
		{ 
		   // Valid bound, return appropriate substring
		   var iLen = String(str).length;
		   return String(str).substring(iLen, iLen - n);
		}
    }
	
/* Left **********************************************************
*
*	TAKES:		str - the string we are LEFTing
*               n - the number of characters we want to return
*	RETURNS:	n characters from the left side of the string
*
**************************************************************************/
		
	function Left(str, n)
	{
			if (n <= 0)     // Invalid bound, return blank string
			{
				return "";
			}
			else if (n > String(str).length)   // Invalid bound, return
			{
				return str;                // entire string
			}
			else // Valid bound, return appropriate substring
			{
				return String(str).substring(0,n);
			}
	}

	
/* Trim **********************************************************
*
*	TAKES:		String
*	RETURNS:	String with the whitespaces on the left and right sides trimmed.
*
**************************************************************************/

	function Trim(stringToTrim)
	{
		return stringToTrim.replace(/^\s+|\s+$/g,"");
	}

/*  Get Tag Group * *******************************************************
 *
 *	TAKES:		Element tag, regular expression that describes the id pattern
 *				of the  group of tag elements you're looking for.
 * 	RETURNS:	Element Array of matched element tag ids with the provided regular
 *				expression.
 *	NOTE:		Uses push to load the element array
 *
 *************************************************************************/
	
	function getTagGroup(elementTag, regExp)
	{
		var tagArray = document.getElementsByTagName(elementTag);
		var elementArray = new Array();
		
		for(ctr=0; ctr < tagArray.length; ctr++)
		{
			if ((tagArray[ctr].id).search(regExp) != -1)
			{
				if (elementArray.length > 0)
				{
					elementArray = elementArray.concat(new Array(tagArray[ctr]));
				}
				else
				{
					elementArray = new Array(tagArray[ctr]);
				}
			}
		}
		
		return elementArray;
	}
	
	
/*  Encode Query String * *******************************************************
 *
 *	TAKES:		Query String
 * 	RETURNS:	Encrypyed Query String
 *	NOTE:		Special characters that need to be encrypted:
 *
 *					- ;	%3B
 *					- ?	%3F
 *					- /	%2F
 *	 				- :	%3A
 *					- #	%23
 *					- &	%26
 *					- =	%3D
 * 					- +	%2B
 *					- $	%24
 *					- ,	%2C
 *					- <space>	%20
 *					- %	%25
 *					- <	%3C
 *					- >	%3E
 *					- ~	%7E
 *
 *************************************************************************/
 
	function encodeQueryString(queryString)
	{
		queryString = queryString.replace("%", "%25");
		queryString = queryString.replace(";", "%3B");
		queryString = queryString.replace("?", "%3F");
		queryString = queryString.replace("/", "%2F");
		queryString = queryString.replace(":", "%3A");
		queryString = queryString.replace("#", "%23");
		queryString = queryString.replace(" ", "%20");
		queryString = queryString.replace("&", "%26");
		queryString = queryString.replace("=", "%3D");
		queryString = queryString.replace("+", "%2B");
		queryString = queryString.replace("$", "%24");
		queryString = queryString.replace(",", "%2C");
		queryString = queryString.replace("<", "%3C");
		queryString = queryString.replace(">", "%3E");
		queryString = queryString.replace("~", "%7E");
		
		return queryString;

	}