/* LIMIT TEXT AREA
--------------------------------- /*
	limit the word or character count of a specific textarea
	different words/characters are determined by the separator
	comma separation does not allow any spaces anywhere
	
	to limit by characters, use '' as the separator, and 
	count as the number of characters.
/* --------------------------------- */
function limit_textarea(form,area,count,separator)
{
	var textarea 	= eval('document.'+form+'.'+area);
	var text_value 	= textarea.value;
	var word_list	= text_value.split(separator);
	var word_count	= word_list.length;
	
	if(text_value != '')
	{
		// if the separator is a comma, the textarea's value should be a comma separated list
		if(separator == ',')
		{
			// if a space is found in the text...
			if(text_value.indexOf(' ') != -1)
			{
				// alert the user that spaces are not allowed
				alert('No spaces allowed, comma separated list');
				
				// remove everything upto and including the space
				textarea.value = textarea.value.substring(0,textarea.value.lastIndexOf(' '));
			}
		}
		if(word_count > count)
		{
			// alert that the limit has been reached/passed
			alert('You\'ve Reached Your Limit');
			
			// remove everything up to the last separator
			if(separator == '') textarea.value = textarea.value.substring(0,count);
			else textarea.value = textarea.value.substring(0,textarea.value.lastIndexOf(separator));
		}
	}
}





/* CREATE PROTECTED EMAIL
------------------------------------ /*
	- Takes the email address and creates a protected mailto link.
	- Prevents bots from harvesting email addresses.
	- Addresses must contain an '&' instead of the '@' symbol
	- Requires replacement text
	
/* ------------------------------------ */
function protect_email(addr,text)
{
	addr_top = addr.substr(0,addr.indexOf(':'));
	addr_btm = addr.substr(addr.indexOf(':')+1,addr.length);
	
	addr = "mai" + "lto" + ":" + addr_top + "@" + addr_btm;
	document.write('<a href="javascript:;" onMouseOver="javscript:this.href=\''+addr+'\';">'+text+'</a>');
}






/* CHECK/REMOVE HTTP://
------------------------------------ /*
	Checks the value for http:// and 
	removes it if found.
------------------------------------ */
function check_http(id,value)
{
	var return_value = value;
	if(value.indexOf('http://') != -1) return_value = value.substring(7,value.length);
	document.getElementById(id).value = return_value;
}






/* SELECT ALL INBOX MESSAGES
------------------------------------ /*
	Selects all inbox messages for removal
------------------------------------ */
function select_all(form,checked)
{
	var form 		= document.forms[form];
	var id_list 	= new Array();
		
	for(var i=0;i<form.length;i++)
	{
		var element = form[i];
		var type	= form[i].type;
		var name	= form[i].name;
		var id		= name.substring(4);
		
		if(checked && type == 'checkbox')
		{
			element.checked = true;
			if(id) id_list.push(id);
		}
		else if(!checked && type == 'checkbox')
		{
			element.checked = false;
			id_list = new Array();
		}
	}
	document.getElementById('rem_id_list').value = id_list.toString();
}






/* SELECT ONE INBOX MESSAGE
------------------------------------ /*
	Selects one inbox messages for removal
------------------------------------ */
function select_one(form)
{	
	var form 		= document.forms[form];
	var id_list 	= new Array();
	
	for(var i=0;i<form.length;i++)
	{
		var element = form[i];
		var type	= form[i].type;
		var name	= form[i].name;
		var checked = form[i].checked;
		var id		= name.substring(4);
				
		if(checked && type == 'checkbox')
		{
			if(id) id_list.push(id);
		}
	}
	document.getElementById('rem_id_list').value = id_list.toString();
}






/* CHANGES FORM ACTION
------------------------------------ /*
	this is used with image fields to initiate the cropping forms
	it is also used within the cropping forms, if there are multiple images needing to be cropped
	
	append the new action to url
/* ------------------------------------ */
function change_action(value,url,form_name)
{
	var form 			= $(form_name);
	var count 			= form.length;
	var new_action 		= url+'crop/';
		
	if(value != '') $(form_name).action = new_action;
	else $(form_name).action = url;
}





/* FORMAT NUMBER
------------------------------------ /*
	copyright Stephen Chapman 24th March 2006, 10th February 2007
	permission to use this function is granted provided
	that this copyright notice is retained intact
/* ------------------------------------ */
function formatNumber(number)
{
	var i = parseFloat(number);
	if(isNaN(i)) { i = 0.00; }
	var neg = '';
	if(i < 0) { neg = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = neg + s;
	return s;
}



/* ARRAY SUM
------------------------------------ /*
	adds all the values of a numeric array together
/* ------------------------------------ */
function sum (o){
    for(var s = 0, i = o.length; i; s += o[--i]);
    return s;
}