// Handles for Marie-Anne subscription and login
//
// @version 	1.0
// @package		marieanne
// @author		Frederic Trudeau <ftrudeau@prospek.ca>
// @author		Sylvain Hovington <sylvain@prospek.ca>
// @copyright   Copyright (c) 2009 Prospek Creation Inc.
// @license		Proprietary

var marieanneTools = {

	registerUrl 			: '/inscription/webservice/register',
	registrationStatusUrl	: '/inscription/webservice/registrationstatus',
	getrendezvoushourUrl 	: '/inscription/webservice/getrendezvoushour',

	/*
	* Init
	*
	* Initialize ze thing.
	*
	* 1) Change HREF attributes on triggers
	* 2) Activate listeners
	*
	* @author		Frederic Trudeau <ftrudeau@prospek.ca>
	* @copyright	Copyright (c) 2009 Prospek Creation Inc.
	* @license		Proprietary
	*/
	init: function() {

		jQuery('p#registrationStatus').fadeTo('fast', 0.1);

		// Get registration status
		if (jQuery("#registrationStatus").length) {
			marieanneTools.getRegistrationStatus();
		}

	},

	/*
	* getRegistrationMessage
	*
	* @author		Frederic Trudeau <ftrudeau@prospek.ca>
	* @copyright	Copyright (c) 2009 Prospek Creation Inc.
	* @license		Proprietary
	*/
	getRegistrationStatus: function() {

		jQuery.ajax ({

			url : this.registrationStatusUrl,
			global : false,
			cache : false,
			dataType : "json",
			type : "POST",
			error : function (XMLHttpRequest, textStatus, errorThrown)
			{
			},
			success : function(data)
			{

				if (jQuery("#triggerSubscribe").length) {
					jQuery("#triggerSubscribe").attr('href', 'javascript:void(0);')
				}

				// display session
				jQuery('h4#registrationSession').text(data['session']).fadeTo('slow', 1);

				// display message
				jQuery('p#registrationStatus').text(data['message']).fadeTo('slow', 1);

				if (data['regactive'] == false) {

					jQuery('input[name=lastname]').attr("disabled", "true");
					jQuery('input[name=firstname]').attr("disabled", "true");
					jQuery('input[name=permanentcode]').attr("disabled", "true");

				} else {

					// Activate listeners
					marieanneTools.activateListeners();

				}
			},
			complete : function() {}

		});

	}, // getRegistrationMessage

	/*
	* activateListeners
	*
	* @author		Frederic Trudeau <ftrudeau@prospek.ca>
	* @copyright	Copyright (c) 2009 Prospek Creation Inc.
	* @license		Proprietary
	*/
	activateListeners: function() {
		if (jQuery("#triggerSubscribe").length) {
			jQuery("#triggerSubscribe").click(function () {
				marieanneTools.doRegister();
			});
		}
	},

	/*
	* Post AJAX request to register
	*
	* @author		Frederic Trudeau <ftrudeau@prospek.ca>
	* @copyright	Copyright (c) 2009 Prospek Creation Inc.
	* @license		Proprietary
	*/
	doRegister: function() {

		jQuery.ajax ({
			url : this.registerUrl,
			global : false,
			cache : false,
			dataType : "json",
			type : "POST",
			data : {
				firstname: jQuery('form#form_inscription_home input[name=firstname]').val(),
				lastname: jQuery('form#form_inscription_home input[name=lastname]').val(),
				permanentcode: jQuery('form#form_inscription_home input[name=permanentcode]').val()
			},
			error : function (XMLHttpRequest, textStatus, errorThrown) {},
			success : function(data) {

				stat = data['stat'];
				message = data['message'];

				// forward to form if register went ok
				if (stat === 'ok') {
					window.location = '/inscription/formulaire';
					return;
				}

				// forward to conditions of age check failed
				if (stat === 'conditions') {
					window.location = '/inscription/conditions';
					return;
				}

				// forward to connexion if registration was already made for current active session and year
				if (stat === 'duplicate') {
					window.location = '/inscription/connexion';
					return;
				}

				// display error message
				if (stat === 'error') {
					jQuery('#errorSubscribe .form_error_text').text(message);
					jQuery('#errorSubscribe').fadeIn();
				}

			},
			complete : function() {}
		});

	},


	/*
	* Post AJAX request to register
	*
	* @author		Sylvain Hovington <sylvain@prospek.ca>
	* @copyright	Copyright (c) 2009 Prospek Creation Inc.
	* @license		Proprietary
	*/
	doGetrendezvoushour: function() {
		jQuery.ajax ({
			url : this.getrendezvoushourUrl,
			global : false,
			cache : false,
			//dataType : "json",
			type : "POST",
			data : {
				date: $('#ipt_idt_rdvday').val()
				//date: jQuery('form#form_rendezvous input[name=rdvday]').val(),
			},
			error : function (XMLHttpRequest, textStatus, errorThrown) {},
			success : function(data) {
				$("#ipt_rdvhour").html(data);
				$("#rdvhour").show();
			},
			complete : function() {}
		});

	}
};

// init
jQuery(function () {
	marieanneTools.init();
});