function $RF(el,rg){
	if($(el).type&&$(el).type.toLowerCase()=='radio'){
		var rg=$(el).name;
		var el=$(el).form;
		}
	else if($(el).tagName.toLowerCase()!='form'){
		return false;
	}
	var checked=$(el).getInputs('radio',rg).find(function(re){
		return re.checked;
	});
	return(checked)?$F(checked):null;
}

function load_tab( ){
	var the_form = 'paging_form_' + current_tab;
	if( $( the_form )== null ){
		load_table( );
	}
}

function load_table(){
	//var the_form = 'paging_form_' + current_tab;
	var url = 'Admin/load_table.php';
	var target = 'tab_content' + current_tab;
	var the_form = 'paging_form_' + current_tab;
	var field_values =( $( the_form ) == null ) ? '' : $( the_form ).serialize();

	var pars = 'which_table='+ current_tab + '&' + field_values;
	var myAjax = new Ajax.Updater( target, url, {method: 'post', parameters: pars, evalScripts: true});
}

function resort_table( field_name, sort_order ){
	var the_form = 'paging_form_' + current_tab;
	document.forms[the_form].field.value = field_name;
	document.forms[the_form].order.value = sort_order;
	update_table( );
}

function update_table( action ){

	var date = new Date();
		
	date.setTime(date.getTime()+(31*24*60*60*1000));
	var expires = "; expires="+date.toGMTString();
	var sfield = $( 'sfield' ).value;
	var scompare = $( 'scompare' ).value;
	var svalue = $( 'svalue' ).value;

	document.cookie = "sfield="+sfield + expires + "; path=/Power-Calc/";
	document.cookie = "scompare="+scompare + expires + "; path=/Power-Calc/";
	document.cookie = "svalue="+svalue + expires + "; path=/Power-Calc/";

	var the_action = ( action == '' ) ? '' : action;
	var the_form = 'paging_form_' + current_tab;
	var field_values =( $( the_form ) == null ) ? '' : $( the_form ).serialize();

	var target = 'tab_content' + current_tab;
	var url = 'Admin/load_table.php';
	var pars = 'which_table='+ current_tab + '&action='+ action + '&' + field_values;

	var myAjax = new Ajax.Updater( target, url, {method: 'post', parameters: pars, evalScripts: true});
}

function OpenMyWindow(action, db, table, id, reload_table){
	var b_reload_table = ( reload_table === '' ? true : reload_table )
	//var the_form = 'paging_form_' + current_tab;
	var url = 'Admin/editrow.php';
	var target = 'edit_entry';
	var pars = 'action=' + action + '&db=' + db + '&table=' + table + '&id=' + id + '&reload=' + b_reload_table;
	var myAjax = new Ajax.Updater( target, url, {method: 'POST', parameters: pars, evalScripts: true});
	$( target ).show( );
}

function toggle_view( target ){
	$(target).toggle();
}

function closeEdit( ){
	var target ='edit_entry';
	//$( target ).hide( );
	new Effect.Fade( target, { duration: 1.0 } );
	
}

function closeDialog( passed_target ){
	$( passed_target ).hide( );
}

function update_record( reload_table ){
	$( 'edit_entry' ).hide(  )
	var b_reload_table = ( reload_table == undefined ? true : reload_table )
	var result=confirm( 'Are you certain that you want to update this record?\n\nThis action cannot be undone.' );
	if ( result==false )
		return;

	var the_form = 'edit_form';
	var field_values =( $( the_form ) == null ) ? '' : $( the_form ).serialize();

	var url = 'Admin/updaterow.php';
	var pars = field_values + '&b_reload_table='+b_reload_table;
	//alert( pars );
	var myAjax = new Ajax.Updater( 'test', url, {method: 'POST', parameters: pars, evalScripts: true});
//alert( 'huh' );
}

function delete_record( db, table, field, id ){
	var result=confirm( 'Are you certain that you want to delete this record?\n\nThis action cannot be undone.' );
	if ( result==false )
		return;

	var url = 'Admin/editrow.php';
	var target = 'edit_entry';
	var pars = 'action=delete&db=' + db + '&table=' + table + '&field=' + field + '&id=' + id;
	var myAjax = new Ajax.Updater( 'test', url, {method: 'POST', parameters: pars, evalScripts: true});
//	var myAjax = new Ajax.Request( url, {method: 'POST', parameters: pars, evalScripts: true});
	$( 'edit_entry' ).hide( );
	//update_table( );
}

function delete_selected( db, table, field ){
	var result=confirm( 'Are you certain that you want to delete all of the selected records?\n\nThis action cannot be undone.' );
	if ( result==false )
		return;
	var checked_list='';
	$$( '.delete_checkbox' ).each(function(e){
		checked_list += ( e.checked ? e.value + ',' : '');

		});
	if( checked_list=='' ){
		alert( "No items were selected!" );
		return;
	}
	var url = 'Admin/editrow.php';
	var target = 'edit_entry';
	var pars = 'action=delete_selected&db=' + db + '&table=' + table + '&field=' + field + '&ids=' + checked_list;
	var myAjax = new Ajax.Updater( 'test', url, {method: 'POST', parameters: pars, evalScripts: true});
//	var myAjax = new Ajax.Request( url, {method: 'POST', parameters: pars, evalScripts: true});
	$( 'edit_entry' ).hide( );
	//update_table( );
}

function changeSearchField( ){
		var url = 'Admin/changeSearchField.php';
	var target = 'svalue_container';
	var pars = 'table=' + $F( 'table' ) +'&sfield=' + $F( 'sfield' ) +'&svalue=' + $F( 'svalue' );
	var myAjax = new Ajax.Updater( target, url, {method: 'GET', parameters: pars, evalScripts: true});
}

