function ajax_get_lookup_result(r)
{ 	
	/*	This moves lookup data set into select options.
		-t is target_lookup_id.
		-r is lookup result set.
		-1st element of result set is used to pass back target lookupid .
		and is immediately deleted.  
		-t has to be trimmed because it comes back with a trailing space.
		-f is follow on function.
	*/	
	
	var k = '';
	var v = '';
	
	for (i= 0; i < r.length; i++)
	{
		k = r[i].KEY;
		v = r[i].VALUE;
		
		/* Deal with DRWUtils habit of padding key and value strings once and for all */
		
		while(''+k.charAt(0)==' ')
			{k = k.substring(1,k.length); }			
		while(''+k.charAt(k.length-1)==' ')
			{k = k.substring(0,k.length-1); }
			
		while(''+v.charAt(0)==' ')
			{v = v.substring(1,v.length); }			
		while(''+v.charAt(v.length-1)==' ')
			{v = v.substring(0,v.length-1); }	
		
		r[i].KEY = k;	
		r[i].VALUE = v;
		
	}
	
	if ( report_ajax_errors(r,true) == false )
	
	{	t = r[0].KEY;
		f = r[0].VALUE;
		r.splice(0,1);
		DWRUtil.removeAllOptions(t);
		DWRUtil.addOptions(t,r,"KEY","VALUE");
	
		if ( f!='-' )
		{
			f = unescape(f);
			try{eval(f)}catch(e){alert('Error in follow on function \n' + f)}
		}
	} 
	return r;
}
	
function ajax_init()
{
	/* This initialises the Ajax engine. 	
	NB AJax documentation says to do this in the header in case you want to 
	fire an ajax function on load, but we use traditional CF code to initialise lookups,
	so this is not necessary.  Its better to ensure
	selected option is data driven so you do not any attempt to initialise
	select options here.
	*/
	
	DWRUtil.useLoadingMessage();	
	DWREngine._errorHandler =  errorHandler;

}

/*This is a dummy follow on function created by dbs to handle lack of a follow on function 
which otherwise errors in IE */
function ajax_no_follow_on(y)
{
	x = y; 
}	

function report_ajax_errors(r,already_trimmed,allow_user_error)
{	
	var k = '';
	var v = '';
	var has_ajax_error = false;
	var has_user_error = false;
	var resume_function = '';
	
	if ( typeof(allow_user_error) == 'undefined' )
	{
		allow_user_error = false;	
	}
	
	ajax_debug_results = new Array;
	
	for (i= 0; i < r.length; i++)
		{
			k = r[i].KEY;
			v = r[i].VALUE;
			
			/* Deal with DRWUtils habit of padding key and value strings once and for all */
			if (!already_trimmed)
			{
				while(''+k.charAt(0)==' ')
					{k = k.substring(1,k.length); }			
				while(''+k.charAt(k.length-1)==' ')
					{k = k.substring(0,k.length-1); }
					
				while(''+v.charAt(0)==' ')
					{v = v.substring(1,v.length); }			
				while(''+v.charAt(v.length-1)==' ')
					{v = v.substring(0,v.length-1); }	
				
				r[i].KEY = k;	
				r[i].VALUE = v;
			}
						
			if ( k == 'AJAX_ERROR' )
			{
				has_ajax_error = true;
			}
			else if ( k == 'USER_ERROR' )
			{
				has_user_error = true;
			}
		}	

	if (has_ajax_error == true)
	{
		return_message = 'ERRORS OCURRED IN CF AJAX HANDLER \n Please report this to the system administrator \n';
		ajax_debug_results = r;
		
		for (i= 0; i < r.length; i++)
		{
			rf = r[i].KEY;

			if ( rf == 'ON_CF_ERROR_FUNCTION' )
			{
				resume_function = unescape(r[i].VALUE);	
			}
			else if ( rf == 'AJAX_ERROR' )
			{
				return_message = return_message + unescape(r[i].VALUE) + ' \n ';
			}
		}
		
		alert(return_message);
		if (resume_function != '')
		{
			resume_function = unescape(resume_function);
			try{eval(resume_function)}catch(e){alert(e)};
		}		
		return true;		
	}

	if (has_user_error == true)
	{		
		return_message = '';
		ajax_debug_results = r;
		
		for (i= 0; i < r.length; i++)
		{
			rf = r[i].KEY;
			
			if ( trim_string(rf) == 'ON_USER_ERROR_FUNCTION' )
			{
				resume_function = unescape(r[i].VALUE);	
			}
			else if ( rf == 'USER_ERROR' )
			{
				return_message = return_message + unescape(r[i].VALUE) + '\n';
			}
		}
		
		alert(return_message);
		if (resume_function != '')
		{
			resume_function = unescape(resume_function);
			try{eval(resume_function)}catch(e){alert(e)};
		}
		
		return !allow_user_error;
	} 

	return false;
}

