function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			if (oldonload) {
				oldonload();
			}
			func();
		}
	}
};

function insertAtCursor( myField, myValue ) {
  //IE support
  if (document.selection) {
    myField.focus();
    sel = document.selection.createRange();
    sel.text = myValue;
  }
  //MOZILLA/NETSCAPE support
  else if (myField.selectionStart || myField.selectionStart == '0') {
    var startPos = myField.selectionStart;
    var endPos = myField.selectionEnd;
    myField.value = myField.value.substring(0, startPos)
                  + myValue 
                  + myField.value.substring(endPos, myField.value.length);
  } else {
    myField.value += myValue;
  }
};

function replace_value( my_field, my_value ) {
	alert('foobar');
  document.getElementById( my_field ).value = 'foo';
};



/**
 * Puts a nicename conversion of the text in element.value into nnt
 *
 * So make lowercase, space to _ and remove all non alphanumeric/_ characters
 */
function nicename ( element, nnt )
{

	if ( document.getElementById( "form_nnt_" + nnt ).checked == true ) {

		//var val = nicename_string( element.value );
		document.getElementById( "form_" + nnt ).value = nicename_string( element.value );

	}

};

/**
 * Converts a string into nicename format.
 */
function nicename_string ( string )
{

	string = string.toLowerCase();
	string = string.replace(/\040/g,"_");
	string = string.replace(/[^\w\g]/g,"");
	return string;

};

/**
 * Toggles the read only status of the nice name field. (NiceNameToggle)
 */
function nnt( id, from )
{

	if ( $( "form_nnt_" + id ).checked == false ) {

		$( "form_" + id ).removeProperty( 'readonly' );

	} else {

		$( "form_" + id ).setProperty( 'value', nicename_string( $( 'form_' + from ).value ) );
		$( "form_" + id ).setProperty( 'readonly', 'readonly' );

	}

};

