iBookingAjax = function( onContent, onError )
{
    var ajaxObject = function()
    {
        try{return new XMLHttpRequest();}catch(ex){};
        try{return new ActiveXObject("Microsoft.XMLHTTP");}catch(ex){};
        try{return new SOAPCall();}catch(ex){};
    }
    if( !onError )
    {
        this.onError = function( error )
        {
                alert( error );
        }
    }
    else
        this.onError = onError;

    if( !onContent )
    {
        this.onContent = function( content )
        {
            alert( content );
        };
    }
    else
        this.onContent = onContent;

    this.call = function( url, args, async )
    {
        iBookingThisPtr = this;
        try
        {var ao = ajaxObject();

            ao.onreadystatechange = function()
            {
                if( ao.readyState==4 || ao.readyState=="complete" )
                {
                    iBookingThisPtr.onContent( ao.responseText );
                }
            }
            if( !args ) args = null;
            if( async == null ) async = false;

            ao.open( "GET", url, async );
            ao.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            ao.send( args );
        }
        catch( e )
        {
            alert( e );
            this.onError( e );
        }
    }
}

iBookingRSData = function( progressId, progressTextId, formTableId )
{
    iBookingRSThisPtr = this;

    this.hotels = new Array();
    this.maxAdults = 1;
    this.maxRooms = 1;

    this.onRequest = function ( str )
    {
        // parse string
        var results = str.split("\n");
        var count = parseInt( results[0] );
        for( var i = 0; i < count; ++i )
        {
            var results2 = results[i + 1].split("=");
            var hotel = new Object();
            hotel.name = results2[0];
            hotel.id = parseInt( results2[1] );
            iBookingRSThisPtr.hotels.push( hotel );
        }
        iBookingRSThisPtr.maxRooms = parseInt( results[count + 1] );
        iBookingRSThisPtr.maxAdults = parseInt( results[count + 2] );
        /*
        if( var el = document.getElementById( progressId ) )
        {

        }
        if( var el = document.getElementById( progressTextId ) )
        {
            el.style.displya = "";
        }
        if( var el = document.getElementById( formTableId ) )
        {
            el.style.display = "";
        }
        */
    }

    this.onError = function ( error )
    {
        if( iBookingThisPtr.progressTextElement )
            iBookingThisPtr.progressTextElement.innerHTML = "Error: " + error;
    }
/*
    this.getData = function ( search_group_id )
    {
        this.ajax.call( "http://hotelapp.ibooking.com/remote_search_info.asp", "search_group_id = " + escape(search_group_id) );
    }
*/
    this.buildSelector = function( el, type )
    {
		if( remote_search_hotels )
			iBookingRSThisPtr.hotels = remote_search_hotels;
		if( remote_search_max_rooms )
			iBookingRSThisPtr.maxRooms = remote_search_max_rooms;
		if( remote_search_max_adults )
			iBookingRSThisPtr.maxAdults = remote_search_max_adults;
		if( remote_search_meal_plans )
			iBookingRSThisPtr.mealPlans = remote_search_meal_plans;
		if( remote_search_towns )
			iBookingRSThisPtr.towns     = remote_search_towns;
		if( remote_search_max_occupancy ) 
			remoteSearchData.maxOccupancy = remote_search_max_occupancy;
		if( typeof(defaultAdultsValue) != 'undefined' ) 
			remoteSearchData.defaultAdultsValue = defaultAdultsValue; 
		if( typeof(defaultChildrenValue) != 'undefined' ) 
			remoteSearchData.defaultChildrenValue = defaultChildrenValue;
			
        if( el )
        {
            switch( type )
            {
                case "hotels":
                {
                    var k = 0;
                    for( var i = 0; i < this.hotels.length; ++i )
                        if( this.hotels[i].id && this.hotels[i].id != "null" && this.hotels[i].id != " " )
                        {
                            el.options[k + 1] = new Option( this.hotels[i].id, this.hotels[i].id );
                            ++k;
                        }
                    break;
                }

                case "adults":
                {
					for( var i = 0; i < this.maxAdults; ++i )
					{
                        el.options[i] = new Option( i + 1, i + 1 );
						if (typeof(this.defaultAdultsValue) != "undefined" && this.defaultAdultsValue != null)
						{
							if ( i+1 == parseInt(this.defaultAdultsValue,10))
							el.selectedIndex = i;
						}
					}
                    break;
                }

                case "rooms":
                {
                    for( var i = 0; i < this.maxRooms; ++i )
                        el.options[i] = new Option( i + 1, i + 1 );
                    break;
                }
				
				case "children":				
				{				
					for(var k = 0; k <= this.maxOccupancy ; k++)
					{
						el.options[k] = new Option(k, k);						
					}					
				}
            }
        }
    }

    this.ajax = new iBookingAjax( this.onRequest, this.onError );
}