function show_job_enquiry(job_id)
{
	var	v_fuseaction = 'job_entry';
	if ( typeof(job_id) =='undefined' )
	{
		try{ job_id ='';job_id = document.getElementById('job_status_job_id').value; }catch(e){};
	}
	if (job_id == '')
	{
		v_fuseaction = 'job_enquiry';
		job_id = 0;
	}
	v_url = _DBSVARS.request.server_protocol + '//' + _DBSVARS.cgi.server_name + '/' + _DBSVARS.request.shared_apps_root;
	v_url =  v_url + '/scheduled_jobs/index.cfm?fuseaction=' + v_fuseaction + '&job_id=' + job_id + '&as_popup=yes&cfroot=' + _DBSVARS.request.cfroot + '&' + _DBSVARS.session.urltoken;
	open_window('open',v_url,'show_job_entry',800,800);
	return;
}

function stop_job(job_id)
{
	if ( typeof(job_id) =='undefined' )
	{
		try{ job_id ='';job_id = document.getElementById('job_status_job_id').value; }catch(e){};
	}
	a_params =[	'ajax_root^site', 
				'job_id^' + job_id,
				'object_name^stop_job',
				];
	ajax_get_data(a_params,'job_result');
	return;		
}

function restart_job(job_id)
{
	if ( typeof(job_id) =='undefined' )
	{
		try{ job_id ='';job_id = document.getElementById('job_status_job_id').value; }catch(e){};
	}

	a_params =[	'ajax_root^site', 
				'job_id^' + job_id,
				'object_name^restart_job',
				];
	ajax_get_data(a_params,'job_result');
	return;		
}

function recover_job(job_id)
{
	if ( typeof(job_id) =='undefined' )
	{
		try{ job_id ='';job_id = document.getElementById('job_status_job_id').value; }catch(e){};
	}
	a_params =[	'ajax_root^site', 
				'job_id^' + job_id,
				'object_name^restart_job',
				'include_errors^yes'
				];
	ajax_get_data(a_params,'job_result');
	return;		
}

function get_job_status()
{

	a_params =[	'ajax_root^site', 
				'job_id^' + this.job_id,
				'object_name^get_job_status',
				];
	ajax_get_data(a_params,'job_result');
	return;			
}

