var t_arrival='';
var t_departure='';
var t_bedrooms='';
var t_sleeps='';
var t_price='';
var t_property_name='';
var t_country='';
var t_state='';
var t_town=''
var t_page='';
var t_pid='';
var t_property_type='';
var t_realestate='';
var t_lastminute='';
var req = "";
var disabled = false;
var t_order=6;
var watchlist = 0;

// LOAD THE AJAX COUNTRY, REGION, STATE


function getCountries(){

	country_REQUEST = new Request.HTML({url:'index.php?custom_page=v61_property_listing&no_header=true',

	   data : {
		   'task':"getCountries"
	   },

	   encoding : 'UTF-8',

	   onRequest: function(){

	   		obj = document.form_search_ajax;

	   		obj.rental_country.disabled=true;

	   },

	   onSuccess: function(html,elements,htmlx,javascriptx) {

	   			obj = document.form_search_ajax;

				obj.rental_country.length=0;

				obj.rental_country.options[0] = new Option("...", "", 0, 0);

	    		eval(javascriptx);

	    		obj.rental_country.disabled=false;

	    		enable_all_commands();

	    		getRegions();

		},

		onFailure: function() {

			obj = document.form_search_ajax;

			obj.rental_country.length=0;

			obj.rental_country.options[0] = new Option("...", "", 0, 0);

			obj.rental_country.disabled=false;

			enable_all_commands();

		}

	});

	country_REQUEST.send();

}

function getRegions(){

	region_REQUEST = new Request.HTML({url:'index.php?custom_page=v61_property_listing&no_header=true',

	   data : {
		   'task':"getRegions",
		   'country':$('rental_country').value
	   },

	   encoding : 'UTF-8',

	   onRequest: function(){

	   		obj = document.form_search_ajax;

	   		obj.rental_region.disabled=true;

	   },

	   onSuccess: function(html,elements,htmlx,javascriptx) {

	   			obj = document.form_search_ajax;

				obj.rental_region.length=0;

				obj.rental_region.options[0] = new Option("...", "", 0, 0);

	    		eval(javascriptx);

	    		obj.rental_region.disabled=false;

	    		enable_all_commands();

	    		getTowns();

		},

		onFailure: function() {

			obj = document.form_search_ajax;

			obj.rental_region.length=0;

			obj.rental_region.options[0] = new Option("...", "", 0, 0);

			obj.rental_region.disabled=false;

			enable_all_commands();

		}

	});

	region_REQUEST.send();

}

function getTowns(){

	town_REQUEST = new Request.HTML({url:'index.php?custom_page=v61_property_listing&no_header=true',

	   data : {
		   'task':"getTowns",
		   'country':$('rental_country').value,
		   'region':$('rental_region').value
	   },

	   encoding : 'UTF-8',

	   onRequest: function(){

	   		obj = document.form_search_ajax;

	   		obj.rental_town.disabled=true;

	   },

	   onSuccess: function(html,elements,htmlx,javascriptx) {

	   			obj = document.form_search_ajax;

				obj.rental_town.length=0;

				obj.rental_town.options[0] = new Option("...", "", 0, 0);

	    		eval(javascriptx);

	    		obj.rental_town.disabled=false;

	    		enable_all_commands();

		},

		onFailure: function() {

			obj = document.form_search_ajax;

			obj.rental_town.length=0;

			obj.rental_town.options[0] = new Option("...", "", 0, 0);

			obj.rental_town.disabled=false;

			enable_all_commands();

		}

	});

	town_REQUEST.send();

}

function compare_selected(){

	if (disabled){
		return false;
	}

	if (property_listing_comparelist.length<=1){

		alert(message_please_select);

	} else {

		if (property_listing_comparelist.length>5){

			alert(message_please_select1+5+' '+message_please_select2);

		} else {

				disable_all_commands();

				MySlide.slideOut().chain( function(){ $('content').innerHTML = "<br><table align='center'><tr><td><img src='templates/v61/images/ajax-preloader.gif'></td></tr></table>";

				MySlide.slideIn().chain( function(){

					   req = new Request.HTML({url:'index.php?custom_page=v61_compare_properties&no_header=true',

					   data : {
						   'properties':property_listing_comparelist.join(",")
					   },

					   encoding : 'UTF-8',

					   onSuccess: function(html,elements,htmlx,javascriptx) {

					   			MySlide.slideOut().chain( function(){

									$('content').set('text','');

					    			$('content').innerHTML=htmlx;

					    			$('content').height = $('content').innerHTML.height;

					    			eval(javascriptx);

					    			MySlide.slideIn();

					    			enable_all_commands();

					   			});

						},

						onFailure: function() {

							$('content').set('text', 'There was an error while trying to process the request. Please try your command again');

							enable_all_commands();

						}

					});

					req.send();

				});

			} );

		}

	}

}

