var tauxPart = 66;
var tauxEnt = 60;
var donationFrequency, donationFrequency1Month, donationFrequency3Month, paymentTypeCB, paymentTypeCheck, paymentTypePrlvtCB, paymentTypePrlvtRib;

//fonction permettant la fermeture de la popup "envoyer à un ami"
function onloadPopUpGoTo(parent, u) {
	if (u == 'close') {
  	parent.tb_remove();
  }
	else if (u == 'reloadParent') {
		newLoc = parent.location.protocol+"//"+parent.location.hostname+parent.location.pathname;
		parent.location.replace(newLoc);
	}
	else if (u != '') {
		parent.location.replace(u);
	}
}

function calculateReductionFromDonationPart() {
	var donationValue = document.forms['form_cout-reel'].don1.value;
	document.forms['form_cout-reel'].reduction1.value = Math.floor(donationValue * tauxPart / 100)+" €";
	document.forms['form_cout-reel'].depense1.value = Math.floor(donationValue * (100 - tauxPart) / 100)+" €";
}

function calculateReductionFromRealSpendingPart() {
	var spendingValue = document.forms['form_montant-don'].depense1.value;
	var don = spendingValue * 100 / (100 - tauxPart) ;
	document.forms['form_montant-don'].don1.value = Math.floor(parseInt(don*100)/100)+" €";
	document.forms['form_montant-don'].reduction1.value = Math.floor(parseInt((don - spendingValue)*100)/100)+" €";
}

function calculateReductionFromDonationEnt() {
	var donationValue = document.forms['form_cout-reel'].don1.value;
	document.forms['form_cout-reel'].reduction1.value = Math.floor(donationValue * tauxEnt / 100)+" €";
	document.forms['form_cout-reel'].depense1.value = Math.floor(donationValue * (100 - tauxEnt) / 100)+" €";
}

function calculateReductionFromRealSpendingEnt() {
	var spendingValue = document.forms['form_montant-don'].depense1.value;
	var don = spendingValue * 100 / (100 - tauxEnt) ;
	document.forms['form_montant-don'].don1.value = Math.floor(parseInt(don*100)/100)+" €";
	document.forms['form_montant-don'].reduction1.value = Math.floor(parseInt((don - spendingValue)*100)/100)+" €";
}

function calculateReductionFSE(don) {
	if (don <0) {
		don = 0;
	}
	document.forms['form_nouveau_compte'].depPart.value = Math.floor(don * (100 - tauxPart) / 100)+" €";
	document.forms['form_nouveau_compte'].depEntr.value = Math.floor(don * (100 - tauxEnt) / 100)+" €";
}

function amount_changed(idProd) {
	prefix = "nfform_" + idProd;
	if (document.getElementById(prefix) != null) {
		prefix = prefix + ":";
	} else { //cas du bug JSF qui rajoute j_id_1 au nom du form
		prefix = prefix + "j_id_1:";
	}
	selectAmountName = prefix + "amountSelect";
	otherAmountName = prefix + "otherAmount";
	select = document.getElementById(selectAmountName);
	input = document.getElementById(otherAmountName);
	if (select.value == -1) {
		input.style.display = "inline";
	} else {
		input.style.display = "none";
		input.value="";
	}
}

function amount_changed(value, otherId) {
	input = document.getElementById(otherId);
	if (value == -1) {
		input.style.display = "inline";
		input.style.display = 0;
	} else {
		input.style.display = "none";
		input.value=value;
	}
}

function amount_changed3(formId, prodId, value, reference)
{
	input = document.getElementById(formId + ":other_" + reference + "_" + prodId);
	if (input == null) {
		input = document.getElementById(formId + ":other_" + reference + "_" + prodId + "j_id_1");
	}

	if (value == -1) {
		input.style.display = "inline";
	} else {
		input.style.display = "none";
	}
}

function checkChangingPrice(formId, amountId){
	input = document.getElementById(formId + ":" + amountId);
	if (input == null) {
		input = document.getElementById(formId + ":" + amountId + "j_id_1");
	}
	if (isNaN(input.value) ||
			(Number(input.value)<=0)) {
		alert("Le montant est invalide.")
		return false;
	}
	return true;
}

function showChangingPrice(idProd){
	changingPriceDiv = document.getElementById("changingPriceDiv_"+idProd);
	if (changingPriceDiv.style.display == "none") {
		changingPriceDiv.style.display = "inline";
	}else {
		changingPriceDiv.style.display = "none";
	}
}

function checkAmount(selAmountId, selOtherId) {
	selAmount = document.getElementById(selAmountId);
	if ((selAmount.options[selAmount.selectedIndex].value == 0) ||
			((selAmount.options[selAmount.selectedIndex].value == -1) && (document.getElementById(selOtherId).value == ''))) {
		alert("Veuillez sélectionner un montant");
		return false;
	}
	return true;
}

function showCompanyAddress(value, divId) {
	divComp = document.getElementById(divId);
	if (divComp != null) {
		if ("true" == value) {
			divComp.style.display = "block";
		}
		else {
			divComp.style.display = "none";
		}
	}
}

function openPopupImage(image, title, width, height) {
	w = window.open("", "chargement", "width="+width+",height="+height+",location=0,directories=0,menubar=0,toolbar=0,scrollbars=0,resizable=0");
	w.document.write("<html><head><title>"+title+"</title></head>\n");
	w.document.write("<body marginwidth=0 marginheight=0 leftmargin=0 topmargin=0>\n");
	w.document.write("<a href='javascript:window.close();'><img src='"+image+"' border=0 alt='"+title+"'></a>\n");
	w.document.write("</body></html>");
	w.document.close();
}

function enableLink(element, id) {
	if(element != null){
		element.href='javascript:void(0);';
		element.onclick=null;
		element.disabled=true;
		if (document.all) element.style.filter = "alpha(opacity='50')";
		else element.style.setProperty("-moz-opacity", (50 / 100), "");
	}
	return true;
}
//**************************************************************************************
//* Fonction pour remplacer la virgule par un point dans la boite de saisie du montant *
//**************************************************************************************
function handleComas(input, zeroOk, previousValue)
{
	var amount = input.value;
	var begin = amount;
	var i = 0;

	if (amount.length == 0) {
		input.value = '0';
		return true;
	}
	while (i < amount.length)
	{
		if (amount.charAt(i) == ',')
		{
			var begin = amount.substr(0, i);
			begin += ".";
			if ((i + 1) < amount.length)
				begin += amount.substr(i + 1, amount.length);
		}
		i++;
	}
	input.value = begin;
	if (isNaN(begin) ||
			(zeroOk && (Number(begin)<0)) ||
			(!zeroOk && (Number(begin)<=0))) {
		alert("Le montant n'est pas valide.");
		input.value=previousValue;
		return false;
	}
	return true;
}

function setCrypto3(numero)
{
	document.getElementById('crypto').innerHTML = "(3 derniers chiffres au dos de votre carte bancaire, au dessus de la signature)";
}
function setCrypto4(numero)
{
	document.getElementById('crypto').innerHTML = "(4 chiffres au dessus à droite de votre numéro de carte)";
}