function run_job(key_list,job_code,job_name,collection_id,start_element_id,entity_label,parameters,confirm_message,job_template_name,no_records_per_hit,item_template_name,no_of_threads,schedule_delay,timeout)
{

	if (typeof(timeout) == 'undefined')
	{
		var timeout = document.getElementById('timeout')|| '';
	}
	
	if (typeof(job_code) == 'undefined')
	{
		var job_code = DWRUtil.getValue('job_code');
	}
	
	if (job_code=='')
	{
		alert('Please select an action');
		return;
	}
	
	if (typeof(job_name) == 'undefined')
	{
		var job_name = DWRUtil.getText('job_code');	
	}
	
	if (typeof(start_element_id) == 'undefined')
	{
		start_element_id = 'b_run_job';	
	}
	
	if (typeof(key_list) == 'undefined')
	{
		key_list = build_key_list();
	}
	
	if (typeof(collection_id) == 'undefined')
	{
		collection_id = DWRUtil.getValue('collection_id');
	}
	
	if (typeof(job_name) == 'undefined')
	{
		job_name = job_code;	
	}
	
	if (typeof(entity_label) == 'undefined')
	{
		entity_label = 'item';	
	}
	
	if (typeof(parameters) == 'undefined')
	{
		parameters = '';	
	}

	if (typeof(job_template_name) == 'undefined')
	{
		job_template_name = '';	
	}
	
	if (typeof(no_records_per_hit) == 'undefined')
	{
		no_records_per_hit = '';	
	}
	
	if (typeof(item_template_name) == 'undefined')
	{
		item_template_name = '';	
	}
	
	if (typeof(no_of_threads) == 'undefined')
	{
		no_of_threads = '';	
	}
		
	if (typeof(schedule_delay) == 'undefined')
	{
		schedule_delay = '';	
	}
	
	if (key_list == '' & collection_id < '1' & job_template_name =='')
	{
		alert('You have not selected any ' + entity_label + 's.  Please select some or all ' 
			  	+ entity_label + 's first or use a collection.');
		return;
	}

	if (typeof(confirm_message) == 'undefined' || confirm_message == '')
	{
		if (key_list =='')
		{
			collection_name = DWRUtil.getText('collection_id');
			confirm_message = 'Do you really want to ' + job_name + ' for all members of the collection ' + collection_name +'?'; 
		}
		else
		{
			confirm_message = 'Do you really want to ' + job_name + ' for all selected ' +  entity_label + 's?';
		}	
	}
	
	if (!confirm(confirm_message))
	{
		return;
	}

	var follow_on_function = ""; 																
	var on_error_function  = "";
	
	this.job_start_element_id = start_element_id;
	document.getElementById(this.job_start_element_id).disabled = true;
	a_params =[	'ajax_root^site', 
				'job_code^' + job_code,
				'key_list^' + key_list,
				'collection_id^' + collection_id,
				'job_name^' + job_name,
				'object_name^create_job',
				'job_params^' + parameters,
				'follow_on_function^'+ follow_on_function,
				'job_template_name^' + job_template_name,
				'no_records_per_hit^' + no_records_per_hit,
				'item_template_name^' + item_template_name,
				'no_of_threads^' + no_of_threads,
				'schedule_delay^' + schedule_delay,
				'timeout^' + timeout	
				];
	ajax_get_data(a_params,'job_result');

	return;
}

/* Unused code couldn't get it to work properly in IE
function show_progress(relative_to, message)
{			
	var its_position
	
	if (!relative_to)
	{
		relative_to = document.getElementsByTagName('BODY')[0];
	}
	
	its_position = findPosition(relative_to);
	
	if (!message | message == '')
	{
		message= 'Working....';
	}
	
	{	
		if ( !this.my_progress_indicator )
		{
			this.my_progress_indicator = document.createElement('div');
			document.getElementsByTagName('BODY')[0].appendChild(this.my_progress_indicator);
		}	
		this.my_progress_indicator.style.display='none'; 
		this.my_progress_indicator.innerHTML = message; 
		this.my_progress_indicator.style.zIndex='5000'; 
		this.my_progress_indicator.style.border='3px double black';
		this.my_progress_indicator.style.backgroundColor='#CCCCCC';
		this.my_progress_indicator.style.padding='5px';
		this.my_progress_indicator.style.left=its_position[0];
		this.my_progress_indicator.style.top=its_position[1];
		this.my_progress_indicator.style.position='absolute'; 
		this.my_progress_indicator.style.display=''; 
	}	
}

function hide_progress()
{			
	try{this.my_progress_indicator.style.display='none';}catch(e){};
}

function job_launched(r)
{
	hide_progress();
	job_result(r);
}*/

function build_key_list(select_id)
{
	var key_list = '';
	var entity_id = '';	
	
	if (typeof(select_id) == 'undefined')
	{
		select_id = 'chk_';	
	}
	for (i= 0; i < 10000; i++)
	{
		j= i+1;
		entity_id = '';		
		try
		{
			box = document.getElementById(select_id + j); 
			entity_id = box.value;	
		}
		catch(e){break}
		
		try
		{
			if (box.checked)
			{
				key_list = key_list + ',' + entity_id;
			}
		}
		catch(e){}
	}	
	return key_list;				
}

