// JavaScript Document

// Global Varaibles ---------------------------------------------

var cursorX = "";
var cursorY = "";
var IE = document.all ? true : false;
var currentPastor = "sherman"; // Default Pastor

// Elements -----------------------------------------------------

function getElement(elementId)
{
	return document.getElementById(elementId);	
}

	
/*  **********************************************************
*
*	TAKES:		NOTHING
*	RETURNS:	NOTHING
*
**************************************************************************/

function showElement(element)
{
	var thisElement = element;
	
	if (typeof thisElement == "string")
	{
		thisElement = getElement(element);	
	}
	
	thisElement.style.display = "block";
}
	
/*  **********************************************************
*
*	TAKES:		NOTHING
*	RETURNS:	NOTHING
*
**************************************************************************/

function hideElement(element)
{
	element.style.display = "none";
}
	
/*  **********************************************************
*
*	TAKES:		NOTHING
*	RETURNS:	NOTHING
*
**************************************************************************/

function getPosX(obj)
{
    var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
  }
	
/*  **********************************************************
*
*	TAKES:		NOTHING
*	RETURNS:	NOTHING
*
**************************************************************************/

  function getPosY(obj)
  {
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
  }
	
/*  **********************************************************
*
*	TAKES:		NOTHING
*	RETURNS:	NOTHING
*
**************************************************************************/


	function setUrl(elementName, ajaxUrl)
	{
		if (ajaxUrl.match("\\?"))
		{
			ajaxUrl = ajaxUrl + "&opid=" + new Date().getTime();	
		}
		else
		{
			ajaxUrl = ajaxUrl + "?opid=" + new Date().getTime();	
		}
		
		showElement(elementName);
		dojo.widget.byId(elementName).setUrl(ajaxUrl);
				
	}

	
/*  **********************************************************
*
*	TAKES:		NOTHING
*	RETURNS:	NOTHING
*
**************************************************************************/

	function getMouseX(evt)
	{
		if (!IE)
		{
			return evt.pageX
		}
		else
		{
		   return event.clientX
		}
		
	}

	
/*  **********************************************************
*
*	TAKES:		NOTHING
*	RETURNS:	NOTHING
*
**************************************************************************/

	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;
		
	}
	
/*  **********************************************************
*
*	TAKES:		NOTHING
*	RETURNS:	NOTHING
*
**************************************************************************/

	function getElement(element)
	{
		 return document.getElementById(element);
	}

	
/*  **********************************************************
*
*	TAKES:		NOTHING
*	RETURNS:	NOTHING
*
**************************************************************************/

	function getWindowWidth()
	{
		return document.all?document.body.clientWidth:window.innerWidth; 	
	}

	
/*  **********************************************************
*
*	TAKES:		NOTHING
*	RETURNS:	NOTHING
*
**************************************************************************/

	function getWindowHeight()
	{
		return document.all?document.body.clientHeight:window.innerHeight; 	
	}

	
/*  **********************************************************
*
*	TAKES:		NOTHING
*	RETURNS:	NOTHING
*
**************************************************************************/

	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;	
		}
	}
	
/*  **********************************************************
*
*	TAKES:		NOTHING
*	RETURNS:	NOTHING
*
**************************************************************************/

	function checkIfMenuContainsMouse()
	{
		if (!( (containsMouse(headerMenu)) || (containsMouse(getElement("headerLinkContainer"))) ))
		{
			hideMenu();
		}
	}
	
/*  **********************************************************
*
*	TAKES:		NOTHING
*	RETURNS:	NOTHING
*
**************************************************************************/

	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));
	}

/* Toggle Checkbox **********************************************************
*
*	TAKES:		Checkbox Id
*	RETURNS:	NOTHING
*
**************************************************************************/

	function toggleCheckbox(checkboxId)
	{
		var checkboxElement = getElement(checkboxId);
		
		if (checkboxElement.checked)
		{
			checkboxElement.checked = false;
		}
		else
		{
			checkboxElement.checked = true;	
		}
	}
	
/* Toggle Element **********************************************************
*
*	TAKES:		Element Id
*	RETURNS:	NOTHING
*
**************************************************************************/

	function toggleElement(elementId)
	{
		var element = getElement(elementId);
		
		if (element)
		{
			if (element.style.display == "none")
			{
				showElement(element);
			}
			else
			{
				hideElement(element);
			}
		}
	}
	
/* 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 = "Error";
		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 = /^\d{2}\/\d{2}\/\d{2}$/;
				errorMessage = "This field can only contain date values in the form: MM/DD/YY";
				break;
			
			case "time":
			
				regExp = /^(0-1)(0-9):(1-5)(0-9) (A|P)M$/;
				errorMessage = "This field can only contain time values in the form: HH:MM AM";
				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;
	}

