/**
 * Ce script contient les fonctions relatives aau fonctionnement de la page 'commande-au-numero.htm'
 */
function collection_choisie( )
{
	var id 						= "";
	var	choix_collection		= null;
	var	div_choix_numeros		= null;
	var	iframe_choix_numeros	= null;
	
	
	if( document.getElementById("choix_collection") != null )
		choix_collection = document.getElementById("choix_collection");
	if( document.getElementById("div_choix_numeros") != null )
		div_choix_numeros = document.getElementById("div_choix_numeros");
	if( document.getElementById("iframe_choix_numeros") != null )
		iframe_choix_numeros = document.getElementById("iframe_choix_numeros");
	
	
	
	if( choix_collection != null )
	{
		id = choix_collection.options[choix_collection.selectedIndex].value;
	}
	
	if( id == "" )
	{
		if( div_choix_numeros != null )
			div_choix_numeros.style.display = 'none';
		return false;
	}
	
	
	if( iframe_choix_numeros != null )
	{
		iframe_choix_numeros.src = '/commander-un-numero/iframe_choix_numeros.htm?rnd='+(Math.random())+'&id_collection='+id;
		
		if( div_choix_numeros != null )
			div_choix_numeros.style.display = 'block';
	}
}
function set_collection_choisie( id )
{
	var	choix_collection = null;
	
	if( document.getElementById("choix_collection") != null )
		choix_collection = document.getElementById("choix_collection");
	
	for( var i=0; i < choix_collection.options.length; i++ )
	{
		if(  choix_collection.options[i].value == id )
		{
			choix_collection.selectedIndex = i;
			choix_collection.onchange();
			break;
		}
	}
}

function monTableau()
{
	this.tBody 				= null;
	this._items 			= new Array();
	this.articles_choisis 	= null;
	
	
	
	if( document.getElementById("monTableau") == null )
		return false;
	this.tBody 	= document.getElementById("monTableau");
	
	this.ajouterUneLigne = function ( ligne )
	{
		var newIndex 	= this.tBody.childNodes.length-1;
		
		this._setColor( newIndex , ligne );
		
		this.tBody.appendChild( ligne.ligne );
		this._items.push( ligne );
	}
	
	this._setColor = function ( index , ligne )
	{
		if( index % 2 != 0 )
		{
			ligne.ligne.className 	= "ligne_verte";
			ligne.img.src 			= "../../../mediatheque/_autres/commander-un-numero/corbeille.gif";
		}
		else
		{
			ligne.ligne.className 	= "ligne_blanche";
			ligne.img.src 			= "../../../mediatheque/_autres/commander-un-numero/corbeille.gif";
		}
	}
	
	this.trouverUneLigneParId = function ( id )
	{
		for( var i = 0 ; i < this._items.length; i++ )
		{
			if( this._items[i] != null )
			{
				if( parseInt(id) == parseInt(this._items[i].article.id) )
				{
					return this._items[i];
				}
			}
		}
		
		return null;
	}
	
	this.retirerUneLigne = function ( ligne )
	{
		if( ligne == null )
			return false;
		
		try
		{
			this.tBody.removeChild( ligne.ligne );
		}
		catch( ex )
		{
		}
		
		
		for( var i = 0 ; i < this._items.length; i++ )
		{
			if( this._items[i] != null )
			{
				if( ligne.article.id == this._items[i].article.id )
				{
					this._items[i] = null;
				}
			}
		}
		
		this._corrigerItems();
	}
	
	this.vider = function ( )
	{
		for( var i = 0 ; i < this._items.length; i++ )
		{
			if( this._items[i] != null )
			{
				this.retirerUneLigne( this._items[i] );
				this._items[i] = null;
				this._corrigerItems();
			}
		}
		for( var i =1; i < this.tBody.childNodes.length; i++ )
		{
			this.tBody.removeChild( this.tBody.childNodes[i] );
		}
		
	}
	
	this.setFraisDePort = function ()
	{
		var e = document.getElementById("montant_frais_de_port");
		
		if( e == null )
			return false;
		
		e.innerHTML = "<b>"+this.articles_choisis.montant_frais_de_port_en_texte()+" €</b>";
	}
	
	this.setMontantGlobal = function ()
	{
		var e = document.getElementById("montant_total");
		
		if( e == null )
			return false;
		
		e.innerHTML = "<b>"+this.articles_choisis.montant_global_en_texte()+" €</b>";
	}
	
	this.construire = function ( article_choisis )
	{
		this.vider();
		this.vider();
		this.vider();
		this.articles_choisis = article_choisis;
		
		for( var i = 0; i < this.articles_choisis._items.length; i++ )
		{
			var ligne = new maLigne( this.articles_choisis._items[i] );
			this.ajouterUneLigne( ligne );
		}
		
		this.setFraisDePort();
		this.setMontantGlobal();
		this.toto();
	}
	
	this.toto = function()
	{
		if( this.articles_choisis == null ) return false;
		
		
		if(this.articles_choisis._items.length == 0 )
		{
			document.getElementById("messageVide").style.display = "block";
		}
		else
		{
			document.getElementById("messageVide").style.display = "none";
		}
	}

	this._corrigerItems = function ()
	{
		var n = new Array();
		
		for( var i = 0; i < this._items.length; i++ )
		{
			if( this._items[i] != null )
			{
				n.push(this._items[i]);
				this._setColor( n.length-1 , this._items[i] );
			}
		}
		
		this._items = n;
		this.length = this._items.length;
		this.toto();
	}
}

