var previousStep = 1;
function participationNextStep(participationStep) {

	document.getElementById('participationSnowtripStep' + previousStep).style.display = "none";
	document.getElementById('participationSnowtripStep' + participationStep).style.display = "block";
	document.getElementById('snowtripBtStep' + previousStep).src = "images/snowtrip/participationBt" + previousStep + ".png";
	document.getElementById('snowtripBtStep' + participationStep).src = "images/snowtrip/participationBt" + participationStep + "_on.png";

	previousStep = participationStep;

	if (participationStep == 2) {

		document.getElementById('participationSnowtripStep3').style.display = "none";
		document.getElementById('snowtripBtStep3').src = "images/snowtrip/participationBt3.png";

		var code = document.getElementById('code');
		var operator = document.getElementsByName('operator');

		for (i = 0; i != operator.length; i++) {
			if (operator[i].checked) {
				operator[i].checked = false;
			}
		}

		code.value = '';
		checkFormToUnlockSubmit();
	}
}

// participation

function activateUserForm(disable) {
	document.getElementById('changeUserInfoBT').style.display = (disable ? "block" : "none");
	document.getElementById('saveUserInfoBT').style.display = (disable ? "none" : "block");
	document.getElementById('phone').disabled = (disable ? true : false);
	document.getElementById('email').disabled = (disable ? true : false);
	document.getElementById('phone').parentNode.style.backgroundImage = (disable ? "none" : "url('images/snowtrip/inputPhoneBk.png')");
	document.getElementById('email').parentNode.style.backgroundImage = (disable ? "none" : "url('images/snowtrip/inputEmailBk.png')");
	document.getElementById('mandatoryFields').innerHTML = '';
}

function checkParticipantInfo() {
	var toReturn = true;
	var message = '';

	if (document.getElementById('phone').value == '' || !isEmailAddr(document.getElementById('email').value) || (!(/[0-9]{9,9}/.test(document.getElementById('phone').value) && document.getElementById('phone').value.length == 9))) {
		if (!(/[0-9]{9,9}/.test(document.getElementById('phone').value) && document.getElementById('phone').value.length == 9)) {
			document.getElementById('mandatoryFields').innerHTML = 'O n&uacute;mero de telem&oacute;vel introduzido n&atilde;o &eacute; v&aacute;lido. Tem que ser composto unicamente por algarismos n&uacute;mericos.';
		}
		else {
			document.getElementById('mandatoryFields').innerHTML = 'Todos os campos s&atilde;o de preenchimento obrigat&oacute;rio';
		}
		toReturn = false;
	}

	if (toReturn) {
		activateUserForm(true);
		checkFormToUnlockSubmit();
		document.getElementById('phoneToSend').value = document.getElementById('phone').value;
		submitAJAXForm('changeParticipantInfo', messageParticipantInfo);
	}
}

function messageParticipantInfo() {
	if (this.readyState == 4) {
		if (this.status == 200) {
			var result = JSON.parse(this.responseText);
			document.getElementById('mandatoryFields').innerHTML = '<u>' + result.message + '</u>';
			document.getElementById('mandatoryFields').style.color = "#14724D";
		}
	}
}

var code = document.getElementById('code');
var operator = document.getElementsByName('operator');

for (i = 0; i != operator.length; i++) {
	if (operator[i].checked) {
		radioChecked = true;
	}
}

function checkFormToUnlockSubmit() {
	var radioChecked = false;
	var phone = document.getElementById('phone');
	var email = document.getElementById('email');
	var code = document.getElementById('code');
	var operator = document.getElementsByName('operator');

	for (i = 0; i != operator.length; i++) {
		if (operator[i].checked) {
			radioChecked = true;
		}
	}

	if (phone.value != '' && (/[0-9]{9,9}/.test(document.getElementById('phone').value) && document.getElementById('phone').value.length == 9) && email.value != '' && code.value != '' && radioChecked) {
		document.getElementById('submeterParticipacaoBt').src = "images/snowtrip/submeterParticipacaoBt_on.png";
		document.getElementById('submeterParticipacaoBt').disabled = false;
	}
	else {
		if (!(/[0-9]{9,9}/.test(document.getElementById('phone').value) && document.getElementById('phone').value.length == 9)) {
			document.getElementById('mandatoryFields').innerHTML = 'O n&uacute;mero de telem&oacute;vel introduzido n&atilde;o &eacute; v&aacute;lido. Tem que ser composto unicamente por algarismos n&uacute;mericos.';
		}
		document.getElementById('submeterParticipacaoBt').src = "images/snowtrip/submeterParticipacaoBt.png";
		document.getElementById('submeterParticipacaoBt').disabled = true;
	}
}

