window.addEvent('domready', function() {
	var IE6 = (navigator.userAgent.toLowerCase().indexOf('msie 6') != -1) && (navigator.userAgent.toLowerCase().indexOf('msie 7') == -1); 
	if(IE6) {
		var sfEls = document.getElementsByTagName("li");
		for (var i=0; i<sfEls.length; i++) {
			sfEls[i].onmouseover = function() {
				this.className = this.className.replace(new RegExp(" sfhover"), "");
				this.className += " sfhover";
			}
			sfEls[i].onmouseout = function() {
				this.className = this.className.replace(new RegExp(" sfhover"), "");
			}
		}
	}
	$$('.form_tampon').addEvent('click', function(e) {
		e.stop();
		$$($('moo_form').get('target')).load('bloc.html?menu=lsv2&'+constructURL($('moo_form')));
		});
});

function lihover() {
	sfHover = function() {
             
        }
        if (window.attachEvent) window.attachEvent("onload", sfHover);
}

function change_cadre_tampon()	 {
	checkbox = document.getElementById('cadre_tampon');
	if(checkbox.checked) {
		for(i = 0; i < document.getElementsByName('couleur-dencrage').length; i++) {
			if(document.getElementsByName('couleur-dencrage')[i].checked) {
				$$($('moo_form').get('target')).load('bloc.html?menu=lsv2&couleur='+document.getElementsByName('couleur-dencrage')[i].value+'&'+constructURL($('moo_form')));
				switch(document.getElementsByName('couleur-dencrage')[i].value) {
					case 'cassette noire':
						document.getElementById('cadre_previsu').style.backgroundImage='url(skin/pictos/couleur-dencrage/cassette-noire.png)';
						break;
					case 'cassette bleue':
						document.getElementById('cadre_previsu').style.backgroundImage='url(skin/pictos/couleur-dencrage/cassette-bleue.png)';
						break;
					case 'cassette rouge':
						document.getElementById('cadre_previsu').style.backgroundImage='url(skin/pictos/couleur-dencrage/cassette-rouge.png)';
						break;
					case 'cassette verte':
						document.getElementById('cadre_previsu').style.backgroundImage='url(skin/pictos/couleur-dencrage/cassette-verte.png)';
						break;
					case 'cassette violette':
						document.getElementById('cadre_previsu').style.backgroundImage='url(skin/pictos/couleur-dencrage/cassette-violette.png)';
						break;
				}
			}
		}
	} else {
		document.getElementById('cadre_previsu').style.backgroundImage="";
	}
}

function _form_check_init(sform, sfield, scss)
{
	var pform = document.forms[sform];
	var ifield;
	var ptable = new Array();
	var ifirst = -1;
	var bchecked=false;

	for(ifield=0; ifield<pform.elements.length;ifield++) {
		if( sfield==pform.elements[ifield].name) 
		{
			if (ifirst==-1)	ifirst = ifield;
			if (pform.elements[ifield].checked) bchecked=true;
			pform.elements[ifield].onclick = function () { _form_check_click(this); }
			pform.elements[ifield]._mcss = scss;
			pform.elements[ifield]._mtfields = ptable;
			pform.elements[ifield].style.width = '0px';
			pform.elements[ifield].style.height = '0px';

			ptable[ptable.length] = pform.elements[ifield];
			_form_check_click(pform.elements[ifield]);
		}
		if (ifirst!=-1 && !bchecked)
		{
			pform.elements[ifirst].checked=true;
			_form_check_click(pform.elements[ifirst]);
		}
	}
}


function _form_check_click(pfield)
{
	if( pfield.type=='radio')
	{
		var pf, ifield;
		for(ifield=0;ifield<pfield._mtfields.length; ifield++)
		{
			pf = pfield._mtfields[ifield];
			if ( pf.checked )
					pf.nextSibling.className = pf._mcss+"Checked";
			else	pf.nextSibling.className = pf._mcss;
		}
	}
	else
	{
		var pf = pfield;
		if ( pf.checked )
				pf.nextSibling.className = pf._mcss+"Checked";
		else	pf.nextSibling.className = pf._mcss;
	}
}


