// Liens externes --------------------------------------------------------------

/*
function openSite(strLocationURL) {
  var siteWindow = null;
  siteWindow = window.open(strLocationURL);
}

function checkExternalLinks() {
  var objLinks = document.getElementsByTagName("a");
  for (i=0; i < objLinks.length; i++) {
    if (objLinks[i].href.length > 0) {
      if (objLinks[i].href.indexOf(document.domain) == -1 && objLinks[i].href.indexOf("javascript:") == -1 && objLinks[i].href.indexOf("mailto:") == -1) {
      objLinks[i].href = "javascript: openSite('" + objLinks[i].href + "');";
      }
    }
  }
}

try {
  window.addEventListener("load", checkExternalLinks, true);
}
catch(ex) {
  window.attachEvent("onload", checkExternalLinks);
}
*/



// Afficher/Masquer ------------------------------------------------------------

function openSite(strLocationURL) {
  var siteWindow = null;
  siteWindow = window.open(strLocationURL);
}

function visibilite(thingId) {
  var bloc1 = document.getElementById('identification');
  var bloc2 = document.getElementById('recherche_avancee');
  if (bloc1.style.display == "none") {
    bloc1.style.display = "";
    bloc2.style.display = "none";
    } else {
    bloc1.style.display = "none";
    bloc2.style.display = "block";
  }
}



// Afficher/Masquer ------------------------------------------------------------
 
