///*****************************************///
///               LICENCE                   ///
///                                         ///
///	Ce script est la propriété exclusive    ///
/// de cdt.fr - Reproduction INTERDITE      ///
///*****************************************///


var CDTVendgrammage=1000;



function testCss(){
var tn= document.createElement('div');
tn.innerHTML="<div id='_iscss_1'>aaa</div><div id='_iscss_2' style='width:100px;'>aaa</div>";

document.body.appendChild(tn);
ty=document.getElementById('_iscss_1').offsetWidth!=document.getElementById('_iscss_2').offsetWidth;
document.body.removeChild(tn);
return ty;


}


//Contenu en html
//Attention ne pas mettre de ' &agrave; l'int&eacute;rieur mais plutôt " ou \'	
var contenu='<p>'+
'  <input type="hidden" name="m" value="or" id="" />'+
'  <input type="hidden" name="c" value="CtrCours" id="" />'+
'  <input type="hidden" name="a" value="doCalculateurOr" id="" />'+
'  <input type="hidden" name="etatJS" value="actif" id="" />'+
'</p>'+
'<fieldset class="etape1">'+
'<legend>Vous souhaitez</legend>'+
'<p class="checkbox">'+
'  <input name="type" type="radio" value="CDTAchete" id="CDTAchete"/>'+
'  <label for="CDTAchete"><strong>Vendre</strong> des pi&egrave;ces ou un lingot d\'or</label> '+
'  <em>(CDT Ach&egrave;te)</em>'+
'</p><p class="checkbox">  <input name="type" type="radio" value="CDTVend" id="CDTVend" checked="checked" />'+
'  <label for="CDTVend"><strong>Acheter</strong> des pi&egrave;ces ou un lingot d\'or</label> '+
'  <em>(CDT Vend)</em>'+
'</p>'+
'</fieldset>'+
'<fieldset id="fpiece" class="etape2">'+
'<legend>Pi&egrave;ce</legend>'+
'<p class="field">  <label for="spieces_id">Lingot/Pi&egrave;ce </label><select id="spieces_id" name="pieces_id">'+
buildPieces()+'</select></p>'+
'<p id="pquantite" class="field">'+
'  <label for="quantite">Quantite </label>'+
'  <input name="quantite" id="quantite" value="'+qteInit()+'"/>'+
' &nbsp;<span id="infoQuantite" class="libelle"></span> &nbsp;<span id="erreurQuantite" class="libelle"></span> </p>'+
'<p id="pgrammage" class="field">'+
'  <label for="grammage">Grammage </label>'+
'  <input name="grammage" id="grammage" value=""/>'+
' &nbsp;<span id="erreurGrammage" class="libelle"></span></p>'+
'<p id="pqualite" class="field"><label for="qualite">Qualit&eacute; </label>'+
'<select name="qualite" id="qualite">'+
'  <option value="1" selected="selected">Boursable</option>'+
'  <option value="2">Non boursable</option>'+
'  <option value="2">Vous ne savez pas</option>'+
'</select> '+
'</p>'+
'<p id="pjustificatif" class="field"><label for="origine">Justificatif d\'achat</label> '+
'<select name="origine" id="origine">'+
'  <option value="1">Vous en poss&eacute;dez un</option>'+
'  <option value="0" selected="selected">Vous n\'en poss&eacute;dez pas</option>'+
'  <option value="0">Vous ne savez pas</option>'+
'</select>'+
'</p>'+
'</fieldset>'+
'<fieldset id="typeVente" class="etape3">'+
'<legend>Type de vente souhait&eacute;e </legend>'+
'<p class="checkbox">'+
'  <input type="radio" id="typeVente1"  value="typeVente1" name="typeVente" />'+
'  <label for="typeVente1">Une vente au gr&eacute; &agrave; gr&eacute; (vente rapide et ferme)</label>'+
'  &nbsp;<span id="prixtypeVente1" class="libelle"></span></p>'+
'<p id="pvaumieux" class="checkbox">'+
'  <input type="radio" id="typeVente2" value="typeVente2" name="typeVente" />'+
'  <label for="typeVente2">Passer un <strong class="ordre">ordre de vente</strong> au mieux</label>'+
' &nbsp;<span id="prixtypeVente2"  class="libelle"></span></p>'+
'<p id="pvlimite" class="checkbox">'+
'  <input type="radio" id="typeVente3" value="typeVente3" name="typeVente" />'+
'  <label for="typeVente3">Passer un <strong class="ordre">ordre de vente</strong> limit&eacute;</label>'+
' &nbsp;<span id="prixtypeVente3"  class="libelle">Afficher : Le prix du jour pour cette quantit&eacute;</span></p>'+
'</fieldset>'+
'<fieldset id="typeAchat" class="etape3">'+
'<legend>Type d\'achat souhait&eacute; </legend>'+
'<p class="checkbox">'+
'  <input type="radio" id="typeAchat1"  value="typeAchat1"  name="typeAchat" checked="checked"/>'+
'  <label for="typeAchat1">Un <strong class="achat">achat imm&eacute;diat</strong> en ligne ou guichet (vente au gr&eacute; &agrave; gr&eacute;)</label>'+
'  &nbsp;<span id="prixtypeAchat1"  class="libelle"></span></p>'+
'<p class="checkbox">'+
'  <input type="radio" id="typeAchat2" value="aumieux" name="typeAchat" />'+
'  <label for="typeAchat2">Passer un <strong class="ordre">ordre d\'achat</strong> au mieux</label>'+
'&nbsp;<span id="prixtypeAchat2" class="libelle"></span></p>'+
'<p class="checkbox">'+
'  <input type="radio" id="typeAchat3"  value="limite" name="typeAchat" />'+
'  <label for="typeAchat3">Passer un <strong class="ordre">ordre d\'achat</strong> limit&eacute;</label>'+
'&nbsp;<span id="prixtypeAchat3" class="libelle"></span></p>'+
'</fieldset>'+
'<fieldset class="etape4">'+
'<legend>Mode d\'exp&eacute;dition </legend>'+
'<p id="pretrait" class="checkbox">'+
'  <input name="expedition" id="retrait" type="radio" value="retrait" checked="checked"/>'+
'  <label for="retrait">Retrait &agrave; Paris - Obligatoire pour le lingot</label>'+
'</p>'+
'<p id="pdepot" class="checkbox">'+
'  <input name="expedition" id="depot" type="radio"  value="depot" />'+
'  <label for="depot">D&eacute;p&ocirc;t &agrave; Paris - Obligatoire pour les lingots de 1kg, 500g et 250g</label>'+
'  </p>'+
'<p id="pexpedition" class="checkbox">'+
'  <input name="expedition" id="expedition" type="radio" value="expedition"/>'+
'  <label for="expedition"> Vous nous exp&eacute;diez vos pi&egrave;ces (envoi s&eacute;curis&eacute; depuis la France)</label>'+
'  </p>'+
'<p id="penvoi" class="checkbox">'+
'  <input name="expedition" id="envoi" type="radio" value="envoi"  checked="checked" />'+
'  <label for="envoi"> CDT vous envoie vos pi&egrave;ces (envoi s&eacute;curis&eacute; et assur&eacute; pour la France)</label>'+
'  </p>'+
'</fieldset>'+
'<p class="submit"><input value="Etape suivante" type="submit" class="submit" /></p>';	
	

