/* Fonction d'affichage d'une fenetre type pop up */
var futurePage;
function PopUp(page,title, width, myHeight)
{
	if (title == "flash") {
		var winPopup = window.open(page, title,'top=200,left=200,height=670,width=1050,toolbar=no,location=no,menubar=no,resizable=yes,scrollbars=no,status=no');
		return false;
	}
	if (width == null)
	width = 1000;
	if (title == "mentionLegales") {
		myHeight = 600;
	} else if (title == "contactezNous") {
		myHeight = 670;
	} else if (title == "lexique") {
		myHeight = 700;
	} else if (title == "simulationProfil") {
		myHeight = 600;
	} else if (title == "googleMap") {
		myHeight = 750;
	} else if (title == "plusProduit") {
		myHeight = 700;
	} else if (title == "etape") {
		myHeight = 600;
	}
	
	var winPopup = window.open(page, title,'top=200,left=200,height=' + myHeight + ',width=' + width + ',toolbar=no,location=no,menubar=no,resizable=yes,scrollbars=no,status=no');
}

function PopUpPDF(page)
{
	futurePage = page
	var winPopup = window.open("/ewf_CGPIWeb/jsp/blank.jsp", 'notice','top=200,left=200,height=800,width=800,toolbar=no,location=no,menubar=no,resizable=yes,scrollbars=no,status=no');
}



function continuerSansCodeApaOrias(element)
{
	var confirmation = document.getElementsByName('confirmerPasDeCode')[0];
	confirmation.value = 1;
	var formulaire = document.getElementsByName('donneesClientForm')[0];
	formulaire.submit();
}

function setActionKey(actionKeyValue) {
	var actionKey = document.getElementsByName('actionKey')[0];
	actionKey.value=actionKeyValue;
	return true;
}

function changeProfil(idProfil, formulaire) {
	var form = document.getElementsByName(formulaire)[0];
	var hProfil = document.getElementsByName('idProfilChoisi')[0];
	hProfil.value = idProfil;
	form.submit();
}

//function initSelectionSupports(form) {
	//addOnClick(form, "tableId");
//}

/* Fonction pour forcer le passage en post lors d'un tri via displaytag */
function addOnClick(form, id){
	var element = document.getElementById(id);
	if( element != null ){
		var links = element.getElementsByTagName("a");
		for (i=0; i < links.length; i++) {
			var link = links[i];

			
			if(link.name != "desec"){
				link.onclick = function(){
					try {
						var target = this.search;
						var toadd = target.substring(1,target.length);

						if (form.action.indexOf("?") == -1){
							form.action += "?" + toadd;
							}else{
							form.action += "&" + toadd;
						}
						form.method.value= "post";
						form.submit();
						return false;
					}
					catch (e) {
						alert('Error : ' + e);
						return false;
					}
				}
			}
		else
			{
				link.onclick = function(){
					var element = document.getElementById(id);
					var trs = element.getElementsByTagName("tr");
					for (i=0; i < trs.length; i++) {
						var tr = trs[i];
						tr.className = tr.className.replace(/\bcheckedline\b/,'');
					}

					var taille = document.forms['selectionSupportForm'].elements.length;
					var element = null;
					for(i=0; i < taille; i++)
					{
						element = document.forms['selectionSupportForm'].elements[i];
						if(element.type == "checkbox")
						{
							if(element.checked)
							{
								element.checked = false;
							}
						}
					}
				}
			}
		}
	}
}


/* Fonction permettant la deselection des supports */
function chkall()
{
	var taille = document.forms['selectionSupportForm'].elements.length;
	var element = null;
	for(i=0; i < taille; i++)
	{
		element = document.forms['selectionSupportForm'].elements[i];
		if(element.type == "checkbox")
		{
			if(element.checked)
			{
				element.checked = false;
			}
		}
	}
}

// Calcul de la repartition pour l'input id et prend en paramètre le type de versement
function calculRepartitionUC(inputElt,typeVersement,idSupport)
{
	var versementInitial = document.getElementById("versementInitialHidden");
	if (versementInitial.value == "") {
		versementInitial.value = "0";
	}
	versementInitial = parseFloat( versementInitial.value.replace(/,/, "."));

	var versementRegulier = null;
	if (document.getElementById("versementRegulierHidden") != null) {
		versementRegulier = document.getElementById("versementRegulierHidden");
		if (versementRegulier.value == "")
			versementRegulier.value = "0";
		versementRegulier = parseFloat(versementRegulier.value.replace(/,/, "."));
	}
	if (inputElt.value == "") {
		inputElt.value = "0";
	}
	var changedMontant = 0;
	if (inputElt != "")
		changedMontant = parseFloat(inputElt.value.replace(/,/,"."));
	if (isNaN(changedMontant))
		changedMontant = 0;
	updateMontant(changedMontant, versementInitial, versementRegulier, typeVersement, idSupport);
	updateTotal(versementInitial, versementRegulier, typeVersement);
	inputElt.value = inputElt.value.replace(/\./,",");
}

