
function openWindow(url, name, specifiedOptions)
{
	if ( !name || name == '__blank' ) name = Math.random();

	var optionsString = '';
		
	if (!prototypeAvailable()) {

		var options = new Array();
		options['width'] = '400';
		options['height'] = '300';
		options['status'] = 'yes';
		options['resizable'] = 'yes';
		options['scrollbars'] = 'no';
		options['menubar'] = 'no';
		options['toolbar'] = 'no';
		options['location'] = 'no';
	
		if ( specifiedOptions ) {
			var optionsArray = specifiedOptions.split(",");
			
			for ( var i = 0; i < optionsArray.length; i++ ) {
				var tempArray = optionsArray[i].split("=");
				
				options[tempArray[0]] = tempArray[1];
			}
		}
		
		for ( key in options ) {
			optionsString = optionsString + key + '=' + options[key] + ',';
		}
		
	}
	else {
		var options = new Hash();
		options.set('width', '400');
		options.set('height', '300');
		options.set('status', 'yes');
		options.set('resizable', 'yes');
		options.set('scrollbars', 'no');
		options.set('menubar', 'no');
		options.set('toolbar', 'no');
		options.set('location', 'no');
		
		if ( specifiedOptions ) {
			var optionsArray = specifiedOptions.split(",");
			
			for ( var i = 0; i < optionsArray.length; i++ ) {
				var tempArray = optionsArray[i].split("=");
				options.set(tempArray[0], tempArray[1]);
			}
		}
		
		options.each(function(pair) {
		  optionsString = optionsString + pair.key + '=' + pair.value + ',';
		  
		});
	}
	
	var newWin = window.open(url, name, optionsString);
	newWin.focus();
	
	return false;
}


function OpenWindow(url, name, specifiedOptions)
{
	if (location.hostname.search(/\.local/)!=-1) {
		alert('OpenWindow is deprecated. Use openWindow instead.');
	}
	// THIS FUNCTION IS DEPRECIATED.
	return openWindow(url, name, specifiedOptions);
}

function confirmAction(targetURL, message)
{
	if ( confirm("Are you sure you wish to " + message) ) {
		if ( !targetURL ) {
			return true;	
		}
		else {
			window.location = targetURL;
			return false;
		}
	}
	else {
		return false;
	}
}


function logAuditEvent(message)
{
	var targetURL = '/common/audit_log.php';
	var path = window.location.toString();
	
	var columns = 'URL,Message';
	var values = path + ',' + message;
	
	postAction(targetURL, columns, values, '', true);
}


function postAction(targetURL, columns, values, message, maintainPage)
{
	if ( message ) {
		var confirmed = confirm("Are you sure you wish to " + message);
	}
	
	columns = new String(columns);
	values = new String(values);
	
	if ( !message || confirmed ) {
		
		if (window.XMLHttpRequest) {
			req = new XMLHttpRequest();
		} 
		else if (window.ActiveXObject) {
			req = new ActiveXObject("Microsoft.XMLHTTP");
		}
		else {
			return;	
		}
		
		columnArray = columns.split(",");
		valueArray = values.split(",");
		
		req.open("POST", targetURL, false);
		
		var PostContents = new String();
		
		if ( columnArray.length > 0 ) {
			for ( var i = 0; i < columnArray.length; i++ ) {
				PostContents = PostContents + columnArray[i] + "=" + valueArray[i] + "&";
			}
		}
		
		req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		req.send(PostContents);
		
		if ( !maintainPage ) {
			window.location = window.location;
		}
	}
	
	return false;
}


function getURI(url)
{
	url = url ? url.toString() : document.location.toString();
	urlPortions = url.split('?');
	
	return urlPortions[0];
}


function stripDomain(url)
{
	url = url ? url.toString() : document.location.toString();
	return url.substring(url.indexOf('/') + 1);
}


function getQueryVariable(variable, url) 
{
	if ( !url ) url = window.location.search;
	url = url.substring(url.indexOf('?') + 1);
	
	var vars = url.split("&");
	
	for ( var i = 0; i < vars.length; i++ ) {
		var pair = vars[i].split("=");
		if (pair[0] == variable) {
			return pair[1];
		}
	} 
	
	return null;
}


function setQueryVariable(variable, value, url)
{
	//if ( !url ) url = window.location.search;
	if ( !url ) url = window.location.toString();
	
	coreUrl = url.substring(0, url.indexOf('?'));
	queryString = url.substring(url.indexOf('?') + 1);
	
	if ( queryString ) {
		
		// this expression needs to be improved
		// $QueryString = '&' . preg_replace("/(?<=&|^)$a_VarName=([^&?]+)?&?/", '', $QueryString);
		regex = new RegExp(variable + "([^&?]+)?&?");
		queryString = "&" + queryString.replace(regex, '');
	}
	
	return coreUrl + "?" + variable + "=" + value + queryString;
}


function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}


function toggleDisplay(elementId, giveFocusToId)
{
	if ( elementId ) {
		
		var element = document.getElementById(elementId);
		
		if ( element ) {
			if ( element.style.display == 'none' ) {
				element.style.display = '';
				if ( giveFocusToId ) {
					document.getElementById(giveFocusToId).focus();
				}
			}
			else {
				element.style.display = 'none';
			}
		}
	}
	
	return false;
}


function pingSession(repeatInterval)
{
	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
	} 
	else if (window.ActiveXObject) {
		req = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else {
		return;	
	}
				
	req.open("POST", '/common/ping_session.php', false);
			
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	req.send('ping');
	
	if ( repeatInterval > 0 ) {
		 self.setTimeout('pingSession(' + repeatInterval + ')', repeatInterval * 1000);
	}
}


// Courtesy of http://www.dustindiaz.com/getelementsbyclass/
function getElementsByClass(searchClass, node, tag) 
{
	var classElements = new Array();
	
	if ( node == null ) {
		node = document;
	}
	
	if ( tag == null ) {
		tag = '*';
	}
	
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	
	return classElements;
}

// Courtesy of http://techpatterns.com/downloads/javascript_cookies.php
function setCookie(name, value, expires, path, domain, secure ) 
{
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );

	if ( !expires ) expires = 30;
	expires = expires * 1000 * 60 * 60 * 24;
		
	var expiresDate = new Date( today.getTime() + (expires) );
	
	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expiresDate.toGMTString() : "" ) + 
	( ( path ) ? ";path=" + path : "" ) + 
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
}

// Courtesy of http://techpatterns.com/downloads/javascript_cookies.php
function getCookie(name)
{	
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
		return null;
	}
	
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ";", len );
	if ( end == -1 ) end = document.cookie.length;
	
	return unescape( document.cookie.substring( len, end ) );
}

function clearValue(element, onlyIfEquals) 
{
	if ( element && element.value ) {

		if ( onlyIfEquals ) { 
			var matchExpression = '/' + onlyIfEquals + '/';
			
			if ( !element.value.match(onlyIfEquals) ) {
				return;
			}
		}
		
		element.value = '';	
	}	
}

// Courtesy of http://www.quirksmode.org/js/findpos.html
function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

function evalElement(element)
{
	element.value = Math.round(eval(element.value) * 100) / 100;
}

function cancelForm()
{
	var refererElem = document.getElementById('Referer');
	
	if ( refererElem ) {
		window.location = refererElem.value;
	}
	else if ( document.referrer ) {
		window.location = document.referrer;
	}
	else {
		history.go(-1);	
	}
}

function setValueIfEmpty(element, value)
{
	if (element.value.length == 0) {
		element.value = value;
	}
}

function prototypeAvailable()
{
	return self.$ ? true : false;
}
