function getXhr(){
	var xhr = null;
	if(window.XMLHttpRequest){
		xhr = new XMLHttpRequest();
	}
	else if(window.ActiveXObject){
		try{
			xhr = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e){
			xhr = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	else{
		alert("Your browser does not support AJAX..."); 
		xhr = false; 
	}
		
  return xhr;
}

function go(c){
	if(!c.data.replace(/\s/g,''))
		c.parentNode.removeChild(c);
}

function clean(d){
	var bal=d.getElementsByTagName('*');

	for(i=0;i<bal.length;i++){
		a=bal[i].previousSibling;
		if(a && a.nodeType==3)
			go(a);
		b=bal[i].nextSibling;
		if(b && b.nodeType==3)
			go(b);
	}
	return d;
}

function captcha(){
	var operation = '';
	var resultat = '';
	var op = '+';
	var tag1 = '<img src="images/interface/';
	var tag2 = '" alt="" />';
	var ind1 = Math.round(Math.random() * 10);
	var ind2 = Math.round(Math.random() * 10);
	
	var img = new Array();
	img[0] = '0.gif';
	img[1] = '1.gif';
	img[2] = '2.gif';
	img[3] = '3.gif';
	img[4] = '4.gif';
	img[5] = '5.gif';
	img[6] = '6.gif';
	img[7] = '7.gif';
	img[8] = '8.gif';
	img[9] = '9.gif';
	img[10]= '0.gif';
	
	operation = tag1 + img[ind1] + tag2 + '&nbsp;' + tag1 + 'plus.gif' + tag2 + '&nbsp;' + tag1 + img[ind2] + tag2;
	resultat = eval(ind1 + op + ind2);
	query = '/global/comment.php?captcha=' + resultat;
	//	Lancement de la fonction AJAX
	var xhr = getXhr();
	xhr.open("GET", query, true);
  xhr.send(null);
	$('operation').innerHTML = operation;
}

function checkCaptcha(){
	if($('verif').value == '' || isNaN($('verif').value)){
		alert("Erreur : Saisir le résultat de l'opération\nError: please fill in the result of the operation");
		return false;
	}
	else{
		return true;
	}
}

function getElementsByClassName(classname, node){
	var a = new Array();
	var els = document.getElementsByTagName(node);
	for(var i=0; i < els.length; i++){
		if(els[i].className == classname){
			a.push(els[i]);
		}
	}
	return a;
}

//	Remplcement des caractères spéciaux provenant des XML
function tweak(exp){
	exp_0 = exp.replace(/[\n]/gi, '<br />');
	exp_1 = exp_0.replace("'", '&lsquo;');
	exp_1 = exp_1.replace("|", '<br />');
	exp_1 = exp_1.replace(" |", '<br />');
	exp_1 = exp_1.replace("| ", '<br />');
	return exp_1;
}

function fullsize(){
	var d = $('full');
	d.style.display = 'block';
}

function unfullsize(){
	$('full').style.display = 'none';
}


function swap(im, id_couleur){
	var img_f = $('image_full');
	var id_produit = $('id_produit').value;
	$('couleur').value = id_couleur;
	img_f.src = 'images/produit/full/' + im;
	get_tailles(id_produit, id_couleur);
	$('img-link').href=img_f.src;
}

function swapView(im){
	var img_f = $('image_full');
	img_f.src = 'images/vues/full/' + im;
	$('img-link').href=img_f.src;
}


function get_tailles(id, couleur){
	var xhr = getXhr();
	$('taille_wait').style.display = 'inline';	
	$('couleur_wait').style.display = 'inline';
	xhr.onreadystatechange = function() {ajax_fill_tailles(xhr);};
	query = '/client/update_tailles.php?id_produit=' + id + '&id_couleur=' + couleur;
	//on appelle le fichier
	xhr.open("GET", query, true);
	xhr.send(null);
	//on affiche le message d'attente
	change();
}


function ajax_fill_tailles(xhr){
	if (xhr.readyState == 4){
		//	On masque le curseur d'attente
		change();	
		$('taille_wait').style.display = 'none';
		$('couleur_wait').style.display = 'none';
		$('tailles').innerHTML = '';
		
		var docXML = xhr.responseXML;
		var id_taille = docXML.getElementsByTagName('id_taille');
		var taille = docXML.getElementsByTagName('taille');
		var couleur = docXML.getElementsByTagName('couleur');

		for(i = 0; i < id_taille.length; i++){
			elem = document.createElement('option');
			opt = $('tailles').appendChild(elem);
			opt.value = id_taille.item(i).getAttribute('data');
			opt.innerHTML = taille.item(i).getAttribute('data');
		}
		$('couleur_select').innerHTML = couleur.item(0).getAttribute('data');
		//$('disponibilite').src = '/images/interface/' + dispo.item(0).getAttribute('data');
	}
}

//	Correction des bugs d'affichage des PNG sous IE6
function IE_CorrectAlpha_PNG(){
	var version = navigator.appVersion;
	if(version.indexOf('MSIE 6.0') != -1){
		var images = document.getElementsByTagName('img');
		for(i=0; i<document.images.length; i++){
			img    = document.images[i];
			imgExt  = img.src.substring(img.src.length-3, img.src.length);
			imgExt  = imgExt.toUpperCase();
			if (imgExt == "PNG"){
				imgID    = (img.id) ? "id='" + img.id + "' " : "";
				imgClass= (img.className) ? "class='" + img.className + "' " : "";
				imgTitle= (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' ";
				imgStyle= "display:inline-block;" + img.style.cssText;
				if (img.align == "left") { imgStyle = "float:left;"  + imgStyle; } else if (img.align == "right"){ imgStyle = "float:right;" + imgStyle; }
				if (img.parentElement.href)   { imgStyle = "cursor:pointer;" + imgStyle; }       
				strNewHTML    = '<span '+imgID+imgClass+imgTitle+' style="width:'+img.width+'px; height:'+img.height+'px;'+imgStyle+';'+'filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\''+img.src+'\', sizingMethod=\'scale\');"></span>';
				img.outerHTML = strNewHTML;
				i = i-1;
			}
		}
	}
}


//	Voir le contenu du panier
function panier_voir(){
	/*if($('panier_conteneur').style.display != 'none'){
		panier_masquer();
	}
	else{
		new Effect.Appear($('panier_conteneur'));
	}*/
	new Effect.Appear($('panier_conteneur'));
}

//	Masquer le contenu du panier
function panier_masquer(){
	new Effect.Fade($('panier_conteneur'));
}

//	Changer l'action du mode du panier
function next_action(elem){
	var naction;
	var action = elem.getAttribute('onclick');
	if(action != null){
		if(action.indexOf('voir') != -1){
			naction = action.replace('voir', 'masquer');
		}
		else if(action.indexOf('masquer') != -1){
			naction = action.replace('masquer', 'voir');
		}
		elem.setAttribute('onclick', naction);
	}
}

function reset_panier(){
	$('panier_titre').style.display = 'block';
	$('panier_contenant').className = 'panier_contenant';
}

//	Fonctions de zoom des images produit
function zoom_in(){
		var ih = parseInt($('image_full').getAttribute('height'));
		var iw = parseInt($('image_full').getAttribute('width'));
		var ratio = ih/iw;
		var gauche = parseInt($('handle').style.left);
		
		if(gauche <= 186){
			$('handle').style.left = gauche + 10 + 'px';
			$('image_full').setAttribute('width', (iw+20));
			$('image_full').setAttribute('height', ((iw+20)*ratio));
		}
	}

function zoom_out(){
	var ih = parseInt($('image_full').getAttribute('height'));
	var iw = parseInt($('image_full').getAttribute('width'));
	var ratio = ih/iw;
	var gauche = parseInt($('handle').style.left);
	
	if(gauche >= 10){
		$('handle').style.left = gauche - 10 + 'px';
		$('image_full').setAttribute('width', (iw-20));
	$('image_full').setAttribute('height', ((iw-20)*ratio));
	}
}

//	number_format(val, nb_dec 'dec_sep', 'mil_sep')
function number_format(a, b, c, d) {
 a = Math.round(a * Math.pow(10, b)) / Math.pow(10, b);
 e = a + '';
 f = e.split('.');
 if (!f[0]) {
  f[0] = '0';
 }
 if (!f[1]) {
  f[1] = '';
 }
 if (f[1].length < b) {
  g = f[1];
  for (i=f[1].length + 1; i <= b; i++) {
   g += '0';
  }
  f[1] = g;
 }
 if(d != '' && f[0].length > 3) {
  h = f[0];
  f[0] = '';
  for(j = 3; j < h.length; j+=3) {
   i = h.slice(h.length - j, h.length - j + 3);
   f[0] = d + i +  f[0] + '';
  }
  j = h.substr(0, (h.length % 3 == 0) ? 3 : (h.length % 3));
  f[0] = j + f[0];
 }
 c = (b <= 0) ? '' : c;
 return f[0] + c + f[1];
}

//	Retirer les espaces devant et derrière une chaine
function trim(str) {
    return str.replace(/^\s*|\s*$/g,"");
}

//	Affecter le bon code de devise avant changement
function set_devise(code){
	$('devise').value = code;
}

//	Affichage | masquage sur clic d'un élément en display none | block
function display_row(id){
	var elem = $(id);
	(elem.style.display == 'none') ? elem.style.display = 'block' : elem.style.display = 'none';
	//(elem.className == 'detail_commande') ? elem.className = 'detail_commande_on' : elem.className = 'detail_commande';
}

//	Vérifier que tous les éléments sont cochéss
function verif_chkbx(){
	var err = 0;
	var chk = document.getElementsByTagName('input');
	for(i = 0; i < chk.length; i++){
		if(chk[i].type == 'checkbox'){
			if(chk[i].checked != true){
				err += 1;
			}
		}
	}
	if(err > 0){
		alert("La commande n'est pas entièrement préparée");
		return false;
	}
	else{
		return true;
	}
}

//	Positionnement du div de confirmation
function position(id){
	var doc = $(id);
	var dH = parseInt(doc.style.height);
	var dW = parseInt(doc.style.width);
	
	if(navigator.appName == 'Microsoft Internet Explorer'){
		var winH = parseInt(document.documentElement.clientHeight);
		var winW = parseInt(document.documentElement.clientWidth);
	}
	else{
		var winH = parseInt(window.innerHeight);
		var winW = parseInt(window.innerWidth);
	}
	with (doc.style){
		position = 'absolute';
		display = 'block';
		zIndex = '1000';
		top = ((winH - dH) / 2) + 'px';
		left = ((winW - dW) / 2) + 'px';
	}
}

function hide_panier_choix(){
	if($('screen').style.display == 'block'){
		$('screen').style.display = 'none';
	}
}




function myPopupRelocate() {
  var scrolledX, scrolledY;
  if( self.pageYOffset ) {
    scrolledX = self.pageXOffset;
    scrolledY = self.pageYOffset;
  } else if( document.documentElement && document.documentElement.scrollTop ) {
    scrolledX = document.documentElement.scrollLeft;
    scrolledY = document.documentElement.scrollTop;
  } else if( document.body ) {
    scrolledX = document.body.scrollLeft;
    scrolledY = document.body.scrollTop;
  }

  var centerX, centerY;
  if( self.innerHeight ) {
    centerX = self.innerWidth;
    centerY = self.innerHeight;
  } else if( document.documentElement && document.documentElement.clientHeight ) {
    centerX = document.documentElement.clientWidth;
    centerY = document.documentElement.clientHeight;
  } else if( document.body ) {
    centerX = document.body.clientWidth;
    centerY = document.body.clientHeight;
  }
	
	var ecartH = parseInt($('taille-table').style.height);
	var ecartL = parseInt($('taille-table').style.width);
	
  var leftOffset = scrolledX + (centerX - ecartL) / 2;
  var topOffset = scrolledY + (centerY - ecartH) / 2;
	
	$("taille-table").style.position = "absolute";
  $("taille-table").style.top = topOffset + "px";
  $("taille-table").style.left = leftOffset + "px";
}

function displayTailles() {
  $('taille-table').style.display = "block";
  myPopupRelocate();
  document.body.onscroll = myPopupRelocate;
  window.onscroll = myPopupRelocate;
}

function stripslashes(str){
	str = str.replace(/\\'/g,'\'');
	str = str.replace(/\\"/g,'"');
	str = str.replace(/\\\\/g,'\\');
	str = str.replace(/\\0/g,'\0');
	return str;
}

//	Fonction pour les onglets
function multiClass(eltId) {
	arrLinkId = new Array('_0','_2');
	intNbLinkElt = new Number(arrLinkId.length);
	arrClassLink = new Array('current','ghost');
	strContent = new String()
	for (i=0; i<intNbLinkElt; i++) {
		strContent = "tabs"+arrLinkId[i];
		if ( arrLinkId[i] == eltId ) {
			document.getElementById(arrLinkId[i]).className = arrClassLink[0];
			document.getElementById(strContent).className = 'on content';
		} else {
			document.getElementById(arrLinkId[i]).className = arrClassLink[1];
			document.getElementById(strContent).className = 'off content';
		}
	}	
}