function clear_selection(){

	if (disabled){
		return false;
	}

	Cookie.write('property_listing_comparelist', '');

	property_listing_comparelist = new Array();

	search_property(t_arrival,t_departure,t_bedrooms,t_sleeps,t_price,t_property_name,t_country,t_state,t_town,t_property_type,t_pid,t_realestate,t_page,'',t_order,watchlist,t_lastminute);

}

var CookiePComparelist = Cookie.read("property_listing_comparelist");

if (CookiePComparelist){

	if (CookiePComparelist!=''){

		var property_listing_comparelist = CookiePComparelist.split(",");

	} else {

		var property_listing_comparelist = new Array();

	}

}
else {

	var property_listing_comparelist = new Array();

}

function check_compare(pid,checked){

	if (checked==false){

		// remove item from cookies

		var test_array=new Array();

		for (x=0;x<property_listing_comparelist.length;x++){

			if (property_listing_comparelist[x]!=pid){

				test_array[test_array.length]=property_listing_comparelist[x];

			}

		}

		test_array = test_array.unique();

		property_listing_comparelist = test_array;

		if (property_listing_comparelist.length>0){

			Cookie.write('property_listing_comparelist', property_listing_comparelist.join(","));

		} else {

			Cookie.write('property_listing_comparelist', '');

		}

	} else {

		// add items to cookies

		property_listing_comparelist[property_listing_comparelist.length]=pid;

		property_listing_comparelist = property_listing_comparelist.unique();

		if (property_listing_comparelist.length>0){

			Cookie.write('property_listing_comparelist', property_listing_comparelist.join(","));
		}

	}

}

var CookiePWatchlist = Cookie.read("property_listing_watchlist");

if (CookiePWatchlist){
	if (CookiePWatchlist!=''){

		var properties_watchlist = CookiePWatchlist.split(",");

	} else {

		var properties_watchlist = new Array();

	}
}
else {

	var properties_watchlist = new Array();

}

Array.prototype.unique =
  function() {
    var a = [];
    var l = this.length;
    for(var i=0; i<l; i++) {
      for(var j=i+1; j<l; j++) {
        // If this[i] is found later in the array
        if (this[i] === this[j])
          j = ++i;
      }
      a.push(this[i]);
    }
    return a;
  };

function remove_watchlist(pid){

	var test_array=new Array();

	for (x=0;x<properties_watchlist.length;x++){

		if (properties_watchlist[x]!=pid){

			test_array[test_array.length]=properties_watchlist[x];

		}

	}

	test_array = test_array.unique();

	properties_watchlist = test_array;



	if (properties_watchlist.length>0){

		Cookie.write('property_listing_watchlist', properties_watchlist.join(","));

		t_pid = properties_watchlist.join(",");

	} else {

		Cookie.write('property_listing_watchlist', '');

		t_pid = '12312312312';

	}

}

function load_watchlist(){

	if (disabled){
		return false;
	}

	watchlist = 1;

	var pidstosend = '';

	if (properties_watchlist) {
		if (properties_watchlist.length>0){

			pidstosend = properties_watchlist.join(",");

		} else {

			pidstosend = '123123123123';

		}
	} else {

		pidstosend = '123123123123';

	}

	search_property('','','','','','','','','','',pidstosend,3,1,'',t_order,1,'');

}

function add_watchlist(pid){

	properties_watchlist[properties_watchlist.length]=pid;

	properties_watchlist = properties_watchlist.unique();

	if (properties_watchlist.length>0){

		Cookie.write('property_listing_watchlist', properties_watchlist.join(","));

	}

}

function change_order(new_order){

	if (disabled){
		return false;
	}

	t_order=new_order;

	search_property(t_arrival,t_departure,t_bedrooms,t_sleeps,t_price,t_property_name,t_country,t_state,t_town,t_property_type,t_pid,t_realestate,t_page,'',t_order,watchlist,t_lastminute);


}

function set_alpha(object,alpha){

	document.getElementById(object).style.MozOpacity=alpha/100;
	if (document.getElementById(object).filters)
	document.getElementById(object).filter='alpha(opacity='+alpha+')';

}