function maLigne( article_choisi )
{
	this.liste		 	= null;
	this.img		 	= null;
	this.ligne 			= null;
	this.col1 			= null;
	this.col2 			= null;
	this.col3 			= null;
	this.col4 			= null;
	this.col5 			= null;
	this.prix_total		= null;
	this.article		= article_choisi;
	
	//////////////////////////////////////////////////////////////////
	this.ligne	= document.createElement("tr");
	this.col1	= document.createElement("td");
	this.col2	= document.createElement("td");
	this.col3	= document.createElement("td");
	this.col4	= document.createElement("td");
	this.col5	= document.createElement("td");
	
	this.ligne.appendChild( this.col1 );
	this.ligne.appendChild( this.col2 );
	this.ligne.appendChild( this.col3 );
	this.ligne.appendChild( this.col4 );
	this.ligne.appendChild( this.col5 );
	
	//////////////////////////////////////////////////////////////////
	this.col1.className = "col1";
	this.col2.className = "col2";
	this.col3.className = "col3";
	this.col4.className = "col4";
	this.col5.className = "col5";
	
	//////////////////////////////////////////////////////////////////
	var s = document.createElement("strong");
	s.appendChild( document.createTextNode( this.article.libelle ) );
	this.col1.appendChild( s );

	//////////////////////////////////////////////////////////////////
	this.col2.appendChild( document.createTextNode( this.article.prix_unitaire_en_texte()+" €" ) );
	
	//////////////////////////////////////////////////////////////////
	this.liste				= document.createElement("select");
	this.liste.className	= "SELECT_TEXT";
	this.liste.name			= "numeros["+this.article.id+"]";
	this.liste.style.width	= "40px";
	this.liste.article 		= this.article;
	this.liste.onchange		= function(){ modifier_quantite( this.article.id , this.selectedIndex ); return false; };
	
	for( var i=0; i <= this.article.stock; i++ )
	{
		var texte 	= "";
		if( i < 10 )
			texte = "0"+(i.toString());
		else
			texte = i.toString();
			
			
		var n 		= document.createElement("option");
		n.value 	= i;
		n.text		= texte;
		n.innerHTML	= texte;
		
		
		if( i == this.article.quantite )
			n.selected = true;
			
		this.liste.appendChild( n );
	}
	this.col3.appendChild( this.liste );
	
	//////////////////////////////////////////////////////////////////
	var s = document.createElement("strong");
	this.prix_total = document.createTextNode( this.article.prix_total_en_texte()+" €" );
	s.appendChild( this.prix_total );
	this.col4.appendChild( s );
	
	//////////////////////////////////////////////////////////////////
	var Href 			= document.createElement("a");
	Href.href 			= "#";
	Href.article 		= this.article;
	Href.onclick 		= function(){ modifier_quantite( this.article.id , 0 ); return false; }
	this.img 			= document.createElement("img");
	this.img.border 	= 0;
	this.img.width 		= 18;
	this.img.height 	= 14;
	this.img.src 		= "../../../mediatheque/_autres/commander-un-numero/corbeille.gif";
	Href.appendChild( this.img );
	this.col5.appendChild( Href );
	
	//////////////////////////////////////////////////////////////////
	this.mettreAjourPrixtotal = function()
	{
		this.prix_total.data = this.article.prix_total_en_texte()+" €";
	}
}

