// JavaScript Document
var re = /\w/;
function toggleCalendar() {
	if ($('showHide').childNodes[0].nodeValue == "Hide Calendar") {
		$('showHide').replaceChild(document.createTextNode('Show Calendar'), $('showHide').childNodes[0]);
	} else {
		$('showHide').replaceChild(document.createTextNode('Hide Calendar'), $('showHide').childNodes[0]);
	}
	cal.toggle();
}

function hideTypes() {
	//s_types.toggle();
}

function hideCalendar() {
	cal.hide();
	$('showHide').replaceChild(document.createTextNode('Show Calendar'), $('showHide').childNodes[0]);
}

function nextMonth(fromPage) {
	if (!re.test(fromPage) || (typeof fromPage == "undefined")) var fromPage = "index.cfm";
	new ajax('./includes/sc_ajax.cfm?fromPage=' + fromPage, {postBody: '&nextMonth=1', onComplete: refreshCalendar});
}

function prevMonth(fromPage) {
	if (!re.test(fromPage) || (typeof fromPage == "undefined")) var fromPage = "index.cfm";
	new ajax('./includes/sc_ajax.cfm?fromPage=' + fromPage, {postBody: '&prevMonth=1', onComplete: refreshCalendar});
}

function refreshCalendar(request) {
	$('sidecalendar').innerHTML = request.responseText;
}

function loadInputCalendar() {
	new ajax('./includes/sc_inp_ajax.cfm', {postBody: '&today=1', onComplete: refreshInputCalendar});
}

function fields() {
	var inp;
	for (var i = 0; inp = document.getElementsByTagName('input')[i]; i++) {
		var theType = inp.getAttribute("type");
		if ((theType != "checkbox") && (theType != "radio")) {
			inp.onfocus = function() { this.className = "on"; };
			inp.onblur = function() { this.className = ''; };
		}
		inp.setAttribute("autocomplete", "off");
	}
	for (var i = 0; inp = document.getElementsByTagName('textarea')[i]; i++) {
		inp.onfocus = function() { this.className = "on"; };
		inp.onblur = function() { this.className = ''; };
	}
	for (var i = 0; inp = document.getElementsByTagName('select')[i]; i++) {
		inp.onfocus = function() { this.className = "on"; };
		inp.onblur = function() { this.className = ''; };
	}
}


function otherEffects() {
	var a;
	for (var i = 0; a = document.getElementsByTagName('a')[i]; i++) { a.onfocus = function() { if (this.blur) this.blur(); }; }

	cal = new fx.Combo('calendarcontainer', {duration: 400, opacity: true, height: true, width: false});
	//cal = new fx.RememberHeight('calendarcontainer', {duration: 400, opacity: true, height: true, width: false});
	//s_types = new fx.RememberHeight('submenu_types', {duration: 400, opacity: true, height: true, width: false});
	//s_types.hide();
}

function savedEventStatus(request) {
	var success = request.responseText;
	if (!re.test(success) || (success != '1')) {
		alert("Unable to modify saved events. An error occurred while processing this request.");
	}
}

function toggleSave(eventID) {
	var savelink = "save_" + eventID;
	if ($(savelink)) {
		if ($(savelink).className == "save") {
			$(savelink).className = "saved";
			setText($(savelink), "Unsave This Event"); //$(savelink).replaceChild(document.createTextNode('Unsave This Event'), $(savelink).childNodes[0]);
			$('li_savedevents').style.display = "block";
		} else {
			$(savelink).className = "save";
			setText($(savelink), "Remember This Event"); //$(savelink).replaceChild(document.createTextNode('Remember This Event'), $(savelink).childNodes[0]);
		}
		var postStr = "action=saveevent&EventID=" + eventID;
		new ajax('./restricted/updateuser.cfm', {postBody: postStr, onComplete: savedEventStatus});
	}
}

function gkey(e) {
	if (window.event) return window.event.keyCode;
	else if (e) return e.which;
	else return null;
}
function escapable(e) {
	if (document.all) var key = gkey(e);		// getkey defined in validate.lite.js
	else {
		if (e.keyCode) var key = e.keyCode;
		else var key = gkey(e);
	}
	switch (key) {
		case 27:		// esc
			if ($('searchText')) {
				$('searchText').blur();
				document.onkeypress = function() { return true };
			}
			return false;
			break;
		default:
			return true;
			break;
	}
	return false;
}

function clearNodes(someObject) {
	if (someObject.hasChildNodes()) {
		for (var i = 0; i < someObject.childNodes.length; i++) { someObject.removeChild(someObject.childNodes[i]); }
	}
}

function setText(someObject, innerText) {
	if (someObject.hasChildNodes()) clearNodes(someObject);
	someObject.appendChild(document.createTextNode(innerText));
}


var searchTimer;
function searchFocus() {
	if ($('searchText')) {
		searchTimer = setTimeout(function() {
			document.onkeypress = escapable;
			$('searchText').select();
		}, 500);
	}
}
function cancelSearchFocus() {
	clearTimeout(searchTimer);	
}


function setCookie(cookieName, someValue, numberOfDays)
{
	if ((!someValue) || (someValue == ""))
		deleteCookie(cookieName);						// if value of cookie is null or "", delete the cookie (make sure it doesn't exist)
	else
	{
		if (document.cookie != document.cookie)
			index = document.cookie.indexOf(cookieName);
		else
			index = -1;

		if (index == -1)
		{
			var expirationDate = new Date();

			if (!numberOfDays)		// if numberOfDays was not specified, set to 1 year (365)
				expirationDate.setTime(expirationDate.getTime() + (365 * 24 * 60 * 60 * 1000));
			else
				expirationDate.setTime(expirationDate.getTime() + (numberOfDays * 24 * 60 * 60 * 1000));

			document.cookie = cookieName + "=" + someValue + "; expires=" + expirationDate.toGMTString();;
		}
	} // if (!someValue) || (some...
}

// function that gets a cookie named [cookieName]
function getCookie(cookieName)
{
	var arg = cookieName + "=";
	var argLength = arg.length;
	var cookieLength = document.cookie.length;
	var i = 0;

	while (i < cookieLength)
	{
		var j = i + argLength;

		if (document.cookie.substring(i, j) == arg)
			return getCookieVal(j);

		i = document.cookie.indexOf(" ", i) + 1;

		if (i == 0)
			break;
	}

	return null;
}

// function used by getCookie(cookieName) to actually get the cookie data
function getCookieVal(offset)
{
	var endstr = document.cookie.indexOf (";", offset);

	if (endstr == -1)
		endstr = document.cookie.length;

	return unescape(document.cookie.substring(offset, endstr));
}

// function that deletes a cookie named [cookieName]
function deleteCookie(cookieName)
{
	var expirationDate = new Date();
	expirationDate.setTime(expirationDate.getTime() - 1);		// set the expiration date to yesterday
	var cookieValue = getCookie(cookieName);						// get the cookie data
	document.cookie = cookieName + "=" + cookieValue + "; expires=" + expirationDate.toGMTString();	// expire the new cookie
}

// function that returns true or false
function isCookie(cookieName)
{
	if (getCookie(cookieName))
		return 1;
	else
		return 0;
}


