/***************************************************************
* Copyright notice
*
* (c) 2008-2010 Saskia Metzler <saskia@merlin.owl.de>
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/

/*
 * This file provides some JavaScript functions for the seminars front-end
 * editor and the registration form.
 *
 * @package TYPO3
 * @subpackage tx_seminars
 *
 * @author Saskia Metzler <saskia@merlin.owl.de>
 * @author Oliver Klee <typo3-coding@oliverklee.de>
 * @author Niels Pardon <mail@niels-pardon.de>
 */



/**
 * Collects the names from the first/last name field pairs and compiles/inserts
 * them into the human-readable "additional attendees" field and the machine-readable
 * "structured attendees" field.
 */
function compileNamesfereg() {
	var humanReadableField = $("tx_seminarsfereg_pi1_registration_editor_attendees_names");
	var machineReadableField = $("tx_seminarsfereg_pi1_registration_editor_structured_attendees_names");

	if (!humanReadableField || !machineReadableField) {
		 return;
	}

	var firstNames = $$(".tx_seminarsfereg_pi1_registration_editor_first_name");
	var lastNames = $$(".tx_seminarsfereg_pi1_registration_editor_last_name");

	if (firstNames.length != lastNames.length) {
		return;
	}

	var humanReadableNames = "";
	var machineReadableNames = [];
	for (var i = 0; i < firstNames.length; i++) {
		var firstName = firstNames[i].value.strip();
		var lastName = lastNames[i].value.strip();

		if ((firstName.empty()) && (lastName.empty())) {
			continue;
		}

		var fullName = (firstName + " " + lastName).strip();
		if (!humanReadableNames.empty()) {
			humanReadableNames += "\r\n";
		}
		humanReadableNames += fullName;

		machineReadableNames[i] = [firstName, lastName];
	}

	humanReadableField.value = humanReadableNames;
	machineReadableField.value = machineReadableNames.toJSON();
}

/**
 * Restores the separate name fields from the hidden field with the names
 * in a JSON-encoded array.
 */
function restoreSeparateNameFieldsfereg() {
	var machineReadableField = $("tx_seminarsfereg_pi1_registration_editor_structured_attendees_names");

	if (!machineReadableField || machineReadableField.value.empty()
		|| !machineReadableField.value.isJSON()) {
		return;
	}

	var firstNames = $$("#tx_seminarsfereg_pi1_registration_editor_separate_names "
		+ ".tx_seminarsfereg_pi1_registration_editor_first_name");
	var lastNames = $$("#tx_seminarsfereg_pi1_registration_editor_separate_names "
		+ ".tx_seminarsfereg_pi1_registration_editor_last_name");

	if (firstNames.length != lastNames.length) {
		return;
	}

	var allNames = machineReadableField.value.evalJSON(true);
	var numberOfNames = Math.min(firstNames.length, allNames.length);

	for (var i = 0; i < numberOfNames; i++) {
		firstNames[i].value = allNames[i][0];
		lastNames[i].value = allNames[i][1];
	}
}

/**
 * Adds or drops name fields to match the number of selected seats.
 */
function fixNameFieldsNumberfereg() {
	var neededNameLines = getNumberOfNeededNameFieldsfereg();
	var nameLines = $$("#tx_seminarsfereg_pi1_registration_editor_separate_names "
		+ ".tx_seminarsfereg_pi1_registration_editor_name_line");
	if (nameLines.length < neededNameLines) {
		var nameLineTemplate =
			$$("#tx_seminarsfereg_pi1_registration_editor_name_template "
				+".tx_seminarsfereg_pi1_registration_editor_name_line")[0];
		var nameLinesContainer =
			$("tx_seminarsfereg_pi1_registration_editor_separate_names");

		for (var i = nameLines.length; i < neededNameLines; i++) {
			nameLinesContainer.appendChild(nameLineTemplate.cloneNode(true));
		}
	} else if (nameLines.length > neededNameLines) {
		for (var i = nameLines.length; i > neededNameLines; i--) {
			nameLines[i - 1].remove();
		}
	}
}

/**
 * Gets the number of needed name fields.
 *
 * @return integer the number of needed name fields, will be >= 0
 */
function getNumberOfNeededNameFieldsfereg() {
	var seatsSelector = $("tx_seminarsfereg_pi1_registration_editor_seats");
	if (!seatsSelector) {
		return 0;
	}

	var seats = parseInt(seatsSelector.value);

	var myselfSelector
	= $("tx_seminarsfereg_pi1_registration_editor_registered_themselves");	
	var selfSeat;
	if (myselfSelector) {
		selfSeat = myselfSelector.checked ? 1 : 0;
	} else {
		selfSeat = 1;
	}
	return seats - selfSeat;
}


