function sendConfirmationLink(requestSpanID)
{
	var myAjax = new Request.HTML({
		'url': "/ajax/send_confirmation_link.php",
		'method': "get",
		'update': requestSpanID,
		'onRequest': function() {
			$(requestSpanID).set('html', '<img src="' + staticsHost + '/img/loading.gif"/> ' + _("Sending..") );
		}
	});

	myAjax.send();
}

function requestChangeMail(requestSpanID, mailFieldID)
{
	new Request({
		'url': '/ajax/player/',
		'data': {'action': 'changeemail', 'newEmail': $(mailFieldID).value},
		'onRequest': function() {
			$(requestSpanID).set('html', '<img src="' + staticsHost + '/img/loading.gif"/> ' + _("Sending..") );
		},
		'onComplete': function(responseText)
		{
			var response = JSON.decode(responseText);
			if ( response.changed )
			{
				$(requestSpanID).set('html', _("The email address change request has been received. A validation email has been sent to your account current email address. You must click the validation link in this email to make the change active.") );
			}
			else
			{
				$(requestSpanID).set('html', _("Invalid email address or email already in use") );
			}
		}
	}).send();
}

function requestChangePassword(requestSpanID, passID, newpassID, confnewpassID, playerNickname)
{
	var pass = $(passID).value;
	var newpass = $(newpassID).value;
	var confnewpass = $(confnewpassID).value;

	if (
		pass == "" || newpass == "" || confnewpass == ""
		||
		pass == undefined || newpass == undefined || confnewpass == undefined
	)
	{
		uralert( _("You must fill all 3 fields.") );
	}
	else if ( newpass != confnewpass )
	{
		uralert( _("The password and the confirmation password are different!") );
	}
	else if ( PWSecurity(newpass, playerNickname) < 40 )
	{
		uralert( _("Your password appear too easy to find. Please secure it using theses methods for example: alternate numbers and letters, mix cases, use specials characters, use at least 8 characters.") );
	}
	else
	{
		new Request({
			'url': '/ajax/player/',
			'data': {'action': 'changepassword', 'pass': pass, 'newpass': newpass},
			'onRequest': function() {
				$(requestSpanID).set('html', '<img src="' + staticsHost + '/img/loading.gif"/> ' + _("Sending..") );
			},
			'onComplete': function(responseText)
			{
				var response = JSON.decode(responseText);
				if ( response.changed )
				{
					$(requestSpanID).set('html', _("The password change request has been received. A validation email has been sent to your account current email address. You must click the validation link in this email to make the change active.") );
				}
				else
				{
					$(requestSpanID).set('html',  response.errorMsg );
				}
			}
		}).send();
	}
}


function sendPrivateMsg(id_player)
{
	if ( $("messageContent").value != "" )
	{
		var typeCheckboxs = document.getElementsByName('messageType');

		var messageType = 'normal';

		for(var i = 0; i < typeCheckboxs.length; ++i)
		{
			if ( typeCheckboxs[i].checked ) messageType = typeCheckboxs[i].value;
		}

		var url = '/ajax/send_private_message.php';

		var myAjax = new Request({
			'url': url,
			'data': { dest_id: id_player, subject: $("messageSubject").value, message: $("messageContent").value, type: messageType },
			'onRequest': function() {
				//$("messageDiv").style.display = "none";
				$("messageLoading").style.display = "inline";

				$("messageSubject").setProperty('disabled', 'disabled');
				$("messageContent").setProperty('disabled', 'disabled');
				$ES("#generalInformationsContainer .validButton").setProperty('disabled', 'disabled');
			},
			'onComplete': function() {
				//$("messageDiv").style.display = "inline";
				$("messageLoading").style.display = "none";

				if ( this.response.text == 0 )
				{
					uralert( _("Your message has been sent."));
					$("messageSubject").setProperty('value', '');
					$("messageContent").setProperty('value', '');
				}
				else
				{
					uralert( _("There was an error while sending your message.") );
				}

				$("messageSubject").setProperty('disabled', '');
				$("messageContent").setProperty('disabled', '');
				$ES("#generalInformationsContainer .validButton").setProperty('disabled', '');
			}
		});

		myAjax.send();
	}
}

function modifyFlag(flagID, country)
{
	$('infoCountry').value = country;
	$('playerFlag').setStyle('display', 'none');
	getFlagSprite(country, 24, false, false, 'currentFlag').replaces( $('currentFlag') );
}

function addContact(id_player_contact, divID)
{
	URBAN.page.popup = new URPopup( _("Are you sure you want to add this player to your Contact List?"), {
		type: 'confirm',
		chainable: true,
		onValidate: function() {
			var url = "/ajax/contact_add.php";

			new Request({
				'url': url,
				'evalScripts': true,
				'data': {id_player_contact: id_player_contact},
				'onRequest': function () { $(divID).set('html', '<img src="' + staticsHost + '/img/loading.gif" style="vertical-align: middle;"/>'); }
			}).send();
		}
	} );
}

