///////////////////////////////////////////////////////////////////////////////////////////////////
//
// Session
//
///////////////////////////////////////////////////////////////////////////////////////////////////
var session_date    = new Date();
var session_start   = session_date.getTime();
var session_min     = 0;  // Minutes that have accumulated
var session_max     = 10; // Maximum session time in minutes
var session_timer   = 0;  // Interval


function startSession()
{
	document.onmousedown = function() { 
		session_min = 0;
		session_date = new Date();
		session_start = session_date.getTime(); 
	}
	clearInterval(session_timer);
	session_start = session_date.getTime();
	session_min = 0;
	session_timer = setInterval(checkSession, 60000);
}


function checkSession()
{
	session_min++;
	
	if (session_min == 8)
	{
		session_confirm = confirm('Your session will expire soon. You\'ll have 2 minutes to respond. Click OK to stay active or Cancel to logout.')
		if (session_confirm)
		{
			var current_date = new Date();
			var now = current_date.getTime();
			var session_length = Math.round((now - session_start)/60000);
			if (session_length >= session_max)
			{
				alert("Sorry, your timeframe to respond has expired. You will be logged out.");
				clearInterval(session_timer);
				location.replace('logout.php?sto');
			}
			startSession();
			return;
		}
		else
		{
			clearInterval(session_timer);
			location.replace('logout.php?sto');
		}
	}
}

// Login Page
function cancelSessionCheck()
{
	clearInterval(session_timer);
	delete session_timer;
}


startSession();


///////////////////////////////////////////////////////////////////////////////////////////////////
//
// Globals
//
///////////////////////////////////////////////////////////////////////////////////////////////////
var browser = (navigator.appName == 'Microsoft Internet Explorer' && navigator.appVersion.indexOf('MSIE 9.0') == -1) ? "ie" : "other";
var popupBackground;
var popupContent;
var splashbg;

var hour = 0;
var minute = 0;
var second = 0;
var tmzn = "";


///////////////////////////////////////////////////////////////////////////////////////////////////
//
// Functions
//
///////////////////////////////////////////////////////////////////////////////////////////////////
/**
 *	Determine if a value exists within an array
 *	@param needle
 *	@param haystack
 */
function in_array(needle, haystack)
{
	for (var obj in haystack)
	{
		if (haystack[obj] == needle) return true;
	}
	
	return false;
}

function inArray(needle, haystack)
{
	for (var obj in haystack)
	{
		if (haystack[obj] == needle) return true;
	}
	
	return false;
}


/**
 *	Sort an array numerically
 *	@param a
 *	@param b
 */
function sortNumeric(a,b)
{
	return a-b;
}


/**
 *  Produce a 2 digit minimum number format
 *	@param num
 */
function formatAddZero(num)
{
	if (num < 10) return "0" + num.toString();
	return num;
}


/**
 *  Compute hours and minutes from the total time in seconds
 *	@param num
 */
function updateTime() 
{
	second++;
	if (second == 60)
	{
		second = 0;
		minute++;
		if (minute == 60)
		{
			minute = 0;
			hour++;
			if (hour == 24)
			{
				hour = 0;
			}
		}
	}
	showTime();
}


function showTime()
{
	if (document.getElementById('clock') != undefined)
	{
		var ampm = (hour < 12) ? "am" : "pm";
		var tmp_hour = (hour > 12) ? (hour - 12) : hour;
		var tmp_hour = (tmp_hour == 0) ? 12 : tmp_hour;
		var time = formatAddZero(tmp_hour) + ":" + formatAddZero(minute) + ":" + formatAddZero(second);
		//document.getElementById('clock').innerHTML = "Server Time: " + time + " " + ampm + " (" + tmzn.toUpperCase() + ") " + session_min;
		document.getElementById('clock').innerHTML = "Server Time: " + time + " " + ampm + " (" + tmzn.toUpperCase() + ")";
	}
}

function startClock(_hour, _minute, _second, _tmzn) 
{
	hour = _hour;
	minute = _minute;
	second = _second;
	tmzn = _tmzn;
	showTime();
	var timer = setInterval(updateTime,1000);
}

function downloadActivity() 
{
	document.getElementById('activity_form').submit();
}


///////////////////////////////////////////////////////////////////////////////////////////////////
//
// Clean Text
//
///////////////////////////////////////////////////////////////////////////////////////////////////

var char_array = new Array(' ','0','1','2','3','4','5','6','7','8','9',
'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
'!','@','#','$','%','&','*','(',')','-','_','+','=',':',';','"','\'','<','>','?',',','.','/','\r','\n');