function select_all(checked,select_id,upto)
{   if ( typeof(checked) == 'undefined' )
	{
		try
		{
			checked = document.getElementById('select_all').checked;	
		}
		catch(e){alert('Cant do this because of programming error: document element select_all is undefined')}
	}
	
	if ( typeof(select_id) =='undefined' )
	{
		select_id = 'chk_';
	}

	v_end = 10000;
	if (upto)
	{	v_end = eval(upto) + 1;
		checked = true;
		if (document.getElementById(select_id + 1).checked)
	    	checked = false;
	}
 
  	
	for (j= 1; j < v_end; j++)
	{			
 		try
		{
 			box = document.getElementById(select_id + j); 
 			box.checked = checked;	
		}
		catch(e){break;}
	}
	return;	
}
function findPosition( oElement ) {

  if( typeof( oElement.offsetParent ) != 'undefined' ) {
    for( var posX = 0, posY = 0; oElement; oElement = oElement.offsetParent ) {
      posX += oElement.offsetLeft;
      posY += oElement.offsetTop;
    }
	 return [ posX, posY ];
  } else {
    return [ oElement.x, oElement.y ];
  }
}

function job_result(r)
{
	var job_id = '';
	var job_code = '';
	var job_no = '';
	var status_code = '';
	var total_no_records = '';
	var no_processed_records = '';
	var no_error_records = '';
	var no_user_error_records = '';
	var no_remaining_records = '';
	
	report_ajax_errors(r);
	
	for (i= 0; i < r.length; i++)
	{
		if (trim_string(r[i].KEY) == 'JOB_ID') 				job_id = r[i].VALUE;
		if (trim_string(r[i].KEY) == 'JOB_CODE') 			job_code = trim_string(r[i].VALUE);
		if (trim_string(r[i].KEY) == 'JOB_NO') 				job_no = r[i].VALUE;
		if (trim_string(r[i].KEY) == 'STATUS_CODE') 		status_code = trim_string(r[i].VALUE);
		if (trim_string(r[i].KEY) == 'TOTAL_NO_RECORDS') 	total_no_records = r[i].VALUE;
		if (trim_string(r[i].KEY) == 'NO_PROCESSED_RECORDS') no_processed_records = r[i].VALUE;
		if (trim_string(r[i].KEY) == 'NO_ERROR_RECORDS') 	no_error_records = r[i].VALUE;
		if (trim_string(r[i].KEY) == 'NO_USER_ERROR_RECORDS') 	no_user_error_records = r[i].VALUE;
		if (trim_string(r[i].KEY) == 'NO_REMAINING_RECORDS')	no_remaining_records = r[i].VALUE;
	}
	
	this.job_id = job_id;
	
	display_job_progress(job_id,job_code,job_no,status_code,total_no_records,no_processed_records,no_error_records,no_user_error_records,no_remaining_records);
	
	if (no_remaining_records > 0 & status_code != 'STOPPED')
	{
		if (!this.timer_active)
		{
			interval = self.setInterval(get_job_status,500);
			this.timer_active = true;
		}		
	}	
	else 
	{
		try
		{	
			try{document.getElementById(this.job_start_element_id).disabled = false;}catch(e){};
			window.clearInterval(interval);
			this.timer_active = false;
		}
		catch(e){}	
	}
}

function display_job_progress(job_id,job_code,job_no,status_code,total_no_records,no_processed_records,no_error_records,no_user_error_records,no_remaining_records)
{
	
	document.getElementById('job_status_job_id').value = job_id;
	document.getElementById('job_status_job_code').value = job_code;
	document.getElementById('job_status_job_no').value = job_no;
	document.getElementById('job_status_status_code').value = status_code;
	document.getElementById('job_status_total_no_records').value = total_no_records;
	document.getElementById('job_status_no_processed_records').value = no_processed_records;
	document.getElementById('job_status_no_error_records').value = no_error_records;
	document.getElementById('job_status_no_user_error_records').value = no_user_error_records;
	document.getElementById('job_status_no_remaining_records').value = no_remaining_records;
	if (job_id > 1)
	{
		document.getElementById('job_status_display').style.display = '';
	}
	else
	{
		document.getElementById('job_status_display').style.display = 'none';
	}
	
	percent_complete = no_processed_records/total_no_records*100;

	v_html = '|';
	for (x=1;x<=percent_complete; x++)
	{
		v_html = v_html + '|';
	}

	if (percent_complete > 99)
	{
		v_html= v_html+'-';
	}
	
	document.getElementById('job_progress_indicator').innerHTML = v_html;
	
	if (status_code == 'FINISHED')
	{
		document.getElementById('job_status_status_code').style.color = 'orange';
		document.getElementById('job_progress_indicator').style.color = 'orange';
	}
	else if (no_error_records > 0)
	{
		document.getElementById('job_status_status_code').style.color = 'red';
		document.getElementById('job_progress_indicator').style.color = 'red';
	}
	else
	{
		document.getElementById('job_status_status_code').style.color = 'green';
		document.getElementById('job_progress_indicator').style.color = 'green';		
	}	
	
	try
	{	
		if (status_code == 'STOPPED')
		{
			document.getElementById('refresh_button').style.display = '';
			document.getElementById('restart_button').style.display = '';
		}
		
		else
		{
			document.getElementById('refresh_button').style.display = 'none';
			document.getElementById('restart_button').style.display = 'none';
		}
	}
	catch(e){};
}