function removeContact(id_player_contact, divID)
{
	URBAN.page.popup = new URPopup( _("Are you sure you want to remove this player from your Contact List?"), {
		type: 'confirm',
		chainable: true,
		onValidate: function() {
			var url = "/ajax/contact_remove.php";

			new Request({
				'url': url,
				'evalScripts': true,
				'data': {id_player_contact: id_player_contact},
				'onRequest': function () { if ( $(divID) ) $(divID).set('html', '<img src="' + staticsHost + '/img/loading.gif" style="vertical-align: middle;"/>'); }
			}).send();
		}
	} );
}

function addToPersonnalBlacklist(id_player_contact, divID)
{
	urconfirm( _("Are you sure you want to add this player to your personnal Blacklist?"), function() {
		new Request({
			'url': '/ajax/player/',
			'data': {'action': 'addtopersonnalblacklist', 'id_player': id_player_contact},
			'onRequest': function (){ if ( $(divID) ) $(divID).set('html', '<img src="' + staticsHost + '/img/loading.gif" style="vertical-align: middle;"/>'); },
			'onComplete': reloadCurrentPage
		}).send();
	} );
}

function removeFromPersonnalBlacklist(id_player_contact, divID)
{
	urconfirm( _("Are you sure you want to remove this player from your personnal Blacklist?"), function() {
		new Request({
			'url': '/ajax/player/',
			'data': {'action': 'removefrompersonnalblacklist', 'id_player': id_player_contact},
			'onRequest': function (){ if ( $(divID) ) $(divID).set('html', '<img src="' + staticsHost + '/img/loading.gif" style="vertical-align: middle;"/>'); },
			'onComplete': reloadCurrentPage
		}).send();
	} );
}

function sendPicture()
{
	if ( $('fileInput').value == "" )
	{
		uralert( _("You must choose a picture file on your computer before clicking on Send.") );
	}
	else
	{
		$('photoForm').submit();
	}
}

function changeNickname (menuPseudoID, pseudoContentID, pseudoSpanID)
{
	var oldPseudoValue = $(menuPseudoID).innerHTML;
	var newPseudoValue = $(pseudoContentID).value;
	if ( oldPseudoValue != newPseudoValue )
	{
		var oldSpanContent = $(pseudoSpanID).innerHTML;

		new Request({
			'url': '/ajax/player/',
			'data': {'action': 'changenickname', 'pseudo': newPseudoValue},
			'onRequest': function (){ $(pseudoSpanID).set('html', '<img src="' + staticsHost + '/img/loading.gif"/> ' + _("Sending..")); },
			'onComplete': function(responseText) {
				var response = JSON.decode(responseText);
				$(pseudoSpanID).set('html', oldSpanContent);
				if ( response.changed )
				{
					$(pseudoContentID).value = newPseudoValue;
					$(menuPseudoID).set('html', newPseudoValue);
					uralert( _("Note: as you nickname has been modified, you must use the new one to log in. Your password is still the same as before.") );
				}
				else
				{
					uralert( response.errorMsg );
				}
			}
		}).send();
	}
}

function previewMenuBG(loadingSpanID, selectFieldName)
{
	var menuBGImageFile = $(selectFieldName).value;
	setMenuBG(loadingSpanID, menuBGImageFile);
}

function setMenuBG (element, loadingSpanID, menuBGImageFile) {
	
	$("playerMenuCustomBG").setStyle("background-image", 'url(' + staticsHost + "/img/v2/ui/menubg/" + menuBGImageFile + ')');
	
	if( element == $('menuChangeSpan').getElement('div.current') )
	{
		element.removeClass('current').addClass('other');
	}
	$(element).addClass('current');
	
	new Request({
		'url': "/ajax/set_preference.php",
		'data': { pref: "menubg", val: menuBGImageFile},
		'onRequest': function () {
			$(loadingSpanID).setStyle("visibility", "visible");
		},
		'onComplete': function (responseText) {
			$(loadingSpanID).setStyle("visibility", "hidden");
		}
	}).send();

}

