// code javascript pour le site Fixanet

// tableau des fonctions à démarrer...
var fonctionInit = new Array(initform);

window.onload = initialisation;

function initialisation()
{
	for(var i = 0; i < fonctionInit.length; i++)
		fonctionInit[i]();
}


// function pour afficher les items du menu
var itemAfficher = new Array();

function afficheItem(id)
{
	if (document.getElementById(id))
	{
		document.getElementById(id).style.display = 'block';
		itemAfficher[itemAfficher.length] = document.getElementById(id);
	}
}

function cacherItem(id)
{
	if (document.getElementById(id))
		document.getElementById(id).style.display = 'none';
}

function cacherTout()
{
	for (var i = 0; i < itemAfficher.length; i++)
		itemAfficher[i].style.display = 'none';
	
	itemAfficher = new Array();
}


function charge(url)
{
	window.location = url;
}

function menuPremierNiveau(obj, display)
{
    if (obj && obj.style)
        obj.style.display = display;
}

/***********************************************/
/* code javasrcipt utilisé lors de la commande */
/***********************************************/
function initform()
{
	if (document.getElementById('idPourAfficherProduit') && refSelectionner != '')
	{
		afficheFormCommande(document.getElementById('idPourAfficherProduit'), refSelectionner);
	}
}


var formCourant = null;
function afficheFormCommande(prod, ref)
{
	if (formCourant != null && formCourant == prod)
	{ // cache form
		actionForm(prod, 'none', document.getElementById('reference').value);		
		document.getElementById('reference').value = '';
		formCourant = null;
	}
	else
	{ // cache formCourant (si non null) et affiche prod
		if (formCourant != null)
		{ // cache formCourant
			actionForm(formCourant, 'none', document.getElementById('reference').value);
		}
		var Browser = {
		  Version: function() {
			var version = 999; // we assume a sane browser
			if (navigator.appVersion.indexOf("MSIE") != -1)
			  // bah, IE again, lets downgrade version number
			  version = parseFloat(navigator.appVersion.split("MSIE")[1]);
			return version;
		  }
		}
		if (Browser.Version()<= 7) {
			actionForm(prod, 'block', ref);
		} else {
			actionForm(prod, 'table-row', ref);
		}
		document.getElementById('reference').value = ref;
		formCourant = prod;
	}
}

function actionForm(form, value, ref)
{
	var cur = form.nextSibling;
	if (!cur.className)
		cur = cur.nextSibling;
	while(cur && cur.className == 'ligne_tarifs')
	{
		cur.style.display = value;
		cur = cur.nextSibling;
		if (cur && !cur.className)
			cur = cur.nextSibling;
	}
	activeChp(ref, (value=='none'?'disabled':''));
}

function activeChp(ref, value)
{
	var i = 0;
	while (document.getElementById(ref+'_'+i))
	{
		document.getElementById(ref+'_'+i).disabled = value;
		i++;
	}
}


function trieProduit(idRubrique, nameColumn)
{
	var str = window.location.href;
	if (str.indexOf('trie') > -1)
		str = str.substr(0, str.indexOf('&trie'));
	
	window.location = str+'&trie='+nameColumn;
}


function in_array(value, array)
{
	for (var i = 0; i < array.length; i++)
	{
		if(array[i] == value)
			return i;
	}
	return -1;
}


function listingCharge(ref)
{
	envoie_requete('lib/ajax/listingCharge.php?ref='+ref+'&num'+Math.random(), afficheListingCharge)
}

function afficheListingCharge(HTML)
{
	new bddfixed(HTML);
}

/**************************************/
/* codee utilisé lors de la recherche */
/**************************************/

function selectionneValeur(colonne, idRubrique)
{
	envoie_requete('lib/ajax/selectProduit.php?colonne='+colonne+'&idRubrique='+idRubrique+'&num'+Math.random(), afficheSelect)
}

function afficheSelect(tab)
{
	var Select = document.getElementById('selectForValueForSearch');
	while(Select.length > 0)
		Select.options[0] = null;
	
	var valeur = tab.split('|');
	for(var elem in valeur)
	{
		Select.options[Select.length] = new Option(valeur[elem], valeur[elem]);
	}
}


function afficheChpRecherche(div, table)
{
	div.style.display = 'none';
	table.style.display = 'block';
}