//On stocke les tranches et prix de chaque pi&egrave;ce dans un objet	
var u=new Object();	
//Requête AJAX synchrone
   $.ajax( {
            type: 'GET',
			//Emplacement du fichier
            url: '../../prix.xml',
            dataType: 'xml',
			contentType: 'text/xml',
			async: false,
            success: function(xml,textStatus, XMLHttpRequest)
                     {
						//On parse le r&eacute;sultat
						//pour toutes les pi&egrave;ces
                       $(xml).find('piece').each(
					   function()
					   {
						var id=$(this).find('id').text();
						u[id]=new Object();
						u[id]['achat']=new Object();
						u[id]['vente']=new Object();
						//pour l'achat
						$(this).find('achat').each(
						function()
						{
							var type=$(this).attr('type');
							u[id]['achat'][type]=new Object();
							//quantit&eacute; minimum pour cette pi&egrave;ce
							u[id]['achat'][type]['min']=$(this).children('quantite_min').text();
							//si pas de maximum, on cr&eacute;e une quantit&eacute; maximale factice codifi&eacute;e par *
							if ($(this).children('quantite_max').length==0)
							{
								u[id]['achat'][type]['max']='*';
							}
							else
							{
							//quantit&eacute; maximale pour cette pi&egrave;ce
								u[id]['achat'][type]['max']=$(this).children('quantite_max').text();
							}
							u[id]['achat'][type]['prix']=$(this).children('prix').text();
							
						}
						);
						//même chose mais pour la vente cette fois-ci
						$(this).find('vente').each(
						function()
						{
							var type=$(this).attr('type');
							u[id]['vente'][type]=new Object();
							u[id]['vente'][type]['min']=$(this).children('quantite_min').text();
							if ($(this).children('quantite_max').length==0)
							{
								u[id]['vente'][type]['max']='*';
							}
							else
							{
								u[id]['vente'][type]['max']=$(this).children('quantite_max').text();
							}
							u[id]['vente'][type]['prix']=$(this).children('prix').text();
							
						}
						);
						}
						);
						
						
						
                      },
			error : function(XMLHttpRequest, textStatus, errorThrown)
			{
				//En cas d'erreur....
				alert(textStatus);
				alert(XMLHttpRequest.responseText);
			}
        });
		
// ID des pi&egrave;ces qui ne sont pas vendues par CDT	
// ID des pi&egrave;ces non vendues au gr&eacute; &agrave; gr&eacute;	
var tableauNonRetenue= pasVendues();
var tableauLingots= Lingots();
var idLingot1kg = Lingot1kg();
var idLingot = Lingot1kg();
var lePrix=0;
	
