//* FILE **********************************************************************
//*                                                                          **
//* NAME: defChamps.js                                                       **
//*                                                                          **
//* GOAL: JS associés à certains Champs ou Carac (functions_def_champs.php)  **
//*                                                                          **
//* FILE **********************************************************************

if (!Array.prototype.indexOf)
{
  Array.prototype.indexOf = function(elt /*, from*/)
  {
    var len = this.length;

    var from = Number(arguments[1]) || 0;
    from = (from < 0)
         ? Math.ceil(from)
         : Math.floor(from);
    if (from < 0)
      from += len;

    for (; from < len; from++)
    {
      if (from in this &&
          this[from] === elt)
        return from;
    }
    return -1;
  };
}

/*
function detectspecialkeys(e){
var evtobj=window.event? event : e
if (evtobj.altKey || evtobj.ctrlKey || evtobj.shiftKey)
alert("you pressed one of the 'Alt', 'Ctrl', or 'Shift' keys")
}
document.onkeypress=detectspecialkeys
*/

var currentKey = "";

document.onkeydown = KeyCheck;
document.onkeyup = KeyUncheck;

function KeyUncheck(e)
{
	currentKey = "";
}

function KeyCheck(e)
{
   var KeyID = (window.event) ? event.keyCode : e.keyCode;
   switch(KeyID)
   {
      case 16:
      currentKey = "Shift";
      break;       case 17:
      currentKey = "Ctrl";
      break;      case 18:
      currentKey = "Alt";
      break;      case 19:
      currentKey = "Pause";
      break;      case 37:
      currentKey = "Arrow Left";
      break;      case 38:
      currentKey = "Arrow Up";
      break;      case 39:
      currentKey = "Arrow Right";
      break;      case 40:
      currentKey = "Arrow Down";
      break;   }
}


var ns4 = (document.layers)? true:false;         //NS 4
var ie4 = (document.all)? true:false;         //IE 4
var dom = (document.getElementById)? true:false;   //NS 6 ou IE 5

function GetDivContent(ID)
{
	if (dom)
	{
		return document.getElementById(ID).innerHTML;
	}
	if (ie4)
	{
		return document.all[ID].innerHTML;
	}
	if (ns4)
	{
		return (eval('document.'+ID+'.document'));
	}
}

function SetDiv(ID,Content)
{
	if (dom)
	{
		document.getElementById(ID).innerHTML = Content;
		return;
	}
	if (ie4)
	{
		document.all[ID].innerHTML = Content;
		return;
	}
	if (ns4)
	{
		with (eval('document.'+ID+'.document')) {
		   open();
		   write(Content);
		   close();
		}
		return;
	}
}


var lastPlageVal = "";

//*****************************************************************************
//*                                                                          **
//* NAME: Champ PLAGE                                                        **
//* GOAL: Gestion des évenements sur un champ de type PLAGE                  **
//*                                                                          **
//*****************************************************************************
function ChampPlageOnClick(divId, val, actifHtm, inactifHtm, theForm, hiddenName)
{
	var inputValue = GetInputValue(theForm, hiddenName);

	if (inputValue != '')
  	var valeurs = inputValue.split(',');
	else
		var valeurs = new Array();

	if (lastPlageVal && (currentKey == 'Shift'))
	{
		if ((1*lastPlageVal) < (1*val))
		{
			min = parseInt(lastPlageVal);
			max = parseInt(val);
		}
		else
		{
			min = parseInt(val);
			max = parseInt(lastPlageVal);
		}
		for (var v=min; v<=max; v++)
		{
			if (v != lastPlageVal)
			{
      	tDivId = 'ac_preartvaleur_PROD_PLAGE_OPTIMISATION_'+ v;
				var htm = GetDivContent(tDivId);
	      var i = 0;
				i = valeurs.indexOf(''+v);
				if (i == -1)
				{
					htm = htm.replace("plage_inactif","plage_actif");
			    valeurs.push(v);
				}
				else
				{
					valeurs.splice(i,1);
					htm = htm.replace("plage_actif","plage_inactif");
				}
				SetDiv(tDivId, htm);
			}
		}
		currentKey = "";
	}
	else
	{
		var i = valeurs.indexOf(val);
		var htm = GetDivContent(divId);
		if (i == -1)
		{
			htm = htm.replace("plage_inactif","plage_actif");
	    valeurs.push(val);
		}
		else
		{
			valeurs.splice(i,1);
			htm = htm.replace("plage_actif","plage_inactif");
		}
		SetDiv(divId, htm);
	}

	lastPlageVal = val;
 	setInputValue(theForm, hiddenName, valeurs.join(","));

} // ChampPlageOnClick