function sendUserNotification( ){
	var notification_template = new Template('#{first_name}\n\nWith this email, we are notifying you that we have created a user name and password for you to access our Power-Calc software (http://www.power-spar.com/Power-Calc/).\n\nYour username is `#{user_name}`\nYour default password is your email address (i.e., `#{email}`).  Please note that the password is case sensitive so it must be entered exactly as shown here.\n\nWe strongly recommend that you change your password the first time that you login.  To do this, log in at http://www.power-spar.com/Power-Calc/ (using the credentials listed above) and then click the `Personal Settings` link in the left-hand Navigation panel.  Your new password will be encrypted before it is stored in the database so we will not know what it is once you\'ve changed it.  If you forget your username and/or password, there are links on the login page that will allow you to retrieve or reset this information.\n\nIf you have any difficulties accessing Power-Calc, or have questions about the contents, please do not hesitate to contact us.  We also welcome your constructive comments regarding the look and feel of the reports and their contents.\n\nAl\n\nAl Caughey\nMenova Energy\n613/599-6232\nal.caughey@power-spar.com');

	$( 'send_notification' ).show(  );
	$( 'notification_from' ).value = 'Power-Calc@power-spar.com';
	$( 'notification_date' ).value = $F( 'last_accessed' );
	$( 'notification_subject' ).value = 'Power-Calc Account Activation for ' + $F( 'name' );
	$( 'notification_to' ).value = $F( 'email' );
	var replacements= new Hash();
	replacements.set('first_name', $F( 'first_name' ) );
	replacements.set('user_name', $F( 'name' ) );
	replacements.set('email', $F( 'email' ) );
	$( 'notification_message' ).value = notification_template.evaluate( replacements );
	//mouseEvents.gsub(' ', ', ');
}

function sendNotification( ){
	var url = 'Admin/sendNotification.php';
	var pars =  ( $('notification_form')== null ) ? 'HUH?' : $('notification_form').serialize();
	var myAjax = new Ajax.Updater('send_notification_link', url, {method: 'post', parameters: pars, evalScripts: true});
}

function refresh_broadcast_to(to_list){

	var url = 'Admin/broadcast_to.php';
	var pars = 'to=' +  to_list;
	var myAjax = new Ajax.Updater('broadcast_list', url, {method: 'post', parameters: pars, evalScripts: true});
}

function send_message(){
	if( validate_field( 'broadcast_list' ) && validate_field( 'broadcast_subject' ) && validate_field( 'broadcast_message' ) ){
		var url = 'Admin/send_message.php';
		var pars = "to=" + $F('broadcast_list') + "&subject=" + $F('broadcast_subject') + "&message=" + $F('broadcast_message');
		var myAjax = new Ajax.Updater('message_sent', url, {method: 'post', parameters: pars, evalScripts: true});
	}
}

function validate_field( fieldname ){
	var str_fieldname = $( fieldname ).value.strip(  );
	if ( str_fieldname == '' ){
		alert( "This field cannot be empty." );
		$( fieldname ).select(  );
		return false;
	}
	else {
		return true
	}

}

function select_all( st ){
	$$( '.delete_checkbox' ).each(function(e){ e.checked = st });
	//$( 'select_none' )[ st ? 'show' : 'hide' ]( );
	//$( 'selected_delete' )[ st ? 'show' : 'hide' ]( );


}

function so_edit( str, x, y ){
	$( 'so_edit_div' ).style.left = Math.min( x-180, 800 );
	$( 'so_edit_div' ).style.top = y-110;
	var url = '/common/get_so_string.php';
	var pars = '';
	pars = ( $( 'saved_id' )==null || $( 'saved_id' ).value=='' ) ? '' : ( 'est_id='+$F( 'saved_id' ) + '&' )
	pars += "str=" + str + "&lang=" + $( 'language' ).value;
	var target = 'test';
	var myAjax = new Ajax.Updater( target, url, {method: 'get', parameters: pars, evalScripts:true} );
}

function so_close( ){
	new Effect.Fade( 'so_edit_div' );
	$( 'so_edit_field' ).clear( );
	$( 'so_edit_field_value' ).clear( );
}

function put_so_string( ){
	var cv = $F( 'so_edit_field_value' );
	var nv = $F( 'so_edit_field' );
	if ( nv != cv ){
		nv = nv.gsub( "&","|" );
		nv = nv.gsub( "%","[[p]]" );
		nv = nv.sub( "#","[[h]]" );
		nv = nv.gsub( /\?/,"[[q]]" );
		cv = cv.gsub( "&","|" );
		cv = cv.gsub( "%","[[p]]" );
		cv = cv.gsub( "#","[[h]]" );
		cv = cv.sub( /\?/,"[[q]]" );
		var url = '/common/put_so_string.php';

		var pars = '';
		pars = ( $RF( 'so_edit_all' )==0 || $( 'saved_id' )==null || $( 'saved_id' ).value=='' ) ? '' : ( 'est_id='+$F( 'saved_id' ) + '&' )
		pars += "str=" + $F( 'so_edit_field_name' ) + "&lang=" + $F( 'language' ) + "&user=" + $F( 'user_id' ) + "&value=" + nv +  "&old=" + cv + "";
		var target = 'so_edit_field_name';
		var myAjax = new Ajax.Updater( target, url, {method: 'POST', parameters: pars, evalScripts:true} );
	}
	new Effect.Fade( 'so_edit_div' );
}