function estEntier(s)
{
	var s=new String(s);
	if (s == '') return false;
	for (i = 0 ; i < s.length ; i++)
	{
		if ((s.charAt(i) < '0') || (s.charAt(i) > '9'))
		{
			return false;
		}
	}
	return true;
}


//Determine le prix de vente
//Affichage message d'erreurs et indications sur la quantit&eacute; minimale de pi&egrave;ce
function prixCDTVend()
{
	var lePrix=0;
	var tranche='';
	var temp='prixCDTVend';
	var piece=$('#spieces_id').attr('value');
	var valeur=$('#quantite').val();
	var grammage=$('#grammage').val();
	var booleen1=true;
	var booleen2=true;
	var min1=1;
	var min2=1;

	
	if (estEntier(valeur))
	{
		valeur=parseInt(valeur,10);
		
		temp='prixCDTVendOrdre';
			for(i=1;i<=3;i++)
			{
				tranche=temp+i;
				if (u[piece]['achat'][tranche]!=null)
				{
				temp1=u[piece]['achat'][tranche]['min'];
				temp2=u[piece]['achat'][tranche]['max'];
				//alert(temp1+":"+temp2);
				if (temp2=='*')
				{
					temp1=parseInt(temp1,10);
					if (valeur>=temp1)
					{
						lePrix=u[piece]['achat'][tranche]['prix'];
						break;
					}
				}
				else
				{
					temp2=parseInt(temp2,10);
						temp1=parseInt(temp1,10);
						if ((valeur>=temp1) && (valeur<=temp2))
						{
							lePrix=u[piece]['achat'][tranche]['prix'];
							break;
						}
				}
				}
			}
			//Les messages indiquant le minimum pour une vente ferme et sur ordre

			//Si on a pas trouv&eacute; de tranche ad&eacute;quate, alors lePrix=0
			if (lePrix==0)
			{
				booleen1=false;
				$('#prixtypeAchat2').html('');
				$('#prixtypeAchat3').html('');
				$('#typeAchat3').removeAttr('checked');
				$('#typeAchat3').attr('disabled','disabled');
				$('#typeAchat2').removeAttr('checked');
				$('#typeAchat2').attr('disabled','disabled');
				//alert('erreur');
			}
			else
			{
				$('#typeAchat2').attr('checked','checked');
				$('#typeAchat2').removeAttr('disabled');
				$('#typeAchat3').removeAttr('disabled');
				if ((piece==idLingot1kg) && (estEntier(grammage)))
				{
					//alert('prica'+grammage/1000);
					lePrix=lePrix*grammage/1000;
				}
				$('#prixtypeAchat2').html('<span>Notre prix : '+lePrix+' Euros</span>');
				$('#prixtypeAchat3').html('<span>Notre prix : '+lePrix+' Euros</span>');
				//alert('ca va '+lePrix);
			}
			lePrix=0;
			temp='prixCDTVendFerme';
			for(i=1;i<=3;i++)
			{
				tranche=temp+i;
				if (u[piece]['achat'][tranche]!=null)
				{
				temp1=u[piece]['achat'][tranche]['min'];
				temp2=u[piece]['achat'][tranche]['max'];
				//alert(temp1+":"+temp2);
				if (temp2=='*')
				{
					temp1=parseInt(temp1,10);
					if (valeur>=temp1)
					{
						lePrix=u[piece]['achat'][tranche]['prix'];
						break;
					}
				}
				else
				{
					temp2=parseInt(temp2,10);
						temp1=parseInt(temp1,10);
						if ((valeur>=temp1) && (valeur<=temp2))
						{
							lePrix=u[piece]['achat'][tranche]['prix'];
							break;
						}
				}
				}
			}
			//Pi&egrave;ces non vendues au gr&eacute; &agrave; gr&eacute;
			if (tableauNonRetenue.in_array($('#spieces_id').attr('value')))
			{
				$('#prixtypeAchat1').html('<span class="noPrice">Pas de vente au gr&eacute; &agrave; gr&eacute; aujourd\'hui</span>');
				$('#typeAchat1').removeAttr('checked');
				$('#typeAchat1').attr('disabled','disabled');
				$('#typeAchat2').attr('checked','checked');
				booleen2=false;
			}
			else
			{
				//alert(lePrix);
				//Si on a pas trouv&eacute; de tranche ad&eacute;quate, alors lePrix=0
				if (lePrix==0)
				{
					booleen2=false;
					$('#prixtypeAchat1').html('');
					$('#typeAchat1').removeAttr('checked');
					$('#typeAchat1').attr('disabled','disabled');
					$('#typeAchat2').attr('checked','checked');
					//alert('erreur');
				}
				else
				{
				//Cas particulier de l'or
				if ((piece==idLingot1kg) && (estEntier(grammage)))
				{
					//alert('prica'+grammage/1000);
					lePrix=lePrix*grammage/1000;
				}
					$('#prixtypeAchat1').html('<span>Notre prix : '+lePrix+' Euros</span>');
					$('#typeAchat1').attr('checked');
					//alert('ca va '+lePrix);
				}
			}
			

	}
	else
	{
		booleen1=false;
		booleen2=false;
		$('#prixtypeAchat2').html('');
		$('#prixtypeAchat3').html('');
		$('#prixtypeAchat1').html('');
		$('#typeAchat3').removeAttr('checked');
		$('#typeAchat3').attr('disabled','disabled');
		$('#typeAchat2').removeAttr('checked');
		$('#typeAchat2').attr('disabled','disabled');
		$('#typeAchat1').removeAttr('checked');
		$('#typeAchat1').attr('disabled','disabled');
		
		$('#erreurQuantite').html('<span>La quantit&eacute; n\'est pas un entier</span>');
	}
	
			var message='';
			if (u[piece]['achat']['prixCDTVendOrdre1']!=null)
			{
				min1=parseInt(u[piece]['achat']['prixCDTVendOrdre1']['min']);
				//message+='Ordre : '+min1;
				message+='<span>Quantit&eacute; minimum : '+min1+'</span>';
			}
			if (u[piece]['achat']['prixCDTVendFerme1']!=null)
			{
				min2=parseInt(u[piece]['achat']['prixCDTVendFerme1']['min']);
				//message+=' Gr&eacute; &agrave; gr&eacute; : '+min2;// Je la d&eacute;sactive le 12/05/2010 car les deux quantit&eacute;s sont les mêmes sur un même type de transaction et/ou de qualit&eacute;
			}
			$('#infoQuantite').html(message);
			if ((booleen1==false) && (booleen2==false))
			{
				message='<span>V&eacute;rifiez la quantit&eacute; (min. = '+Math.min(min1,min2)+')</span>';
				$('#erreurQuantite').html(message);
			}
			else
			{
				$('#erreurQuantite').html('');
			}
			
}


