function tidy_up_interface(){
// rounding functions - some will not execute unless the object exists
    // round the service navigation buttons
    if ($('service_menus')){
        var div_string=$('service_menus').value;
        if (div_string.length!=0){
            var adiv=div_string.split(",");
            for (var d=0;d<adiv.length;d++){
                var div_name='s_' + adiv[d];
                Rico.Corner.round(div_name);
            }
        }
     }
	// round the service options buttons
    if ($('service_buttons')){
	    var div_string=$('service_buttons').value;
        if (div_string.length!=0){
            var adiv=div_string.split(",");
            for (var d=0;d<adiv.length;d++){
                var div_name='b_' + adiv[d];
                if ($(div_name)){Rico.Corner.round(div_name)}
            }
        }
	}
    if ($('requires_full_page')){
        // bulk operations dialogue requires that we re-configure the page to allow for extra space
        if ($('admin_navigation_area')){
            //alert($('requires_full_page'))
                new Rico.Effect.Position('admin_navigation_area', -1000,null, 100,10)   
                new Rico.Effect.SizeAndPosition( 'admin_workspace', 10,0, 760, 500, 100, 20);
                new Rico.Effect.SizeAndPosition( 'groupdiv', 10,0, 180, 500, 100, 20);
                new Rico.Effect.SizeAndPosition( 'memberdiv', 190,0, 180, 500, 100, 20);
                new Rico.Effect.SizeAndPosition( 'datadiv', 380,0, 400, 500, 100, 20);
                Element.show('requires_full_page');
            
        }
    }
	if ($('home_page_login')){Rico.Corner.round('home_page_login');}
	if ($('round_stats_panel')){Rico.Corner.round('round_stats_panel');}
    //if ($('back_to_parent')){ Rico.Corner.round('back_to_parent');}
    if ($('top_new')){Rico.Corner.round('top_new');}
    if ($('bottom_new')){Rico.Corner.round('bottom_new');}
    if ($('toolbar_div')){Rico.Corner.round('toolbar_div');}
	if ($('a_to_z')){Rico.Corner.round('a_to_z');}
	if ($('editor_title')){Rico.Corner.round('editor_title');}
   
}

/*
    Event.observe(window, 'load', enable_ajax);
    Event.observe(document, 'click', function(event) {  
         var element = Event.element(event);
         mouse_x=Event.pointerX(event)
         mouse_y=Event.pointerY(event)
         //alert(mouse_x)
    }); 
 */
function enable_ajax(){
 ajax_feedback = {
        onCreate: function(){
        var pos_y=mouse_y-200;
        var pos_x=mouse_x-100;
            $('ajax_status').setStyle({top: pos_x,left:pos_y});
            $('ajax_status').show();
        },
        
        onComplete: function() {
            
                $('ajax_status').hide();
            
        }
    };
    
    Ajax.Responders.register(ajax_feedback);
}




var dump_html=''
var dump_message_box_commands = ''


function test(elname){
    alert(elname);
    Element.hide(elname);
}

function slideindiv(message_box, text, location, finalwidth, finalheight, msgtype)
{
    new Rico.Effect.SizeAndPosition( message_box, current_X, current_Y, finalwidth, 1, 1,  15, {complete:populate_div(message_box, text)} );
    Rico.Corner.round($('message_div'), {corners:"all", border:'#000000'});
    Rico.Corner.round($('message_box_title'), {corners:"top"});    
    switch(msgtype){
        case 'information':
            Rico.Corner.round($('command_ok'), {corners:"all"});
        break;
        case 'alert':
        break;
        case 'warning':
        break;
        case 'confirm':
            Rico.Corner.round($('command_proceed'), {corners:"all"});
            Rico.Corner.round($('command_cancel'), {corners:"all"});
        break;
        case '':
        break;
    }
    
}




function refresh_messagebox()
{
	//Rico.Corner.round($('message_div'), {corners:"none"});
   // Rico.Corner.round($('message_box_title'), {corners:"none"}); 
	
	Rico.Corner.round($('message_div'), {corners:"all", border:'#000000'});
    Rico.Corner.round($('message_box_title'), {corners:"top"});  
}

function reset_messagebox()
{
    $('message_div').style.width = '0';
    $('message_div').style.height = '0';
    $('message_div').style.top = '0';
    $('message_div').style.left = '200';
    Element.hide('message_div');
}

