G_autoSuggest['comp'] = new Array();
G_autoSuggest['loc'] = new Array();
G_autoSuggest['bustype'] = new Array();

G_autoSuggest['compnameID'] = '0';
G_autoSuggest['localityID'] = '0';
G_autoSuggest['bustypeID'] = '0';

/**
 * Company search results
 */
G_autoSuggest['comp']['callId'] = 0;
G_autoSuggest['comp']['timeoutdelay'] = 333;
G_autoSuggest['comp']['cache'] = new Array();

function CompSuggestions() {
	this.nameId = 'compnameID';
}

CompSuggestions.prototype.requestSuggestions = function(oAutoSuggestControl) {
	this.control = oAutoSuggestControl;
	
	if(!G_autoSuggest['comp']['cache'][G_autoSuggest['bustypeID']+','+G_autoSuggest['localityID']]) {
		G_autoSuggest['comp']['cache'][G_autoSuggest['bustypeID']+','+G_autoSuggest['localityID']] = new Array();
	}
	
	G_autoSuggest['comp']['callId']++;
	callId = G_autoSuggest['comp']['callId'];
	oThis = this;
	
	if(G_autoSuggest['comp']['cache'][G_autoSuggest['bustypeID']+','+G_autoSuggest['localityID']][this.control.textbox.value]) {
		setTimeout('oThis.fetchSuggestionsCache(\''+callId+'\')', G_autoSuggest['comp']['timeoutdelay']);
	} else {
		setTimeout('oThis.fetchSuggestions(\''+callId+'\')', G_autoSuggest['comp']['timeoutdelay']);
	}
}

CompSuggestions.prototype.fetchSuggestions = function(callId) {
	if(callId == G_autoSuggest['comp']['callId']) {
		this.searchWords = this.control.textbox.value;

		this.get = '?lookup=county&search='+this.searchWords;
		this.url = '_includes/autosuggest.php';
	
		apilite_post(this.url+this.get, '', 'fetchCompSuggestionsOK', this.control, 'fetchCompSuggestionsFailed', this.control);
	}
}

CompSuggestions.prototype.fetchSuggestionsCache = function(callId) {
	if(callId == G_autoSuggest['comp']['callId']) {
		var lines = G_autoSuggest['comp']['cache'][G_autoSuggest['bustypeID']+','+G_autoSuggest['localityID']][this.control.textbox.value];
		
		displayCompKeywords(this.control, lines)
	}
}

function fetchCompSuggestionsOK(response, oAutoSuggestControl) {	
	response = response.split("\n");
	if(response[0] != '') {
		response.pop();
		if(response.length > 0) {
			displayCompKeywords(oAutoSuggestControl, response);
			return;
		}
	}
	oAutoSuggestControl.hideSuggestions();
}

function fetchCompSuggestionsFailed(response, oAutoSuggestControl) {
	oAutoSuggestControl.hideSuggestions();
}


/*
 * If you need to contain the values in Divs id them as asRowContent_(X) where X is anything unique.
 * Make sure a div containing the selected value is at the end of the string, usually hidden
 */
function displayCompKeywords(oAutoSuggestControl, lines) {
	var aSuggestions = new Array();
	this.control = oAutoSuggestControl;
	
	if(this.control.textbox.value != '') {
		G_autoSuggest['comp']['cache'][G_autoSuggest['bustypeID']+','+G_autoSuggest['localityID']][oAutoSuggestControl.textbox.value] = new Array();
	}
	
	if(lines) {
		for(var i = 0; i < lines.length; i++) {

			var aLine = lines[i].split(',');
			var sCompanyId = aLine.pop();
			var sResultsCache = aLine.join(); //shift();
//			var sCompanyId = aLine.shift();
			var sSuggestion = '' //aLine.join(',');
					
			if(this.control.textbox.value != '') {G_autoSuggest['comp']['cache'][G_autoSuggest['bustypeID']+','+G_autoSuggest['localityID']][oAutoSuggestControl.textbox.value].push(lines[i]);}
			
			if(sResultsCache.indexOf('.') != -1) {
				var sResults = sResultsCache.split('.')[1];
				var sResultsText = (sResults > 1) ? 'matches' : 'match';
			} else {
				var sResults = sResultsCache;
				var sResultsText = (sResults > 1) ? 'branches' : 'branch';
			}
			
			var sSuggestionDis = (sCompanyId == 'compnameID=0') ? '<span class="multiMatchesRow" id="suggestMM_'+oAutoSuggestControl.layerId+i+'">'+sSuggestion+'</span>' : sSuggestion;
			
			var sSuggestionDiv = sSuggestionDis+'<div class="cs_srdRowRes" id="suggestContentRes_'+oAutoSuggestControl.layerId+i+'">'+sResults+'</div><div class="hide" id="suggestion_'+oAutoSuggestControl.layerId+i+'">'+sSuggestion+','+sCompanyId+'</div>';
						
			aSuggestions.push(sSuggestionDiv);
		}
	}
	
	oAutoSuggestControl.autosuggest(aSuggestions);
}

