/* /js/signup/common.js *//* UTF8 COOKIE: éà */



/*
	On user submit:

	1) Check username availability (checkUsernameAjaxRequest)
	2) Check whether email already is use, warn if so (checkEmailAjaxRequest)
	3) Create account for real

*/


function accountRegistrationClass(ownerAccount)
{
	var self = this;

	this.ownerAccount = ownerAccount;
	this.signupData = null;
	
	this.listCCAjaxRequest = new kigoAjaxRequest2(
		function(data)
		{
			return self.onListCC(data);
		},
		'ajax/signup/read_cc.php'
	);	

	this.checkUsernameAjaxRequest = new kigoAjaxRequest2(
		function(data)
		{
			return self.onCheckUsername(data);
		},
		'ajax/signup/check_username.php'
	);

	this.checkEmailAjaxRequest = new kigoAjaxRequest2(
		function(data)
		{
			return self.onCheckEmail(data);
		},
		'ajax/signup/check_email.php'
	);

	this.createAccountAjaxRequest = new kigoAjaxRequest2(
		function(data)
		{
			return self.onCreateAccount(data);
		},
		this.ownerAccount ? 'ajax/signup/owner-signup.php' : 'ajax/signup/ra-signup.php' + window.location.search
	);
	
	//
	vkDom.onLoad(function() { self.init(); });
}


accountRegistrationClass.prototype.init = function()
{
	this.formManager = new kigoForm2();

	if(this.ownerAccount)
	{		
		this.formManager.define('OWNER_FIRSTNAME', 		'OWNER_FIRSTNAME', 			kigoForm2.FLAG_WRITE | kigoForm2.FLAG_MANDATORY);
		this.formManager.define('OWNER_LASTNAME', 		'OWNER_LASTNAME', 			kigoForm2.FLAG_WRITE | kigoForm2.FLAG_MANDATORY);
		this.formManager.define('OWNER_CONTACT_INFO', 	'OWNER_CONTACT_INFO', 		kigoForm2.FLAG_WRITE);
	}
	else
	{		
		this.formManager.define('RA_NAME', 				'RA_NAME', 					kigoForm2.FLAG_WRITE | kigoForm2.FLAG_MANDATORY);
		this.formManager.define('RA_CONTACT_FIRSTNAME', 'RA_CONTACT_FIRSTNAME', 	kigoForm2.FLAG_WRITE | kigoForm2.FLAG_MANDATORY);
		this.formManager.define('RA_CONTACT_LASTNAME', 	'RA_CONTACT_LASTNAME', 		kigoForm2.FLAG_WRITE | kigoForm2.FLAG_MANDATORY);
		this.formManager.define('RA_CONTACT_INFO', 		'RA_CONTACT_INFO', 			kigoForm2.FLAG_WRITE);
	}

	this.formManager.define('ACCOUNT_USERNAME', 		'ACCOUNT_USERNAME', 		kigoForm2.FLAG_WRITE | kigoForm2.FLAG_MANDATORY);
	this.formManager.define('ACCOUNT_PASSWORD', 		'ACCOUNT_PASSWORD', 		kigoForm2.FLAG_WRITE | kigoForm2.FLAG_MANDATORY);
	this.formManager.define('ACCOUNT_PASSWORD_REPEAT', 	'ACCOUNT_PASSWORD_REPEAT', 	kigoForm2.FLAG_MANDATORY);
	this.formManager.define('ACCOUNT_EMAIL', 			'ACCOUNT_EMAIL', 			kigoForm2.FLAG_WRITE | kigoForm2.FLAG_MANDATORY);
	this.formManager.define('COUNTRY_ID', 				'COUNTRY_ID', 				kigoForm2.FLAG_WRITE | kigoForm2.FLAG_MANDATORY);
	this.formManager.define('CURRENCY_ID', 				'CURRENCY_ID', 				kigoForm2.FLAG_WRITE | kigoForm2.FLAG_MANDATORY);

	this.listCCAjaxRequest.run();
}

accountRegistrationClass.prototype.onListCC = function(data)
{
	(new kigoSelect2('COUNTRY_ID')).addOptions(data.COUNTRIES, true);
	(new kigoSelect2('CURRENCY_ID')).addOptions(data.CURRENCIES, true);

	vkDom.focus(this.ownerAccount ? 'OWNER_FIRSTNAME' : 'RA_NAME');
	
	return true;
}


