﻿// Arrays
var firstSearchArray = new Array("First Search By","Destination","Holiday type", "Walking grade", "Duration", "Date","Search by map");

function populateComboBoxes(  id, data )
{
        var dll = document.createElement("select");
        dll.id = id;
        dll.name = id;
		dll.className  = "cbxSelectBoxStyle";
    
        for( index = 0; index < data.length; index++ )
        {    
            option = new Option();
            option.value = index;
            option.innerHTML = data[index];
            dll.appendChild( option );   
        }
     
        return dll;
}

function populateComboBoxes2(  id, data, selectedIndex )
{

		var dll = document.createElement("select");
        dll.id = id;
        dll.name = id;
		dll.className  = "cbxSelectBoxStyle";
         
        for( index = 0; index < data.length; index++ )
        {    
            option = new Option();
            option.value = index;
            option.innerHTML = data[index];
            
            if( selectedIndex == index )
            {
                option.selected = true;
            }
            dll.appendChild( option );   
        }
     
        return dll;
}

function populateComboBoxes3(  id, intRegionID, intSelectedIndex )
{
        var dll = document.createElement("select");
        dll.id = id;
        dll.name = id;
		dll.className  = "cbxSelectBoxStyle";
		
		var topOption = new Option();
		topOption.value = 0;
		topOption.innerHTML = "Regions...";
		dll.appendChild( topOption );
        
        for( index = 0; index < countries[ intRegionID].length; index++ )
        {    
            option = new Option();
            option.value = countries[intRegionID][index][0];
            option.innerHTML = countries[ intRegionID][index][1];
            
            if( countries[intRegionID][index][0] == intSelectedIndex )
            {
                option.selected = true;
            }
            
            dll.appendChild( option );  
        }
     
        return dll;
}

function populateComboBoxes4(  id, data, intSelectedIndex )
{
        var dll = document.createElement("select");
        dll.id = id;
        dll.name = id;
		dll.className  = "cbxSelectBoxStyle";
        
        for( index = 0; index < data.length; index++ )
        {    
            option = new Option();
            option.value = data[index][0];
            option.innerHTML = data[index][1];
            
            if( data[index][0] == intSelectedIndex )
            {
                option.selected = true;
            }
            
            dll.appendChild( option );  
        }
     
        return dll;
}

function populateDescription( id, text )
{
        var desc = document.createElement("div");
        desc.className = "searchFont";
        desc.id =id;
        desc.innerHTML = text;
        return desc;
}

function removeElement( elementID )
{
    var node = document.getElementById( elementID );
    node.parentNode.removeChild( node );
}


