// Add an option to a listbox
function addOption(listbox, name, value)
{
    var defaultSelected = true;
    var selected = true;
    var optionName = new Option(name, value, defaultSelected, selected)
    var length = listbox.length;
    listbox.options[length] = optionName;
}

// Get all their engines from their cookie
function getEngines()
{
	var startName = "";
	var endName = "";
	var engineName = "";
	var engineUrl = "";
	var workingString = document.cookie;

	var tmpEngines = new Array();
	var engineIndex = 0;

	// Mark engines with "spengine.Google", for example
	startName = workingString.indexOf("spengine.");
	while(startName > -1)
	{
		// Figure out the engine name (which is the name of the cookie)
		// then get the url from that cookie
		endName = workingString.indexOf("=", startName);
		engineName = workingString.substring(startName, endName);
		engineUrl = getCookie(engineName);

		// Make a specially formatted list in the form of Engine|Url
  		engineName = replace(engineName, "spengine.", "");
		engineName = unescape(engineName);
		tmpEngines[engineIndex] = engineName + "|" + engineUrl;

		// Move on to the next engine
		// Figure out where the current one starts
		startName = workingString.indexOf("spengine.");
		engineIndex++;

		// And where the next one starts
      var endPos = workingString.indexOf("spengine.", startName + 1);
		if(endPos < 0)
			break;

		// Then chop out the current one
		workingString = workingString.substring(endPos, workingString.length);
	}

	return tmpEngines;
}

// Get a named cookie from the user
function getCookie(name)
{
	var index = document.cookie.indexOf(name + "=");
	if (index == -1) return null;
	index = document.cookie.indexOf("=", index) + 1;

	var endstr = document.cookie.indexOf(";", index);
	if (endstr == -1) endstr = document.cookie.length;

	return unescape(document.cookie.substring(index, endstr));
}

function replace(string,text,by)
{
	// Replaces text with by in string
	var strLength = string.length, txtLength = text.length;
    		if ((strLength == 0) || (txtLength == 0)) return string;

    		var i = string.indexOf(text);
    		if ((!i) && (text != string.substring(0,txtLength))) return string;
    		if (i == -1) return string;

    		var newstr = string.substring(0,i) + by;

    		if (i+txtLength < strLength)
        		newstr += replace(string.substring(i+txtLength,strLength),text,by);

    		return newstr;
}

