var JOURNEY_ONEWAY_TYPE = 1;
var JOURNEY_RETURN_TYPE = 2;
var JOURNEY_ROUND_TYPE  = 3;

var bPageInitStage = true;
var xmlhttp = null;
var xmlhttpReady = true;
var iframeReady = false;
var curFunc = "";
var bJourneyTypeRequest = false;
var bTicketPricesRequest = false;
var bXmlhttpSupported = false;

var id_type = 0;
var frm_leaving_ind = 0;

bXmlhttpSupported = isXmlhttpSupported();

function getXmlHttp()
{
    var oXmlHttp = null;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
// JScript gives us Conditional compilation, we can cope with old IE versions.
// and security blocked creation of the objects.
    try
    {
        oXmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e)
    {
        try
        {
            oXmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch (E)
        {
            oXmlHttp = null;
        }
    }
@end @*/
    if (!oXmlHttp && typeof XMLHttpRequest != 'undefined')
    {
        try
        {
            oXmlHttp = new XMLHttpRequest();
        }
        catch (e)
        {
            oXmlHttp = null;
        }
    }

    return oXmlHttp;
}

function isXmlhttpSupported()
{
    if (getXmlHttp() == null) return false;
    return true;
}

function updateOnLeavingFrom(journey_mode)
{
    // update leaving from caption
    var el = document.getElementById("leavingCaption");
    if (el)
    {
        if (journey_mode == 1 || journey_mode == 2)
        {
            el.innerHTML = LNG_LEAVING + ":";
        }

        if (journey_mode == 3)
        {
            el.innerHTML = LNG_TOUR_NAME + ":";
        }
    }
}

function onJourneyTypeChange(type, radio, journey_mode)
{
    if ((bXmlhttpSupported && !xmlhttpReady) || (!bXmlhttpSupported && !iframeReady))
    {
        return;
    }

    if(!bPageInitStage && cur_radio == radio) return;

    bPageInitStage = false;

    updateOnLeavingFrom(journey_mode);

    if (durations[radio] > 0)
    {
        setUpAccommodations(radio);
    }
    else
    {
        clearSelect(document.getElementById("accommodation_type"));
        document.getElementById("accommodationsDiv").style.display = "none";
    }

    var arr = new Object();
    var elDepartureCityTable = document.getElementById("departure_city_table");

    if ((type == 27) || (type == 28))
    {
        elDepartureCityTable.style.display = '';

        clearSelect(document.getElementById("departure_city"));

        arr[0] = new ListItem("1", "Edinburgh (departing at 09:00)");
        arr[1] = new ListItem("2", "Glasgow (departing at 10:15)");

        setupSelect(document.getElementById("departure_city"), arr);
    }
    else if (type == 25)
    {
        elDepartureCityTable.style.display = 'none';

        clearSelect(document.getElementById("departure_city"));
        arr[0] = new ListItem("", "");

        setupSelect(document.getElementById("departure_city"), arr);
    }
    else
    {
        if (types[radio] == 3)
        {
            elDepartureCityTable.style.display = '';
        }
        else
        {
            elDepartureCityTable.style.display = 'none';
            clearSelect(document.getElementById("departure_city"));
        }
    }

    cur_radio = radio;
    cur_type = type;
    cur_mode = types[radio];        // id_type
    curFunc = "getLeavingPoints";

    var id_journey_str = (typeof(id_journey) != 'undefined') ? "&id_journey=" + id_journey : "";

    sendRequest("ajax_library.asp?func=" + curFunc + "&id_type=" + cur_mode + "&id_journey_type=" + cur_type + "&id_afl=" + id_afl + id_journey_str);
}

function onLeavingPointChange(bUsrSelect, lng)
{
    if ((bXmlhttpSupported && !xmlhttpReady) || (!bXmlhttpSupported && !iframeReady))
    {
        return;
    }

    bUsrSelect = bUsrSelect || 0;

    if(bUsrSelect) 
    {
        id_leaving = frm.leaving[frm.leaving.selectedIndex].value;
    }

    if ((id_leaving == "N/A" || id_leaving == "0") && (typeof(frm.leaving[frm.leaving.selectedIndex + 1]) != "undefined"))
    {
        id_leaving = frm.leaving[frm.leaving.selectedIndex + 1].value;  
    }

    id_leaving = id_leaving || 0;

    cur_lng = lng;
    curFunc = "getJourneysDetails";

    // check for additional journey
    var additional_journey_param = "";
    if (frm.id_additional_journey && frm.id_additional_journey.value)
    {
        additional_journey_param = "&id_additional_journey=" + frm.id_additional_journey.value;
    }
    sendRequest("ajax_library.asp?func=" + curFunc + "&lng=" + cur_lng + "&id_type=" + cur_mode + "&id_journey_type=" + cur_type + "&id_leaving=" + id_leaving + (id_afl && Number(id_afl) ? "&id_afl=" + id_afl : "") + additional_journey_param);
}

function getRouteFlights(route_data, id_journey, id_season, date)
{
    id_season = id_season || 0;

    curFunc = "getRouteFlights";
    sendRequest("ajax_library.asp?func=" + curFunc + "&route_data=" + route_data + "&id_journey=" + id_journey + "&id_season=" + id_season + "&date=" + date, false);
}

function getExtrasBooked(departure_date, departure_time, id_extra, id_journey, is_returning)
{
    if ((bXmlhttpSupported && !xmlhttpReady) || (!bXmlhttpSupported && !iframeReady))
    {
        return;
    }

    var edit_cart_item_str = (edit_cart_item && (item_index_for_edit != 0)) ? "&item_index_for_edit="+item_index_for_edit : "";
    curFunc = "getExtrasBooked";

    sendRequest("ajax_library.asp?func=" + curFunc + "&departure_date=" + departure_date + "&departure_time=" + departure_time + "&id_extra=" + id_extra + "&id_journey=" + id_journey + (is_returning ? "&returning=Y" : "") + edit_cart_item_str + getTimestampParam(false), false);
}

function onLeavingPointChange2(leaving_ind, bUsrSelect, lng)
{
//    if (!xmlhttpReady)
    if ((bXmlhttpSupported && !xmlhttpReady) || (!bXmlhttpSupported && !iframeReady))
    {
        return;
    }

    bUsrSelect = bUsrSelect || 0;
    frm_leaving_ind = leaving_ind;

    switch(Number(frm_leaving_ind))
    {
        case 1:
            id_leaving1 = id_leaving1 || 0;
            if(bUsrSelect) id_leaving1 = frm.leaving1[frm.leaving1.selectedIndex].value;
            break;
        default:
            return;
    }

    cur_lng = lng;
    curFunc = "getJourneysDetails";
    sendRequest("ajax_library.asp?func=" + curFunc + "&lng=" + cur_lng + "&id_type=" + cur_mode + "&id_journey_type=" + cur_type + "&id_leaving=" + id_leaving1 + (id_afl && Number(id_afl) ? "&id_afl=" + id_afl : ""));
}

//
//  makes param str (started with '&') from form elements.
//
function getFormParamString(frm, form_id)
{
    var form_el = frm || document.getElementById(form_id);

    if (!form_el)
        return "";

    var els = form_el.elements;

    if (!els)
        return "";

    var param_str = "";

    for (var i = 0; i < els.length; ++i)
    {
        if (!els[i] || els[i].disabled)
            continue;

        var tag = els[i].tagName.toLowerCase();
        switch(tag)
        {
            case "input":
            {
                var type = els[i].type.toLowerCase() || "text";
                switch(type)
                {
                    case "text":
                    case "hidden":
                    {
                        param_str += "&" + escape(els[i].name) + "=" + escape(els[i].value);
                        break;
                    }

                    case "checkbox":
                    case "radio":
                    {
                        if (els[i].checked)
                        {
                           param_str += "&" + escape(els[i].name) + "=" + escape(els[i].value);
                        }
                        break;
                    }
                }
                break;
            }

            case "select":
            {
                if (els[i].selectedIndex != -1)
                   param_str += "&" + escape(els[i].name) + "=" + escape(els[i].options[els[i].selectedIndex].value);
                break;
            }
        }
    }

    return param_str;
}

function addToCart(frm, lng)
{
    if ((bXmlhttpSupported && !xmlhttpReady) || (!bXmlhttpSupported && !iframeReady))
    {
        return;
    }
    var edit_cart_item_str = (edit_cart_item && (item_index_for_edit != 0)) ? "&item_index_for_edit="+item_index_for_edit : "";

    curFunc = "addToCart";
    sendRequest("ajax_cart.asp?func=" + curFunc + getFormParamString(frm) + "&lng=" + lng + edit_cart_item_str + getTimestampParam());
}

function getParamsForEdit(frm)
{
    if ((bXmlhttpSupported && !xmlhttpReady) || (!bXmlhttpSupported && !iframeReady))
    {
        return;
    }
    var edit_cart_item_str = (edit_cart_item && (item_index_for_edit != 0)) ? "&item_index_for_edit="+item_index_for_edit : "";
    var route_parameter_str = (route_parameter != "") ? "&route="+route_parameter : "";

    curFunc = "getCartItem";
    //+ getFormParamString(frm)
    sendRequest("ajax_cart.asp?func=" + curFunc + "&lng=" + cur_lng + edit_cart_item_str + route_parameter_str + getTimestampParam(), false);
    selectCartItemForEdit();
}

function removeFromCart(id_journey, cart_item_index)
{
    if ((bXmlhttpSupported && !xmlhttpReady) || (!bXmlhttpSupported && !iframeReady))
    {
        return;
    }

    curFunc = "removeFromCart";
    sendRequest("ajax_cart.asp?func=" + curFunc + "&journey=" + id_journey + "&item_index=" + cart_item_index + getTimestampParam());
}

function editFromCart(id_journey, cart_item_index)
{
    if (typeof(edit_cart_item) != "undefined")
    {
        edit_cart_item = true;
        item_index_for_edit = cart_item_index;
        getParamsForEdit(frm);
        bPageInitStage = true;
        pageInit();

    }
    else
    {
        main.id_journey.value = id_journey;
        main.item_index.value = item_index;
        submitFormA('edit', frm);
    }
}

function updateDatesFields(formName, fieldName, jsDate)
{
    var frm = document[formName];
    var year  = jsDate.getFullYear();
    var month = jsDate.getMonth() + 1;
    var day   = jsDate.getDate();

    if (fieldName == "returning" || fieldName == "departuring2" || fieldName == "departuring3" || fieldName == "departuring4")
    {
/*        var curDate = new Date();
        curDate.setFullYear(frm["_year_" + fieldName].selectedIndex+earlyYear);
        curDate.setMonth(frm["_month_" + fieldName].selectedIndex);
        curDate.setDate(frm["_day_" + fieldName].selectedIndex+1);

        if (curDate < jsDate)
        {
            frm["_day_" + fieldName].selectedIndex   = day - 1;
            frm["_month_" + fieldName].selectedIndex = month - 1;
            frm["_year_" + fieldName].selectedIndex  = year - earlyYear;
            month = (String(month).length == 2) ? month : "0" + month;
            day = (String(day).length == 2) ? day : "0" + day;
            var date = "" + year + month + day;
            frm[fieldName].value = date;
        }
*/
        var date = "" + year + makeTwoDigit(month) + makeTwoDigit(day);
        frm[fieldName].value = date;
        setVisualDate(document.getElementById(fieldName));
        //updateSelectors(fieldName);
    }
    else
    {
/*
     //   for(var d in frm["_day_" + fieldName].options)
        for(var d = 0; d < frm["_day_" + fieldName].options.length; d++)
        {
            if (parseInt(frm["_day_" + fieldName].options[d].value, 10) == parseInt(day,10))
            {
                frm["_day_" + fieldName].options[d].selected = true;
                break;
            }
        }
//        frm["_day_" + fieldName].value   = day;
        frm["_month_" + fieldName].selectedIndex = month - 1;
        frm["_year_" + fieldName].selectedIndex  = year - earlyYear;
        month = (String(month).length == 2) ? month : "0" + month;
        day = (String(day).length == 2) ? day : "0" + day;
        var date = "" + year + month + day;
        frm[fieldName].value = date;
*/
        var date = "" + year + makeTwoDigit(month) + makeTwoDigit(day);
        frm[fieldName].value = date;
        setVisualDate(document.getElementById(fieldName));
        //updateSelectors(fieldName);
    }
}


function getTicketPrices(frm, extra_prices, update_than_edit_cart)
{
    if ((bXmlhttpSupported && !xmlhttpReady) || (!bXmlhttpSupported && !iframeReady))
    {
        return;
    }
//----------------------------------------------
    if (edit_cart_item && !update_than_edit_cart)
    {
        if (cur_departuring_date != "") updateDatesFields('main', 'departuring', cur_departuring_date);

        if (cur_returning_date != "") updateDatesFields('main', 'returning', cur_returning_date);

        if (cur_departuring_time != "") setSelected(main.departure_time, cur_departuring_time);

        if (cur_returning_time != "") setSelected(main.return_time, cur_returning_time);

        if (cur_departure_city_id != "") setSelected(main.departure_city, cur_departure_city_id);

        if (typeof(ticket_ids) != "undefined" && ticket_ids != "")
        {
            var arr_tickets = ticket_ids.split(",");

            for(var id_t0 in arr_tickets)
            {
                setSelected(document.getElementById("ticket_" + arr_tickets[id_t0]), 0);
            }
        }

        for(var id_tt in cur_tickets)
        {
            setSelected(document.getElementById("ticket_" + id_tt), cur_tickets[id_tt]);
        }

        for(var id_ex in cur_extra_tickets)
        {
            for(var id_extt in cur_extra_tickets[id_ex])
            {
                setSelected(document.getElementById("extra_ticket_"+id_extt+"_"+id_ex), cur_extra_tickets[id_ex][id_extt]);
            }
        }

        var oBtnSelect = document.getElementById("buttonSelect");

        if (oBtnSelect != null)
        {
            oBtnSelect.innerHTML = Lng_SAVE_CHANGES;
            oBtnSelect.href = "javascript:continueBooking('save_changes')";
        }

        if ((typeof(current_accommodation) != "undefined") && (typeof(current_accommodation_rooms) != "undefined"))
        {
            // clearing
            for (var id_accommodation in accommodation_rooms)
            {
                for (var i = 0; i < accommodation_rooms[id_accommodation].length; i++)
                {
                    var room_select = document.getElementById("accommodation_room_" + accommodation_rooms[id_accommodation][i]);
                    setSelected(room_select, 0);

                    var radio_room_type = document.getElementsByName("room_type_"+id_accommodation+"_"+accommodation_rooms[id_accommodation][i]);

                    if (radio_room_type != null)
                    {
                            radio_room_type[0].checked = true;
                    }
                }
            }

            // filling
            var rooms_selects = document.getElementById("accommodation_type");

            setSelected(rooms_selects, current_accommodation);

            onAccommodationTypeChange(rooms_selects);

            for (var i_room in current_accommodation_rooms)
            {
                    var room_select = document.getElementById("accommodation_room_" + i_room);
                    if (room_select != null)
                            setSelected(room_select, current_accommodation_rooms[i_room].count);

                    var radio_room_type = document.getElementsByName("room_type_"+current_accommodation+"_"+i_room);
                    if (radio_room_type != null)
                    {
                            for(i_type in radio_room_type)
                            {
                                    if (radio_room_type[i_type].value == current_accommodation_rooms[i_room].type)
                                            radio_room_type[i_type].checked = true;
                            }
                    }
            }
        }
    }
//----------------------------------------------
    var id_journey_type = 0;

    for (var i = 0; i < frm.jtype.length; ++i)
    {
        if (frm.jtype[i].checked)
        {
            id_journey_type = frm.jtype[i].value;
        }
    }

    var additional_journey_param = "";

    if (frm.id_additional_journey && frm.id_additional_journey.value)
    {
        additional_journey_param = "&id_additional_journey=" + frm.id_additional_journey.value;
    }

    var curr_parameter = "";

    // global variable "curr" coming by ajax
    if (curr != null && curr != "")
    {
       curr_parameter = curr;
    }
    else
    {
       if (frm.currency.type == "hidden" || !frm.currency.selectedIndex || frm.currency.selectedIndex < 0 || !frm.currency.options[frm.currency.selectedIndex].value)
       {
           curr_parameter = frm.currency.value;
       }
       else
       {
           curr_parameter = frm.currency.options[frm.currency.selectedIndex].value;
       }
    }
////
    var ticketsRequestedParameter = new Array();

    if (ticket_ids)
    {
        var arrTickets = ticket_ids.split(",");

        for(var counter = 0; counter < arrTickets.length; counter++)
        {
            var ticketSelect = document.getElementById("ticket_" + arrTickets[counter]);

            ticketsRequestedParameter.push(arrTickets[counter] + "_" + ticketSelect[ticketSelect.selectedIndex].value);
        }
    }
////
    frm.currency.value = curr_parameter;
    curFunc = "getPrices";
    sendRequest("ajax_library.asp?func=" + curFunc + "&id_type=" + cur_mode + "&id_journey_type=" + id_journey_type + "&id_journey=" + frm.journey.value + "&id_leaving=" + frm.leaving.value + "&route=" + frm.route.value + "&departure_date=" + frm.departuring.value + "&currency=" + curr_parameter + (extra_prices ? "&extra_prices=1" : "") + additional_journey_param + (ticketsRequestedParameter.length > 0 ? ("&tickets=" + ticketsRequestedParameter.join("|")) : ""));

    showHideViewCartLink();
}

function changeCurrency(frm)
{
    if ((bXmlhttpSupported && !xmlhttpReady) || (!bXmlhttpSupported && !iframeReady))
    {
        return;
    }

    if (frm.currency.type == "hidden" || !frm.currency.selectedIndex || frm.currency.selectedIndex < 0 || !frm.currency.options[frm.currency.selectedIndex].value)
    {
        curr = frm.currency.value;
    }
    else
    {
        curr = frm.currency.options[frm.currency.selectedIndex].value;
    }

    getTicketPrices(frm, true);
}

function getProviderCurrentTime()
{
    curFunc = "getProviderCurrentTime";
    sendRequest("ajax_library.asp?func=" + curFunc, false);
}

function sendRequest(url, async)
{
//    if (!xmlhttpReady)
    if ((bXmlhttpSupported && !xmlhttpReady) || (!bXmlhttpSupported && !iframeReady))
    {
        return;
    }

    if (async == null)
    {
        async = true;
    }

    try
    {
        if (bXmlhttpSupported)
        {
            xmlhttp = getXmlHttp();

            xmlhttp.open("GET", url, async);

            try
            {
                if (async)
                {
                    xmlhttp.onreadystatechange = responseHandler;
                }
                else
                {
                    xmlhttpReady = false;
                }

                xmlhttp.send(null);

                if (!async)
                {
                    xmlhttpReady = true;
                    eval(xmlhttp.responseText);
                }

/*
                if (xmlhttpReady == true)
                {
                    xmlhttp.send(null);
                }

                if (!async)
                {
                   var responseData = xmlhttp.responseText;

                   eval(responseData);
                }
*/
            }
            catch(ex)
            {
            }
        }
        else
        {
            if (iframeReady == true)
            {
                setIFrameReadyState(false);
                window.ifr.location.href = url;
            }
        }
    }
    catch (e)
    {
    }
}

function responseHandler()
{
    if(xmlhttp.readyState == 4)
    {
        if (xmlhttp.status != 200)
        {
            xmlhttpReady = true;
            return;
        }

        if (xmlhttp.responseText == "")
        {
            xmlhttpReady = true;
            return;
        }

        var responseData = xmlhttp.responseText;

        eval(responseData);

        switch(curFunc)
        {
            case "getLeavingPoints":
            {
                procLeavingPointsResponseData();

                curFunc = "";
                xmlhttpReady = true;
                bJourneyTypeRequest = true;

                onLeavingPointChange(0, cur_lng);

                break;
            }
            case "getJourneysDetails":
            {
                xmlhttpReady = true;
                procJourneyDetailsResponseData();
                curFunc = "";
                bTicketPricesRequest = true;
                getTicketPrices(frm);
                break;
            }

            case "addToCart":
            {
                procAddToCartResponseData();
                break;
            }

            case "removeFromCart":
            {
                procRemoveFromCartResponseData();
                break;
            }

            case "getPrices":
            {
                procGetTicketPricesResponseData();
                bTicketPricesRequest = false;
                break;
            }

            case "updateCart":
            {
                procAddToCartResponseData();
                curFunc = "";
                xmlhttpReady = true;
                bTicketPricesRequest = true;
                getTicketPrices(frm, true);
                break;
            }

        }

        if(!bJourneyTypeRequest && !bTicketPricesRequest)
        {
            curFunc = "";
            xmlhttpReady = true;
        }
    }
    else
    {
        if (xmlhttp.readyState >= 0 && xmlhttp.readyState <= 3)
        {
            if (curFunc != "addToCart" && curFunc != "removeFromCart" && curFunc != "getPrices" && curFunc != "updateCart" && curFunc != "getExtrasBooked")
            showWaitMessage();
        }

        xmlhttpReady = false;
    }
}

function procLeavingPointsResponseData()
{
/*
    var arr  = new Array();
    var vals = String(data[cur_type].start_points).split(',');
    if(vals.length == 0) return;
    var enabled = new Object();
    var tmp_arr = String(data[cur_type].enabled_points).split(',');

    for(var i in tmp_arr)
    {
        enabled[tmp_arr[i]] = true;
    }

    if (!edit_cart_item)
    {
        var b = false;
        for(var i in vals)
        {
            if (inArr(hiddenPoints, vals[i]))
            {
                continue;
            }

            if(typeof(enabled[vals[i]]) != 'undefined')
            {
                arr[vals[i]] = vals[i];
                if(!b)
                {
                    id_leaving = arr[vals[i]];
                    b = true;
                }
            }
        }

        if(st_point)
        {
            for(var i in arr)
            {
                if(arr[i] == st_point)
                {
                    id_leaving = arr[i];
                    break;
                }
            }
        }
    }
*/

    clearSelect(frm.leaving);

    var arr  = new Object();
    var vals = String(data[cur_type].start_points).split(',');

    if(vals.length == 0)
    {
        return;
    }

    var enabled = new Object();
    var tmp_arr = String(data[cur_type].enabled_points).split(',');

    for(var i in tmp_arr)
    {
        enabled[tmp_arr[i]] = true;
    }

    var count = 0;
    var flag = false;
    var b = 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;

                if(!b)
                {
                    id_leaving = vals[i];
                    b = 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++;
    }

    if(st_point)
    {
        for(var i in arr)
        {
            if (arr[i].id == st_point)
            {
                id_leaving = arr[i].id;
                break;
            }
        }
    }

    setupSelect(frm.leaving, arr, (show_NA_by_deafult && getArrayLength(arr) > 1));
    enable(frm.leaving);

    makeHighlighted(frm.leaving);
/*
    if(st_point)
    {
        setSelected(frm.leaving, st_point);
        st_point = 0;
    }
*/
}

function procJourneyDetailsResponseData()
{
    if(bJourneyTypeRequest)
    {
        updatePage(cur_type, cur_radio);
        bJourneyTypeRequest = false;
    }
    else
    {
        setupRouteOptions();
/*
        switch(Number(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 showHideCurrency(show)
{
    var selected_currency = document.getElementById("selected_currency");
    if (!selected_currency)
    {
        return;
    }

    selected_currency.innerHTML = "";
    if (!frm || !frm.currency)
    {
        return;
    }

    frm.currency.style.visibility = show ? "visible" : "hidden";
    if (!show && curr)
    {
        for (var i = 0; i < frm.currency.options.length; ++i)
        {
            if (frm.currency.options[i].value == curr)
            {
                frm.currency.selectedIndex = i;
                selected_currency.appendChild(document.createTextNode(frm.currency.options[i].text));
            }
        }
    }
}

function procAddToCartResponseData()
{
    var cart_el = document.getElementById("cart_div");
    if (!cart_el) return;
    if (cart_html != null && cart_el)
    {
       cart_el.innerHTML = cart_html;
       var divider_el = document.getElementById("TicketsCartDivider");
       if (divider_el)
       {
           divider_el.style.display = (cart_html ? "" : "none");
       }

       var button_el = document.getElementById("continue_button");
       if (button_el)
       {
           button_el.className = (cart_html ? "button" : "ButtonDisabled");
       }
       showHideCurrency(!cart_html);

       showHideViewCartLink();
    }
}

function showHideViewCartLink()
{
    var cart_show_object = document.getElementById("TicketsCartDivider");
    var cart_show = "none";

    if (cart_show_object != null)
    {
        cart_show = cart_show_object.style.display;
    }

    var obj = document.getElementById("linkToCart");

    if (obj != null)
    {
        obj.style.display = cart_show;
    }
}

function procRemoveFromCartResponseData()
{
    procAddToCartResponseData();
}

function procGetTicketPricesResponseData()
{
    if (ticket_prices)
    {
        updateTicketPrices(frm, ticket_prices);
    }

    if (extra_prices)
    {
        updateExtraPrices(extra_prices);
    }
}

function showWaitMessage()
{
    var arr = new Array();
    arr[0] = new ListItem("0", msg_loading);

    switch(cur_mode)
    {
        case 1:
        {
//            hide(getElem('table_2'));
            show(getElem('table_1'));
            hide(getElem('f_returning'));
            hide(getElem('f_description'));
            show(getElem('f_going'));

            if (curFunc == "getLeavingPoints")
            {
                clearSelect(frm.leaving);
                setupSelect(frm.leaving, arr);
            }
            clearSelect(frm.going);
            setupSelect(frm.going, arr);
            break;
        }
        case 2:
        {
//            hide(getElem('table_2'));
            show(getElem('table_1'));
            hide(getElem('f_description'));
            show(getElem('f_going'));
            show(getElem('f_returning'));

            if (curFunc == "getLeavingPoints")
            {
                clearSelect(frm.leaving);
                setupSelect(frm.leaving, arr);
            }
            clearSelect(frm.going);
            setupSelect(frm.going, arr);
            break;
        }
        case 3:
        {
//            hide(getElem('table_2'));
            show(getElem('table_1'));
            hide(getElem('f_description'));
            hide(getElem('f_going'));
            hide(getElem('f_returning'));

            if (curFunc == "getLeavingPoints")
            {
                clearSelect(frm.leaving);
                setupSelect(frm.leaving, arr);
            }
            break;
        }
    }
}

function callInProgress(xmlhttp)
{
    switch ( xmlhttp.readyState )
    {
        case 1, 2, 3:
            return true;
        break;

        // Case 4 and 0
        default:
            return false;
        break;
    }
}

function updateXmlHttpStatus()
{
    xmlhttpReady = !callInProgress(xmlhttp);
    //return callInProgress(xmlhttp);
}

function setIFrameReadyState(state)
{
    if (bXmlhttpSupported) return;
    iframeReady = state;
    if (iframeReady)
    {
        if (bPageInitStage)
        {
            onJourneyTypeChange(cur_type, cur_mode);
//            bPageInitStage = false;
        }
        else
        {
            var responseData = window.ifr.document.body.innerHTML;
            responseData = responseData.replace(/\n/g,"");

            if (responseData == "")
            {
                return;
            }

            eval(responseData);

            switch(curFunc)
            {
                case "getLeavingPoints":
                {
                    procLeavingPointsResponseData();
                    curFunc = "";
                    bJourneyTypeRequest = true;

                    onLeavingPointChange(0, cur_lng);
                    break;
                }

                case "getJourneysDetails":
                {
                    procJourneyDetailsResponseData();
                    break;
                }

                case "addToCart":
                {
                    procAddToCartResponseData();
                    break;
                }

                case "removeFromCart":
                {
                    procRemoveFromCartResponseData();
                    break;
                }

                case "getPrices":
                {
                    procGetTicketPricesResponseData();
                    break;
                }

                case "updateCurrency":
                {
                    procUpdatePricesResponseDate();
                    break;
                }
            }

            if (!bJourneyTypeRequest)
            {
                curFunc = "";
//                iframeReady = true;
            }
        }
    }
    else
    {
        if (curFunc != "addToCart" && curFunc != "removeFromCart" && curFunc != "getPrices" && curFunc != "updateCurrency" && curFunc != "getExtrasBooked")
           showWaitMessage();
    }
}

function getTimestampParam(is_first_param)
{
    var date = new Date();
    return (is_first_param ? "?" : "&") + "time_stamp=" + date.getTime();
}