function populateCountries(bPreviousSearch, intSelectedIndex )
{
    var regionSearch = "";
    var cbxCountries = "";

    if( document.getElementById("trCountries") != null )
    {
        removeElement("trCountries");
    }
    
    regionSearch = parseInt( document.getElementById("cbxRegions").value );
    
   // alert( regionSearch );
   
    switch( regionSearch )
    {
        case 1:
            if( bPreviousSearch )
            {
                cbxCountries = populateComboBoxes3("cbxCountries",  regionSearch, intSelectedIndex );
            }
            else
            {     
                cbxCountries = populateComboBoxes3("cbxCountries",  regionSearch, -1 );
            }
            
            break;
        case 2:
            if( bPreviousSearch )
            {
                cbxCountries = populateComboBoxes3("cbxCountries",  regionSearch, intSelectedIndex );
            }
            else
            {     
                cbxCountries = populateComboBoxes3("cbxCountries",  regionSearch, -1 );
            }
            break;
        case 3:
                    if( bPreviousSearch )
            {
                cbxCountries = populateComboBoxes3("cbxCountries",  regionSearch, intSelectedIndex );
            }
            else
            {     
                cbxCountries = populateComboBoxes3("cbxCountries",  regionSearch, -1 );
            }
            
            break;
        case 4:
                     if( bPreviousSearch )
            {
                cbxCountries = populateComboBoxes3("cbxCountries",  regionSearch, intSelectedIndex );
            }
            else
            {     
                cbxCountries = populateComboBoxes3("cbxCountries",  regionSearch, -1 );
            }
            
            break;
        case 5:
                    if( bPreviousSearch )
            {
                cbxCountries = populateComboBoxes3("cbxCountries",  regionSearch, intSelectedIndex );
            }
            else
            {     
                cbxCountries = populateComboBoxes3("cbxCountries",  regionSearch, -1 );
            }
            
            break;
        case 6:
                     if( bPreviousSearch )
            {
                cbxCountries = populateComboBoxes3("cbxCountries",  regionSearch, intSelectedIndex );
            }
            else
            {     
                cbxCountries = populateComboBoxes3("cbxCountries",  regionSearch, -1 );
            }
            
            break;
        case 7:
                     if( bPreviousSearch )
            {
                cbxCountries = populateComboBoxes3("cbxCountries",  regionSearch, intSelectedIndex );
            }
            else
            {     
                cbxCountries = populateComboBoxes3("cbxCountries",  regionSearch, -1 );
            }
            
            break; 
        case 8:
                     if( bPreviousSearch )
            {
                cbxCountries = populateComboBoxes3("cbxCountries",  regionSearch, intSelectedIndex );
            }
            else
            {     
                cbxCountries = populateComboBoxes3("cbxCountries",  regionSearch, -1 );
            }
            
            break;
        case 9:
                     if( bPreviousSearch )
            {
                cbxCountries = populateComboBoxes3("cbxCountries",  regionSearch, intSelectedIndex );
            }
            else
            {     
                cbxCountries = populateComboBoxes3("cbxCountries",  regionSearch, -1 );
            }
            
            break;
        case 10:
                     if( bPreviousSearch )
            {
                cbxCountries = populateComboBoxes3("cbxCountries",  regionSearch, intSelectedIndex );
            }
            else
            {     
                cbxCountries = populateComboBoxes3("cbxCountries",  regionSearch, -1 );
            }
            
            break;
        case 11:
                     if( bPreviousSearch )
            {
                cbxCountries = populateComboBoxes3("cbxCountries",  regionSearch, intSelectedIndex );
            }
            else
            {     
                cbxCountries = populateComboBoxes3("cbxCountries",  regionSearch, -1 );
            }
            
            break;
        case 12:
                     if( bPreviousSearch )
            {
                cbxCountries = populateComboBoxes3("cbxCountries",  regionSearch, intSelectedIndex );
            }
            else
            {     
                cbxCountries = populateComboBoxes3("cbxCountries",  regionSearch, -1 );
            }
            
            break;
        case 13:
                    if( bPreviousSearch )
            {
                cbxCountries = populateComboBoxes3("cbxCountries",  regionSearch, intSelectedIndex );
            }
            else
            {     
                cbxCountries = populateComboBoxes3("cbxCountries",  regionSearch, -1 );
            }
            
            break;
        case 14:
                     if( bPreviousSearch )
            {
                cbxCountries = populateComboBoxes3("cbxCountries",  regionSearch, intSelectedIndex );
            }
            else
            {     
                cbxCountries = populateComboBoxes3("cbxCountries",  regionSearch, -1 );
            }
            
            break;
        case 15:
                     if( bPreviousSearch )
            {
                cbxCountries = populateComboBoxes3("cbxCountries",  regionSearch, intSelectedIndex );
            }
            else
            {     
                cbxCountries = populateComboBoxes3("cbxCountries",  regionSearch, -1 );
            }
            
            break;
        case 16:
                     if( bPreviousSearch )
            {
                cbxCountries = populateComboBoxes3("cbxCountries",  regionSearch, intSelectedIndex );
            }
            else
            {     
                cbxCountries = populateComboBoxes3("cbxCountries",  regionSearch, -1 );
            }
            
            break;
        case 17:
                     if( bPreviousSearch )
            {
                cbxCountries = populateComboBoxes3("cbxCountries",  regionSearch, intSelectedIndex );
            }
            else
            {     
                cbxCountries = populateComboBoxes3("cbxCountries",  regionSearch, -1 );
            }
            
            break;
        case 18:
                     if( bPreviousSearch )
            {
                cbxCountries = populateComboBoxes3("cbxCountries",  regionSearch, intSelectedIndex );
            }
            else
            {     
                cbxCountries = populateComboBoxes3("cbxCountries",  regionSearch, -1 );
            }
            
            break;
        case 19:
                     if( bPreviousSearch )
            {
                cbxCountries = populateComboBoxes3("cbxCountries",  regionSearch, intSelectedIndex );
            }
            else
            {     
                cbxCountries = populateComboBoxes3("cbxCountries",  regionSearch, -1 );
            }
            
            break;
        case 20:
                    if( bPreviousSearch )
            {
                cbxCountries = populateComboBoxes3("cbxCountries",  regionSearch, intSelectedIndex );
            }
            else
            {     
                cbxCountries = populateComboBoxes3("cbxCountries",  regionSearch, -1 );
            }
            
            break;
    }
    

  
    if( document.getElementById("divDestination") )
   { 
        var desc = document.getElementById("divDestination");
   
        desc.innerHTML = "Choose a region within";  
    }
    addRow("tblSearch","trCountries", cbxCountries );

    cbxCountries.onchange = new Function("processCountry();");
}