function initRepartition() {
	var nbSupports = document.getElementById("nombreDeSupports").value;
	var typeV = "Initial";
	var idSupportLocal = 0;
	for (idSupportLocal = 0; idSupportLocal < nbSupports; idSupportLocal++) {
		var eltPourcent = document.getElementById("support[" + idSupportLocal + "].repartition" + typeV);
		calculRepartitionUC(eltPourcent, typeV, idSupportLocal);
	}
	var fondEuroFlag = document.getElementById("fondsEurosFlag").value;
	if (fondEuroFlag == "true")
		calculFondsEuros(typeV);
	typeV = "Regulier";
	if (document.getElementById("versementRegulierHidden") != null) {
		for (idSupportLocal = 0; idSupportLocal < nbSupports; idSupportLocal++) {
			var eltPourcent = document.getElementById("support[" + idSupportLocal + "].repartition" + typeV);
			calculRepartitionUC(eltPourcent, typeV, idSupportLocal);
		}
		if (fondEuroFlag == "true")
			calculFondsEuros(typeV);
	}
	
}

function calculFondsEuros(type) {
	var fonds = document.getElementById("fondEuroSupport.montant" + type + "Formatted");
	var fondsRep = document.getElementById("fondEuroSupport.repartition" + type);
	if (fondsRep.value == "") {
		fondsRep.value = "0";
	}
	var versementInitial = document.getElementById("versementInitialHidden");
	versementInitial = parseFloat( versementInitial.value.replace(/,/,"."));

	if (document.getElementById("versementRegulierHidden") != null) {
		var versementRegulier = document.getElementById("versementRegulierHidden");
		versementRegulier = parseFloat(versementRegulier.value.replace(/,/, "."));
	}
	var versement = 0;
	if (type == "Initial")
		versement = versementInitial;
	else
		versement = versementRegulier;
	if (isNaN((versement * parseFloat(fondsRep.value.replace(/,/,".")) / 100).toFixed(2)))
		fonds.value="0,00";
	else
		fonds.value = (versement * parseFloat(fondsRep.value.replace(/,/, ".")) / 100).toFixed(2);
	updateTotal(versementInitial, versementRegulier, type);
	fonds.value = fonds.value.replace(/\./, ",");
	fondsRep.value = fondsRep.value.replace(/\./, ",");
}



function updateTotal(vInit, vReg, typeV) {
	var montantTotalElt = document.getElementById("totalMontant" + typeV);
	var pourcentTotalElt = document.getElementById("totalRepartition" + typeV);
	var resteMontantElt = document.getElementById("resteMontant" + typeV);
	var restePourcentElt = document.getElementById("resteRepartition" + typeV);
	
	var hasFondEuro = document.getElementById("fondsEurosFlag").value;
	
	var nbSupports = document.getElementById("nombreDeSupports").value;
	
	var totalPourcent = 0.0;
	var totalMontant = 0.0;
	var idSupportLocal2 = 0
	for (idSupportLocal2 = 0; idSupportLocal2 < nbSupports; idSupportLocal2++) {
		var eltMontant = document.getElementById("support[" + idSupportLocal2 + "].montant" + typeV + "Formatted");
		var eltPourcent = document.getElementById("support[" + idSupportLocal2 + "].repartition" + typeV);
		totalPourcent = totalPourcent + parseFloat(eltPourcent.value.replace(/,/, "."));
		totalMontant = totalMontant + parseFloat(eltMontant.value.replace(/,/, "."));
	}

	if (hasFondEuro == "true") {
		var fondMontant = document.getElementById("fondEuroSupport.montant" + typeV + "Formatted");
		var fondPourcent = document.getElementById("fondEuroSupport.repartition" + typeV);
		totalMontant = totalMontant + parseFloat(fondMontant.value.replace(/,/, "."));
		fondPourcent.value = fondPourcent.value.replace(/\./, ",");
		totalPourcent = totalPourcent + parseFloat(fondPourcent.value.replace(/,/, "."));
	}
	if (isNaN(totalMontant))
		totalMontant = 0;
	montantTotalElt.value = totalMontant.toFixed(2);
	montantTotalElt.value = montantTotalElt.value.replace(/\./, ",");
	if (isNaN(totalPourcent))
		totalPourcent = 0;
	pourcentTotalElt.value = totalPourcent.toFixed(2);
	pourcentTotalElt.value = pourcentTotalElt.value.replace(/\./, ",");
	restePourcentElt.value = (100 - totalPourcent).toFixed(2);
	restePourcentElt.value = restePourcentElt.value.replace(/\./, ",");
	if (typeV == "Initial") {
		resteMontantElt.value = (vInit - totalMontant).toFixed(2);
	} else {
		resteMontantElt.value = (vReg - totalMontant).toFixed(2);
	}
	resteMontantElt.value = resteMontantElt.value.replace(/\./, ",");
}

