// FONCTIONS SUP

////////////////////////////////////

function updateTable()
{

	var xmlURL	= '/zShop/XmlSearch/1?Promotion=1' ;
	var request	= createRequest();

	request.open( "GET", xmlURL, true );
	
	request.onreadystatechange = function()
	{

		if( request.readyState == 4 )
		{
			var response = request.responseXML ;
			refreshTable( 'promo', response );
		}

	};

	request.send( null );

}

function refreshTable( TableID, XMLresponse )
{
 
	var Auth		= XMLresponse.getElementsByTagName( 'authenticated' )[0];
	var isAuth		= Auth.firstChild.data;


	if( isAuth == 'true' )
	{
	
		

		
		//affiche le menu authentifié
		document.getElementById( 'menuAuth' ).style.display = 'block';

		var list = XMLresponse.getElementsByTagName( 'product' );

		for( var i = 0 ; i < list.length ; i++ )
		{
			addProduct( TableID, list[i] );
		}
		
		//affPromo (TableID);
		
		return;
		
	}
	else
	{
		
		//affiche le menu anonyme
		document.getElementById( 'menuAnonyme' ).style.display = 'block';		
		notAuth( TableID );
	}

}



function affPromo (TableID) {
	
	var table	= document.getElementById( TableID );
	var mtr		= document.createElement( 'tr' );
	var mtd		= document.createElement( 'td' );
	var mb		= document.createElement( 'b' );
	
	
	mb.appendChild( document.createTextNode( ' > Télécharger nos offres du moment (bon de commande) ' ) );
	
	mtd.setAttribute( "colspan", "5" );
	mtd.setAttribute( "height", "25" );
	mtd.appendChild( mb );
	mtr.appendChild( mtd );
	table.appendChild( mtr );
		
}


function notAuth( TableID )
{

	var table	= document.getElementById( TableID );
	var tr		= document.createElement( 'tr' );
	var td		= document.createElement( 'td' );
	var b		= document.createElement( 'b' );
	
	
	
	b.appendChild( document.createTextNode( 'Vous devez être authentifié pour acceder à nos offres.' ) );
	
	td.setAttribute( "colspan", "5" );
	td.appendChild( b );
	tr.appendChild( td );
	table.appendChild( tr );

	document.getElementById( 'wait' ).style.display = 'none';
}
	

	