function processCountry()
{
    if( document.getElementById("trDestinationDesc") )
    {
        removeElement("trDestinationDesc");
    }
    
    populateRemoveLastSection();
    populateGo();
    populateFineSearch();
    
    var form=document.forms['frmSearch'];
    form.submit();  
}


function populateRegions( bPreviousSearch, intSelectedIndex )
{
    var cbxRegions;
    // Check if the tablerow trRegion already exists in the table
    if( document.getElementById("trRegion") != null )
    {
        // Remove the table row for region
        removeElement("trRegion");
    }  
     
    var desc = populateDescription( "divDestination","Choose your destination");
     
    if( bPreviousSearch )
    {
        cbxRegions = populateComboBoxes4("cbxRegions",regions, intSelectedIndex );   
        addRow("tblSearch","trRegion",cbxRegions);
    }
    else
    {
        cbxRegions = populateComboBoxes4("cbxRegions",regions, -1);
        addRow("tblSearch","trDestinationDesc", desc );
        addRow("tblSearch","trRegion",cbxRegions);
    }
   
    cbxRegions.onchange = new Function("processRegion();");
}

function processRegion()
{
    populateCountries( false, -1 );
    populateGo();
    populateFineSearch();
    
    var form = document.forms["frmSearch"];
    form.submit();
}

function populateHolidayTypes( bPreviousSearch, intSelectedIndex )
{
    var cbxHolidayTypes;
    
    if( document.getElementById("trHolidayType") != null )
    {
        removeElement("trHolidayType");
    }
    
    var desc = populateDescription("divHolidayTypeDesc","Choose your holiday type");
    
    var option = new Option();
    option.value = 999;
    option.innerHTML = "Tell me more";
    
    if( bPreviousSearch )
    {
        cbxHolidayTypes = populateComboBoxes4("cbxHolidayTypes", holidayTypes, intSelectedIndex);
        cbxHolidayTypes.appendChild( option );
        addRow("tblSearch","trHolidayType", cbxHolidayTypes );
    }
    else
    {
        cbxHolidayTypes = populateComboBoxes4("cbxHolidayTypes", holidayTypes, -1);
        cbxHolidayTypes.appendChild( option );
        addRow("tblSearch","trHolidayTypeDesc", desc );  
        addRow("tblSearch","trHolidayType", cbxHolidayTypes );
    }
    
    cbxHolidayTypes.onchange = new Function("processHolidayTypes();");
}

function processHolidayTypes()
{
//     if( document.getElementById("trHolidayTypeDesc") )
//     {
        var intHolidayTypeID = parseInt( document.getElementById("cbxHolidayTypes").value );
        
        if( intHolidayTypeID == 999 )
        {
            window.location="/Engine.aspx?pageID=139"; 
        }
        else
        {
            if( document.getElementById("trHolidayTypeDesc"))
            {
                removeElement("trHolidayTypeDesc");
            }
            
            populateRemoveLastSection();
            populateGo();
            populateFineSearch();
            
            var form = document.forms["frmSearch"];
            form.submit();
        }
//     }
}

