
function submit_my_form(myForm, all_params,container){
	show_information("please wait",true);
	$(myForm).request({
		parameters : all_params,
		asynchronous:false, evalScripts:true, 
	onSuccess: function(transport){
				$('information').insert('done.');
		show_information('<h2 class="ok">Termin&eacute;</h2>');
		},
	onUninitialized: function(transport){
			show_information(" Uninitialized");
		},
	onLoading: function(transport){
		show_information("<h1> Loading : "+new_page+"</h1>");
			$(container).update("<div class='please_wait'> &nbsp;</div>");
		},
	onLoaded: function(transport){
			show_information("<hr/> Loading done.");
			new_page='loaded';
		},
	onFailure: function(transport){
		show_information("<h1> Fail.</h1>");
		new_page='ERROR';

	},
	onComplete: function(transport){
		new_page=transport.responseText;
		if(container==''){
			alert('Termin&eacute;');
		}else{
			if(new_page!=''){
				$(container).update(new_page);
				$('information').hide();
			}
			else{
				$('information').insert('done.');
			}
		}
	}
	});
		return false;
}

function show_information(msg, reset){
 	if(reset){
		$("msg").update('');
		$("information").hide();
	}else{
		$("information").show();
		if(msg){
			$("information").insert(' '+msg);
		} 
	}
}
function switch_picture(url_picture, alt_picture, description_picture){
	$('div_big_photo').update( "<div class='please_wait'>PLEASE WAIT</div>");
	/* alert("wait"); */
	var bigPhoto = new Element('img', { 'id': 'BigPhoto','class': 'BigPhoto', 'src': url_picture, 'alt': alt_picture, 'title': description_picture, 'height':"300px" });
	//	bigPhoto.writeAttribute('height', "270");
	$('div_big_photo').update(bigPhoto);
	$('div_big_photo').innerHTML;
}
function search_apartments(oldbutton){
	$('search_window').update("<div class='please_wait'>&nbsp;</div>");
	button=$(oldbutton);
	myForm=get_form_from_button(button); 

	if(!Object.isElement($(myForm))){
		//alert(myForm+" doesn't exist");
		show_information("(fail) : "+myForm+" doesn't exist");
	}
	else{
		show_information("starting search...",true);
		$(myForm).request({
			parameters : {'action' : 'search'
			},
			asynchronous:true, evalScripts:true, 
onSuccess: function(transport){
		show_information('Update successful');
			},
onUninitialized: function(transport){
				//alert("onUninitialized");
				show_information(" (not uninitialized)");
			},
onLoading: function(transport){
				//alert("onLoading");
				show_information(" (Loading)");
			},
onLoaded: function(transport){
				//alert("onLoaded");
				show_information(" (Loaded)");
			},
/* onInteractive: function(transport){
				//alert("onInteractive");
				show_information(transport.responseText+" (on Interactive)");
			}, */
			onFailure: function(transport){
				//alert("onFailure");
				show_information(transport.responseText+" (on failure)");
			},
			onComplete: function(transport){
				show_information("(on complete)");
				new_page=transport.responseText;
			$('search_window').update(new_page);
			}
		});
	}
}

function contact_us(oldbutton){
	button=$(oldbutton);
	myForm=get_form_from_button(button); 
		
	other_params={'action':'send_contact_us'};
	res=submit_my_form(myForm, other_params,'contact_us_window');
	//alert(res);
	return false;
}

function contact_us_owner(oldbutton){
	button=$(oldbutton);
	myForm=get_form_from_button(button); 
		
	other_params={'action':'send_contact_us_owner'};
	res=submit_my_form(myForm, other_params,'contact_us_window');
	//alert(res);
	return false;
}

function sort_page(oldbutton,num_page){
	button=$(oldbutton);
	myForm=get_form_from_button(button); 
		
	other_params={'action':'sort_pages','page' : num_page};
	res=submit_my_form(myForm, other_params,'search_window');
	show_information('',true);
	return res;
}

function get_form_from_button(button){
	one_element=$(button).ancestors('form');
	myForm=false;
	for(i=0;i<20;i++){
			if($(one_element[i]).hasClassName('my_real_form')){
				myForm=one_element[i];
				break;
			}else{
			//	show_information(" doesn't match<br/>")
			}
	}
	return myForm;
}

function change_form_action(oldbutton,newaction){
	button=$(oldbutton);
	myForm=get_form_from_button(button); 
	myForm.writeAttribute('action',newaction);
	return true;
}
function update_area_by_city(oldbutton,id_city){
/*	arr=$w(idkeyword_city);	id_city=arr[0];	keyword_city=arr[1];*/
	/*idkeyword_city.sub(/([0-9]+)(\w+)/, function(match){return {match[1]},1);*/
	/*keyword_city=idkeyword_city.sub(/(\w+)/, function(match){return match[0]},1);*/
	$('div_area').update("<div class='please_wait'> &nbsp;</div>");
	new Ajax.Updater('div_area', '/ajax.php', {
	parameters: { 'action' : 'update_area_by_city', 'id_city': id_city}
	});
	return false;
	
}

function price_simulation(oldbutton){
	$('search_window').update("<div class='please_wait'> &nbsp;</div>");
	button=$(oldbutton);
	myForm=get_form_from_button(button); 
	other_params={'action':'price_simulation'};
	res=submit_my_form(myForm, other_params,'search_window');
	$('information').hide();
	return res;
}

function switch_calendar(id_apart, y, m){
	new Ajax.Updater('espace_calendrier', '/ajax.php', {
  parameters: { 'action' : 'switch_calendar', 'id': id_apart, 'y':y, 'm':m }
});
	return false;
}
