var g_ConfirmationIdObjects = new Array();

function applyDiscountCode(discountCode)
{
    if (!discountCode)
    {
        return false;
    }

    var input_el = document.getElementById("discount");
    if (!input_el || input_el.style.display == "none")
    {
        return false;
    }

    var discount_code_el = document.getElementById("discount_code");
    if (!discount_code_el)
    {
        return false;
    }

    input_el.style.display = "none";
    discount_code_el.innerHTML = discountCode;
    return true;
}

function showImage(id)
{
    var el = document.getElementById("tour_image");
    if (el)
    {
        el.innerHTML = "";
        var img = new Image();
        img.src = "images/" + id_prov + "/" + id + ".gif";
        el.appendChild(img);
    }
}

function hideImage(id)
{
    var el = document.getElementById("tour_image");
    if (el)
    {
        el.innerHTML = "";
    }
}

function showDescription(id)
{
    hide(getElem('f_description'));
    var name_el = getElem('description_header');
    if (name_el && frm.leaving && frm.leaving.selectedIndex != -1 && points && (points[frm.leaving.value] || pickup_points[frm.leaving.value]))
    {
        var journey_name = points[frm.leaving.value] || pickup_points[frm.leaving.value];
        journey_name = journey_name.replace("]x[", "");

        if (frm.going && frm.going.selectedIndex != -1 && getElem("f_going").style.display != "none")
        {
            var id_route = frm.going.value.split("_");
            var point = points[id_route[2]] || pickup_points[id_route[2]];
            if (point)
            {
                journey_name += " - " + (point.replace("]x[", ""));
            }
        }
        name_el.innerHTML = journey_name;
    }
    else if(name_el)
    {
        name_el.innerHTML = "&nbsp;";
    }

    if (typeof(desc[id]) != 'undefined')
    {
        document.getElementById('description').innerHTML = desc[id];
        show(getElem('f_description'));
    }

    if (show_image && show_image[id])
    {
        showImage(id);
    }
    else
    {
        hideImage(id);
    }
}

function changePassengerWord(idJourney)
{
    if (numberOf[idJourney])
    {
        var spanRedefined = getElem("lng_amount_redefined");

        if (spanRedefined)
        {
            spanRedefined.innerHTML = numberOf[idJourney].replace(/[\|]/g, "");
            hide(getElem("lng_amount_original"));
            show(spanRedefined);
        }
    }
    else
    {
        hide(getElem("lng_amount_redefined"));
        show(getElem("lng_amount_original"));
    }
}

function updateTicketPrices(frm, prices)
{
    var ticketIddsArr = ticket_ids.split(",");

    for (counter = 0; counter < ticketIddsArr.length; counter++)
    {
        idTicketType = ticketIddsArr[counter];

        if (!prices[idTicketType])
        {
            hide(getElem("amount_" + idTicketType));
        }
        else
        {
            var el = getElem("ticket_" + idTicketType + "_price");

            if (el)
            {
               el.innerHTML = prices[idTicketType];
            }
        }
    }
/*
    for (var id_tt in prices)
    {
         var el = getElem("ticket_" + id_tt + "_price");

         if (el)
         {
            el.innerHTML = prices[id_tt];
         }
    }
*/
}

function updateRoundJourneyPickupPoints(type, id_journey)
{
    var elDepartureCityTable = getElem('departure_city_table');
    if (type != 3)
    {
        hide(elDepartureCityTable);
        return;
    }

    arr = new Object();
    var rjppointsCount = 0;
    var curr_id_journey = frm.journey.value;
    for (var id_rj_pickup_point in rj_pickup_points[curr_id_journey])
    {
        var rjppoint = rj_pickup_points[curr_id_journey][id_rj_pickup_point];
        arr[rjppointsCount] = new ListItem("" + id_rj_pickup_point, unescapeHTML(rjppoint.name + (rjppoint.departure_time == '' ? '' : ' (' + rjppoint.departure_time + ')')));
        rjppointsCount++;
    }

    if (rjppointsCount > 0)
    {
        setupSelect(frm.departure_city, arr);
        show(elDepartureCityTable);
    }
    else if (id_prov == 3106 && cur_type != 25)
    {
        show(elDepartureCityTable);
    }
    else
    {
        clearSelect(frm.departure_city);
        hide(elDepartureCityTable);
    }
}

function updatePage(type, radio)
{
    cur_mode = types[radio];
    updateCall = "checkAvailableTime(calOracleField, " + cur_mode + ")";
    cur_type = type;
    checkRadio(radio);

    switch(Number(types[radio]))
    {
        case 1:
        {
            show(getElem('table_1'));
            hide(getElem('f_description'));
            show(getElem('f_going'));
            hide(getElem('f_returning'));
//            setupSelectsTable1(cur_type);
            setupRouteOptions();
            break;
        }
        case 2:
        {
            show(getElem('table_1'));
            hide(getElem('f_description'));
            show(getElem('f_going'));
            show(getElem('f_returning'));
//            setupSelectsTable1(cur_type);
            setupRouteOptions();
            break;
        }
        case 3:
        {
            show(getElem('table_1'));
            hide(getElem('f_description'));
            hide(getElem('f_going'));
            hide(getElem('f_returning'));
//            setupSelectsTable1(cur_type);
            setupRouteOptions();
            break;
        }
    }
    return false;
}

function getElem(id)
{
    var el = null;

    if(document.getElementById)
    {
        el = document.getElementById(id);
    }
    else
    {
        el = document.all(id);
    }

    return el;
}

function show(elem)
{
    if (!elem)
    {
        return;
    }

    try
    {
        elem.style.display = "";
    }
    catch(e) {}
}

function hide(elem)
{
    if (!elem)
    {
        return;
    }
    try
    {
        elem.style.display = "none";
    }
    catch(e) {}
}

function checkRadio(idx)
{
    if(typeof(frm.jtype.length) != 'undefined')
    {
        frm.jtype[idx].checked = true;
    }
    else
    {
        frm.jtype.checked = true;
    }

    var id_journey_type = 0;
    for (var i = 0; i < frm.jtype.length; ++i)
    {
        frm.jtype[i].parentNode.className = frm.jtype[i].parentNode.className.replace("radioSelected", "");

        if (frm.jtype[i].checked)
           frm.jtype[i].parentNode.className += " radioSelected";
    }
}

function disable(elem)
{
    elem.disabled = true;
}

function enable(elem)
{
    elem.disabled = false;
}

function initTable1()
{
    disable(frm.leaving);
    disable(frm.going);
    disable(frm.departure_time);
    disable(frm.return_time);
}