//Determine le prix de vente
//Affichage message d'erreurs et indications sur la quantit&eacute; minimale de pi&egrave;ce
function prixCDTAchete()
{
	var lePrix=0;
	var tranche='';
	var temp='prixCDTAchete';
	var boursable='';
	var grammage=$('#grammage').val();
	var booleen1=true;
	var booleen2=true;
	var min1=1;
	var min2=1;
	
	if ($('#qualite').val()=='1')
	{
		boursable='Boursable';
	}
	else
	{
		boursable='NonBoursable';
	}
	var piece=$('#spieces_id').attr('value');
	var valeur=$('#quantite').val();
	if (estEntier(valeur))
	{
		valeur=parseInt(valeur,10);
		temp='prixCDTAchete';
		temp=temp+'Ordre';
		
			for(i=1;i<=3;i++)
			{
				tranche=temp+i;
				if (u[piece]['vente'][tranche]!=null)
				{
				temp1=u[piece]['vente'][tranche]['min'];
				temp2=u[piece]['vente'][tranche]['max'];
				//alert(temp1+":"+temp2);
				if (temp2=='*')
				{
					temp1=parseInt(temp1,10);
					if (valeur>=temp1)
					{
						lePrix=u[piece]['vente'][tranche]['prix'];
						break;
					}
				}
				else
				{
					temp2=parseInt(temp2,10);
						temp1=parseInt(temp1,10);
						if ((valeur>=temp1) && (valeur<=temp2))
						{
							lePrix=u[piece]['vente'][tranche]['prix'];
							break;
						}
				}
				}
			}
			//Si on a pas trouv&eacute; de tranche ad&eacute;quate, alors lePrix=0
			if (lePrix==0)
			{
				booleen1=false;
				$('#prixtypeVente2').html('');
				$('#prixtypeVente3').html('');
				$('#typeVente1').attr('checked','checked');
				$('#typeVente2').removeAttr('checked');
				$('#typeVente2').attr('disabled','disabled');
				$('#typeVente3').removeAttr('checked');
				$('#typeVente3').attr('disabled','disabled');
				if (boursable!='Boursable')
				{
					$('#prixtypeVente2').html('<span class="noPrice">Uniquement pour les pi&egrave;ces boursables</span>');
					$('#prixtypeVente3').html('<span class="noPrice">Uniquement pour les pi&egrave;ces boursables</span>');
				}
				
				//alert('erreur');
			}
			else
			{
				if ((piece==idLingot1kg) && (estEntier(grammage)))
				{
					//alert('prica'+grammage/1000);
					lePrix=lePrix*grammage/1000;
				}
				if (boursable=='Boursable')
				{
					$('#typeVente2').removeAttr('disabled');
					$('#typeVente2').attr('checked','checked');
					$('#typeVente3').removeAttr('disabled');
					$('#prixtypeVente2').html('<span>Notre prix : '+lePrix+' Euros</span>');
					$('#prixtypeVente3').html('<span>Notre prix : '+lePrix+' Euros</span>');
				}
				else
				{
					$('#typeVente1').attr('checked','checked');
					$('#typeVente2').attr('disabled','disabled');
					$('#typeVente3').attr('disabled','disabled');
					$('#prixtypeVente2').html('<span class="noPrice">Uniquement pour les pi&egrave;ces boursables</span>');
					$('#prixtypeVente3').html('<span class="noPrice">Uniquement pour les pi&egrave;ces boursables</span>');
				}
				//alert('ca va '+lePrix);
			}
			
		
		lePrix=0;
		temp='prixCDTAchete';
		temp=temp+boursable+'Ferme'		
			
			for(i=1;i<=3;i++)
			{
				tranche=temp+i;
				if (u[piece]['vente'][tranche]!=null)
				{
				temp1=u[piece]['vente'][tranche]['min'];
				temp2=u[piece]['vente'][tranche]['max'];
				//alert(temp1+":"+temp2);
				if (temp2=='*')
				{
					temp1=parseInt(temp1,10);
					if (valeur>=temp1)
					{
						lePrix=u[piece]['vente'][tranche]['prix'];
						break;
					}
				}
				else
				{
					temp2=parseInt(temp2,10);
						temp1=parseInt(temp1,10);
						if ((valeur>=temp1) && (valeur<=temp2))
						{
							lePrix=u[piece]['vente'][tranche]['prix'];
							break;
						}
				}
				}
			}
			//Si on a pas trouv&eacute; de tranche ad&eacute;quate, alors lePrix=0
			if (lePrix==0)
			{
				booleen2=false;
				$('#prixtypeVente1').html('');

				//$('#typeVente1').removeAttr('checked');
				$('#typeVente1').attr('disabled','disabled');
				//alert('erreur');
			}
			else
			{
				//Cas particulier de l'or
				if ((piece==idLingot1kg) && (estEntier(grammage)))
				{
					//alert('prica'+grammage/1000);
					lePrix=lePrix*grammage/1000;
				}
				$('#prixtypeVente1').html('<span>Notre prix : '+lePrix+' Euros</span>');
				//alert('ca va '+lePrix);
				$('#typeVente1').removeAttr('disabled');
			}
			
			
	}
	else
	{
			$('#prixtypeVente1').html('');
			$('#prixtypeVente2').html('');
			$('#prixtypeVente3').html('');
			$('#typeVente3').removeAttr('checked');
			$('#typeVente3').attr('disabled','disabled');
			$('#typeVente2').removeAttr('checked');
			$('#typeVente2').attr('disabled','disabled');
			
			booleen1=false;
			booleen1=false;
			
			$('#erreurQuantite').html('<span>La quantit&eacute; n\'est pas un entier</span>');
			
	}
	//On affiche pas la qualit&eacute; si piece  = or
	//Cependant, on positionne la qualit&eacute; sur Boursable
	//if (piece==idLingot)
	if (tableauLingots.in_array(piece))
	{
		$('#pqualite').hide('slow');
		$('#qualite')[0].selectedIndex=0;
	}
	//alert(booleen1+':'+booleen2);
	temp='prixCDTAchete';
		temp=temp+boursable+'Ferme'		//Les messages indiquant le minimum pour un achat ferme et sur ordre
		var message='';
			if ((u[piece]['vente']['prixCDTAcheteOrdre1']!=null) && (boursable=='Boursable'))
			{
				min1=parseInt(u[piece]['vente']['prixCDTAcheteOrdre1']['min']);
				//message+=' Ordre : '+min1;
				message+=' <span>Quantit&eacute; minimum : '+min1+'</span>';
				
			}
			if (u[piece]['vente'][temp+1]!=null)
			{
				min2=parseInt(u[piece]['vente'][temp+1]['min']);
				//message+=' Gr&eacute; &agrave; gr&eacute; : '+min2; // Je la d&eacute;sactive le 12/05/2010 car les deux quantit&eacute;s sont les mêmes sur un même type de transaction et/ou de qualit&eacute;
			}
			$('#infoQuantite').html(message);
			temp=Math.min(min1,min2);
			if ((estEntier(valeur)==false) || ((estEntier(valeur)==true) && (valeur<temp)))
			{
				message='<span>V&eacute;rifiez la quantit&eacute; (min. = '+temp+')</span>';
				$('#erreurQuantite').html(message);
			}
			else
			{
				$('#erreurQuantite').html('');
			}
	
}