function addProduct( TableID, currentNode )
{

	var table	= document.getElementById( TableID );
	var id		= getFirstNodeValue( currentNode, 'internal-id' );
	var ref		= getFirstNodeValue( currentNode, 'ref' );
	var mark	= getFirstNodeValue( currentNode, 'mark' );
	var name	= getFirstNodeValue( currentNode, 'name' );
	var desc	= getFirstNodeValue( currentNode, 'short-description' );
	var desg	= getFirstNodeValue( currentNode, 'long-description' );
	var urlp	= getFirstNodeValue( currentNode, 'url' );
	var price	= getFirstNodeValue( currentNode, 'fc-price' );
	var stock	= 0;
	var order	= 0;

	var stocks	= currentNode.getElementsByTagName( 'stock' );

	for( var i = 0 ; i < stocks.length ; i++ )
	{

		if( stocks[i].attributes.getNamedItem('name').nodeValue == 'En stock' )
		{
			stock = parseInt( stocks[i].firstChild.nodeValue );
		}

		if( stocks[i].attributes.getNamedItem('name').nodeValue == 'En commande fournisseur' )	
		{
			order = parseInt( stocks[i].firstChild.nodeValue );
		}

	}
	
	var tr		= document.createElement( "tr" );
	var td1		= document.createElement( "td" );
	var td2		= document.createElement( "td" );
	var td3		= document.createElement( "td" );
	var td4		= document.createElement( "td" );
	var td5		= document.createElement( "td" );
	var img		= document.createElement( "img" );
	var imgA	= document.createElement( "a" );
	var nameA	= document.createElement( "a" );
	var baskI	= document.createElement( "img" );
	var baskA	= document.createElement( "a" );
	var favoI	= document.createElement( "img" );
	var favoA	= document.createElement( "a" );
	var fontB1	= document.createElement( "font" );
	var fontB2	= document.createElement( "font" );
	var fontP1	= document.createElement( "font" );
	var fontP2	= document.createElement( "font" );
	var fontPr	= document.createElement( "font" );

	img.setAttribute( "src", "/products/tiny/" + ref + ".jpg" );
	img.setAttribute( "alt", name );
	img.setAttribute( "class", "vign-prod" );
	img.setAttribute( "width", "60" );
	img.setAttribute( "height", "60" );
	img.setAttribute( "border", "0" );

	imgA.setAttribute( "href", urlp );
	imgA.appendChild( img );

	td1.setAttribute( "class", "vignette" );
	td1.appendChild( imgA );
	

	nameA.setAttribute( "href", urlp );
	nameA.setAttribute( "class", "noir" );
	nameA.appendChild( document.createTextNode( name ) );
	
	fontB1.setAttribute( "class", "bleu" );
	fontB2.setAttribute( "class", "bleu" );
	fontP1.setAttribute( "class", "petit" );
	fontP2.setAttribute( "class", "petit" );

	fontB1.appendChild( document.createTextNode( mark ) );
	fontB2.appendChild( document.createTextNode( ref ) );
	
	fontP1.appendChild( document.createTextNode( "Marque : " ) );
	fontP1.appendChild( fontB1 );
	fontP2.appendChild( document.createTextNode( "Ref : " ) );
	fontP2.appendChild( fontB2 );
	
	td2.appendChild( nameA );
	td2.appendChild( document.createElement( "br" ) );
	td2.setAttribute( "width", "330" );

	td2.appendChild( fontP1 );
	td2.appendChild( document.createElement( "br" ) );
	td2.appendChild( fontP2 );

	if( stock > 0 )
	{
		td2.appendChild( document.createElement( "br" ) );
		var fontDs = document.createElement( "font" );
		fontDs.setAttribute( "class", "dispo" );
		fontDs.appendChild( document.createTextNode( "En stock" ) );
		td2.appendChild( fontDs );
	}

	if( order > 0 )
	{
		td2.appendChild( document.createElement( "br" ) );
		var fontDo = document.createElement( "font" );
		fontDo.setAttribute( "class", "dispo" );
		fontDo.appendChild( document.createTextNode( "En commande fournisseur" ) );
		td2.appendChild( fontDo );
	}
	
	fontPr.setAttribute( "class", "petit rouge" );
	
	fontPr.appendChild( document.createTextNode( price + " €" ) );
	
	td3.setAttribute( "nowrap", "nowrap" );
	td3.setAttribute( "class", "aright" );
	td3.appendChild( fontPr );

	favoI.setAttribute( "src", "/medias-nemo/images/coeur-over-nemo.gif" );
	favoI.setAttribute( "border", "0" );
	favoI.setAttribute( "width", "15" );
	favoI.setAttribute( "height", "15" );
	favoI.setAttribute( "alt", "Ajouter cet article à vos favoris" );
	favoA.setAttribute( "href", "/boutique/Favourites.jsp?addFavourite=" + id );
	favoA.appendChild( favoI );
	td4.appendChild( favoA );

	baskI.setAttribute( "src", "/medias-nemo/images/caddie-over-nemo.gif" );
	baskI.setAttribute( "border", "0" );
	baskI.setAttribute( "width", "15" );
	baskI.setAttribute( "height", "15" );
	baskI.setAttribute( "alt", "Ajouter cet article à votre panier" );
	baskA.setAttribute( "href", "/boutique/Basket.jsp?addBasket=" + id );
	baskA.appendChild( baskI );
	td5.appendChild( baskA );

	tr.appendChild( td1 );
	tr.appendChild( td2 );
	tr.appendChild( td3 );
	tr.appendChild( td4 );
	tr.appendChild( td5 );

	table.appendChild( tr );

	document.getElementById( 'wait' ).style.display = 'none';
	
}
