/////////////////////////////////////////////////////////////
//	General Utility Class
//
//	REQUIRES: Prototype v1.6+, Scriptaculous v1.8+
//
/////////////////////////////////////////////////////////////


////////////////////////////////
//	UTILITY CLASS
//	
////////////////////////////////
var Utility = {
			
	//	TEXT/FORM VALIDATION 
	
	////////////////////////////////
	//	isVisible - See whether or not
	//	an element is currently visible.
	isVisible: function(e) {
		//	Whereas e = element ID
		//returns true if e is should be visible to user.
		if (typeof e == "string") {
			e = document.getElementById(e);
		}
		while (e.nodeName.toLowerCase() != 'body' && e.style.display.toLowerCase() != 'none' && e.style.visibility.toLowerCase() != 'hidden') {
			e = e.parentNode;
		}
		if (e.nodeName.toLowerCase() == 'body') {
			return true;
		} else{
			return false;
		}
	},
	
	////////////////////////////////
	//	validatePhoneNumber - Ensures
	//	that phone numbers are formatted
	//	to a (000) 000-0000 format
	validatePhoneNumber: function(e)
	{
		var value = $(e).value;
		value = value.replace(/[^\d]+/ig, "");
	
		if(value.length == 7)
		{
			value = "(503) " + value;
			value = value.slice(0,9) + "-" + value.slice(9,14);
		}
		else if(value.length == 10) {
			value = "(" + value.slice(0,3) + ") " + value.slice(3,6) + "-" + value.slice(6,10);
		}
		else if(value.length == 11) {
			value = value.slice(0,1) + " (" + value.slice(1,4) + ") " + value.slice(4,7) + "-" + value.slice(7,11);
		}
		else if(value.length > 0 && value.length < 7)
			new Effect.Highlight( e, {startcolor:'#ff0000'} );
			
		$(e).value = value;
	},
	
	////////////////////////////////
	//	validateStateCode - Ensures
	//	that the STATE code is always
	//	two letters and is capitalized
	validateStateCode: function(e)
	{
		if( $(e).value.length < 2 )
			new Effect.Highlight( e, {startcolor:'#ff0000'} );
		else
			$(e).value = $(e).value.toUpperCase();
	},
	
	////////////////////////////////
	//	validateDate - 
	//	Ensure that the displayed date
	//	format is standardized,
	//	mm/dd/yyyy
	validateDate: function(e) 
	{
		var value = $(e).value
		var mysql_date_fmt = /(\d{4})-(\d{2})-(\d{2})/
		var std_date_fmt = /\d{2}-\d{2}-\d{4}/
		
		if(value.length == 4)
		{
			var year = value.slice(2,4);
			// If year starts with 0, add 20 to it. else, ad 19
			if(year.slice(0,1) == 0)
				year = "20" + year;
			else
				year = "19" + year;
				
			value = "0" + value.slice(0,1) + "/0" + value.slice(1,2) + "/" + year;
		}
		else if(value.length == 5)
		{
			var year = value.slice(3,5);
			// If year starts with 0, add 20 to it. else, ad 19
			if(year.slice(0,1) == 0)
				year = "20" + year;
			else
				year = "19" + year;
				
			value = "0" + value.slice(0,1) + "/" + value.slice(1,3) + "/" + year;
		}
		else if(value.length == 6)
		{
			// Problem. This could either be 121005 as meaning 12/10/2005, or it could be 231978 meaning
			// 02/03/1978. Must check value
			var year = value.slice(2,6);			
			value = "0" + value.slice(0,1) + "/0" + value.slice(1,2) + "/" + year;
		}
		else if(value.length == 7)
		{
			var year = value.slice(3,7);
			value = "0" + value.slice(0,1) + "/" + value.slice(1,3) + "/" + year;
		}
		else if(value.length == 8)
		{
				value = value.slice(0,2) + "/" + value.slice(2,4) + "/" + value.slice(4,8);
		}
		
		if(value == "00/00/0000")
			value = "";
		
		$(e).value = value;	
	},
	
	////////////////////////////////
	//	validateEmail - Basic check
	//	to ensure email address contains
	//	the @ and . symbols. 
	validateEmail: function(e)
	{
		var atpos = $(e).value.indexOf("@");
		var dotpos = $(e).value.lastIndexOf(".");
		
		if (atpos < 1 || dotpos < (atpos + 2) || (dotpos + 2) >= $(e).value.length)
		{
			//new Effect.Highlight( e, {startcolor:'#ff0000'} );
			return false;
		}
		else 
			return true;
	},
	
	//	IMAGE MANIPULATION
	
	////////////////////////////////
	//	changeImage - Swap the src
	//	image of referenced source
	//	element
	changeImage: function(e, img)
	{
		$(e).src = img;
	},
	
	//	ELEMENT & COLOR MANIPULATION
	
	////////////////////////////////
	//	changeImage - Swap the src
	//	image of referenced source
	//	element
	changeBackgroundColor: function(e, color)
	{
		$(e).style.backgroundColor=Color;
		$(e).style.bgColor=Color;
	}
	
	
};
