//
// Original author/genius :
//
// Dunstan Orchard - 1976design.com/blog/
//
// http://creativecommons.org/licenses/by-nc-sa/1.0/
//

// define all the starting variables
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// id of the search form
var searchFormId = 'new-project-req'; 

// id of the search form input field
var searchInputId = 'projectName';

// search form input messages
var searchInputMessage1 = '';

// id of the div we wish to create and display the results in
var resultsDivId = 'projectNameTest';

// id of the object we wish to append the results div to
var resultsDivParentId = 'xproject';

// php file which will produce the results
var processURI = '/g/exists.php';

// set intial vars
var liveSearchReq = false;
var keyPressDelay = null;
var liveSearchLast = '';
var isIE = false;
// on IE we only have to initialize it once
if (window.XMLHttpRequest) {liveSearchReq = new XMLHttpRequest();}

// set the timing vars
var processTimerCount = 0;
var timedOut = 0;



var ips = ['projectName', "email", 'userName', "contrib-email-0", "contrib-email-1", "contrib-email-2"];

var a_ips = ['projectDescription', 'name', 'contrib-name-0', 'contrib-name-1', 'contrib-name-2'];

var parent_child = new Array();

parent_child['projectName'] = 'xproject';
parent_child["email"] = "vemail";
parent_child['userName'] = 'xuser';
parent_child["contrib-email-0"] = "c0";
parent_child["contrib-email-1"] = "c1";
parent_child["contrib-email-2"] = "c2";



// prep functions
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// create the results div _once_ on page load
// (this function is called from an onload function)
function runLSPrep()
	{
	// set vars to simplify code
	var resultsDivParent = $(resultsDivParentId);
	var searchForm = $(searchFormId);
	var searchInput = $(searchInputId);
	var scratch, a, b;
	
	// event listener for the escape key being pressed anywhere
	addEvent(document, 'keydown', escapeReset, false);

	for(var i = 0; i < ips.length; i++) {
		scratch = ips[i];

		searchInput = $(scratch);
		resultsDivId = scratch + 'Test';
		resultsDivParent = $(parent_child[scratch]);

		// disable autocomplete on the search box
		searchInput.setAttribute('autocomplete', 'off');

		searchInput.className = 'ajaxInput';

		a = ' inl'; b = 'span';//'div';
	//	if(resultsDivId.match(/email/)) { a = ' inl'; b = 'span'; }

		// create the div which will hold the results
		var resultsDiv = document.createElement(b);
		// set its id
		resultsDiv.className = 'ajaxBox' + a;
		resultsDiv.id = resultsDivId;
	//	resultsDiv.style.display = 'none;
		// append to parent
		resultsDivParent.appendChild(resultsDiv);
	

		// event listener for hiding the search field result boxen
		addEvent(searchInput, 'focus', liveSearchHide, false);

		// event listener for starting the search
		addEvent(searchInput, 'keyup', liveSearchStart, false);

		// event listener for resetting things on blur
		addEvent(searchInput, 'blur', resetOnBlur, false);
		}

	for(i = 0; i < a_ips.length; i++) {

		searchInput = $(a_ips[i]);
		// event listener for hiding the search field result boxen
		addEvent(searchInput, 'focus', liveSearchHide, false);
		}

	}



// livesearch functions
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
function liveSearchStart(e)
	{

var kCignore;

	// if the 'escape' key has been pressed while typing in the search box
	if (e.keyCode == 27)
		{
		resetEverything();
		return false;
		}

if ( e.keyCode == 9 || e.keyCode == 13 ||
    e.keyCode == 16 || e.keyCode == 17 || e.keyCode == 35 || e.keyCode == 36 ||
    e.keyCode == 37 || e.keyCode == 38 || e.keyCode == 39 || e.keyCode == 40)
	kCignore = true;

	if(e.target)
		searchInputId = e.target.id;
	else if(e.srcElement)
		searchInputId = e.srcElement.id;

	resultsDivId = searchInputId + 'Test';

	// set vars to simplify code
	var resultsDiv = $(resultsDivId);
	var searchInput = $(searchInputId);

	// clear the keyPressDelay if it exists from before
	if (keyPressDelay)
		{
		window.clearTimeout(keyPressDelay);
		}
	
	if (searchInput.value != '')
		{
		if(resultsDiv.childNodes.length != 0 &&
		   resultsDiv.childNodes[0].textContent != ''
		) {
		   searchInput.className = 'ajaxInput';
		   resultsDiv.childNodes[0].className = 'ajaxAct';
		} else
		searchInput.className = 'ajaxActInput';

		var i, d;
		for(i=0; i < ips.length; i++) {
		d = ips[i] + 'Test';
		if($(d)) $(d).style.display = 'none';
		}

		var b = 'inline';//'block';
	//	if(resultsDivId.match(/email/)) b = 'inline';

		resultsDiv.style.display = b;
		// wait 0.8 seconds after a keypress before running the search
		keyPressDelay = window.setTimeout('_liveSearchDoSearch()', 800);
		}
	return true;
	}


function _liveSearchDoSearch() {

	var searchInput = $(searchInputId);
	var pars = 'p=' + encodeURIComponent(searchInput.value);
	var uri = processURI;

	if(resultsDivId == 'userNameTest') pars = 'u=' + searchInput.value;
	if(resultsDivId.match(/email/)) {
		pars = 'e=' + encodeURIComponent(searchInput.value); uri = '/g/vmail.php';
		}

	var myAjax = new Ajax.Updater(resultsDivId, uri, {method: 'get', parameters: pars});
	searchInput.className = 'ajaxInput';

}




// reset and clear functions
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
function resetEverything()
	{
	// set vars to simplify code
	var searchInput = $(searchInputId);
	searchInput.className = 'ajaxInput';

	liveSearchReq.abort();
	liveSearchHide();
	}

function resetOnBlur() {

	// set vars to simplify code
	var resultsDiv = $(resultsDivId);

	for(var i=0; i < ips.length; i++) {
		resultsDiv = $(ips[i]);
		if (resultsDiv.childNodes[0]) {
			if (resultsDiv.childNodes[0].className == 'ajaxOK')
			{
			liveSearchReq.abort();
			liveSearchHide();
			}
		}
	}

}

// if a key has been pressed when focus was outside the search box
function escapeReset(e)
	{
	// if the 'escape' key was been pressed
	if (e.keyCode == 27)
		{
		resetEverything();
		return false;
		}
	}

// [on]focus handler
function clearSearchText(e)
	{
	liveSearchHide();	

	}


function liveSearchHide()
	{
	// set vars to simplify code
	var i, d;
	for(i=0; i < ips.length; i++) {
		d = ips[i] + 'Test';
		$(ips[i]).className = 'ajaxInput';
		if($(d)) $(d).style.display = 'none';
		}
	}