function populate_div(message_box, text)
{
    message_box.innerHTML = text;
}

function disposemessage()
{
    reset_messagebox($('message_div'));
    $('message_div').innerHTML = '';
}

/* 
    confirm action simply takes a prompt and displays it based on type, passing on the handler to be executed if confirmed
 */
function confirm_execution(action_type, action_prompt, action_params, position, finalwidth, finalheight){
    
	//alert(action_params)
	var msgtype='confirm'
    var actionHTML='';
    var actions_array='';
    switch(action_type){
        case 'delete':
            reset_messagebox()
			//alert(action_params)
            actions_array='cancel_action(3)^execute_request(3,\'' + action_params + '\')'
            actionHTML=message_object('please confirm',action_prompt,msgtype,actions_array)
		    Element.show('message_div');
			slideindiv(message_div, actionHTML, position,finalwidth, finalheight, msgtype); 
            break;
		case 'media':
			reset_messagebox()
		    actions_array='cancel_action(3)^confirm_execution(\'addmedia\',\'\',\'||true|false\',\'top\', 350, 200)'
			actionHTML=message_object('media',action_prompt,'media',actions_array)
    		Element.show('message_div');
			execute_request(6,action_params)
			slideindiv(message_div, actionHTML, position,finalwidth, finalheight, msgtype); 
			break;	
		case 'viewmedia':
			var msgtype='information'
            dump_html=$('message_div').innerHTML;			
			//reset_messagebox()
			Element.hide('command_cancel');
			Element.hide('command_proceed');			
			
			
			actions_array='cancel_action(4)'
            actionHTML=message_object('Media',action_prompt,'viewmedia',actions_array)			
			execute_request(7,action_params)				
			
			var el=get_el("command_ok");
		    if (el){
		       Rico.Corner.round('command_ok', {corners:"all"});
		    }	
			populate_div(message_div, actionHTML)
			Element.show('message_div');
			//Rico.Corner.round($('message_div'), {corners:"all", border:'#000000'});
		    //Rico.Corner.round($('message_box_title'), {corners:"top"});  
			//refresh_messagebox()										
			break;			
						
		case 'sortmedia':	
			var msgtype='information'
            dump_html=$('message_div').innerHTML;	
			
			
			actions_array='cancel_action(4)^execute_request(9,\'' + action_params + '\')'
            actionHTML=message_object('Sort Media',action_prompt,'save',actions_array)    
			execute_request(8,action_params)							
			populate_div(message_div, actionHTML)
			Element.show('message_div');
			refresh_messagebox()  
			Rico.Corner.round($('command_proceed'), {corners:"all"});
            Rico.Corner.round($('command_cancel'), {corners:"all"});
			
			break;
        case '':    
			
    }
    
	
      
    
}

function cancel_action(action_type){
	 switch(action_type){
        case 3: disposemessage(message_div)
				break;
		case 4: $('message_div').innerHTML=dump_html;	
				break;	
	}
}



