<?php
	require_once( $GLOBALS["site_root"]."/includes/widgets/Widget.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/models/User.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/models/Member.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/models/TemplateMail.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/urlgen/MyUser.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/models/MyUser.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/models/MyMember.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/models/Constants.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/models/TodoList.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/models/TaskManager.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/models/Status.inc.php" );
	
	require_once( $GLOBALS["site_root"]."/includes/widgets/RegistrationName.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/RegistrationEmail.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/RegistrationPassword.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/MyLanguages.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/MySpecialties.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/NativeLanguage.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/BotJS.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/SystemArticle.inc.php" );
	
	require_once( $GLOBALS["site_root"]."/includes/widgets/Zipcode.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/County.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/Country.inc.php" );

	class RegistrationPane extends Widget {

		protected $oRegistrationIntro = null;

		protected $sRegistrationType = "freelancer";
		// general widgets
		protected $oBotJS = null;
		protected $oEmail = null;
		protected $oPassword = null;
		protected $oPassword2 = null;
		// translator widgets
		// protected $oUrl = null;
		protected $oNativeLanguage = null;
		protected $oWorkingLanguages = null;
		// protected $oSpecialties = null;
		// contact widgets
		protected $oZipcode = null;
		protected $oCity = null;
		protected $oAddress = null;
		protected $oCounty = null;
		protected $oCountry = null;
		// 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;

		// agency
		protected $oContactName = null;
		protected $oContactEmail = null;
		protected $oContactPhone = null;

		protected $sMode = "registration";

		public function setRegistrationType( $v ) { $this->sRegistrationType = $v; }
		
		protected function generateLangDefs() {
			fo3_add_js_langdef( "No_native_language" );
			fo3_add_js_langdef( "No_working_languages" );
			fo3_add_js_langdef( "Empty_contact_name" );
			fo3_add_js_langdef( "Empty_contact_email" );

			fo3_add_js_langdef( "Empty_name" );
			fo3_add_js_langdef( "Empty_contact_phone" );
			fo3_add_js_langdef( "Invalid_contact_phone" );
			fo3_add_js_langdef( "Wrong_or_used_email_address" );
			fo3_add_js_langdef( "Empty_password" );
			fo3_add_js_langdef( "Passwords_differ" );
			fo3_add_js_langdef( "Empty_zipcode" );
			fo3_add_js_langdef( "Empty_city" );
			fo3_add_js_langdef( "Empty_address" );
			fo3_add_js_langdef( "Empty_county" );
			fo3_add_js_langdef( "Empty_country" );

			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" );

			fo3_add_js_langdef( "registration_Too_much_working_language" );
			fo3_add_js_langdef( "registration_Acceptable_number_of_languages" );
		}

		protected function putSystemArticle( $name , $callback_meth = "" ) {
			$sa = new SystemArticle( "SystemArticle" , $this );
			$sa->setArticleFromName( $name );
			if (!empty( $callback_meth ))
				$this->$callback_meth( $sa );
			$this->setParam( "static_article" , $sa->render() );
		}

		protected function handleActivation() {
			$this->setParam( "error" , "" );
			$invalid = false;
			if (!isset( $this->aG["hash"] )) {
				$invalid = true;
			} else if (strlen( $this->aG["hash"] )<32) {
				$invalid = true;
			}

			if ($invalid) {
				$this->putSystemArticle( "sys_registration_activation_failed" );
				return;
			}

			$um = new UserModel();
			$user_id = $um->getUserIdFromHash( $this->aG["hash"] );
			if (!$user_id) {
				$this->putSystemArticle( "sys_registration_activation_failed" );
				return;
			}

			$user = $um->getUserFromUserId( $user_id );

			$already_acked = $um->isUserAcknowledged( $user_id );
			$um->acknowledgeUser( $user_id );

			// is it simple user or not
			$mm = new MemberModel();
			$member = $mm->getMemberFromUserId( $user_id );
			if (!$member) {
				// simple user
				$myuser = new MyUserModel();
				$myuser->loginUser( $user_id );

				$ulm = new UserLogModel();
				$ulm->logActivation( $user_id );
				$ulm->logLogin();
				$this->putSystemArticle( "sys_simple_user_registration_activation_succeed" );
				return;
			}
			
			$mm->setMemberDirty( $member["member_id"] , true );	// regisztracio is dirtyt general

			$mmm = new MyMemberModel();
			$mmm->loginUser( $user_id );

			$ulm = new UserLogModel();
			$ulm->logActivation( $user_id );
			$ulm->logLogin( $user_id );

			if (!$already_acked) $this->addMemberTodoList( $user_id );	// csak akkor van forced figcsi, ha mar nem az elso aktivalasa

			$article_name = sprintf( "sys_member_registration_activation_succeed" );
			$this->putSystemArticle( $article_name , "prepareSAMemberInfo" );
			


			if (!$already_acked) {
				// 1. mail a usernek
				$mm->updateMemberStatus( $member["member_id"] , STATUS_NEW_USER_HAS_TO_PAY );
				$tm = new TaskManagerModel();
				$task_id = $tm->createStatusSendTask( array( $member["member_id"] ) );

				// 2. mail az adminnal
				$tm = new TemplateMailModel( "sys_admin_member_registration_confirmed" );
				$cm = new ConstantsModel( $tm );
				$cm->addSystemParametersToTemplate();

				$mmm = new MyMemberModel();
				$cm->addSystemParametersToTemplate();
				$cm->addUserConstantsToTemplate( $mmm->getLoggedinUserId() );
				$cm->addMemberConstantsToTemplate( $mmm->getLoggedinMemberId() );

				$tm->sendMail( $GLOBALS["fo3_admin_email"] );
			}
		}

		protected function prepareSAMemberInfo( $obj ) {
			$cm = new ConstantsModel( $obj );
			$mmm = new MyMemberModel();
			$cm->addSystemParametersToTemplate();
			$cm->addUserConstantsToTemplate( $mmm->getLoggedinUserId() );
			$cm->addMemberConstantsToTemplate( $mmm->getLoggedinMemberId() );
		}

		protected function loadRegistrationPostedDatas() {
			$arr = array(
				$this->oBotJS, $this->oName , $this->oEmail , $this->oPassword , $this->oPassword2 ,
				/* $this->oUrl , */ $this->oNativeLanguage , $this->oWorkingLanguages , /* $this->oSpecialties , */
				$this->oZipcode , $this->oCity , $this->oAddress , $this->oCounty , $this->oCountry,
				$this->oBillingName , $this->oBillingZipcode , $this->oBillingCity , $this->oBillingAddress , $this->oBillingCountry,
				$this->oShippingName , $this->oShippingZipcode , $this->oShippingCity , $this->oShippingAddress , $this->oShippingCountry,
				$this->oContactName , $this->oContactEmail , $this->oContactPhone );
			foreach ($arr as $wdg) $wdg->refreshValueFromArray( $this->aP );
		}

		protected function checkGeneralParameters() {
			$warning = "";
			$user = new UserModel();

			if (!$this->oBotJS->isValid()) {
				$warning.=__("No_bots_please")."<br/>";
			}

			if (!strlen( $this->oName->getValue() ) ) {
				// empty name
				$warning.=__("Empty_name" )."<br/>";
			}

			if (!strlen( $this->oPassword->getValue() ) ) {
				$warning.=__("Empty_password" )."<br/>";
			}

			if ($this->oPassword->getValue()!=$this->oPassword2->getValue()) {
				$warning.=__("Password_mismatch" );
			}

			if (!strlen( $this->oEmail->getValue() ) ) {
				// check for empty email
				$warning.= __("Empty_email" );
			} else if (!fo3_is_email_valid( $this->oEmail->getValue() )) {
				// check for valid email
				$warning.=(!empty( $warning )) ? "<br/><br/>" : "";
				$warning.= __("Invalid_email" );
			} else {
				switch ($this->sRegistrationType) {
				case "jo":
					if ($user->isUserExistsOnEmail( $this->oEmail->getValue() )) {
						// check for already existing email
						$warning.=(!empty( $warning )) ? "<br/><br/>" : "";
						$warning.= __("Already_used_email" ) . "<br/><br/>";
						// $warning.=sprintf( "<a href=\"%s\">%s &raquo;</a>" , MyUserURL::renderForgottenPasswordURL() , __("Forgotten_password") );
					}
					break;
				default:
					// memberkent regisztral, de mar volt ajanlatkero (igy lehet ajanlatkerobol tag)
					$member = new MemberModel();
					if ($member->isMemberExistsOnEmail( $this->oEmail->getValue() )) {
						// check for already existing email
						$warning.=(!empty( $warning )) ? "<br/><br/>" : "";
						$warning.= __("Already_used_email" ) . "<br/><br/>";
						// $warning.=sprintf( "<a href=\"%s\">%s &raquo;</a>" , MyUserURL::renderForgottenPasswordURL() , __("Forgotten_password") );
					}
					break;
				}
			}

			if (!strlen( $this->oContactPhone->getValue())) $warning.=__("Empty_contact_phone")."<br/>";
			return $warning;
		}

		protected function checkTranslatorParameters() {
			// native language
			$warning = "";
			if (!strlen( $this->oNativeLanguage->getValue())) {
				$warning.=__("No_native_language")."<br/>";
			}
			// no working languages
			$languages = $this->oWorkingLanguages->getLanguages();
			if (empty( $languages )) {
				$warning.=__("No_working_languages")."<br/>";
			}
			// check for maximum number of languages
			$max_languages = SystemParametersModel::getVar( "max_languages_".$this->sRegistrationType );
			if (count($languages)>$max_languages) {
				$warning.=sprintf( "%s<br/>%s %d<br/><br/>",
					__("registration_Too_much_working_language"),
					__("registration_Acceptable_number_of_languages"),
					$max_languages );
			}

			// public address info
			if (!strlen( $this->oZipcode->getValue())) $warning.=__("Empty_zipcode")."<br/>";
			if (!strlen( $this->oCity->getValue())) $warning.=__("Empty_city")."<br/>";
			if (!strlen( $this->oAddress->getValue())) $warning.=__("Empty_address")."<br/>";
			if (!strlen( $this->oCounty->getValue())) $warning.=__("Empty_county")."<br/>";
			if (!strlen( $this->oCountry->getValue())) $warning.=__("Empty_country")."<br/>";
			
			// 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/>";
			
			// only agency
			switch ($this->sRegistrationType) {
			case "parttime":
			case "freelancer":
				return $warning;
			}

			// contact datas
			if (!strlen( $this->oContactName->getValue())) $warning.=__("Empty_contact_name")."<br/>";
			if (!strlen( $this->oContactEmail->getValue())) $warning.=__("Empty_contact_email")."<br/>";
			else if (!fo3_is_email_valid( $this->oContactEmail->getValue() )) $warning.=__("Invalid_contact_email")."<br/>";
			return $warning;
		}

		public function isUsedMemberAlias( $alias ) {
			$query = sprintf( "select count(*) as num from members where member_aliasname='%s'" , FNTH_db_addslashes( $alias ));
			return (FNTH_db_get_field( $query , "num")>0);
		}

		protected function prepareMemberDatasForRegistration() {
			$retval = array();
			$retval["member_aliasname"]=fo3_generate_unique_alias(
				$this->oName->getValue(),
				array( $this , "isUsedMemberAlias" ) , array( "lowercase" => false ) );
			$retval["member_username"]=$retval["member_aliasname"];
			$retval["member_type"]=$this->sRegistrationType;
			$retval["current_status"]=STATUS_NEW_USER;
			$retval["use_email"]="own";							// DEFAULTPARAM
			$retval["zip"]=$this->oZipcode->getValue();
			$retval["county_id"]=$this->oCounty->getValue();
			$retval["country_id"]=$this->oCountry->getValue();
			$retval["city"]=$this->oCity->getValue();
			$retval["address"]=$this->oAddress->getValue();

			// $retval["url"]=$this->oUrl->getValue();
			$retval["preferred_paymode"]="directdebit";			// DEFAULTPARAM
			$retval["billing_name"]=$this->oBillingName->getValue();
			$retval["billing_zip"]=$this->oBillingZipcode->getValue();
			$retval["billing_country_id"]=$this->oBillingCountry->getValue();
			$retval["billing_city"]=$this->oBillingCity->getValue();
			$retval["billing_address"]=$this->oBillingAddress->getValue();

			$retval["shipping_name"]=$this->oShippingName->getValue();
			$retval["shipping_zip"]=$this->oShippingZipcode->getValue();
			$retval["shipping_country_id"]=$this->oShippingCountry->getValue();
			$retval["shipping_city"]=$this->oShippingCity->getValue();
			$retval["shipping_address"]=$this->oShippingAddress->getValue();

			$retval["contact_name"]=$this->oContactName->getValue();
			$retval["contact_email"]=$this->oContactEmail->getValue();

			$retval["notifications"]="mylangs";					// DEFAULTPARAM
			$retval["custom_language"]=$this->oWorkingLanguages->getCustomLanguage();
			// $retval["custom_specialty"]=$this->oSpecialties->getCustomSpecialty();
			$retval["is_dirty"]=true;
			return $retval;
		}

		protected function prepareUserDatas() {
			$p = array(
				"user_name" => $this->oName->getValue(),
				"email" => $this->oEmail->getValue(),
				"pwd" => $this->oPassword->getValue(),
				"contact_phone" => $this->oContactPhone->getValue() );
			if (isset( $this->aP["registration_type"] ) && $this->aP["registration_type"]=="agency")
				$p["is_instant_sender"]=true;
			return $p;
		}

		protected function registrateJobOfferer() {
			$user = new UserModel();
			$user_id = 0;
			// do the registrations

			$p = $this->prepareUserDatas();
			$user_id = $user->addUser( $p );
			if (!$user_id) {
				FrontendPage::warningMessage( "registration_errors" , __( "error_Invalid_input" ) );
				return false;
			}
			$ulm = new UserLogModel();
			$ulm->logRegistration( $user_id , $this->oName->getValue() );
			
			$hash = $user->getUserHash( $user_id );
			
			$mail = new TemplateMailModel( "sys_simple_user_registration_confirmation_required" );
			$mail->setParam( "url_activate" , MyUserURL::renderRegistrationActivationURL( array( "hash" => $hash ) ) );
			$mail->setParam( "name" , $this->oName->getValue() );
			$mail->sendMail( $this->oEmail->getValue() );
			return true;
		}
		
		protected function registrateTranslator() {
			$user = new UserModel();

			// hatha volt ajanlatkero elotte (es most atregisztral feljebb)
			$user_id = $user->getUserIdFromEmail( $this->oEmail->getValue() );
		
			FNTH_db_begin_transaction();

			$p = $this->prepareUserDatas();

			if ($user_id) {
				// ez mar volt ajanlatkero
				$p["reg_tstamp"]=fo3_date("'Y-m-d H:i:s'");	// elorerantjuk a datumat
				$user->updateUser( $user_id , $p );
			} else {
				$user_id = $user->addUser( $p );
			}

			if (!$user_id) {
				FrontendPage::warningMessage( "registration_errors" , __( "error_Invalid_input" ) );
				FNTH_db_rollback_transaction();
				return false;
			}

			// now member information
			$datas = $this->prepareMemberDatasForRegistration();

			$languages = $this->oWorkingLanguages->getLanguages();
			$mm = new MemberModel();
			$member_id = $mm->addMember( $user_id , $datas );
			if (!$member_id) {
				FrontendPage::warningMessage( "registration_errors" , __( "error_Invalid_input" ) );
				FNTH_db_rollback_transaction();
				return false;
			}

			$mm->setMemberNativeLanguage( $member_id , $GLOBALS["langcode"] , $this->oNativeLanguage->getValue() );
			$mm->setMemberLanguages( $member_id , $languages );
			// SL110606 - create language pair array and insert it
			$langpairs = array();
			$servicetype_id=1;
			$langpairs[$servicetype_id]=array();
			foreach ($languages as $src_lang_id) {
				$langpairs[$servicetype_id][$src_lang_id]=array();
				foreach ($languages as $dst_lang_id)
					if ($src_lang_id!=$dst_lang_id)
						$langpairs[$servicetype_id][$src_lang_id][$dst_lang_id]=true;
			}
			$mm->setMemberLanguagePairs3D( $member_id , $langpairs );

			// $member->setMemberSpecialties( $member_id , $this->oSpecialties->getSpecialties() );
			FNTH_db_commit_transaction();

			$ulm = new UserLogModel();
			$ulm->logMemberRegistration(
				$user_id ,
				$member_id ,
				$this->sRegistrationType ,
				$this->oName->getValue() );

			$hash = $user->getUserHash( $user_id );

			// 1. level a usernek
			$tm = new TaskManagerModel();
			$task_id = $tm->createStatusSendTask( array( $member_id ) );

			// 2. level az adminnak
			$tm = new TemplateMailModel( "sys_admin_member_registration" );
			$cm = new ConstantsModel( $tm );
			$cm->addSystemParametersToTemplate();
			$cm->addUserConstantsToTemplate( $user_id );
			$cm->addMemberConstantsToTemplate( $member_id );
			$tm->sendMail( $GLOBALS["fo3_admin_email"] );
			return true;
		}

		protected function handleRegistration() {
			if (!isset( $this->aP["smb_registration"] )) return;
			$this->loadRegistrationPostedDatas();
			$warning = "";
			switch ($this->sRegistrationType) {
			case "jo":
				// job offerer
				$warning = $this->checkGeneralParameters();
				// all the validations are done
				if (!empty($warning)) {
					FrontendPage::warningMessage( "registration_errors" , $warning );
					break;
				}
				if (!$this->registrateJobOfferer()) break;
				FNTH_redirect( MyUserURL::renderRegistrationSubmittedURL( ));	// array( "hash" => $hash ) ) );
				break;
			case "parttime":
			case "freelancer":
			case "agency":
				// translators
				$warning = $this->checkGeneralParameters();
				$warning.= $this->checkTranslatorParameters();
				if (!empty($warning)) {
					FrontendPage::warningMessage( "registration_errors" , $warning );
					break;
				}
				if (!$this->registrateTranslator()) break;
				FNTH_redirect( MyUserURL::renderRegistrationSubmittedURL( ));	// array( "hash" => $hash ) ) );
				break;
			}
		}
		
		public function handleEvents( ) {
			$this->handleChildEvents();
			if (isset( $this->aG["mode"] )) $this->sMode = $this->aG["mode"];
			if (isset( $this->aG["registration_type"] )) $this->sRegistrationType = $this->aG["registration_type"];
			if (isset( $this->aP["registration_type"] )) $this->sRegistrationType = $this->aP["registration_type"];

			switch ($this->sMode) {
			case "registration":
				$this->handleRegistration();
				break;
			case "activation":
				$this->handleActivation();
				break;
			}
		}

		protected function generateGeneralWidgets() {
			$ctrlw = 200;
			$this->oRegistrationIntro = new SystemArticle( "registration_intro" , $this );
			$this->oBotJS = new BotJS( "BotJS" , $this );
			$this->oName = new RegistrationName( "name" , $this );
			$this->oName->setWidth( $ctrlw );
			$this->oEmail = new RegistrationEmail( "email" , $this );
			$this->oEmail->setWidth( $ctrlw );
			$this->oPassword = new RegistrationPassword( "pwd" , $this );
			$this->oPassword->setWidth( $ctrlw );
			$this->oPassword2 = new RegistrationPassword( "pwd2" , $this );
			$this->oPassword2->setWidth( $ctrlw );
			$this->oContactPhone = new FrontendTextbox( "contact_phone" , $this );
			$this->oContactPhone->setMandatory( true );
			$this->oContactPhone->setWidth( $ctrlw );
		}

		protected function generateTranslatorWidgets() {
			// $this->oUrl = new FrontendTextbox( "url" , $this );
			$this->oNativeLanguage = new NativeLanguage( "native_language" , $this );
			$this->oWorkingLanguages = new MyLanguages( "working_languages" , $this );
			$this->oWorkingLanguages->setLanguage( 1 );
			// $this->oSpecialties = new MySpecialties( "specialties" , $this );
		}

		protected function generateContactWidgets() {
			$this->oZipcode = new Zipcode( "zip" , $this );
			$this->oZipcode->setAttribute( "maxlength" , 16 );
			$this->oZipcode->setMandatory( true );
			$this->oZipcode->setWidth( 80 );
			$this->oCity = new FrontendTextbox( "city" , $this );
			$this->oCity->setAttribute( "maxlength" , 64 );
			$this->oCity->setMandatory( true );
			$this->oCity->setWidth( 225 );

			$this->oAddress = new FrontendTextbox( "address" , $this );
			$this->oAddress->setWidth( 370 );
			$this->oAddress->setAttribute( "maxlength" , 96 );
			$this->oAddress->setMandatory( true );

			$this->oCounty = new County( "county" , $this );
			$this->oCounty->setMandatory( true );
			$this->oCountry = new Country( "country" , $this );
			$this->oCountry->setMandatory( true );
			$this->oCountry->setValue( "hu" );
			$this->oCountry->setWidth( 370 );
		}

		protected function generateBillingWidgets() {
			$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() {
			$this->oShippingName = new FrontendTextbox( "shipping_name" , $this );
			$this->oShippingName->setWidth( 370 );
			$this->oShippingName->setAttribute( "maxlength" , 128 );
			$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->setAttribute( "maxlength" , 64 );
			$this->oShippingCity->setMandatory( true );
			$this->oShippingCity->setWidth( 225 );

			$this->oShippingAddress = new FrontendTextbox( "shipping_address" , $this );
			$this->oShippingAddress->setWidth( 370 );
			$this->oShippingAddress->setAttribute( "maxlength" , 96 );
			$this->oShippingAddress->setMandatory( true );
			$this->oShippingCountry = new Country( "shipping_country" , $this );
			$this->oShippingCountry->setMandatory( true );
			$this->oShippingCountry->setValue( "hu" );
			$this->oShippingCountry->setWidth( 370 );
		}

		protected function generateAgencyWidgets() {
			$this->oContactName = new FrontendTextbox( "contact_name" , $this );
			$this->oContactName->setMandatory( true );
			$this->oContactName->setAttribute( "maxlength" , 96 );
			$this->oContactEmail = new FrontendTextbox( "contact_email" , $this );
			$this->oContactEmail->setMandatory( true );
			$this->oContactEmail->setAttribute( "maxlength" , 128 );
		}

		public function __construct( $id = "RegistrationPane" , $parent = null ) {
			parent::__construct( $id , $parent );
			$this->generateGeneralWidgets();
			$this->generateTranslatorWidgets();
			$this->generateContactWidgets();
			$this->generateBillingWidgets();
			$this->generateShippingWidgets();
			$this->generateAgencyWidgets();
		}

		protected function generateJSVars() {
			fo3_add_js_var( "max_languages_parttime" , SystemParametersModel::getVar( "max_languages_parttime" ) );
			fo3_add_js_var( "max_languages_freelancer" , SystemParametersModel::getVar( "max_languages_freelancer" ) );
			fo3_add_js_var( "max_languages_agency" , SystemParametersModel::getVar( "max_languages_agency" ) );
		}

		protected function addMemberTodoList( $user_id ) {
			$tlm = new TodoListModel();

			$mmm = new MyMemberModel();
			$menu = $mmm->getMenu( true );
			foreach ($menu as $menuitem) {
				switch ($menuitem["name"]) {
				case "registrational_datas":
				case "languages":
				case "billing_shipping":
					break;
				default:
					$tlm->addTodoListItem( $user_id ,
						$menuitem["name"],
						$menuitem["title"],
						$menuitem["url"],
						true );
				}
			}
		}

		protected function initializeGeneralWidgets() {
			if (!isset( $this->oName )) return;
			$this->oName->setAttribute( "maxlength" , 35 );

			$this->oRegistrationIntro->setArticleFromName( "sys_registration_intro" );
			$this->oRegistrationIntro->setOnlyBody( true );
			$cm = new ConstantsModel( $this->oRegistrationIntro );
			$cm->addSystemParametersToTemplate();

			// add registrationtype
			$this->oRegistrationIntro->setParam( "regtype" , $this->sRegistrationType );
		}
		
		public function render() {
			if ($this->sRendered) return $this->sRendered;
			FNTHPage::setPageJSHead( "valid_phone_js" , ( $GLOBALS["site_www"]."/design/rp/js/valid_phone.js" ) );
			FNTHPage::addUniquePageFullInit( sprintf( "InitRegistrationPane( '%s' );\n" , $this->sRegistrationType ) );
			/*
			$mail = new TemplateMailModel( "registration" );
			$hash = "xxx";
			$mail->setParam( "url_activate" , MyUserURL::renderRegistrationActivationURL( array( "hash" => $hash ) ) );
			$mail->sendMail();
			*/
			$this->initializeGeneralWidgets();
			$this->generateLangDefs();
			$this->generateJSVars();

			$this->setParam( "mode" , $this->sMode );
			switch ($this->sMode) {
			case "registration":
				$this->setParam( "regtype" , $this->sRegistrationType );
				break;
			case "activation":
				break;
			}
			return parent::render();
		}

	}

?>