CompSuggestions.prototype.handleOnFocus = function(oAutoSuggestControl) {
// 	if(document.getElementById('coLocation').value != '' || document.getElementById('serviceType').value != '') {
// 		CompSuggestions.prototype.requestSuggestions(oAutoSuggestControl);
// 	}
	// No coLocation so no need to check if it contains a value
	if(document.getElementById('serviceType').value != '') {
		CompSuggestions.prototype.requestSuggestions(oAutoSuggestControl);
	}
}

CompSuggestions.prototype.prefilCompDetails = function(oAutoSuggestControl) {
	this.compnameId = G_autoSuggest['compnameID'];
	this.get = '?compname_id='+this.compnameId;
	this.url = 'includes/ajax/_search_comp_details.php';
	apilite_post(this.url+this.get, '', 'prefilCompDetailsOK', '', 'prefilCompDetailsOK', '');
}

function prefilCompDetailsOK(response, args) {
	var strData = response.split("\n")[0];	// Take first response no matter what
	var aCompDetails = new Array();
	var sQuery = '', sUrl = '';
	
	while(strData.length > 0) {
		var matches = strData.match(/^(\w+)\((\d+|)\)(.+)/);
		
		aCompDetails[matches[1]] = matches[3].substr(0,matches[2]);
		
		strData = matches[3].substr(matches[2]);
		
		// alert(matches[1]+','+aCompDetails[matches[1]]+"\n\n"+strData);
	}
		
	sQuery = '?bustype=&compname_id='+G_autoSuggest['compnameID']+'&locality_id='+G_autoSuggest['localityID'];
	sUrl = 'includes/ajax/_search_bustype.php';
		
	apilite_post(sUrl+sQuery, '', 'prefilCompBusiness', aCompDetails, 'prefilCompBusiness', aCompDetails);	
	
}

function prefilCompBusiness(response, aCompDetails) {
	oBus = document.getElementById('serviceType');
	sBus = response.split(',')[2];
	
	// The business type
	oBus.value = sBus;
	
	// Let's prefil some company details, shall we?
	document.getElementById('webcardId').value = aCompDetails['wcID'];
	document.getElementById('street').value = aCompDetails['ad1'];
	document.getElementById('postcode').value = aCompDetails['pc'];
	document.getElementById('phone').value = aCompDetails['ph'];
	// document.getElementById('faxNo').value = aCompDetails['fa'];
	document.getElementById('email').value = aCompDetails['em'];
	// document.getElementById('website').value = aCompDetails['www'];
}

/**
 * Locality search results
 */

G_autoSuggest['loc']['callId'] = 0;
G_autoSuggest['loc']['timeoutdelay'] = 333;
G_autoSuggest['loc']['cache'] = new Array();

function LocationSuggestions() {
	this.nameId = 'localityID';
}
 