var t = null;
function construire_tableau( )
{
	if( t == null )
		t = new monTableau();
	if( t == "undefined" )
		t = new monTableau();
	
	
	
	t.construire( this.Articles_choisis_ );



}
function modifier_quantite( id , qte )
{
	var ligne = t.trouverUneLigneParId( id );
	
	if( qte == 0 )
	{
		if( ligne != null )
		{
			t.retirerUneLigne( ligne );
		}
	}
	var liste			 	= document.getElementById("iframe_choix_numeros").contentWindow.document.getElementById("article_" + id );
	liste.selectedIndex 	= qte;
	document.getElementById("iframe_choix_numeros").contentWindow.ajouter_Article( liste , id );
	
	ligne.mettreAjourPrixtotal();
	t.setFraisDePort();
	t.setMontantGlobal();
	t.toto();
}

function valider()
{ 
	
	if( this.Articles_choisis_._items.length == 0 )
	{
		alert("Votre panier est vide.");
	}
	else if( this.Articles_choisis_.quantite() > 20 )
	{
		alert("Votre commande contient plus de 20 articles, veuillez modifier votre sélection.");
	}
	else if( document.getElementById("cgv_").checked == false )
	{
		alert("Veuillez accepter les conditions générales de vente.");
	}
	else
	{
		document.f_numeros.submit();
	}
}

function afficher_confirmation()
{
	var div_contenu_numero 		= null;
	var div_bg_confirmation 	= null;
	var iframe_bg_confirmation 	= null;
	
	if( document.getElementById("contenu_numero") == null )
		return false;
	div_contenu_numero = document.getElementById("contenu_numero");	
	
	if( document.getElementById("bg_confirmation") == null )
		return false;
	div_bg_confirmation = document.getElementById("bg_confirmation");
	
	if( document.getElementById("iframe_bg_confirmation") == null )
		return false;
	iframe_bg_confirmation = document.getElementById("iframe_bg_confirmation");
	
	construire_tableau( );
	
	div_bg_confirmation.style.width 	= "685px";
	div_bg_confirmation.style.height 	= (div_contenu_numero.clientHeight-2)+"px";
	div_bg_confirmation.style.top		= "20px";
	div_bg_confirmation.style.left		= "34px";
	
	opacite 										= 0.3;
	iframe_bg_confirmation.style.opacity 			= opacite; //navigateurs récents (firefox, opera, Safari>1,2...) css3
	iframe_bg_confirmation.style.MozOpacity 		= opacite; //anciennes versions firefox
	iframe_bg_confirmation.style.KhtmlOpacity 		= opacite; //Konqueror, Safari<1,2
	iframe_bg_confirmation.style.filter 			= "alpha(opacity=" + opacite * 100 + ")"; //Internet Explorer
	div_bg_confirmation.style.display				= "block";
	
	if( location.href.substr( location.href.length-4 ) != "#top" )
		location.href = location.href+"#top";
	else
		location.href = location.href.substr( 0,location.href.length-4 )+"#top";
}
function cacher_confirmation()
{
	var div_contenu_numero 		= null;
	var div_bg_confirmation 	= null;
	var iframe_bg_confirmation 	= null;
	
	if( document.getElementById("contenu_numero") == null )
		return false;
	div_contenu_numero = document.getElementById("contenu_numero");
	
	if( document.getElementById("bg_confirmation") == null )
		return false;
	div_bg_confirmation = document.getElementById("bg_confirmation");
	
	if( document.getElementById("iframe_bg_confirmation") == null )
		return false;
	iframe_bg_confirmation = document.getElementById("iframe_bg_confirmation");
	
	
	opacite 										= 0;
	iframe_bg_confirmation.style.opacity 			= opacite; //navigateurs récents (firefox, opera, Safari>1,2...) css3
	iframe_bg_confirmation.style.MozOpacity 		= opacite; //anciennes versions firefox
	iframe_bg_confirmation.style.KhtmlOpacity 		= opacite; //Konqueror, Safari<1,2
	iframe_bg_confirmation.style.filter 			= "alpha(opacity=" + opacite * 100 + ")"; //Internet Explorer
	div_bg_confirmation.style.display 				= "none";
}