function constructURL(elem)
{
	var list = elem.getChildren();
	var url = '';
	list.each(function(e)
	{
		if(e.hasChild)
			url = url+constructURL(e);
		
		if((e.get('tag') == 'input')||(e.get('tag') == 'select')||(e.get('tag') == 'textaera'))
		{
			if(e.get('type') == 'checkbox')
				url = url + e.get('name')+'='+e.get('checked')+'&'
			else if(e.get('type') == 'radio')
				url = url + e.get('value')+'='+e.get('checked')+'&'
			else
				url = url + e.get('name')+'='+e.get('value')+'&'
		}
	});
	return url;
}

function totalControl()
{
var pFields = document.frm.elements;
var pSelect, poption;
var nbmax = parseInt(pFields.qtemax.value);
var stepevol;
var iSelect, nbcmd, nbreste;	

	// compte le consomme
	
	stepevol = (nbmax / 10);
	if (stepevol<5) stepevol = 5;
	
	nbcmd = 0;
	for (iSelect=1; pSelect = pFields['taille'+iSelect]; iSelect++)
	{
		iline = pSelect.selectedIndex;
		if (iline>0) 
		{
			value = parseInt(pSelect.options[iline].value);
			nbcmd += value;
		}
	}
	
	// plafonne les SELECT
	
	nbreste = nbmax-nbcmd;
	
	for (iSelect=1; pSelect = pFields['taille'+iSelect]; iSelect++)
	{
		iline = pSelect.selectedIndex;
		if (iline<=0) iline=0;
		
		value = parseInt(pSelect.options[iline].value);
		nmaxset = value + nbreste;
		pSelect.options.length=1;
		stepvalue=stepevol;
		while (stepvalue<=nmaxset)
		{
			poption = new Option(stepvalue,stepvalue)
			pSelect.options[pSelect.options.length]=poption;
			if (stepvalue==value) pSelect.selectedIndex = pSelect.options.length-1;
			stepvalue+= stepevol;
		}
	}
	document.getElementById('commander').style.visibility = ( nbreste==0 ? 'visible':'hidden' );
	document.getElementById('comment').style.visibility = ( nbreste==0 ? 'hidden':'visible' );
	var p; p = document.getElementById("reste"); if (p) p.innerHTML = nbreste;
}


var bFormValid=false;


function recalcPreview()
{
	var t;
	var taux = parseFloat(document.frm.tauxtva.value);
	var baseprix  = document.frm.baseprix.value.replace(",",".").replace(" ","");
	var qte  = document.frm.qte.value.replace(",",".").replace(" ","");
	t = baseprix.split("euro")
	baseprix = t[0];
//	var baseprix  = document.frm.baseprix.value.replace(",",".").replace(" ","");
	baseprix  = parseFloat(baseprix);
	var prix = baseprix;
	var s;
	var list = $$('.optionclass');
    	list.each(function(poption)
    	{
			if (poption.selected)
			{
				t = poption.text.split ("(") 
				if (t.length>1)
				{
					s = t[1].replace(/ /g, "");
					if (s.charAt(0)=="+" || s.charAt(0)=="-")
						{
						s2 = "";
						for (i=0;i<s.length;i++) 
							{
							c = s.charAt(i);
							if (c=='+' || c=='-' || c=='.' || (c>='0' && c<='9')) s2 += c;
							}
						if (c=='+' || c=='-' || c=='.' || (c>='0' && c<='9')) s2 += c;
						t = s2.split("euro");
						prix = prix + parseFloat(s2); 
						}
				}
				
			}
		});

  if (parseFloat(qte)>0) prix = prix * parseFloat(qte);
	
	// formattage et prise en compte tva 
	vht = "" + Math.round(prix * 100);
	vht = vht.substr(0,vht.length-2)+","+vht.substr(vht.length-2);
	
	vttc = "" + Math.round(prix * (100 + taux)) ;
	vttc = vttc.substr(0,vttc.length-2)+","+vttc.substr(vttc.length-2);


var p;
	p = document.getElementById("montantht"); if (p) p.innerHTML = vht;
	p = document.getElementById("montantttc"); if (p) p.innerHTML = vttc;
	p = document.getElementById("montant"); if (p) p.value = vht;
	
	bFormValid = (parseFloat(vht) >0);
	
}


