﻿function pageSearch()
{
	this.searchElement = null;

	this.generate = function(elementID) {
		if ($(elementID))
		{
			this.searchElement = $(elementID);
			var phrase = this.searchElement.value;
			if (phrase.length > 0) {
				var replacedurl = 'search_results.html?quest=' + phrase + '&element=' + elementID;
				setTimeout(function()	{ window.location = replacedurl; }, 0);
			}
		}
	}

	this.getResults = function(phrase, elementID, notfoundtext) {

		$(elementID).value = phrase;

		var req = new Ajax('/cgi-bin/search.cgi', {
			method: 'get',
			data: { 'operator' : operator, 'phrase' : phrase },
			onComplete: function(response)
			{
				var script = response.match(/<script\s+type\s*=\s*\"text\/javascript\">([\s\S]+?)<\/script>/gi);
				if (script != null)
				{
					for (i = 0; i < script.length; i++)
					{
						script[i] = script[i].replace(/<[\/]?script.*?>/g, '');
						if (navigator.appName != 'Microsoft Internet Explorer')
						{
							eval(script[i]);
						}
					}
				}
				this.gererateOutput(response, phrase, elementID);
			}.bind(this)
		}).request();
	}

	this.gererateOutput = function(response, phrase, elementID) {
		var resultDiv = $('search_results');

		if (response == 'false') {
		
				resultDiv.innerHTML = notfoundtext;

		}
		else {
			resultDiv.innerHTML = response;
		}

	}
};

var pageSearch = new pageSearch();