//setupSelectsTable1(type)
function setupRouteOptions()
{
/*
    clearSelect(frm.leaving);

    var arr  = new Object();
    var vals = String(data[type].start_points).split(',');
    if(vals.length == 0)
    {
        return;
    }

    var enabled = new Object();
    var tmp_arr = String(data[type].enabled_points).split(',');

    for(var i in tmp_arr)
    {
        enabled[tmp_arr[i]] = true;
    }

    var count = 0;
    var flag = false;
    for (var i in vals)
    {
        if (inArr(hiddenPoints, vals[i]))
        {
            continue;
        }


        if (vals[i].indexOf('**') == -1)
        {
            if(typeof(enabled[vals[i]]) != 'undefined')
            {
                var point_name = points[vals[i]] || pickup_points[vals[i]];

                arr[count]  = new ListItem(vals[i], unescapeHTML(point_name));
                flag        = true;
            }
        }
        else
        {
            if (vals[i] != '**null')
            {
                vals[i] = vals[i].substring(2,vals[i].length);
                arr[count] = new ListItem("-1", unescapeHTML(separators[vals[i]]));
            }
            else
            {
                if (flag)
                {
                    arr[count] = new ListItem("-1", unescapeHTML(separators[-1]));
                }
            }
        }

        count++;
    }

    setupSelect(frm.leaving, arr);
    enable(frm.leaving);

    makeHighlighted(frm.leaving);

    if(st_point)
    {
        setSelected(frm.leaving, st_point);
        st_point = 0;
    }
*/
    switch(cur_mode)
    {
        case 1:
        case 2:
        {
            updateTo(frm.going, frm.leaving);
//            updateT(frm.departure_time, frm.going, frm.departuring);
            break;
        }
        case 3:
        {
            updateT3(frm.departure_time, frm.leaving, frm.departuring);
            break;
        }
    }
}

function compareNames(a, b)
{
    var name1 = a.text;
    var name2 = b.text;

    if (name1.substr(0, 3) == "]x[")
    {
        name1 = name1.substr(3, name1.length)
    }

    if (name2.substr(0, 3) == "]x[")
    {
        name2 = name2.substr(3, name2.length)
    }

    var result = 0;

    if (name1 > name2)
    {
        result = 1;
    }
    else if (name1 < name2)
    {
        result = -1;
    }

    return result;
}

function updateTo(sel, obj, type)
{
    if(!type)
    {
        type = cur_type;
    }

    var point = obj[obj.selectedIndex].value;

    if ((point == "N/A" || point == "0") && obj[obj.selectedIndex + 1])
    {
        point = obj[obj.selectedIndex + 1].value;
    }

    clearSelect(sel);

    var arr = new Array();
    var vals = String(data[type].p[point]).split(',');

    if (vals.length == 0)
    {
        return;
    }

    var count       = 0;
    var point_name  = "";

    for (var i in vals)
    {
        if (inArr(hiddenPoints, getDataFromCompositeValue(vals[i], 2)))
        {
            continue;
        }

        point_name = points[routes[vals[i]].e] || pickup_points[routes[vals[i]].e];
        arr.push(new ListItem(vals[i], unescapeHTML(point_name)));
        count++;
    }

    setupSelect(sel, arr, (show_NA_by_deafult && arr.length > 1));
    enable(sel);

    makeHighlighted(sel, true);

    if (st_route && st_route != 0)
    {
        setSelected(sel, (String(st_route).indexOf("_") > 0 ? st_route : st_route + "_" + point), true);
        st_route = 0;
    }

    updateT(frm.departure_time, sel, frm.departuring);

    if(cur_mode == 2)
    {
        updateTR(frm.return_time, sel, frm.returning);
    }
}

function updateFlightDescription(sel)
{
    var airportPointsArr = airport_points.split(",");

    var departuringTimeText = getElem("departuring_time");
    var flightDeparturingTimeArrivalText = getElem("flight_direct_time_arrival");
    var flightDeparturingTimeDepartureText = getElem("flight_direct_time_departure");
    var directFlightArrivalText = getElem("direct_flight_arrival");
    var directFlightDepartureText = getElem("direct_flight_departure");

    var returningTimeText = getElem("returning_time");
    var flightReturningTimeArrivalText = getElem("flight_return_time_arrival");
    var flightReturningTimeDepartureText = getElem("flight_return_time_departure");
    var returnFlightArrivalText = getElem("return_flight_arrival");
    var returnFlightDepartureText = getElem("return_flight_departure");

    if (inArr(airportPointsArr, getDataFromCompositeValue(sel[sel.selectedIndex].value, 1)))
    {
        hide(departuringTimeText);
        show(flightDeparturingTimeArrivalText);
        hide(flightDeparturingTimeDepartureText);
        show(directFlightArrivalText);
        hide(directFlightDepartureText);

        if (cur_mode == 2)
        {
            hide(returningTimeText);
            hide(flightReturningTimeArrivalText);
            show(flightReturningTimeDepartureText);
            show(returnFlightDepartureText);
            hide(returnFlightArrivalText);
        }
    }
    else if (inArr(airportPointsArr, getDataFromCompositeValue(sel[sel.selectedIndex].value, 2)))
    {
        hide(departuringTimeText);
        hide(flightDeparturingTimeArrivalText);
        show(flightDeparturingTimeDepartureText);
        show(directFlightDepartureText);
        hide(directFlightArrivalText);

        if (cur_mode == 2)
        {
            hide(returningTimeText);
            show(flightReturningTimeArrivalText);
            hide(flightReturningTimeDepartureText);
            show(returnFlightArrivalText);
            hide(returnFlightDepartureText);
        }
    }
    else
    {
        show(departuringTimeText);
        hide(flightDeparturingTimeArrivalText);
        hide(flightDeparturingTimeDepartureText);
        hide(directFlightArrivalText);
        hide(directFlightDepartureText);

        if (cur_mode == 2)
        {
            show(returningTimeText);
            hide(flightReturningTimeArrivalText);
            hide(flightReturningTimeDepartureText);
            hide(returnFlightArrivalText);
            hide(returnFlightDepartureText);
        }
    }
}