function display_job_overview(job_id,total_no_jobs,no_error_jobs,no_in_progress_jobs,no_finished_jobs,no_stopped_jobs)
{	
/*	if (job_id > 1)
	{
		document.getElementById('job_overview_display').style.display = 'none';
	}
	else
	{
		document.getElementById('job_overview_display').style.display = '';
	}*/

	document.getElementById('total_no_jobs').value = total_no_jobs;
	document.getElementById('no_error_jobs').value = no_error_jobs;
	document.getElementById('no_in_progress_jobs').value = no_in_progress_jobs;
	document.getElementById('no_finished_jobs').value = no_finished_jobs;
	document.getElementById('no_stopped_jobs').value = no_stopped_jobs;
}

function get_job_overview()
{
	var follow_on_function = "job_error"; 																
	var on_error_function  = "job_error";
	a_params =[	'ajax_root^site', 
				'object_name^get_job_overview',
				'follow_on_function^'+ follow_on_function,
				'on_cf_error_function^' + on_error_function,
				'on_user_error_function^' + on_error_function,
				];
	ajax_get_data(a_params,'overview_result');

	return;
}

function overview_result(r)
{
	var job_id = '';
	var job_code = '';
	var job_no = '';
	var status_code = '';
	var total_no_records = '';
	var no_processed_records = '0';
	var no_error_records = '0';
	var no_user_error_records = '0';
	var no_remaining_records = '0';
	var total_no_jobs = '0';
	var no_error_jobs = '0';
	var no_in_progress_jobs = '0';
	var no_finished_jobs = '0';
	var no_stopped_jobs = '0';
	
	for (i= 0; i < r.length; i++)
	{
		if (trim_string(r[i].KEY) == 'TOTAL_NO_JOBS')		total_no_jobs = r[i].VALUE;
		if (trim_string(r[i].KEY) == 'NO_ERROR_JOBS')		no_error_jobs = r[i].VALUE;
		if (trim_string(r[i].KEY) == 'NO_IN_PROGRESS_JOBS')	no_in_progress_jobs = r[i].VALUE;
		if (trim_string(r[i].KEY) == 'NO_FINISHED_JOBS')	no_finished_jobs = r[i].VALUE;
		if (trim_string(r[i].KEY) == 'NO_STOPPED_JOBS')		no_stopped_jobs = r[i].VALUE;		

		if (trim_string(r[i].KEY) == 'JOB_ID') 				job_id = r[i].VALUE;
		if (trim_string(r[i].KEY) == 'JOB_CODE') 			job_code = trim_string(r[i].VALUE);
		if (trim_string(r[i].KEY) == 'JOB_NO') 				job_no = r[i].VALUE;
		if (trim_string(r[i].KEY) == 'STATUS_CODE') 		status_code = trim_string(r[i].VALUE);
		if (trim_string(r[i].KEY) == 'TOTAL_NO_RECORDS') 	total_no_records = r[i].VALUE;
		if (trim_string(r[i].KEY) == 'NO_PROCESSED_RECORDS') no_processed_records = r[i].VALUE;
		if (trim_string(r[i].KEY) == 'NO_ERROR_RECORDS') 	no_error_records = r[i].VALUE;
		if (trim_string(r[i].KEY) == 'NO_USER_ERROR_RECORDS') 	no_user_error_records = r[i].VALUE;
		if (trim_string(r[i].KEY) == 'NO_REMAINING_RECORDS') no_remaining_records = r[i].VALUE;
	}
	
	this.job_id = job_id;
	
	/*display_job_progress(job_id,job_code,job_no,status_code,total_no_records,no_processed_records,no_error_records,no_remaining_records);	*/	
	display_job_overview(job_id,total_no_jobs,no_error_jobs,no_in_progress_jobs,no_finished_jobs,no_stopped_jobs);
	
	if (!this.timer_active)
	{
		interval = self.setInterval(get_job_overview,5000);
		this.timer_active = true;
	}
}

