
/*  cheapskiflights.co.uk
 *  (c) 2007 Manta Software Limited
 */
var clientId;
var autocompletePopupId;
var autocompleteInputId;
var countryInputId;
var resortInputId;
var resortHiddenId;
var maxAutoSize = 20;
var defaultResortText = '>> enter all or part of resort name here';

function delayHideAutoComplete(delayms) {
    if (delayms > 0 && autoCompleteIsVisible(autocompletePopupId))
        var mytimeout = window.setTimeout(hideAutoComplete, delayms); 
}

function hideAutoComplete() {
    clearChoices();
    hideDiv(autocompletePopupId, true);
}

function getElementLeft(element) {
    var off = 0;
    while (element) {
	off += element.offsetLeft;
	element = element.offsetParent;
    }
    return off;
}

function getCountry (code) {
    switch (code)
    {
        case 'AD': return 'Andorra';
        case 'AR': return 'Argentina';
        case 'AU': return 'Australia';
        case 'AT': return 'Austria';
        case 'FB': return 'Bulgaria';
        case 'CA': return 'Canada';
        case 'CL': return 'Chile';
        case 'CZ': return 'Czech Republic';
        case 'EE': return 'Estonia';
        case 'FI': return 'Finland';
        case 'FR': return 'France';
        case 'DE': return 'Germany';
        case 'GR': return 'Greece';
        case 'IS': return 'Iceland';
        case 'IR': return 'Iran';
        case 'IL': return 'Israel';
        case 'IT': return 'Italy';
        case 'JP': return 'Japan';
        case 'LB': return 'Lebanon';
        case 'LI': return 'Liechtenstein';
        case 'NZ': return 'New Zealand';
        case 'NO': return 'Norway';
        case 'PL': return 'Poland';
        case 'RO': return 'Romania';
        case 'RU': return 'Russia';
        case 'SK': return 'Slovak Republic';
        case 'SI': return 'Slovenia';
        case 'KP': return 'South Korea';
        case 'SP': return 'Spain';
        case 'SE': return 'Sweden';
        case 'CH': return 'Switzerland';
        case 'GB': return 'United Kingdom';
        case 'US': return 'United States';
        default: return '';
    }
}

function getElementTop(element) {
    var off = 0;
    while (element) {
	off += element.offsetTop;
	element = element.offsetParent;
    }
    return off;
}

function hideDiv(divId, hide) {
    if (document.layers) document.layers[divId].visibility= hide ? "hide" : "show";
    else $(divId).style.visibility= hide ? "hidden" : "visible";
}

function clearChoices() {
    var numChildNodes = $(autocompletePopupId).childNodes.length;
    for (var i = numChildNodes-1; i >= 0; i--)
         $(autocompletePopupId).removeChild($(autocompletePopupId).childNodes[i]);
}

function clearResult()
{
    $(autocompleteInputId).value=$(autocompleteInputId).value.replace(defaultResortText,'');
}

function onLoad() {
    if (!$(resortInputId) || !$(countryInputId))
        return;
    showResorts(resortInputId, countryInputId);
    if ($(autocompleteInputId)) {
        hideDiv(autocompletePopupId, true);
        $(autocompleteInputId).value = defaultResortText;
        initAutoCompleteHandler(autocompleteInputId);
    }
}

function initAutoCompleteHandler(id)
{
    if (document.all) {
	    // Special handling required for IE
	    $(id).onclick = new Function('clearResult();');
	    $(id).onblur = new Function('delayHideAutoComplete(500);');
	    $(id).onkeydown = new Function('return autoCompleteKeyDownEvent("' + autocompletePopupId + '", event); ');
	    $(id).onkeyup = new Function('return autoCompleteKeyUpEvent(event);');
    }
    else {
	    // non-IE browsers
	    $(id).onclick = function(){ clearResult();};
        $(id).onblur = function() { delayHideAutoComplete(500); }
	    $(id).onkeydown = function(event) { return autoCompleteKeyDownEvent(autocompletePopupId, event);}
	    $(id).onkeyup = function(event) { return autoCompleteKeyUpEvent(event);}
    }
}