function afficheListing(tr)
{
	cur = tr.nextSibling;
	if (! cur.style)
		cur = cur.nextSibling;
	
	while (cur)
	{
		if (cur.style.display != 'table-row')
			cur.style.display = 'table-row';
		else
			cur.style.display = 'none';
		
		cur = cur.nextSibling;
		if (cur && !cur.style)
			cur = cur.nextSibling;
	}
}

/*******************************************************/
/* utilise pendant le tuto : trouvez la bonne fixation */
/*******************************************************/
var savTheOnePart = '';

function choixMateriaux(id_mat)
{
	savTheOnePart = document.getElementById('endroitMettreSuiteProcessTuto1').innerHTML;
	document.getElementById('titreMateriaux').innerHTML = materiaux[id_mat];
	var HTML = '<form method="get">' +
					'<input type="hidden" name="page" value="charge" />' +
					'<input type="hidden" name="materiaux" value="'+id_mat+'" />' +
					'Quelle masse : <input type="text" size="4" value="0.0" name="masse" style="text-align:right" /> kg<br />' +
					'Nombre de chevilles : <input type="text" size="4" value="1" name="nbCheville" /><br />' +
					
					'<input type="submit" value="Rechercher" /> <input type="button" value="Retour choix Matériaux" onclick="retourChoixMateriaux()" />' +
				'</form>';
	
	document.getElementById('endroitMettreSuiteProcessTuto1').innerHTML = HTML;
}

function retourChoixMateriaux()
{
	document.getElementById('titreMateriaux').innerHTML = '';
	document.getElementById('endroitMettreSuiteProcessTuto1').innerHTML = savTheOnePart;
	savTheOnePart = '';
}

/******************************/
/* gestion de l'espace membre */
/******************************/

var adrCourant = new Array(null, '');
var chpCourant = null;
function passeInfo(adr)
{
	if (chpCourant == null)
	{
		sortInfo();
		adr.onmouseover = null;
		
		adrCourant = Array(adr, adr.innerHTML);
		adr.innerHTML = '<input type="text" name="'+adr.title+'" value="'+adr.innerHTML+'" onclick="clickChp(this)" />';
		//size="'+adr.innerHTML.length+'"
	}
}

function clickChp(chp)
{
	chp.onclick = null;
	chpCourant = chp;
	var input = document.createElement('INPUT');
	input.type = 'button';
	input.value = 'Valider';
	input.onclick = changeValue;
	chp.parentNode.appendChild(input);
	
	input = document.createElement('INPUT');
	input.type = 'button';
	input.value = 'Annuler';
	input.onclick = function ()
	{
		chpCourant = null;
		sortInfo();
	}
	chp.parentNode.appendChild(input);
}

function changeValue()
{
	chp = this.previousSibling;
	charge('index.php?page=espaceMembre&action=modifInfoPerso&name='+chp.name+'&value='+chp.value);
}

function sortInfo()
{
	if (adrCourant[0] != null && chpCourant == null)
	{
		adrCourant[0].innerHTML = adrCourant[1];
		adrCourant[0].onmouseover = function ()
		{
			passeInfo(this);
		}
	}
	
}

function afficheFormComplete()
{
	if (document.getElementById('idFormulaireDynamic').style.display != 'none')
	{
		document.getElementById('idFormulaireDynamic').style.display = 'none';
		document.getElementById('idFormulaireStatic').style.display = 'block';
		document.getElementById('lienOnClickFormComplete').innerHTML = '(Formulaire dynamique)';
	}
	else
	{
		document.getElementById('idFormulaireDynamic').style.display = 'block';
		document.getElementById('idFormulaireStatic').style.display = 'none';
		document.getElementById('lienOnClickFormComplete').innerHTML = '(Formulaire complet)';
	}
}


/************************************/
/*		Finaliser la commande		*/
/************************************/

function selectionModeLivraison()
{
	var HTML = document.getElementById('idPourAfficherTextDansPopupChoixLivr').innerHTML;
	
	new bddfixed(HTML);
}

function aficheCGV()
{
	var HTML = document.getElementById('idPourAfficherTextDansPopupCGV').innerHTML;
	
	new bddfixed(HTML);
}

function suitePaiement()
{
	envoie_requete('lib/ajax/genereCards.php?num'+Math.random(), replaceHtml);
}

function replaceHtml(HTML)
{
	alert(HTML);
	document.getElementById('html_replace_cards').innerHTML = HTML;
}