function scrubTXT(str)
{	
	var new_str = "";
	for (i=0; i < str.length; i++)
	{
		var character = str.charAt(i);
		if (in_array(character, char_array)) new_str += character;
	}
	return new_str;
}


function countTXT(field, counter, max_len, other) 
{
	var len = 0;
	var txt = scrubTXT(field.value);
	var sbj = (other != "") ? scrubTXT(document.getElementById(other).value) : "";
	var lines = 0;
	var lineMultiplier = (browser == "ie") ? 2 : 1;
	var countAdjust = (browser == "ie") ? 0 : 1;
	if (lines == 0) { tmp = txt.split("\n"); lines = tmp.length; }
	lines = (lines * 2) - (lines * lineMultiplier);
	len = txt.length + sbj.length + lines - countAdjust;
	if (len > max_len) 
	{
		if (browser == "ie") 
		{
			txt = txt.substr(0, (max_len-sbj.length + lines - countAdjust));
		}
		else
		{
			txt = txt.substr(0, (max_len-sbj.length + lines - (lines * 2) + countAdjust));
		}
		len = txt.length + sbj.length + lines - countAdjust;
	}
	if (field.value != txt) field.value = txt;
	field.scrollTop = field.scrollHeight;
	document.getElementById(counter).innerHTML = len + "/" + max_len;
}


///////////////////////////////////////////////////////////////////////////////////////////////////
//
// Images
//
///////////////////////////////////////////////////////////////////////////////////////////////////
if (document.images)
{
	var logo = new Image(144,82);
	logo.src = "images/logo_rgo.png";
	
	var loader = new Image(43,13);
	loader.src = "images/loading_block.gif";
	
	var lock = new Image(25,25);
	lock.src = "images/lock.png";
}


///////////////////////////////////////////////////////////////////////////////////////////////////
//
// Contact Us
//
///////////////////////////////////////////////////////////////////////////////////////////////////
function validateContactMessage()
{
	var subj = document.getElementById('subject');
	var body = document.getElementById('body');
	if (subj.value.length < 1)
	{
		alert("Please provide a subject line for your request");
		subj.focus();
		return false;
	}
	if (body.value.length < 1)
	{
		alert("The message body cannot be left blank");
		body.focus();
		return false;
	}
	document.getElementById('send_btn').style.visibility = "hidden";
	document.getElementById('sending_btn').style.display = "inline-block";
	return true;
}



/**
 *	Return the dimension of the current window
 */
function createOverlay()
{
	var pageSize = getPageSize();
	var overlay = document.createElement('div');
	overlay.setAttribute('id', 'overlay');
	overlay.setAttribute('classname', 'overlay');
	//overlay.setAttribute('onclick', 'removeOverlay()');
	overlay.style.height = pageSize[0] + 'px';
	overlay.style.width = pageSize[1] + 'px';
	document.getElementsByTagName('body')[0].appendChild(overlay);
}


/**
 *	Return the dimension of the current window
 */
function removeOverlay()
{
	var overlay = document.getElementById('overlay');
	overlay.parentNode.removeChild(overlay);
}


/**
 *	Return the dimension of the current window
 */
function getPageSize()
{
	var xScroll      = 0; 
	var yScroll      = 0;
	var windowWidth  = 0;
	var windowHeight = 0;
	
	if (window.innerHeight) 
	{	
		if (document.documentElement.scrollHeight && document.documentElement.scrollHeight > document.body.scrollHeight)
		{
			yScroll = document.documentElement.scrollHeight;
			xScroll = document.documentElement.scrollWidth;
		}
		else
		{
			yScroll = document.body.scrollHeight;
			xScroll = document.body.scrollWidth;
		}
		windowHeight = window.innerHeight;
		windowWidth  = window.innerWidth;
	}
	else if (document.documentElement)
	{
		yScroll      = document.documentElement.scrollHeight;
		xScroll      = document.documentElement.scrollWidth;
		windowHeight = document.documentElement.clientHeight;
		windowWidth  = document.documentElement.clientWidth;
	}
	else if (document.body)
	{
		xScroll      = document.body.offsetWidth;
		yScroll      = document.body.offsetHeight;
		windowHeight = document.body.clientHeight;
		windowWidth  = document.body.clientWidth;
	}
	
	pageHeight = (yScroll < windowHeight) ? windowHeight : yScroll;
	pageWidth  = (xScroll < windowWidth)  ? windowWidth  : xScroll;

	return [pageHeight,pageWidth];
}



///////////////////////////////////////////////////////////////////////////////////////////////////
//
// Book Marks
//
///////////////////////////////////////////////////////////////////////////////////////////////////
function createBookMark()
{
	if (document.all)
	{
		window.external.AddFavorite(location.href,document.title);
		return;
	}
	
	if (window.sidebar)
	{
		window.sidebar.addPanel(document.title,location.href,'');
		return;
	}
}