function execute_request(action_type,action_params){
    var execute_url='';
    var execute_objectid=0;
    var execute_id=0;
    var use_ajax=false;
    var execute_params='';
    var do_refresh=false;
    switch(action_type){
        case 3:
            var params=action_params.split('|');
            execute_objectid=params[0];
            execute_id=params[1];
            use_ajax=params[2];
            do_refresh=params[3];
            execute_params='objectid=' + execute_objectid + '&recordid=' + execute_id + '&mode=3';
            execute_url='../common/execute_delete.asp';
            ajax_get_url(execute_url,execute_params,'message_div','alert(\'done\')');
            
            Element.hide('row'+execute_id);
            disposemessage(message_div);
            if(do_refresh=='true')  
            {             
                window.location.reload(true);
            }
            
            break;
       case 0:
       		break;
	   case 6:// open initial media page
            var params=action_params.split('|');
            execute_objectid=params[0];
            execute_id=params[1];
            use_ajax=params[2];
            do_refresh=params[3];
            execute_params='objectid=' + execute_objectid + '&recordid=' + execute_id + '&mode=4';
            execute_url='../common/ajax_xref_media.asp';
            ajax_get_url(execute_url,execute_params,'',populate_list);              
            break;
       case 7:  // view media
			var params=action_params.split('|');
            execute_objectid=params[0];
            execute_id=params[1];
            use_ajax=params[2];
            do_refresh=params[3];
            execute_params='objectid=' + execute_objectid + '&recordid=' + execute_id + '&mode=4&action=viewmedia';
            execute_url='../common/ajax_xref_media.asp';
            ajax_get_url(execute_url,execute_params,'',populate_list);            
            break;
		case 8:  // sort media
			var params=action_params.split('|');
            execute_objectid=params[0];
            execute_id=params[1];
            use_ajax=params[2];
            do_refresh=params[3];
            execute_params='objectid=' + execute_objectid + '&recordid=' + execute_id + '&action=sortmedia';
            execute_url='../common/ajax_xref_media.asp';							
            ajax_get_url(execute_url,execute_params,'',populate_save);            
            break;
		case 9:  // save media order
			var els = document.getElementsByTagName('div');
	        var elsLen = els.length;
			var pattern = new RegExp("(^|\\s)listitem(\\s|$)");
			mediaorder = ''
			j=-1
			for (i=0; i < elsLen; i++) {
				if ( pattern.test(els[i].className) ) {		
					j++;		
					mediaorder+= els[i].id+ "$|$" + "$|$" + j.toString() + "$|$";
				}				
			}		
	        //$('txt_sequence').value=retcode;			
			var params=action_params.split('|');           
            execute_objectid=params[0];
            execute_id=params[1];
            use_ajax=params[2];
            do_refresh=params[3];
            execute_params='objectid=' + execute_objectid + '&recordid=' + execute_id + '&action=savemediaorder&mediaorder='+mediaorder;
            execute_url='../common/ajax_xref_media.asp';							
            ajax_get_url(execute_url,execute_params,'','');  
			        
			$('message_div').innerHTML = dump_html;
			execute_request(6,action_params)
            break;
		
       }
}


function populate_list(obj){
	$('list_container').innerHTML=obj.responseText;
}

function populate_save(obj){
	$('saving_container').innerHTML=obj.responseText;
}

/*
        message object, returns html needed to present a message box
*/
function message_object(msg_title,msg_narrative,msg_type, msg_actions){
    var message_html='<table class="message_box" width="100%" cellpadding="0" cellspacing="0" border="0">';
    if (msg_title.length!=0){
        message_html+='<tr><td colspan="2" id="message_box_title" class="message_box_title">' + msg_title + '</td></tr>';
    }
    switch (msg_type){
        case 'information':
            message_html+='<tr><td class="message_box_text"><div class="message_box_narrative_image" id="message_box_narrative_image">' + msg_narrative +'</td><td><img id="messge_box_icon" class="message_box_icon" src="../common/images/icons/msg_information.gif" style="border:0px;"></div></td></tr>';
            message_html+='<tr><td colspan="2" class="message_box_commands" align="center"><div id="command_ok" style="top:5px; text-align:centre;" class="command_ok" onclick="javascript:' + msg_actions + '">OK</div></td></tr>';        
            break;
        case 'alert':
            break;
        case 'warning':
            break;
        case 'confirm':
            var array_actions=msg_actions.split('^');
            message_html+='<tr><td class="message_box_text"><div class="message_box_narrative_image" id="message_box_narrative_image">' + msg_narrative +'</td><td><img id="messge_box_icon" class="message_box_icon" src="../common/images/icons/msg_information.gif" style="border:0px;"></div></td></tr>';
            message_html+='<tr><td colspan="2" class="message_box_commands"><div id="command_cancel" class="command_cancel" style="text-align:centre;" onclick="javascript: ' + array_actions[0] + '">Cancel</div><div id="command_proceed" class="command_proceed" style="text-align:centre;" onclick="javascript: ' + array_actions[1] + '">Proceed</div></td></tr>';        
            break;
		case 'media':
            var array_actions=msg_actions.split('^');
            message_html+='<tr><td colspan="2"><div class="listingdiv" id="list_container"></div></td></tr>';
            message_html+='<tr><td colspan="2" class="message_box_commands"><div id="command_cancel" class="command_cancel" style="text-align:centre;" onclick="javascript: ' + array_actions[0] + '">Cancel</div><div id="command_proceed" class="command_proceed" style="text-align:centre;" onclick="javascript: ' + array_actions[1] + '">Add</div></td></tr>';        
			break;
		case 'save':
            var array_actions=msg_actions.split('^');
            message_html+='<tr><td class="message_box_text"><div class="savediv" id="saving_container">' + msg_narrative +'</td><td></div></td></tr>';
            message_html+='<tr><td colspan="2" class="message_box_commands"><div id="command_cancel" class="command_cancel" style="text-align:centre;" onclick="javascript: ' + array_actions[0] + '">Cancel</div><div id="command_proceed" class="command_proceed" style="text-align:centre;" onclick="javascript: ' + array_actions[1] + '">Save</div></td></tr>';        
            break;
		case 'viewmedia':
			var array_actions=msg_actions.split('^'); 
			message_html+='<tr><td colspan="2"><div class="listingdiv" id="list_container"></div></td></tr>';
            message_html+='<tr><td colspan="2" class="message_box_commands" align="center"><div id="command_ok" style="top:5px; text-align:center;" class="command_ok" onclick="javascript:' + msg_actions + '">OK</div></td></tr>';        
			message_html+='<tr><td height="5px"></td></tr>';
            break;	
			
        case '':
            message_html+='<tr><td class="message_box_text" align="center"><div class="message_box_narrative" id="message_box_narrative">' + msg_narrative +'</div></td></tr>';
            
            break;
    }
    message_html+='';
    message_html+='';
    message_html+='';
    message_html+='';
    message_html+='';
    message_html+='</table>';
    
    
    return message_html;
}
/*var myGlobalHandlers = {
    onCreate: function(){
    Element.show('message_div');
    },
    
    onComplete: function() {
        if(Ajax.activeRequestCount == 0){
            disposemessage(message_div);

        }
    }
};*/


