// Copyright Dalbrook Limited 2006
// All rights reserved

var xml_quotes_http;

function quotes_database_widget (body_id) {
	xml_quotes_http = new_request_object();
	page_url = 'get_quote.php?context=' + body_id
    open_request(xml_quotes_http, page_url, quotes_state_change);
}

function quotes_state_change () {
	if (xml_quotes_http.readyState == 4) {
		if (xml_quotes_http.status == 200) {
			quotes_results();
		}
	}
}

function quotes_results() {
	var xml_doc = xml_quotes_http.responseText;
	var out_element = document.getElementById('quotes_out');
	var start_string = "<h5 id='page_header'>";
	var end_string = "</h5>";
	display_results(xml_doc, out_element, start_string, end_string);
}

function display_results(xml_doc, out_element, start_string, end_string) {
	var table_start = xml_doc.indexOf(start_string);
	var table_contents = xml_doc.substring(table_start);
	var table_end = table_contents.indexOf(end_string) + end_string.length;
	var i;
	var pattern;
	var substitutes = [];
	
	substitutes[0] = []
	substitutes[0][0] = 'APOSTROPHE';
	substitutes[0][1] = '&#039;';
	
	substitutes[0] = []
	substitutes[0][0] = 'POUND';
	substitutes[0][1] = '&#163;';
	
	table_contents = table_contents.substring(0,table_end);
	
	for (i=0; i<substitutes.length; i++) {
		pattern = new RegExp(substitutes[i][0],"g");
		table_contents = table_contents.replace(pattern, substitutes[i][1]);
	}
	
	out_element.innerHTML = table_contents;
}
