// Infoserve wrapper around the autosuggest.js
// Provide special behaviours including:
// - Grouping of record types in what field
// - Location interaction between what and where
// - Setting county and geometric values from where box
// - Setting category values from what box
// 

// Dialog box showing the hidden values
function showValues() {
	var msg = "Values passed to backend:\n"
	msg += "where: "+document.form1.where.value+"\n";
	msg += "what: "+document.form1.what.value+"\n";
	msg += "preferredTerm: "+document.form1.preferredTerm.value+"\n";
	msg += "new_search: "+document.form1.new_search.value+"\n";
	msg += "category_id: "+document.form1.category_id.value+"\n";
	msg += "where_type: "+document.form1.where_type.value+"\n";
	msg += "grid: "+document.form1.grid.value+"\n";
	msg += "locality: "+document.form1.locality.value+"\n";
	msg += "county: "+document.form1.county.value+"\n";
	alert(msg);
}

// When a what entry is selected update the hidden category and type fields
// If its a non-multi location company result, update the where fields
// But only if the locality changed, otherwise we loose the county
// and grid from the selected locality
function onActivateWhat(input, row) {
	var value = "";
	var type = row[8];
	var parts = type.split("|");
	type = parts[0];
	input.form.preferredTerm.value = type;
	switch (type) {
		case '0':
			value = row[0];
			input.form.category_id.value = row[2];
			break;
		case '2':
			value = row[0];
			break;
		case '3':
		case '4':
			value = row[4];
			var locality = row[5];
			if (locality != "Multi-Location" && locality!=input.form.locality.value){
				//alert("replacing "+input.form.locality.value+" with "+locality)
				input.form.where.value = locality;
				input.form.locality.value = locality;
				input.form.grid.value = row[6];
				input.form.county.value = "";
				input.form.where_type.value = "";
			}
			break;
	}
	input.value = value;	
	input.form.where.focus();
	input.form.where.select();
	return false;
}

// When a where entry is selected update the hidden fields
// with grid value, locality and county values
function onActivateWhere(input, row) {
	input.value = row[0];
	autoPopulateWhere(input,row);
	input.form.where.focus();
	input.form.where.select();
	return false;
}

// Clear the hidden where fields
// This should be done whenever the where field is set to empty
function clearHiddenWhereFields() {
	document.form1.grid.value       = "";
	document.form1.where_type.value = "";
	document.form1.locality.value   = "";
	document.form1.county.value     = "";
}

// Extract the location, county and grid values from the where row
function autoPopulateWhere(input, row) {
	input.form.grid.value       = row[4];
	input.form.where_type.value = type;
	var type = row[2];
	if (type=="0") {
		// county
		input.form.locality.value = "";
		input.form.county.value   = row[0];
	} else if (type=="1") {
		// locality
		input.form.locality.value = row[0];
		input.form.county.value   = row[1];
    } else if (type=="2") {
		// postcode
		input.form.locality.value = "";
		input.form.county.value = row[1];
    } else if (type=="3") {
		// postcode prefix
		input.form.locality.value = row[1];
		input.form.county.value   = "";
    } else {
		input.form.locality.value = "";
		input.form.county.value   = "";
	}
}

// When we auto trigger the where suggest we don't want to autopopulate
// the fields with the top result, we want to keep the last selection
function focusTriggerWhereSuggest() {
	var input = document.form1.where;
	document.form1.supressAutoPopulate.value = 1;
	// TODO: find a solution to replace this without doing an endless loop
	//input.suggest();
	document.form1.supressAutoPopulate.value = 0;
}

// By default use values from the first suggested where result
// These are used for the geometric, county and locality fields
// Note that we only take the first row if another row wasn't already selected
function preWhere(input, rows) {
	var suppress = input.form.supressAutoPopulate.value;
	if (suppress=="0" && rows.length > 0) {
		row = rows[0];
		autoPopulateWhere(input,row);
	}
	return rows;
}

// Get the value for the node
function infoserveGetNodeValue(row) {
	var type = row[8];
	var parts = type.split("|");
	if (parts.length==0) {
		return "Unknown Type";
	}
	type = parts[0];
	switch (type) {
		case '0':
			return row[0];
		case '2':
			return row[0];
		case '3':
		case '4':
			return row[4];
		default:
			return "Unknown Type";
	}
}

// Get the display data as keyword and category
// We use a triple forward-slash separator
function infoserveGetNodeString(row) {
	var type = row[8];
	var parts = type.split("|");
	if (parts.length==0) {
		return "Unknown Type";
	}
	type = parts[0];
	switch (type) {
		case '0':
			return row[0]+"///"+row[2];
		case '2':
			return row[0]+"///";
		case '3':
		case '4':
			return row[4]+"///"+row[5];
		default:
			return "Unknown Type///";
	}
}

// Returns the group name for 'what' field row types
function infoserveGetGroupName(row) {
	var type = row[8];
	var parts = type.split("|");
	if (parts.length==0) {
		return "Unknown Type";
	}
	type = parts[0];
	switch (type) {
		case '0':
			return "Product or Service";
		case '2':
			return "Business Type";
		case '3':
		case '4':
			return "Business Name";
		default:
			return "Unknown Type "+type;
	}
}

function getLocality() {
	var locality = document.form1.locality.value;
	return locality;
}

function getGrid() {
	var grid = document.form1.grid.value;
	return grid;
}

function setupFrontEnd() {

       SetupMMSuggest(document.form1.where,
		{ 'requestURL': "http://search.infoserve.com/mmsuggest2.asp?mm_port=20003&max_results=25&keywords=$v$&query=$v$"
		, 'searchDelay': 200
		, 'width': 200
		, 'align': 'left'
		// use this parameter to automatically hide any SELECT objects that may be displayed above the suggest by IE
		//, 'overlappedSelects': new Array(document.form1.select)
		, 'flags': AS_NOLOGO + AS_DISPLAY_INPUT + AS_NOSUBMIT
		, 'inputTitle': "Your input"
		, 'onActivate': onActivateWhere
    , 'normalfg': "#092A57"
	  , 'normalbg': "#E5F2F7"
	  , 'highlightfg': "white"
	  , 'highlightbg': "navy"
    , 'debug' : ""
    , 'preFunction': preWhere
    });

        SetupMMSuggest(document.form1.what,
		{'requestURL': "http://search.infoserve.com/mmsuggest2.asp?mm_port=20002&max_results=25&keywords=$v$&query=$v$&grid=$getGrid()$"
		, 'searchDelay': 200
		, 'width': 400
		, 'align': 'left'
		// use this parameter to automatically hide any SELECT objects that may be displayed above the suggest by IE
		//, 'overlappedSelects': new Array(document.form1.select)
		, 'flags': AS_NOLOGO + AS_DISPLAY_INPUT + AS_NOSUBMIT + AS_GROUPED + AS_GENERATED_CATEGORIES_NOT_SELECTABLE + AS_NO_ROOT
		, 'inputTitle': "Your input"
		, 'onActivate': onActivateWhat
    , 'normalfg': "#092A57"
	  , 'normalbg': "#E5F2F7"
	  , 'highlightfg': "white"
	  , 'highlightbg': "navy"
    , 'debug': ""
    });
       
       clearHiddenWhereFields();
       
       document.form1.supressAutoPopulate.value = 0;
       document.form1.where.onfocus = focusTriggerWhereSuggest;
       //document.form1.what.onfocus  = document.form1.what.suggest;
       
       document.form1.what.focus();
}
