function noID() {
  return -1 ;
}

function isNavigatorMSIE() {
  return (navigator.appName == "Microsoft Internet Explorer" ) ;
}

function SimpleReplace(strbase, strtofind, strreplacing) {
  var re = new RegExp('(' + strtofind  + ')', "g") ;
  return strbase.replace(re, strreplacing) ;
}

function EmptyLookupList(listcontrol, doKeepNoIDElem) {
  len = listcontrol.length ;
  for ( i = len-1 ; i >= 0 ; i--) {
    opt = listcontrol.options.item(i) ;
    if (! (doKeepNoIDElem && opt.value == noID())) 
      listcontrol.remove(i) ;
  }
}

function getCorrectFormattedInputDate(dateinput) {
  len = dateinput.length ;
  if ( len > 0 ) {
    p = 0 ;
    i = 0 ;
    dc = new Array(3) ;
    dc[0] = 0 ;
    while (i < 3 && p < len) {
      c = (dateinput.charAt(p++)) ;
      if (c >= "0" && c <="9")
        dc[i] = dc[i] * 10 + parseInt(c) ; 
      else if (dc[i] > 0) {
        i++;
        dc[i] = 0 ; 
      }
    }
    isok = false ;
    if (i > 3 || i < 2 || (i == 2  && dc[i].length == 0)) {
      alert("Mauvais format pour la date (exemple de date valide: 17.5.04)")
      isok = false ;
    } else {
      if (dc[2] < 100)
        if (dc[2] < 30)
          dc[2] = 2000 + dc[2] ;
        else
          dc[2] = 1900 + dc[2] ;
      try {
        d = new Date(dc[2], dc[1] -1, dc[0]) ;
        if (d.getDate() != dc[0] || d.getMonth() != dc[1] -1 || d.getFullYear() != dc[2])
          isok = false ;
        else {
          isok = true ;
          return (dc[0] - dc[0] % 10) / 10 + "" + dc[0] % 10 + "." + (dc[1] -dc[1] % 10) / 10 + "" + dc[1] % 10 + "." + dc[2] ;
        }
      } catch (e) {
        isok = false ;
      }
      if (! isok) {
        alert("La date n'est pas valide (exemple de date valide: 17.5.04)") ;
      }
    }
  }
  return "" ;
}


function ResetLookupListWithID(lookupListID) {
  document.getElementById(lookupListID).selectedIndex = 0 ;
}

function OpenNewWindow(page,width,height) {
  OpenWindow('',page,width,height) ;
}

function OpenWindow(title, page, width,height) {
/*
alert("zzzbeg"+title);//zzz
  w = window.open() ;
*/
  window.open(page,title,'resizable=yes,width=' + width + ',height=' + height) ;
//alert("zzzend"+ title);//zzz
}

function ChangeDocumentLocation(url) {
  document.location = url ;
}
//lists
function getListSep() {
  return "," ;
}

function getElem(stringsList, index) {
  stringsArr = stringsList.split(getListSep()) ;
alert("zzzgetElem" + index) ;
  return stringsArr[index] ;
}                                   

function getPrevElem(stringsList, index) {
  return getElem(stringsList, index-1) ;
}

function getNextElem(stringsList, index) {
  return getElem(stringsList, index+1) ;
}

//XML functions
function GetSubNodeSimpleValue(node, subnodename){
//alert(node.getElementsByTagName(subnodename).item(0).childNodes.length);
  subnodechildren = node.getElementsByTagName(subnodename).item(0).childNodes ;
  if (subnodechildren.length == 0) {
    return "" ;
  } else {
    return subnodechildren.item(0).nodeValue ;
  }
}

function SignalXMLMessageFromServer(xmlmessagevalue) {
  alert(GetSubNodeSimpleValue(xmlmessagevalue,"message")) ;
}

//XMLHttpRequest 
// General functions
function XHRGetNewObject() {
  try {
	   return new XMLHttpRequest();   // Firefox,... 
	} catch(e1) {
	  try {
      return new ActiveXObject("Microsoft.XMLHTTP"); // Internet Explorer 
    } catch(e2) {
      return null ;
    }
  } 
}

function XHRSetXMLStateChangedFunction(xhr, funcDataFullyReceived) {
  xhr.onreadystatechange = 
    function() { 
      if (xhr.readyState == 4) 
        funcDataFullyReceived(xhr.responseXML); 
    } 
}

function XHRSendWithReactionFunction(xhr, url, reactionfunction, isasynchronous) {
  if (isasynchronous)
    XHRSetXMLStateChangedFunction(xhr, reactionfunction) ;
  xhr.open("GET", url, isasynchronous) ;
  xhr.send(null) ;
  if (! isasynchronous)
    reactionfunction(xhr.responseXML) ;
}

//DB Retrieve


function SendPageToServerWithReturnedXMLMessage(url) {
  xhr = XHRGetNewObject() ;
  XHRSendWithReactionFunction(xhr, url,
                              SignalXMLMessageFromServer,
                              false) ;
}
//End of XML-related functions

