var OneRowHeight = 0;
function textAreaDynamicHeight(textArea){
    if (OneRowHeight == 0) 
        OneRowHeight = textArea.style.height;
    var rows = textArea.value.split('\n');
    nRows = -1;
    for (i = 0; i < rows.length; i++) {
        if (rows[i].length >= textArea.cols) 
            nRows += Math.floor(rows[i].length / textArea.cols);
    }
    nRows += rows.length;
    if (nRows > 1) {
        textArea.rows = nRows+1;
        textArea.style.height = "auto";
    }
    else 
        if (nRows == 1) {
            textArea.rows = 1;
        }
        else 
            if (nRows == 0) {
                textArea.rows = 1;
                textArea.style.height = OneRowHeight;
            }
}



 function boogleElement(id){
  for(i=0;i<arguments.length;++i){
    if(document.getElementById(arguments[i])){
      if(document.getElementById(arguments[i]).style.display == "none"){
        document.getElementById(arguments[i]).style.display = "block";
      } else {
        document.getElementById(arguments[i]).style.display = "none";
      }
    }
  }
  return true;
}
function switchImgSrc(id, src1, src2){
  if(document.getElementById(id).src==src1)document.getElementById(id).src=src2;
  else document.getElementById(id).src = src1;

  return true;
}

function openBlankPage(page){
if(page != '')window.open(page);
}
function refresh(){
 // window.location.href = window.location.href;
}
function refreshParent(){
  window.opener.location.href = window.opener.location.href;
}
/***************************************************************************************************************/
function printIt() {
  if (typeof(window.print) != 'undefined'){
  window.print();
  }
}

/***************************************************************************************************************/
function hideElement(id){
  for(i=0;i<arguments.length;++i){
    if(document.getElementById(arguments[i])){
      document.getElementById(arguments[i]).style.display = "none";
    }
  }
  return false;
}
/***************************************************************************************************************/
function boogleDisableElement(id){
  for(i=0;i<arguments.length;++i){
    if(document.getElementById(arguments[i])){
      if(document.getElementById(arguments[i]).disabled){
        document.getElementById(arguments[i]).disabled = false;
      } else {
        document.getElementById(arguments[i]).disabled = true;
      }
    }
  }
  return true;
}

/***************************************************************************************************************/

function disableArea(id){
  for(i=0;i<arguments.length;++i){
    if(document.getElementById(arguments[i])){
      document.getElementById(arguments[i]).setAttribute("readonly","readonly");
    }
  }
  return false;

}
/***************************************************************************************************************/

function showElement(id){
  for(i=0;i<arguments.length;++i){
    if(document.getElementById(arguments[i])){
      document.getElementById(arguments[i]).style.display = "";
    }
  }
  return true;
}

 /***************************************************************************************************************/
function unASCIIChars( s )
{
  s = s.replace('+','%2B');
  s = s.replace('/','%2F');
  s = s.replace('@','%40');
  s = s.replace('*','%2A');
 return s;
}


function encode(id){

  if(!document.getElementById(id)) return '';
  else return unASCIIChars(escape(document.getElementById(id).value));
}


 /***************************************************************************************************************/

function setHtml(id,value,type){
  if(type=='a') {
    document.getElementById(id).innerHTML = document.getElementById(id).innerHTML + value;
  } else if(type=='-a') {
    document.getElementById(id).innerHTML = value + document.getElementById(id).innerHTML;
  } else {
    document.getElementById(id).innerHTML = value;
  }
  return true;
}
 /***************************************************************************************************************/
function setEditor(param){
 text = encode(param);
 window.open("ajax.php?action=25&id=" + param + "&text=" + text,"pop1","width=600,height=600");

 }


function resizePlus(param){
document.getElementById(param).rows += 3;
}
function resizeMinus(param){
document.getElementById(param).rows -= 3;
}

 /***************************************************************************************************************/
 function getEditor(id,editor){
   getEditorContent(editor);
   window.opener.setValue(id,getValue(editor+'_content'));
   window.close();
 }
 /***************************************************************************************************************/
 function getEditorContent(parametre){
    edoc = document.getElementById(parametre).contentDocument;
    document.getElementById(parametre+'_content').value = edoc.body.innerHTML
  }
 /***************************************************************************************************************/   
function getHtml(id){
  if(document.getElementById(id)) {
    returnval = document.getElementById(id).innerHTML;
  } else {
    returnval = '';
  }
  return returnval;
}
/***************************************************************************************************************/
function getActionAndTime(text){
  d = new Date()
  d =d.toLocaleString();
  return d + ' : ' + text + '<br />';


}
/***************************************************************************************************************/