function updateTR(sel, obj, dates, type)
{
    if(!type)
    {
        type = cur_type;
    }

    var idx = obj[obj.selectedIndex].value;

    if ((idx == "N/A" || idx == "0") && obj[obj.selectedIndex + 1].value)
    {
        idx = obj[obj.selectedIndex + 1].value;
    }

    var point1 = routes[idx].s;
    var point2 = routes[idx].e;

    var found = false;
    var arr_i = new Array();

    for (var i in data[type].r)
    {
        var r = routes;

        if((r[i].s == point2) && (r[i].e == point1))
        {
            found = true;
            arr_i.push(i);
            point = i;
        }
    }

    if(!found) 
    {
        return;
    }

    for (h = 0; h < arr_i.length; h++)
    {
        var r = routes;

        for (var times in r[arr_i[h]].dr)
        {
            var arr_dates = r[arr_i[h]].dr[times].split('-');

            if (arr_dates.length == 2)
            {
                var curr_date  = new Date(getStringDate(dates.value));
                var start_date = new Date(getStringDate(arr_dates[0]));
                var end_date   = new Date(getStringDate(arr_dates[1]));

                if ((curr_date >= start_date) && (curr_date <= end_date))
                {
                    var point = arr_i[h];
                    break;
                }
            }
            else
            {
                var point = arr_i[h];
                break;
            }
        }
    }

    frm.route_ret.value = point;

    hideUnavailableDates(point, dates, "return");

    if(sel.selectedIndex != -1)
    {
        var currT = sel.options[sel.selectedIndex].value || "";
    }
        
    used_season_id = 0; //getSeasonId(seasons[getDataFromCompositeValue(point, 0)], dates.value)
        
    fillTimes(sel, used_season_id, point, dates, type, frm.journey.value);

/*
    clearSelect(sel);

/////////////Seasons functionality////////////////////////////

    var arr = new Object();

    if (used_season_id)
    {
        var season_times = seasons[getDataFromCompositeValue(point, 0)][used_season_id].times.split(",");

        for (var i = 0; i < season_times.length; i++)
        {
            if (!checkNoRunSeasonTime(getDataFromCompositeValue(point, 0), used_season_id, season_times[i], dates.value))
            {
                arr[season_times[i]] = new ListItem(season_times[i], (/ANY/.test(season_times[i]) ? ANY_TITLE : season_times[i]));
            }
        }
    }
    else
    {
        var vals = String(routes[point].t).split(',');
        if(vals.length == 0) return;

        for(var i in vals)
        {
            if(!checkNoRunTime(point, vals[i], dates.value))
                arr[vals[i]] = new ListItem(vals[i], (/ANY/.test(vals[i]) ? ANY_TITLE : vals[i]));
        }
    }

    if (getArrayLength(arr) == 1)
    {
        if (typeof(arr["ANY"]) != "undefined")
        {
            var block = document.getElementById("block_" + sel.name);

            if (block)
            {
                if (block.id == "block_" + sel.name)
                {
                      block.style.visibility = "hidden";
                }
            }
        }
        else
        {
            var block = document.getElementById("block_" + sel.name);

            if (block)
            {
                if (block.id == "block_" + sel.name)
                {
                      block.style.visibility = "visible";
                }
            }
        }
    }
    else
    {
        if (!getArrayLength(arr))
        {
            var block = document.getElementById("block_" + sel.name);

            if (block)
            {
                if (block.id == "block_" + sel.name)
                {
                      block.style.visibility = "hidden";
                }
            }
        }
        else
        {
            var block = document.getElementById("block_" + sel.name);

            if (block)
            {
                if (block.id == "block_" + sel.name)
                {
                      block.style.visibility = "visible";
                }
            }
        }
    }

    if (!getArrayLength(arr))
    {
        disable(sel);
        return;
    }

    setupSelect(sel, arr);
    enable(sel);
*/
    if(currT)
    {
        setSelected(sel, currT);
    }
        
    onDirectTimeChange(0, frm.journey.value, frm.returning.value, getDataFromCompositeValue(frm.return_time[frm.return_time.selectedIndex].value, 0), getDataFromCompositeValue(point, 0), getDataFromCompositeValue(point, 1), used_season_id, point, getDataFromCompositeValue(frm.return_time[frm.return_time.selectedIndex].value, 1), "return");
}

function updateT3(sel, obj, dates, type)
{
    if(!type)
    {
        type = cur_type;
    }

    var leavingPoint = obj[obj.selectedIndex].value;

    if ((leavingPoint == "N/A" || leavingPoint == "0") && obj[obj.selectedIndex + 1])
    {
        leavingPoint = obj[obj.selectedIndex + 1].value;
    }

    var point = data[type].p[leavingPoint];

    frm.journey.value = data[type].r[point].j;
    frm.route.value = point;

    used_season_id = getSeasonId(seasons[getDataFromCompositeValue(point, 0)], dates.value);
/*
    try
    {
        updateTicketTypes(ticket_types[frm.journey.value][0][0][used_season_id]);
    }
    catch(e)
    {
        updateTicketTypes("");
    }
*/

    updateTicketTypes(ticket_types[frm.journey.value][0][0][used_season_id] || "");

    showDescription(frm.journey.value);
    changePassengerWord(frm.journey.value);

    updateRoundJourneyPickupPoints(cur_mode, frm.journey.value)
    hideUnavailableDates(point, dates, "departure");

    if (preferred_currency[frm.journey.value])
    {
        updatePreferredCurrencies(frm.currency, preferred_currency[frm.journey.value]);
    }
        
        var currT = null;

    if (sel.selectedIndex != -1)
    {
        currT = sel.options[sel.selectedIndex].value || "";
    }
        
        fillTimes(sel, used_season_id, point, dates, type, frm.journey.value);
        
/*
    clearSelect(sel);

/////////////Seasons functionality////////////////////////////

    var arr = new Object();

    if (used_season_id)
    {
        var season_times = seasons[getDataFromCompositeValue(point, 0)][used_season_id].times.split(",");

        for (var i = 0; i < season_times.length; i++)
        {
            if (!checkNoRunSeasonTime(getDataFromCompositeValue(point, 0), used_season_id, season_times[i], dates.value))
            {
                arr[season_times[i]] = new ListItem(season_times[i], (/ANY/.test(season_times[i]) ? ANY_TITLE : season_times[i]));
            }
        }
    }
    else
    {
        var vals = String(routes[point].t).split(',');

        if(vals.length == 0)
        {
            return;
        }

        for (var i in vals)
        {
            if(!checkNoRunTime(point, vals[i], dates.value))
            {
                arr[vals[i]] = new ListItem(vals[i], (/ANY/.test(vals[i]) ? ANY_TITLE : vals[i]));
            }
        }
    }

    if (getArrayLength(arr) == 1)
    {
        if (typeof(arr["ANY"]) != "undefined")
        {
            var block = document.getElementById("block_" + sel.name);
            if (block)
            {
                if (block.id == "block_" + sel.name)
                {
                    block.style.visibility = "hidden";
                }
            }
        }
        else
        {
            var block = document.getElementById("block_" + sel.name);
            if (block)
            {
                if (block.id == "block_" + sel.name)
                {
                    block.style.visibility = "visible";
                }
            }
        }
    }
    else
    {
        if (!getArrayLength(arr))
        {
            var block = document.getElementById("block_" + sel.name);

            if (block)
            {
                if (block.id == "block_" + sel.name)
                {
                      block.style.visibility = "hidden";
                }
            }
        }
        else
        {
            var block = document.getElementById("block_" + sel.name);

            if (block)
            {
                if (block.id == "block_" + sel.name)
                {
                    block.style.visibility = "visible";
                }
            }
        }
    }

    if (!getArrayLength(arr))
    {
        disable(sel);
        return;
    }

    setupSelect(sel, arr);
    enable(sel);
*/

    if (currT)
    {
        setSelected(sel, currT);
    }
        
    onDirectTimeChange(0, frm.journey.value, frm.departuring.value, getDataFromCompositeValue(frm.departure_time[frm.departure_time.selectedIndex].value, 0), getDataFromCompositeValue(frm.route.value, 0), getDataFromCompositeValue(frm.route.value, 1), used_season_id, frm.route.value, getDataFromCompositeValue(frm.departure_time[frm.departure_time.selectedIndex].value, 1), "direct");

    buildConfirmationDatesSelection();
    buildExtraHTML();
}

