var idTimeOut;
function addP(id, val, autoSubmit) {
	var obj = document.getElementById(id);
	if (parseInt(obj.value) > 0 || val > 0) {
		obj.value = parseInt(obj.value) + val;
	}
	if (isNaN(parseInt(obj.value))) obj.value = 0;
	if (autoSubmit) {
		clearTimeout(idTimeOut);
		idTimeOut = setTimeout('submitPanier()', 2000);
	}
}

function submitPanier() {
	document.getElementById('colonneDroiteForm').submit();
}

function swap(obj, src) {
	if (obj) {
		obj.src = src;
	}
}

/*********/
/* COVER */
/*********/
var popupCover = null;
var timeoutCover = null;
function ownWindowOpen(url, name, feature) {
	showCover();
	if (feature) {
		if (feature.indexOf('resizable') == -1) feature += ",resizable";
		if (feature.indexOf('scrollbars') == -1) feature += ",scrollbars";
		if (feature.indexOf('modal') == -1) feature += ",modal";
	} else {
		feature = "resizable,scrollbars,modal";
	}
	if (!name) name = 'POPUP';
	popupCover = window.open(url, name, feature);
	timeoutCover = window.setInterval('intervalCover()', 500);
	return false;
}
function intervalCover() {
	if (!popupCover || popupCover.closed) {
		window.clearInterval(timeoutCover);
		hideCover();
	}
}
function coverFocus(evt) {
    YAHOO.util.Event.stopEvent(evt);
    if (popupCover) popupCover.focus();
}
function showCover() {
    YAHOO.util.Dom.setStyle(document.body, "opacity", 0.5);
    document.body.style.backgroundColor = "#ddd";
    document.body.style.cursor = "wait";
    YAHOO.util.Event.addListener(document, 'click', coverFocus);
}
function hideCover() {
	YAHOO.util.Dom.setStyle(document.body, "opacity", "1");
	document.body.style.backgroundColor = "";
	document.body.style.cursor = "";
	YAHOO.util.Event.removeListener(document, 'click', coverFocus);
}



/********/
/* Ajax */
/********/
function getHTTPObject() {
  var xmlhttp = false;
  /* Compilation conditionnelle d'IE */
  /*@cc_on
  @if (@_jscript_version >= 5)
     try
     {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
     }
     catch (e)
     {
        try
        {
           xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch (E)
        {
           xmlhttp = false;
        }
     }
  @else
     xmlhttp = false;
  @end @*/
  /* on essaie de créer l'objet si ce n'est pas déjà fait */
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined')
  {
     try
     {
        xmlhttp = new XMLHttpRequest();
     }
     catch (e)
     {
        xmlhttp = false;
     }
  }
  return xmlhttp;
}

function ajax_searchProduitFromCodeinterne(cpt) {
	idRow = cpt;
	oHTTP = getHTTPObject();
	if (oHTTP) {
		oHTTP.onreadystatechange = ajax_retourSearchProduitFromCodeinterne;
		oHTTP.open("POST", SERVER_ROOT + "cmdExpressAjax.php", true);
		oHTTP.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		oHTTP.send("action=searchProduitFromCodeinterne&cpt=" + cpt + "&PRO_CODEINTERNE=" + document.getElementById('PRO_CODEINTERNE_'+cpt).value);
	}
}


function ajax_retourSearchProduitFromCodeinterne() {
	if (oHTTP.readyState != 4 || oHTTP.status != 200) return;
	var response = oHTTP.responseText;
	if (response != 'failure') {
		var tableau=response.split('|');
		var id     =tableau[0];
		var libelle=tableau[1];
		document.getElementById('PRO_LIBELLE_' + idRow).innerHTML = '<span id="PRO_LIBELLE_' + idRow + '">' + libelle + '</span>';
		if (id == '') {
			// si pas de produit correspondant au code interne, on enlève la checkbox de sélection
			document.getElementById('P_CHOOSER_' + idRow).checked = false;
			document.getElementById('P_CHOOSER_' + idRow).style.display = "none"
		} else {
			document.getElementById('P_CHOOSER_' + idRow).checked = true;
			document.getElementById('P_CHOOSER_' + idRow).style.display = "inline";
			document.getElementById('addP_' + idRow).value = id;
			document.getElementById('SUPP_' + idRow).innerHTML = '<input type="button" id="PRO_SUPPRIMER_' + idRow + '" class="submit supprimer" value="" name="boutonOk" onclick="deleteTrLigne(' + idRow + ')">';
		}
	}
}

function ajax_searchCodeClient() {
	oHTTP = getHTTPObject();
	if (oHTTP) {
		oHTTP.onreadystatechange = ajax_retourSearchCodeClient;
		oHTTP.open("POST", SERVER_ROOT + "authentificationAjax.php", true);
		oHTTP.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		oHTTP.send("action=searchCodeClient&codeClient=" + document.getElementById('CLI_NUMERO_LOG_1_V').value);
	}
}

function ajax_retourSearchCodeClient() {
	if (oHTTP.readyState != 4 || oHTTP.status != 200) return;
	var response = oHTTP.responseText;
	if (response != 'failure') {
		if (response == 0) {
			alert('Code client incorrect !');
		}
	}
}