function populateWalkingGrades( bPreviousSearch, intSelectedIndex )
{   
    var cbxWalkingGrades;
    
    if( document.getElementById("trWalkingGrade") != null )
    {
        removeElement("trWalkingGrade");
    }
    
    var option = new Option();
    option.value = 999;
    option.innerHTML = "Tell me more";
    
    
    if( bPreviousSearch )
    {
        cbxWalkingGrades = populateComboBoxes4("cbxWalkingGrades",grades, intSelectedIndex );
        cbxWalkingGrades.appendChild( option );
        addRow("tblSearch","trWalkingGrade", cbxWalkingGrades );
    }
    else
    {
        cbxWalkingGrades = populateComboBoxes4("cbxWalkingGrades",grades);
        cbxWalkingGrades.appendChild( option );
  
        var desc = populateDescription( "divWalkingGradesDesc","Choose a walking grade");
        addRow( "tblSearch","trWalkingGradeDesc", desc );
        addRow("tblSearch","trWalkingGrade", cbxWalkingGrades );
    } 
    cbxWalkingGrades.onchange = new Function("processWalkingGrades();"); 
}

function processWalkingGrades()
{
   if( document.getElementById("trWalkingGradeDesc") )
   {
        removeElement("trWalkingGradeDesc");
   }
    var intWalkingGrade = parseInt( document.getElementById("cbxWalkingGrades").value );
    
    if( ( intWalkingGrade == 11 ) || ( intWalkingGrade == 12 ) )
    {
        alert("You cannot search on Easiest or Hardest");
    }
    else if( intWalkingGrade == 999 )
    {
        window.location="/Engine.aspx?pageID=126";
    }
    else
    {
        
        populateRemoveLastSection();
        populateGo();
        populateFineSearch();
        
        var form = document.forms["frmSearch"];
        form.submit();
    }
   
}

function populateDurations( bPreviousSearch, intSelectedIndex )
{
    
    
    var cbxDurations;
    
    if( document.getElementById("trDurations") != null )
    {
        removeElement("trDurations");
    }
    
    if( bPreviousSearch )
    {
        cbxDurations = populateComboBoxes2("cbxDurations",durations, intSelectedIndex );
        addRow("tblSearch","trDurations", cbxDurations );
    }
    else
    {
        cbxDurations = populateComboBoxes("cbxDurations", durations);
      
        var desc = populateDescription("divDurationsDesc","Choose the Duration");
        addRow("tblSearch","trDurationsDesc", desc );
        addRow("tblSearch","trDurations", cbxDurations );
     } 
     
     cbxDurations.onchange = new Function("processDurations();");
   
}

function processDurations()
{
     if( document.getElementById("trDurationsDesc") )
     {
        removeElement("trDurationsDesc");
     }
     
     populateRemoveLastSection();
     populateGo();
     populateFineSearch();
     
     var form = document.forms["frmSearch"];
     form.submit();
}

function populateDepartureAirports( bPreviousSearch, intSelectedIndex)
{
    //var departureAirports = new Array("Airports...","Birmingham","Gatwick","Glasgow","Heathrow","Luton","Manchester","Newcastle","Stansted");
    
    var cbxDepartureAirports;
    
    if( document.getElementById("trDepartureAirports")!= null )
    {
        removeElement("trDepartureAirports");
    }
    
    if( bPreviousSearch )
    {
        cbxDepartureAirports = populateComboBoxes4("cbxDepartureAirports",departureAirports, intSelectedIndex );
        addRow("tblSearch","trDepartureAirports", cbxDepartureAirports );
    }
    else
    {    
        cbxDepartureAirports = populateComboBoxes4("cbxDepartureAirports", departureAirports);
    
        var desc = populateDescription("divDepartureAirportDesc","Choose departure point");
        addRow( "tblSearch","trDepartureAirportDesc", desc );
        addRow("tblSearch","trDepartureAirports", cbxDepartureAirports );
    } 
    cbxDepartureAirports.onchange = new Function("processDepartureAirport();");

}