function updateT(sel, obj, dates, type)
{
    if(!type)
    {
        type = cur_type;
    }

    var point = obj[obj.selectedIndex].value;

    if ((point == "N/A" || point == "0") && obj[obj.selectedIndex + 1])
    {
        point = obj[obj.selectedIndex + 1].value;
    }

    frm.journey.value = data[type].r[point].j;
    frm.route.value = point;

    var used_season_id = getSeasonId(seasons[getDataFromCompositeValue(point, 0)], dates.value);

    var point_1 = frm.leaving[frm.leaving.selectedIndex].value;
    var point_2 = getDataFromCompositeValue(obj[obj.selectedIndex].value, 2);

    if (ticket_types[frm.journey.value][getDataFromCompositeValue(point, 0)] && ticket_types[frm.journey.value][getDataFromCompositeValue(point, 0)][point_1])
    {
        updateTicketTypes(ticket_types[frm.journey.value][getDataFromCompositeValue(point, 0)][point_1][used_season_id]);
    }
    else if (ticket_types[frm.journey.value][0])
    {
        updateTicketTypes(ticket_types[frm.journey.value][0][0][used_season_id]);
    }
/*
    if (pickup_points[point_1])
    {
        try
        {
                updateTicketTypes(ticket_types[frm.journey.value][getDataFromCompositeValue(point, 0)][point_1][used_season_id]);
        }
        catch(ex)
        {
                updateTicketTypes("");
        }

    }
    else
    {
        try
        {
                updateTicketTypes(ticket_types[frm.journey.value][0][0][used_season_id]);
        }
        catch(ex)
        {
                updateTicketTypes("");
        }
    }
*/
    updateRoundJourneyPickupPoints(cur_mode, frm.journey.value);

    hideUnavailableDates(point, dates, "departure");

    showDescription(frm.journey.value);
    changePassengerWord(frm.journey.value);
        
    fpoints.value = routes[point].e;

    if (preferred_currency[frm.journey.value] != null)
    {
        updatePreferredCurrencies(frm.currency, preferred_currency[frm.journey.value]);
    }

    if(sel.selectedIndex != -1)
    {
        var currT = sel.options[sel.selectedIndex].value || "";
    }
        
    fillTimes(sel, used_season_id, point, dates, type, frm.journey.value);
/*
    clearSelect(sel);

/////////////Seasons functionality////////////////////////////

    var arr = new Object();

    if (used_season_id)
    {
        var season_times = seasons[getDataFromCompositeValue(point, 0)][used_season_id].times.split(",");

        for (var i = 0; i < season_times.length; i++)
        {
            if (!checkNoRunSeasonTime(getDataFromCompositeValue(point, 0), used_season_id, season_times[i], dates.value))
            {
                arr[season_times[i]] = new ListItem(season_times[i], (/ANY/.test(season_times[i]) ? ANY_TITLE : season_times[i]));
            }
        }
    }
    else
    {
        var vals = String(routes[point].t).split(',');

        if(vals == '')
        {
            disable(sel);
            return;
        }

        for(var i in vals)
        {
            if(!checkNoRunTime(point, vals[i], dates.value))
            {
                arr[vals[i]] = new ListItem(vals[i], (/ANY/.test(vals[i]) ? ANY_TITLE : vals[i]));
            }
        }
    }

    if (getArrayLength(arr) == 1)
    {
        if (typeof(arr["ANY"]) != "undefined")
        {
            var block = document.getElementById("block_" + sel.name);

            if (block)
            {
                if (block.id == "block_" + sel.name)
                {
                    block.style.visibility = "hidden";
                }
            }
        }
        else
        {
            var block = document.getElementById("block_" + sel.name);

            if (block)
            {
                if (block.id == "block_" + sel.name)
                {
                    block.style.visibility = "visible";
                }
            }
        }
    }
    else
    {
        if (!getArrayLength(arr))
        {
            var block = document.getElementById("block_" + sel.name);

            if (block)
            {
                if (block.id == "block_" + sel.name)
                {
                    block.style.visibility = "hidden";
                }
            }
        }
        else
        {
            var block = document.getElementById("block_" + sel.name);

            if (block)
            {
                if (block.id == "block_" + sel.name)
                {
                    block.style.visibility = "visible";
                }
            }
        }
    }

    if (!getArrayLength(arr))
    {
        disable(sel);
        return;
    }

    setupSelect(sel, arr);
    enable(sel);
*/
    if(currT)
    {
        setSelected(sel, currT);
    }
        
    updateFlightDescription(obj);
      
    onDirectTimeChange(0, frm.journey.value, frm.departuring.value, getDataFromCompositeValue(frm.departure_time[frm.departure_time.selectedIndex].value, 0), getDataFromCompositeValue(frm.route.value, 0), getDataFromCompositeValue(frm.route.value, 1), used_season_id, frm.route.value, getDataFromCompositeValue(frm.departure_time[frm.departure_time.selectedIndex].value, 1), "direct");

    buildConfirmationDatesSelection();
    buildExtraHTML();
}

