
/**
 * To enable a save action on the switching of 'tabs' on the edit profile page.
 */
function editprofile_tab ( object )
{

	$( 'form_redirect' ).setProperty( 'value', $( object ).getProperty( 'href' ) );
	document.forms['form_editprofile'].submit();
	return false;

};

function age_range ()
{

	var lower = document.getElementById( 'form_olderthan' );
	var upper = document.getElementById( 'form_youngerthan' );

	if ( eval( lower.value ) > eval( upper.value ) ) upper.value = lower.value;

};



/**
 * For search form, clicking on any will unselect all other checkboxes in the checkset.
 */
function form_any ( obj )
{

	var mode;

	/**
	 * Get obj value, if nothing ('any') then in untick all children
	 * mode, if something then ensure the 'any' input is unchecked.
	 */
	if ( obj.value == '' ) {
		mode = 'untick_all';
	} else {
		mode = 'untick_any';
	}

	var group = $(obj).getParent().getParent();

	/**
	 * Foreach child of parent node if that child is an input
	 * make it checked or unchecked based on what was clicked.
	 */
	for( var i = 0; i < group.childNodes.length; i++ )
		if ( group.childNodes[i].className == 'form_checkset_option' )
			if ( group.childNodes[i].firstChild.value == '' ) {

				if ( mode == 'untick_any' ) {
					group.childNodes[i].firstChild.checked = null;
				}
			} else {
				if ( mode == 'untick_all' ) {
					group.childNodes[i].firstChild.checked = null;
				}
			}

};

function insertAtCursor( myField, myValue ) {

	myField = document.getElementById( myField );

	//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;
	}

};



/**
 * Refreshes the 'alerts area' once per minute.
 */
window.addEvent( 'domready', function () {

	/**
	 * Init the ajax and make the initial request to populate the area.
	 */
	var alerts = new Ajax( '/alert', { method: 'get', update: $( "alerts_area" ) } ).request();

	/**
	 * Set it to refresh once per minute.
	 */
	alerts.request.periodical( 60000, alerts );

} );


/**
 * Sends an ajax request to remove a notification and refresh the list.
 */
function remove_notification( id )
{

	var alerts = new Ajax( '/alert/remove/' + id, { method: 'get', update: $( "alerts_area" ) } ).request();

	return false;

};

/* Not currently implemented.
function clear_alerts_area ()
{

	// AJAX req to clear the alerts area

}
*/



function new_window ( url, opt )
{

	window.open( url, '', opt );
	return false;

};



/**
 * Function for pagination navigation in messaging,
 * smiles, search results, etc.
 */
function jumppage ( object, path )
{

	var offset = object.value;

	/**
	 * Replce %offset% in path with offset.
	 */
	window.location = path.replace( /%offset%/, offset );

};






/**
 * Clears all checkboxes with the input_checkbox class.
 */
function clear_checkboxes ()
{

	$$( '.input_checkbox' ).each( function ( checkbox ) {
		checkbox.removeProperty( 'checked' );
	} );

};


function select_checkboxes ( selector )
{

	if ( $(selector).getProperty('checked') ) {
		$$( '.input_checkbox' ).each( function ( checkbox ) {
			if ( !checkbox.hasClass( 'selectall' ) )
				checkbox.setProperty( 'checked', 'checked' );
		} );
	} else {
		$$( '.input_checkbox' ).each( function ( checkbox ) {
			if ( !checkbox.hasClass( 'selectall' ) )
				checkbox.removeProperty( 'checked' );
		} );
	}

	return true;

};

function checkboxes ( theclass )
{

	$$( '.input_checkbox' ).each( function ( checkbox ) {
		if ( $( checkbox ).hasClass( theclass ) ) {
			if ( !checkbox.hasClass( 'selectall' ) )
				checkbox.setProperty( 'checked', 'checked' );
		} else {
			checkbox.removeProperty( 'checked' );
		}
	} );

};

function submit_to ( theform, uri )
{

	$( theform ).setProperty( 'action', uri );
	$( theform ).submit();

};

function group_message ( theform )
{

	$( theform ).setProperty( 'action', '/messaging/new' );
	$( theform ).submit();
	
};