//Ajax.Responders.register(myGlobalHandlers);
Event.observe(document, 'mousedown', mouse_coordinates);
var current_X;
var current_Y;
function mouse_coordinates(event){
    current_X=Event.pointerX(event)
    current_Y=Event.pointerY(event)
    //alert(current_X);
   //alert(current_y);   
    
//    alert(current_Y);
}    
function stopscrolling(){
    //alert('here');
    $('scrollingmarq').scrollamount = '0';
}
function creategallery(scrollimage, imagetitles){
    var images = '';
    images += '<table><tr>'
    for(i=0; i<scrollimage.length; i++)
    {images+='<td><div class="gallery"><img src="../images/'+ scrollimage[i] +'" onmousedown="javascript:stopscrolling();"><br>'+ imagetitles[i] +'</div></td>';}
    images += '</tr></table>'
    var galleryobject = '';
    $('gallery_area').innerHTML+='<div id="picture_preview" class="picture_preview"></div>';
    $('gallery_area').innerHTML+='<table><tr><td><img src="../images/prev_arrow.gif" onmouseover="javascript:scrollback();"></td>';
    $('gallery_area').innerHTML+='<td><marquee width="300px;" id="scrollingmarq">'+images+'</marquee></td>';
    $('gallery_area').innerHTML+='<td><img src="../images/next_arrow.gif" onmouseover="javascript:scrollfwd();"></td></tr></table>';
}

function scrollback(){
    $('scrollingmarq').direction = 'left';
}
function scrollfwd(){
    $('scrollingmarq').direction = 'right';
}

// on occassion we re-configure the workspace to allow more screen area for a particular dialogue - this function returns everything back to the  way is was before
function reset_workspace(){
                //new Rico.Effect.Position('admin_navigation_area', 1,null, 200,10, {complete: function(){new Rico.Effect.Position('admin_navigation_area', -1,null, 200,10)}})   
                //new Rico.Effect.SizeAndPosition( 'admin_workspace', 190,0, 760, 500, 100, 30);
                  window.location.href='../index.asp'
                
                //Element.hide('bulk_operations');
                //new Rico.Effect.SizeAndPosition( 'memberdiv', 190,0, 180, 500, 100, 30);
                //new Rico.Effect.SizeAndPosition( 'datadiv', 380,0, 400, 500, 100, 30);

}

