var arrDest=new Array(),prevSearch='',goIndex=-1,codFld='depCountry',goFld='destChoices',classFld='cabin',defaultCabins=new Array();
var performCabinHelper=true,isDepart=false,mouseOverDynamicField=false,searchFld='',toFld,fromFld,useAJAX=true;
if(document.addEventListener)
document.addEventListener("mouseup", catchClick, false)
else if(document.attachEvent)
document.attachEvent("onmouseup", catchClick)
function catchClick(e){
if(!mouseOverDynamicField)
toggleDestinations(false);
}
function destination(text,value){
this.text=text;
this.value=value;
}
function cabinClass(text,value){
this.text=text;
this.value=value;
}
function getDestinations(e,thisSearch){
if(!useAJAX)
return;
if(performCabinHelper)
resetCabins();
var keyCode=e.keyCode?e.keyCode:e.which?e.which:e.charCode;
if(thisSearch.length<3 || keyCode==27){
toggleDestinations(false);
return;
}
if(((keyCode==40) || (keyCode==9)) && document.getElementById(goFld).options.length>0 && document.getElementById(goFld).options[0].value!=''){
toggleDestinations(true);
document.getElementById(goFld).focus();
document.getElementById(goFld).selectedIndex=0;
return false;
}
if(prevSearch!='' && thisSearch.indexOf(prevSearch)==0)
populateDestinations();
else{
prevSearch=thisSearch.substring(0,3);
try{locationHelper.getMatchedLocations(saveDestinations,prevSearch);}
catch(err){};
}
}
function saveDestinations(data){
arrDest=new Array();;
for(var key in data)
arrDest[arrDest.length]=new destination(data[key],key);
populateDestinations();
}
function populateDestinations(){
DWRUtil.removeAllOptions(goFld);
var searchTerm=document.getElementById(searchFld).value.toLowerCase(),dest;
if(searchTerm.length<3){
toggleDestinations(false);
return;
}
for(var i=0;i<arrDest.length;i++){
dest=arrDest[i].text.toLowerCase().split(',');
for(var j=0;j<dest.length;j++)
if(dest[j].indexOf(searchTerm)==0 || dest[j].indexOf(searchTerm)==1 && j>0){
document.getElementById(goFld).options[document.getElementById(goFld).options.length]=new Option(arrDest[i].text,arrDest[i].value);
break;
}
}
toggleDestinations(document.getElementById(goFld).options.length>0?true:false);
}
function displayCities(to,from){
if(!useAJAX)
return;
var thisSearch='';
if(isComplexPage())
performCabinHelper=false;
if(typeof(to)=='string'){
to=document.getElementById(to);
isDepart=true;
}
else{
from=document.getElementById(from);
isDepart=false;
}
toFld=to;
fromFld=from;
if(isDepart){
thisSearch=from.value;
if(searchFld!=from.id)
toggleDestinations(false);
searchFld=from.id;
}
else{
thisSearch=to.value;
if(searchFld!=to.id)
toggleDestinations(false);
searchFld=to.id;
}
document.getElementById(goFld).selectedIndex=-1;
if(thisSearch.length<3){
return;
}
else if(prevSearch==''){
prevSearch=thisSearch.substring(0,3);
locationHelper.getMatchedLocations(saveDestinations,prevSearch);
}
else if(thisSearch.indexOf(prevSearch)==0 && document.getElementById(goFld).options.length>0 && document.getElementById	(goFld).options[0].value!=0){
toggleDestinations(true);
}
else{
prevSearch=thisSearch.substring(0,3);
locationHelper.getMatchedLocations(saveDestinations,prevSearch);
}
}
function selectDestination(a){
if(!useAJAX)
return;
var depCode='',destCode='',destName='';
if (typeof a=='undefined')
destName=DWRUtil.getText(goFld);
else
{
toFld=document.getElementById('to');
fromFld=document.getElementById('from');
destName=document.getElementById('to').value;
depCode=a;
}
if(destName=='')
return;
if (typeof a=='undefined')
{
if(isDepart)
DWRUtil.setValue(fromFld,destName);
else
DWRUtil.setValue(toFld,destName);
}
depCode=getAirportCode(DWRUtil.getValue(fromFld));
destCode=getAirportCode(DWRUtil.getValue(toFld));
if(depCode.length==3 && destCode !='')
{
if(performCabinHelper)
{
var lang='en';
if(document.nav_form.language.value != '')
lang=document.nav_form.language.value;
try{cabinHelper.getCabinsForRoute(populateClasses,depCode.toUpperCase(),destCode.toUpperCase(),lang);}
catch(err){};
}
try{locationHelper.isBusinessUKSector(changeRadio,depCode.toUpperCase(),destCode.toUpperCase());}
catch(err){};
}
toggleDestinations(false);
if (typeof a=='undefined')
{
DWRUtil.removeAllOptions(goFld);
document.getElementById(goFld).blur();
if(isDepart)
fromFld.focus();
else
toFld.focus();
}
}
function getAirportCode(displayName){
if(displayName.indexOf(',')!=-1){
var arrSplit=displayName.split(',');
if(arrSplit.length==4)
return arrSplit[2].substring(1);
}
return displayName;
}
function updateDestination(event){
var keyCode=event.keyCode?event.keyCode:event.which?event.which:event.charCode;
switch(keyCode){
case 13:
selectDestination()
if (fieldToTabTo) {
document.getElementById(fieldToTabTo).focus();
if (document.getElementById(fieldToTabTo).type=='text') {
document.getElementById(fieldToTabTo).select();
}
}
break;
case 9:
selectDestination()
if (fieldToTabTo) {
document.getElementById(fieldToTabTo).focus();
if (document.getElementById(fieldToTabTo).type=='text') {
document.getElementById(fieldToTabTo).select();
}
}
break;
case 27:
document.getElementById(searchFld).focus();
break;
case 38:
if(goIndex==-1){
document.getElementById(goFld).selectedIndex=-1;
document.getElementById(searchFld).focus();
}
break;
}
}
function changeIndex(e,selIndex){
var keyCode=e.keyCode?e.keyCode:e.which?e.which:e.charCode;
if(keyCode==38 && goIndex==0 && selIndex==0)
selIndex=-1
goIndex=selIndex;
}
function positionGo(){
var ele=document.getElementById(searchFld);
var x=getLeft(ele);
var y=getTop(ele);
document.getElementById(goFld).style.left=x+'px';
document.getElementById(goFld).style.top=y+'px';
}
function getLeft(ele){
var l=ele.offsetLeft;
while((ele=ele.offsetParent)!=null){
l+=ele.offsetLeft;
}
return l;
}
function getTop(ele){
var h;
if(ele.clientHeight)
h=ele.clientHeight;
else if(ele.offsetHeight)
h=ele.offsetHeight;
var t=ele.offsetTop+h+6;
while((ele=ele.offsetParent)!=null){
t+=ele.offsetTop;
}
return t;
}
function toggleDestinations(show){
if(!document.getElementById(goFld))
return;
if(show)
positionGo();
else
document.getElementById(goFld).tabIndex=-1;
document.getElementById(goFld).style.display=(show?'block':'none');
}
function populateClasses(data){
var arrClass=new Array();
var cabinDrop=document.getElementById(classFld);
var classMatch=true;
for(var key in data)
arrClass[arrClass.length]=new cabinClass(data[key],key);
if(arrClass.length==0)
return;
if(arrClass.length==cabinDrop.length){
for(var i=0;i<cabinDrop.length;i++){
if(arrClass[i].value != cabinDrop[i].value){
classMatch=false;
break;
}
}
if(classMatch)
return;
}
DWRUtil.removeAllOptions(classFld);
DWRUtil.addOptions(classFld,data);
}
function resetCabins(){
if(isComplexPage())
return;
var cabinDrop=document.getElementById(classFld);
var classMatch=true;
if(defaultCabins.length>0 && defaultCabins.length==cabinDrop.length){
for(var i=0;i<defaultCabins.length;i++){
if(defaultCabins[i].value != cabinDrop[i].value){
classMatch=false;
break;
}
}
if(classMatch)
return;
}
DWRUtil.removeAllOptions(classFld);
for(var i=0;i<defaultCabins.length;i++){
document.getElementById(classFld).options[document.getElementById(classFld).length] = new Option(defaultCabins[i].text,defaultCabins[i].value);
}
}
function isComplexPage(){
return document.nav_form.pageid.value=='COMPJRNY'?true:false;
}
function storeCabins(cabinCodes,cabinNames){
var arrCodes=cabinCodes.split(':'),arrNames=cabinNames.split(':');
for(var i=0;i<arrCodes.length;i++)
defaultCabins[i] = new cabinClass(arrNames[i],arrCodes[i]);
}
function toggleOverAJAX(over){
mouseOverDynamicField=over;
}
function changeRadio(data)
{
if(document.getElementById('flexCond'))
{
if(data)
{
document.getElementById('flexCond').style.display='none';
document.getElementById('businessUK').style.display='inline';
}
else
{
document.getElementById('flexCond').style.display='inline';
document.getElementById('businessUK').style.display='none';
}
}
}

