/* Create a name space for JavaScript used by the site. This way, we'll be less
 * likely to step on any other libraries and code. */

var SENIORS = {
	utils : {},
	contact : {},
	locations : {},
	tinyMce : {},
	form: {}
};


SENIORS.utils.getActiveStyle = function(obj)
{
  	
	// Ensure that we have an object and not just the ID of an HTML element.
	
	obj = SENIORS.utils.getObject(obj);
	
	if (obj.currentStyle)
	{
		// Internet Explorer
		return obj.currentStyle;
	}
	else if (document.defaultView && document.defaultView.getComputedStyle)
	{
		// Mozilla/FireFox
		return document.defaultView.getComputedStyle(obj, null);
	}
	else
	{
		// Fall back to non-computed style.
		return obj.style;
	}
  	
};

// Takes 1 argument which is either the ID or object reference of elements whose
// style display mode will be toggled.

SENIORS.utils.getObject = function(param)
{
	if (typeof(param) == "object")
	{
		return param;
	}
	else
	{
		return document.getElementById(param);
	}
};

SENIORS.utils.toggleDisplay = function()
{
	for ( var i = 0; i < arguments.length; i++ )
	{
		
		var obj = SENIORS.utils.getObject(arguments[i]);
		
		if (obj == null)
		{
			alert("Could not find object with ID " + arguments[i] + " in the page.");
		}
		
		var objStyle = SENIORS.utils.getActiveStyle(obj);
		
		// Since all elements are hidden with a value of "none", whereas each
		// element may have its own display value, we'll check for "none".
		
		if (objStyle.display == "none")
		{
			SENIORS.utils.show(obj);
		}
		else
		{
			SENIORS.utils.hide(obj);
		}
	
	}
};

SENIORS.utils.show = function(obj)
{
	
	// Make sure we have an object and not just the ID of the object.
	
	obj = SENIORS.utils.getObject(obj);
	
	obj.style.display = SENIORS.utils.getDefaultDisplay(obj);

	obj.style.visibility = "visible";
	
};


SENIORS.utils.hide = function(obj)
{
	
	// Make sure we have an object and not just the ID of the object.
	
	obj = SENIORS.utils.getObject(obj);
	
	obj.style.display = "none";
	
	obj.style.visibility = "hidden";
	
};

// Determines the default display value for the object's HTML element type. For
// instance, a DIV would have a default display of "block". This method should
// ensure that we use the correct display value for each browser.

SENIORS.utils.getDefaultDisplay = function(obj)
{
	
	// Make sure we have an object and not just the ID of the object.
	
	obj = SENIORS.utils.getObject(obj);
	
	var value = "";
	
	// Create a new element with a tag name that corresponds to the object.
	
	var objCopy = document.createElement(obj.tagName);
	
	// Unless we add the element to the document, we won't know the actual
	// display value. IE will return an empty string; Firefox, "block".
	
	// By setting the visibility to hidden, we ensure that the user will not
	// see any such changes.
	
	objCopy.visibility = "hidden";
	
	document.body.appendChild(objCopy);
	
	value = SENIORS.utils.getActiveStyle(objCopy).display;
	
	document.body.removeChild(objCopy);
	
	return value;
	
};

/******************************************************************************
 **********                      Contact Form                        **********
 ******************************************************************************/

SENIORS.contact.stateVisible = true;

SENIORS.contact.handleCountrySelection = function(country, state, province, selectedValue)
{
	
	country = SENIORS.utils.getObject(country);
	
	state = SENIORS.utils.getObject(state);
	
	province = SENIORS.utils.getObject(province);
	
	var countryCode = country.options[country.selectedIndex].value;
	
	if (SENIORS.contact.countryHasStates(countryCode))
	{
		
		var states = SENIORS.contact.countries[countryCode];
		
		SENIORS.contact.populateSelectList(state, states, selectedValue);
		
		SENIORS.contact.showState(state, province);
		
	}
	else
	{
		
		SENIORS.contact.showProvince(state, province);
		
		province.value = selectedValue;
		
	}
	
};

SENIORS.contact.showState = function(state, province)
{
	
	SENIORS.utils.show(state);
	
	SENIORS.utils.hide(province);
	
	SENIORS.contact.stateVisible = true;
	
};

SENIORS.contact.showProvince = function(state, province) {
	
	SENIORS.utils.show(province);
	
	SENIORS.utils.hide(state);

	SENIORS.contact.stateVisible = false;
		
};

SENIORS.contact.countries = { };

SENIORS.contact.countryHasStates = function(country)
{
	if (SENIORS.contact.countries[country])
	{
		return true;
	}
	else
	{
		return false;
	}
};

SENIORS.contact.setState = function(stateSelectList, provinceText, stateName)
{
	
	if (SENIORS.contact.stateVisible)
	{
		
		stateSelectList = SENIORS.utils.getObject(stateSelectList);
		
		SENIORS.form.selectOption(stateSelectList, stateName);
		
	}
	else
	{
		
		provinceText = SENIORS.utils.getObject(provinceText);
		
		provinceText.value = stateName;
		
	}
	
};