LocationSuggestions.prototype.requestSuggestions = function(oAutoSuggestControl) {
	this.control = oAutoSuggestControl;
	
	if(!G_autoSuggest['loc']['cache'][G_autoSuggest['bustypeID']+','+G_autoSuggest['compnameID']]) {
		G_autoSuggest['loc']['cache'][G_autoSuggest['bustypeID']+','+G_autoSuggest['compnameID']] = new Array();
	}
	
	G_autoSuggest['loc']['callId']++;
	callId = G_autoSuggest['loc']['callId'];
	oThis = this;
	
	if(G_autoSuggest['loc']['cache'][G_autoSuggest['bustypeID']+','+G_autoSuggest['compnameID']][this.control.textbox.value]) {
		setTimeout('oThis.fetchSuggestionsCache(\''+callId+'\')', G_autoSuggest['loc']['timeoutdelay']);
	} else {
		setTimeout('oThis.fetchSuggestions(\''+callId+'\')', G_autoSuggest['loc']['timeoutdelay']);
	}
}

LocationSuggestions.prototype.fetchSuggestions = function(callId) {
	if(callId == G_autoSuggest['loc']['callId']) {
		this.searchWords = this.control.textbox.value;
		this.get = '?lookup=town&search='+this.searchWords;
		this.url = '_includes/autosuggest.php';
		apilite_post(this.url+this.get, '', 'fetchLocSuggestionsOK', this.control, 'fetchLocSuggestionsFailed', this.control);
	}
}

LocationSuggestions.prototype.fetchSuggestionsCache = function(callId) {
	if(callId == G_autoSuggest['loc']['callId']) {
		var lines = G_autoSuggest['loc']['cache'][G_autoSuggest['bustypeID']+','+G_autoSuggest['compnameID']][this.control.textbox.value];
		
		displayLocKeywords(this.control, lines)
	}
}

function fetchLocSuggestionsOK(response, oAutoSuggestControl) {
	response = response.split("\n");
	if(response[0] != '') {
		response.pop();
		if(response.length > 0) {
			displayLocKeywords(oAutoSuggestControl, response);
			return;
		}
	}
	oAutoSuggestControl.hideSuggestions();
}

function fetchLocSuggestionsFailed(response, oAutoSuggestControl) {
	oAutoSuggestControl.hideSuggestions();
}

function displayLocKeywords(oAutoSuggestControl, lines) {
	var aSuggestions = new Array();
	this.control = oAutoSuggestControl;
	
	if(this.control.textbox.value != '') {
		G_autoSuggest['loc']['cache'][G_autoSuggest['bustypeID']+','+G_autoSuggest['compnameID']][oAutoSuggestControl.textbox.value] = new Array();
	}
	for(var i = 0; i < lines.length; i++) {
		var aLine = lines[i].split(',');
		var sLocationId = aLine.pop();

		var sResultsCache = aLine.join();
//		var sLocationId = aLine.shift();
		var sSuggestion = '' //aLine.join(',');
		
		if(this.control.textbox.value != '') {G_autoSuggest['loc']['cache'][G_autoSuggest['bustypeID']+','+G_autoSuggest['compnameID']][oAutoSuggestControl.textbox.value].push(lines[i]);}
		
		var sResults = sResultsCache;
		var sResultsText = (sResults > 1) ? 'branches' : 'branch';
		
		var sSuggestionDiv = sSuggestion+'<div class="cs_srdRowRes" id="suggestContentRes_'+oAutoSuggestControl.layerId+i+'">'+sResults+'</div><div class="hide" id="suggestion_'+oAutoSuggestControl.layerId+i+'">'+sSuggestion+','+sLocationId+'</div>';
		
		aSuggestions.push(sSuggestionDiv);
	}
	
	oAutoSuggestControl.autosuggest(aSuggestions);
}

LocationSuggestions.prototype.handleOnFocus = function(oAutoSuggestControl) {
	if(document.getElementById('companyName').value != '' || document.getElementById('serviceType').value != '') {
		LocationSuggestions.prototype.requestSuggestions(oAutoSuggestControl);
	}
}

G_autoSuggest['bustype']['callId'] = 0;
G_autoSuggest['bustype']['timeoutdelay'] = 333;
G_autoSuggest['bustype']['cache'] = new Array();

function BusTypeSuggestions() {
	this.nameId = 'bustypeID';
}