function noID() {
  return -1 ;
}

function isNavigatorMSIE() {
  return (navigator.appName == "Microsoft Internet Explorer" ) ;
}

function SimpleReplace(strbase, strtofind, strreplacing) {
  var re = new RegExp('(' + strtofind  + ')', "g") ;
  return strbase.replace(re, strreplacing) ;
}

function EmptyLookupList(listcontrol, doKeepNoIDElem) {
  len = listcontrol.length ;
  for ( i = len-1 ; i >= 0 ; i--) {
    opt = listcontrol.options.item(i) ;
    if (! (doKeepNoIDElem && opt.value == noID())) 
      listcontrol.remove(i) ;
  }
}

function getCorrectFormattedInputDate(dateinput) {
  len = dateinput.length ;
  if ( len > 0 ) {
    p = 0 ;
    i = 0 ;
    dc = new Array(3) ;
    dc[0] = 0 ;
    while (i < 3 && p < len) {
      c = (dateinput.charAt(p++)) ;
      if (c >= "0" && c <="9")
        dc[i] = dc[i] * 10 + parseInt(c) ; 
      else if (dc[i] > 0) {
        i++;
        dc[i] = 0 ; 
      }
    }
    isok = false ;
    if (i > 3 || i < 2 || (i == 2  && dc[i].length == 0)) {
      alert("Mauvais format pour la date (exemple de date valide: 17.5.04)")
      isok = false ;
    } else {
      if (dc[2] < 100)
        if (dc[2] < 30)
          dc[2] = 2000 + dc[2] ;
        else
          dc[2] = 1900 + dc[2] ;
      try {
        d = new Date(dc[2], dc[1] -1, dc[0]) ;
        if (d.getDate() != dc[0] || d.getMonth() != dc[1] -1 || d.getFullYear() != dc[2])
          isok = false ;
        else {
          isok = true ;
          return (dc[0] - dc[0] % 10) / 10 + "" + dc[0] % 10 + "." + (dc[1] -dc[1] % 10) / 10 + "" + dc[1] % 10 + "." + dc[2] ;
        }
      } catch (e) {
        isok = false ;
      }
      if (! isok) {
        alert("La date n'est pas valide (exemple de date valide: 17.5.04)") ;
      }
    }
  }
  return "" ;
}


function ResetLookupListWithID(lookupListID) {
  document.getElementById(lookupListID).selectedIndex = 0 ;
}

function OpenNewWindow(page,width,height) {
  OpenWindow('',page,width,height) ;
}

function OpenWindow(title, page, width,height) {
/*
alert("zzzbeg"+title);//zzz
  w = window.open() ;
*/
  window.open(page,title,'resizable=yes,width=' + width + ',height=' + height) ;
//alert("zzzend"+ title);//zzz
}

function ChangeDocumentLocation(url) {
  document.location = url ;
}
//lists
function getListSep() {
  return "," ;
}

function getElem(stringsList, index) {
  stringsArr = stringsList.split(getListSep()) ;
alert("zzzgetElem" + index) ;
  return stringsArr[index] ;
}                                   

function getPrevElem(stringsList, index) {
  return getElem(stringsList, index-1) ;
}

function getNextElem(stringsList, index) {
  return getElem(stringsList, index+1) ;
}

//XML functions
function GetSubNodeSimpleValue(node, subnodename){
//alert(node.getElementsByTagName(subnodename).item(0).childNodes.length);
  subnodechildren = node.getElementsByTagName(subnodename).item(0).childNodes ;
  if (subnodechildren.length == 0) {
    return "" ;
  } else {
    return subnodechildren.item(0).nodeValue ;
  }
}

function SignalXMLMessageFromServer(xmlmessagevalue) {
  alert(GetSubNodeSimpleValue(xmlmessagevalue,"message")) ;
}

//XMLHttpRequest 
// General functions
function XHRGetNewObject() {
  try {
	   return new XMLHttpRequest();   // Firefox,... 
	} catch(e1) {
	  try {
      return new ActiveXObject("Microsoft.XMLHTTP"); // Internet Explorer 
    } catch(e2) {
      return null ;
    }
  } 
}

function XHRSetXMLStateChangedFunction(xhr, funcDataFullyReceived) {
  xhr.onreadystatechange = 
    function() { 
      if (xhr.readyState == 4) 
        funcDataFullyReceived(xhr.responseXML); 
    } 
}

function XHRSendWithReactionFunction(xhr, url, reactionfunction, isasynchronous) {
  if (isasynchronous)
    XHRSetXMLStateChangedFunction(xhr, reactionfunction) ;
  xhr.open("GET", url, isasynchronous) ;
  xhr.send(null) ;
  if (! isasynchronous)
    reactionfunction(xhr.responseXML) ;
}

//DB Retrieve


function SendPageToServerWithReturnedXMLMessage(url) {
  xhr = XHRGetNewObject() ;
  XHRSendWithReactionFunction(xhr, url,
                              SignalXMLMessageFromServer,
                              false) ;
}
//End of XML-related functions

