String.prototype.trim = function()
{
    return this.replace(/^\s*|\s*$/g,'');
}

String.prototype.ltrim = function()
{
    return this.replace(/^\s*/g,'');
}

String.prototype.rtrim = function()
{
    return this.replace(/\s*$/g,'');
}

Number.prototype.format = function(num)
{
	x = this.toString().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 save(destination)
{
	goto_section = document.getElementById('goto-section');
	
	read_only = document.getElementById('read_only');
	if (read_only && read_only.value == '1')
	{
		// Just allow link to activate
		return true;
	}
	
	// Submit form
	goto_section.value = destination;
	goto_section.form.submit();
	
	return false;
}

function openHelp(slug)
{
	var helpwin = window.open('/grants/application/help/'+slug, 'helpwin', 'width=400,height=400,scrollbars,resizable,toolbar,menubar');
	helpwin.focus();
	return false;
}

function open_dialog(url)
{
	var dialogwin = window.open(url, 'dialogwin', 'width=400,height=400,scrollbars,resizable');
	dialogwin.focus();
	return false;
}

function print_app(id, filename)
{
	printwin = window.open('/grants/application/print_pdf.php?id='+id, 'printwin', 'toolbar=no,menubar=no');
	printwin.focus();
}

function make_integer (field, allow_negative)
{
  var value = field.value;
  var re = /[,$]/g;
  newvalue = parseInt(value.replace(re, ""));

  if (isNaN(newvalue))
  {
    newvalue = 0;
  }
  else if (!allow_negative && newvalue < 0)
  {
  	newvalue = 0;
  }
  field.value = newvalue;
}

function limit_textarea(area, max_chars, force)
{
	counter_id = area.id + '-counter';
	counter = document.getElementById(counter_id);
	chars_per_word = 6;
	
	if (force && area.value.length > max_chars)
	{
		area.value = area.value.substring(0, max_chars);
		alert('You may only enter up to ' + max_chars + ' characters here.  Your text has been truncated to fit within this limit.');
	}
	
	remaining = max_chars - area.value.length;
	new_text = '';
	if (remaining == 1)
	{
		new_text = remaining + ' character left';
	}
	else if (remaining >= 0)
	{
		new_text = remaining + ' characters left';
	}
	else if (remaining == -1)
	{
		new_text = remaining * -1 + ' character over the limit';
	}
	else
	{
		new_text = remaining * -1 + ' characters over the limit';
	}
	
	counter.innerHTML = new_text;
	
	if (remaining < 0)
	{
		counter.className = 'counter error';
	}
	else
	{
		counter.className = 'counter';
	}
}

function toggle_na(check, textarea)
{
	if (check && textarea)
	{
		if (check.checked)
		{
			textarea.value = 'Not applicable';
		}
		else
		{
			textarea.value = '';
		}
	}
}

function init_na(check, textarea)
{
	if (check && textarea)
	{
		if (textarea.value == 'Not applicable')
		{
			check.checked = true;
		}
		else
		{
			check.checked = false;
		}
	}
}

/*
 * Implement a dropdown menu based on a HTML list
 * Author: Dave Lindquist (http://www.gazingus.org)
 */

var currentMenu = null;

if (!document.getElementById)
    document.getElementById = function() { return null; }

function initializeMenu(menuId, actuatorId)
{
	var menu = document.getElementById(menuId);
	var actuator = document.getElementById(actuatorId);

	if (menu == null || actuator == null) return;

	//if (window.opera) return; // I'm too tired

	actuator.onmouseover = function()
	{
		if (currentMenu) 
		{
			currentMenu.style.visibility = "hidden";
			this.showMenu();
		}
	}

	actuator.onclick = function() 
	{
		if (currentMenu == null) 
		{
			this.showMenu();
		}
		else
		{
			currentMenu.style.visibility = "hidden";
			currentMenu = null;
		}

		return false;
	}

	actuator.showMenu = function()
	{
		menu.style.left = this.offsetLeft + "px";
		menu.style.top = this.offsetTop + this.offsetHeight + "px";
		menu.style.visibility = "visible";
		currentMenu = menu;
	}
}

var zoom_win;

function zoom(control, title)
{
	if (typeof control != 'object')
	{
		control = document.getElementById(control);
	}
	
	if (!zoom_win || zoom_win.closed)
	{
		zoom_win = window.open('', 'zoom', 'status=no,location=no,directories=no,menubar=no,toolbar=no,scrollbars=yes,height=700,width=700,resizable=yes');

		zoom_win.document.open();
		zoom_win.document.write('<!DOCTYPE html');	
		zoom_win.document.write('    PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"');	
		zoom_win.document.write('    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">');	
		zoom_win.document.write('<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">');	
		zoom_win.document.write('<head>');	
		zoom_win.document.write('<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">');	
		zoom_win.document.write('<title>'+title+'</title>');
		zoom_win.document.write('<script type="text/javascript">');
		zoom_win.document.write('function save_changes()');
		zoom_win.document.write('{');
		zoom_win.document.write('	window.opener.document.getElementById(\''+control.id+'\').value = document.getElementById(\'text\').value;');
		zoom_win.document.write('	window.close();');
		zoom_win.document.write('}');
		zoom_win.document.write('function initialize()');
		zoom_win.document.write('{');
		zoom_win.document.write('	textarea = document.getElementById(\'text\');');
		zoom_win.document.write('	textarea.style.width  = "100%";');
		zoom_win.document.write('	textarea.style.height = "580px";');
		zoom_win.document.write('	textarea.value = window.opener.document.getElementById(\''+control.id+'\').value;');
		zoom_win.document.write('}');
		zoom_win.document.write('</script>');	
		zoom_win.document.write('</head>');	
		zoom_win.document.write('<body onload="initialize()">');	
		zoom_win.document.write('<form style="margin: 0; padding: 0">');
		zoom_win.document.write('<input type="hidden" name="control" value="'+control.id+'" />');
		zoom_win.document.write('	<div><label for="text">'+title+'</label><br /><textarea id="text" rows="12"></textarea></div>');
		zoom_win.document.write('	<div><input id="save" type="button" onclick="save_changes()" value="Save" /></div>');
		zoom_win.document.write('</form>');
		zoom_win.document.write('</body>');	
		zoom_win.document.write('</html>');
		zoom_win.document.close();
	}
	
	zoom_win.focus();
	
	return false;
}