function action_vote(voteCount, votePrice, playerClintz, id_poll_question, id_poll_answer)
{
	if ( votePrice > playerClintz )
	{
		uralert( _("You do not own enough Clintz to vote.") );
		return;
	}

	urconfirm( getString( _("It's the @0@e time you vote for this poll. This vote will cost you @1@ Clintz. Confirm your vote?"), [voteCount, votePrice]), function() {
		var url = "/ajax/poll_add_vote.php?id_poll_question=" + id_poll_question + "&id_poll_answer=" + id_poll_answer;

		var myAjax = new Request({
			'url': url,
			'method': "get",
			'onRequest': function()
			{
				$("pollDiv").set('html', '<img src="' + staticsHost + '/img/loading.gif" /> ' + _("Sending..") );
			},
			'onComplete': function(responseText)
			{
				var infos = responseText.split(";");
				var time = new Date();

				// Load poll results
				new Request.HTML({
					'url': '/poll/poll_result.php?id_poll=' + infos[0] + '&time=' + time.getMilliseconds(),
					'method': "get",
					'evalScripts': true,
					'update': $("pollDiv")
				}).send();
			}
		});

		myAjax.send();
	} );
}

function sendInvites()
{
	var friendsName = $('friendName0').value + ',' + $('friendName1').value + ',' + $('friendName2').value + ',' + $('friendName3').value + ',' + $('friendName4').value;

	var friendsMail = $('friendEmail0').value + ',' + $('friendEmail1').value + ',' + $('friendEmail2').value + ',' + $('friendEmail3').value + ',' + $('friendEmail4').value;

	if ( friendsMail == ",,,," )
	{
		uralert( _("No email address was entered! Invitations can't be sent...") );
	}
	else
	{
		var myAjax = new Request.HTML({
			'url': "/ajax/send_invitation.php",
			'update': $('ajaxLink'),
			'data': { friendsName: friendsName, friendsMail: friendsMail, customMsg: $('customMsg').value },
			'onLoading': function()
			{
				$('ajaxLink').set('html', '<img src="' + staticsHost + '/img/loading.gif"/> ' + _("Sending..") );
			},
			'onComplete': function()
			{
				resetInvites();
			}
		});

		myAjax.send();
	}
}

function resetInvites()
{
	for (var i = 0; i < 5; ++i)
	{
		$('friendName' + i).value = "";
		$('friendEmail' + i).value = "";
	}
}

function bindInfosForm(formID, resultID)
{
	$(formID).addEvent('submit', function(e) {
		//Prevents the default submit event from loading a new page.
		e.stop();

		//Empty the log and show the spinning indicator.
		var log = $(resultID).empty().set('html', '<img src="' + staticsHost + '/img/loading.gif"/>');

		//Set the options of the form's Request handler.
		this.set('send', { onComplete: function(response) {
			log.empty();
			URBAN.roar.alert( '', _("Your personal data has been successfully saved!"), ''); //imgHtml + item['text'].join("<br/>") + '<br clear="all"/>' );


			/*
			log.empty().set('html', _("Your personal data has been successfully saved!") );
			log.setStyle('opacity', 1);
			log.setStyle('background-color', '#FF0');
			log.fade.delay(1000, log, 0);
			*/
		}});
		//Send the form.
		this.send();
	});
}

function revokeAccess (application_title, application_key, divID)
{
	urconfirm( _("Revoke %s access to your account ?").sprintf(application_title), function() {
		new Request({
			'url': '/ajax/player/',
			'data': {'action': 'revokeAccess', 'application_key': application_key},
			'onRequest': function (){ if ( $(divID) ) $(divID).set('html', '<img src="' + staticsHost + '/img/loading.gif" style="vertical-align: middle;"/>'); },
			'onComplete': function(responseText) {
				var response = JSON.decode(responseText);
				if ( response.revoked )
				{
					$('app_' + application_key).setStyle('text-decoration', 'line-through');
					$('app_' + application_key).getElement('.revokeLink').destroy();
				}
				else
				{
					uralert( _("There was an error while revoking access to %s.").sprintf(application_title) );
				}
			}
		}).send();
	} );
}

function unlinkAccount(externalAccountTypeID, externalAccountTypeName)
{
	urconfirm( _("Unlink your account with %s ?\nNote : after unlinking, you will need to use you Urban Rivals login & password to log into the game.").sprintf(externalAccountTypeName), function() {
		new Request({
			'url': '/ajax/player/',
			'data': {'action': 'unlinkAccount', 'externalAccountTypeID': externalAccountTypeID},
			'onRequest': function (){ $('account_' + externalAccountTypeID).getElement('.unlinkAccount').set('html', '<img src="' + staticsHost + '/img/loading.gif" style="vertical-align: middle;"/>'); },
			'onComplete': function(responseText) {
				var response = JSON.decode(responseText);
				if ( response.unlinked )
				{
					$('account_' + externalAccountTypeID).setStyle('text-decoration', 'line-through');
					$('account_' + externalAccountTypeID).getElement('.unlinkAccount').destroy();
				}
				else
				{
					uralert( _("There was an error while unlink your account with %s.").sprintf(externalAccountTypeName) );
				}
			}
		}).send();
	} );
}