function amountFormat(v)
{
var s;
var entier, decimal;

	entier = Math.floor(v);
	decimal = "" + (100.0 + (v - entier ) * 100.0);

	return entier + "," + decimal.substr(1,2);
}

function qte2price_old()
{
var pFields = document.frm.elements;
var qte = parseInt(pFields.qte.value);
var nbTarifs = parseInt(pFields.nb_tarifs.value);
var fraisColor = pFields.frais_tech.value;
var vHT = 0;
var vTTC = 0;
var i, ptarif, pqte, starif, vTarif, qteTarif;

	// identifie le nb de couleurs
	imarque = 1;
	ncolors = 0;
	nmarquesCaps = 0;
    seuil=0;
	while ( pMarque = pFields["Marquage "+imarque] )
	{
		// si SANS est present alors il peut y avoir un NEUTRE
		if (pMarque.options[0].value=="Sans")
		{
			if ( (iselected = pMarque.selectedIndex) >= 1)
			{
				sMarque = pMarque.options[iselected].value;
				if (sMarque=="Sans");
				else if (sMarque.indexOf("2 couleurs") >=0)		ncolors += 2;
				else if (sMarque.indexOf("3 couleurs") >=0)		ncolors += 3;
				else if (sMarque.indexOf("4 couleurs") >=0)		ncolors += 4;
				else	ncolors += 1;
			}
		}
		else
		// si SANS est absent alors 1 ou 2
		{
			ncolors += 1;
			if ( (iselected = pMarque.selectedIndex) >= 1)
			{
				sMarque = pMarque.options[iselected].value;
				if (sMarque.indexOf("2 couleurs") >=0)			ncolors += 1;
				else if (sMarque.indexOf("3 couleurs") >=0)		ncolors += 2;
				else if (sMarque.indexOf("4 couleurs") >=0)		ncolors += 3;
			}
		}
		nmarquesCaps ++;
		imarque++;
	}

	vTarif = 0;
	qteTarif = 0;
	tauxTVA = parseFloat(document.frm.tauxtva.value);

	// recherche du tarif adapte a la quantite et neutre
	
	if (isNaN(qte) || qte==0) { qte = 0;}
	bhaveNeutral =false;
    
	for(i=1; i<=(nbTarifs+1);i++)
	{
		pqte = document.getElementById("quantite"+i);
		if (pqte)
		{
			lqte = parseInt(pqte.innerHTML);
			starif = document.getElementById("tarif"+i).innerHTML;
			ttarif = starif.split(" ");
			tempTarif = parseFloat ( ttarif[0].replace(",","."));
			tempTarif = Math.round ( tempTarif * 100.0) / 100.0;
	            if (seuil==0) seuil = lqte;
		
			if (lqte==0 || isNaN(lqte))
			{
				bhaveNeutral =true;
				if (ncolors==0)
				{
					vTarif = tempTarif;
					break;
				}
			}
			else
			if (qte>=lqte)
			{
				vTarif = tempTarif;
				qteTarif = lqte;
			}
		}
	}
        if (qte<seuil)vTarif=0;

	vHT = vTarif;

	
	// variation sur le tarif
	// 0.16/0.12 5000/0.10 10000;;
	
	coeff= 0;
	iFrais = 0;
	vFraisFixeHT = 0;
	vFrais = pFields['frais'].value;
	tFrais = new Array();

	if (ncolors> 0  && vHT>0 && vFrais!="")
	{
		tFrais = vFrais.split (";");
		if (ncolors>1)
		{
			if ( tFrais.length==1 ) 
					{ coeff = ncolors -1; iFrais = 0 ; }
			else	{ coeff = 1; iFrais = ncolors-2 ; }
		}
	}
	if (ncolors>1 && vHT>0)
	{
		srate = fraisColor;
		if (fraisColor.substr(0,1)=="*")  srate = fraisColor.substr(1);
		
		temp = parseFloat ( srate.replace(",","."));
		temp  = Math.round ( temp  * 100.0) / 100.0;
		
		if (fraisColor.substr(0,1)=="*") vFraisFixeHT = temp* (ncolors-1);
		else  vFraisFixeHT = temp;
	}
	
	vdeltaTarif = 0;
	if (iFrais>=0 && vFrais!="" && tFrais.length>0)
	{
		sLigneFrais = tFrais[iFrais];
		tPrixQteList = sLigneFrais.split ("/");
		
		for(iFraisSeuil=0; iFraisSeuil<tPrixQteList.length; iFraisSeuil++)
		{
			tPrixQte = tPrixQteList[iFraisSeuil].split(" ");
			if (tPrixQte.length==1) tPrixQte[1]=0;
			
			tempTarif = parseFloat ( tPrixQte[0].replace(",","."));
			vPrix = Math.round ( tempTarif * 100.0) / 100.0;
			lqte = parseInt(tPrixQte[1]);
			
			if (qte>=lqte)
			{
				vdeltaTarif = coeff * vPrix;
			}
		}
	}
//stemp = "	coeff:" + coeff +"	ncolors:" +  ncolors  +"	iFrais:" + iFrais +"	vdeltaTarif:" +  vdeltaTarif;
	// on n'applique le delta que si la commande est acceptable
	
	if (vHT>0)	vHT += vdeltaTarif;
	
	// mise en place tarifs

	montantHT = Math.round( (vHT * qte + vFraisFixeHT) * 100 ) / 100;
	montantTTC = Math.round (montantHT * ( 100.0 + tauxTVA )) / 100.0;
	
	document.getElementById('montantht').innerHTML = amountFormat(montantHT);
	document.getElementById('montantttc').innerHTML = amountFormat(montantTTC);
	p = document.getElementById("montant"); if (p) p.value = amountFormat(montantHT);

	document.getElementById('commander').style.visibility = (montantHT ? "visible":"hidden");
	document.getElementById('montants').style.visibility = document.getElementById('commander').style.visibility;
	
	//document.getElementById('trace').innerHTML = stemp;
}

