/********************************************************************
	Sniffing the browser type based on code from CodeHouse.
	You can obtain this script at http://www.codehouse.com
********************************************************************/
function CJL_BrowserSniffer()
{
	var ua = navigator.userAgent;

	this.isOpera = function()
	{
		return /Opera/.test(ua);
	}

	this.isSafari = function()
	{
		return /Safari/.test(ua);
	}

	this.isGecko = function()
	{
		return navigator.product == "Gecko" && ! ( this.isOpera() || this.isSafari() );
	}

	this.isIEWin = function()
	{
		return window.external && /Win/.test(ua);
	}

	this.isIEMac = function()
	{
		return window.external && /Mac/.test(ua);
	}

	this.getVersion = function()
	{
		if( this.isIEWin() || this.isIEMac() )
		{
			return Number(ua.match(/MSIE ([0-9.]+)/)[1]);
		}
		else if ( this.isSafari() )
		{
			return Number(ua.match(/[0-9.]+$/));
		}
		else if ( this.isGecko() )
		{
			var n = ua.match(/rv:([0-9.]+)/)[1];

			var ar = n.split(".");

			var s = ar[0] + ".";

			for(var i = 1; i < ar.length; ++i)
			{
				s += ("0" + ar[i]).match(/.{2}$/)[0];
			}

			return Number(s);
		}
		else if ( this.isOpera() )
		{
			return Number(ua.match(/Opera ([0-9.]+)/)[1]);
		}
		else
		{
			return null;
		}
	}
}

sniffer = new CJL_BrowserSniffer();

if ( sniffer.isIEWin() )
{
	browserType = "iewin";
}
else if ( sniffer.isIEMac() )
{
	browserType = "iemac";
}
else if ( sniffer.isSafari() )
{
	browserType = "safari"
}
else if ( sniffer.isGecko() )
{
	browserType = "gecko";
}
else if ( sniffer.isOpera() )
{
	browserType = "opera";
}
else
{
	browserType = "unknown";
}

// alert( "Your browser type is \"" + browserType + "\"" );