function buildExtraHTML()
{
    var extras_viewstate = new Object();

    for (var id_extra in extras[frm.journey.value])
    {
        if ((getElem("extra_tickets_" + id_extra) && getElem("extra_tickets_" + id_extra).style.display == "none") || extras[frm.journey.value][id_extra].by_default != "Y")
        {
            extras_viewstate[id_extra] = "none";
        }
    }

    var extras_div = getElem("extras_div");

    if (extras_div)
    {
        extras_div.innerHTML = "";
    }

    if (extras[frm.journey.value])
    {
        var extras_header = document.createElement("div");
        extras_header.className = "SubHeader";
        extras_header.appendChild(document.createTextNode("Also available:"));

        extras_div.appendChild(extras_header);

        var table = document.createElement("table");
        table.setAttribute("cellpadding", "0");
        table.setAttribute("cellspacing", "0");
        table.style.width = "100%";

        var is_first = true;

        for (var id_extra in extras[frm.journey.value])
        {
            var td = insertRow(table);

            if (!is_first)
            {
               var divider = document.createElement("div");
               divider.className = "HDivider";
               td.appendChild(divider);
            }

            var extra = extras[frm.journey.value][id_extra];

            var extra_name = document.createElement("div");
            extra_name.className = "cellText2 ExtraName";
            extra_name.appendChild(document.createTextNode(unescapeHTML(extra.name)));
            td.appendChild(extra_name);

            var extra_description = document.createElement("div");
            extra_description.className = "ExtraDescription";
            extra_description.appendChild(document.createTextNode(unescapeHTML(extra.description)));
            td.appendChild(extra_description);

            td = insertRow(table);
            td.className = "ExtraRadioButtons";

            // yes radio button
            try
            {
                var radio = document.createElement("<input type='radio' name='extra_book_" + id_extra + "' id='extra_book_" + id_extra + "' value='Y' " + (extras_viewstate[id_extra] != "none" ? "checked" : "") + " onclick='showExtraPrices(" + id_extra + ")' />");
            }
            catch(ex)
            {
                var radio = document.createElement("input");
                radio.setAttribute("type", "radio");
                radio.setAttribute("name", "extra_book_" + id_extra);
                radio.setAttribute("id", "extra_book_" + id_extra);
                radio.setAttribute("value", "Y");
                radio.setAttribute("onclick", "showExtraPrices(" + id_extra +")");
                radio.checked = (extras_viewstate[id_extra] != "none");
            }

            td.appendChild(radio);
            td.appendChild(document.createTextNode(" Yes, Please  "));


            // no radio button
            try
            {
                var radio = document.createElement("<input type='radio' name='extra_book_" + id_extra + "' id='extra_book_" + id_extra + "' value='N' " + (extras_viewstate[id_extra] == "none" ? "checked" : "") + " onclick='hideExtraPrices(" + id_extra + ")' />");
            }
            catch(ex)
            {
                var radio = document.createElement("input");
                radio.setAttribute("type", "radio");
                radio.setAttribute("name", "extra_book_" + id_extra);
                radio.setAttribute("id", "extra_book_" + id_extra);
                radio.setAttribute("onclick", "hideExtraPrices(" + id_extra + ")");
                radio.setAttribute("value", "N");
                radio.checked = (extras_viewstate[id_extra] != "none");
            }

            radio.checked = (extras_viewstate[id_extra] == "none");

            td.appendChild(radio);
            td.appendChild(document.createTextNode(" No, Thanks."));

            // extras tickets
            var extra_tickets = extra.tickets.split(",");

            var prices_table = document.createElement("table");
            prices_table.className = "TicketTypesTable";
            prices_table.setAttribute("cellpadding", "0");
            prices_table.setAttribute("cellspacing", "0");
            prices_table.id = "extra_tickets_" + id_extra;

            prices_table.style.display = (extras_viewstate[id_extra] == "none" ? extras_viewstate[id_extra] : "");

            for (var i = 0; i < extra_tickets.length; i++)
            {
                if (!extra.prices[i])
                   continue;

                var price_str = extra.prices[i];
                var tr = prices_table.insertRow(-1);

                td = tr.insertCell(-1);
                td.className = "name nameSpecialWidth";
                td.appendChild(document.createTextNode(ticket_names[extra_tickets[i]]));

                td = tr.insertCell(-1);
                td.className = "amount";

                try
                {
                    var select = document.createElement("<select class='fields2' name='extra_ticket_" + extra_tickets[i] + "_" + id_extra + "' id='extra_ticket_" + extra_tickets[i] + "_" + id_extra + "'>");
                }
                catch(ex)
                {
                    var select = document.createElement("select");
                    select.setAttribute("name", "extra_ticket_" + extra_tickets[i] + "_" + id_extra);
                    select.setAttribute("id", "extra_ticket_" + extra_tickets[i] + "_" + id_extra);
                    select.setAttribute("class", "fields2")
                }

                fillNumeric(select, 0, extras[frm.journey.value][id_extra].max_allowed);
                td.appendChild(select);

                var span = document.createElement("span");
                span.id = "extra_" + id_extra + "_price_" + extra_tickets[i];
                span.appendChild(document.createTextNode(unescapeHTML(price_str)));

                var td = tr.insertCell(-1);
                td.className = "price";
                td.appendChild(span);
                td.appendChild(document.createTextNode(" each"));
            }

            td = insertRow(table);
            td.className = "ExtraTickets";
            td.appendChild(prices_table);

            is_first = false;
        }

        extras_div.appendChild(table);
    }
}

function hideExtraPrices(id_extra)
{
    if (document.getElementById("extra_tickets_" + id_extra))
    {
        document.getElementById("extra_tickets_" + id_extra).style.display = "none";
    }
}

function showExtraPrices(id_extra)
{
    if (document.getElementById("extra_tickets_" + id_extra))
    {
        document.getElementById("extra_tickets_" + id_extra).style.display = "";
    }
}

function updateExtraPrices(extra_prices)
{
    if (!extra_prices)
    {
        return false;
    }

    for (var id_extra in extra_prices)
    {
        var oExtra = extra_prices[id_extra];
        var tickets = oExtra.tickets.split(", ");
        for (var i = 0; i < tickets.length; ++i)
        {
            var id_ticket = parseInt(tickets[i]);
            if (id_ticket)
            {
                var span_id = "extra_" + id_extra + "_price_" + id_ticket;
                var span_el = document.getElementById(span_id);
                if (span_el)
                {
                    span_el.innerHTML = "";
                    span_el.appendChild(document.createTextNode(unescapeHTML(oExtra.prices[i])));
                }
            }
        }
    }
}

function buildConfirmationDatesSelection()
{
    g_ConfirmationsIdObjects = null;
    g_ConfirmationsIdObjects = [];

    var confirmations_div = document.getElementById("confirmations_div");

    if (confirmations_div)
    {
        confirmations_div.innerHTML = "";
    }

    var confirmations_tr = document.getElementById("confirmations_tr");

    if (show_confirmations[frm.journey.value])
    {
        var confirmations_title = document.createElement("div");
        confirmations_title.className = "SubHeader";
        confirmations_title.appendChild(document.createTextNode("Book Your Additional Tour:"));
        confirmations_div.appendChild(confirmations_title);

        var confirmations_table = document.createElement("table");
        confirmations_table.setAttribute("cellpadding", "0");
        confirmations_table.setAttribute("cellspacing", "0");
        confirmations_table.className = "JourneyParam";

        var is_first = true;

        for (var id_confirmation in show_confirmations[frm.journey.value])
        {
            var tr = confirmations_table.insertRow(-1);
            var td = tr.insertCell(-1);

            //td.height = 12;
            //td.colSpan = 2;

            if (!is_first)
            {
                var divider = document.CreateElement("div");
                divider.className = "HDivider";
                td.appendChild(divider);
            }

            //td.className = "ExtraName";
            td.colSpan = 2;
            var confirmation_name = document.createElement("div");
            confirmation_name.className = "cellText2 ExtraName";
            confirmation_name.appendChild(document.createTextNode(show_confirmations[frm.journey.value][id_confirmation].name));

            td.appendChild(confirmation_name);

            var tr = confirmations_table.insertRow(-1);
            var td = tr.insertCell(-1);
            td.className = "SubHeader FieldCaption FirstParam";
            td.appendChild(document.createTextNode("Departing:"));  // add correct spellling

            td_for_picker = tr.insertCell(-1);
            td_for_picker.className = "FirstParam";
            /*
            var day_id = "_day_confirmation_" + id_confirmation + "_departuring";
            var month_id = "_month_confirmation_" + id_confirmation + "_departuring";
            var year_id = "_year_confirmation_" + id_confirmation + "_departuring";
            */
            var visual_id = "visual_confirmation_" + id_confirmation + "";
            var hidden_id = "confirmation_" + id_confirmation + "";
            var special_hidden_id = "confirmation_" + id_confirmation + "__special_field";
            /*
            var idObjIndex = g_ConfirmationIdObjects.length;
            g_ConfirmationIdObjects.push({Day: day_id, Month: month_id, Year: year_id, OldDate: hidden_id, Date: sp_hidden_id});
              */
            var visual_date_field = document.createElement("select");
            visual_date_field.id = visual_id;
            visual_date_field.name = visual_id;
            visual_date_field.className = "ParamField";

            /*var date = new Date();
            date.setDate(date.getDate() + 1);
            var current_date = getJSDate(date);
            */

            var current_date = document.getElementById("departuring").value;

            var hidden_date_field = document.createElement("input");
            hidden_date_field.type = "hidden";
            hidden_date_field.id = hidden_id;
            hidden_date_field.name = hidden_id;

            var date = new Date();
            date.setDate(date.getDate() + 1);
            hidden_date_field.value = getJSDate(date);

            var sp_hidden_date_field = document.createElement("input");
            sp_hidden_date_field.type = "hidden";
            sp_hidden_date_field.id = special_hidden_id;
            sp_hidden_date_field.name = special_hidden_id;

            /*
            var day_select = createDatePickerItem(day_id, idObjIndex);
            var month_select = createDatePickerItem(month_id, idObjIndex);
            var year_select = createDatePickerItem(year_id, idObjIndex);

            var image_link = document.createElement("a");
            image_link.href = "javascript:doNothing();";
            image_link.onclick = function (event)
            {
                stopBubble(event);
                var index = parseInt(idObjIndex.toString(), 10);
              //  showCalendar(g_ConfirmationIdObjects[index-1].OldDate, g_ConfirmationIdObjects[index-1].Day, event, g_ConfirmationIdObjects[index-1]);
            };

            var image = document.createElement("img");
            image.src = "images/calendar.gif";
            image.style.vertical_align = "bottom";
            image.style.border = "0";
            image_link.appendChild(image);

            buildDaySelectorWithObject(hidden_date_field, day_select);
            buildMonthSelectorWithObject(hidden_date_field, month_select);
            buildYearSelectorWithObject(hidden_date_field, 0, year_select);
            */
            td_for_picker.appendChild(visual_date_field);
            td_for_picker.appendChild(hidden_date_field);
            td_for_picker.appendChild(sp_hidden_date_field);
           /* td_for_picker.appendChild(day_select);
            td_for_picker.appendChild(month_select);
            td_for_picker.appendChild(year_select);*/
            td_for_picker.appendChild(document.createTextNode(" "));
//            td_for_picker.appendChild(image_link);

            is_first = false;

        }

        confirmations_div.appendChild(confirmations_table);

        confirmations_tr.style.display = "";

        for (var id_confirmation in show_confirmations[frm.journey.value])
        {
            var visual_id = "visual_confirmation_" + id_confirmation + "";
            var hidden_id = "confirmation_" + id_confirmation + "";
            var special_hidden_id = "confirmation_" + id_confirmation + "__special_field";
            var hidden_date_field = document.getElementById(hidden_id);

            setVisualDate(hidden_date_field);

            //------------for DatePicker --------------------//

           // $(document).ready(function(){
            $("#"+hidden_id).datepicker($.extend({},
            $.datepicker.regional[""], {
            beforeShow: readLinked,
            onSelect: updateLinked,
            duration: "slow",
            showStatus: true,
            closeAtTop: false,
            mandatory: true,
            dateFormat: "yymmdd",
            minDate: new Date(2007, 1 - 1, 1),
            maxDate: new Date(2010, 12 - 1, 31),
            showOn: "both",
            buttonImage: "images/calendar.gif",
            buttonImageOnly: true,
            isLegend: true,
            specialDays: fullDays
            } ));

            $("#"+visual_id).focus(function() {
              $("#"+hidden_id).datepicker("show");
             $("#"+visual_id).blur();
             $("#ui-datepicker-div").focus();  $("#"+special_hidden_id).focus(); });

            //------------------------------------------//

        }

    }
    else if (confirmations_tr)
    {
        confirmations_tr.style.display = "none";
    }
}