function autoCompleteKeyDownEvent(id, event) {
    switch(event.keyCode) {
    case 38: // up arrow
	    autoCompleteMove(id, -1);
	    break;

    case 40: // down arrow
	    autoCompleteMove(id, 1);
	    break;

    case 9: // tab
	    if (autoCompleteIsVisible(id)) {
	        autoCompleteChoose(id);
	        return false;
	    }
	    break;
	
    case 13: // enter
	    if (autoCompleteIsVisible(id)) {
	        autoCompleteChoose(id);
	        return false;
	    }
	    break;

    case 27: // escape
	    hideAutoComplete();
	    break;
    }
    return true;
}


function autoCompleteKeyUpEvent(event) {
    switch(event.keyCode) {
    case 38: // up arrow
    case 40: // down arrow
	case 9: // tab
	case 13: // enter
	case 27: // escape
	    return false;
	default:
	    findResorts();
	    break;
    }
    return true;
}

function autoCompleteMove(id, moveAmount) {
    var tcells = $(id).getElementsByTagName("td");
    if (tcells.length == 0)
        return;
    var currentChoice = -1;
    for (var i = 0; i < tcells.length; i++) 
    {
        if (tcells[i].className=="mouseOver")
        {
            currentChoice = i;
            break;
        }
    }
    var newChoice = (currentChoice + moveAmount) % tcells.length;
    if (newChoice < 0)
        newChoice = tcells.length + newChoice;
    tcells[newChoice].className = "mouseOver";
    tcells[currentChoice].className = "mouseOut";
 }

function autoCompleteChoose(id) {
    var tcells = $(id).getElementsByTagName("td");
    if (tcells.length == 0)
        return;
    var currentChoice = -1;
    for (var i = 0; i < tcells.length; i++) 
    {
        if (tcells[i].className=="mouseOver")
        {
            replaceInput(tcells[i]);
            break;
        }
    }
    return;
 }
 
 function autoCompleteIsVisible(id) {
    return $(id).hasChildNodes();
 }

function findResorts () {
    var matchesFound = 0;
    clearChoices();
    if ($(autocompleteInputId).value.length == 0)
    {
        hideDiv(autocompletePopupId, true);
        $(autocompleteInputId).value = defaultResortText;
        return;
    }
    $(autocompleteInputId).value = $(autocompleteInputId).value.replace(defaultResortText,'');
    hideDiv(autocompletePopupId, false);
    // Create a table to hold results
    var ttable = document.createElement("table");
    var tbody = document.createElement("tbody");
    var trow, tcell, tnode;
    
    var s = res.split('|');
    for (i=0; i < s.length && matchesFound < maxAutoSize; i++)
	{
		t = s[i].split('?');
		var resortName = t[2];
		
		if (resortName.toLowerCase().indexOf($(autocompleteInputId).value.toLowerCase()) >= 0)
		{
			// Matching name found
			matchesFound++;
			trow = document.createElement("tr");
			tcell = document.createElement("td");
			tcell.onmouseover = function(){this.className='mouseOver';};
			tcell.onmouseout = function(){this.className='mouseOut';};
			tcell.className = matchesFound > 1 ? 'mouseOut' : 'mouseOver';
			tcell.setAttribute("border","0");
			tcell.id = '_r' + t[1];
			tcell.onclick = function(){replaceInput(this);};
			tnode = document.createTextNode(resortName + ', ' + getCountry(t[0].substring(0,2)));
			tcell.appendChild(tnode);
			trow.appendChild(tcell);
			tbody.appendChild(trow);
		}
	}
	ttable.appendChild(tbody);
	ttable.style.position = 'absolute';
    ttable.style.zIndex = 999;
    ttable.className = "autocomplete";
    ttable.style.width = "320px";
    $(autocompletePopupId).appendChild(ttable);
    ttable.id = "t_autocomplete";
    initAutoCompleteHandler(autocompleteInputId);
}