SENIORS.contact.handleStateSelection = function(stateSelectList)
{
	
	stateSelectList = SENIORS.utils.getObject(stateSelectList);
	
	state = SENIORS.form.getValue(stateSelectList);
	
};

/* Interests and Sources */

SENIORS.contact.interests = { };

SENIORS.contact.handleInterestSelection = function(interest, source, selectedValue)
{
	
	interest = SENIORS.utils.getObject(interest);
	
	source = SENIORS.utils.getObject(source);
	
	var interestName = interest.options[interest.selectedIndex].value;
	
	var interestConfig = SENIORS.contact.interests[interestName];
	
	// Fetch the list of sources for this interest. If we have any, display
	// them in the drop down. Otherwise, hide the drop down.
	
	var sources = interestConfig.options;
	
	if (sources.length == 0)
	{
		
		SENIORS.utils.hide("SourceRow");
		
		SENIORS.form.clearSelectList(source);
		
	}
	else
	{
		
		SENIORS.utils.show("SourceRow");
		
		SENIORS.contact.populateSelectList(source, sources, selectedValue);
		
	}
	
	// Whether or not the user will be asked for their time frame for making a
	// decision will depend on the interest.
	
	if (interestConfig.showDecisionTimeFrame)
	{
		SENIORS.utils.show("DecisionTimeFrameRow");
	}
	else
	{
		SENIORS.utils.hide("DecisionTimeFrameRow");
	}
	
	// Sync up the controls dependent on source.
	
	SENIORS.contact.handleSourceSelection(source);
	
};

SENIORS.contact.handleSourceSelection = function(source)
{
	
	var source = SENIORS.utils.getObject(source);
	
	var selectedSource = SENIORS.form.getValue(source);
	
	if (selectedSource == "Other")
	{
		SENIORS.utils.show("SourceRow_Other");
	}
	else
	{
		SENIORS.utils.hide("SourceRow_Other");
	}
	
};

// Takes a select list object and an array objects. The objects must contain
// name and value properties.

SENIORS.contact.populateSelectList = function(selectList, options, selectedValue)
{
	
	SENIORS.form.clearSelectList(selectList);
	
	for (var i = 0; i < options.length; i++)
	{
		
		var optionData = options[i];
		
		var option = new Option(optionData.text, optionData.value);
		
		if (selectedValue)
		{
			if (option.value == selectedValue)
			{
				option.selected = true;
			}
		}
		
		selectList.options[i] = option;
		
	}
	
};

/******************************************************************************
 **********                         Form                             **********
 ******************************************************************************/

SENIORS.form.clearSelectList = function(selectList)
{
	for (var i = selectList.length - 1; i >= 0; i--)
	{
		selectList.options[i] = null;
	}
};


SENIORS.form.selectOption = function(selectList, optionValue)
{
	
	for (var i = 0; i < selectList.length; i++)
	{
		
		var option = selectList.options[i];
		
		if (option.value == optionValue)
		{
			option.selected = true;
		}
		
	}
	
};

SENIORS.form.getValue = function(element)
{
	
	element = SENIORS.utils.getObject(element);
	
	if (element.options)
	{
		if (element.options.length)
		{
			return element.options[element.selectedIndex].value;
		}
		else
		{
			return "";
		}
	}
	else
	{
		return element.value;
	}
	
};

/******************************************************************************
 **********                        TinyMCE                           **********
 ******************************************************************************/

/* Converts textarea elements to WYSIWYG editors. Takes the path to the root of
 * the Web site and an array of element IDs. */
SENIORS.tinyMce.convert = function(path, elements)
{
	
	if (!path.endsWith("/"))
	{
		path += "/";
	}
	
	var config = {
		mode : "exact",
		elements : elements.join(),
		theme : "advanced",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		theme_advanced_buttons1 : "bold, italic, underline, strikethrough, sub, sup, |, justifyleft, justifycenter, justifyright, justifyfull, |, formatselect, removeformat, |, forecolor, backcolor",
		theme_advanced_buttons2 : "bullist, numlist, |, outdent, indent, |, undo, redo, |, link, unlink, anchor, |, table, image, hr, charmap, |, cleanup, code",
		theme_advanced_buttons3 : "",
		content_css : path + "App_Themes/Default/TinyMce.css",
		plugins : "table, imagemanager"
	};
	
	tinyMCE.init(config);
	
};

/******************************************************************************
 **********                     Locations Page                       **********
 ******************************************************************************/

/* Called from the onchange handler of a select list. Assumes the option value
 * is the center URL. Sends the user to that URL in a new window. */
SENIORS.locations.goToCenter = function(select)
{
	
	var url = select.options[select.selectedIndex].value;
	
	if (url)
	{
		window.open(url);
	}
	
};

/******************************************************************************
 **********                         String                           **********
 ******************************************************************************/

String.prototype.endsWith = function(string)
{
	
	if (string == "")
	{
		return true;
	}
	
	if (this.length < string.length)
	{
		return false;
	}
	
	if (this.right(string.length) == string)
	{
		return true;
	}
	
	return false;
	
};

String.prototype.right = function(count)
{
	return this.substring(this.length - count, this.length);
};