//Pour savoir si une valeur donn&eacute;e se trouve dans un tableau donn&eacute;
Array.prototype.in_array = function(p_val) {
	for(var i = 0, l = this.length; i < l; i++) {
		if(this[i] == p_val) {
			return true;
		}
	}
	return false;
}


//G&egrave;re les MAJ des quantit&eacute;s et du grammage		
function handleFormChanged(objet)
		{
		var temp=$(objet).val();
		//alert(temp);
			if (estEntier(temp))
			{
				var temp=$('input[name=\'type\']:checked').attr('value');
				var or=$('#spieces_id').val();
				if (temp=='CDTVend')
				{
					$('#typeVente').hide('slow');
					$('#typeVente1').removeAttr('checked');
					$('#typeVente2').removeAttr('checked');
					$('#typeVente3').removeAttr('checked');
					var gramme=$('#grammage').val();
					if ((or==idLingot1kg) && (gramme!=''))
					{
						if ((estEntier(gramme)) && (estEntier($('#quantite').val())))
						{
							//alert('la');
							$('#typeAchat').show('slow');
							$('#typeAchat1').removeAttr('disabled');
						}
						else
						{
							//alert('ici');
						$('#typeAchat').hide('slow');
						$('#typeAchat2').removeAttr('checked');
						$('#typeAchat3').removeAttr('checked');
						$('#typeAchat1').removeAttr('checked');
						}
					}
					else
					{
						if (or!=idLingot)
						{
							$('#typeAchat').show('slow');
							$('#typeAchat1').removeAttr('disabled');
						}
						else
						{
							$('#typeAchat').hide('slow');
							$('#typeAchat2').removeAttr('checked');
							$('#typeAchat3').removeAttr('checked');
							$('#typeAchat1').removeAttr('checked');
						}
					}
				}
				else
				{
					$('#typeAchat').hide('slow');
					$('#typeAchat2').removeAttr('checked');
						$('#typeAchat3').removeAttr('checked');
						$('#typeAchat1').removeAttr('checked');
					$('#typeVente').show('slow');
					if ($('#qualite').val()=='1')
					{
						$('#typeVente2').removeAttr('disabled');
						$('#typeVente2').attr('checked','checked');
						$('#typeVente3').removeAttr('disabled');
					}
					else
					{
						$('#typeVente2').attr('disabled','disabled');
						$('#typeVente3').attr('disabled','disabled');
						$('#typeVente2').removeAttr('checked');
						$('#typeVente3').removeAttr('checked');
					}
				}
				//return true;
			}
			else
			{
				$('#typeVente').hide('slow');
				$('#typeVente1').removeAttr('checked');
				$('#typeVente2').removeAttr('checked');
				$('#typeVente3').removeAttr('checked');
				$('#typeAchat').hide('slow');
				$('#typeAchat2').removeAttr('checked');
				$('#typeAchat3').removeAttr('checked');
				$('#typeAchat1').removeAttr('checked');
				//return false;
			}
		achete=$('input[name=\'type\']:checked').attr('value');
		 if (achete=='CDTVend')
		{
			prixCDTVend();
		}
		else
		{
			prixCDTAchete();
		}
		}