function processDepartureAirport()
{
   if( document.getElementById("trDepartureAirportDesc") )
   {
        removeElement("trDepartureAirportDesc");
   }
   
   populateRemoveLastSection();
   populateGo();
   populateFineSearch();
        
   var form = document.forms["frmSearch"];
   form.submit();
}


function populateDate( bPreviousSearch, intSelectedDay, intSelectedMonth, intSelectedYear, intGiveOrTake )
{
    
    var months = new Array("", "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sept","Oct","Nov","Dec"); 
    var giveOrTakes = new Array("1 wk","2wk","1 mth");
    
    var today = new Date();
    var thisDay = today.getDate();
    var thisMonth = today.getMonth();
    var thisYear = today.getFullYear();
    
    if( ( document.getElementById("trDate") != null ) 
        && ( document.getElementById("trGiveOrTake") != null ) )
    {
        removeElement("trDate");
        removeElement("trGiveOrTake");
    }
    
    var cbxDay = document.createElement("select");
    cbxDay.name = "cbxDay";
    cbxDay.className = "cbxDateStyle"; 
    
    for( index = 1; index <= 31; index++ )
    {    
        option = new Option();
        option.value = index;
        option.innerHTML = index;
        
        if( bPreviousSearch )
        {
            if( index == intSelectedDay )
            {
                option.selected = true;
            }
        }
        else
        {
            if( index == thisDay )
            {
                option.selected = true;
            }
        }
        cbxDay.appendChild( option );   
    }
    
    var cbxMonth = document.createElement("select");
    cbxMonth.name = "cbxMonth";
   cbxMonth.className = "cbxDateStyle"; 
   
 
    for( index = 1; index < months.length; index++ )
    {    
        option = new Option();
        option.value = index;
        option.innerHTML = months[index];
        
        if( bPreviousSearch )
        {
            if( index == intSelectedMonth )
            {
                option.selected = true;
            }
        }
        else
        {
            if( thisMonth == index - 1)
            {
                option.selected = true;
            }
        }
        cbxMonth.appendChild( option );   
    }
    
    var cbxYear = document.createElement("select");
    cbxYear.name = "cbxYear";
   cbxYear.className = "cbxDateStyle"; 
    
    for( index = 2007; index <= 2012; index++ )
    {    
        option = new Option();
        option.value = index;
        var y = index - 2000;
        if( y < 10 )
        {
            option.innerHTML = "0" + y;
        }
        else
        {
            option.innerHTML = y;
        }
        if( bPreviousSearch )
        {
            if( index == intSelectedYear )
            {
                option.selected = true;
            }
        }
        else
        {
            if( index == thisYear )
            {
                option.selected = true;
            }
        }
        
        cbxYear.appendChild( option );   
    }
    
    var cbxGiveOrTake = populateComboBoxes2("cbxGiveOrTake", giveOrTakes, intGiveOrTake );
   cbxGiveOrTake.className = "cbxDateStyle"; 
 
    var cell = document.createElement("div");
    var cell2 = document.createElement("div");
       
    cell.appendChild( cbxDay );
    cell.appendChild( cbxMonth);
    cell.appendChild( cbxYear );
      
    cell2.innerHTML = "Give or Take?";
    cell2.className = "giveOrTakeFont"; 
    cell2.appendChild( cbxGiveOrTake );
    cell2.align = "right";
    cell2.width = "100%";
   
   var desc = populateDescription("divDateDesc","When do you wish to go");
   
    if( bPreviousSearch )
    {
        addRow("tblSearch","trDate", cell );
        addRow("tblSearch","trGiveOrTake", cell2 );
    }
    else
    {
        addRow("tblSearch", "trDateDesc", desc ); 
        addRow("tblSearch","trDate", cell );
        addRow("tblSearch","trGiveOrTake", cell2 );
    }
    cbxDay.onchange = new Function("processDate();");
    cbxMonth.onchange = new Function("processDate();");
    cbxYear.onchange = new Function("processDate();");
    cbxGiveOrTake.onchange = new Function("processDate();"); 
    populateGo();
}