// generic form validation
function check_form_inputs(form){
alert(navigator.appName);
if(navigator.appName!='Netscape'){
        var s=$(form.name).getElements();
        for (var i=0;i<s.length;i++){
            //Element.extend(s[i]);
            if(s[i].validationmsg){
                var validation_scripts=s[i].validationmsg.split(";")
                for (var n=0;n<validation_scripts.length;n++){
                    if (eval(validation_scripts[n])==false){
                        return false;
                    };
                }
            };
        }
        return false;
    }
else{
    //firefox and other browsers handled here
    var tests=$F('validators');
    alert(tests);
}        

}

// check email format
function check_input_email(el){
    
	var sval=$(el) //.value
   // alert(sval);
// check that a value has been entered for the user name field
	if (sval.length==0){
		return false;
	}
// check that the format matches email address format	
	if (!fixstring(sval,true)){
		alert("Your email address is invalid - this system only supports the format 'yourname@address.com' !");
		return false;
	}
	
    return true;

}
//generic function that checks the length of a text input and if zero displays the passed message
function check_length(el_name,msg){
    var s_value=$(el_name).value;
    
    if (s_value.length==0){
        alert(msg);
        $(el_name).focus;
        return false;
    }
    return true;
}
/* position_floating: set-up the floating dialogue in the correct position */
function position_floating(){   
    new Rico.Effect.SizeAndPosition( 'floating_dialogue', current_X, current_Y, 600, 480, 10, 1,{complete: show_floating});
}
/* show_floating: displays the floating dialogue and checks if there are any elements that should be watched for focus and blur events */
function show_floating(){
    Element.show('floating_dialogue') // display floating div   
    var el=get_el("watch_list_elements"); //get list of elements to observe
    if (el) {
        watch_list_array=$('watch_list_elements').value.split(",");
        for (var i=0; i < watch_list_array.length;i++){
            
            Event.observe(watch_list_array[i], 'focus', switch_hilite_on);        
            Event.observe(watch_list_array[i], 'blur', switch_hilite_off);        
        }
    }
}


//switch_hilite_on: used for generic form presentation - moves the highlight to event source
function switch_hilite_on(event){
    var s=Event.element(event).id;
    var el=get_el('prompt_' + s);
    if (el){
        $('prompt_' + s).toggleClassName('highlight');
    }
    var el=get_el('input_' + s);
    if (el){
        $('input_' + s).toggleClassName('highlight')
    }
}

//switch_hilite_off: used for generic form presentation - moves the highlight to event source
function switch_hilite_off(event){
    var s=Event.element(event).id;
    var el=get_el('prompt_' + s);
    if (el){
        $('prompt_' + s).toggleClassName('highlight');
    }
    var el=get_el('input_' + s);
    if (el){
        $('input_' + s).toggleClassName('highlight')
    }
}

//submit_editor: generic form submission script
function submit_editor(){
    //alert("here")
    if (disable_commands()){
            var el=get_el("js_validator")    // check to see if there is a validator for the AJAX form before we submit it
            if (el){
                
                if (eval(el.value)!=true){
                    if (enable_commands()){
                    return false;
                    }
                }
            }
            // prepare to post form to generic form handler 
            var p=$('save').serialize(); 
            var url=$F('post_to'); // parameter that must be contained in the form being processed e.g. common/agility_ajax.asp
            //alert(url);
            //alert(p);
           //ajax_post_url(url, p, 'editor_contents',debug_view)    
            ajax_post_url(url, p, 'editor_contents',refresh_view)  
            return false;  
     }
//   ajax_post_url(url, p, 'editor_contents',debug_view)    
    
}   

//close_editor: generic form submission script
function close_editor(){
    $('floating_dialogue').hide();
}
/* execute_editor: generic handler to call another javascript */
function execute_editor(function_name, function_arguments){
    var f = function_name + '(' + function_arguments + ');'
    eval(f);
}
function remove_row(row_id){
    Element.remove(row_id);
     //$('floating_dialogue').show();
}



