/*******************************************************************************
 * Copyright notice
 * 
 * (c) 2009 Damian Parpan <damian.parpan@sysinf.ch> 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!
 ******************************************************************************/

function syscodegenCheckLoginAndEmail() {
	var password = $F('pass');
	var language = $F('lang');
	var L = $F('L');
	var username = password;
	var emailState = $F('emailcheck');
	var requestResponse = '';
	var email = '';
	if($('email')) {
		email = $F('email');
	}
	
	var loginStatus = syscodegenCheckLogin(username,password);
	if(! loginStatus) {
		
		// Set emailState to something so that the
		// email will not be checked, and the whole
		// function rerturns false. By returning false,
		// the form will not be sent.
		emailState = 'canNotBeChecked';
	}
		
	
	if(emailState == 'no') {
		// Remove eventuelly shown warning about a wrong login.
		if($('tx-syscodegen-login-not-correct')) {
			$('tx-syscodegen-login-not-correct').remove();
		}
		
		// Check if the user has an email address.
		// If not, show a new input field to provide the email address
		// and return false, so the form is not sent.
		// If an email address could be found, return true to send the form.
		new Ajax.Request('index.php', {
			asynchronous : false,
			method : 'get',
			parameters : {
				lang : language,
				L : L,
				'syscodegen[pass]' : password,
				'syscodegen[user]' : username,
				'syscodegen[func]' : 'hasEmail',
				eID : 'tx_syscodegen_newloginbox_ajax'
			},
			onSuccess : function(response) {
				// set the value of the email field with the response text.
				requestResponse = response.responseText;
				Form.Element.setValue('emailcheck', response.responseText);
			}
		});
		
		if(requestResponse == 'yes' || email != '') {
			// The user already has an email address, or
			// he provides one through the email field,
			// return true to send the form.
			return true;
		} else {
			if(! $('email')) {
				
				new Ajax.Request('index.php', {
					asynchronous : false,
					method : 'get',
					parameters : {
						lang : language,
						L : L,
						'syscodegen[func]' : 'getEmailField',
						eID : 'tx_syscodegen_newloginbox_ajax'
					},
					onSuccess : function(response) {
						requestResponse = response.responseText;
						$('pass').insert({
				       		after:requestResponse
				       	});
						// Sets the emailcheck to yes which means,
						// the system has been checked for an email and
						// the client can now provide one if he wants.
			       		Form.Element.setValue('emailcheck', 'yes');
					}
				});
			}
			return false;
		}
	} else if(emailState == 'yes' || email != '')  {
		// The user already has an email address, or
		// he provides one through the email field,
		// return true to send the form.
		return true;
	} else {
		// Form can not be sent.
		return false;
	}
}


/**
 * Checks if the user login is valid. Returns true if it is, false otherwise.
 * 
 * @param username	string
 * @param password	string
 * @return	boolean		true, if the login is valid, false otherwise.
 */
function syscodegenCheckLogin(username,password) {
	var result = false;
	var language = $F('lang');
	var L = $F('L');
	
	new Ajax.Request('index.php', {
		asynchronous : false,
		method : 'get',
		parameters : {
			lang : language,
			L : L,
			'syscodegen[func]' : 'checkFeLogin',
			'syscodegen[user]' : username,
			'syscodegen[pass]' : password,
			eID : 'tx_syscodegen_newloginbox_ajax'
		},
		onSuccess : function(response) {
			requestResponse = response.responseText;
			if(requestResponse == 'yes') {
				// Login is OK, return true.
				result = true;
			} else {
				// Login NOT OK, show a warning to the user.
				if($('tx-syscodegen-login-not-correct')) {
					// Message already visible; just highlight it.
					$('tx-syscodegen-login-not-correct').highlight({ duration: 0.5 });
				} else {
					// Show wrong password message
					$('pass').insert({
			       		after:requestResponse
			       	});
					result = false;
				}
			}
		}
	});
	
	return result;
}

