﻿var timerId = 0;
var searchDiv;
var searchRes;
var txtMin;
var txtMax;
var cboDestination;
var cboResort;
var cboCountry;
var res;
var destid;
var countryId;
var destinationId;
var resortId;

window.addEvent('domready', function(){
        
    cboCountry = $(cboC);   
    cboDestination = $(cboD);
    cboResort = $(cboR);                       
    
    cboCountry.addEvent('change', function(){sndRequest(cboC);});
    cboDestination.addEvent('change', function() {sndRequest(cboC, cboD, 'res');});  
    cboResort.addEvent('change', function() {sndRequestResort(cboR);});   
    
});

function sndRequest(id, destid, res) 
{      
            
	if (timerId != 0)
		return;
		
	timerId = window.setTimeout("processRequest ('" + id + "', '" + destid + "', '" + res + "')", 600);
}

function sndRequestResort(id) 
{   
            
	if (timerId != 0)
		return;
		
	timerId = window.setTimeout("processRequestResort ('" + id + "')", 600);
}

function processRequest(id, destid, res) 
{
    clearTimeout(timerId);
    timerId = 0;
    
    var txt = $(id);
        
    if (!txt)
        return;       
    
    searchword = txt.value;     
    
    if (res == 'res') {
        //Change Destination
        var destination = $(destid);      
        destinationId = destination.value;        
        
        //CD - Set Country & Repopulate Destinations & Set Destination based on this country
        url = ajaxUrl + '/GetDestination?query=' + escape(destinationId);           
        var ajax = new AJAXrequest(url, getDestination);
        ajax.addRandom = false;
        ajax.errorFunction = onError;
        ajax.returnXml = true;
        ajax.process(); 
        
        //CD - Repopulate Resorts
        url = ajaxUrl + '/SearchResorts?query=' + escape(searchword) + '&destinationId=' + escape(destinationId);                                            
        var ajax = new AJAXrequest(url, populateResorts);
        ajax.addRandom = false;
        ajax.errorFunction = onError;
        ajax.returnXml = true;
        ajax.process();             
           
     
    }
    else
    {     
        //Change Country     
        if (searchword == 'Please choose a country...')
        {
            searchword = 0;
        }
        
        countryId = 0;
        destinationId = 0;
        resortId = 0;
        
        //CC - Repopulate Destinations
        url = ajaxUrl + '/SearchDestinations?query=' + escape(searchword);                          
        var ajax = new AJAXrequest(url, populateDestinations);
        ajax.addRandom = false;
        ajax.errorFunction = onError;
        ajax.returnXml = true;
        ajax.process();
        
        //CC - Repopulate Resorts
        url = ajaxUrl + '/SearchResorts?query=' + escape(searchword) + '&destinationId=0';        
        var ajax = new AJAXrequest(url, populateResorts);
        ajax.addRandom = false;
        ajax.errorFunction = onError;
        ajax.returnXml = true;
        ajax.process();
    }
}

function processRequestResort(id) 
{
    clearTimeout(timerId);
    timerId = 0;
    
    var txt = $(id);
        
    if (!txt)
        return;       
                
    searchword = txt.value;  
           
    url = ajaxUrl + '/GetResort?query=' + escape(searchword);                            
    var ajax = new AJAXrequest(url, getResort);
    ajax.addRandom = false;
    ajax.errorFunction = onError;
    ajax.returnXml = true;
    ajax.process();    
        
    $(id).selectedIndex = searchword;
}

function populateDestinations(xml)
{
    var item, link;
    var i;
    
    var dataTitle = xml.getElementsByTagName('Name');
    var dataID= xml.getElementsByTagName('ID');
    var dataFolder= xml.getElementsByTagName('Folder');
    var dataCountry = xml.getElementsByTagName('Country');               
    
    cboDestination.innerHTML = '';
    
    var opt;
    opt = new Element('option');
    opt.text = 'All Destinations';
    opt.value = '0';   
    cboDestination.options.add(opt);
    
    for (i=0; i< dataTitle.length; i++)
    {
        
        if (dataTitle[i].firstChild != null)
        {       
            opt = new Element('option');        
            opt.text = dataTitle[i].firstChild.nodeValue;
            opt.value = dataID[i].firstChild.nodeValue;                   
            cboDestination.options.add(opt);
        }
    } 
    
    cboDestination.value = destinationId;         
        
}

function getDestination(xml)
{    
    //CD - Set Country
    var dataCountry = xml.getElementsByTagName('CountryID');
    countryId = dataCountry[0].firstChild.nodeValue;      
    cboCountry.value = countryId;    
    
    //CD - Repopulate Destination based on Country
    url = ajaxUrl + '/SearchDestinations?query=' + escape(countryId);                        
    var ajax = new AJAXrequest(url, populateDestinations);
    ajax.addRandom = false;
    ajax.errorFunction = onError;
    ajax.returnXml = true;
    ajax.process(); 
    
    //CD - Set Destination
    var dataID = xml.getElementsByTagName('ID');
    destinationId = dataID[0].firstChild.nodeValue;
    cboDestination.value = destinationId;

}

function populateResorts(xml)
{
    
    var item, link;
    var i;

    var dataResort = xml.getElementsByTagName('Resort');
    var dataID= xml.getElementsByTagName('ID');    
    var dataTitle = xml.getElementsByTagName('Name');                        
    
    cboResort.innerHTML = '';
        
    var opt;
    opt = new Element('option');
    opt.text = 'All Resorts';
    opt.value = '0';   
    cboResort.options.add(opt);    
           
    for (i=0; i< dataID.length; i++)
    {         
        opt = new Element('option');
        opt.text = dataTitle[i].firstChild.nodeValue;
        opt.value = dataID[i].firstChild.nodeValue;         
        cboResort.options.add(opt);       
    }       
     
    cboResort.value = resortId;            
      
}

function getResort(xml)
{
    
    var item, link;
    var i;      
    var dataID = xml.getElementsByTagName('ID');              
    var dataCountry = xml.getElementsByTagName('CountryID');                     
        
    cboCountry.value = dataCountry[0].firstChild.nodeValue;
    
    resortId = dataID[0].firstChild.nodeValue;   
    countryId = dataCountry[0].firstChild.nodeValue;           
    var dataDestination = xml.getElementsByTagName('DestinationID');
    destinationId = dataDestination[0].firstChild.nodeValue;               
 
    // Repopulate Destination based on Country
    url = ajaxUrl + '/SearchDestinations?query=' + escape(countryId);                    
    var ajax = new AJAXrequest(url, populateDestinations);
    ajax.addRandom = false;
    ajax.errorFunction = onError;
    ajax.returnXml = true;
    ajax.process();                    
    
    //Repopulate Resort based on Destination
    url = ajaxUrl + '/SearchResorts?query=' + escape(countryId) + '&destinationId='+destinationId;                    
    var ajax = new AJAXrequest(url, populateResorts);   
    ajax.addRandom = false;
    ajax.errorFunction = onError;
    ajax.returnXml = true;
    ajax.process();  
                   
}

function onError(status)
{
    if (status == "500")
        alert ("There was an error while processing your request");
    else if (status == "404")
        alert ("Page not found");
    else
        alert ("Error (status " + status + ")");
}