/* /static/js/send2friend.js */ /* UTF8 COOKIE éà */

function	send2friendClass()
{
	this.ajaxUrl = '/fr/ajax/send2friend';
	this.hotel = null;
	this.stay = null;
}

send2friendClass.prototype.openHotel = function(id)
{
	this.hotel = id;
	this.stay = null;
	return this.open();
}


send2friendClass.prototype.openStay = function(id)
{
	this.hotel = null;
	this.stay = id;
	return this.open();
}


send2friendClass.prototype.open = function()
{
	var	el = vkDom.el('SEND2FRIEND_POPUP');
	var	minTop = 440;	// Avoid displaying the map over the flash player

	if(el)
	{
		// Cleanup fields first
		vkDom.el('S2F_NAME').value = '';
		vkDom.el('S2F_EMAIL').value = '';
		vkDom.el('S2F_FRIEND_NAME').value = '';
		vkDom.el('S2F_FRIEND_EMAIL').value = '';

		el.style.left = (Math.max(document.documentElement.scrollLeft, document.body.scrollLeft) + ((document.documentElement.clientWidth-el.clientWidth)>>1))+'px';
		el.style.top = Math.max(minTop, (Math.max(document.documentElement.scrollTop, document.body.scrollTop) + ((document.documentElement.clientHeight-el.clientHeight)>>1)))+'px';

		vkDom.visibility('SEND2FRIEND_POPUP', true);
		vkDom.visibility('S2F_BUTTON', true);

		vkDom.el('S2F_NAME').focus();
	}

	return false;
}

send2friendClass.prototype.close = function()
{
	vkDom.visibility('S2F_BUTTON', false);
	vkDom.visibility('SEND2FRIEND_POPUP', false);
	return false;
}


send2friendClass.prototype.submit = function(id)
{
	var	self = this;

	vkDom.visibility('S2F_BUTTON', false);
	
	//vkDebug.enable();
	getAjax();
	//ajax.debug = true;
	ajax.onLoad = function(status, data)
	{
		if(data != null && typeof(data) == 'number')
		{
			switch(data)
			{
				case 0:	// Okay

					alert('Le message a été envoyé à votre ami(e).');
					self.close();
					break;

				case 1:	// Name missing
					alert('Merci de bien vouloir renseigner votre nom.');
					vkDom.visibility('S2F_BUTTON', true);
					vkDom.focus('S2F_NAME');
					break;

				case 2: // Email missing
					alert('Merci de bien vouloir renseigner votre adresse email.');
					vkDom.visibility('S2F_BUTTON', true);
					vkDom.focus('S2F_EMAIL');
					break;

				case 3: // Incorrect email address
					alert('L\'adresse email saisie n\'est pas valide.\nMerci de bien vouloir saisir votre adresse email.');
					vkDom.visibility('S2F_BUTTON', true);
					vkDom.select('S2F_EMAIL');
					break;

				case 4:	// Friend name missing
					alert('Merci de bien vouloir renseigner le nom de votre ami(e).');
					vkDom.visibility('S2F_BUTTON', true);
					vkDom.focus('S2F_FRIEND_NAME');
					break;

				case 5: // Friend email missing
					alert('Merci de bien vouloir renseigner l\'adresse email de votre ami(e).');
					vkDom.visibility('S2F_BUTTON', true);
					vkDom.focus('S2F_FRIEND_EMAIL');
					break;
	
				case 6: // Incorrect friend email address
					alert('L\'adresse email de votre ami(e) n\'est pas valide.\nMerci de bien vouloir saisir son adresse email.');
					vkDom.visibility('S2F_BUTTON', true);
					vkDom.select('S2F_FRIEND_EMAIL');
					break;

				default:	// Something bad happened
					self.submitProblem();
					break;
			}
		}
		else
			self.submitProblem();
	}
	
	ajax.onTimeout = function()
	{
		self.submitProblem();
	}
	
	ajax.post(
		this.ajaxUrl,
		this.buildPostObject(),
		15000
	);

	return false;
}

send2friendClass.prototype.submitProblem = function()
{
	alert('Un problème inattendu s\'est produit.\nMerci de bien vouloir réessayer ultérieurement.');
	vkDom.visibility('S2F_BUTTON', true);
}


send2friendClass.prototype.buildPostObject = function()
{
	var	po = {};

	po.NAME = vkDom.el('S2F_NAME').value;
	po.EMAIL = vkDom.el('S2F_EMAIL').value;
	po.FRIEND_NAME = vkDom.el('S2F_FRIEND_NAME').value;
	po.FRIEND_EMAIL = vkDom.el('S2F_FRIEND_EMAIL').value;

	if(this.hotel != null)
		po.HOTEL = this.hotel;
	else
		po.STAY = this.stay;

	return po;
}

var	send2friend = new send2friendClass();