function createLineImage()
{
    try
    {
        var image_line = document.createElement("<img width=\"100%\" height=\"1\" border=\"0\" src=\"images/pixel_line.gif\" />");
    }
    catch(ex)
    {
        var image_line = document.createElement("img");
        image_line.setAttribute("width", "100%");
        image_line.setAttribute("height", "1");
        image_line.setAttribute("border", "0");
        image_line.setAttribute("src", "images/pixel_line.gif");
    }

    return image_line;
}

function createDatePickerItem(main_id, id_object_index)
{
    var day_select = document.createElement("select");
    day_select.name= main_id;
    day_select.id = main_id;
    day_select.className = "fields";
    day_select.onchange = function (event)
    {
        var index = parseInt(id_object_index.toString(), 10);
        onDateSelectorChange(g_ConfirmationIdObjects[index - 1]);
    }
    /*
    try
    {
        var day_select = document.createElement("<select name=\"" + main_id + "\" id=\"" + main_id + "\" class=\"fields\"  onclick=\"updateDate(document.getElementById('" + day_id + "'), document.getElementById('" + month_id + "'), document.getElementById('" + year_id + "'), document.getElementById('" + hidden_id + "'))\" />");
    }
    catch(ex)
    {
        var day_select = document.createElement("select");

        day_select.name = main_id;
        day_select.id = main_id;
        day_select.className = "fields";
        day_select.setAttribute("onclick", "updateDate(document.getElementById('" + day_id + "'), document.getElementById('" + month_id + "'), document.getElementById('" + year_id + "'), document.getElementById('" + hidden_id + "'))");
    }
    */

    return day_select;
}

function insertRow(table)
{
    tr = table.insertRow(-1);
    td = tr.insertCell(-1);

    return td;
}

function getArrayLength(array)
{
    var count = 0;
    for (var id in array)
        count ++;
    return count;
}

function checkNoRunSeasonTime(point, season_id, time, dates)
{
    var date    = new Date(getStringDate(dates));
    var weekDay = date.getDay();

    var rdates  = seasons[point][season_id].dr[time] || "";
    var rtimes  = seasons[point][season_id].nr[time] || "";

    if (!rdates && !rtimes)
    {
        return false;
    }

    rdates = rdates.split("|");
    rtimes = rtimes.split("|");

    for (var i in rdates)
    {
        var dArr    = rdates[i].split("-");
        var tstr    = rtimes[i] || "";
        var start   = new Date(getStringDate(dArr.length == 2 ? dArr[0] : dates));
        var end     = new Date(getStringDate(dArr.length == 2 ? dArr[1] : dates));

        if ((start <= date) && (date <= end))
        {
            return (inArr(tstr.split(','), String(weekDay)));
        }
    }

    return true;
}

function checkNoRunTime(point, time, dates)
{
    var date = new Date(getStringDate(dates));
    var weekDay = date.getDay();

    var rdates = routes[point].dr[time] || "";
    var rtimes = routes[point].nr[time] || "";

    if(!rdates && !rtimes)
        return false;

    rdates = rdates.split("|");
    rtimes = rtimes.split("|");

    for(var i in rdates)
    {
        var dArr = rdates[i].split("-");
        var tstr = rtimes[i] || "";
        var start = new Date(getStringDate(dArr.length == 2 ? dArr[0] : dates));
        var end   = new Date(getStringDate(dArr.length == 2 ? dArr[1] : dates));

        if (((start <= date) && (date <= end)))
        {
            return(inArr(tstr.split(','), String(weekDay)));
        }
    }
    return true;
}

function checkTimeRange(d_start, t_start, hours, dateForProvider)
{
    hours = Number(hours) || 0;

    var current = dateForProvider || new Date();
    var start = new Date(d_start);

    if(/ANY/.test(t_start))
    {
        t_start = current.getHours() + ":" + (current.getMinutes() + 1);
    }

    if(t_start.indexOf(":") != -1)
    {
        var t_arr = t_start.split(":");

        if(t_arr.length == 2)
        {
            start.setHours(t_arr[0]);
            start.setMinutes(t_arr[1]);
        }
    }

    if(current.getTime() > start.getTime()) return false;

    var diff = start.getTime() - current.getTime();

    if(diff < hours * 60 * 60 * 1000)
    {
        return false;
    }

    return true;
}