function processDate()
{
    if( document.getElementById("trDateDesc" ) )
   {
        removeElement("trDateDesc");
        populateGo();
        populateFineSearch();
   } 
}

function processFirstSearch()
{
    var firstSearch = parseInt( document.getElementById("cbxFirstSearch").value );
      
    switch( firstSearch )
    {
        case 1:
            removeElement("trFirstSearchDesc");
            removeElement("trFirstSearch");
            this.populateRegions( true, 20);
            this.populateCountries(false, - 1);
            break;
        case 2: 
            removeElement("trFirstSearchDesc");
            removeElement("trFirstSearch");
            this.populateHolidayTypes( false, -1);
            break;
        case 3:
            removeElement("trFirstSearchDesc");
            removeElement("trFirstSearch");
            this.populateWalkingGrades( false, -1);
            break;
        case 4:
            removeElement("trFirstSearchDesc");
            removeElement("trFirstSearch");
            this.populateDurations( false, -1);
            break;
        case 5:
            removeElement("trFirstSearchDesc");
            removeElement("trFirstSearch");
            this.populateDate( false, -1);
            break;

        case 6:
        
            window.location="/map_search/uk.aspx";
            break;
     }

}

function processFineSearch()
{
   var fineSearch = parseInt( document.getElementById("cbxFineSearch").value );
      
    switch( fineSearch )
    {
        case 1:
            removeElement("trFineSearchDesc");
            removeElement("trFineSearch");
            removeElement("trRemoveLastSection");
            removeElement("trGo");
            this.populateRegions( true, 20);
            this.populateCountries( true, -1 );
            break;
        case 2: 
            removeElement("trFineSearchDesc");
            removeElement("trFineSearch");
            removeElement("trRemoveLastSection");
            removeElement("trGo");
            this.populateHolidayTypes(false, -1);
            break;
        case 3:
            removeElement("trFineSearchDesc");
            removeElement("trFineSearch");
            removeElement("trRemoveLastSection");
            removeElement("trGo");
            this.populateWalkingGrades( false, -1);
            break;
        case 4:
            removeElement("trFineSearchDesc");
            removeElement("trFineSearch");
            removeElement("trRemoveLastSection");
            removeElement("trGo");
            this.populateDurations( false, -1);
            break;
        case 5:
            removeElement("trFineSearchDesc");
            removeElement("trFineSearch");
            removeElement("trRemoveLastSection");
            removeElement("trGo");
            this.populateDate( false, -1,-1,-1,-1);
            break;
        case 6:
            removeElement("trFineSearchDesc");
            removeElement("trFineSearch");
            removeElement("trRemoveLastSection");
            removeElement("trGo");
            this.populateDepartureAirports();
            break;
     }
}

function resetSearch()
{
    
    if( document.getElementById("trRegion") != null )
    {
        removeElement("trRegion"); 
        removeElement("trCountries");
    } 
    else if( document.getElementById("trHolidayType") != null ) 
    {
        removeElement("trHolidayType");
    }
    else if( document.getElementById("trWalkingGrade") != null ) 
    {
        removeElement("trWalkingGrade"); 
    }
    else if( document.getElementById("trDurations") != null )
    {
        removeElement("trDurations");
    }
    else if( ( document.getElementById("trDate") != null )
           && ( document.getElementById("trGiveOrTake") != null ) )
    { 
         removeElement("trDate"); 
        removeElement("trGiveOrTake"); 
    }
    else if( document.getElementById("trDepartureAirports") != null ) 
    {    
        removeElement("trDepartureAirports");
    } 

}

function deleteLoadingRow() {
    document.getElementById("loadingSearch").style.display = 'none';
}