function qte2price()
{
var pFields = document.frm.elements;
var qte = parseInt(pFields.qte.value);
var nbTarifs = parseInt(pFields.nb_tarifs.value);
var vHT = 0;
var vTTC = 0;
var i, ptarif, pqte, starif, vTarif, qteTarif;

    seuil=0;
	vTarif = 0;
	qteTarif = 0;
	tauxTVA = parseFloat(document.frm.tauxtva.value);

	// recherche du tarif adapte a la quantite et neutre
	
	if (isNaN(qte) || qte==0) { qte = 0;}
	bhaveNeutral =false;
    
	for(i=1; i<=(nbTarifs+1);i++)
	{
		pqte = document.getElementById("quantite"+i);
		if (pqte)
		{
			lqte = parseInt(pqte.innerHTML);
			starif = document.getElementById("tarif"+i).innerHTML;
			ttarif = starif.split(" ");
			tempTarif = parseFloat ( ttarif[0].replace(",","."));
			tempTarif = Math.round ( tempTarif * 100.0) / 100.0;
	            if (seuil==0) seuil = lqte;
		
			if (lqte==0 || isNaN(lqte))
			{
				bhaveNeutral =true;
				vTarif = tempTarif;
				break;
			}
			else
			if (qte>=lqte)
			{
				vTarif = tempTarif;
				qteTarif = lqte;
			}
		}
	}
        if (qte<seuil)vTarif=0;

	vHT = vTarif;

	// mise en place tarifs

	montantHT = Math.round( (vHT * qte) * 100 ) / 100;
	montantTTC = Math.round (montantHT * ( 100.0 + tauxTVA )) / 100.0;
	
	document.getElementById('montantht').innerHTML = amountFormat(montantHT);
	document.getElementById('montantttc').innerHTML = amountFormat(montantTTC);
	p = document.getElementById("montant"); if (p) p.value = amountFormat(montantHT);

	document.getElementById('commander').style.visibility = (montantHT ? "visible":"hidden");
	document.getElementById('montants').style.visibility = (montantHT ? "visible":"hidden");
	document.getElementById('hors_qtes').style.visibility = (montantHT ? "hidden":"visible");
	
	//document.getElementById('trace').innerHTML = stemp;
	bFormValid = (montantHT >0);
}