function checkCutOffTime(d_start, t_start, cut_off_time, dateForProvider)
{
    if (cut_off_time)
    {
        var current = new Date(dateForProvider) || new Date();
        var date_tomorrow = new Date(dateForProvider) || new Date();
        date_tomorrow.setDate(date_tomorrow.getDate() + 1);

        var start = new Date(d_start);

        if(/ANY/.test(t_start))
        {
            t_start = current.getHours() + ":" + (current.getMinutes() + 1);
        }

        if(t_start.indexOf(":") != -1)
        {
            var t_arr = t_start.split(":");
            if(t_arr.length == 2)
            {
                start.setHours(t_arr[0]);
                start.setMinutes(t_arr[1]);
            }
        }

        var cut_off_time_arr = cut_off_time.split(":");

        if ((getJSDate(start) == getJSDate(current)) || (getJSDate(start) == getJSDate(date_tomorrow) && ((current.getHours() > cut_off_time_arr[0]) || (current.getHours() == cut_off_time_arr[0] && current.getMinutes() > cut_off_time_arr[1]))))
        {
            return false;
        }
    }

    return true;
}

function checkDates(d_start, t_start)
{
    if(t_start == "ANY")
    {
        t_start = "23:59";
    }

    if(t_start.indexOf(":") == -1)
    {
        t_start = '';
    }

    var current = new Date();
    var start = new Date(d_start);

    if(t_start)
    {
        var t_arr = t_start.split(":");
        if(t_arr.length == 2)
        {
            start.setHours(t_arr[0]);
            start.setMinutes(t_arr[1]);
        }
    }

    if(current.getTime() > start.getTime())
    {
        return false;
    }

    return true;
}

function checkReturnJourneyDates(d_start, t_start, d_end, t_end, question_string)
{
    question_string = question_string || "You have selected a return on the same day as your outward journey. Are you sure about this?";
    var flag = false;

    if(t_start == "ANY")
    {
        t_start = "23:59";
    }

    if(t_end == "ANY")
    {
        flag = true;
        t_end = "23:59";
    }

    if(t_start.indexOf(":") == -1)
    {
        t_start = '';
    }

    if(t_end.indexOf(":") == -1)
    {
        t_end = '';
    }

    var start = new Date(d_start);

    if(t_start)
    {
        var t_arr = t_start.split(":");
        if(t_arr.length == 2)
        {
            start.setHours(t_arr[0]);
            start.setMinutes(t_arr[1]);
        }
    }

    var end   = new Date(d_end);

    if(t_end)
    {
        var t_arr = t_end.split(":");
        if(t_arr.length == 2)
        {
            end.setHours(t_arr[0]);
            end.setMinutes(t_arr[1]);
        }
    }

    if(start.getTime() > end.getTime())
    {
        return false;
    }
    else if (start.getTime() == end.getTime() && flag)
    {
        return confirm(question_string);
    }

    return true;
}

function getStringDate(date)
{
    return (date.substring(4, 6) + "/" +
            date.substring(6, 8) + "/" +
            date.substring(0, 4));
}

function checkAvailableTime(fld, cur_mode)
{
    switch(fld.name)
    {
        case 'departuring':
        {
            if(cur_mode != 3)
            {
                updateT(frm.departure_time, frm.going, frm.departuring);
            }
            else
            {
                updateT3(frm.departure_time, frm.leaving, frm.departuring);
            }
            break;
        }
        case 'returning':
        {
            updateTR(frm.return_time, frm.going, frm.returning);
            break;
        }
    }

    if(cur_mode == 2 && fld.name == 'departuring')
    {
        //validateReturnDate(true);
        updateTR(frm.return_time, frm.going, frm.returning);
    }
}

function inArr(arr, val)
{
    for(var i in arr)
    {
        if(arr[i] == val)
            return true;
    }
    return false;
}

function hideUnavailableDates(point, dates, direction)
{
    var date = new Date(getStringDate(dates.value));
    var curr_route = point;

    current_global_route = curr_route;

    var ok = false;

    var today = new Date();
    today.setHours(0,0,0,0);

    var periods = new Array();
    var periods_nr = new Array();
    var period_i = 0;

    var current_period_i = -1;

    var this_day = specialDay(date, null);
    if (this_day[0])
    {
        ok = true;
    }

    if (!ok)
    {
        var season_id = getSeasonId(seasons[getDataFromCompositeValue(curr_route, 0)], getJSDate(date));

        var source_variable = "";

        if (season_id)
        {
            source_variable = seasons[getDataFromCompositeValue(curr_route, 0)][season_id];
            source_variable.t = new Object();
            source_variable.t = seasons[getDataFromCompositeValue(curr_route, 0)][season_id].times;
        }
        else
        {
            source_variable = routes[curr_route];
        }

//    var times_arr = routes[curr_route].t.split(",");
        var times_arr = source_variable.t.split(",");

        var in_period = -1;

        for(var times_id in times_arr)
        {
            var current_time = times_arr[times_id];

            if ((typeof(source_variable.dr) != "undefined") && (typeof(source_variable.dr[current_time]) != "undefined") && (source_variable.dr[current_time] != ""))
            {
                var temp_current_period = source_variable.dr[current_time];
                temp_current_period = temp_current_period.split("|");

//                var temp_current_norun = source_variable.nr[current_time];
//                temp_current_norun = temp_current_norun.split("|");

                for (var temp_id_p in temp_current_period)
                {
                    periods[period_i] = temp_current_period[temp_id_p];
//                    periods_nr[period_i]= temp_current_norun[temp_id_p];
                    if (inThisPeriod(date, periods[period_i]))
                    {
                        in_period = period_i;
                    }

                    period_i++;
                }
            }
            else
            {
                in_period = 0;
            }
        }

        var day_ok = false;
        do
        {
            if ((in_period == -1) && (periods.length > 0))
            {
                var temp_period_arr = Array();
                temp_period_arr = periods;

                var min_period_start = "";
                var min_period_start_date = new Date(9999,11,31);
                var min_period_end = "";
                //var temp_today = date;
                var temp_today = new Date();

                var temp_period_arr = Array();
                temp_period_arr = periods;
                for(var k = 0; k < temp_period_arr.length; k++)
                {
                    min_period_start = new Date(getStringDate(temp_period_arr[k].split("-")[0]));
                    min_period_end = new Date(getStringDate(temp_period_arr[k].split("-")[1]));

                    if ((min_period_start > temp_today) && (min_period_start_date > min_period_start))
                    {
                        min_period_start_date = min_period_start;
                        in_period = k;
                    }
                }

                if ((min_period_start_date.getTime() < (new Date()).getTime()) || (min_period_start_date.getTime() == (new Date(9999,11,31)).getTime()))
                {
                    date = new Date(); // for show something in date field
                    var this_day = specialDay(date, null);
                    if (!this_day[0])
                    {
                        alert("No days available.");
                    }

                    break;
                }

                date = min_period_start_date;

                if (inThisPeriod(date, periods[in_period]))
                {
                    var this_day = specialDay(date, null);
                    if (this_day[0])
                    {
                       day_ok = true; //break; // end loop
                    }
                }
                else
                {
                    in_period = -1;
                }

            }
            else
            {
                date = setNextDay(date, periods, false, in_period);
                if (date == null)
                {
                    date = new Date();
                    alert("No days available.");
                }

                day_ok = true;
            }
        }
        while (day_ok == false)
    }

    var current_obj = null;
    switch(direction)
    {
        case "departure":
            current_obj = document.getElementById("departuring");
            if (current_obj != null)
            {
                current_obj.value = "" + date.getFullYear() + makeTwoDigit(parseInt(date.getMonth(),10) + 1) + makeTwoDigit(date.getDate());
                setVisualDate(current_obj);
                day_ok = true;
            }
            current_obj = document.getElementById("returning");
            var return_date = new Date(current_obj.value.substr(0,4), parseInt(current_obj.value.substr(4,2),10) - 1, current_obj.value.substr(6,2));
            if (date < return_date)
            {
                break;
            }
        case "return":
            current_obj = document.getElementById("returning");
            if (current_obj != null)
            {
                current_obj.value = "" + date.getFullYear() + makeTwoDigit(parseInt(date.getMonth(),10) + 1) + makeTwoDigit(date.getDate());
                setVisualDate(current_obj);
                day_ok = true;
            }
            break;
        default:
            day_ok = true;  // escape from endless loop
            break;
    }
}

