PageFront = Class.create(Page, {

	onload: function()
	{
		this.execute(this.action);
	},

	initAjaxContent: function()
	{
		AjaxRequest.updateLink('content');
		AjaxRequest.updateForm('content');

		$$('input[name="goback"]').each(function(btnBack)
		{
			btnBack.writeAttribute('name', '');

			btnBack.observe('click', function(event)
			{
				new Insertion.Bottom(btnBack.up('form'), '<input type="hidden" name="goback" value="1" />');
			});

		});

		$$('.removeInputName').each(function(input)
		{
			inputName = input.readAttribute('name');
			input.writeAttribute('name', '');

			input.observe('click', function(event)
			{
				new Insertion.Bottom(input.up('form'), '<input type="hidden" name="'+inputName+'" value="'+input.getValue()+'" />');
			});
		});

		Cufon.refresh(".cufon");
	},

	loadAjaxContent: function()
	{
		Event.observe(document, 'dom:loaded', function(event)
		{
			this.initAjaxContent();

		}.bind(this));
	},

	execute: function(action)
	{
		this.preExecute();
		switch(action)
		{
			case 'income_property':
				this.executeIncomeProperty();
			break;

			case 'subscription':
				this.executeSubscription();
			break;


			case 'subscription_edit':
			case 'subscription_add':
			case 'group_subscription_add':
				this.executeSubscriptionEdit();
			break;

			/*new*/
			
			/*end*/


			case 'sign_in':
				this.executeSignIn();
			break;

			case 'sell_property':
				this.executeSellProperty();
			break;

			case 'your_profile_info_index':
				this.executeYourProfileInfoIndex();
			break;

			case 'property_edit_step_1':
				this.executePropertyEditStep1();
			break;

			case 'property_edit_step_2':
			case 'your_profile_ads_index':
				this.executePropertyEditStep2();
			break;

			case 'property_edit_step_3':
			case 'your_profile_ads_incoming':
				this.executePropertyEditStep3();
			break;

			case 'property_edit_step_4':
			case 'your_profile_ads_spending':
				this.executePropertyEditStep4();
			break;

			case 'property_edit_step_5':
			case 'your_profile_ads_other':
				this.executePropertyEditStep5();
			break;

			case 'property_edit_step_6':
				this.executePropertyEditStep6();
			break;

			case 'property_edit_step_7':
				this.executePropertyEditStep7();
			break;

			case 'admin_user_manager_buyer':
				this.executeAdminUserManagerBuyer();
			break;

			case 'admin_user_manager_seller':
				this.executeAdminUserManagerSeller();
			break;

			case 'your_profile_folder_index':
				this.executeYourProfileFolderIndex();
			break;

			case 'your_profile_ads_statistic':
				this.executeYourProfileAdsStatistic();
			break;

			default:
			break;
		}
	},

	preExecute: function()
	{
		if($('btn_login_live'))
		{
			$('btn_login_live').observe('click', function(loginEvent)
			{
				$('btn_login_live').writeAttribute('href', 'javascript:void(0);');
	
				$('box_login_live').removeClassName('hide');
				$('box_login_live').show();
	
			}.bind(this));
	
			$('btn_login_live_close').observe('click', function(loginCloseEvent)
			{
				$('box_login_live').hide();
	
			}.bind(this));
		}
		this.firstTryInput = new Array();
		$$('input.initOnFocus').each(function(inputElem)
		{
			inputElem.observe('focus', function(event)
			{
				if(this.firstTryInput.indexOf(inputElem.identify()) == -1)
				{
					inputElem.setValue('');
					this.firstTryInput.push(inputElem.identify());
				}
			}.bind(this));

		}.bind(this));


		$$('.tooltip_notice').each(function(el)
		{
			if(!$('tooltip_'+el.identify()))
			{
				var span = Builder.node('span',{id:'tooltip_'+el.identify(),className:'tooltipBtn'},'?');
				new Insertion.After(el, span);

				new Effect.Tooltip(span, '<div style="font-weight:bold;">'+el.readAttribute('title') + '</div><div style="margin-top:3px;">' + el.innerHTML + '</div>', {className: 'tooltip', offset:{x:4, y:4}});
			}
		});
	},

	executeIncomeProperty: function()
	{
		if($('btn_mortgage_calculator'))
		{
			$('btn_mortgage_calculator').observe('click', function(ev)
			{
				new AjaxRequest(this.options.urlMortgageCalculator, {
					parameters:'sale_price='+$F('sale_price')
				});

				//jsWin.instance().setOptions({width:600, height:400, modal:true});
				//jsWin.instance().setContentFromUrl(this.options.urlMortgageCalculator);

				//jsWin.instance().element.setStyle({padding:'0px 10px'});

				//console.debug(jsWin.instance().element.select('form'));

				//console.debug($('mortgage_calculator_form'));

				//jsWin.instance().element.select('form').first().writeAttribute('onsubmit', 'return false;');

			}.bind(this));
		}
	},

	executeSignIn: function()
	{
		if($('btn_login_password_forgot'))
		{
			$('btn_login_password_forgot').writeAttribute('href', 'javascript:void(0);');
			$('btn_login_password_forgot').observe('click', function(event)
			{
				$('login_master_forgot').setValue('1');
				$('tr_login_password').hide();
				$('btn_login_password_forgot_wrap').hide();
	
				$('btn_login_submit_wrap').hide();
	
				$('btn_login_password_forgot_cancel_wrap').removeClassName('hide');
				$('btn_login_password_forgot_cancel_wrap').show();
	
				$('btn_login_submit_forgot_wrap').removeClassName('hide');
				$('btn_login_submit_forgot_wrap').show();
	
				$('tr_login_forgot_message').removeClassName('hide');
				$('tr_login_forgot_message').show();
			});
		}

		if($('btn_login_password_forgot_cancel'))
		{
			$('btn_login_password_forgot_cancel').writeAttribute('href', 'javascript:void(0);');
			$('btn_login_password_forgot_cancel').observe('click', function(event)
			{
				$('login_master_forgot').setValue('');
	
				$('btn_login_password_forgot_cancel_wrap').hide();
	
				$('btn_login_submit_forgot_wrap').hide();
	
				$('tr_login_forgot_message').hide();
	
				$('tr_login_password').removeClassName('hide');
				$('tr_login_password').show();
	
				$('btn_login_password_forgot_wrap').removeClassName('hide');
				$('btn_login_password_forgot_wrap').show();
	
				$('btn_login_submit_wrap').removeClassName('hide');
				$('btn_login_submit_wrap').show();
			});
		}
	},


	executeSubscription: function()
	{
	},



	initTransactionForm: function()
	{
		if($('choix-paiment'))
		{
			$('choix-paiment').select('a').each(function(btnElem)
			{
				btnElem.observe('click', function(event)
				{
					$('choix-paiment').select('a').each(function(btnElem2)
					{
						btnElem2.removeClassName('current');
					});
	
					this.choosePaymentMethod(btnElem);
	
				}.bind(this));
	
			}.bind(this));
		}

		this.choosePaymentMethod = function(btnPayment)
		{
			$('methode-paiment').select('.tab').each(function(tabElem)
			{
				tabElem.hide();
				if(tabElem.hasClassName(btnPayment.readAttribute('rel')))
				{
					tabElem.removeClassName('hide');
					tabElem.show();

					btnPayment.addClassName('current');

					if(tabElem.hasClassName('methode-credit'))
					{
						$('transaction_master_payment_method').setValue('credit');
					}
					else
					{
						$('transaction_master_payment_method').setValue('check');
					}
				}
			});
		}
	},

	executeSubscriptionEdit: function()
	{
		this.initTransactionForm();

		if($(this.options.input_country_htmlid))
		{
			$(this.options.input_country_htmlid).observe('change', function(event)
			{
				new AjaxRequest(this.options.ajaxUrlChangeCountry, {
					parameters:'country='+$F(this.options.input_country_htmlid)
				});
	
			}.bind(this));
		}

		if($(this.options.input_state_htmlid))
		{
			$(this.options.input_state_htmlid).observe('change', function(event)
			{
				new AjaxRequest(this.options.ajaxUrlChangeState, {
					parameters:'country='+$F(this.options.input_country_htmlid)+'&state_iso='+$F(this.options.input_state_htmlid)
				});
	
			}.bind(this));
		}

		if($(this.options.input_state_htmlid))
		{
			$(this.options.input_state_htmlid).observe('click', function(event)
			{
				if($F(this.options.input_country_htmlid).blank())
				{
					jsWin.instance().setTitle(this.options.error_select_state.title);
					jsWin.instance().setContent('<div class="error_alert">'+this.options.error_select_state.content+'</div>');
					jsWin.instance().addOptions({modal:true});
					jsWin.instance().show();
					jsWin.instance().center();
	
					$(this.options.input_state_htmlid).blur();
				}
	
			}.bind(this));
		}


		if($(this.options.input_city_htmlid))
		{
			$(this.options.input_city_htmlid).observe('click', function(event)
			{
				if($F(this.options.input_state_htmlid).blank())
				{
					jsWin.instance().setTitle(this.options.error_select_city.title);
					jsWin.instance().setContent('<div class="error_alert">'+this.options.error_select_city.content+'</div>');
					jsWin.instance().addOptions({modal:true});
					jsWin.instance().show();
					jsWin.instance().center();
	
					$(this.options.input_city_htmlid).blur();
				}
	
			}.bind(this));
		}

		if($(this.options.is_agent_box_htmlid))
		{
			$(this.options.is_agent_box_htmlid).select('input[type="radio"]').each(function(inputRadio)
			{
				inputRadio.observe('click', function(event)
				{
					if(inputRadio.getValue() == '1')
					{
						$(this.options.office_name_box_htmlid).removeClassName('hide');
						$(this.options.office_name_box_htmlid).show();
					}
					else
					{
						$(this.options.office_name_box_htmlid).hide();
					}
	
				}.bind(this));
	
			}.bind(this));
		}


		if($(this.options.form_btn_edit_type))
		{
			$(this.options.form_btn_edit_type).writeAttribute('href', 'javascript:void(0);');
			$(this.options.form_btn_edit_type).observe('click', function(event)
			{
				new Insertion.Bottom(this.options.form_html_id, '<input type="hidden" name="ajax_action" value="store_form" />');
	
				AjaxRequest.submitForm(this.options.form_html_id);
			}.bind(this));
		}
	},

	executeYourProfileInfoIndex : function()
	{
		$(this.options.input_country_htmlid).observe('change', function(event)
		{
			new AjaxRequest(this.options.ajaxUrlChangeCountry, {
				parameters:'country='+$F(this.options.input_country_htmlid)
			});

		}.bind(this));

		$(this.options.input_state_htmlid).observe('change', function(event)
		{
			new AjaxRequest(this.options.ajaxUrlChangeState, {
				parameters:'country='+$F(this.options.input_country_htmlid)+'&state_iso='+$F(this.options.input_state_htmlid)
			});

		}.bind(this));

		$(this.options.input_state_htmlid).observe('click', function(event)
		{
			if($F(this.options.input_country_htmlid).blank())
			{
				jsWin.instance().setTitle(this.options.error_select_state.title);
				jsWin.instance().setContent('<div class="error_alert">'+this.options.error_select_state.content+'</div>');
				jsWin.instance().addOptions({modal:true});
				jsWin.instance().show();
				jsWin.instance().center();

				$(this.options.input_state_htmlid).blur();
			}

		}.bind(this));


		$(this.options.input_city_htmlid).observe('click', function(event)
		{
			if($F(this.options.input_state_htmlid).blank())
			{
				jsWin.instance().setTitle(this.options.error_select_city.title);
				jsWin.instance().setContent('<div class="error_alert">'+this.options.error_select_city.content+'</div>');
				jsWin.instance().addOptions({modal:true});
				jsWin.instance().show();
				jsWin.instance().center();

				$(this.options.input_city_htmlid).blur();
			}

		}.bind(this));



		if($(this.options.input_country_htmlid_contact))
		{
			$(this.options.input_country_htmlid_contact).observe('change', function(event)
			{
				new AjaxRequest(this.options.ajaxUrlChangeCountryContact, {
					parameters:'country='+$F(this.options.input_country_htmlid_contact)
				});
	
			}.bind(this));
		}

		if($(this.options.input_state_htmlid_contact))
		{
			$(this.options.input_state_htmlid_contact).observe('change', function(event)
			{
				new AjaxRequest(this.options.ajaxUrlChangeStateContact, {
					parameters:'country='+$F(this.options.input_country_htmlid_contact)+'&state_iso='+$F(this.options.input_state_htmlid_contact)
				});
	
			}.bind(this));


			$(this.options.input_state_htmlid_contact).observe('click', function(event)
			{
				if($F(this.options.input_country_htmlid_contact).blank())
				{
					jsWin.instance().setTitle(this.options.error_select_state.title);
					jsWin.instance().setContent('<div class="error_alert">'+this.options.error_select_state.content+'</div>');
					jsWin.instance().addOptions({modal:true});
					jsWin.instance().show();
					jsWin.instance().center();
	
					$(this.options.input_state_htmlid_contact).blur();
				}
	
			}.bind(this));
	
	
			$(this.options.input_city_htmlid_contact).observe('click', function(event)
			{
				if($F(this.options.input_state_htmlid_contact).blank())
				{
					jsWin.instance().setTitle(this.options.error_select_city.title);
					jsWin.instance().setContent('<div class="error_alert">'+this.options.error_select_city.content+'</div>');
					jsWin.instance().addOptions({modal:true});
					jsWin.instance().show();
					jsWin.instance().center();
	
					$(this.options.input_city_htmlid_contact).blur();
				}
	
			}.bind(this));
		}




		if($(this.options.input_country_htmlid_office))
		{
			$(this.options.input_country_htmlid_office).observe('change', function(event)
			{
				new AjaxRequest(this.options.ajaxUrlChangeCountryOffice, {
					parameters:'country='+$F(this.options.input_country_htmlid_office)
				});
	
			}.bind(this));
		}

		if($(this.options.input_state_htmlid_office))
		{
			$(this.options.input_state_htmlid_office).observe('change', function(event)
			{
				new AjaxRequest(this.options.ajaxUrlChangeStateOffice, {
					parameters:'country='+$F(this.options.input_country_htmlid_office)+'&state_iso='+$F(this.options.input_state_htmlid_office)
				});
	
			}.bind(this));


			$(this.options.input_state_htmlid_office).observe('click', function(event)
			{
				if($F(this.options.input_country_htmlid_office).blank())
				{
					jsWin.instance().setTitle(this.options.error_select_state.title);
					jsWin.instance().setContent('<div class="error_alert">'+this.options.error_select_state.content+'</div>');
					jsWin.instance().addOptions({modal:true});
					jsWin.instance().show();
					jsWin.instance().center();
	
					$(this.options.input_state_htmlid_office).blur();
				}
	
			}.bind(this));
	
	
			$(this.options.input_city_htmlid_office).observe('click', function(event)
			{
				if($F(this.options.input_state_htmlid_office).blank())
				{
					jsWin.instance().setTitle(this.options.error_select_city.title);
					jsWin.instance().setContent('<div class="error_alert">'+this.options.error_select_city.content+'</div>');
					jsWin.instance().addOptions({modal:true});
					jsWin.instance().show();
					jsWin.instance().center();
	
					$(this.options.input_city_htmlid_office).blur();
				}
	
			}.bind(this));
		}

	},


	executeSellProperty: function()
	{
		$$('.'+this.options.btnDeletePlexClass).each(function(elem)
		{
			elem.writeAttribute('href', 'javascript:void(0);');

			elem.observe('click', function(event)
			{
				if(confirm(this.options.messageDeleteConfirm))
				{
					plexId = /[0-9]+$/.exec(elem.identify()).first();

					new AjaxRequest(this.options.ajaxUrlDeletePlex, {
						parameters:'plex_id='+plexId
					});
				}

			}.bind(this));

		}.bind(this));
	},

	executePropertyEditStep1: function()
	{
		this.initTransactionForm();

		if($(this.options.form_btn_edit_type))
		{
			$(this.options.form_btn_edit_type).writeAttribute('href', 'javascript:void(0);');
			$(this.options.form_btn_edit_type).observe('click', function(event)
			{
				new Insertion.Bottom(this.options.form_html_id, '<input type="hidden" name="ajax_action" value="store_form" />');
	
				AjaxRequest.submitForm(this.options.form_html_id);
			}.bind(this));
		}
	},


	executePropertyEditStep2: function()
	{
		if($(this.options.input_country_htmlid))
		{
			$(this.options.input_country_htmlid).observe('change', function(event)
			{
				new AjaxRequest(this.options.ajaxUrlChangeCountry, {
					parameters:'country='+$F(this.options.input_country_htmlid)
				});
	
			}.bind(this));
		}

		if($(this.options.input_state_htmlid))
		{
			$(this.options.input_state_htmlid).observe('change', function(event)
			{
				new AjaxRequest(this.options.ajaxUrlChangeState, {
					parameters:'country='+$F(this.options.input_country_htmlid)+'&state_iso='+$F(this.options.input_state_htmlid)
				});
	
			}.bind(this));
		}

		if($(this.options.input_state_htmlid))
		{
			$(this.options.input_state_htmlid).observe('click', function(event)
			{
				if($F(this.options.input_country_htmlid).blank())
				{
					jsWin.instance().setTitle(this.options.error_select_state.title);
					jsWin.instance().setContent('<div class="error_alert">'+this.options.error_select_state.content+'</div>');
					jsWin.instance().addOptions({modal:true});
					jsWin.instance().show();
					jsWin.instance().center();
	
					$(this.options.input_state_htmlid).blur();
				}
	
			}.bind(this));
		}

		if($(this.options.input_city_htmlid))
		{
			$(this.options.input_city_htmlid).observe('click', function(event)
			{
				if($F(this.options.input_state_htmlid).blank())
				{
					jsWin.instance().setTitle(this.options.error_select_city.title);
					jsWin.instance().setContent('<div class="error_alert">'+this.options.error_select_city.content+'</div>');
					jsWin.instance().addOptions({modal:true});
					jsWin.instance().show();
					jsWin.instance().center();
	
					$(this.options.input_city_htmlid).blur();
				}
	
			}.bind(this));
		}


		if($('sell_property_step2_master_active'))
		{
			this.checkActive = function()
			{
				if($('sell_property_step2_master_active').checked == true)
				{
					$('sell_property_step2_master_publish_date').enable();
				}
				else
				{
					$('sell_property_step2_master_publish_date').disable();
				}
			}

			$('sell_property_step2_master_active').observe('click', function(ev)
			{
				Page.instance.checkActive();
			});

			Page.instance.checkActive();
		}

		if($('btn_cal_publish_date'))
		{
			$('btn_cal_publish_date').observe('click', function(ev)
			{
				Date.prototype.toFormattedString = PageFront.toDateFormattedString;

				new CalendarDateSelect('sell_property_step2_master_publish_date', {
						popup:"force",
						time:true
				});
			});
		}

		this.checkInputMaxLength = function()
		{
			if($('sell_property_step2_master_description_bilingual').getValue().length > 1000)
			{
				$('sell_property_step2_master_description_bilingual').setValue($('sell_property_step2_master_description_bilingual').getValue().truncate(1000, ''));
			}

			$('description_bilingual_nbchar').update(1000 - $('sell_property_step2_master_description_bilingual').getValue().length);

			if($('sell_property_step2_master_inclusions').getValue().length > 250)
			{
				$('sell_property_step2_master_inclusions').setValue($('sell_property_step2_master_inclusions').getValue().truncate(250, ''));
			}

			$('inclusions_nbchar').update(250 - $('sell_property_step2_master_inclusions').getValue().length);

			if($('sell_property_step2_master_exclusions').getValue().length > 250)
			{
				$('sell_property_step2_master_exclusions').setValue($('sell_property_step2_master_exclusions').getValue().truncate(250, ''));
			}

			$('exclusions_nbchar').update(250 - $('sell_property_step2_master_exclusions').getValue().length);
		}.bind(this);

		if($('sell_property_step2_master_description_bilingual'))
		{
			$('sell_property_step2_master_description_bilingual').observe('keyup', function(event)
			{
				Page.instance.checkInputMaxLength();
			});

			$('sell_property_step2_master_description_bilingual').observe('change', function(event)
			{
				Page.instance.checkInputMaxLength();
			});

			$('description_bilingual_nbchar').update(1000 - $('sell_property_step2_master_description_bilingual').getValue().length);
		}


		if($('sell_property_step2_master_inclusions'))
		{
			$('sell_property_step2_master_inclusions').observe('keyup', function(event)
			{
				Page.instance.checkInputMaxLength();
			});

			$('sell_property_step2_master_inclusions').observe('change', function(event)
			{
				Page.instance.checkInputMaxLength();
			});

			$('inclusions_nbchar').update(250 - $('sell_property_step2_master_inclusions').getValue().length);
		}

		if($('sell_property_step2_master_exclusions'))
		{
			$('sell_property_step2_master_exclusions').observe('keyup', function(event)
			{
				Page.instance.checkInputMaxLength();
			});

			$('sell_property_step2_master_exclusions').observe('change', function(event)
			{
				Page.instance.checkInputMaxLength();
			});

			$('exclusions_nbchar').update(250 - $('sell_property_step2_master_exclusions').getValue().length);
		}
	},


	executePropertyEditStep3: function()
	{
		this.detachApartment = function(apartId, position)
		{
			if(!apartId)
			{
				if($('apartment_box_'+position))
				{
					$('apartment_box_'+position).remove();
					Page.instance.calculateIncome();
				}
			}
			else
			{
				if(confirm(this.options.detachApartmentConfirm))
				{
					new AjaxRequest(this.options.ajaxUrlDetachApartment, {
						parameters: 'apartment_id='+apartId+'&position='+position
					});
				}
			}

		}.bind(this);


		this.applyElectricityToAll = function(elemId)
		{
			$$('select.apart_electricity').each(function(el)
			{
				el.setValue($(elemId).getValue());
			});
		}

		this.applyHeatingToAll = function(elemId)
		{
			$$('select.apart_heating').each(function(el)
			{
				el.setValue($(elemId).getValue());
			});
		}


		if($('btn_add_apartment_form'))
		{
			$('btn_add_apartment_form').writeAttribute('href', 'javascript:void(0);');

			$('btn_add_apartment_form').observe('click', function(event)
			{
				new AjaxRequest(this.options.ajaxUrlAddApartmentGroup, {
					parameters:'nb_apart='+$$('.revenuBox').size()
				});

			}.bind(this));
		}

		this.calculateIncome = function()
		{
			this.incomeTotalMonth = 0;

			if(!this.calculateIncomeSet)
			{
				this.calculateIncomeSet = $H();
			}

			$$('.incomeEvent').each(function(elemForEvent)
			{
				if(!this.calculateIncomeSet.get(elemForEvent.identify()))
				{
					this.calculateIncomeSet.set(elemForEvent.identify(), 
						elemForEvent.observe('change', function(event)
						{
							Page.instance.calculateIncome();
						})
					)
				}

			}.bind(this));

			$$('.checkIncomeEvent').each(function(elemForEvent)
			{
				if(!this.calculateIncomeSet.get(elemForEvent.identify()))
				{
					this.calculateIncomeSet.set(elemForEvent.identify(), 
						elemForEvent.observe('click', function(event)
						{
							Page.instance.calculateIncome();
						})
					)
				}

				rel = elemForEvent.readAttribute('rel');

				if(elemForEvent.checked == true)
				{
					if($('sell_property_step3_master_'+rel+'_nb'))
					{
						$('sell_property_step3_master_'+rel+'_nb').enable();
					}

					if($('sell_property_step3_master_'+rel+'_income'))
					{
						$('sell_property_step3_master_'+rel+'_income').enable();
					}

					if($('sell_property_step3_master_'+rel+'_income_text'))
					{
						$('sell_property_step3_master_'+rel+'_income_text').enable();
					}
				}
				else
				{
					if($('sell_property_step3_master_'+rel+'_nb'))
					{
						$('sell_property_step3_master_'+rel+'_nb').disable();
						$('sell_property_step3_master_'+rel+'_nb').setValue('');
					}

					if($('sell_property_step3_master_'+rel+'_income'))
					{
						$('sell_property_step3_master_'+rel+'_income').disable();
						$('sell_property_step3_master_'+rel+'_income').setValue('');
					}

					if($('sell_property_step3_master_'+rel+'_income_text'))
					{
						$('sell_property_step3_master_'+rel+'_income_text').disable();
						$('sell_property_step3_master_'+rel+'_income_text').setValue('');
					}
				}

			}.bind(this));

			$$('.anIncome').each(function(price)
			{
				var reg = /([0-9])+_master_price$/;
				var apartPosition = reg.exec(price.identify());

				if(apartPosition)
				{
					apartPosition = apartPosition[1];
	
					nbIdentical = $('apartments_'+apartPosition+'_master_nb_identical') ? parseInt($F('apartments_'+apartPosition+'_master_nb_identical')) : 1;
				}
				else
				{
					nbIdentical = $(price.identify().gsub('_income', '_nb')) ? $F(price.identify().gsub('_income', '_nb')).gsub(' ', '') : 1;

					if(isNaN(nbIdentical)) { nbIdentical = 1 }
				}

				if(isNaN(nbIdentical)) { nbIdentical = 0 }

				priceIncome = isNaN(parseFloat(price.getValue())) ? 0 : parseFloat(price.getValue().gsub(' ', '').gsub(',', '.'));

				this.incomeTotalMonth += priceIncome * nbIdentical;

			}.bind(this));

			incomeTotalMonth = (Math.round(this.incomeTotalMonth*100)/100);
			incomeTotalYear = (Math.round(this.incomeTotalMonth*12*100)/100);

			$('property_income_month').update(number_format(incomeTotalMonth, 2, ',', ' '));
			$('property_income_year').update(number_format(incomeTotalYear, 2, ',', ' '));

		}.bind(this);

		this.calculateIncome();


		this.verifOccupation = function(elem, relElem)
		{
			if(elem.checked == true && elem.hasClassName('owner_occupation'))
			{
				relElem.checked = true;
			}

			if(elem.checked == false && elem.hasClassName('is_occupation'))
			{
				relElem.checked = false;
			}
		}

		$$('.owner_occupation', '.is_occupation').each(function(el)
		{
			el.observe('click', function(ev)
			{
				Page.instance.verifOccupation(el, $(el.readAttribute('rel')));
			});

		});
	},

	executePropertyEditStep4: function()
	{
		PageFront.calculateSpending = function()
		{
			spendingTotalMonth = 0;
			$$('form.spending').first().select('input.spending').each(function(spending)
			{
				spendingCost = isNaN(parseFloat(spending.getValue())) ? 0 : parseFloat(spending.getValue().gsub(' ', '').gsub(',', '.'));

				if(!PageFront.calculateSpendingSet)
				{
					spending.observe('change', function(event)
					{
						PageFront.calculateSpending();
					});
				}

				spendingTotalMonth += spending.hasClassName('byYear') ? spendingCost/12 : spendingCost;
			});

			spendingTotalMonthOutput = (Math.round(spendingTotalMonth*100)/100);
			spendingTotalYearOutput = (Math.round(spendingTotalMonth*12*100)/100);

			$('property_spending_month').update(number_format(spendingTotalMonthOutput, 2, ',', ' '));
			$('property_spending_year').update(number_format(spendingTotalYearOutput, 2, ',', ' '));

			PageFront.calculateSpendingSet = true;
		}


		PageFront.verifMaintenanceSpending = function()
		{

			if(!$('sell_property_step4_master_has_maintenance_spending').checked)
			{
				$('sell_property_step4_master_maintenance_spending').setValue('');
				$('sell_property_step4_master_maintenance_spending').disable();
			}
			else
			{
				$('sell_property_step4_master_maintenance_spending').enable();
			}

			PageFront.calculateSpending();
		}

		$('sell_property_step4_master_has_maintenance_spending').observe('click', PageFront.verifMaintenanceSpending);

		PageFront.verifMaintenanceSpending();


		PageFront.verifOtherSpending = function()
		{

			if(!$('sell_property_step4_master_has_other_spending').checked)
			{
				$('sell_property_step4_master_other_spending').setValue('');
				$('sell_property_step4_master_other_spending').disable();

				$('sell_property_step4_master_other_spending_text').setValue('');
				$('sell_property_step4_master_other_spending_text').disable();
			}
			else
			{
				$('sell_property_step4_master_other_spending').enable();
				$('sell_property_step4_master_other_spending_text').enable();
			}

			PageFront.calculateSpending();
		}

		$('sell_property_step4_master_has_other_spending').observe('click', PageFront.verifOtherSpending);

		PageFront.verifOtherSpending();
	},


	executePropertyEditStep5 : function()
	{
		this.calculate = function()
		{
			this.municipalAssessmentSpendingTotalMonth = 0;
			this.lotSizeArea = 1;
			this.buildingSizeArea = 1;

			if(!this.calculateSet)
			{
				this.calculateSet = $H();
			}

			$$('.spendingEvent').each(function(elemForEvent)
			{
				if(!this.calculateSet.get(elemForEvent.identify()))
				{
					this.calculateSet.set(elemForEvent.identify(), 
						elemForEvent.observe('change', function(event)
						{
							Page.instance.calculate();
						})
					)
				}

			}.bind(this));


			$$('.municipal_assessment_spending').each(function(spending)
			{
				municipalAssessmentSpending = isNaN(parseFloat(spending.getValue().gsub(' ', '').gsub(',', '.'))) ? 0 : parseFloat(spending.getValue().gsub(' ', '').gsub(',', '.'));

				this.municipalAssessmentSpendingTotalMonth += municipalAssessmentSpending;

			}.bind(this));

			if(!$('sell_property_step5_master_municipal_assessment_readonly'))
			{
				new Insertion.After($('sell_property_step5_master_municipal_assessment'), 
					'<span id="sell_property_step5_master_municipal_assessment_readonly" class="readonly calculate">'+number_format(this.municipalAssessmentSpendingTotalMonth, 2, ',', ' ')+' $</span>'
				)
			}
			else
			{
				$('sell_property_step5_master_municipal_assessment_readonly').update(number_format(this.municipalAssessmentSpendingTotalMonth, 2, ',', ' ')+' $');
			}

			$('sell_property_step5_master_municipal_assessment').setValue(this.municipalAssessmentSpendingTotalMonth.toFixed(2));
			//$('sell_property_step5_master_municipal_assessment').writeAttribute('type', 'hidden');
			$('sell_property_step5_master_municipal_assessment').setStyle({visibility:'hidden', width:'0px', height:'0px'});




			$$('.lot_size_spending').each(function(size)
			{
				lotSizeSpending = isNaN(parseFloat(size.getValue().gsub(' ', '').gsub(',', '.'))) ? 0 : parseFloat(size.getValue().gsub(' ', '').gsub(',', '.'));

				this.lotSizeArea *= lotSizeSpending;

			}.bind(this));

			unitSelect = $('sell_property_step5_master_lot_size_measure_unit');

			if(!$('sell_property_step5_master_lot_size_readonly'))
			{
				new Insertion.After($('sell_property_step5_master_lot_size'), 
					'<span id="sell_property_step5_master_lot_size_readonly" class="readonly calculate">'+number_format(this.lotSizeArea, 2, ',', ' ')+' '+unitSelect.options[unitSelect.options.selectedIndex].innerHTML+' <sup>2</sup></span>'
				)
			}
			else
			{
				$('sell_property_step5_master_lot_size_readonly').update(number_format(this.lotSizeArea, 2, ',', ' ')+' '+unitSelect.options[unitSelect.options.selectedIndex].innerHTML+' <sup>2</sup>');
			}

			$('sell_property_step5_master_lot_size').setValue(this.lotSizeArea);
			//$('sell_property_step5_master_lot_size').writeAttribute('type', 'hidden');
			$('sell_property_step5_master_lot_size').setStyle({visibility:'hidden', width:'0px', height:'0px'});




			$$('.building_size_spending').each(function(size)
			{
				buildingSizeSpending = isNaN(parseFloat(size.getValue().gsub(' ', '').gsub(',', '.'))) ? 0 : parseFloat(size.getValue().gsub(' ', '').gsub(',', '.'));

				this.buildingSizeArea *= buildingSizeSpending;

			}.bind(this));

			unitSelect = $('sell_property_step5_master_building_size_measure_unit');

			if(!$('sell_property_step5_master_building_size_readonly'))
			{
				new Insertion.After($('sell_property_step5_master_building_size'), 
					'<span id="sell_property_step5_master_building_size_readonly" class="readonly calculate">'+number_format(this.buildingSizeArea, 2, ',', ' ')+' '+unitSelect.options[unitSelect.options.selectedIndex].innerHTML+' <sup>2</sup></span>'
				)
			}
			else
			{
				$('sell_property_step5_master_building_size_readonly').update(number_format(this.buildingSizeArea, 2, ',', ' ')+' '+unitSelect.options[unitSelect.options.selectedIndex].innerHTML+' <sup>2</sup>');
			}

			$('sell_property_step5_master_building_size').setValue(this.buildingSizeArea);
			//$('sell_property_step5_master_building_size').writeAttribute('type', 'hidden');
			$('sell_property_step5_master_building_size').setStyle({visibility:'hidden', width:'0px', height:'0px'});

		}


		this.calculate();

		this.notSubmitInput = function(inputElem)
		{
			inputElem.observe('keypress', function(event)
			{
				if(event.keyCode == Event.KEY_RETURN)
				{
					Page.instance.tmpForm = Event.element(event).up('form');

					if(!Page.instance.tmpFormAction)
					{
						Page.instance.tmpFormAction = Page.instance.tmpForm.readAttribute('action');
					}

					Page.instance.tmpForm.writeAttribute('action', 'javascript:void(0);');

					Page.instance.toggleForm = function()
					{
						Page.instance.tmpForm.writeAttribute('action', Page.instance.tmpFormAction);
						AjaxRequest.isBusy = false;
					};

					AjaxRequest.isBusy = true;
					Page.instance.toggleForm.delay(0.1);
				}
			});
		}

		$$('.notSubmitInput').each(function(notSubmitInputElem)
		{
			this.notSubmitInput(notSubmitInputElem);

		}.bind(this));

		this.initImageList = function()
		{
			$$('.plex_image_name').each(function(plexImageInput)
			{
				plexImageInput.observe('change', function(event)
				{
					var reg = /[0-9]+$/; 
					var imageId = reg.exec(plexImageInput.identify()).first();
	
					imageName = plexImageInput.getValue();
	
					new AjaxRequest(this.options.changeImageTitle, {
						parameters:'image_id='+imageId+'&image_name='+imageName
					});
	
				}.bind(this));
	
			}.bind(this));

		}.bind(this)

		this.initImageList();

		if(!this.options.formStandard)
		{
			this.executeYourProfileInfoIndex();
		}

		//IMAGE SELECTOR


		this.imageSelector = new ImageSelector({
			uploaded:function(response, instance)
			{
				if(response.success)
				{
					new AjaxRequest(this.options.ajaxUrlImageUploaded.unescapeHTML(), {
						parameters:'image_id='+response.image.id+'&image_name='+$F('sell_property_step5_master_image_name')
					});
				}
				else
				{
					new AjaxRequest(this.options.ajaxUrlImageUploaded.unescapeHTML(), {
						parameters:'error=1&message='+ response.message
					});
				}

				instance.inputFileElem.setValue('');
				$('sell_property_step5_master_image_name').setValue('');

			}.bind(this),

			onChoose:function(image, instance)
			{
				if(!$(this.options.thumbImageImgHtmlId))
				{
					$(this.options.thumbImageBoxHtmlId).update('<img src="'+this.options.urlBase+image.size1.url+'" id="'+this.options.thumbImageImgHtmlId+'" />');
				}
				else
				{
					$(this.options.thumbImageImgHtmlId).writeAttribute('src', this.options.urlBase+image.size1.url);
				}

				$(this.options.thumbImageHtmlId).setValue(image.id);

			}.bind(this),

			onRemove:function(image, instance)
			{
				if(confirm(instance.options.messageConfirm))
				{
					new AjaxRequest(this.options.ajaxUrlImageDelete, {
						parameters:'image_id='+image.id
					});
	
					if($F(this.options.thumbImageHtmlId) == image.id)
					{
						$(this.options.thumbImageHtmlId).setValue('');
						//$(this.options.thumbImageBoxHtmlId).update('');
					}
				}

			}.bind(this),

			messageConfirm:this.options.messageConfirm,

			messageInvalidExtensions:this.options.messageInvalidExtensions,
			titleInvalidExtensions:this.options.titleInvalidExtensions,

			messageEmpty:this.options.messageEmpty,
			titleEmpty:this.options.titleEmpty
		});


		$(this.options.inputFileHtmlId).observe('change', function(event)
		{
			this.imageSelector.upload(this.options.inputFileHtmlId, this.options.ajaxUrlImageUpload);
		}.bind(this));

		$('sell_property_step5_building_material').observe('change', function(event)
		{
			if($F('sell_property_step5_building_material') == 'other')
			{
				$('sell_property_step5_master_building_material_other').removeClassName('hide');
				$('sell_property_step5_master_building_material_other').show();
			}
			else
			{
				$('sell_property_step5_master_building_material_other').hide();
			}
		});
	},

	executePropertyEditStep6: function()
	{
		if($('btn_sign_preview'))
		{
			$('btn_sign_preview').observe('click', function(ev)
			{
				new AjaxRequest(this.options.ajaxUrlSignPreview);

			}.bind(this));
		}
	},

	executePropertyEditStep7: function()
	{
		$(this.options.input_country_htmlid).observe('change', function(event)
		{
			new AjaxRequest(this.options.ajaxUrlChangeCountry, {
				parameters:'country='+$F(this.options.input_country_htmlid)
			});

		}.bind(this));

		$(this.options.input_state_htmlid).observe('change', function(event)
		{
			new AjaxRequest(this.options.ajaxUrlChangeState, {
				parameters:'country='+$F(this.options.input_country_htmlid)+'&state_iso='+$F(this.options.input_state_htmlid)
			});

		}.bind(this));

		$(this.options.input_state_htmlid).observe('click', function(event)
		{
			if($F(this.options.input_country_htmlid).blank())
			{
				jsWin.instance().setTitle(this.options.error_select_state.title);
				jsWin.instance().setContent('<div class="error_alert">'+this.options.error_select_state.content+'</div>');
				jsWin.instance().addOptions({modal:true});
				jsWin.instance().show();
				jsWin.instance().center();

				$(this.options.input_state_htmlid).blur();
			}

		}.bind(this));
	},

	toggleDetails : function(me)
	{
		linkName = Element.extend(me).next('span').innerHTML;
		Element.extend(me).next('span').update(Element.extend(me).innerHTML);
		Element.extend(me).update(linkName);

		if(Element.extend(me).up('div').next('div').hasClassName('hide'))
		{
			Element.extend(me).up('div').next('div').removeClassName('hide');
		}
		else
		{
			Element.extend(me).up('div').next('div').addClassName('hide');
		}
	},

	executeAdminUserManagerBuyer: function()
	{
		$$('.user_is_seller').each(function(checkbox)
		{
			checkbox.observe('click', function(clickEvent)
			{
				var reg = /[0-9]+$/;
				var userId = reg.exec(checkbox.identify()).first();
	
				if(checkbox.checked)
				{
					new AjaxRequest(Page.instance.getOption('urlClean'), {
						parameters:{
							action:'add_in_group',
							group_ident:'seller',
							user_id:userId
						}
					});
				}
				else
				{
					if(confirm(Page.instance.getOption('confirm_remove_group')))
					{
						new AjaxRequest(Page.instance.getOption('urlClean'), {
							parameters:{
								action:'remove_to_group',
								group_ident:'seller',
								user_id:userId
							}
						});
					}
					else
					{
						checkbox.checked = true;
					}
				}
			});
		});

		$('admin_user_manager_buyer').select('.user_action_list .delete').each(function(el)
		{
			el.observe('click', function(ev)
			{
				if(confirm(this.options.messageConfirmDelete))
				{
					new AjaxRequest(el.readAttribute('href'));
				}

				return ev.stop();

			}.bind(this));

		}.bind(this));
	},

	executeAdminUserManagerSeller: function()
	{
		$('admin_user_manager_seller').select('.user_action_list .delete').each(function(el)
		{
			el.observe('click', function(ev)
			{
				if(confirm(this.options.messageConfirmDelete))
				{
					new AjaxRequest(el.readAttribute('href'));
				}

				return ev.stop();

			}.bind(this));

		}.bind(this));
	},

	executeYourProfileFolderIndex: function()
	{
		$('user_alerts_enabled').observe('click', function(ev)
		{
			status = $('user_alerts_enabled').checked ? 'enable' : 'disable';

			new AjaxRequest(this.options.urlChangeAlertStatus, {
				parameters:'status='+status
			});

		}.bind(this));
	},

	executeYourProfileAdsStatistic: function()
	{
		lines = [];

		if(this.options.chart_view_label)
		{
			dView = this.options.charts_pts.evalJSON();
			lines.push({data:dView.view, label:this.options.chart_view_label, lines:{show:true}, points:{show:true}});
		}

		if(this.options.chart_list_label)
		{
			dView = this.options.charts_pts.evalJSON();
			lines.push({data:dView.list, label:this.options.chart_list_label, lines:{show:true}, points:{show:true}});
		}

		/**
		* Draw the graph.
		*/

		var f = Flotr.draw(
			$('chart_plex_user_stats'),lines,{
				xaxis:{
					ticks: dView.xTicks
				},
				yaxis:{
					noTicks: 10,
					tickDecimals: 0
				},
				legend: {
					position: 'nw'
				},
				title: '',
				subtitle: '',
				mouse: {
					track: true,		// => true to track mouse
					position: 'se',		// => position to show the track value box
					trackFormatter: function(obj)
					{
						dateTime = new Date(1000*obj.x);

						return dateTime.toLocaleDateString()+' : '+obj.y;
					},	// => fn: int -> string
					margin: 3,		// => margin for the track value box
					color: '#ff3f19',	// => color for the tracking points, null to hide points
					trackDecimals: 0,	// => number of decimals for track values
					radius: 1,		// => radius of the tracking points
					sensibility: dView.options.unit_time * dView.options.interval		// => the smaller this value, the more precise you've to point with the mouse
				},
				spreadsheet: {
					show: true,
					tabGraphLabel: this.options.tabGraphLabel,
					tabDataLabel: this.options.tabDataLabel,
					toolbarDownload: this.options.toolbarDownload,
					toolbarSelectAll: this.options.toolbarSelectAll
				}
			}
		);

		if($('filter_plex_stats_master_filter_type'))
		{
			$('filter_plex_stats_master_filter_type').observe('change', function(ev)
			{

				if($F('filter_plex_stats_master_filter_type') == 'x')
				{
					$('chart_plex_custom_filter').setStyle({display:'block'});
					//$('chart_plex_navig_date').setStyle({display:'block'});
				}
				else
				{
					$('chart_plex_custom_filter').setStyle({display:'none'});
					$('chart_plex_navig_date').setStyle({display:'none'});
				}

			}.bind(this));
		}




		if($('btn_chart_stats_begin_time'))
		{
			$('btn_chart_stats_begin_time').observe('click', function(ev)
			{
				Date.prototype.toFormattedString = PageFront.toDateFormattedString;

				new CalendarDateSelect('filter_plex_stats_master_begin_time', {
						popup:"force",
						time:false
				});
			});
		}

		if($('btn_chart_stats_end_time'))
		{
			$('btn_chart_stats_end_time').observe('click', function(ev)
			{
				Date.prototype.toFormattedString = PageFront.toDateFormattedString;

				new CalendarDateSelect('filter_plex_stats_master_end_time', {
						popup:"force",
						time:false
				});
			});
		}

	}

});


PageFront.toDateFormattedString = function(include_time){

	str = this.getFullYear() + '-' + doublezero(this.getMonth()+1) + "-" + doublezero(this.getDate());

	if (include_time)
	{
		hour = this.getHours();
		str += " " + this.getAMPMHour() + ":" + this.getPaddedMinutes() //" " + this.getAMPM()
	}

	return str
}