/********************************************************************
	Allows us to choose an alternet style sheet. A call on load will
	be made to getPreferredStyleSheet to deal with a improper
	implementation made in MSIE.
********************************************************************/
function setActiveStyleSheet(title)
{
	var i, a, main;
	for (i=0; (a = document.getElementsByTagName("link")[i]); i++)
	{
		if (a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title"))
		{
			a.disabled = true;
			if(a.getAttribute("title") == title)
			{
				a.disabled = false;
//				 alert( "You are now using an alternate style sheet with the name \"" + title + "\"" );
			}
		}
	}
}

function getPreferredStyleSheet()
{
	var i, a;
	for(i=0; (a = document.getElementsByTagName("link")[i]); i++)
	{
		if((a.getAttribute("rel").indexOf("style") != -1) &&
			(a.getAttribute("rel").indexOf("alt") == -1) &&
			(a.getAttribute("title")))
			return a.getAttribute("title");
	}

	return null;
}


/********************************************************************
	Allows a main check-box to toggle a group of checkboxes prefixed
	with the same name.
********************************************************************/
function checkGroup( mainCheck, subCheck )
{
	e = document.getElementsByName( subCheck );
	for( i=0; i< e.length; i++ )
	{
		if( e[i].type == "checkbox" )
		{
			e[i].checked=mainCheck.checked;
		}
	}
}
      
/********************************************************************
	Toggle display status of a block based on id name.
********************************************************************/
function showHideById( name )
{

	// check for DOM1 compatibility
	if( document.getElementById )
	{

		// loop thru all the elements
		for( var i = 0; document.getElementById( name + i ) != null; i++ )
		{
			var element = document.getElementById( name + i );
	
			if( element.style.display != 'none' )
			{
				element.style.display = 'none';
			}
			else
			{
				element.style.display = '';
			}
		}
	}

}


/********************************************************************
	hide a block based on id name.
********************************************************************/
function hideById( name )
{

	// check for DOM1 compatibility
	if( document.getElementById )
	{

		// loop thru all the elements
		for( var i = 0; document.getElementById( name + i ) != null; i++ )
		{
			var element = document.getElementById( name + i );
	
			if( element.style.display != 'none' )
			{
				element.style.display = 'none';
			}
		}
	}

}

/********************************************************************
	Disable a form input field.
********************************************************************/
function disableInput(form, name) {
	form[name].disabled = true;
	return true;
}

/********************************************************************
	To jump to a new page based on a selection from an <option> tag,
	pass something like
	
	jumpTo( 'http://www.re-access.com/search', 'company', this )
	
	where 'this' is an actual 'this', which will pull a value from the
	'value' in the <option> tag. The browser should then point point to
	
	http://www.re-access.com/search?company=<value of this>
********************************************************************/
function jumpTo( url, option, argument )
{
	var selected = argument.options[argument.selectedIndex].value;
	if(selected != 0)
	{
		window.location.href = url + "?" + option + "=" + selected;
	}
}

/********************************************************************
	These scripts propertly change pull-down menus that represent a
	date in a form.
********************************************************************/
//
// dateSelect is the publically called function.
//
function dateSelect( formName, property, mode )
{
	var propstring = property.split( "." );
	var table = propstring[0];
	var prop = propstring[1];
	
	// change date menu when a year or month is changed.
	if( mode != 'day' ) {
		calPop( formName, prop );
	}

	setHidden( formName, prop, table );
//	alert((eval(formName + "['" + table + "." + prop + "']")).value)
}

//
// Check for leap year.
//
function LeapYear(year)
{
	if ((year/4)   != Math.floor(year/4))	return false;
	if ((year/100) != Math.floor(year/100)) return true;
	if ((year/400) != Math.floor(year/400)) return false;
	return true;
}

//
// make sure menus have correct number of days for month.
//
function calPop( formName, property )
{
	var yearsel = eval ( formName + "." + property + "year" );
	var monthsel = eval( formName + "." + property + "month" );
	var daysel = eval( formName + "." + property + "day" );
	
	var daysinmonth   = new Array( 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	var daysinmonthLY = new Array( 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	
	var date = new Date( yearsel.options[yearsel.selectedIndex].value,
						 monthsel.options[monthsel.selectedIndex].value,
						 daysel.options[daysel.selectedIndex].value,
						 0, 0, 0, 0);
	var year = date.getFullYear();
	
	if( LeapYear( year ) )
	{
		daysinmonth = daysinmonthLY;
	}
	
	for (var i = 0; i < daysel.length; i++)
	{
		daysel.options[i] = null;
	}
	
	var i = 0;
	for (i = 0; i < daysinmonth[monthsel.selectedIndex]; i++)
	{
		daysel.options[i] = new Option(i+1, i+1, false, false);
	}
	
//	alert("length: " + daysel.options.length);
//	alert("last item is: " + daysel.options[i-1].value);
}

function setHidden( formName, property, table )
{
	var yearsel = eval ( formName + "." + property + "year" );
	var monthsel = eval( formName + "." + property + "month" );
	var daysel = eval( formName + "." + property + "day" );
//	alert("yearsel: " + yearsel.value + ", monthsel: " + monthsel.value + ", daysel: " + daysel.value);
	
	// month array is zero-based, but need months starting at 1.
	var month = Number(monthsel.value) + 1;

	var datefield = eval( formName + "['" + table + "." + property + "']" );
	// need date in "yyyy-mm-dd" format
	var datefieldval = yearsel.value + "-" + month + "-" + daysel.value;

	datefield.value = datefieldval;
//	 alert( datefield.value );
}

/********************************************************************
  Code dedicated to preloading navigation images
********************************************************************/
function newImage(arg)
{
	if (document.images)
	{
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}


/********************************************************************
  U.S. state is selected iff country == US
********************************************************************/
function usstatesel( formName, property, attribute )
{
	if( property == null )
	{
		var state = eval ( formName + ".statecode" );
		var country = eval ( formName + ".countrycode");
	}
	else if ( attribute == null )
	{
		var state = eval ( formName + "['" + property + "' + '.' + 'statecode']" );
		var country = eval ( formName + "['" + property + "' + '.' + 'countrycode']");
	}
	else
	{
		var state = eval ( formName + "['" + property + "' + '.' + '" + attribute + "statecode']" );
		var country = eval ( formName + "['" + property + "' + '.' + '" + attribute + "countrycode']");
	}
	
	if( state.options[state.selectedIndex].value != "" )
	{
		for( var i = 0; i < country.options.length; i++ )
		{
			if( country.options[i].value == "US" )
			{
				country.options[i].selected = true;
				break;
			}
		}
	}
	else {
		country.options[0].selected = true;
	}
}

function nonuscountrysel( formName, property, attribute )
{
	if( property == null )
	{
		var state = eval ( formName + ".statecode" );
		var country = eval ( formName + ".countrycode");
	}
	else if ( attribute == null )
	{
		var state = eval ( formName + "['" + property + "' + '.' + 'statecode']" );
		var country = eval ( formName + "['" + property + "' + '.' + 'countrycode']");
	}
	else
	{
		var state = eval ( formName + "['" + property + "' + '.' + '" + attribute + "statecode']" );
		var country = eval ( formName + "['" + property + "' + '.' + '" + attribute + "countrycode']");
	}

	if( country.options[country.selectedIndex].value != "US" )
	{
		state.options[0].selected = true;
	}
}

//
// Set the browser's title
//
function setTitle(title)
{
	window.document.title = "RenewableEnergyWorld.com | " + title;
}

// clear out a field with a specified regexp
function clearit(expression, field) {
	widget = eval(field);
	if (widget.value.match(expression) != null)
	{
		return widget.value = "";
	}
}

function stripHtml(contentString) {
	var newContentString = contentString.replace(/(<([^>]+)>)/ig,"");
	return newContentString;
}

/**
 * Used to set a pages focus field, using id="focusfield" in a tag def.
 * Eg. <input type="text" id="focusfield">
 */
function focusField() {
	if(document.getElementById("focusField"))
	{
    	document.getElementById("focusField").focus();
    }
}

// Removes leading whitespaces
function LTrim( value ) {
	
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
	
}

// Removes ending whitespaces
function RTrim( value ) {
	
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
	
}

// Removes leading and ending whitespaces
function trim( value ) {
	return LTrim(RTrim(value));	
}	


/*
 * Replaces certain characters with certain other characters.
 *
 * Based on filterChars.js, which was originally 
 * written by: David Lindquist (dave@gazingus.org)
 * Graciously brought to our attention by timdude@timdude.com
 * Modified for our use by matthew@re-access.com
 *
 */
String.prototype.filterChars = function()
{
	var formatted = trim(this);
	
	// all values following the \u are HEX Unicode values of special Unicode characters
	var BULLET              = new RegExp( '(\u2022)', 'g' );
	var COPY                = new RegExp( '(\u00A9)', 'g' );
	var DEGREES             = new RegExp( '(\u00B0|\u00BA)', 'g' );
	var ELLIPSIS            = new RegExp( '(\u2026)', 'g' );
	var EURO                = new RegExp( '(\u20AC|&#x20AC;)', 'g' );
	var M_DASH              = new RegExp( '(\u2014)', 'g' );
	var N_DASH              = new RegExp( '(\u2013)', 'g' );
	var PLUSMN              = new RegExp( '(\u00B1)', 'g' );
	var POUND               = new RegExp( '(\u00A3)', 'g' );
	var REG                 = new RegExp( '(\u00AE)', 'g' );
	var SMART_QUOTE         = new RegExp( '(\u201C|\u201D|&ldquo;|&rdquo;)', 'g' );
	var SMART_APOSTROPHE    = new RegExp( '(\u2018|\u2019|&lsquo;|&rsquo;)', 'g' );
	var SPACE               = new RegExp( '(\u00A0)', 'g' );
	var TRADE               = new RegExp( '(\u2122)', 'g' );
	var YEN                 = new RegExp( '(\u00A5)', 'g' );
	var SZLIG               = new RegExp( '(\u00DF)', 'g' );
	var MU                  = new RegExp( '(\u00B5)', 'g' );
	var OHM                 = new RegExp( '(\u03A9)', 'g' );
	
	var A_ACCENT         = new RegExp( '(\u00C0|\u00C1|\u00C2|\u00C3|\u00C4)', 'g' );
	var a_ACCENT         = new RegExp( '(\u00E0|\u00E1|\u00E2|\u00E3|\u00E4)', 'g' );
	var A_RING           = new RegExp( '(\u00C5)', 'g' );
	var a_RING           = new RegExp( '(\u00E5)', 'g' );
	var AE               = new RegExp( '(\u00C6)', 'g' );
	var aE               = new RegExp( '(\u00E6)', 'g' );

	var E_ACCENT         = new RegExp( '(\u00C8|\u00C9|\u00CA|\u00CB)', 'g' );
	var e_ACCENT         = new RegExp( '(\u00E8|\u00E9|\u00EA|\u00EB)', 'g' );

	var I_ACCENT         = new RegExp( '(\u00CC|\u00CD|\u00CE|\u00CF)', 'g' );
	var i_ACCENT         = new RegExp( '(\u00EC|\u00ED|\u00EE|\u00EF)', 'g' );

	var O_ACCENT         = new RegExp( '(\u00D2|\u00D3|\u00D4)', 'g' );
	var o_ACCENT         = new RegExp( '(\u00F2|\u00F3|\u00F4)', 'g' );
	var OE               = new RegExp( '(\u00D6|\u00D8)', 'g' );
	var oE               = new RegExp( '(\u00F6|\u00F8)', 'g' );

	var U_ACCENT         = new RegExp( '(\u00D9|\u00DA|\u00DB)', 'g' );
	var u_ACCENT         = new RegExp( '(\u00F9|\u00FA|\u00FB)', 'g' );
	var UE               = new RegExp( '(\u00DC)', 'g' );
	var uE               = new RegExp( '(\u00FC)', 'g' );
	
	var N_TILDE          = new RegExp( '\u00D1', 'g' );
	var n_TILDE          = new RegExp( '\u00F1', 'g' );
	var C_CEDIL          = new RegExp( '\u00C7', 'g' );
	var c_CEDIL          = new RegExp( '\u00E7', 'g' );

	// Make the replacements
	formatted = formatted.replace( BULLET,            '&bull;' );
	formatted = formatted.replace( COPY,              '&copy;' );
	formatted = formatted.replace( DEGREES,           '&deg;' );
	formatted = formatted.replace( ELLIPSIS,          '&hellip;' );
	formatted = formatted.replace( EURO,              '&euro;' );
	formatted = formatted.replace( M_DASH,            '&mdash;' );
	formatted = formatted.replace( N_DASH,            '&ndash;' );
	formatted = formatted.replace( PLUSMN,            '&plusmn;' );
	formatted = formatted.replace( POUND,             '&pound;' );
	formatted = formatted.replace( REG,               '&reg;' );
	formatted = formatted.replace( SMART_QUOTE,       '"' );
	formatted = formatted.replace( SMART_APOSTROPHE,  '\'' );
	formatted = formatted.replace( SPACE,             '&nbsp;' );
	formatted = formatted.replace( TRADE,             '&trade;' );
	formatted = formatted.replace( YEN,               '&yen;' );
	formatted = formatted.replace( SZLIG,             '&szlig;' );
	formatted = formatted.replace( MU,                '&mu;' );
	formatted = formatted.replace( OHM,               '&Omega;' );

	formatted = formatted.replace( A_ACCENT,          'A' );
	formatted = formatted.replace( a_ACCENT,          'a' );
	formatted = formatted.replace( A_RING,            'Aa' );
	formatted = formatted.replace( a_RING,            'aa' );
	formatted = formatted.replace( AE,                'Ae' );
	formatted = formatted.replace( aE,                'ae' );

	formatted = formatted.replace( E_ACCENT,          'E' );
	formatted = formatted.replace( e_ACCENT,          'e' );

	formatted = formatted.replace( I_ACCENT,          'I' );
	formatted = formatted.replace( i_ACCENT,          'i' );

	formatted = formatted.replace( O_ACCENT,          'O' );
	formatted = formatted.replace( o_ACCENT,          'o' );
	formatted = formatted.replace( OE,                'Oe' );
	formatted = formatted.replace( oE,                'oe' );

	formatted = formatted.replace( U_ACCENT,          'U' );
	formatted = formatted.replace( u_ACCENT,          'u' );
	formatted = formatted.replace( UE,                'Ue' );
	formatted = formatted.replace( uE,                'ue' );

	formatted = formatted.replace( N_TILDE,           'N' );
	formatted = formatted.replace( n_TILDE,           'n' );
	formatted = formatted.replace( C_CEDIL,           'C' );
	formatted = formatted.replace( c_CEDIL,           'c' );

	return formatted;
}


function getElement(resource)
{
	var element;
	
	if(typeof(resourceId) == "object")
		element = resource;
	else
	{
		if(document.getElementById(resource) != null)
			element = document.getElementById(resource);
	}	

	return element;
}


function toggleDisplay(resource)
{
	var element = getElement(resource);
	
	if(element.style.display == "none")
		element.style.display = "block";
	else
		element.style.display = "none";
}

function searchSubmit(tabElement)
{
	var hiddenField = getElement("hiddenField");
	hiddenField.value = tabElement.id;
	
	var searchForm = getElement("searchForm");
	var searchField = getElement("searchField");
	
	hiddenField.value = tabElement.id;
	
	if(searchField.value != "")
	{
		searchForm.submit();
	}
	else
	{
		setSearchTabs(tabElement.id);
	}
	
	return false;
}

function setSearchTabs(tabResource)
{
	var tabElement = getElement(tabResource);
	var tabs = getElement("searchTabs").getElementsByTagName("a");
	var numTabs = tabs.length;
	var curTab;
	
	if(numTabs > 0)
	{
		for(var i = 0; i < numTabs; i++)
		{
			curTab = tabs[i];
			
			if(curTab.id == tabElement.id)
			{
				curTab.style.textDecoration = "underline";
				curTab.style.fontWeight = "bold";
				curTab.style.color = "#f26015";
			}
			else
			{
				curTab.style.textDecoration = "none";
				curTab.style.fontWeight = "normal";
				curTab.style.color = "#0143a7";			
			}
		}	
	}
}



function homeNewsBlock(tabNumber, tabName, tabBody)
{
	for(i = 1; document.getElementById(tabName + i) != null; i++)
	{
		var tTab = document.getElementById(tabName + i);
		var tBody = document.getElementById(tabBody + i);
	
		if(tabNumber == i)
		{
			/* Set tab body to be displayed */
			tBody.style.display = "block";
			
			/* Set tab styles */
			tTab.style.border = "1px solid #cdcdcd";
			tTab.style.backgroundColor = "#dcdcdc";

		}
		else
		{
			tBody.style.display = "none";
			
			/* Set tab styles */
			tTab.style.border = "1px solid #f3f3f3";
			tTab.style.backgroundColor = "transparent";
		}
	}
}

function setStoryImageWidth(image, imageId, maxWidth)
{
	var img = new Image();
	img.src  = image;
	
	var imgWidth = img.width;

	var imgElement = document.getElementById(imageId);	

	if(imgWidth > maxWidth)
	 	imgElement.width = maxWidth;
	else
		imgElement.width = imgWidth;
 
	imgElement.style.display = "block";
}

function newsImagePop(contentId, contentTypeId)
{
	var url = "/rea/imagegallery?cid=" + contentId + "&ctid=" + contentTypeId;

	var newwindow=window.open(url, 'name', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=650,height=600,left = 200,top = 100');
	newwindow.focus();
}

function podcastListenPop(podcastId) {
	var url = "/rea/news/podcast/listen?id=" + podcastId;
	
	var newwindow=window.open(url, 'name', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=770,height=420,left = 200,top = 100');
	newwindow.focus();	
}

function storyToolPop(url)
{
	//alert(url);
	var newwindow=window.open(url,'name','height=450,width=500');
	newwindow.focus();
}

function showLayer() {
	if(browserType == "iewin") {
		var selectElements = document.getElementsByTagName("select");
		
		for (var x = 0; x < selectElements.length; x++) {
			selectElements[x].style.visibility = "hidden";
		}
	}
}

// Son of Suckerfish deals with :hover in IE
sfHover = function() {
	var sfEls = document.getElementById("headerNav").getElementsByTagName("li");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);

function hideLayer() {
	if(browserType == "iewin") {
		var selectElements = document.getElementsByTagName("select");
		
		for (var x = 0; x < selectElements.length; x++) {
			selectElements[x].style.visibility = "visible";
		}
	}
}

function generateTaLink(taId, taName)
{
	if(taId != -1)
	{
		for(var i = 1; i <= 4; i++)
		{
			var curLink = document.getElementById("urlLink" + i);
			var curLinkText = document.getElementById("urlText" + i);
			
			if(curLink.value == "" && curLinkText.value == "")
			{
				curLink.value = "http://www.renewableenergyworld.com/rea/partner?cid=" + taId;
				curLinkText.value = taName;
				break;
			}
		}
	}
}

function generateStoryAlert(storyType)
{
	if(storyType == 6 || storyType == 14)
	{
		var storyLink = document.getElementById("urlLink1");
		var storyIntro = document.getElementById("storyIntro");
		var storyBody = document.getElementById("storyBody");
		
		if(storyLink.value != "")
		{
			storyIntro.value = storyLink.value;
			alert("Intro link has been created");
		}
		else
		{
			storyIntro.value = "*** Add story link here ***";
			alert("Add alert link to intro");
		}
		
		storyBody.value = "<!-- leave this here -->";
	}
}

function registerDrop()
{	
	var arrowImg = new Image();
	arrowImg.src = "/images/template/orange-block-arrow.gif";
	
	var opaqueLayer = document.createElement('div');
	opaqueLayer.className = 'opaqueLayer';
	opaqueLayer.style.height = (screen.height + document.body.scrollHeight) + 'px';
	opaqueLayer.style.opacity = '.7';                      
	opaqueLayer.style.MozOpacity = '.7';                   
	opaqueLayer.style.filter = 'alpha(opacity=70)';
	opaqueLayer.id = "theOpaqueLayer";	

	document.body.appendChild(opaqueLayer);
	
	var popUp = document.getElementById('registerDrop');
	popUp.style.display = "block";
}


function createRegisterCookie()
{
	if(readCookie('showRegisterDrop') == null)
		createCookie('showRegisterDrop', "yes", 365);
}

function closeRegisterDrop()
{
	var drop = document.getElementById('registerDrop');
	var opaque = document.getElementById('theOpaqueLayer');
	
	drop.style.display = "none";
	document.body.removeChild(opaque);	
}


function processCountryChange(countrySelect)
{
	var stateBlock = document.getElementById('stateBlock');
	var regionBlock = document.getElementById('regionBlock');
	
	if(countrySelect.value == "US")
	{
		stateBlock.style.display = "block";
		regionBlock.style.display = "none";
	}
	else
	{
		stateBlock.style.display = "none";
		regionBlock.style.display = "block";	
	}
}


/*******************************************************************************
*	Creates a cookie
*******************************************************************************/
function createCookie(name, value, days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

/*******************************************************************************
*	Reads a cookie
*******************************************************************************/
function readCookie(name)
{
	var ca = document.cookie.split(';');
	var nameEQ = name + "=";
	
	for(var i = 0; i < ca.length; i++)
		{
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1, c.length); //delete spaces
				if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
		}
		
	return null;
}

/*******************************************************************************
*	Removes a cookie
*******************************************************************************/
function eraseCookie(name)
{
  createCookie(name, "", -1);
}

function easyCheckbox(checkBoxId)
{
	var checkBox = document.getElementById(checkBoxId);
	
	if(checkBox.checked == true)
		checkBox.checked = false;
	else
		checkBox.checked = true;
}

function adminFakeLinkPop(link, offset, jSessionId)
{
	var columnBase = link.parentNode;

	var popContainer = document.createElement('div');
	popContainer.className = 'adminFakeLinkPop';
	popContainer.style.marginTop = offset + "px";
	
	var popBody = document.createElement('div');
	popBody.className = "adminFakeLinkBody";

	var closeRow = document.createElement('p');
	closeRow.className = "closeFakeLinkPop";
	closeRow.onclick = function()
	{
		this.parentNode.parentNode.parentNode.removeChild(this.parentNode.parentNode);
	};
	
	closeRow.appendChild(document.createTextNode('X Close'));
	
	popBody.appendChild(closeRow);	
		
	var mainParagraph = document.createElement('p');
	mainParagraph.style.margin = "0";
	mainParagraph.style.textAlign = "center";
	
	mainParagraph.appendChild(document.createTextNode('To use this functionality you must upgrade your account.'));
	
	popBody.appendChild(mainParagraph);
	
	var upgradeLink = document.createElement('a');
	upgradeLink.href = '/rea/admin/upgradeaccount;jsessionid=' + jSessionId + "?accountSelectType=upgrade";
	upgradeLink.appendChild(document.createTextNode('Click Here to Upgrade!'));
	upgradeLink.className = 'popUpgradeLink';
	
	popBody.appendChild(upgradeLink);
	popContainer.appendChild(popBody);
	
	columnBase.appendChild(popContainer);
	
	return false;
}


var uploadImageCount = 0;

function addImageBlock(containerId)
{
	var container = document.getElementById(containerId);
	uploadImageCount++;

	// builds the input file block
	var mainFileDiv = document.createElement('div');
	var fileField = document.createElement('input');
	fileField.type = "file";
	fileField.name = "imageFile" + uploadImageCount.toString();
	fileField.size = "35";
	
	var textFileDiv = document.createElement('div');
	textFileDiv.appendChild(document.createTextNode("Image File"));
	
	mainFileDiv.appendChild(textFileDiv);
	mainFileDiv.appendChild(fileField);

	// builds the input text image credit block
	var mainCreditDiv = document.createElement('div');
	var creditField = document.createElement('input');
	creditField.type = "text";
	creditField.name = "imageCredit" + uploadImageCount.toString();	
	
	var textCreditDiv = document.createElement('div');
	textCreditDiv.appendChild(document.createTextNode("Image Credit"));
	
	mainCreditDiv.appendChild(textCreditDiv);
	mainCreditDiv.appendChild(creditField);
	
	
	// build the input text image caption block
	var mainCaptionDiv = document.createElement('div');
	var captionField = document.createElement('textarea');
	captionField.style.height = "50px";
	captionField.name = "imageCaption" + uploadImageCount.toString();
	
	var textCaptionDiv = document.createElement('div');
	textCaptionDiv.appendChild(document.createTextNode("Image Caption"));

	mainCaptionDiv.appendChild(textCaptionDiv);
	mainCaptionDiv.appendChild(captionField);
	
	// append the above form fields to the main div container in jsp
	container.appendChild(mainFileDiv);
	container.appendChild(mainCreditDiv);
	container.appendChild(mainCaptionDiv);
}

function getElementObj(elementResource)
{
	var elementObj = null;
	
	if(typeof(elementResource) == 'object')
		elementObj = elementResource;
	else
	{
		if(document.getElementById(elementResource) != null)
			elementObj = document.getElementById(elementResource);
	}
	
	return elementObj;
}

function toggleAdminBox(elementId, hasCompany)
{	
	var companyIsActive = parseInt(hasCompany);
	
	if(document.getElementById(elementId) == null)
	{
		if(companyIsActive > 0)
			elementId = 'navSec2';
		else
			elementId = 'navSec3';
	}
		
	var curBox;
	
	if(document.getElementById(elementId) != null)
	{
		for(var i = 1; i < 5; i++)
		{
			if(curBox = document.getElementById('navSec' + i) != null)
			{
				 curBox = document.getElementById('navSec' + i);
				 
				if(curBox.id != elementId)
					new Effect.BlindUp(curBox);
				else
					new Effect.BlindDown(curBox);
			}
		}
	}
}

function appendActiveNavArrow(navId)
{
	if(document.getElementById(navId) != null)
		var navElement = document.getElementById(navId).style.backgroundColor = '#e2e2e2';
}

function zebraTables(tableId)
{
	var table = document.getElementById(tableId);
	var numRows = table.rows.length;
	
	if(numRows > 0)
	{
		for(var i = 0; i < numRows; i++)
		{
			var row = table.rows[i];
			if(i % 2 == 0)
				row.className = 'tableOddRow';
		}
	}
}

// Counts characters for a given form field
function characterCounter(textResource, maxCharLimit, displayResource)
{
	var textField = getElementObj(textResource);
	var displayContainer = getElementObj(displayResource);
	
	if(textField != null && displayContainer != null)
	{
		
		var numCharacters = textField.value.length;
		var charactersLeft = (maxCharLimit - numCharacters);
		
		//alert('Num characters left' + charactersLeft);
		if(charactersLeft <= 0)
		{
			charactersLeft = 0;
			textField.value = textField.value.substring(0, maxCharLimit);			
		}
		
		if(charactersLeft <= 50 && charactersLeft >= 25)
			displayContainer.parentNode.style.color = "#d1d32f";
		else
		if(charactersLeft < 25)
			displayContainer.parentNode.style.color = "#bd2432";
		else
			displayContainer.parentNode.style.color = "#000";
		
		displayContainer.innerHTML = charactersLeft;		
	}
}

function getHeightOffset()
{
	var scrOfY = 0;
	
	if(typeof(window.pageYOffset) == 'number')
		scrOfY = window.pageYOffset;
	else if(document.body && (document.body.scrollLeft || document.body.scrollTop))
		scrOfY = document.body.scrollTop;
	else if(document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop))
		scrOfY = document.documentElement.scrollTop;
	
	return scrOfY;
}

function createOpaqueLayer()
{
	var opaqueLayer = document.createElement('div');
	opaqueLayer.className = 'opaqueLayer';
	opaqueLayer.style.height = (screen.height + document.body.scrollHeight) + 'px';
	opaqueLayer.style.opacity = '.5';                      
	opaqueLayer.style.MozOpacity = '.5';                   
	opaqueLayer.style.filter = 'alpha(opacity=50)';
	opaqueLayer.id = "theOpaqueLayer";	

	document.body.appendChild(opaqueLayer);
}

function removeOpaqueLayer()
{
	var opaque = document.getElementById('theOpaqueLayer');
	document.body.removeChild(opaque);	
}

function IeSucksUrlDate()
{
	return new Date();
}

function toggleContentSearchItem(container) {
	if(container.className == "contentSearchItem")
		container.className = "contentActiveSearchItem";
	else
		container.className = "contentSearchItem";
}