function setNextDay(input_date, periods, no_run, in_period)
{
    var date = input_date;
    var run_day = false;
    var count = 0;

//    if ((periods) && (periods.length > 0))
//    {
        do
        {
            var this_day = specialDay(date, null);
            if (this_day[0])
            {
                run_day = true;
            }

            if (run_day == false)
            {
                date.setDate(date.getDate() + 1);
            }

            count++;
        }
        while (run_day == false && count < 300); // && (inThisPeriod(date, periods[in_period]) == true));
//    }

    if (!run_day)
    {
        date = null;
    }

    return date;
}

function checkNoRunDaysForHide(no_run_days, date_selected)
{
    var flag = true;

    if (no_run_days)
    {
        no_run_days_array = no_run_days.split(",");

        var weekDay = date_selected.getDay();

        for (var i in no_run_days_array)
        {
            if (weekDay == no_run_days_array[i])
            {
                flag = false;
                break;
            }
        }
    }

    return flag;
}

function getSeasonId(seasons, date)
{
    var used_season_id = 0;

    if (!seasons)
    {
        return 0;
    }

    for (var id_season in seasons)
    {
        var season_dates = seasons[id_season].dates.split(",");

        for (var i = 0; i < season_dates.length; i++)
        {
            if (date == season_dates[i])
            {
                used_season_id = id_season;
                return used_season_id;
            }
        }
    }

    return used_season_id;
}

function getDataFromCompositeValue(value, i)
{
    return value.split("_")[i];
}

function getDaysInMonthF(oDate)
{

    var days;
    var month = oDate.getMonth()+1;
    var year  = oDate.getFullYear();

    // RETURN 31 DAYS
    if (month==1 || month==3 || month==5 || month==7 || month==8 ||
        month==10 || month==12)  {
        days=31;
    }
    // RETURN 30 DAYS
    else if (month==4 || month==6 || month==9 || month==11) {
        days=30;
    }
    // RETURN 29 DAYS
    else if (month==2)  {
        if (isLeapYear(year)) {
            days=29;
        }
        // RETURN 28 DAYS
        else {
            days=28;
        }
    }
    return (days);
}

function isLeapYearF(Year)
{
    if (((Year % 4)==0) && ((Year % 100)!=0) || ((Year % 400)==0)) {
        return (true);
    }
    else
    {
        return (false);
    }
}

function updateSelectors(input)
{
    if (input == "departuring" || input == "returning")
    {
        checkAvailableTime(document.getElementById(input), cur_mode);

        if (input == "departuring")
        {
            getTicketPrices(document.main, true, true);
        }
    }
    else
    {
        setVisualDate(document.getElementById(input));
    }
}

function fillTimes(sel, used_season_id, point, dates, type, id_journey)
{
    clearSelect(sel);
        
    getRouteFlights(point, id_journey, used_season_id, dates.value);

    var arr = new Array();
    var flag = true;

    if (used_season_id)
    {
        var idRoute = getDataFromCompositeValue(point, 0);
        var time_flights = new Object();
        var season_times = seasons[idRoute][used_season_id].times.split(",");
                
        if (season_times.length == 0)
        {
            return;
        }
                
        if (getArrayLength(seasons[idRoute][used_season_id].flights) > 0)
        {
            flag = false;
        }

        for (var i = 0; i < season_times.length; i++)
        {
            if (!checkNoRunSeasonTime(idRoute, used_season_id, season_times[i], dates.value))
            {
                            if (seasons[idRoute][used_season_id].flights && getArrayLength(seasons[idRoute][used_season_id].flights[season_times[i]]) > 0)
                {
                    time_flights = seasons[idRoute][used_season_id].flights[season_times[i]];

                    for (var id_flight in time_flights)
                    {
                        arr.push(new ListItem(season_times[i] + "_" + id_flight, time_flights[id_flight].flight_number));
                    }
                }
                else if (flag)
                {
                    arr.push(new ListItem(season_times[i], (/ANY/.test(season_times[i]) ? ANY_TITLE : season_times[i])));
                }
            }
        }
    }
    else
    {
        var vals = String(routes[point].t).split(",");

        if (vals.length == 0)
        {
            return;
        }

        if (getArrayLength(routes[point].flights) > 0)
        {
            flag = false;
        }

        for (var i in vals)
        {
            if (!checkNoRunTime(point, vals[i], dates.value))
            {
                if (routes[point].flights && getArrayLength(routes[point].flights[vals[i]]) > 0)
                {
                    for (var id_flight in routes[point].flights[vals[i]])
                    {
                        arr.push(new ListItem(vals[i] + "_" + id_flight, routes[point].flights[vals[i]][id_flight].flight_number));
                    }
                }
                else if (flag)
                {
                    arr.push(new ListItem(vals[i], (/ANY/.test(vals[i]) ? ANY_TITLE : vals[i])));
                }
            }
        }
    }
        
    arr.sort(compareNames);

    if (!flag)
    {
        arr.unshift(new ListItem("N/A", "N/A"));
    }
        
    var block = getElem("block_" + sel.name);

    if ((getArrayLength(arr) == 1 && arr[0].id == "ANY") || !getArrayLength(arr))
    {
        hide(block);
    }
    else
    {
        show(block);
    }

    if (!getArrayLength(arr))
    {
        disable(sel);
        return;
    }

    setupSelect(sel, arr);
    enable(sel);
        
        var elBlockDepartureTime = getElem("block_departure_time");
        
    if (type == 27 || type == 28)
    {
        elBlockDepartureTime.style.visibility = "hidden";
    }
    else if (type == 25 && getArrayLength(arr))
    {
        elBlockDepartureTime.style.visibility = "visible";
    }
}