function setValue(id, value, type){

   if(type=='a') {
    document.getElementById(id).value = document.getElementById(id).value + value;
  } else if(type=='-a') {
    document.getElementById(id).value = value + document.getElementById(id).value;
  } else {
    document.getElementById(id).value = value;
  }


 return true;
}
/***************************************************************************************************************/

function getValue(id){
 if(document.getElementById(id)) returnval = document.getElementById(id).value;
 else returnval = '';
 return returnval;
}

/***************************************************************************************************************/
function clearValue(id){
  for(i=0;i<arguments.length;++i){
    if(document.getElementById(arguments[i])){
      document.getElementById(arguments[i]).value = '';
    }
  }
  return true;
}

/***************************************************************************************************************/
function fromSelectToSelect(from,to){
  to = document.getElementById(to);
  toLength = to.options.length
  fromIndex = from.selectedIndex;
  fromValue = from.options[fromIndex].value;
  fromText = from.options[fromIndex].text;
  flag = 0;
  for(i = 0 ; i < toLength ; ++i){
    if(to.options[i].value == fromValue) flag++;
  }
  if(flag == 0) {
    to.options[toLength] = new Option(fromText,fromValue);
    return true;
  } else {
    return false;
  }


}
 /***************************************************************************************************************/
function deleteFromSelect(from){
  fromIndex = from.selectedIndex;
  ret = from.options[fromIndex].value;
  from.options[fromIndex]=null;
  return ret;
}
 /***************************************************************************************************************/
function addToSelect(sel, value, label){
  to = document.getElementById(sel);
  toLength = to.options.length

  to.options[toLength] = new Option(label,value);
}
/***************************************************************************************************************/
function menuSwitch(menu, way)
{
 // Init
 menu = document.getElementById(menu);
 var menumax = menu.length -2;
 var menusel = menu.selectedIndex;

 // Debordement
 if ((menusel < 0) || (menusel < 1 && way == -1) || (menusel > menumax && way == 1)) { return false; }

 // Permutation
 tmpopt = new Option( menu.options[menusel+way].text, menu.options[menusel+way].value );

 menu.options[menusel+way].text = menu.options[menusel].text;
 menu.options[menusel+way].value = menu.options[menusel].value;
 menu.options[menusel+way].selected = true;

 menu.options[menusel].text = tmpopt.text;
 menu.options[menusel].value = tmpopt.value;
 menu.options[menusel].selected = false;
 return true;
}
/***************************************************************************************************************/

function doAjaxQuery(action,param){
  if(window.XMLHttpRequest) xhr_object = new XMLHttpRequest();
  else if(window.ActiveXObject) xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
  else return(false);

  xhr_object.open("GET", '/ajax.php?action=' + action + '&' + param, false);
  xhr_object.send(null);
  
  /*xhr_object.open("POST", '/ajax.php');
  xhr_object.send('action=' + action + '&' + param, false);*/
 

  if(xhr_object.readyState == 4) {    
    return unescape(xhr_object.responseText);
  } else {
    alert("error");
  }
}
/***************************************************************************************************************/

function doAjaxQuery2(action,param,target,functionName){
  setHtml(target,'<img src="./pictures/loading.gif" height=25 />');
    var xhr;
    try { xhr = new XMLHttpRequest(); }
    catch(e)
    {
      xhr = new ActiveXObject(Microsoft.XMLHTTP);
    }

    xhr.onreadystatechange  = function()
    {

    if(xhr.readyState  == 3){
    //setHtml(target,unescape(xhr.responseText),'a');
    }
    if(xhr.readyState  == 4)
    {
         if(xhr.status  == 200){

         setHtml(target,'');
         setHtml(target,unescape(xhr.responseText),'a');
         
         if(functionName) {eval(functionName); }
         }
         else {}
    }
    }

   xhr.open("GET", '/ajax.php?action=' + action + '&' + param, true);
   xhr.send(null);



}

/***************************************************************************************************************/

function str_pad (input, pad_length, pad_string, pad_type)
{
  input = String (input);
  pad_string = pad_string != null ? pad_string : " ";
  if (pad_string.length > 0)
  {
    var padi = 0;
    pad_type = pad_type != null ? pad_type : "STR_PAD_RIGHT";
    pad_length = parseInt (pad_length);
    switch (pad_type)
    {
      case "STR_PAD_BOTH":
        input = str_pad (input
                       , input.length + Math.ceil ((pad_length - input.length) / 2.0)
                       , pad_string, "STR_PAD_RIGHT");
     // break;  // kein break!
      case "STR_PAD_LEFT":
        var buffer = "";
        for (var i = 0, z = pad_length - input.length; i < z; ++i)
        {
          buffer += pad_string.charAt(padi); // [padi] IE 6.x bug
          if (++padi == pad_string.length)
            padi = 0;
        }
        input = buffer + input;
        break;
      default:
        for (var i = 0, z = pad_length - input.length; i < z; ++i)
        {
          input += pad_string.charAt(padi);
          if (++padi == pad_string.length)
            padi = 0;
        }
        break;
    }
  }
  return input;
}