function save_to_collection(object_id,currentrow,entity_name)
{	
	var collection_id = DWRUtil.getValue('collection_id');
	var operation;
	var collection_id_field;	
	
	if (collection_id < 1)
	{
		alert('Please select a collection first');
		return;
	}
	
	collection_id_field = document.getElementById('collection_id_'+currentrow);

	if (collection_id_field.value > 0)
	{
		operation = 'remove_from';
	}
	else
	{
		operation = 'add_to';
	}
	
	var a_params = new Array();	
	a_params[0] = 'ajax_root^site';
	a_params[1] = 'entity_name^' + entity_name;
	a_params[2] = 'collection_id^' + collection_id;
	a_params[3] = 'key_list^' + object_id;
	a_params[4] = 'silent^yes';
	a_params[5] = 'object_name^' + operation + '_collection';

	ajax_get_data(a_params,'collection_result');
	
	image = document.getElementById('collect_'+currentrow);
		
	if (operation == 'add_to')
	{
		image.src = _DBSVARS.request.imagesroot + '//full_basket_no_border.gif';
		collection_id_field.value = collection_id;
	}
	else
	{
		image.src = _DBSVARS.request.imagesroot + '//empty_basket_no_border.gif';	
		collection_id_field.value = 0;		
	}	
		
	return;	
}

function remove_from_collection(object_id,collection_id,entity_name)
{	
	var operation;
	var collected_id_field;
	
	collected_id_field = document.getElementById('collected_'+object_id+'_'+collection_id);

	if (collected_id_field.value == 1)
	{
		operation = 'remove_from';
	}
	else
	{
		operation = 'add_to';
	}

	image = eval('document.f_account_enquiry.collection_'+object_id+'_'+collection_id);
		
	if (operation == 'add_to')
	{
		image.src = _DBSVARS.request.imagesroot + '//full_basket_no_border.gif';
		collected_id_field.value = 1;
	}
	else
	{
		image.src = _DBSVARS.request.imagesroot + '//empty_basket_no_border.gif';
		collected_id_field.value = 0;	
	}
	
	var a_params = new Array();	
	var operation = 'remove_from';
	a_params[0] = 'ajax_root^site';
	a_params[1] = 'entity_name^' + entity_name;
	a_params[2] = 'collection_id^' + collection_id;
	a_params[3] = 'key_list^' + object_id;
	a_params[4] = 'silent^yes';
	a_params[5] = 'object_name^' + operation + '_collection';

	ajax_get_data(a_params,'collection_result');
					
	return;	
}			

function save_all_to_collection(select_id,collection_action,entity_name,key_list)
{			
	var collection_id = DWRUtil.getValue('collection_id');
	if (collection_id < 1)
	{
		alert('Please select a collection first');
		return;
	}

	if (typeof(key_list) == 'undefined')
	{
		key_list = build_key_list(select_id);	
	}
	
	var a_params = new Array();	

	a_params[0] = 'ajax_root^site';
	a_params[1] = 'entity_name^' + entity_name;
	a_params[2] = 'collection_id^' + collection_id;
	a_params[3] = 'key_list^' + key_list;
	a_params[4] = 'object_name^' + collection_action + '_collection';
	
	ajax_get_data(a_params,'collection_result');
	
	toggle_collection_images(select_id,collection_action);
	
	return;	
}	


function load_collection()
{
	if (document.getElementById('collection_id').value > '0')
	{
		 document.getElementById('load_collection').value = 'yes';
		 document.getElementById('do_search').value = 'yes';
		 document.getElementById('set_user_selections').value = 'yes';
		 return true;
	}
	else
	{
		alert('Please select a collection to load');
		return false;
	}
}		

function change_collection()
{
	if (document.getElementById('collection_id').value < '1')
	{
		document.getElementById('load_collection').value = 'no';
	}
	document.getElementById('do_search').value = 'yes';
	document.getElementById('set_user_selections').value = 'yes';
}