BusTypeSuggestions.prototype.requestSuggestions = function(oAutoSuggestControl) {
	this.control = oAutoSuggestControl;
	
	if(!G_autoSuggest['bustype']['cache'][G_autoSuggest['localityID']+','+G_autoSuggest['compnameID']]) {
		G_autoSuggest['bustype']['cache'][G_autoSuggest['localityID']+','+G_autoSuggest['compnameID']] = new Array();
	}
	
	G_autoSuggest['bustype']['callId']++;
	callId = G_autoSuggest['bustype']['callId'];
	oThis = this;
	
	if(G_autoSuggest['bustype']['cache'][G_autoSuggest['localityID']+','+G_autoSuggest['compnameID']][this.control.textbox.value]) {
		setTimeout('oThis.fetchSuggestionsCache(\''+callId+'\')', G_autoSuggest['bustype']['timeoutdelay']);
	} else {
		setTimeout('oThis.fetchSuggestions(\''+callId+'\')', G_autoSuggest['bustype']['timeoutdelay']);
	}
}

BusTypeSuggestions.prototype.fetchSuggestions = function(callId) {
	if(callId == G_autoSuggest['bustype']['callId']) {
		this.searchWords = this.control.textbox.value;
		this.get = '?lookup=business&search='+this.searchWords;
		this.url = '_includes/autosuggest.php';
		//alert(this.url+this.get);
		//return;
		
		apilite_post(this.url+this.get, '', 'fetchBusTypeSuggestionsOK', this.control, 'fetchBusTypeSuggestionsFailed', this.control);
	}
}

BusTypeSuggestions.prototype.fetchSuggestionsCache = function(callId) {
	if(callId == G_autoSuggest['bustype']['callId']) {
		var lines = G_autoSuggest['bustype']['cache'][G_autoSuggest['localityID']+','+G_autoSuggest['compnameID']][this.control.textbox.value];
		
		displayBusTypeKeywords(this.control, lines)
	}
}

function fetchBusTypeSuggestionsOK(response, oAutoSuggestControl) {
	response = response.split("\n");
	if(response[0] != '') {
		response.pop();
		if(response.length > 0) {
			displayBusTypeKeywords(oAutoSuggestControl, response);
			return;
		}
	}
	oAutoSuggestControl.hideSuggestions();
}

function fetchBusTypeSuggestionsFailed(response, oAutoSuggestControl) {

	oAutoSuggestControl.hideSuggestions();
}

function displayBusTypeKeywords(oAutoSuggestControl, lines) {
	var aSuggestions = new Array();
	this.control = oAutoSuggestControl;
	
	if(this.control.textbox.value != '') {
		G_autoSuggest['bustype']['cache'][G_autoSuggest['localityID']+','+G_autoSuggest['compnameID']][oAutoSuggestControl.textbox.value] = new Array();
	}
	
	for(var i = 0; i < lines.length; i++) {
		var aLine = lines[i].split(',');
		var sBusTypeId = aLine.pop();

		var sResultsCache = aLine.join();
//		var sBusTypeId = aLine.shift();
		var sSuggestion = '' //aLine.join(',');
						
		if(this.control.textbox.value != '') {G_autoSuggest['bustype']['cache'][G_autoSuggest['localityID']+','+G_autoSuggest['compnameID']][oAutoSuggestControl.textbox.value].push(lines[i]);}
		
		var sResults = sResultsCache;
		var sResultsText = (sResults > 1) ? 'companies' : 'company';
		
		var sSuggestionDiv = sSuggestion+'<div class="cs_srdRowRes" id="suggestContentRes_'+oAutoSuggestControl.layerId+i+'">'+sResults+'</div><div class="hide" id="suggestion_'+oAutoSuggestControl.layerId+i+'">'+sSuggestion+','+sBusTypeId+'</div>';
		
		aSuggestions.push(sSuggestionDiv);
	}
	
	oAutoSuggestControl.autosuggest(aSuggestions);
}

BusTypeSuggestions.prototype.handleOnFocus = function(oAutoSuggestControl) {
	if(document.getElementById('mktsec_search').value != '') {
		BusTypeSuggestions.prototype.requestSuggestions(oAutoSuggestControl);
	}
}