function removeLastRow()
{
    
    
   if( document.getElementById("trFineSearchDesc") 
        && document.getElementById("trFineSearch")  )
   {
        removeElement("trFineSearchDesc");
        removeElement("trFineSearch");
       
   }
   
   if( document.getElementById("trRemoveLastSection") )
   {
        removeElement("trRemoveLastSection");
   }
   
   if( document.getElementById("trGo") )
   {
        removeElement("trGo");
    }
   var tableRef = document.getElementById("tblSearch");
   var pos = tableRef.rows.length - 1;
    

   if( tableRef.rows[pos].id == "trHolidayType" )
   {
        if( document.getElementById("trHolidayTypeDesc") )
        {
            removeElement("trHolidayTypeDesc");
            removeElement("trHolidayType");
            checkSearch();
           // populateFineSearch();
        }
        else
        {
            removeElement("trHolidayType");
            checkSearch();
           // populateFineSearch();
        }
   }
   
   if( tableRef.rows[pos].id == "trWalkingGrade" )
   {
        if( document.getElementById("trWalkingGradeDesc") )
        {
            removeElement("trWalkingGradDesc");
            removeElement("trWalkingGrade");
            checkSearch();
          //  populateFineSearch();
        }
        else
        {
            removeElement("trWalkingGrade");
            checkSearch();
          //  populateFineSearch();
        }
   }
   
   if( tableRef.rows[pos].id == "trGiveOrTake")
   {
        if( document.getElementById("trDateDesc") )
        {
            removeElement("trDateDesc");
            removeElement("trDate");
            removeElement("trGiveOrTake");
            checkSearch();
         //  populateFineSearch();
        }
        else
        {
            removeElement("trDate");
            removeElement("trGiveOrTake");
            checkSearch();
         //   populateFineSearch();
        }
    }
    
    if( tableRef.rows[pos].id == "trDurations" )
    {
        if( document.getElementById("trDurationsDesc") )
        {
            removeElement("trDurationsDesc");
            removeElement("trDurations");
          //  populateFineSearch();
            checkSearch();
        }
        else
        {
            removeElement("trDurations");
         //   populateFineSearch();
            checkSearch();
        }
    }
    
    if( tableRef.rows[pos].id == "trDepartureAirports" )
    {
        if( document.getElementById("trDepartureAirportDesc") )
        {
            removeElement("trDepartureAirportDesc");
            removeElement("trDepartureAirports");
         //   populateFineSearch();
            checkSearch();
        }
        else
        {
            removeElement("trDepartureAirports");
        //    populateFineSearch();
            checkSearch();
        }
    }
    
    if( tableRef.rows[pos].id == "trCountries")
    {
        if( document.getElementById("divDestination") )
        {
            removeElement("divDestination");
            removeElement("trRegion");
            removeElement("trCountries");
            checkSearch();
        }
        else
        {
            removeElement("trRegion");
            removeElement("trCountries");
            checkSearch();
        }
    }
    if( tableRef.rows[pos].id == "trFirstSearchDesc" || tableRef.rows[pos].id == "trFirstSearch") {
    } else {
        var form=document.forms['frmSearch'];form.submit();
    }

}