function replaceInput(tcell) {
    var ipos = tcell.firstChild.nodeValue.indexOf (', ');
    if (ipos > 0)
        $(autocompleteInputId).value = tcell.firstChild.nodeValue.substring(0,ipos);
    else
        $(autocompleteInputId).value = tcell.firstChild.nodeValue;
    clearChoices();
    hideDiv(autocompletePopupId, true);
    window.location.href= 'resorts.aspx?rid=' + tcell.id.substring(2);
}

function showResorts (resortsId, countryId) {
    var c = $(countryId).options[$(countryId).selectedIndex].value;
    var s = res.split('|');
	var t;
	$(resortsId).options.length=0;
	$(resortsId).options[0] = new Option('Choose a resort', '0');
	for (i=0; i < s.length; i++)
	{
		t = s[i].split('?');
		if (c == t[0])
		{
			$(resortsId).options[$(resortsId).options.length] =
			 new Option(t[2], t[1]);
			if ($(resortHiddenId).value == t[1])
			    $(resortsId).options.selectedIndex = $(resortsId).options.length-1;
		}
	}
	$(resortHiddenId).value = $(resortsId).options[$(resortsId).options.selectedIndex].value;
}

function resortSelected (resortsId, hdnFld)
{
	var resortId = $(resortsId).options[$(resortsId).options.selectedIndex].value;
    $(hdnFld).value = resortId;
    if (resortId == '0')
        return;
    if (window.location.href.indexOf('flightsearch') < 0)
        window.location.href= 'resorts.aspx?rid=' + resortId;
    // else do nothing
}

function airportOptionChanged(checkboxId, fromAirportId, toAirportId, maxMilesId, postcodeId)
{
    $(fromAirportId).disabled = $(checkboxId).checked;
    $(maxMilesId).disabled = !$(checkboxId).checked;
    $(postcodeId).disabled = !$(checkboxId).checked;
    filterToAirport(checkboxId, fromAirportId, toAirportId);
}

function resortOptionChanged(checkboxId, toAirportId, maxTransferId, countryId, resortId)
{
    $(toAirportId).disabled = $(checkboxId).checked;
    $(maxTransferId).disabled = !$(checkboxId).checked;
    $(countryId).disabled = !$(checkboxId).checked;
    $(resortId).disabled = !$(checkboxId).checked;
}

function filterToAirport(checkBoxId, fromAirportId, toAirportId)
{
   var n = 0;
    
    $(toAirportId).options.length=0;
	$(toAirportId).options[0] = new Option('Choose an airport','');
	
    if (!$(checkBoxId).checked)
    {
        // Find valid routes from the selected airport
        var r = rte.split('|');
        for (i=0; i < r.length; i++)
        {
            var r2 = r[i].split('?');
            if (r2[0] == $(fromAirportId).options[$(fromAirportId).options.selectedIndex].value)
            {
                // r2[1]...r2[n] contain destination aiport codes
                for (j=1; j < r2.length; j++)
                {
                    $(toAirportId).options[++n] = new Option(a[r2[j]] + ' (' + r2[j] + ')',
                        r2[j]);
                }
                break;
            }
        }
    }
    else
    {
        // Show all airports in toAirport
        var a2 = al.split('|');
        
        for (i=0; i < a2.length; i++)
             $(toAirportId).options[++n] = new Option(a[a2[i]] + ' (' + a2[i] + ')',
                        a2[i]);
    }
}

function toggleArrow (imgId, blockId)
{
   if ($(blockId).className != 'hide') {
        $(blockId).className = 'hide';
        $(imgId).src = 'img/adown.gif';
    }
    else {
        $(blockId).className = 'show';
        $(imgId).src = 'img/aup.gif';
    }
}
 