﻿function StopPropagation(e)
{
	//check to see if e is defined. if not, use window.event 
	(e || window.event).cancelBubble = true;
}

function DeterminePagePrefix(control_name, control_tag)
{
	var return_value = "";
	var matching_control_types = document.getElementsByTagName(control_tag);
	var match_index = 0;
	
	for (var i = 0; i < matching_control_types.length; i++)
	{
		if ((match_index = matching_control_types[i].id.indexOf(control_name)) > -1)
		{
			return_value = matching_control_types[i].id.substring(0, match_index);
			break;
		}
	}

	return return_value;
}

function CheckForEnterKey(e, function_to_call)
{
	var evt = (e || window.event);
	var code = (e.keyCode) ? e.keyCode : e.which;

	if (code == 13)
		function_to_call(e);
}

function Search(e)
{
	var server_control_prefix = DeterminePagePrefix("textbox_search", "INPUT");
	document.getElementById(server_control_prefix + "button_search").click();
	StopPropagation(e);
	e.returnValue = false;
}

function ToggleImage(image_object, image_src)
{
	image_object.src = image_src;
}

/**
* Creates a cookie on the clients machine if it doesn't exist. Or just updates it if it does.
* @param {string} name The name of the cookie to create.
* @param {string} value The value to write to the cookie.
* @param {date} expires The date that the cookie should expire. Setting this to null will cause the cookie to expire when the browser closes. 
* @param {string} path Setting a path for the cookie allows the current document to share cookie information with other pages within the same domain—that is, if the path is set to /thispathname, all pages in /thispathname and all pages in subfolders of /thispathname can access the same cookie information.
* @param {string} domain Setting the domain of the cookie allows pages on a domain made up of more than one server to share cookie information.
* @param {string} secure Setting a cookie as secure; means the stored cookie information can be accessed only from a secure environment.
*/
function SetCookie(name, value, expires, path, domain, secure)
{
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime(today.getTime());

	/*
	if the expires variable is set, make the correct expires time, the current script below will set it for x
	number of days, to make it for hours, delete * 24, for minutes, delete * 60 * 24
	*/
	if (expires)
		expires = expires * 1000 * 60 * 60 * 24;

	var expires_date = new Date(today.getTime() + expires);

	document.cookie = name + "=" + escape(value) + ((expires) ? ";expires=" + expires_date.toGMTString() : "") +
							((path) ? ";path=" + path : "") + ((domain) ? ";domain=" + domain : "") +
							((secure) ? ";secure" : "");
};

/**
* Returns the value for the cookie.
* @param {string} name The name of the cookie to retrieve.
* @return The cookie value, if present or null if it doesn't.
* @type string
*/
function GetCookie(name)
{
	var start = document.cookie.indexOf(name + "=");
	var len = start + name.length + 1;

	if ((!start && name != document.cookie.substring(0, name.length)) || start == -1)
		return null;

	var end = document.cookie.indexOf(";", len);
	if (end == -1)
		end = document.cookie.length;

	return unescape(document.cookie.substring(len, end));
};

/**
* Deletes a cookie, if it exists.
* @param {string} name The name of the cookie to delete.
* @param {string} path The path that was used when creating the cookie.
* @param {string} domain The domain that was used when creating the cookie.
*/
function DeleteCookie(name, path, domain)
{
	if (GetCookie(name))
		document.cookie = name + "=" + ((path) ? ";path=" + path : "") + ((domain) ? ";domain=" + domain : "") +
								";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

/**
* Registers a listener to an object for a particular event.
* @param {Object} object The object to attach our listener to.
* @param {string} event_name The name of the event to listen for. Do not pass the 'on' prefix. (i.e. click, mouseover, etc...)
* @param {function} callback The function to call when the event occurs.
* @return The status of whether it was able to register for the event.
* @type boolean
*/
function AddEvent(object, event_name, callback)
{
	var return_value = false;

	if (object.addEventListener)
	{
		object.addEventListener(event_name, callback, true);
		return_value = true;
	}
	else if (object.attachEvent)
		return_value = object.attachEvent("on" + event_name, callback);

	return return_value;
}

/**
* Removes a listener from an object for a particular event.
* @param {Object} object The name of the cookie to retrieve.
* @param {string} event_name The name of the event to listen for. Do not pass the 'on' prefix. (i.e. click, mouseover, etc...)
* @param {function} callback The function to call when the event occurs.
* @return The status of whether it was able to remove the listener for the event.
* @type boolean
*/
function RemoveEvent(object, event_name, callback)
{
	var return_value = false;

	if (object.removeEventListener)
	{
		object.removeEventListener(event_name, callback, true);
		return_value = true;
	}
	else if (object.detachEvent)
		return_value = object.detachEvent("on" + event_name, callback);

	return return_value;
}