function ShowRemoteSearchPopup( search_group_id, form1, form2, form3 )
{
    if ((typeof(remote_search_provider_id) != "undefined") && (remote_search_provider_id > 0))
    {
        if ((typeof(remote_search_language) != "undefined") && (remote_search_language != ""))
            ShowHotelPopup(remote_search_provider_id, form1, form2, form3, remote_search_language);    
        else
            ShowHotelPopup(remote_search_provider_id, form1, form2, form3);    
    }
    else
    {
        var url = 'http://hotelapp.ibooking.com/hotel_net/search/Search.aspx?id_search_group=' + escape(search_group_id);
        url += "&town=&classification=&region=&location=" + escape( form1.hotel_location.value );
        url += "&day=" + escape( form2.arrival_day.value ) + "&month=" + escape( form2.arrival_date.value.substr(4, 2) ) + "&year=" + escape( form2.arrival_year.value );
        url += "&nights=" + escape( form3.nights.value ) + "&quantity=" + escape( form3.rooms.value )
			 + "&adults=" + escape( form3.adults.value )
			 + "&childs1=" + escape( form3.children.value );
             + "&t=" + ( new Date() ).getTime() + "&tp_site=Y";

        var newWin = window.open(url,'cal','dependent=yes,width=600,height=610,titlebar=yes,top=0,left=0,scrollbars=yes');
    }
}

function ShowHotelPopup(id_prov, form1, form2, form3, lng)
{
    if (id_prov)
    {
        var id_mpln = (form1 && form1.meal_plan) ? escape(form1.meal_plan.value) : 0;
        var curDate = new Date();
        var day = ( form2 && form2.arrival_day ) ? form2.arrival_day.value : curDate.getDate();
        var month = ( form2 && form2.arrival_date ) ? form2.arrival_date.value.substr(4, 2) : ( curDate.getMonth() + 1 );
        var year = ( form2 && form2.arrival_year ) ? form2.arrival_year.value : ( curDate.getYear() );

        var nights = ( form3 && form3.nights ) ? form3.nights.value : 1;
        var rooms = ( form3 && form3.rooms ) ? form3.rooms.value : 1;
        var adults = ( form3 && form3.adults ) ? form3.adults.value : 1;
		var children = ( form3 && form3.children ) ? form3.children.value : 0;

        var url = 'http://hotelapp.ibooking.com/hotel_net/ProcessExternalAction.aspx?command=start_from_tp_site&id_prov=' + escape(id_prov);

        if (id_mpln)
        {
            url += "&id_mpln=" + id_mpln;
        }

        url += "&start_date=" + escape( year + month + day);
        url += "&nights=" + escape( nights ) + "&quantity=" + escape( rooms ) + "&adults=" + escape( adults ) + "&childs1=" + escape( children );
        url += "&t=" + ( new Date() ).getTime() + "&tp_site=Y";

        if (lng)
        {
           url += "&lng=" + escape(lng);
        }

        var newWin = window.open(url,'search','dependent=yes,width=600,height=610,titlebar=yes,top=0,left=0,scrollbars=yes');
    }
}

//
// type : 0 - remote_search
//        1 - provider
//        2 - provider group
//
function WriteRemoteSearchData( id_search_group, type, show_towns, id_provider_group)
{
	var id_provider = (type == 1 ? id_search_group : 0);

	var result = "";
    	result += "<script type=\"text/javascript\">\
        	var remote_search_hotels = new Array();\
        	var remote_search_max_rooms = 1;\
        	var remote_search_max_adults = 1;\
        	var remote_search_meal_plans = new Array();\
        	var remote_search_towns = new Array();\
			var remote_search_max_occupancy = 1;\
			var remote_search_meal_plans = new Array();\
        	</script>";

	var url = "http://hotelapp.ibooking.com/remote_search_info.asp";

	var show_towns_param = "";
	if (show_towns == 2)
	{
		show_towns_param = "&show_hotels=1";
	}
	else
	{
		show_towns_param = "&show_towns=1";
	}


	switch(type)
	{
		case 1:
		{
			url += "?provider_id=" + id_provider;
		}
		break;

		case 2:
		{
			url += "?provider_group_id=" + id_provider_group;
		}
		break;

		default:
		{
			url += "?search_group_id=" + id_search_group + show_towns_param;
		}
	}

	var constructor_params  = "";
	switch(type)
	{
		case 1:
		{
			constructor_params = "0, " + id_provider;
		}
		break;

		case 2:
		{
			constructor_params = id_search_group + ", 0, " + id_provider_group
		}
		break;

		default:
		{
			constructor_params = id_search_group;
		}
	}

	if (show_towns == 2)
	{
		constructor_params = id_search_group + ", 0, 1, 2";
	}

	result += "<script type=\"text/javascript\" src=\"" + url + "\"></script>";
	result += "<script type=\"text/javascript\">remoteSearchData = new iBookingRSData(" + constructor_params + ");</script>";
	document.write(result);

	result = "<script type=\"text/javascript\">\
		if( remote_search_hotels     ) remoteSearchData.hotels    = remote_search_hotels;\
		if( remote_search_max_rooms  ) remoteSearchData.maxRooms  = remote_search_max_rooms;\
		if( remote_search_meal_plans ) remoteSearchData.mealPlans = remote_search_meal_plans;\
		if( remote_search_max_adults ) remoteSearchData.maxAdults = remote_search_max_adults;\
		if( remote_search_towns      ) remoteSearchData.towns     = remote_search_towns;\
		if( remote_search_max_occupancy ) remoteSearchData.maxOccupancy = remote_search_max_occupancy;\
		if( remote_search_towns      ) remoteSearchData.towns     = remote_search_towns; \
		if( typeof(defaultAdultsValue) != 'undefined' ) remoteSearchData.defaultAdultsValue = defaultAdultsValue; \
		if( typeof(defaultChildrenValue) != 'undefined' ) remoteSearchData.defaultChildrenValue = defaultChildrenValue; \
		</script>";

	document.write( result );
}