function popupcentree(page,largeur,hauteur,options)
{
	var top=(screen.height-hauteur)/2;
	var left=(screen.width-largeur)/2;
	window.open(page,"","top="+top+",left="+left+",width="+largeur+",height="+hauteur+","+options);
}

function hideButton(button)
{
  	document.getElementById(button+'Button').style.display = 'none';
  	document.getElementById(button+'Text').style.display = 'block';
	imgAnim = $('#'+button+'Text > img:eq(0)');
	setTimeout("imgAnim.attr('src', imgAnim.attr('src'))", 100);
  	return true;
}
function showButton(button)
{
  	document.getElementById(button+'Button').style.display = 'block';
  	document.getElementById(button+'Text').style.display = 'none';
  	return true;
}

   var keyStr = "ABCDEFGHIJKLMNOP" +
                "QRSTUVWXYZabcdef" +
                "ghijklmnopqrstuv" +
                "wxyz0123456789+/" +
                "=";

   function encode64(input) {
      var output = "";
      var chr1, chr2, chr3 = "";
      var enc1, enc2, enc3, enc4 = "";
      var i = 0;

      do {
         chr1 = input.charCodeAt(i++);
         chr2 = input.charCodeAt(i++);
         chr3 = input.charCodeAt(i++);

         enc1 = chr1 >> 2;
         enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
         enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
         enc4 = chr3 & 63;

         if (isNaN(chr2)) {
            enc3 = enc4 = 64;
         } else if (isNaN(chr3)) {
            enc4 = 64;
         }

         output = output +
            keyStr.charAt(enc1) +
            keyStr.charAt(enc2) +
            keyStr.charAt(enc3) +
            keyStr.charAt(enc4);
         chr1 = chr2 = chr3 = "";
         enc1 = enc2 = enc3 = enc4 = "";
      } while (i < input.length);

      return output;
   }

   function decode64(input) {
      var output = "";
      var chr1, chr2, chr3 = "";
      var enc1, enc2, enc3, enc4 = "";
      var i = 0;

      // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
      var base64test = /[^A-Za-z0-9\+\/\=]/g;
      if (base64test.exec(input)) {
         alert("There were invalid base64 characters in the input text.\n" +
               "Valid base64 characters are A-Z, a-z, 0-9, '+', '/', and '='\n" +
               "Expect errors in decoding.");
      }
      input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

      do {
         enc1 = keyStr.indexOf(input.charAt(i++));
         enc2 = keyStr.indexOf(input.charAt(i++));
         enc3 = keyStr.indexOf(input.charAt(i++));
         enc4 = keyStr.indexOf(input.charAt(i++));

         chr1 = (enc1 << 2) | (enc2 >> 4);
         chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
         chr3 = ((enc3 & 3) << 6) | enc4;

         output = output + String.fromCharCode(chr1);

         if (enc3 != 64) {
            output = output + String.fromCharCode(chr2);
         }
         if (enc4 != 64) {
            output = output + String.fromCharCode(chr3);
         }

         chr1 = chr2 = chr3 = "";
         enc1 = enc2 = enc3 = enc4 = "";

      } while (i < input.length);

      return output;
   }

function warningSession(timeout, url)
{
	result = confirm(sessionText);
	if(result)
	{
		//Fais la requete AJAX
		makeRequest(url);
		setTimeout("warningSession("+timeout+", '"+url+"')", timeout);
	}
}


var http_request = false;

function makeRequest2(url, champ) {

    http_request = false;

    if (window.XMLHttpRequest) { // Mozilla, Safari,...
        http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType) {
            http_request.overrideMimeType('text/html');
            // See note below about this line
        }
    } else if (window.ActiveXObject) { // IE
        try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
        }
    }

    if (!http_request) {
        alert('Giving up :( Cannot create an XMLHTTP instance');
        return false;
    }
    http_request.open('GET', url, false);
    http_request.send(null);

	  if (http_request.status != 200)
	  {
	    r = "There was a problem with the request:";
	    alert(r + "\n\n" + http_request.statusText);
	  }
	  else
	  {
	    document.getElementById(champ).innerHTML = http_request.responseText;
	  }
}

function makeRequest(url) {

    http_request = false;

    if (window.XMLHttpRequest) { // Mozilla, Safari,...
        http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType) {
            http_request.overrideMimeType('text/xml');
            // See note below about this line
        }
    } else if (window.ActiveXObject) { // IE
        try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
        }
    }

    if (!http_request) {
        alert('Giving up :( Cannot create an XMLHTTP instance');
        return false;
    }
    http_request.onreadystatechange = alertContents;
    http_request.open('GET', url, true);
    http_request.send(null);

}

function alertContents() {
return;
    if (http_request.readyState == 4) {
        if (http_request.status == 200) {
            alert(http_request.responseText);
        } else {
            alert('There was a problem with the request.');
        }
    }
}

function alertContents2(http_request, champ)
{
  if (http_request.readyState == 4)
  {
    if (http_request.status == 200)
    {
      tdHtml = document.getElementById(champ);
      tdHtml.innerHTML = http_request.responseText;
    }
    else
    {
      alert('There was a problem with the request.');
    }
  }
}

function displayTableauProduitDynaHighlight(id, nomClasse)
{
	ligne = document.getElementById(id);
	if(ligne)
	{
		ligne.className = nomClasse;
	}
}

