/*
* name: fsnm.search.generic.js
* author: RAJF
* notes: Generic functions used by multiple scripts
* Do not edit below the config block without re-testing search interface and results.
*/

/*
* ## CONFIG ##
*/
// For development purposes the request is run through a .net web service proxy.
// Set to 0 when on live. Acts as switch for URL encoding through proxy.
var _isProxied = 1;
/*
* ## END CONFIG ##
*/

// code for proxy, will need to be adapted when on the live environment.
function handleRequest(serviceUrl, onSuccess)
{
	if(_isProxied)
	{
		serviceUrl = urlencode(serviceUrl);
	}
	
	$.ajax({
		type: "POST",
		url: "/SearchConnect.jsp?searchUrl=" + serviceUrl,
		data: "Url="+serviceUrl,
		beforeSend: function(xhr) { 
			xhr.setRequestHeader("Content-Length", ("Url="+serviceUrl).length);
		},
		dataType: "xml",
		error: function(){
				alert("Sorry, the search service has encountered a problem. Please refresh the page and try again.");
		},
		success: function(xmlDataSet)
		{			
			onSuccess(xmlDataSet, serviceUrl);
		}
	});
}

// Does what it says on the tin
function urlencode(str) {
	str = escape(str);
	str = str.replace('+', '%2B');
	str = str.replace('%20', '+');
	str = str.replace('*', '%2A');
	str = str.replace('/', '%2F');
	str = str.replace('@', '%40');
	return str;
}

// Initializes a new instance of the StringBuilder class and appends the given value if supplied
function StringBuilder(value)
{
    this.strings = new Array("");
    this.append(value);
}

// Appends the given value to the end of this instance.
StringBuilder.prototype.append = function (value)
{
    if (value)
    {
        this.strings.push(value);
    }
}

// Clears the string buffer
StringBuilder.prototype.clear = function ()
{
    this.strings.length = 1;
}

// Converts this instance to a String.
StringBuilder.prototype.toString = function ()
{
    return this.strings.join("");
}

// Create hash table pseudo class
function Hash()
{
	this.length = 0;
	this.items = new Array();
	for (var i = 0; i < arguments.length; i += 2) {
		if (typeof(arguments[i + 1]) != 'undefined') {
			this.items[arguments[i]] = arguments[i + 1];
			this.length++;
		}
	}
   
	this.removeItem = function(in_key)
	{
		var tmp_value;
		if (typeof(this.items[in_key]) != 'undefined') {
			this.length--;
			var tmp_value = this.items[in_key];
			delete this.items[in_key];
		}
	   
		return tmp_value;
	}

	this.getItem = function(in_key) {
		return this.items[in_key];
	}

	this.setItem = function(in_key, in_value)
	{
		if (typeof(in_value) != 'undefined') {
			if (typeof(this.items[in_key]) == 'undefined') {
				this.length++;
			}

			this.items[in_key] = in_value;
		}
	   
		return in_value;
	}

	this.hasItem = function(in_key)
	{
		return typeof(this.items[in_key]) != 'undefined';
	}
}

// log to firebug console
function debug(message)
{
	if (window.console) console.log(message);
}