﻿// Generic Functions
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}

/* Character Counter */
function charCounter(id, maxlimit){

	if (!$('counter-'+id)){
		$(id).insert({after: '<div id="counter-'+id+'"></div>'});
	}

	if($F(id).length >= maxlimit)
	{
			$(id).value = $F(id).substring(0, maxlimit);
			$('counter-'+id).addClassName('charcount-limit');
			$('counter-'+id).removeClassName('charcount-safe');
	}
	else
	{	
		$('counter-'+id).removeClassName('charcount-limit');
		$('counter-'+id).addClassName('charcount-safe');
	}

	$('counter-'+id).update( $F(id).length + '/' + maxlimit );	
}

function makeItCount(id, maxsize){
	if ($(id)){
		Event.observe($(id), 'keyup', function(){charCounter(id, maxsize);}, false);
		Event.observe($(id), 'keydown', function(){charCounter(id, maxsize);}, false);
		charCounter(id,maxsize);
	}
}

/* expander used to show/hide sitemap */


// On Load Events
Event.observe(window, 'load', function() {

	$('expander-open').observe('click', function() {
		openExpander()
	});

	$('expander-close').observe('click', function() {
		openExpander()
	});
	
	var docTypes = new Array('doc', 'pdf', 'xls');
	$$('.primary a, .secondary a').each(function(link)
	{
	    for(var idx = 0; idx < docTypes.length; idx++)
	    {	        
		    if (link.href.indexOf('.' + docTypes[idx]) > -1 &&
			    !link.hasClassName('link-' + docTypes[idx]))
		    {
			    link.addClassName('link-' + docTypes[idx]);
		    }
	    }
		if (
		    (link.href.indexOf('http://') > -1 || link.href.indexOf('https://') > -1)  &&
		    link.href.indexOf('http://commerce.') == -1 &&
			!link.hasClassName('link-external') && 
			link.childElements().length == 0)
		{
		    link.addClassName('link-external');		    
		}
	});
	
});

function openExpander()
{
	var expanderOpen = $('wrapper-expander');

	if (!expanderOpen.hasClassName('opened'))
	{
		new Effect.BlindDown('wrapper-expander', {duration:.5});
		expanderOpen.removeClassName('closed');
		expanderOpen.addClassName('opened');
		return false;
	}

	if (!expanderOpen.hasClassName('closed'))
	{
		new Effect.BlindUp('wrapper-expander', {duration:.5});
		expanderOpen.removeClassName('opened');
		expanderOpen.addClassName('closed');
		return false;
	}
}
            
function OpenProperties(link)
{
	myLightWindow.activateWindow({
		href: link.href, 
		title: 'Page Properties', 
		width: 750,
		height: 580
	});
}