<?php
	require_once( $GLOBALS["site_root"]."/includes/widgets/MemberWidget.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/MyProfileMenu.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/FrontendTextbox.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/FrontendSubmit.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/County.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/Country.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/Zipcode.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/PaymentOptions.inc.php" );
	
	require_once( $GLOBALS["site_root"]."/includes/models/MyMember.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/models/Member.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/models/TodoList.inc.php" );

	class MyBillingShippingPane extends MemberWidget {

		// billing widgets
		protected $oBillingName = null;
		protected $oBillingZipcode = null;
		protected $oBillingCity = null;
		protected $oBillingAddress = null;
		protected $oBillingCountry = null;
		
		// shipping widgets
		protected $oShippingName = null;
		protected $oShippingZipcode = null;
		protected $oShippingCity = null;
		protected $oShippingAddress = null;
		protected $oShippingCountry = null;

		// payment widgets
		protected $oPreferredPaymode = null;

		protected function generateBillingWidgets() {
			if (!$this->bLoggedIn) return;
			$this->oBillingName = new FrontendTextbox( "billing_name" , $this );
			$this->oBillingName->setWidth( 370 );
			$this->oBillingName->setAttribute( "maxlength" , 128 );
			$this->oBillingName->setMandatory( true );
			$this->oBillingZipcode = new Zipcode( "billing_zip" , $this );
			$this->oBillingZipcode->setAttribute( "maxlength" , 16 );
			$this->oBillingZipcode->setMandatory( true );
			$this->oBillingZipcode->setWidth( 80 );
			$this->oBillingCity = new FrontendTextbox( "billing_city" , $this );
			$this->oBillingCity->setAttribute( "maxlength" , 64 );
			$this->oBillingCity->setMandatory( true );
			$this->oBillingCity->setWidth( 225 );
			$this->oBillingAddress = new FrontendTextbox( "billing_address" , $this );
			$this->oBillingAddress->setAttribute( "maxlength" , 96 );
			$this->oBillingAddress->setMandatory( true );
			$this->oBillingAddress->setWidth( 370 );
			$this->oBillingCountry = new Country( "billing_country" , $this );
			$this->oBillingCountry->setMandatory( true );
			$this->oBillingCountry->setValue( "hu" );
			$this->oBillingCountry->setWidth( 370 );
		}

		protected function generateShippingWidgets() {
			if (!$this->bLoggedIn) return;
			$this->oContactName = new FrontendTextbox( "contact_name" , $this );
			$this->oContactName->setAttribute( "maxlength" , 128 );
			$this->oContactName->setWidth( 370 );

			$this->oShippingName = new FrontendTextbox( "shipping_name" , $this );
			$this->oShippingName->setAttribute( "maxlength" , 128 );
			$this->oShippingName->setWidth( 370 );
			$this->oShippingZipcode = new Zipcode( "shipping_zip" , $this );
			$this->oShippingZipcode->setAttribute( "maxlength" , 16 );
			$this->oShippingZipcode->setMandatory( true );
			$this->oShippingZipcode->setWidth( 80 );
			$this->oShippingCity = new FrontendTextbox( "shipping_city" , $this );
			$this->oShippingCity->setMandatory( true );
			$this->oShippingCity->setWidth( 225 );
			$this->oShippingAddress = new FrontendTextbox( "shipping_address" , $this );
			$this->oShippingAddress->setAttribute( "maxlength" , 96 );
			$this->oShippingAddress->setMandatory( true );
			$this->oShippingAddress->setWidth( 370 );
			$this->oShippingCountry = new Country( "shipping_country" , $this );
			$this->oShippingCountry->setMandatory( true );
			$this->oShippingCountry->setValue( "hu" );
			$this->oShippingCountry->setWidth( 370 );
		}

		protected function generateMiscWidgets() {
			if (!$this->bLoggedIn) return;
			$this->oPreferredPaymode = new PaymentOptions( "preferred_paymode" , $this );
		}

		public function handleEvents() {
			if (!$this->bLoggedIn) return parent::handleEvents();
			$this->refreshChildValuesFromArray();
			$submitted = false;
			if (isset( $this->oSubmit ) && $this->oSubmit->isSubmitted()) $submitted = true;
			if (isset( $this->oAdminSubmit ) && $this->oAdminSubmit->isSubmitted()) $submitted = true;
			if (!$submitted) return parent::handleEvents();

			$valid = true;

			$warning="";
			// billing address info
			if (!strlen( $this->oBillingName->getValue())) $warning.=__("Empty_billing_name")."<br/>";
			if (!strlen( $this->oBillingZipcode->getValue())) $warning.=__("Empty_billing_zipcode")."<br/>";
			if (!strlen( $this->oBillingCity->getValue())) $warning.=__("Empty_billing_city")."<br/>";
			if (!strlen( $this->oBillingAddress->getValue())) $warning.=__("Empty_billing_address")."<br/>";
			if (!strlen( $this->oBillingCountry->getValue())) $warning.=__("Empty_billing_country")."<br/>";
			
			// shipping address info
			if (!strlen( $this->oShippingName->getValue())) $warning.=__("Empty_shipping_name")."<br/>";
			if (!strlen( $this->oShippingZipcode->getValue())) $warning.=__("Empty_shipping_zipcode")."<br/>";
			if (!strlen( $this->oShippingCity->getValue())) $warning.=__("Empty_shipping_city")."<br/>";
			if (!strlen( $this->oShippingAddress->getValue())) $warning.=__("Empty_shipping_address")."<br/>";
			if (!strlen( $this->oShippingCountry->getValue())) $warning.=__("Empty_shipping_country")."<br/>";

			if (strlen( $warning ) ) {
				GlobalWarningMessage( "billing_shipping_errors" , $warning );
				$valid = false;
			}

			if (!$valid) {
				return parent::handleEvents();
			}


			$this->saveDatas( isset( $this->oAdminSubmit ) && $this->oAdminSubmit->isSubmitted() );

			// clear todo
			$tlm = new TodoListModel();
			$tlm->clearTodoListItem( $this->iUserId , "billing_shipping" );

			// log	
			$ulm = new UserLogModel();
			$ulm->logMemberActivity( USER_LOG_GENERAL | USER_GENERAL_MODIFIED_PROFILE , "változtatott számlázási/postázási adatain" );		// LANGFIX
			
			FNTH_refresh();
			return parent::handleEvents();
		}

		protected function saveDatas( $admin ) {
			$data = array();
			$old_datas = $this->member_model->getMemberFromMemberId( $this->iMemberId );
			$dirty = false;
			$admin_dirty = false;


			$data["billing_name"]=$this->oBillingName->getValue();
			if ($old_datas["billing_name"]!=$data["billing_name"]) {
				$dirty = $admin_dirty = true;
				$this->dirty_model->setDirtyObject( $this->iUserId , $this->sPageName , "billing_name" );
			}
			$data["billing_zip"]=$this->oBillingZipcode->getValue();
			if ($old_datas["billing_zip"]!=$data["billing_zip"]) {
				$dirty = $admin_dirty = true;
				$this->dirty_model->setDirtyObject( $this->iUserId , $this->sPageName , "billing_zip" );
			}
			$data["billing_city"]=$this->oBillingCity->getValue();
			if ($old_datas["billing_city"]!=$data["billing_city"]) {
				$dirty = $admin_dirty = true;
				$this->dirty_model->setDirtyObject( $this->iUserId , $this->sPageName , "billing_city" );
			}
			$data["billing_address"]=$this->oBillingAddress->getValue();
			if ($old_datas["billing_address"]!=$data["billing_address"]) {
				$dirty = $admin_dirty = true;
				$this->dirty_model->setDirtyObject( $this->iUserId , $this->sPageName , "billing_address" );
			}
			$data["billing_country_id"]=$this->oBillingCountry->getValue();
			if ($old_datas["billing_country_id"]!=$data["billing_country_id"]) $dirty = true;

			$data["shipping_name"]=$this->oShippingName->getValue();
			if ($old_datas["shipping_name"]!=$data["shipping_name"]) {
				$dirty = $admin_dirty = true;
				$this->dirty_model->setDirtyObject( $this->iUserId , $this->sPageName , "shipping_name" );
			}
			$data["shipping_zip"]=$this->oShippingZipcode->getValue();
			if ($old_datas["shipping_zip"]!=$data["shipping_zip"]) {
				$dirty = $admin_dirty = true;
				$this->dirty_model->setDirtyObject( $this->iUserId , $this->sPageName , "shipping_zip" );
			}
			$data["shipping_city"]=$this->oShippingCity->getValue();
			if ($old_datas["shipping_city"]!=$data["shipping_city"]) {
				$dirty = $admin_dirty = true;
				$this->dirty_model->setDirtyObject( $this->iUserId , $this->sPageName , "shipping_city" );
			}
			$data["shipping_address"]=$this->oShippingAddress->getValue();
			if ($old_datas["shipping_address"]!=$data["shipping_address"]) {
				$dirty = $admin_dirty = true;
				$this->dirty_model->setDirtyObject( $this->iUserId , $this->sPageName , "shipping_address" );
			}
			$data["shipping_country_id"]=$this->oShippingCountry->getValue();
			if ($old_datas["shipping_country_id"]!=$data["shipping_country_id"]) $dirty = true;

			$data["contact_name"]=$this->oContactName->getValue();
			if ($old_datas["contact_name"]!=$data["contact_name"]) {
				$dirty = $admin_dirty = true;
				$this->dirty_model->setDirtyObject( $this->iUserId , $this->sPageName , "contact_name" );
			}
			$data["preferred_paymode"]=$this->oPreferredPaymode->getValue();
			if ($old_values["preferred_paymode"]!=$data["preferred_paymode"]) $dirty = true;


			$this->member_model->updateMember( $this->iMemberId , $data );
			// clear admin ack
			if ($dirty) {
				// $this->user_model->adminAcknowledgeUser( $this->iUserId , false );		// mar a dirty csinalja
				$this->member_model->setMemberDirty( $this->iMemberId , true );
			}
			if ($admin) {
				$this->clearDirtyObjectAdmin( $this->sPageName );
			}
		}

		protected function loadDatas() {
			if (!$this->bLoggedIn) return;
			$data = $this->member_model->getMemberFromMemberId( $this->iMemberId );
			$this->oPreferredPaymode->setValue( $data["preferred_paymode"] );
			$this->oBillingName->setValue( $data["billing_name"] );	
			$this->oBillingZipcode->setValue( $data["billing_zip"] );
			$this->oBillingCity->setValue( $data["billing_city"] );
			$this->oBillingAddress->setValue( $data["billing_address"] );
			$this->oBillingCountry->setValue( $data["billing_country_id"] );
			
			$this->oShippingName->setValue( $data["shipping_name"] );	
			$this->oShippingZipcode->setValue( $data["shipping_zip"] );
			$this->oShippingCity->setValue( $data["shipping_city"] );
			$this->oShippingAddress->setValue( $data["shipping_address"] );
			$this->oShippingCountry->setValue( $data["shipping_country_id"] );
	
			$this->oContactName->setValue( $data["contact_name"] );
			if (AdminUserModel::isAdminLoggedIn()) {
				if ($this->dirty_model->isDirtyObject( $this->iUserId , $this->sPageName , "billing_name" ))
					$this->oBillingName->addClass( "adminblue" );
				if ($this->dirty_model->isDirtyObject( $this->iUserId , $this->sPageName , "billing_zip" ))
					$this->oBillingZipcode->addClass( "adminblue" );
				if ($this->dirty_model->isDirtyObject( $this->iUserId , $this->sPageName , "billing_city" ))
					$this->oBillingCity->addClass( "adminblue" );
				if ($this->dirty_model->isDirtyObject( $this->iUserId , $this->sPageName , "billing_address" ))
					$this->oBillingAddress->addClass( "adminblue" );

				if ($this->dirty_model->isDirtyObject( $this->iUserId , $this->sPageName , "shipping_name" ))
					$this->oShippingName->addClass( "adminblue" );
				if ($this->dirty_model->isDirtyObject( $this->iUserId , $this->sPageName , "shipping_zip" ))
					$this->oShippingZipcode->addClass( "adminblue" );
				if ($this->dirty_model->isDirtyObject( $this->iUserId , $this->sPageName , "shipping_city" ))
					$this->oShippingCity->addClass( "adminblue" );
				if ($this->dirty_model->isDirtyObject( $this->iUserId , $this->sPageName , "shipping_address" ))
					$this->oShippingAddress->addClass( "adminblue" );
				
				if ($this->dirty_model->isDirtyObject( $this->iUserId , $this->sPageName , "contact_name" ))
					$this->oContactName->addClass( "adminblue" );
			}
		}

		protected function generateLangDefs() {
			fo3_add_js_langdef( "Empty_billing_name" );
			fo3_add_js_langdef( "Empty_billing_zipcode" );
			fo3_add_js_langdef( "Empty_billing_city" );
			fo3_add_js_langdef( "Empty_billing_address" );
			fo3_add_js_langdef( "Empty_billing_country" );

			fo3_add_js_langdef( "Empty_shipping_name" );
			fo3_add_js_langdef( "Empty_shipping_zipcode" );
			fo3_add_js_langdef( "Empty_shipping_city" );
			fo3_add_js_langdef( "Empty_shipping_address" );
			fo3_add_js_langdef( "Empty_shipping_country" );
			
		}

		public function __construct( $id , $parent ) {
			parent::__construct( $id , $parent );
			if (!$this->bLoggedIn) return;
			$this->sPageName = "billing_shipping";
			$this->oMyProfileMenu->setCurrentMenuItem( $this->sPageName );
			
			$this->generateBillingWidgets();
			$this->generateShippingWidgets();
			$this->generateMiscWidgets();

			$this->generateMemberWidgetNormalSubmit( $this->sPageName );
		}

		public function render() {
			if ($this->sRendered) return $this->sRendered;
			// load the datas
			$this->generateLangDefs();
			$this->loadDatas();
			fo3_form_change_warning( "document.frm_billing_shipping" );
			return parent::render();
		}

	}

?>