function updateMontant(nouveauPourcent, vInit, vReg, typeVersement, idSupport) {
	var montantElt = document.getElementById("support[" + idSupport + "].montant" + typeVersement + "Formatted");
	if (typeVersement == "Initial")
		montantElt.value = (vInit * parseFloat(nouveauPourcent) / 100).toFixed(2);
	else
		montantElt.value = (vReg * parseFloat(nouveauPourcent) / 100).toFixed(2);
	montantElt.value = montantElt.value.replace(/\./, ",");
}


function MM_findObj(n, d) { //v4.01
	var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
		d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
		if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
		for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
		if(!x && d.getElementById) x=d.getElementById(n); return x;
	}
	function MM_nbGroup(event, grpName) { //v6.0
		var i,img,nbArr,args=MM_nbGroup.arguments;
		if (event == "init" && args.length > 2) {
			if ((img = MM_findObj(args[2])) != null && !img.MM_init) {
				img.MM_init = true; img.MM_up = args[3]; img.MM_dn = img.src;
				if ((nbArr = document[grpName]) == null) nbArr = document[grpName] = new Array();
				nbArr[nbArr.length] = img;
				for (i=4; i < args.length-1; i+=2) if ((img = MM_findObj(args[i])) != null) {
					if (!img.MM_up) img.MM_up = img.src;
					img.src = img.MM_dn = args[i+1];
					nbArr[nbArr.length] = img;
				} }
			} else if (event == "over") {
				document.MM_nbOver = nbArr = new Array();
				for (i=1; i < args.length-1; i+=3) if ((img = MM_findObj(args[i])) != null) {
					if (!img.MM_up) img.MM_up = img.src;
					img.src = (img.MM_dn && args[i+2]) ? args[i+2] : ((args[i+1])?args[i+1] : img.MM_up);
					nbArr[nbArr.length] = img;
				}
			} else if (event == "out" ) {
				for (i=0; i < document.MM_nbOver.length; i++) { img = document.MM_nbOver[i]; img.src = (img.MM_dn) ? img.MM_dn : img.MM_up; }
			} else if (event == "down") {
				nbArr = document[grpName];
				if (nbArr) for (i=0; i < nbArr.length; i++) { img=nbArr[i]; img.src = img.MM_up; img.MM_dn = 0; }
				document[grpName] = nbArr = new Array();
				for (i=2; i < args.length-1; i+=2) if ((img = MM_findObj(args[i])) != null) {
					if (!img.MM_up) img.MM_up = img.src;
					img.src = img.MM_dn = (args[i+1])? args[i+1] : img.MM_up;
					nbArr[nbArr.length] = img;
				} }
			}

			function MM_preloadImages() { //v3.0
				var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
				var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
				if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
			}

			
			
function createConnection() {			
	var xhr;
	try {
	   xhr = new ActiveXObject("Microsoft.XMLHTTP");    // essayer Internet Explorer 
	}
	catch(e)   // Echec
	{
	  xhr = new XMLHttpRequest()
	}
	return xhr;
}

function verifyOPCVM(elt, event) {
	var con = createConnection();
	con.onreadystatechange  = function()
    { 
         if(con.readyState  == 4)
         {
              if(con.status  == 200) 
                 window.open(elt.href, "_blank", "", false); 
              else {
                 elt.innerHTML = "<img style='margin-right:39px;' src='/ewf_marketing/cgpiweb/static/private/img/pictos/attention.gif' alt='Données indisponibles' title='Données indisponibles'>";
                 elt.href = "#";
                 elt.onclick = function() { return false; };
              }
         }
    };
    con.open("GET", elt.href, true);
    con.send(null);
    return false;
}

function formatNumber(number) {

  var txt=number.toString();
  var right = txt.substring(txt.indexOf("."));
  var result = "";
  txt = txt.substring(0, txt.indexOf("."));
  for (i = txt.length - 1; i >= 0; i--) {
    if ((txt.length - i - 1)% 3 == 0)
      result = txt[i] + " " + result;
    else
    result = txt[i] + result;
  }
  result = result.substring(0, result.length - 1) + right;
  return result;
}