function editor_switchboard(object_id,record_id){
var f_name,f_param
    switch(object_id){
        case 1127: // client editor
            f_name="edit_client";
            f_param=record_id;
            break;
        case 1128: //document groups
            f_name="edit_document_group";
            f_param=record_id;
            break;
        case 1129: //document groups
            f_name="edit_fund";
            f_param=record_id;
            break;
        case 1130: //document groups
            f_name="edit_user";
            f_param=record_id;
            break;
        case 1131: //library document
            f_name="edit_document";
            f_param=record_id;
            break;
        case 1132: //permission group
            f_name="edit_group";
            f_param=record_id;
            break;
        case 1137: //help file
            f_name="edit_help";
            f_param=record_id;
            break;
        case 1149: //help file
            f_name="edit_shortcut";
            f_param=record_id;
            break;
        default:
    }
    if (f_name.strip().length!=0){
        execute_editor(f_name,f_param);
    }
    
}

/* render_listing: when an Ajax enabled listing is presented it must be populated when the page loads */
function render_listing(){
    var el=get_el("list_default"); // js command is stored in a control called list_default
    if (el) {
        // if the js command exists then we must eval it
        //alert(el.value);
        eval(el.value);
    }
    
}
function update_list_default(params){
    var el=get_el("list_default"); // js command is stored in a control called list_default
    if (el) {
           var url="ajax_get_url('common/agility_ajax.asp','" + params + "', 'list_area', round_tools);"
           el.value=url
    }
}
/* debug_view: displays the results of a form that has been submitted via ajax - for debug purposes */
function debug_view(obj){
    $('floating_dialogue').innerHTML=obj.responseText;
}

/* refresh_view: once a form as been submitted and processed, tidy up is required */
function refresh_view(){
    close_editor();
    render_listing();
}

/*open_page: reloads main window with url */
function open_page(url){
if (url.strip().length!=0){
    window.location.href=url;
    }
}

/* round_tools: if there are service command options displayed on page then round the edges */
function round_tools(){
    var el=get_el("list_tools"); //element that contains the id of the tools to be rounded
    if (el){
        var s_data=el.value;
        if (s_data.strip().length!=0){
            var a_data=s_data.split(',');
            for (var i=0;i<a_data.length;i++){
                //Rico.Corner.round('tool_' + a_data[i],{corners:"all"});
            }
        }
    }
}

/*close_panel: generic hide a floating div */
function close_panel(panel){
    $(panel).hide();
}
/*show_panel: generic show a floating div */
function show_panel(panel){
    $(panel).show();
}



/*show_order_panel: display the order panel */
function show_order_panel(){
        var pos_y=mouse_y-160;
        var pos_x=mouse_x-300;
    window.status=pos_x + ' '+ pos_y;
    //alert(pos_x);
    //alert(pos_y);
    $('order_panel').setStyle({top: pos_y,left:pos_x});
    show_panel('order_panel');
    
}
/*edit_order: function to pop up a menu of options for changing order status */
function edit_order(record_id){
    
    var url="../asp/ajax.asp"
    var p="recordid=" + record_id  + "&objectid=513&m=7"
    ajax_get_url(url,p, 'order_panel', show_order_panel);
    
}


/* check_state_change: validate the state change dialogue */
function check_state_change(){
    // ensure that something is selected
    var el=get_el("list_states");
    if (el){
        if (el.selectedIndex==0){
            alert("please select an option from the list !");
            return false;
        }
    }
    var status=el.options[el.selectedIndex].value;
    
    var el=get_el("object_id");
    var object_id=el.value;
    
    var el=get_el("update_mode");
    var u_mode=el.value;
    
    var el=get_el("current");
    var c_status=el.value;
    
    var el=get_el("update_type");
    var u_type=el.value;
    
    var el=get_el("record_id");
    var record_id=el.value
    
    var url="../asp/ajax.asp"
    var p="objectid=" + object_id + "&m=" + u_mode + "&recordid="+ record_id + "&action=update_state&c_status=" + c_status + "&n_status=" + status + "&u_type=" + u_type
    ajax_get_url(url,p, 'order_panel', update_orders);
//    close_panel('order_panel');
//    ajax_get_url(url,p, '', '');
    return false;

}
function update_orders(){
    var s=$("state_grid").getElementsByClassName('current_row');
    //alert(s[0].inspect());
    var f=$(s[0]).immediateDescendants();
    var b=f[0].immediateDescendants()
    var e=b[0].readAttribute('onclick');
    //alert(inspect(
//    __doPostBack('state_grid','Sort$state_value');
//    __doPostBack('state_grid','Sort$state_value');
//    document.location.href='../asp/Default.aspx'
    eval(e);
}


