/*
 * Management of cookies
 */

// Set a cookie
function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
  ((expires) ? "; expires=" + expires.toGMTString() : "") +
  ((path) ? "; path=" + path : "") +
  ((domain) ? "; domain=" + domain : "") +
  ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}

// Get a cookie
function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf(prefix);
  if (begin == -1) return null;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1) end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}

// Delete a cookie
function deleteCookie(name, path, domain) {
  if (getCookie(name)) {
          document.cookie = name + "=" +
          ((path) ? "; path=" + path : "") +
          ((domain) ? "; domain=" + domain : "") +
          "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

	
	
function doNothing(){
	alert("Pas encore implémenté");
}


/*
 * Various useful scriplets
 */

// Move selected item from one listbox to another listbox
function switchItems(sourceList, targetList)
{
  for (i=sourceList.options.length -1; i!=-1; i--) {
    var e = sourceList.options[i];
    if (e.selected) {
      sourceList.options[i] = null;
      var j = targetList.options.length;
      e.selected = false;
      if(targetList.options[j-1].value=="-1"){
        targetList.options[j-1] = e;
      }else{
        targetList.options[j] = e;
      }
    }
  }
}

// Toggle the visibility of a DHTML object
function toggleVisibility(target) {
  obj = document.all[target];
  var hidden = (obj.style.display=='none') ;
  if (hidden) {
    obj.style.display = 'inline';
  } else {
    obj.style.display = 'none';
  }
}


// Trim a string
function trim(string) {
  return string.replace(/(^\s*)|(\s*$)/g,'');
}


// Open a popup window
function openPopup(url, width, height) {
  if (!width) { width=650; }
  if (!height) { height=450; }
  var topWindow = (screen.height - height)/2;
  var leftWindow = (screen.width - width) /2;
  var w = window.open(url, "popup", "toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width="+width+",height="+height+",top="+topWindow+",left="+leftWindow);
	w.focus();
  return w;
}

function openPopupForExcel(url, width, height) {
  if (!width) { width=650; }
  if (!height) { height=450; }
  var topWindow = (screen.height - height)/2;
  var leftWindow = (screen.width - width) /2;
  var w = window.open(url, "popup", "toolbar=no,location=no,directories=no,status=yes,menubar=yes,scrollbars=yes,resizable=yes,width="+width+",height="+height+",top="+topWindow+",left="+leftWindow);
	w.focus();
  return w;
}

// Open a popup window with a specific name - channah 15 02 05
function openPopupWithName(url, name, width, height) {
  if (!width) { width=650; }
  if (!height) { height=450; }
  var topWindow = (screen.height - height)/2;
  var leftWindow = (screen.width - width) /2;
  var w = window.open(url, name, "toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width="+width+",height="+height+",top="+topWindow+",left="+leftWindow);
	w.focus();
  return w;
}

// Increment/Decrement input text per unit +/- buttons
//var cnt = 0;
//function add() {cnt++;set();}
//function sub() {cnt--;set();}
//function set() {myform.count.value = cnt;} 

//goes with :
//<form name="myform">
//<input type="button" value="-"
//onclick="sub()" /><input type="input"
//value="0" size="3" name="count"
//onblur="set();" /><input type="button"
//value=" + " onclick="add()" />
//</form> 

function modValue(the_field, n){ 
     the_field.value = +the_field.value + n; 
     if (the_field.focus) the_field.focus(); 
   }
   
function addCallbackError(errorMsg){
	document.all.callbackError.innerText = errorMsg;
}

function addCallbackMessage(msg){
	document.all.callbackMessage.innerText = msg;
}
// Extract the keys from a map (value-pair array)
function getMapKeys(obj){
	var keys = [];
 	for (var property in obj)
    keys.push(property);
  	return keys;
}