<? defined('_JEXEC') or die('Restricted access'); ?>
<? defined('KOOWA') or die('Restricted access'); ?>

<?=$this->renderHelper('behavior.mootools'); ?>

<?
// Load language file
$lang = &JFactory::getLanguage();
$lang->load('com_ohanah');
$lang->load('com_ohanah', JPATH_ADMINISTRATOR);
?>

<style src="/media/com_ohanah/v2/frontend-event-form.css" />
<div class="panelContent ohanah">

	<? if ($joomlaVersion != '3.0') : ?>
		<? if (($params->get('loadJQuery') != '0') && (!JFactory::getApplication()->get('jquery'))) : ?>
			<? JFactory::getDocument()->addScript(JURI::root(1).'/media/com_ohanah/js/jquery.min.js');?>
			<? JFactory::getDocument()->addScript(JURI::root(1).'/media/com_ohanah/js/jquery-migrate-1.2.1.min.js');?>
			<? JFactory::getApplication()->set('jquery', true); ?>
		<? endif; ?>
	<? else : ?>
		<? JHtml::_('behavior.framework', true); ?>
		<? JHtml::_('jquery.framework'); ?>
	<? endif ?>

	<!-- <style src="/media/com_ohanah/css/jquery-ui.css" />
	<script src="/media/com_ohanah/js/jquery-ui-1.9.2/js/jquery-ui-1.9.2.custom.min.js" /> -->

	<?=$this->renderHelper('behavior.validator') ?>
	<? 
		$lang =& JFactory::getLanguage();
		$locale = $lang->get('tag');

		$localeMap = Array(
				"en-GB" => "en-US",
				"af-ZA" => "af-ZA",
				"ar-AA" => "ar",
				"bs-BA" => "en-US",
				"bg-BG" => "en-US",
				"ca-ES" => "ca-CA",
				"zh-TW" => "zh-CH",
				"hr-HR" => "en-US",
				"cs-CZ" => "cs-CZ",
				"ds-DK" => "en-US",
				"nl-NL" => "nl-NL",
				"et-EE" => "et-EE",
				"fi-FI" => "fi-FI",
				"fr-FR" => "fr-FR",
				"de-DE" => "de-DE",
				"el-GR" => "en-US",
				"hu-HU" => "hu-HU",
				"it-IT" => "it-IT",
				"lv-LV" => "en-US",
				"mk-MK" => "en-US",
				"nb-NO" => "no-NO",
				"fa-IR" => "fa",
				"pl-PL" => "pl-PL",
				"pt-BR" => "pt-BR",
				"pt-PT" => "pt-PT",
				"ro-RO" => "en-US",
				"ru-RO" => "ru-RU",
				"sl-SL" => "si-SI",
				"es-ES" => "es-ES",
				"sv-SE" => "sv-SE",
				"tr-TR" => "tr-TR",
			);
	?>

	<?  $joomlaVersion = JVersion::isCompatible('1.6.0') ? '1.6' : '1.5';
		if ($joomlaVersion == '1.6') { ?>
			<script src="/media/com_ohanah/js/mootools-locale/Locale.<?=$localeMap["$locale"]?>.Date.js" />
			<script src="/media/com_ohanah/js/mootools-locale/Locale.<?=$localeMap["$locale"]?>.Form.Validator.js" />
			<script>
				Locale.use("<?=$localeMap["$locale"]?>");
			</script>
		<? }
	?>
	<script src="/media/lib_koowa/js/koowa.js" />

	<script type="text/javascript">
		var $jq = jQuery.noConflict(); 

		$jq(function() {
			// prevent double submission of form
	 		// from http://stackoverflow.com/questions/2830542/prevent-double-submission-of-forms-in-jquery
		 	$jq('form').bind('submit',function(e){
		 		if ($jq(".validation-failed").length == 0) { // if there is no validation messages
		 			// if there is no terms and conditions or there is terms and conditions but it's checked, proceed
		 			if ($jq("#termsandconditions").length == 0 || $jq("#termsandconditions").is(":checked")) {
		 				// is there info that data is already submited? 
		 				if ($jq(this).data('submitted') === true) {
				      // Previously submitted - don't submit again
				      e.preventDefault();
				    } else {
				      // Mark it so that the next submit can be ignored
				      $jq(this).data('submitted', true);
				    }
				  // there is terms and conditions checkbox and it's not checked
		 			} else {
		 				alert("<?=JComponentHelper::getParams('com_ohanah')->get('terms_warning')?>");
		 				e.preventDefault();
		 			}
		 		}
		  	});
		});
	</script>	

	<?= $this->renderHelper('module.injector', array('title' => '', 'placeholder' => 'ohanah-event-registration-1', 'position' => $params->get('eventRegistrationModulePosition1'))) ?>
	<?= $this->renderHelper('module.injector', array('title' => '', 'placeholder' => 'ohanah-event-registration-2', 'position' => $params->get('eventRegistrationModulePosition2'))) ?>
	<?= $this->renderHelper('module.injector', array('title' => '', 'placeholder' => 'ohanah-event-registration-3', 'position' => $params->get('eventRegistrationModulePosition3'))) ?>

	<? if (KRequest::get('get.ohanah_event_id', 'int')) : ?>

		<? $event = $this->getService('com://site/ohanah.model.events')->id(KRequest::get('get.ohanah_event_id', 'int'))->getItem(); ?>

		<?= $this->loadIdentifier('com://site/ohanah.view.event.default_header', array('event' => $event, 'params' => $params)); ?>

		<? if (!$event->limit_number_of_attendees or $event->countAttendees() < $event->attendees_limit) : ?>

			<h4><?=$this->getHelper('translator')->translate('OHANAH_REGISTRATION')?></h4>

			<form action="index.php?option=com_ohanah&view=registration" method="post" class="form-validate -koowa-form box" name="adminForm" enctype="multipart/form-data"  id="adminForm">
				<input type="hidden" name="view" value="registration" />

				<span class="fieldTitle"><?=$this->getHelper('translator')->translate('OHANAH_REGISTRATION_NAME');?></span><br/>
				<input type="text" class="text size7 required" name="name" id="name" <? if (!$user->guest) echo 'value="'.$user->name.'"'; ?> />

				<br />
				<div class="mandatory-field field1">
					<input type="text" id='surname' name='surname'>
				</div>

				
				<div class="mandatory-field field2">
					<label for="sex-male">Male</label>
					<input type="checkbox" id="sex-male" name="sex_male" value="1" placeholder="Male" />
				</div>
				
				<div class="mandatory-field field3">
					<label for="sex-female">Female</label>
					<input type="checkbox" id="sex-female" name="sex_female" value="1" placeholder="Female" />
				</div>
				
				<? $active_custom_fields = new JRegistry($event->customfields); ?>

				<? for ($i = 1; $i <= 10; $i++) : ?>
					<? $conditionIsTrue = $active_custom_fields->get('cf'.$i) ?>
					<? $label = $active_custom_fields->get("custom_field_label_".$i) ?>
					<? $mandatory = $active_custom_fields->get('custom_field_label_'.$i.'_mandatory') ?>

					<? if ($conditionIsTrue) : ?>
						<br /><span class="fieldTitle"><?=$label?></span><br/>
						<input type="text" name="custom_field_value_<?=$i?>_person_1" class="customfield text size7 <? if ($mandatory) echo 'required'?>" value="" /><br />
					<? endif; ?>
				<? endfor ?>

				<? if ($event->payment_gateway != 'custom' && $event->allow_only_one_ticket != 1) : ?>

					<? 
					if ($event->limit_number_of_attendees) {
						if (!$event->attendees_limit) {
							$available = 5;
						} else {
							$available = ($event->attendees_limit - $event->countAttendees()); 
							if ($available < 0) $available = 0; 	
						}
					} else {
						$available = 5;
					}
					?>

					<div>
						<table style="width:20%">
							<tr>
								<td>
									<span class="fieldTitle"><?=$this->getHelper('translator')->translate('OHANAH_REGISTRATION_NUMBER_OF_TICKETS');?></span><br/>
									<div class="dropdownWrapper">
										<div class="dropdown size5">
											<select name="number_of_tickets" id="number_of_tickets">
												<option value="1">1</option>
												<? if ($available > 1) : ?>
													<option value="2">2</option>
													<? if ($available > 2) : ?>
														<option value="3">3</option>
														<? if ($available > 3) : ?>
															<option value="4">4</option>
															<? if ($available > 4) : ?>
																<option value="5">5</option>
															<? endif ?>
														<? endif ?>
													<? endif ?>
												<? endif ?>
											</select>
										</div>
									</div>
								</td>
							</tr>
						</table>

																
						<span id="ohanah_registration_fields"></span>


						<script>
					 		var $jq = jQuery.noConflict(); 

							$jq(function() {

									

				  				var oldNumberOfTickets;

							    $jq('#number_of_tickets').focus(function() {
							        oldNumberOfTickets = parseInt(this.value);	
							    });

								$jq('#number_of_tickets').change(function() {
									newNumberOfTickets = parseInt(this.getElement(':selected').text);

									for (i = 2; i<=5; i++) {
										if (i > newNumberOfTickets) {
											if ($('ohanah-registration-container-person-'+i)) {
												$('ohanah-registration-container-person-'+i).destroy();
											}
										} else {
											if (!$('ohanah-registration-container-person-'+i)) {
												var code = '';
												code += ('<div id="ohanah-registration-container-person-'+i+'"><span class="fieldTitle"><?=$this->getHelper('translator')->translate("OHANAH_NAME_OF_ADDITIONAL_PERSON")?> '+(i)+'</span><br/><input type="text" name="field_name_person_'+i+'" class="customfield text size7 required" value="" /><br/>');

												<? for ($i = 1; $i <= 10; $i++) : ?>
													<? $conditionIsTrue = $active_custom_fields->get('cf'.$i) ?>
													<? $label = $active_custom_fields->get("custom_field_label_".$i) ?>
													<? $mandatory = $active_custom_fields->get('custom_field_label_'.$i.'_mandatory') ?>
													
													<? if ($conditionIsTrue) : ?>
														code += ('<span class="fieldTitle"><?=$label?> <?=$this->getHelper('translator')->translate("OHANAH_OF_PERSON")?> '+i+'</span><br/><input type="text" name="custom_field_value_<?=$i?>_person_'+i+'" class="customfield text size7 <? if ($mandatory) echo 'required'?>" /><br />');
													<? endif; ?>
												<? endfor ?>

												code += '</div>';

												$jq('#ohanah_registration_fields').append(code);
											}
										}
									}
								});
							});
						</script>	
					</div>

				<? else : ?>
					<input type="hidden" name="number_of_tickets" value="1" />
				<? endif ?>

				<br />
				<table class="notes">
					<tr>
						<td colspan="2"><span class="fieldTitle"><?=$this->getHelper('translator')->translate('OHANAH_REGISTRATION_NOTES');?></span><br/>	
						<textarea class="description" name="notes" id="notes_textarea" style="border: 1px lightgray solid;"><?=$registration->notes?></textarea>
						</td>
					</tr>
				</table>

				<br />
				<? $token = JSession::getFormToken(); ?>
				<input type="hidden" name="action" id="action" value="add" />
				<input type="hidden" name="ohanah_event_id" id="ohanah_event_id" value="<?=KRequest::get('get.ohanah_event_id', 'int')?>" />
				<input type="hidden" name="Itemid" id="Itemid" value="<?=KRequest::get('get.Itemid', 'int')?>" />
			
				<? 
				if ($event->payment_gateway != 'none' && $event->ticket_cost) $text = 'OHANAH_BUY_TICKETS';
				else $text = 'OHANAH_REGISTER';
				?>
				<? if (JComponentHelper::getParams('com_ohanah')->get('terms_text')) : ?>
					<p><input type="checkbox" name="termsandconditions" value="1" id="termsandconditions"><?=JComponentHelper::getParams('com_ohanah')->get('terms_text')?></p>
				<? endif ?>
				<?= $this->renderHelper('button.button', array('type' => 'input', 'text' => $this->getHelper('translator')->translate($text))); ?>
				

			</form>
		<? else : ?>
			<?=$this->getHelper('translator')->translate('OHANAH_TICKETS_SOLD_OUT')?>
		<? endif ?>
	<? endif ?>
</div>