<?php
	require_once( $GLOBALS["site_root"] . "/includes/widgets/Captcha.inc.php" );
	require_once( $GLOBALS["site_root"] . "/includes/models/AdminUser.inc.php" );
	require_once( $GLOBALS["site_root"] . "/includes/models/MyMember.inc.php" );

	class CaptchaHtmlHide extends Captcha {
		protected $sHiddenHtml = "testing";
		protected $bEnabled = true;
		
		public function setHiddenHtml( $v ) { $this->sHiddenHtml = $v; }
		public function setEnable( $v ) { $this->bEnabled = $v; }

		public function __construct( $id , $parent ) {
			parent::__construct( $id , $parent );
			if (AdminUserModel::isAdminLoggedIn()) $this->bEnabled = false;
			$mm = new MyMemberModel();
			if ($mm->isMemberLoggedIn()) $this->bEnabled = false;
		}

		/*
		public function handleEvents() {
			$retval = parent::handleEvents();
			if (!$this->isSubmitted()) return $retval;
			return 
		}
		*/

		protected function generateHtmlHead() {
			return FNTH_embed_js_string( sprintf("
				var RecaptchaOptions = {
					theme : 'white',
					lang : '%s',
					custom_translations : {
						instructions_visual : '%s',
						instructions_audio : '%s',
						play_again : '%s',
						cant_hear_this : '%s',
						visual_challenge : '%s',
						audio_challenge : '%s',
						refresh_btn : '%s',
						help_btn : '%s',
						incorrect_try_again : '%s'
					}
				};",
				$GLOBALS["language"],
				__( "recaptcha_type_text" ),
				__( "recaptcha_audio" ),
				__( "recaptcha_play_again" ),
				__( "recaptcha_cant_hear_this" ),
				__( "recaptcha_visual_challenge" ),
				__( "recaptcha_audio_challenge" ),
				__( "recaptcha_refresh" ),
				__( "recaptcha_help" ),
				__( "recaptcha_incorrect_try_again" ),
				$this->getId() ) );
		}

		public function renderCaptcha() {
			$retval = "";
			switch ($this->sType) {
			case "recaptcha":
				FNTHPage::setPageHead( "recaptcha_ajax_js" , FNTH_embed_js_file( "http://www.google.com/recaptcha/api/js/recaptcha_ajax.js" ) );
				$retval = "";
				break;
			}
			return $retval;
		}

		public function render() {
			if ($this->sRendered) return $this->sRendered;
			$this->setParam( "bEnabled" , $this->bEnabled );
			if ($this->bEnabled) {
				FNTHPage::setPageHead( "captchahtmlhideinit" , $this->generateHtmlHead() );
				if (!$this->isSubmitted()) return parent::render();

				$data = array();
				$data["id"]=$this->getId();
				if (!$this->isValidResponse()) {
					$data["error"]=__("invalid_captcha");
				} else {
					$data["html"]=$this->sHiddenHtml;
				}
				FNTH_echo_json_response( $data );
				exit();
			} else {
				$this->setParam( "html" , $this->sHiddenHtml );
				return parent::render();
			}
		}

	}

?>