function Afficher(id) {
	var Obj;
  Obj = document.getElementById(id);
  if(Obj){
  	var yScroll;
		if (window.innerHeight && window.scrollMaxY) {	
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ 
			yScroll = document.body.scrollHeight;
		} else { 
			yScroll = document.body.offsetHeight;
		}

		var windowHeight;
		if (self.innerHeight) {	
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { 
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { 
			windowHeight = document.body.clientHeight;
		}
    
    Obj.style.height = ((yScroll < windowHeight) ? windowHeight : yScroll)  + 'px';  
    
    with(Obj.style){
      display="block";
			var selects = document.getElementsByTagName('select');
  		for(var i = 0; i < selects.length; i++) {
  			selects[i].style.visibility = 'hidden';
  		}
    }
  }
}

function Masquer(id) {
	var Obj;
  Obj = document.getElementById(id);
  if(Obj){
    with(Obj.style){
      display="none";
			var selects = document.getElementsByTagName('select');
  		for(var i = 0; i < selects.length; i++) {
  			selects[i].style.visibility = 'visible';
  		}
    }
  }
}



// Afficher/Masquer Bloc -------------------------------------------------------
 
function AfficherBloc(id) {
	var Obj;
  Obj = document.getElementById(id);
  if(Obj.style.display == "none"){
    Obj.style.display = "block";
  } else {
    Obj.style.display = "none";
  }
}



// Ajouter/Supprimer une class -------------------------------------------------

function addClass(element, className) {
  if (!hasClass(element, className)) {
    if (element.className) element.className += " " + className;
    else element.className = className;
  }
};

function removeClass(element, className) {
  var regexp = new RegExp("(^|\\s)" + className + "(\\s|$)");
  element.className = element.className.replace(regexp, "$2");
};

function hasClass(element, className) {
  var regexp = new RegExp("(^|\\s)" + className + "(\\s|$)");
  return regexp.test(element.className);
};



// Function permettant l'affichage et le masquage de champ en fonction du pays -
// selectionné dans la liste du formulaire -------------------------------------

function verif_pays() {
	element = document.getElementById('pays').options[document.getElementById('pays').selectedIndex].value;
	siret = document.getElementById('siret').parentNode.parentNode;
	tva_intercom = document.getElementById('tva_intercom').parentNode.parentNode;
	if(element == '16'){
		if(hasClass(siret, 'inactif')){
			removeClass(siret,'inactif');
		}
		if(!hasClass(tva_intercom, 'inactif')){
			addClass(tva_intercom,'inactif');
		}
		if(document.getElementById('siret').value == '')
			document.getElementById('siret').style.background = '#D6E66A';
		else
			document.getElementById('siret').style.background = '#FFFFFF';
	}
	else{
		if(!hasClass(siret, 'inactif')){
			addClass(siret,'inactif');
		}
		if(hasClass(tva_intercom, 'inactif')){
			removeClass(tva_intercom,'inactif');
		}
		if(document.getElementById('tva_intercom').value == '')
			document.getElementById('tva_intercom').style.background = '#D6E66A';
		else
			document.getElementById('tva_intercom').style.background = '#FFFFFF';
	}
}



// Menu ------------------------------------------------------------------------

jQuery().ready(function() {
	jQuery('#leftmenu_int').accordion({
		active: false,
		header: 'h3',
		navigation: true,
		animated: 'slide',
		alwaysOpen: false,
		autoheight: false
	});
});

function initMenu() {
  jQuery("#leftmenu_int #selected").addClass("selected");
  jQuery("#leftmenu_int #selected h3").addClass("selected");
  jQuery('#leftmenu_int .conteneur').click(
    function() {
      jQuery("#leftmenu_int #selected h3").removeClass();
    }
  );
}
$(document).ready(function() {initMenu();});

// Achat rapide ----------------------------------------------------------------

function maj_achat_rapide(cpt) {
	var total_ar = 0;
	var total_ligne = 0;
	var cpt_art = document.getElementById('cpt_art').value;

	for(var i=0;i<cpt_art;i++){
		if(document.getElementById('quantite_'+i).value != ''){
			total_ar = total_ar + (document.getElementById('quantite_'+i).value*document.getElementById('pu_'+i).value);	
		}
	}
	total_ligne = (document.getElementById('quantite_'+cpt).value*document.getElementById('pu_'+cpt).value);	
	document.getElementById('total_art_'+cpt).innerHTML = total_ligne.toFixed(2)+' &euro;';
	document.getElementById('total_ar').innerHTML = '<strong>'+total_ar.toFixed(2)+' &euro;</strong>';
}

function reset_quantite_achat_rapide(){
	var cpt_art = document.getElementById('cpt_art').value;
	for(var i=0;i<cpt_art;i++){
		document.getElementById('quantite_'+i).value = '';
		document.getElementById('total_art_'+i).innerHTML = '0.00 &euro;';
	}
	document.getElementById('total_ar').innerHTML = '<strong>0.00 &euro;</strong>';
}
function reset_quantite2_achat_rapide(cpt){
	document.getElementById('quantite_'+cpt).value = '';
	var total_ar = 0;
	var cpt_art = document.getElementById('cpt_art').value;
	for(var i=0;i<cpt_art;i++){
		if(document.getElementById('quantite_'+i).value != ''){
			total_ar = total_ar + (document.getElementById('quantite_'+i).value*document.getElementById('pu_'+i).value);
		}
	}
	document.getElementById('total_ar').innerHTML = '<strong>'+total_ar.toFixed(2)+' &euro;</strong>';
	document.getElementById('total_art_'+cpt).innerHTML = '0.00 &euro;';
}	


// Galerie & Info bulle --------------------------------------------------------

jQuery().ready(function() {
  $("#slider").easySlider({
  	controlsBefore:	'<p id="controls">',
  	controlsAfter:	'</p>',
  	prevId: 'prevBtn',
  	nextId: 'nextBtn'
  });
  /*
  $('#blocListe .content').tooltip({ 
      track: true, 
      delay: 0, 
      showURL: false, 
      showBody: " - ", 
      fade: 250 
  });
  */
  $('#container').tabs({
    //fxAutoHeight: false,
    //fxSlide: true,
    fxFade: true
  });
});



// CSS Browser Selector --------------------------------------------------------

var css_browser_selector = function() {
  var ua=navigator.userAgent.toLowerCase(),is=function(t){
    return ua.indexOf(t) != -1;
  },
  h=document.getElementsByTagName('html')[0],b=(!(/opera|webtv/i.test(ua))&&/msie (\d)/.test(ua))?('ie ie'+RegExp.$1):is('firefox/2')?'gecko ff2':is('firefox/3')?'gecko ff3':is('gecko/')?'gecko':is('opera/9')?'opera opera9':/opera (\d)/.test(ua)?'opera opera'+RegExp.$1:is('konqueror')?'konqueror':is('applewebkit/')?'webkit safari':is('mozilla/')?'gecko':'',os=(is('x11')||is('linux'))?' linux':is('mac')?' mac':is('win')?' win':'';var c=b+os+' js'; h.className += h.className?' '+c:c;
}();

var tab_lang = new Array('fr','en');
function affLang(id){
	for(var i=0;i<tab_lang.length;i++){
		document.getElementById('lang_'+tab_lang[i]).style.display = 'none';
	}
	document.getElementById('lang_'+id).style.display = 'block';
}

// Recalculer ------------------------------------------------------------------

var bIsFirebugReady = (!!window.console && !!window.console.log);

$(document).ready(
	function (){
		$("#idPluginVersion").text($.Calculation.version);
		$("input[name^=quantite_]").bind("keyup", recalc);
		recalc();
		//$("input[name^=quantite_]").bind("blur", colisage);
		//$("input[name^=supprimer_]").bind("change", suppr);
		$("input[name^=sum]").sum("keyup", "#totalSum");
		$("input[name^=avg]").avg({
			bind:"keyup"
			, selector: "#totalAvg"
			, onParseError: function(){
				this.css("backgroundColor", "#cc0000")
			}
			, onParseClear: function (){
				this.css("backgroundColor", "");
			}
		});
		$("input[name^=min]").min("keyup", "#numberMin");
		$("input[name^=max]").max("keyup", {
			selector: "#numberMax"
			, oncalc: function (value, options){
				$(options.selector).val(value);
			}
		});
		$("#idTotalTextSum").click(
			function (){
				var sum = $(".textSum").sum();
				$("#totalTextSum").text(sum.toString() + " \u20AC HT");
			}
		);
		$("#idTotalTextAvg").click(
			function (){
				var avg = $(".textAvg").avg();
				$("#totalTextAvg").text(avg.toString());
			}
		);
	}
);

/*
function colisage(){
	var qte = $("input[name^=quantite_]").val();
	var tab_ident = new Array();
	tab_ident = $("input[name^=quantite_]").attr("id").split('_');
	var colis = $("#colis_"+tab_ident[1]).text();
	if(parseInt(qte) % parseInt(colis) != 0){
		alert('Attention les quantités doivent être proportionnelles au colisage de chaque produit.');
		$("#quantite_"+tab_ident[1]).focus();
	}
}

function suppr(){
	if(this.checked == true) 
		if(!confirm('Etes vous sur ?'))
			this.checked = false;
		else{
			$.post("html/panier/maj_panier.php",{
			ident: $("input[name^=supprimer_]").attr("id")
			},
			''
			);
		}
}
*/
function recalc(){
	$("[id^=total_article]").calc(
		"quantite * prix",
		{
			quantite: $("input[name^=quantite_]"),
			prix: $("[id^=prix_article_]")
		},
		function (s){
			return s.toFixed(2) + " \u20AC HT";
		},
		
		//alert($("input[name^=quantite_]").val()),
		//alert ($("input[name^=quantite_]").attr("id")),
		
    function ($this){
			var frais = $("#frais").sum();
			var total_55 = $("#total_55").sum();
			var total_196 = $("#total_196").sum();
			var sum = $this.sum() + frais;
			var ttc = sum + total_55 + total_196;
			var avg = $this.avg();
			/*
			$.post(
			"html/panier/maj_panier.php",{
			ident: $("input[name^=quantite_]").attr("id"),
			value: $("input[name^=quantite_]").val()
			},
			''
			);
			*/
			$("#total_panier").text(
				sum.toFixed(2) + " \u20AC HT"
			);
			
			$("#total_panier_ttc").text(
				ttc.toFixed(2) + " \u20AC TTC"
			);
			maj_button();
		}
	);
}

$(document).ready(function(){
	$("#form_panier").submit(function(){
	var cpt_art = $("#cpt_art").val();
	var colisage = 0;
	var quantite = 0;
	var erreur = 0;
	for(var i=0;i<cpt_art;i++){
		colisage = $("#colis_"+i).text();
		quantite = $("#quantite_"+i).val();
		if(quantite%colisage != 0){
			$("#quantite_"+i).css({
			backgroundColor: "#1D5B42",
			color: "#FFF"
			});
			erreur = 1;
		}
		else{
			$("#quantite_"+i).css({
			backgroundColor: "#FFF",
			color: "#000"
			});
		}
	}
	if(erreur){
		alert('Attention les quantités doivent être proportionnelles au colisage de chaque produit.')
		return false;
	}
	else
		return true;
	});
	
	$("#poursuivre").click(function(){
	$("#form_panier").submit();
	});
	maj_button();
	
	$(".suppr").click(function(){
		var id = $(this).attr('id');
		var tab_id = id.split('_');
		
		$.post(
			"html/panier/suppr_art.php", 
			{ 
				id_panier: tab_id[1]
			}
		);
		$("#prod_"+tab_id[1]).hide();
		$("#prix_article_"+tab_id[1]).html('');
		recalc();
	});
	
});

function maj_button(){
	var total_panier = $('#total_panier').html();
	if(total_panier != null){
		var tab_total = total_panier.split(' ');
		total_panier = tab_total[0];
		if(total_panier < 500 && $('#pays').val() != '16')
			$('#poursuivre').css('display', 'none');
		else if(total_panier < 250 && $('#pays').val() == '16')
			$('#poursuivre').css('display', 'none');
		else
			$('#poursuivre').css('display', 'block');
	}
}

// CSS Browser Selector --------------------------------------------------------

var css_browser_selector = function() {
  var ua=navigator.userAgent.toLowerCase(),is=function(t){
    return ua.indexOf(t) != -1;
  },
  h=document.getElementsByTagName('html')[0],b=(!(/opera|webtv/i.test(ua))&&/msie (\d)/.test(ua))?('ie ie'+RegExp.$1):is('firefox/2')?'gecko ff2':is('firefox/3')?'gecko ff3':is('gecko/')?'gecko':is('opera/9')?'opera opera9':/opera (\d)/.test(ua)?'opera opera'+RegExp.$1:is('konqueror')?'konqueror':is('applewebkit/')?'webkit safari':is('mozilla/')?'gecko':'',os=(is('x11')||is('linux'))?' linux':is('mac')?' mac':is('win')?' win':'';var c=b+os+' js'; h.className += h.className?' '+c:c;
}();