function doSearch(){

	if (disabled){
		return false;
	}

	var x_arrival = '';
	var x_departure = '';
	var x_bedrooms= '';
	var x_sleeps = '';
	var x_price = '';
	var x_country = '';
	var x_region = '';
	var x_town = '';

	if ($('rental_arrival'))
		x_arrival = $('rental_arrival').value;
	if ($('rental_departure'))
		x_departure = $('rental_departure').value;
	if ($('rental_bedrooms'))
		x_bedrooms= $('rental_bedrooms').value;
	if ($('rental_sleeps'))
		x_sleeps = $('rental_sleeps').value;
	if ($('rental_price'))
		x_price = $('rental_price').value;
	if ($('rental_country'))
		x_country = $('rental_country').value;
	if ($('rental_region'))
		x_region = $('rental_region').value;
	if ($('rental_town'))
		x_town = $('rental_town').value;

	search_property(x_arrival,x_departure,x_bedrooms,x_sleeps,x_price,'',x_country,x_region,x_town,'','',3,1,'',t_order,0,0);

}

function search_location(country,state,town){

	if (disabled){
		return false;
	}

	search_property('','','','','','',country,state,town,'','',3,1,'',t_order,0,'');

}

function load_pid(pid)
{

	if (disabled){
		return false;
	}

	search_property('','','','','','','','','','',pid,3,1,'load_property_page('+pid+',2)',t_order,0,'');

}

function load_hotel_calendar(pid,affected_pid){

	if (disabled){
		return false;
	}

	set_alpha('page_content_'+affected_pid,50);

	req2 = new Request.HTML({url:'index.php?custom_page=v61_property_listing&no_header=true',

	   data : {
		   'task':"get",
		   'pid':pid,
		   'page':6,
		   'from_watchlist':watchlist
	   },

	   encoding : 'UTF-8',

	   onSuccess: function(html,elements,htmlx,javascriptx) {

	   			$('page_content_'+affected_pid).set('text','');

	    		$('page_content_'+affected_pid).innerHTML=htmlx;

	    		eval(javascriptx);

	    		set_alpha('page_content_'+affected_pid,100);

	    		MySlide.slideIn().chain( function(){ MySlide.slideIn().chain( function(){ enable_all_commands(); } )});



		},

		onFailure: function() {

			$('page_content_').set('text', 'There was an error while trying to process the request. Please try your command again');

			set_alpha('page_content_'+affected_pid,100);

			enable_all_commands();

		}

	});

	req2.send();

}

function load_property_page(pid,page){

	if (disabled){
		return false;
	}

	disable_all_commands();

	if ($('tab_'+pid+'_1')){
		$('tab_'+pid+'_1').className='cat-item';
	}
	if ($('tab_'+pid+'_2')){
		$('tab_'+pid+'_2').className='cat-item';
	}
	if ($('tab_'+pid+'_3')){
		$('tab_'+pid+'_3').className='cat-item';
	}
	if ($('tab_'+pid+'_4')){
		$('tab_'+pid+'_4').className='cat-item';
	}
	if ($('tab_'+pid+'_5')){
		$('tab_'+pid+'_5').className='cat-item';
	}
	if ($('tab_'+pid+'_6')){
		$('tab_'+pid+'_6').className='cat-item';
	}
	if ($('tab_'+pid+'_7')){
		$('tab_'+pid+'_7').className='cat-item';
	}

	if ($('tab_'+pid+'_'+page)){
		$('tab_'+pid+'_'+page).className='cat-item current-cat';
	}

	set_alpha('page_content_'+pid,50);

	req2 = new Request.HTML({url:'index.php?custom_page=v61_property_listing&no_header=true',

			   data : {
				   'task':"get",
				   'pid':pid,
				   'page':page,
				   'from_watchlist':watchlist
			   },

			   encoding : 'UTF-8',

			   onSuccess: function(html,elements,htmlx,javascriptx) {

			   			$('page_content_'+pid).set('text','');

			    		$('page_content_'+pid).innerHTML=htmlx;

			    		eval(javascriptx);

			    		set_alpha('page_content_'+pid,100);

			    		MySlide.slideIn().chain( function(){ MySlide.slideIn().chain( function(){ enable_all_commands(); } )});



				},

				onFailure: function() {

					$('page_content_').set('text', 'There was an error while trying to process the request. Please try your command again');

					set_alpha('page_content_'+pid,100);

					enable_all_commands();

				}

			});

			req2.send();

}