function gotoAnchor(num)
{
	var currentLocation = window.location.href;
	currentAnchor = currentLocation.indexOf('#');
	if(currentAnchor >= 0)
	{
		currentLocation = currentLocation.substring(0, currentAnchor);
	}
	window.location.href = currentLocation+'#'+num;
}
function convertInToMm(champ, index)
{
	ent = parseInt($('input[name='+champ+'_in'+index+']').val());
	num = parseInt($('input[name='+champ+'_inNum'+index+']').val());
	den = parseInt($('input[name='+champ+'_inDen'+index+']').val());

	val = 0;
	ok = false;
	if(num > 0 && den > 0)
	{
		val += num/den;
		ok = true;
	}
	if(ent > 0)
	{
		val += ent;
		ok = true;
	}
	if(ok)
	{
		valMM = Math.round(val * 100 * 25.4)/100;
		$('input[name='+champ+'_mm'+index+']').val(valMM);
	}
	else
	{
		$('input[name='+champ+'_mm'+index+']').val('');
	}
}

function convertInUniqueToMm(champ, index)
{
	inVal = $('input[name='+champ+'_in'+index+']').val();
	regExp = new RegExp("^([0-9]{1,3}|[0-9]{1,2}/[0-9]{1,2}|[0-9]{1,3} [0-9]{1,2}/[0-9]{1,2})?$");
	isOk = regExp.test(inVal);
	if(isOk)
	{
		regExp1 = new RegExp("^[0-9]{1,3}$");
		regExp2 = new RegExp("^[0-9]{1,2}/[0-9]{1,2}$");
		regExp3 = new RegExp("^[0-9]{1,3} [0-9]{1,2}/[0-9]{1,2}$");
		if(regExp3.test(inVal))
		{
			ent = parseInt(inVal.substring(0, inVal.indexOf(' ')));
			num = parseInt(inVal.substring(inVal.indexOf(' ')+1, inVal.indexOf('/')));
			den = parseInt(inVal.substring(inVal.indexOf('/')+1));
		}
		else if(regExp2.test(inVal))
		{
			ent = 0;
			num = parseInt(inVal.substring(0, inVal.indexOf('/')));
			den = parseInt(inVal.substring(inVal.indexOf('/')+1));
		}
		else if(regExp1.test(inVal))
		{
			ent = parseInt(inVal);
			num = 0;
			den = 0;
		}
		else
		{
			ent = 0;
			num = 0;
			den = 0;
		}
		val = 0;
		ok = false;
		if(num > 0 && den > 0)
		{
			val += num/den;
			ok = true;
		}
		if(ent > 0)
		{
			val += ent;
			ok = true;
		}
		if(ok)
		{
			valMM = Math.round(val * 100 * 25.4)/100;
			$('input[name='+champ+'_mm'+index+']').val(valMM);
		}
		else
		{
			$('input[name='+champ+'_mm'+index+']').val('');
		}
	}
	else
	{
		$('input[name='+champ+'_mm'+index+']').val('');
	}

}

function convertMmToIn(champ, index)
{
	valMm = parseFloat($('input[name='+champ+'_mm'+index+']').val());

	val = 0;
	ok = false;
	if(valMm > 0)
	{
		val = valMm * 0.0393700787;
		ok = true;
	}
	if(ok)
	{
		fractItems = FractionItems(val);
		$('input[name='+champ+'_in'+index+']').val(fractItems[0]);
		if(fractItems[1] > 0)
		{
			$('input[name='+champ+'_inNum'+index+']').val(fractItems[1]);
			$('input[name='+champ+'_inDen'+index+']').val(fractItems[2]);
		}
		else
		{
			$('input[name='+champ+'_inNum'+index+']').val('');
			$('input[name='+champ+'_inDen'+index+']').val('');
		}
	}
	else
	{
		$('input[name='+champ+'_in'+index+']').val('');
		$('input[name='+champ+'_inNum'+index+']').val('');
		$('input[name='+champ+'_inDen'+index+']').val('');
	}
}
function convertMmToInUnique(champ, index)
{
	valMm = parseFloat($('input[name='+champ+'_mm'+index+']').val());

	val = 0;
	ok = false;
	if(valMm > 0)
	{
		val = valMm * 0.0393700787;
		ok = true;
	}
	if(ok)
	{
		fractItems = FractionItems(val);
		if(fractItems[1] > 0)
		{
			newVal = fractItems[0]+' '+fractItems[1]+'/'+fractItems[2];
		}
		else
		{
			newVal = fractItems[0];
		}
		$('input[name='+champ+'_in'+index+']').val(newVal);
	}
	else
	{
		$('input[name='+champ+'_in'+index+']').val('');
	}
}

function FractionItems(value)
{
	precisionPo = 32;
	entier = parseInt(value);
	reste = value - entier;
	if(reste == 0)
	{
		return new Array(entier, 0, 0);
	}
	h = Math.round(reste * precisionPo);
	if(h == precisionPo)
	{
		return new Array(entier+1, 0, 0);
	}
	if(h == 0)
	{
		return new Array(entier, 0, 0);
	}
	b = precisionPo;
	divOk = true;
	while(divOk)
	{
		if(h%2 == 0 && b%2 == 0)
		{
			h = h/2;
			b = b/2;
		}
		else
		{
			divOk = false;
		}
	}
	if(entier > 0)
	{
		return new Array(entier, h, b);
	}
	else
	{
		return new Array(0, h, b);
	}
}