function processMessage() {
	if (this.readyState == 4) {
		if (this.status == 200) {

			var result = JSON.parse(this.responseText);
			var snowtripErrorDiv = document.getElementById('snowtripErrorDiv');
			var titleText = '';
			snowtripErrorDiv.innerHTML = '';

			if ((parseInt(result.errorCode)) == -1) {
				titleText = 'Ocorreu um erro na tua participa&ccedil;&atilde;o';
			}
			else {
				titleText = 'Participa&ccedil;&atilde;o efectuada com sucesso';
			}

			var title = document.createElement('h1');
			title.appendChild(document.createTextNode(htmlEntitiesDecode(titleText)));
			snowtripErrorDiv.appendChild(title);

			var text = document.createElement('p');
			text.appendChild(document.createTextNode(htmlEntitiesDecode(result.message)));
			snowtripErrorDiv.appendChild(text);

			if ((parseInt(result.errorCode)) == -1) {
				snowtripErrorDiv.className = 'snowtripErrorDiv';
			}
			else {
				snowtripErrorDiv.className = 'snowtripSucessDiv';
			}

			participationNextStep(3);
		}
	}
}

// challenge a friend

function activateUserFormChallenge(disable) {
	document.getElementById('changeUserInfoBT').style.display = (disable ? "block" : "none");
	document.getElementById('saveUserInfoBT').style.display = (disable ? "none" : "block");
	document.getElementById('name').disabled = (disable ? true : false);
	document.getElementById('email').disabled = (disable ? true : false);
	document.getElementById('name').parentNode.style.backgroundImage = (disable ? "none" : "url('images/snowtrip/desafiarInputBk.png')");
	document.getElementById('email').parentNode.style.backgroundImage = (disable ? "none" : "url('images/snowtrip/desafiarInputBk.png')");
	document.getElementById('mandatoryFields').innerHTML = '&nbsp;';
}

function checkParticipantInfoChallenge() {
	var toReturn = true;
	var message = '';

	if (document.getElementById('senderName').value == '' || !isEmailAddr(document.getElementById('senderEmail').value)) {
		document.getElementById('mandatoryFields').innerHTML = 'Todos os campos s&atilde;o de preenchimento obrigat&oacute;rio';
		document.getElementById('mandatoryFields').style.color = "#14724D";
		toReturn = false;
	}

	if (toReturn) {
		document.getElementById('senderName').value = document.getElementById('name').value;
		document.getElementById('senderEmail').value = document.getElementById('email').value;
		activateUserFormChallenge(true);
		submitAJAXForm('changeParticipantInfo', messageParticipantInfoChallenge);
	}
}

function messageParticipantInfoChallenge() {
	if (this.readyState == 4) {
		if (this.status == 200) {
			var result = JSON.parse(this.responseText);
			document.getElementById('mandatoryFields').innerHTML = '<u>' + result.message + '</u>';
			document.getElementById('mandatoryFields').style.color = "#14724D";
		}
	}
}

function checkFriendChallengeForm() {
	var toReturn = true;
	var message = '';

	if (document.getElementById('frmName').value == '' || !isEmailAddr(document.getElementById('frmEmail').value)) {
		// trocar imagem
		// document.getElementById('mandatoryFields').innerHTML = 'Todos
		// os campos s&atilde;o de preenchimento obrigat&oacute;rio';
		toReturn = false;
	}

	return toReturn;
}

function checkFriendChallengeFormToUnlockSubmit() {
	var name = document.getElementById('frmName');
	var email = document.getElementById('frmEmail');
	var senderName = document.getElementById('senderName');
	var senderEmail = document.getElementsByName('senderEmail');

	if (name.value != '' && email.value != '' && senderName.value != '' && senderEmail.value != '') {
		document.getElementById('desafiarBt').src = "images/snowtrip/desafiarEnviarBt_on.png";
		document.getElementById('desafiarBt').disabled = false;
	}
	else {
		document.getElementById('desafiarBt').src = "images/snowtrip/desafiarEnviarBt.png";
		document.getElementById('desafiarBt').disabled = true;
	}
}