function search_property(s_arrival,s_departure,s_bedrooms,s_sleeps,s_price,s_property_name,s_country,s_state,s_town,s_property_type,s_pid,s_realestate,s_page,functionx,s_order,from_watchlist,s_lastminute){

	disable_all_commands();

	// associate to temporary stored values

	t_arrival=s_arrival;
	t_departure=s_departure;
	t_bedrooms=s_bedrooms;
	t_sleeps=s_sleeps;
	t_price=s_price;
	t_country=s_country;
	t_state=s_state;
	t_town=s_town;
	t_property_type=s_property_type;
	t_pid=s_pid;
	t_realestate=s_realestate;
	t_page=s_page;
	t_order=s_order;
	t_lastminute=s_lastminute;

	MySlide.slideOut().chain( function(){ $('content').innerHTML = "<br><table align='center'><tr><td><img src='templates/v61/images/ajax-preloader.gif'></td></tr></table>";

		MySlide.slideIn().chain( function(){

			   req = new Request.HTML({url:'index.php?custom_page=v61_property_listing&no_header=true',

			   data : {
				   'arrival':t_arrival,
				   'departure':t_departure,
				   'bedrooms':t_bedrooms,
				   'sleeps':t_sleeps,
				   'price':t_price,
				   'country':t_country,
				   'state':t_state,
				   'town':t_town,
				   'property_type':t_property_type,
				   'pid':t_pid,
				   'realestate':t_realestate,
				   'page':t_page,
				   'order':t_order,
				   'from_watchlist':from_watchlist,
				   'last_minute':t_lastminute
			   },

			   encoding : 'UTF-8',

			   onSuccess: function(html,elements,htmlx,javascriptx) {

			   			MySlide.slideOut().chain( function(){

							$('content').set('text','');

			    			$('content').innerHTML=htmlx;

			    			$('content').height = $('content').innerHTML.height;

			    			eval(javascriptx);

			    			MySlide.slideIn();

			    			enable_all_commands();

			    			if (functionx!=''){
			    				eval(functionx);
			    			}

			   			});

				},

				onFailure: function() {

					$('content').set('text', 'There was an error while trying to process the request. Please try your command again');

					enable_all_commands();

				}

			});

			req.send();

		});

	} );

}

function disable_all_commands(){
		disabled=true;
}

function enable_all_commands(){
		disabled=false;
}

function load_page(page_number){

	if (disabled){
		return false;
	}

	search_property(t_arrival,t_departure,t_bedrooms,t_sleeps,t_price,t_property_name,t_country,t_state,t_town,t_property_type,t_pid,t_realestate,page_number,'',t_order,watchlist,t_lastminute);



}

function load_home(){

	if (disabled){
		return false;
	}

	watchlist = 0;

	disable_all_commands();

	MySlide.slideOut().chain( function(){ $('content').innerHTML = "<br><table align='center'><tr><td><img src='templates/v61/images/ajax-preloader.gif'></td></tr></table>";

		MySlide.slideIn().chain( function(){

			   req = new Request.HTML({url:'index.php?custom_page=v61_home&s=1&no_header=true',

			   data : { 'none':''},

			   encoding : 'UTF-8',

			   onSuccess: function(html,elements,htmlx,javascriptx) {

			   			MySlide.slideOut().chain( function(){

							$('content').set('text','');

			    			$('content').innerHTML=htmlx;

			    			$('content').height = $('content').innerHTML.height;

			    			MySlide.slideIn();

			    			if (document.getElementById('rental_arrival')){

				var cal1 = new Progresiv.Calendar.setup({

				inputField     :    "rental_arrival",     // id of the input field
				ifFormat       :    "%d/%m/%Y",     // format of the input field
				button         :    "img1",  // trigger button (well, IMG in our case)
				showsTime      :    false

			 ,dateStatusFunc :    function (date) {
				// disable weekend days (Saturdays == 6 and Sundays == 0)
				var now=new Date();

				now.setDate(now.getDate()-1);
				if (date<now){
					return true;
				} else {
					return false;
				}
				}
				});

				var cal2 = new Progresiv.Calendar.setup({

				inputField     :    "rental_departure",     // id of the input field
				ifFormat       :    "%d/%m/%Y",     // format of the input field
				button         :    "img2",  // trigger button (well, IMG in our case)
				showsTime      :    false

			 ,dateStatusFunc :    function (date) {
				// disable weekend days (Saturdays == 6 and Sundays == 0)
				var now=new Date();

				now.setDate(now.getDate()-1);
				if (date<now){
					return true;
				} else {
					return false;
				}
				}
				});

					getCountries();

				}

			    			enable_all_commands();

			   			});

				},

				onFailure: function() {

					$('content').set('text', 'There was an error while trying to process the request. Please try your command again');

					enable_all_commands();

				}

			});

			req.send();

		});

	} );



}


function load_rental(){

	if (disabled){
		return false;
	}

	watchlist = 0;

	search_property('','','','','','','','','','','',0,1,'',t_order,0,'');
}

function load_lastminute(){

	if (disabled){
		return false;
	}

	watchlist = 0;

	search_property('','','','','','','','','','','',0,1,'',t_order,0,1);
}


function load_realestate(){
	if (disabled){
		return false;
	}

	watchlist = 0;

	search_property('','','','','','','','','','','',1,1,'',t_order,0,'');
}