function toggle_collection_images(select_id,collection_action,record_count)
{
	var key = '';
	var box;
	
	var loop_to;
	
	if (typeof(record_count) == 'undefined')
	{
		loop_to = 10000;	
	}
	else
	{
		loop_to = record_count	
	}
	
	for (j= 1; j < loop_to; j++)
	{
		try
		{
			
			box = document.getElementById(select_id + j); 
			key = box.value;
			if (box.checked)
			{
				try
				{	image = document.getElementById('collect_'+j);
					if (collection_action == 'add_to')
					{
						image.src = _DBSVARS.request.imagesroot + '//full_basket_no_border.gif';
						image.style.display='';
					}
					else
					{
						image.src = _DBSVARS.request.imagesroot + '//empty_basket_no_border.gif';	
						image.style.display='';				
					}	
				}catch(e){}
			}
		}
		catch(e)
		{
			if (typeof(record_count) == 'undefined')
			{
				break;
			}
		}
	}
}

function show_new_collection()
{
	document.getElementById('new_collection').style.display = '';
	document.getElementById('choose_collection').style.display = 'none';
}

function hide_new_collection()
{
	document.getElementById('new_collection').style.display = 'none';
	document.getElementById('choose_collection').style.display = '';
}

function save_new_collection(entity_name)
{

var a_params = new Array();	
	var collection_name = trim_string(document.getElementById('new_collection_name').value);
	var shared_flag = document.getElementById('share_new_collection').checked;
	if ( collection_name == '')
	{
		alert('Please enter a collection name.');
		return;
	}
	else
	{
		a_params[0] = 'ajax_root^site';
		a_params[1] = 'entity_name^' + entity_name;
		a_params[2] = 'collection_name^' + collection_name;
		a_params[3] = 'shared_flag^' + shared_flag;
		a_params[4] = 'object_name^new_collection';
		a_params[5] = 'follow_on_function^load_new_collection' + collection_name + ')';
		ajax_get_data(a_params,'collection_result');	
		return;	
	}		 	
}
	
function collection_result(r)
{
	if (report_ajax_errors(r)==false)
	{
		var return_message = '';
		var status = '';
		var message = '';
		var collection_id = '';
		var o_option = '';
		var len = 0;
		var recordcount = 0;
	
			for (i= 0; i < r.length; i++)
		{					
					
			return_message = return_message + r[i].KEY + ' = ' + r[i].VALUE + '\n';
					
			if (r[i].KEY == 'status')
			{
				status = r[i].VALUE; 
			}
			
			if (r[i].KEY == 'message')
			{
				message = r[i].VALUE; 
			}	
			
			if (r[i].KEY == 'collection_id')
			{
				collection_id = r[i].VALUE;	
			}	
		}

	if (status == '')
		{
			alert(return_message); 
		}
		else if (message != '')
		{
			alert(message);
		}
		if (collection_id > 0)
		{
			o_option = new Option(document.getElementById('new_collection_name').value,collection_id);
			len = document.getElementById('collection_id').options.length;

			document.getElementById('collection_id').options[len] = o_option;
			document.getElementById('collection_id').selectedIndex = len;
			
			try
			{
				recordcount = document.getElementById('query_record_count').value;
			}
			catch(e){}
			
			for (i= 0; i < recordcount; i++)
			{
				j= i+1;					
				try
				{
					image = document.getElementById('collect_'+j);
					image.src = _DBSVARS.request.imagesroot + '//empty_basket_no_border.gif';	
					image.style.display='';				
				}
				catch(e){}
			}	
			if (document.getElementById('new_collection').style.display == '')
			{
				perform_action('SEARCH');
				return;
			}
			document.getElementById('new_collection').style.display = 'none';
			document.getElementById('choose_collection').style.display = '';
		}
	}	
}

function get_value_from_ajax_response(r,var_name)
{
	
	var_name = var_name.toUpperCase();
	
	for (i= 0; i < r.length; i++)
	{
		k = r[i].KEY;
		v = r[i].VALUE;
		k = k.toUpperCase();
		
		while(''+k.charAt(0)==' ')
			{k = k.substring(1,k.length); }			
		while(''+k.charAt(k.length-1)==' ')
			{k = k.substring(0,k.length-1); }
			
		while(''+v.charAt(0)==' ')
			{v = v.substring(1,v.length); }			
		while(''+v.charAt(v.length-1)==' ')
			{v = v.substring(0,v.length-1); }
		
		if ( k == var_name)
		{
			return v;	
		}
	}
	
	return '';
}