function getRef() {
  var _ref = $("noBR")[0].innerHTML;
      _ref = _ref.substr(1, _ref.length - 2);

  return _ref;
}

function ascii_value (c)
{
	// restrict input to a single character
	c = c . charAt (0);

	// loop through all possible ASCII values
	var i;
	for (i = 0; i < 256; ++ i)
	{
		// convert i into a 2-digit hex string
		var h = i . toString (16);
		if (h . length == 1)
			h = "0" + h;

		// insert a % character into the string
		h = "%" + h;

		// determine the character represented by the escape code
		h = unescape (h);

		// if the characters match, we've found the ASCII value
		if (h == c)
			break;
	}
	return i;
}

function is_number(ch){
  // var asciiCh = String.charCodeAt(ch);
  var asciiCh = ascii_value(ch);
  return ((asciiCh >= 48) && (asciiCh <= 57)) ? true : false;
}

function affStock(reference, htmlIdPosition) {
  if (htmlIdPosition != null) {
    var pStockID = "stock-value" + htmlIdPosition;
  } else {
	var pStockID = "stock-value";
  }
  
  document.getElementById(pStockID).innerHTML = "<img id=\"stock-value-img\" src=\"/v3/images/4-1.gif\">";

  var ref      = reference; // debile
  var text     = "";
  var cssClass = "";

  var artifer = false;
  if (ref.substr(0, 1) == "a") { ref = "A" + ref.substr(1, ref.length - 1); artifer = true; }
  if ((ref.substr(0, 1) == "A") && (is_number(ref.substr(1, 1)))) { ref = "A" + ref.substr(1, ref.length - 1); artifer = true; }
  if (is_number(ref.substr(0, 1))) { ref = "A" + ref; artifer = true; }

  $.ajax({
    type:		"POST",
    url:		"/v3/stock/stock.php",
    data:		"ref=" + ref,
    success:	function(res){
        eval(res.toString());

        if (result.ref == "inconnue") {
          text = "Qt&eacute; inconnue,<br>contactez nous.<br>";
        } else {
          if (result.qte < 1) {
            text     = "en r&eacute;appro.";
            cssClass = "red";

            if (artifer) { text = "indisponible"; }

          } else {
            text     = result.qte + " pi&egrave;ce";
            if (result.qte > 1) text += "s";
            cssClass = "green";
          }

          // text = "<img src=\"/v3/images/icons/" + icon + "\"> Stock: <em>" + text + "</em>";
		  if (artifer) {
			text = "Derni&egrave;re(s) pi&egrave;ce(s): <em class=\"" + cssClass + "\">" + text + "</em> <span class=\"ref\">~ " + result.ref + "</span><br>";
		  } else {
            text = "<nobr>Stock: <em class=\"" + cssClass + "\">" + text + "</em> <span class=\"ref\">~ " + result.ref + "</span></nobr><br>";
		  }
        }

		$("img#stock-value-img").css("visibility", "hidden");
		$("img#stock-value-img").css("position", "absolute");
		
		if (htmlIdPosition != null) { $("#" + pStockID).addClass("stockClass"); }
		$("#" + pStockID).append(text);
    }
  });
}

function getStock() {
  affStock(getRef(), null);
}

function getStockByRef(reference) {
  affStock(reference, null);
}

function getStockByRefIdpos(reference) {
  affStock(reference, reference);
}

function affDelaiStock(delai) {
  var text = "Stock: <em class=\"orange\">d&eacute;lai " + delai + " jours</em>";
  $("#stock-value").append(text);
}