
var ac_mmb_compliance = (document.nav_form) ? true : false,
ac_user_audience = (ac_mmb_compliance) ? document.nav_form.audience.value : 'travel',
ac_user_login = (ac_mmb_compliance) ? document.nav_form.logintype.value : 'public',
ac_user_language = (ac_mmb_compliance) ? document.nav_form.language.value : 'en',
ac_user_country = (ac_mmb_compliance) ? document.nav_form.country.value : 'gb',
ac_results_count = 0,
ac_results_selected = 1,
ac_timeout,
ac_timeout_last = 0,
ac_parent,
ac_scroll_active = false,
ac_ie6_compliance = 1,
ac_remainder = false;
String.prototype.trim = function() { return this.replace(/^[\s]+|[\s]+$/gi,''); };
function matchAutoComplete(sample, match) {
var i,
array,
position = sample.toLowerCase().indexOf(match.toLowerCase()),
string,
text,
regex;
if (position !== -1) {
array = sample.split(',');
for (i = 0; i < array.length; i += 1) {
regex = null;
regex = new RegExp(match,'gi');
text = regex.exec(array[i]);
if (text !== null) {
string = '<span class="autoCompleteMatch">'+ text +'</span>';
array[i] = array[i].replace(regex, string);
}
}
return array.join(',');
}
else { return sample; }
}
function parseDWR_long(i, response) {
var a,b;
a = response.indexOf('s'+i+'=');
a = response.indexOf('"',a)+1;
      b = response.indexOf('"',a) - a;
return response.substr(a,b);
}
function parseDWR_short(i, response) {
var a;
if( response.indexOf('=s'+i+';') !== -1 ) {
a = response.indexOf('\']=s'+i+';');
return response.substr(a-3,3);
}
else { return false; }
}
function parseDWR_long_for_code(string) {
var array;
if (string &&string.indexOf(',') !== -1) {
array = string.split(',');
return array[2].trim();
}
else { return false; }
}
function parseOJ(response, search) {
var response_element = $(response.trim()),
response_list = response_element.children('li');
response_element.attr('id','destChoices');
response_element.find('li:first').addClass('acHighlight');
response_list.each(function() {
$(this).html(matchAutoComplete($(this).html(), search));
});
ac_results_count = response_list.length;
if (ac_ie6_compliance === 2) { ac_results_count += 1; }
return response_list;
}
function parseDWR(response, search) {
var i,
count = 2,
markup = '';
for ( i = 1; i<count; i+=1 ) {
if ( response.indexOf('s'+i+'=') !== -1 ) {
count += 1;
if ( i === 1 ) { markup += '<li id="'+ parseDWR_short(i, response) +'" class="acHighlight">'+ matchAutoComplete(parseDWR_long(i, response),search) +'</li>'; }
else { markup += '<li id="'+ parseDWR_short(i, response) +'">'+ matchAutoComplete(parseDWR_long(i, response),search) +'</li>'; }
}
}
ac_results_count = i - 2;
if (ac_ie6_compliance === 2) { ac_results_count += 1; }
return (markup !== '') ? markup : false;
}
function clearAutoComplete() {
ac_results_count = 0;
ac_results_selected = ac_ie6_compliance;
ac_scroll_active = false;
$('.ajaxResults').hide().find('ul').html('');
return true;
}
function autoComplete(input, results, service, data_type) {
var url,
val = $('#'+ input).val();
ac_timeout_last = 0;
if ( !val || val.trim() === '' || val.length < 3) { clearAutoComplete(); return false; }
url = service + val.trim() + "&language=" + ac_user_language;
$.get(url, function(response) {
var i,
results_element,
results_native,
prop,
select_diff,
y_start,
y_end,
ojId = $('#ojId'),
ojGw = $('#ojGw'),
ojCountry = $('#ojCountry');
response = response.trim();
if ( response !== '<ul></ul>' || val.indexOf('s1') !== -1 ) {
if ( data_type === 'DWRdests' || data_type === 'Ancillarydests' ) { response = parseDWR(response, val); }
else if ( data_type === 'OJdests' ) { response = parseOJ(response, val); }
if (response) {
results_element = $('#'+ results);
$('ul#destChoices', results_element).html(response);
results_element.show().children('ul#destChoices').scrollTop(0);
$('ul li', results_element).click(function() {
if (typeof makeSelection === 'function') { makeSelection('#'+input, '#'+results, data_type); return false; }
var departure_airport,
destination_airport;
$('#'+ input).val($(this).text());
if ( data_type === 'OJdests' ) {
ojId.attr('value', $(this).attr("id"));
ojGw.attr('value', $(this).attr("gw"));
ojCountry.attr('value', $(this).attr("country"));
departure_airport = $("#fromPkg > option:selected").val();
destination_airport = $(this).attr('gw');
updateCabin(departure_airport, destination_airport, ac_user_language, "dp");
updateTicket(departure_airport, destination_airport);
}
else if ( data_type === 'DWRdests' ) {
if ($('select#from').length > 0) { departure_airport = $('select#from option:selected').val(); }
else { departure_airport = parseDWR_long_for_code($('#from').val()); }
destination_airport = $(this).attr('id');
var depDateValue = $('#depDate').val();
if( depDateValue && depDateValue!='DD/MM/YY' && depDateValue!='MM/DD/YY') {
updateCabinsForDate(departure_airport, destination_airport, ac_user_language, document.nav_form.country.value, depDateValue);
}else{
updateCabin(departure_airport, destination_airport, ac_user_language);
}
updateTicket(departure_airport, destination_airport);
}
else if ( data_type === 'Ancillarydests' ) {
matchedDest = $(this).attr('id');
$("#ojGw").val(matchedDest);
}
clearAutoComplete();
return true;
});
if (ac_ie6_compliance === 2) {
$('ul#destChoices', results_element).bgiframe({height:$('ul#destChoices', results_element).get(0).scrollHeight});
}
$('ul li', results_element).mouseenter(function() {
ac_results_selected = $(this).parent('ul#destChoices').children('li').removeClass('acHighlight').index(this) + ac_ie6_compliance;
$(this).addClass('acHighlight');
});
if ((window.Touch || window.TouchEvent) && $('ul#destChoices').is(':visible')) {
results_native = document.getElementById(results).childNodes;
if ( results_native.length > 1 ) {
for ( i=0; i<results_native.length; i+=1 ) {
if ( results_native[i].id === 'destChoices' ) {
results_native = results_native[i];
break;
}
}
}
else { results_native = results_native[0]; }
results_native.ontouchmove = function(e) {
if (e.touches.length < 2) {e.preventDefault();}
prop = (e.touches && e.touches.length > 0) ? e.touches[0] : e;
if (!y_start) {y_start = prop.clientY;}
y_end = prop.clientY;
return true;
};
results_native.ontouchend = function(e) {
select_diff = ~~((y_start - y_end) * 1.3);
$('ul#destChoices', results_element).animate({scrollTop:'+='+select_diff},{queue:false,duration:300});
e.preventDefault();
y_start = false;
y_end = false;
return true;
};
}
}
else { clearAutoComplete(); return false; }
}
else { clearAutoComplete(); return false; }
});
}
function setAutoComplete(input, results, service, data_type) {
var input_element = ($('#'+ input).length > 0) ? $('#'+ input) : false,
results_element = ($('#'+ results).length > 0) ? $('#'+ results) : false,
timeout;
if ( !results_element || !input_element ) { return false; }
if ( input_element.data('events') && input_element.data('events').keydown ) { return false; }
if ( !results_element.hasClass('ajaxResults') ) { results_element.addClass('ajaxResults'); }
if ( $('ul#destChoices', results_element).length === 0 ) { results_element.html('<ul id="destChoices">'); }
input_element.attr('autocomplete','off');
if ( window.Touch || window.TouchEvent ) {
input_element.bind('blur.clearAutoComplete', function() {
timeout = setTimeout(function() {
if (ac_scroll_active) { return false; }
else { clearAutoComplete(); }
clearTimeout(timeout);
}, 200);
});
$('body').bind('click.clearAutoComplete', function() { clearAutoComplete(); });
}
else {
$('body').bind('click.clearAutoComplete', function() { clearAutoComplete(); });
}
$('ul#destChoices', results_element).scroll(function() {
if ($(document.activeElement).attr('id') !== input) { ac_scroll_active = true; $('#'+ input).focus(); }
});
if (typeof jQuery().bgiframe === 'function' && $('ul#destChoices', results_element).bgiframe() !== false) { ac_ie6_compliance = 2; }
input_element.click(function() {
var val = $(this).val(),
element = document.getElementById(input);
if (element.setSelectionRange) { element.setSelectionRange(0, 9999); }
else { element.select(); }
if ( val && val.trim().length > 2) { autoComplete(input, results, service, data_type); }
else { return false; }
});
input_element.keydown(function(key) {
var val = $(this).val().trim(),
code = key.keyCode || key.which,
temp,
sizzle = $('ul', results_element);
if (sizzle.scrollTop() === ((ac_results_count * 18) - 72)) { ac_remainder = true; }
else { ac_remainder = false; }
$('#ojId').val('');
$('#ojGw').val('');
$('#ojCountry').val('');
if ( val.length >= 2 ) {
if ( code === 8 && val.length === 3) { clearAutoComplete(); }
else if ((code > 64 && code < 91) || code === 8) {
if (ac_timeout_last === 0) {
ac_results_selected = ac_ie6_compliance;
ac_timeout = setTimeout(function () { autoComplete(input, results, service, data_type); clearTimeout(ac_timeout); }, 200);
ac_timeout_last = ac_timeout;
}
}
else if (code === 40) {
key.preventDefault();
if (ac_results_selected === ac_results_count) {
sizzle.animate({scrollTop:$('ul', results_element).get(0).scrollHeight}, {queue:false, duration:100});
return false;
}
else {
$('ul#destChoices li:nth-child('+ ac_results_selected +')', results_element).removeClass('acHighlight');
if ( ( ((ac_results_selected + 1) - ac_ie6_compliance) % 4 === 0 ) ) {
$('ul', results_element).animate({scrollTop:'+=72'},{queue:false, duration:100});
}
ac_results_selected += 1;
$('ul#destChoices li:nth-child('+ ac_results_selected +')', results_element).addClass('acHighlight');
}
}
else if (code === 38) {
key.preventDefault();
if (ac_results_selected === ac_ie6_compliance) {
sizzle.animate({scrollTop:0}, {queue:false, duration:100});
return false;
}
else {
$('ul#destChoices li:nth-child('+ ac_results_selected +')', results_element).removeClass('acHighlight');
ac_results_selected -= 1;
if (((ac_results_selected + 1) - ac_ie6_compliance ) === (( (ac_results_count + 1) - ac_ie6_compliance ) - (( (ac_results_count + 1) - ac_ie6_compliance ) % 4) )) {
temp = (( (ac_results_count + 1) - ac_ie6_compliance ) % 4)*18;
sizzle.animate({scrollTop:'-='+temp}, {queue:false, duration:100});
}
else if (((((ac_results_selected + 1) - ac_ie6_compliance) % 4) === 0) && (ac_results_selected < (( (ac_results_count + 1) - ac_ie6_compliance ) - (( (ac_results_count + 1) - ac_ie6_compliance ) % 4)))) {
$('ul', results_element).animate({scrollTop:'-=72'}, {queue:false, duration:100});
}
$('ul#destChoices li:nth-child('+ ac_results_selected +')', results_element).addClass('acHighlight');
ac_remainder = false;
}
}
else if (code === 13 && $('ul#destChoices', results_element).is(':visible')) {
$('ul#destChoices li:nth-child('+ ac_results_selected +')', results_element).click();
return false;
}
}
else { clearAutoComplete(); }
});
}
function setDPDestination(productType, stationCode) {
var theTargetField = '';
switch (productType) {
case 'hotel': theTargetField = '#hotelGOTo';
break;
case 'car': theTargetField = '#carGOTo';
break;
case 'experience': theTargetField = '#expGOTo';
break;
case 'flighthotel': theTargetField = '#packageTo';
break;
case 'flightcar': theTargetField = '#packageTo';
break;
}
finalURL = OJUrl + '?capability=' + productType + '&ajaxSearch=' + stationCode + "&language=" + ac_user_language;
$.get(finalURL, function(data){
var ansLength = data.length;
if(ansLength > 0){
data = data.replace(/\r/g,'');
data = data.replace(/\n/g,'');
var splitData = data.split("<li");
for (var i=0; i<=splitData.length; i++) {
var gwmatch = /gw="(.+?)"\s*country/.exec(splitData[i]);
if (gwmatch) {
if ((gwmatch[1]==stationCode.toUpperCase())) {
var ojGw=gwmatch[1];
var textmatch = /">(.+?)<\/li>/.exec(splitData[i]);
							if (textmatch) {
								var ojText=textmatch[1];
								//if the code is in the text in brackets
								if (ojText.indexOf('('+stationCode.toUpperCase())!=-1) {
									var idmatch = /id\s*=\s*"(.+?)"\s*name/.exec(splitData[i]);
									if (idmatch) {
										var ojId=idmatch[1];
									}
									var countrymatch = /country\s*=\s*"(.+?)">/.exec(splitData[i]);
									if (countrymatch) {
										var ojCountry=countrymatch[1];
									}
									if ((ojId) && (ojGw) && (ojCountry) && (ojText)) {		//sanity check
										$("#ojId").val(ojId);
										$("#ojGw").val(ojGw);
										$("#ojCountry").val(ojCountry);
										$(theTargetField).val(ojText);
										return;
									}
								}
							}
						}
					}
				}
			}
		});
	}