/***************************************************************************************************************/
var positionMoveElement;

function moveElement(element){
 if(document.getElementById) {
  haut+=1;
  window.document.getElementById(element).style.top =haut;
  if(haut>200) { clearInterval(positionMoveElement); }
 }
}

/***************************************************************************************************************/

function verifLength(champ)
{
  len = champ.value.split("\n").length;

  if(len <10 ) len = 10;
  if(len > 35 )   len =35;
  champ.rows = len;
}
/***************************************************************************************************************/

function searchMember(inputField,  selectField){
  //document.getElementById(selectField).options.length = 0;
  document.getElementById(selectField).innerHTML = null;
  param = 'type=nickname&value=' +  encode(inputField);
  result = doAjaxQuery('047',param);
  if(result != ''){
   optGroup = document.createElement('optgroup');
   optGroup.label = "Nickname";
   document.getElementById(selectField).appendChild(optGroup);
    addToSelect(selectField, 0, "Choose a member");
    arrResult = result.split('#');
    for(i=0 ; i<arrResult.length; ++i){
      rec = arrResult[i].split('|');
      addToSelect(selectField, rec[0], rec[1]);
    }
  }
  param = 'type=email&value=' +  encode(inputField);
  result = doAjaxQuery('047',param);
  if(result != ''){
   optGroup = document.createElement('optgroup');
   optGroup.label = "Email";
   document.getElementById(selectField).appendChild(optGroup);
    addToSelect(selectField, 0, "Choose a member");
    arrResult = result.split('#');
    for(i=0 ; i<arrResult.length; ++i){
      rec = arrResult[i].split('|');
      addToSelect(selectField, rec[0], rec[1]);
    }
  }
}


function searchMail(inputField,  selectField){
  //document.getElementById(selectField).options.length = 0;
  document.getElementById(selectField).innerHTML = null;
  param = 'type=nickname&value=' +  encode(inputField);
  result = doAjaxQuery('048',param);
    addToSelect(selectField, 0, "Choose a member");
  if(result != ''){
   optGroup = document.createElement('optgroup');
   optGroup.label = "Nickname";
   document.getElementById(selectField).appendChild(optGroup);
    arrResult = result.split('#');
    for(i=0 ; i<arrResult.length; ++i){
      rec = arrResult[i].split('|');
      addToSelect(selectField, rec[0], rec[1]);
    }
  }
  param = 'type=email&value=' +  encode(inputField);
  result = doAjaxQuery('048',param);
  if(result != ''){
   optGroup = document.createElement('optgroup');
   optGroup.label = "Email";
   document.getElementById(selectField).appendChild(optGroup);
     arrResult = result.split('#');
    for(i=0 ; i<arrResult.length; ++i){
      rec = arrResult[i].split('|');
      addToSelect(selectField, rec[0], rec[1]);
    }
  }
}







function insertAtCursor(area, text) {
  var input = document.getElementById(area);
  input.focus();
  /* pour l'Explorer Internet */
  if(typeof document.selection != 'undefined') {
    /* Insertion du code de formatage */
    var range = document.selection.createRange();
    var insText = range.text;
    range.text =  text;
    /* Ajustement de la position du curseur */
    range = document.selection.createRange();
    if (insText.length == 0) {
      range.move('character', -text.length);
    } else {
      range.moveStart('character', text.length   );
    }
    range.select();
  }
  /* pour navigateurs plus récents basés sur Gecko*/
  else if(typeof input.selectionStart != 'undefined')
  {
    /* Insertion du code de formatage */
    var start = input.selectionStart;
    var end = input.selectionEnd;
    var insText = input.value.substring(start, end);
    input.value = input.value.substr(0, start) +  text + input.value.substr(end);
    /* Ajustement de la position du curseur */
    var pos;
    if (insText.length == 0) {
      pos = start + text.length;
    } else {
      pos = start +  insText.length + text.length;
    }
    input.selectionStart = pos;
    input.selectionEnd = pos;
  }
  /* pour les autres navigateurs */
  else
  {
     setHtml(area,text,'a');
  }
}