function addRow( tableID, rowID, cellData )
{
    var tableRef = document.getElementById( tableID );
    var pos = tableRef.rows.length;
    var newRow = tableRef.insertRow( pos );
    newRow.id = rowID;
    var newCell = newRow.insertCell( 0 );
    newCell.align = "right";
    newCell.appendChild( cellData );
}
/*
function populateFineSearch()
{
    
    var cbxFineSearch = document.createElement("select");
    cbxFineSearch.id = "cbxFineSearch";
	cbxFineSearch.className = "cbxSelectBoxStyle";
    
    
    var top = new Option();
    top.value = 0;
    top.innerHTML = "Refine Search...";
    cbxFineSearch.appendChild( top );
      

    
    if( ( document.getElementById("trRegion") != null ) 
        && ( document.getElementById("trHolidayType") != null )
        && ( document.getElementById("trWalkingGrade") != null )
        && ( document.getElementById("trDurations") != null )
        && ( document.getElementById("trDate") != null )
        && ( document.getElementById("trDepartureAirports") != null ) )
    {
      //  removeElement("trFineSearchDesc");
      //  removeElement("trFineSearch");
      populateRemoveLastSection();
      populateGo();
    }
    else
    {
        if( document.getElementById("trFineSearch") != null )
        {
            removeElement("trFineSearchDesc");
            removeElement("trFineSearch");
        }
    
        if( document.getElementById("trRegion") == null ) 
        {
            var regionOption = new Option();
            regionOption.value = 1;
            regionOption.innerHTML = "Destinations";
            cbxFineSearch.appendChild( regionOption );        
        }
    
        if( document.getElementById("trHolidayType") == null ) 
        {
            var holidayTypeOption = new Option();
            holidayTypeOption.value = 2;
            holidayTypeOption.innerHTML = "Holiday Type";
            cbxFineSearch.appendChild( holidayTypeOption );      
         }
      
        if( document.getElementById("trWalkingGrade") == null ) 
        {
            var walkingGradeOption = new Option();
            walkingGradeOption.value = 3;
            walkingGradeOption.innerHTML = "Walking Grade";
            cbxFineSearch.appendChild( walkingGradeOption );      
        } 
    
        if( document.getElementById("trDurations") == null ) 
        {
            var durationOption = new Option();
            durationOption.value = 4;
            durationOption.innerHTML = "Duration";
            cbxFineSearch.appendChild( durationOption );      
        } 
    
        if( document.getElementById("trDate") == null ) 
        {
            var dateOption = new Option();
            dateOption.value = 5;
            dateOption.innerHTML = "Date";
            cbxFineSearch.appendChild( dateOption );      
        }
        
        var desc = populateDescription( "divFineSearchDesc","Refine your search" );

         addRow("tblSearch","trFineSearchDesc", desc );
         addRow("tblSearch","trFineSearch", cbxFineSearch );
         populateRemoveLastSection();
        populateGo();
    
        cbxFineSearch.onchange = new Function( "processFineSearch();" );
     }  
}
*/

function populateRemoveLastSection()
{
    if( document.getElementById("trRemoveLastSection") )
    {
        removeElement("trRemoveLastSection");
    }
   
    var linkRemoveLastSection = document.createElement("a");
    linkRemoveLastSection.href="#";
    linkRemoveLastSection.innerHTML = "Remove Last Selection";
    linkRemoveLastSection.className = "fontHeader3";
    linkRemoveLastSection.onclick = new Function("removeLastRow();");
    
    addRow( "tblSearch","trRemoveLastSection", linkRemoveLastSection );
}


function populateGo()
{

    if( document.getElementById("trGo") )
    {
        removeElement("trGo");
    }
    
    var btnGo = document.createElement("input");
    btnGo.type = "image";
    btnGo.src = "/images/icon.Quick.Searcn.Go.gif";
    btnGo.onClick = "processForm();";
    
    addRow("tblSearch","trGo", btnGo);
    
    deleteLoadingRow();
}

function processForm()
{
	var quickSearchText = document.getElementById("inputQuickSearch");
	var quickSearchForm = document.getElementById("frmQuickSearch");
	if(quickSearchText.value != ""){
		quickSearchForm.action = "/Holiday_Search.aspx?Search=1&SearchTerm=" + quickSearchText.value;
	}
  
    return true;
}


function populateFirstSearch()
{
    
    
    var cbxFirstSearch;
    
    if( document.getElementById("trFirstSearch") != null )
    {
        removeElement("trFirstSearch");
    }
    
    cbxDurations = populateComboBoxes("cbxFirstSearch", firstSearchArray, "First Search by" );
  
     var desc = populateDescription("divFirstSearchDesc","Find your holiday");
    addRow("tblSearch","trFirstSearchDesc", desc );
    addRow("tblSearch","trFirstSearch", cbxDurations );
   
   cbxDurations.onchange = new Function("processFirstSearch();");
   
   deleteLoadingRow();
    
}


function checkSearch()
{
    var tableRef = document.getElementById("tblSearch");
    var pos = tableRef.rows.length - 1;
    if( pos == 1 )
    {
        populateFirstSearch();
    }
    else
    {
        populateFineSearch();
    }
}

function populateQuickSearch( strQuick )
{
    var txtQuick = document.getElementById("inputQuickSearch");
    txtQuick.value = strQuick;
}
