/**
* This library is intended to simplify client mechanics writing.
*
* It will encompass many little developement tricks such as text list management, ...
**/

/*
* adds a value to a comma separated list, ensuring value is unique. The list is the value content of the 
* provided text. The function returns the modified list. The list does not care about value ordering.
*/
function addToList(textlist, value)
{
   unescaped_value = value;
   // escapes all regex meta
   value = value.replace(/ /g, "\\s");
   value = value.replace(/\(/g, "\\(");
   value = value.replace(/\)/g, "\\)");
   value = value.replace(/\[/g, "\\[");
   value = value.replace(/\]/g, "\\]");
   value = value.replace(/\+/g, "\\+");
   value = value.replace(/\./g, "\\.");
   value = value.replace(/\*/g, "\\*");
   value = value.replace(/\^/g, "\\^");
   value = value.replace(/\$/g, "\\$");
   value = value.replace(/\?/g, "\\?");
   regex1 = "(^|,)" + value + "(,|$)";
   re = new RegExp(regex1);
   re.compile();
   if (!textlist.search(re))
   {
      textlist += "," + unescaped_value;
   }
   // removes first comma
   if (textlist.charAt(0) == ",")
      textlist = textlist.substring(1);
   self.status = "'" + textlist + "'";
   return textlist;
}

function delFromList(textlist, value)
{
   // escapes all regex meta
   value = value.replace(/ /g, "\\s");
   value = value.replace(/\(/g, "\\(");
   value = value.replace(/\)/g, "\\)");
   value = value.replace(/\[/g, "\\[");
   value = value.replace(/\]/g, "\\]");
   value = value.replace(/\+/g, "\\+");
   value = value.replace(/\./g, "\\.");
   value = value.replace(/\* /g, "\\*");
   value = value.replace(/\^/g, "\\^");
   value = value.replace(/\$/g, "\\$");
   value = value.replace(/\?/g, "\\?");
   reS = new RegExp("^" + value + "(,|$)");
   reM = new RegExp("," + value + ",");
   reE = new RegExp("," + value + "$");
   textlist = textlist.replace(reS, "");
   textlist = textlist.replace(reM, ",");
   textlist = textlist.replace(reE, "");
   self.status = "'" + textlist + "'";
   return textlist;
}

function isInList(textlist, value)
{
   regex1 = "(^|,)" + value + "(,|$)";
   re = new RegExp(regex1);
   if (textlist.match(re))
       return true;
   return false;
}

function toggleListState(textlist, checkboxId, value){
   var obj = document.getElementById(checkboxId);
   if (obj.checked == 1){
      textlist = addToList(textlist, value);
   }
   else{
      textlist = delFromList(textlist, value);
   }
   return textlist;
}

function togglePanel(panelId){
   aPanel = document.getElementById(panelId);
   if (aPanel.style.visibility == "hidden") {
      aPanel.style.visibility = "visible";
      aPanel.style.display = "block";
      document.images[panelId + "_button"].src = "images/icons/close.gif";
   }   
   else {
      aPanel.style.visibility = "hidden";
      aPanel.style.display = "none";
      document.images[panelId + "_button"].src = "images/icons/open.gif";
   }
}

var helpPopupId = 0;
var helpPopupsRegister = new Array();

function openHelpPopup(helpURL){
   var magicName = 'help' + helpPopupId;
   helpPopupId++;
   helpPopupsRegister[helpPopupsRegister.length] = window.open(helpURL, magicName, 'toolbar=0,statusbar=0,menubar=0,width=470,height=500,resizable=1,scrollbars=auto,dependant=1');
}

var stdPopupId = 0;
var stdPopupsRegister = new Array();

function openStdPopup(popupURL, base){
   var magicName = 'std' + stdPopupId;
   stdPopupId++;
   stdPopupsRegister[stdPopupsRegister.length] = window.open(base + "spopup.php?p=" + popupURL, magicName, 'toolbar=0,statusbar=0,menubar=0,width=470,height=500,resizable=1,scrollbars=auto,dependant=1');
}

function openStdPopupScrollable(popupURL, base){
   var magicName = 'std' + stdPopupId;
   stdPopupId++;
   stdPopupsRegister[stdPopupsRegister.length] = window.open(base + "spopup.php?p=" + popupURL, magicName, 'toolbar=0,statusbar=0,menubar=0,width=470,height=500,resizable=1,scrollbars=1,dependant=1');
}

function openImage(popupURL, base){
   var magicName = 'std' + stdPopupId;
   stdPopupId++;
   stdPopupsRegister[stdPopupsRegister.length] = window.open(base + "photo.php?" + popupURL, magicName, 'toolbar=0,statusbar=0,menubar=0,width=100,height=100,resizable=1,scrollbars=auto,dependant=1');
}

// some generic graphic control for rollover
isamap = new Object();
isamap[0] = "_df"
isamap[1] = "_ov"
isamap[2] = "_ot"
isamap[3] = "_dn"
var isimages = new Object();

function isimgact(id, act){
	if(document.images) document.images[id].src = eval( "isimages." + id + isamap[act] + ".src");
}

function deleteDialog(confirmText, successLocation){
	if (confirm(confirmText)){
	   document.location.href = successLocation;
	}
}