//Quand le document est charg&eacute;...
$(document).ready(function(){
	$('#monformulaire').append(contenu);
	$('#pqualite').hide();
	$('#qualite')[0].selectedIndex=1;
	$('#pgrammage').hide();
	$('#pjustificatif').hide();
	$('#typeVente').hide();
	$('#typeAchat').hide();
	$('#pexpedition').hide();
	$('#pdepot').hide();
	$('#typeAchat1').attr('disabled','disabled');
	
	$('#typeVente2').attr('disabled','disabled');
	$('#typeVente3').attr('disabled','disabled');
	$('#typeVente2').removeAttr('checked');
	$('#typeVente3').removeAttr('checked');
	 
	 //A chaque MAJ de grammage, on appelle handleFormChanged
	 $('#grammage').bind('change paste keyup', function() {
          handleFormChanged('#grammage');
		  		
		 
     });
	 
	 //A chaque MAJ de quantite, on appelle handleFormChanged
	 	 $('#quantite').bind('change paste keyup', function() {
          handleFormChanged('#quantite');
		

     });


		
	 //A chaque MAJ de la qualit&eacute;...
	$('#qualite').change(function() {
	if ($(this).val()=='Boursable')
			{
				$('#typeVente2').removeAttr('disabled');
				$('#typeVente2').attr('checked','checked');
				$('#typeVente3').removeAttr('disabled');
			}
			else
			{
				$('#typeVente1').attr('checked','checked');
				$('#typeVente2').attr('disabled','disabled');
				$('#typeVente3').attr('disabled','disabled');
				$('#typeVente2').removeAttr('checked');
				$('#typeVente3').removeAttr('checked');
			}
			achete=$('input[name=\'type\']:checked').attr('value');
		 if (achete=='CDTVend')
		{
			prixCDTVend();
		}
		else
		{
			prixCDTAchete();
		}
	});
	
	//A chaque changement de pi&egrave;ce...
	//$('#spieces_id').change(function() {
	$('#spieces_id').bind('change keypress',function()  {
	
	var achete=$('input[name=\'type\']:checked').attr('value');
	var temp=$(this).attr('value');		
		if (temp!=idLingot)
		{
			$('#pquantite').show('slow');
			$('#grammage').attr('value','');
			$('#pgrammage').hide('slow');
			if (achete=='CDTVend')
			{
				$('#pqualite').hide('slow');
				$('#qualite')[0].selectedIndex=0;
			}
			else
			{
				$('#pqualite').show('slow');
			}
		}
		else
		{
			if (achete=='CDTVend')
			{
				$('#pquantite').show('slow');
				$('#grammage').attr('value',CDTVendgrammage);
				$('#pgrammage').hide('slow');
				$('#pqualite').hide('slow');
				$('#qualite')[0].selectedIndex=0;
				//alert('ici');
			}
			else
			{
				$('#pquantite').hide('slow');
				$('#quantite').attr('value','1');
				//TODO
				$('#grammage').attr('value','');
				$('#pgrammage').show('slow');
				$('#pqualite').show('slow');
				//alert('ica');
			}
		}
		
	
	if (tableauNonRetenue.in_array(temp))
		{
			$('#typeAchat1').removeAttr('checked');
			$('#typeAchat1').attr('disabled','disabled');
			//$('#typeVente1').removeAttr('checked');
			//$('#typeVente1').attr('disabled','disabled');
		}
		else
		{
			$('#typeAchat1').removeAttr('disabled');
			//$('#typeVente1').removeAttr('disabled');
		}
		
		//alert(achete);
	if ((temp==idLingot) && (achete=='CDTAchete'))
		{//alert('842');
			$('#pdepot').show('slow');
			$('#pexpedition').show('slow');
			$('#penvoi').hide('slow');
			$('#pretrait').hide('slow');

			$('#depot').attr('checked','checked');
			$('#expedition').removeAttr('checked');
			$('#expedition').attr('disabled','disabled');
			$('#retrait').removeAttr('checked');
			$('#envoi').removeAttr('checked');
		}
		else if ((temp==idLingot) && (achete=='CDTVend'))
		{//alert('855');
			$('#pretrait').show('slow');
			$('#penvoi').show('slow');
			$('#pexpedition').hide('slow');
			$('#pdepot').hide('slow');

			$('#retrait').attr('checked','checked');
			$('#envoi').removeAttr('checked');
			$('#envoi').attr('disabled','disabled');
			$('#depot').removeAttr('checked');
			$('#expedition').removeAttr('checked');
		}
		 //Ce n'et pas un lingot
		else
		{
			if (achete=='CDTVend')
			{//alert('871');
				$('#pretrait').show('slow');
				$('#penvoi').show('slow');
				$('#pexpedition').hide('slow');
				$('#pdepot').hide('slow');
	
				$('#envoi').attr('checked','checked');
				$('#envoi').removeAttr('disabled');
				$('#depot').removeAttr('checked');
				$('#expedition').removeAttr('checked');

			}
			else
			{//alert('884');
				$('#pdepot').show('slow');
				$('#pexpedition').show('slow');
				$('#penvoi').hide('slow');
				$('#pretrait').hide('slow');
	
				$('#expedition').removeAttr('disabled');
				$('#expedition').attr('checked','checked');
				$('#retrait').removeAttr('checked');
				$('#envoi').removeAttr('checked');			
			}
		}
		
	if (jQuery.trim($('#quantite').val())!='')
	{
		handleFormChanged('#quantite');
	}
	else
	{
		$('#typeVente').hide('slow');
		$('#typeVente1').removeAttr('checked');
		$('#typeVente2').removeAttr('checked');
		$('#typeVente3').removeAttr('checked');
		$('#typeAchat').hide('slow');
		$('#typeAchat2').removeAttr('checked');
		$('#typeAchat3').removeAttr('checked');
		$('#typeAchat1').removeAttr('checked');
	}
	if (jQuery.trim($('#grammage').val())!='')
	{
		handleFormChanged('#grammage');
	}
	else
	{
		if (temp==idLingot)
		{
		$('#typeVente').hide('slow');
		$('#typeVente1').removeAttr('checked');
		$('#typeVente2').removeAttr('checked');
		$('#typeVente3').removeAttr('checked');
		$('#typeAchat').hide('slow');
		$('#typeAchat2').removeAttr('checked');
		$('#typeAchat3').removeAttr('checked');
		$('#typeAchat1').removeAttr('checked');
			handleFormChanged('#grammage');
		}
	}
	
		 if (achete=='CDTVend')
		{
			prixCDTVend();
		}
		else
		{
			prixCDTAchete();
		}
		
	});
	
	////A chaque changement de type (vente ou achat)...
	$('input[name=\'type\']').click(function() {
	var temp=$(this).attr('value');
	//alert(temp);
	
	if ($('#spieces_id').attr('value')!=idLingot1kg)
		{
			$('#pquantite').show('slow');
			$('#grammage').attr('value','');
			$('#pgrammage').hide('slow');
		}
		else
		{
			if (temp=='CDTVend')
			{
				$('#pquantite').show('slow');
				$('#pgrammage').hide('slow');
				$('#grammage').attr('value',CDTVendgrammage);
				//$('#pgrammage').show('slow');
			}
			else
			{
				$('#pquantite').hide('slow');
				$('#quantite').attr('value','1');
				$('#grammage').attr('value','');
				$('#pgrammage').show('slow');
			}
		}
	
	//alert('quantite:'+$('#quantite').val());
	if (jQuery.trim($('#quantite').val())!='')
	{
		handleFormChanged('#quantite');
	}
	else
	{
		$('#typeVente').hide('slow');
		$('#typeVente1').removeAttr('checked');
		$('#typeVente2').removeAttr('checked');
		$('#typeVente3').removeAttr('checked');
		$('#typeAchat').hide('slow');
		$('#typeAchat2').removeAttr('checked');
		$('#typeAchat3').removeAttr('checked');
		$('#typeAchat1').removeAttr('checked');
	}
	//alert('quantite:'+$('#grammage').val());
	if (jQuery.trim($('#grammage').val())!='')
	{
		handleFormChanged('#grammage');
	}
	else
	{
		if ($('#spieces_id').attr('value')==idLingot)
		{
		$('#typeVente').hide('slow');
		$('#typeVente1').removeAttr('checked');
		$('#typeVente2').removeAttr('checked');
		$('#typeVente3').removeAttr('checked');
		$('#typeAchat').hide('slow');
		$('#typeAchat2').removeAttr('checked');
		$('#typeAchat3').removeAttr('checked');
		$('#typeAchat1').removeAttr('checked');
			handleFormChanged('#grammage');
		}
	}
	

	
	if (temp=='CDTVend')
	{
		$('#pqualite').hide('slow');
		$('#qualite')[0].selectedIndex=0;
		$('#pjustificatif').hide('slow');
		
		
		if (tableauNonRetenue.in_array($('#spieces_id').attr('value')))
		{
			$('#typeAchat1').removeAttr('checked');
			$('#typeAchat1').attr('disabled','disabled');
			//$('#typeVente1').removeAttr('checked');
			//$('#typeVente1').attr('disabled','disabled');
		}
		else
		{
			$('#typeAchat1').removeAttr('disabled');
			//$('#typeVente1').removeAttr('disabled');
		}
		//CDTVend
		//alert('1033');
		$('#pretrait').show('slow');
		$('#penvoi').show('slow');
		$('#pexpedition').hide('slow');
		$('#pdepot').hide('slow');

		$('#envoi').attr('checked','checked');
		$('#envoi').removeAttr('disabled');
		$('#depot').removeAttr('checked');
		$('#expedition').removeAttr('checked');
		
		//if ($('#spieces_id').attr('value')==idLingot) //ID du lingot pour obliger le retrait
		if (tableauLingots.in_array($('#spieces_id').attr('value'))) //ID du lingot pour obliger le retrait
		{//alert('1044');
			$('#envoi').removeAttr('checked');
			$('#envoi').attr('disabled','disabled');
			$('#retrait').attr('checked','checked');
		}
		else
		{//alert('1050');
			$('#retrait').removeAttr('checked');
			$('#envoi').removeAttr('disabled');
			$('#envoi').attr('checked','checked');
		}		
		//$('#typeVente1').removeAttr('disabled');
		
		
	}
	else
	{
	
		$('#pqualite').show('fast');
		$('#pjustificatif').show('fast');


		
			temp1=$('#grammage').attr('value');
			temp2=$('#quantite').attr('value');

			if ($('#qualite').val()=='1')
			{
				$('#typeVente2').removeAttr('disabled');
				$('#typeVente3').removeAttr('disabled');
			}
			else
			{
				$('#typeVente2').attr('disabled','disabled');
				$('#typeVente3').attr('disabled','disabled');
				$('#typeVente2').removeAttr('checked');
				$('#typeVente3').removeAttr('checked');
			}

		
		//alert('1060');
		$('#pdepot').show('slow');
		$('#pexpedition').show('slow');
		$('#penvoi').hide('slow');
		$('#pretrait').hide('slow');

		$('#expedition').removeAttr('disabled');
		$('#expedition').attr('checked','checked');
		$('#retrait').removeAttr('checked');
		$('#envoi').removeAttr('checked');
				
		if ($('#spieces_id').attr('value')==idLingot)
		{//alert('1072');
			$('#expedition').removeAttr('checked');
			$('#expedition').attr('disabled','disabled');
			$('#depot').attr('checked','checked');
		}
		else
		{//alert('1078');
			$('#depot').removeAttr('checked');
			$('#expedition').removeAttr('disabled');
			$('#expedition').attr('checked','checked');
		}
		

		
	}
	
		if (temp=='CDTVend')
		{
			prixCDTVend();
		}
		else
		{
			prixCDTAchete();
		}
		
	
	
		
	});
	//test si css est enti&egrave;rement d&eacute;sactiv&eacute; ou pas....
		if (testCss()==false)
		{
			alert('Attention les feuilles de style sont d&eacute;sactiv&eacute;es. Pour un fonctionnement optimal, d&eacute;sactiver aussi le javascript et recharger la page s\'il vous plaît');
		}
});