///////////////////////////////////////////////////////////////////////////////////////////////////
//
//
//
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
//
// Show Splash
//
///////////////////////////////////////////////////////////////////////////////////////////////////
function openVideo(_src, _splash)
{
	var output = "";
	output += "<!-- START OF THE PLAYER EMBEDDING TO COPY-PASTE -->\n";
	output += "<object id=\"player\" classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" name=\"player\" width=\"980\" height=\"584\">\n";
	output += "<param name=\"movie\" value=\"player.swf\" />\n";
	output += "<param name=\"allowfullscreen\" value=\"false\" />\n";
	output += "<param name=\"allowscriptaccess\" value=\"always\" />\n";
	output += "<param name=\"wmode\" value=\"transparent\">\n";
	output += "<param name=\"flashvars\" value=\"file="+_src+"&skin=longtail/glow.zip&image="+_splash+"\" />\n";
	output += "<embed\n";
	output += "type=\"application/x-shockwave-flash\"\n";
	output += "id=\"player\"\n";
	output += "name=\"player2\"\n";
	output += "src=\"player.swf\" \n";
	output += "width=\"980\" \n";
	output += "height=\"584\"\n";
	output += "allowscriptaccess=\"always\"\n"; 
	output += "allowfullscreen=\"false\"\n";
	output += "wmode=\"transparent\"\n";
	output += "flashvars=\"file="+_src+"&skin=longtail/glow.zip&image="+_splash+"\"\n"; 
	output += "/>\n";
	output += "</object>\n";
	output += "<!-- END OF THE PLAYER EMBEDDING -->\n";
	
	/*
	// Get page size (lightbox function)
	var arrayPageSize = this.getPageSize();
	var pageWidth = arrayPageSize[0] + 'px';
	var pageHeight = arrayPageSize[1] + 'px';
	
	if (browser == "ie")
	{
		splashbg = document.createElement('<div id="splashbg" class="splashbg" style="width:'+pageWidth+';height:'+pageHeight+';" onclick="javascript:removeSplash();"></div>');
	}
	else
	{
		splashbg = document.createElement('div');
		splashbg.setAttribute('id', 'splashbg');
		splashbg.setAttribute('class','splashbg');
		splashbg.setAttribute('style','width:'+pageWidth);
		splashbg.setAttribute('style','height:'+pageHeight);
		//splashbg.setAttribute('onclick', 'closeVideo()'); 
	}
	document.getElementsByTagName('body')[0].appendChild(splashbg);
	*/
	
	createOverlay();
	
	if (browser == "ie")
	{
		splashmovie = document.createElement('<div id="splashmovie" class="splashmovie"></div>');
		
		closemoviebutton = document.createElement('<img class="close" src="videos/close.png" onclick="javascript:closeVideo();" />');
	}
	else
	{
		splashmovie = document.createElement('div');
		splashmovie.setAttribute('id', 'splashmovie');
		splashmovie.setAttribute('class','splashmovie');
		
		closemoviebutton = document.createElement('img');
		closemoviebutton.setAttribute('id', 'closemoviebutton');
		closemoviebutton.setAttribute('class','close');
		closemoviebutton.setAttribute('src','videos/close.png');
		closemoviebutton.setAttribute('onclick', 'closeVideo()');
	}

	splashmovie.innerHTML = output;
	document.getElementsByTagName('body')[0].appendChild(splashmovie);
	
	splashmovie.appendChild(closemoviebutton);
	
	// Objects
	var obj_array = document.getElementsByTagName('object');
	for (obj in document.getElementsByTagName('object'))
	{
		if (typeof obj_array[obj] != 'object') continue;
		if (obj_array[obj].id != 'player') obj_array[obj].style.visibility = 'hidden';
	}
	
	// Activity Report Fix
	if (document.getElementById('flash-report')) document.getElementById('flash-report').style.visibility = 'hidden';
}


function closeVideo()
{
	var splashmovie = document.getElementById('splashmovie');
	//var splashbg = document.getElementById('splashbg');
	
	splashmovie.parentNode.removeChild(splashmovie);
	//splashbg.parentNode.removeChild(splashbg);
	
	removeOverlay();
	
	// Objects
	var obj_array = document.getElementsByTagName('object');
	for (obj in obj_array)
	{
		if (typeof obj_array[obj] != 'object') continue;
		if (obj_array[obj].id != 'player') obj_array[obj].style.visibility = 'visible';
	}
	
	// Activity Report Fix
	if (document.getElementById('flash-report')) document.getElementById('flash-report').style.visibility = 'visible';
}