accountRegistrationClass.prototype.createAccount = function()
{
	var	missing;

	this.formManager.unmarkMissingAll();
	this.signupData = this.formManager.write();

	missing = this.formManager.missingMandatory(false);	// No need to mark, we'll do it below
		
	// Additional format checks

	if(!kigoVal.USERNAME(this.signupData.ACCOUNT_USERNAME))
		missing.push('ACCOUNT_USERNAME');	

	if(!kigoVal.PASSWORD(this.signupData.ACCOUNT_PASSWORD))
		missing.push('ACCOUNT_PASSWORD');

	if(!kigoVal.EMAIL(this.signupData.ACCOUNT_EMAIL))
		missing.push('ACCOUNT_EMAIL');

	if(missing.length)
	{		
		this.formManager.markMissing(missing);
		this.formManager.missingMandatoryMessage(missing);
		return;
	}
	
	if(this.signupData.ACCOUNT_PASSWORD != (new kigoDom('ACCOUNT_PASSWORD_REPEAT')).domNode().value)
	{
		this.formManager.markMissing(['ACCOUNT_PASSWORD', 'ACCOUNT_PASSWORD_REPEAT']);
		vkPopup.message(
			'Check your password',
				'The repeated password does not match the original.', 
			'warn',
			function() 
			{
				vkDom.select('ACCOUNT_PASSWORD')
			}
		);

		return;
	}

	if(!(new kigoDom('TERMS')).domNode().checked)
	{
		vkPopup.message(
			'Please read and accept Terms of Use',
				'Please read and accept the Terms of Use before proceeding.',
			'info', 
			null
		);	

		return;
	}

	// Okay, proceed to the first step - check username availability

	this.checkUsernameAjaxRequest.run( { 'ACCOUNT_USERNAME' : this.signupData.ACCOUNT_USERNAME } );
}


accountRegistrationClass.prototype.onCheckUsername = function(data)
{
	switch(data)
	{
		case E_OK:
			// Proceed to the second step...
			this.checkEmailAjaxRequest.run(
			{
				'ACCOUNT_EMAIL'		:	this.signupData.ACCOUNT_EMAIL,
				'RA'				:	this.ownerAccount ? false : true,
				'OWNER'				:	this.ownerAccount ? true : false
			} );
			return true;

		case E_CONFLICT:
			this.usernameUnavailableMessage();
			return true;
	}

	return false;
}

accountRegistrationClass.prototype.onCheckEmail = function(data)
{
	var	self = this;

	switch(data)
	{
		case E_OK:
			vkPopup.yesno(
				'Continue ?',
					'Your account is about to be created.\n'+
					'\n'+
					'Please confirm that the following email address is correct:\n'+
					'\n'+
					this.signupData.ACCOUNT_EMAIL+'\n'+
					'\n'+
					'An email will be sent to this address to validate your account. If this address is misspelled, please press "No" and enter the correct address.',
				'ask', 
				function(yesno)
				{
					if(yesno)
						self.createAccountAjaxRequest.run(self.signupData)
				}, 
				true
			);
			return true;

		case E_CONFLICT:

			var	typeLabel = this.ownerAccount ? 'owner' : 'rental agency';

			vkPopup.yesno(
				'Email already in use', 
					'This email address is already used in an existing '+typeLabel+' account.\n'+
					'\n'+
					'If that is your account and you lost your username or password, go to login page - http://'+_hostname+'/login.php - and click on "Forgot password" to recover you username and password.\n'+
					'\n'+
					'Do you still want to create a new '+typeLabel+' account?',
				'ask', 
				function(yesno)
				{
					if(yesno) 
						self.createAccountAjaxRequest.run(self.signupData)
				}, 
				true
			);
			return true;
	}

	return false;
}


accountRegistrationClass.prototype.onCreateAccount = function(data)
{
	switch(data)
	{
		case E_OK:

			var	url = this.ownerAccount ? 'owner/' : 'ra/';

			vkPopup.message(
				'Your account was created',
					'Your account was created.\n'+
					'You may now start using your account.',
				'ok',
				function() 
				{ 
					goTo(url, true);
				}
			);
			return true;

		case E_CONFLICT:
			this.usernameUnavailableMessage();
			return true;
	}

	return false;
}


// May occur in two different steps..
accountRegistrationClass.prototype.usernameUnavailableMessage = function()
{
	vkPopup.message(
		'Username unavailable',
			'The specified account username is not available.\n'+
			'Please select a different username.',
		'warn', 
		function() 
		{
			vkDom.focus('ACCOUNT_USERNAME');
		}
	);
}

