// single quote is "&#039;"
var navIE = (typeof (document.all) != "undefined");
var DOM = (document.getElementById) ? 1 : 0;
var vernav = parseInt(navigator.appVersion);
// example:
// eval ('document'+_all+_pl+'bS'+_pr+_attr+'.left = xS+24');
function layershow (id) {
   var str = 'document'+_all+_pl+id+_pr+_attr+'.visibility = "'+_show+'";'
//alert (str);
   eval (str);
}
function layerhide (id) {
   eval ('document'+_all+_pl+id+_pr+_attr+'.visibility = "'+_hide+'";');
}
function objhide (obj) {
   eval ('obj'+_attr+'.visibility = "'+_hide+'";');
}
function objshow (obj) {
   eval ('obj'+_attr+'.visibility = "'+_show+'";');
}
function movelayer (id, x, y) {
   eval ('document'+_all+_pl+id+_pr+_attr+'.left'+'='+x);
   eval ('document'+_all+_pl+id+_pr+_attr+'.top'+'='+y);
}
function moveobj (obj, x, y) {
   obj.left = x;
   obj.top = y;
}
function put_in_layer (obj, str) {
   if ("undefined" == typeof (obj) || null == obj) {
      return false;
   }
   if (navIE || DOM) {
      obj.innerHTML=str;
   } else {
      obj.document.write(str);
      obj.document.close();
   }
}
if (navIE) {
   _all = '.all';
   _attr = '.style';
   _show = 'visible';
   _hide = 'hidden';
   _offTop = '.offsetTop';
   _offLeft = '.offsetLeft';
   _anch = '.all';
   _pl = '["';
   _pr = '"]';
   _height = '.offsetHeight';
   _width = '.offsetWidth';
   _bgcolor = '.backgroundColor';
   _lforms = '.document.forms';
} else if (DOM) {
   _all = '.getElementById';
   _attr = '.style';
   _show = 'visible';
   _hide = 'hidden';
   _offTop = '.offsetTop';
   _offLeft = '.offsetLeft';
   _anch = '.anchors';
   _pl = '("';
   _pr = '")';
   _height = '.offsetHeight';
   _width = '.offsetWidth';
   _bgcolor = '.backgroundColor';
   _lforms = '.getElementsByTagName("form").namedItem';
} else {
   _all = '';
   _attr = '';
   _show = 'show';
   _hide = 'hide';
   _offTop = '.y';
   _offLeft = '.x';
   _anch = '.anchors';
   _pl = '["';
   _pr = '"]';
   _height = '.clip.height';
   _width = '.clip.width';
   _bgcolor = '.bgColor';
   _lforms = '.document.forms';
}
function aff(obj_name) {
   eval ('var obj = '+obj_name);
   if ('object' == typeof(obj)) {
      for (var i in obj) {
         alert (obj_name+"."+i+" = "+obj[i]);
      }
   } else {
      alert (obj_name+" = "+obj);
   }
}
function aff_obj(obj_name, obj) {
   var str = "";
   for (var i in obj) {
      str += obj_name+"."+i+" = "+obj[i]+"\n";
   }
   return str;
}
function aff_obj_doc(obj_name, obj) {
   var str = aff_obj(obj_name, obj);
   document.write(str.replace(/\n/g,"<br>\n"));
}
function aff_obj_alert(obj_name, obj) {
   alert(aff_obj(obj_name, obj));
}
function aff_str_doc(str_name, str) {
   document.write(str_name+"=\""+str.replace(/\n/g,"<br>\n")+"\"");
}
function aff_str_alert(str_name, str) {
   alert(str_name+"=\""+str+"\"");
}
function aff_doc(str_name, obj) {
   document.write(str_name+"="+obj+"<br>\n");
}
function aff_alert(str_name, obj) {
   alert(str_name+"="+obj+"\n");
}
function watch_prop_doc (obj, prop) {
obj.watch(prop,
   function (id,oldval,newval) {
      document.writeln("obj." + id + " changed from "
         + unescape(oldval) + " to " + unescape(newval)+"<br>")
      return newval;
   })
}
function watch_prop_alert (obj, prop) {
obj.watch(prop,
   function (id,oldval,newval) {
      alert("obj." + id + " changed from "
         + unescape(oldval) + " to " + unescape(newval))
      return newval;
   })
}
function chrono(name,act) {
// Launch, run and stop a named chronometre.
// 'Act' is "start"|"run"|"stop" ("stop" is default),
// user needs only "start" and "stop".
   var mes;
   if (act == "run") {
      if (window.chr[name].status == "run") {
         window.chr[name].chrono = ((new Date())-window.chr[name].start)/1000.;
         setTimeout("chrono('"+name+"','run')",500);
      }
   } else if (act == "start" ||
   		typeof (window.chr) == "undefined" ||
		typeof (window.chr[name]) == "undefined") {
      if (typeof (window.chr) == "undefined")
         window.chr = new Object;
      if (typeof (window.chr[name]) == "undefined")
         window.chr[name] = new Object;
      window.chr[name].start = new Date();
      window.chr[name].name = name;
      if (window.chr[name].status != "run") {
         window.chr[name].status = "run";
         setTimeout("chrono('"+name+"','run')",500);
      }
      return;
   } else {     // stop
      window.chr[name].chrono = ((new Date())-window.chr[name].start)/1000.;
      window.chr[name].status = "";
   }
   mes = "";
   for (var i in window.chr) {
      mes += window.chr[i].name+":"+window.chr[i].chrono+"s ";
   }
   window.defaultStatus = mes;
}
function echo(str) {
   document.write (str);
}
function afft(name,arr,lev) {
   if (typeof (lev) == "undefined") { lev = 0; }
   echo ("<table bgcolor=#aaaaaa cellpadding=2 border=1 cellspacing=0 size=1>"+
        "<tr><th colspan=2>"+name+"</th>");
   if (typeof (arr) == "object") {
      echo ("</tr>\n");
      for (var i in arr) {
         echo ("<tr><th>"+i+"</th><td bgcolor=#cccccc align=center>");
         if (false && typeof (arr[i]) == "object" && lev < 3) {
            aff_obj_doc (i,arr[i]);
         } else {
            echo (arr[i]);
         }
         echo ("</td></tr>\n");
      }
   } else {
      echo ("<td bgcolor=#cccccc>"+arr+"</td></tr>");
   }
   echo ("</table>\n");
}
function affwin(nom) {
   var wpopup = open ("", "popUp", "width=600,height=400,scrollbars");
   wpopup.document.close();
   wpopup.document.open();
   eval ('var obj='+nom+';');
   if (typeof (obj) == "object") {
      for (var i in obj) {
         wpopup.document.write (nom+'.'+i+'='+obj[i]+'<br>\n');
      }
   } else {
      wpopup.document.write (nom+'='+obj);
   }
}
function affw (nom, obj) {
   var wpopup = open ("", "popUp", "width=600,height=400,scrollbars");
   wpopup.document.close();
   wpopup.document.open();
   if (typeof (obj) == "object") {
      for (var i in obj) {
         wpopup.document.write (nom+'.'+i+'='+obj[i]+'<br>\n');
      }
   } else {
      wpopup.document.write (nom+'='+obj);
   }
}
function put_in_menu (form_menu, marray, itsel) {
//alert (aff_obj ('arr', marray));
//alert ('avant '+form_menu.options.length);
   for (var i=0; i < form_menu.options.length; i++) {
      form_menu.options[i] = null;
   }
   form_menu.options.length = 1;
//alert (aff_obj ('opts', form_menu.options));
//alert (form_menu.options.length);
   var k = 0;
   for (var i in marray) {
      var tmp = new Option (marray[i], i);
      form_menu.options[k++] = tmp;
//alert (aff_obj ('new opt', tmp));
      if (i == itsel) {
         form_menu.options[k-1].selected = true;
//alert (itsel+','+(k-1));
      }
   }
   if (form_menu.options.selectedIndex == -1)
      form_menu.options.selectedIndex = 0;
//alert (aff_obj ('new opts', form_menu.options));
}
function change_menu (the_form, marray, pere, fils, itsel) {
   i = the_form[pere].selectedIndex;
   put_in_menu (the_form[fils], marray[i], itsel);
}
function change_rmenu (the_form, rmarray, pere, listmenus, listitsel) {
   // rmarray is recursif array
   /* if rmarray[item]['aff'] and rmarray[item]['arr'] exist
   	and rmarray[item]['arr'] is an Object then rmarray[item]['arr']
        is rmarray of the next level and rmarray[item]['aff'] is shown in menu.
      else if rmarray[item] is Object then rmarray[item] is rmarray of
      	the next level and item is shown in menu.
      else
        rmarray is simple marray to be puted in menu.
   */
   // find marray of pere
   parray = rmarray;
   for (i in listmenus) {
      if (i == pere) {
         break;
      }
      ind = the_form[i].selectedIndex;
      val = the_form[i].options[ind].value;
      item = parray[val];
      if (typeof (item) == "object" &&
      	     typeof (item.arr) == "object" &&
             typeof (item.aff) == "string") {
         parray = item.arr;
      } else if (typeof (item) == "object") {
         parray = item;
      } else {
         return;
      }
   }
   // fulfil the content of the fils
   i = the_form[pere].selectedIndex;
   val = the_form[pere].options[i].value;
   marray = new Object ('');
   item = parray[val];
   if (typeof (item) == "object" &&
      	     typeof (item.arr) == "object" &&
             typeof (item.aff) == "string") {
      content = item.arr;
   } else if (typeof (item) == "object") {
      content = item;
   }
   
   for (i in content) {
      item = content[i];
      if (typeof (item) == "object" &&
      	     typeof (item.arr) == "object" &&
             typeof (item.aff) == "string") {
         marray[i] = item.aff;
      } else if (typeof (item) == "object") {
         marray[i] = i;
      } else {
         marray[i] = item;
      }
   }
   fils = listmenus[pere];
//alert ('pere='+pere+' typeof listmenus = '+typeof (listmenus)+' fils='+fils);
   sel = (typeof (listitsel[fils]) == "string" ? listitsel[fils] : '');
   put_in_menu (the_form[fils], marray, sel);
   
   // study if needed recursif call
   if (typeof (listmenus[fils]) == "undefined") {
      return;
   } else {
      // recursif call
      change_rmenu (the_form, rmarray, fils, listmenus, listitsel);
   }
}
function filldays (objForm, pref) {
   // pref is variable name contaning resulting date and used
   // as prefix for year, month and day variable names
   objYear = objForm[pref+'year'];
   objMonth = objForm[pref+'month'];
   objDay = objForm[pref+'day'];
   iyear = objYear.selectedIndex;
   imonth = objMonth.selectedIndex;
   iday = objDay.selectedIndex;
   if (iyear == 0) {
      objMonth.selectedIndex = 0;
      // strip out all days letting one void
      for (var i = 0; i < objDay.length; i++) {
         objDay.options[i] = null;
      }
      objDay.options[0] = new Option('');
      objDay.selectedIndex = 0;
      setdatevar (objForm, pref);
      return;
   }
   if (imonth == 0) {
      objMonth.selectedIndex = 1;
   }
   timefirst = new Date(objYear.options[iyear].text, objMonth.options[imonth].value,1);
   timelast = timefirst - 86400000;
   timelast = new Date(timelast);
   var daysInMonth = timelast.getDate();
   for (var i = 0; i < objDay.length; i++) {
      objDay.options[i] = null;
   }
   for (var i = 0; i < daysInMonth; i++) {
      objDay.options[i] = new Option(i+1);
   }
   objDay.selectedIndex = Math.min (iday, daysInMonth-1);
   setdatevar (objForm, pref);
}
function setdatevar (objForm, vname) {
   objYear = objForm[vname+'year'];
   iyear = objYear.selectedIndex;
   if (iyear == 0) {
      objForm[vname].value = '';
      return;
   }
   objMonth = objForm[vname+'month'];
   objDay = objForm[vname+'day'];
   if (typeof(objForm[vname+'hour']) != "undefined" &&
      typeof(objForm[vname+'min'])) {
      // set the hours and minutes
      Hour = " "+objForm[vname+'hour'].value;
      Min = ":"+objForm[vname+'min'].value;
   } else {
      Hour = Min = "";
   }
   imonth = objMonth.selectedIndex;
   iday = objDay.selectedIndex;
   var dateval = objYear.options[iyear].text+'-'+
      lpad (objMonth.options[imonth].value, 2, '0')+'-'+
      lpad (objDay.options[iday].text, 2, '0')+
      Hour+Min;
   objForm[vname].value = dateval;
//alert (vname+'='+objForm[vname].value);
   return;
}
function setdatemenu (objForm, vname, dateval) {
   // dateval is in YYYY-MM-DD format
   var re_date = new RegExp ('([0-9]{4})[^0-9]?([0-9]{1,2})[^0-9]?([0-9]{1,2})[^0-9]?');
   var year, month, day, i;
   if (re_date.test (dateval)) {
      year = RegExp.$1;
      month = RegExp.$2;
      day = RegExp.$3;
//alert (dateval+' -> d,m,y='+day+','+month+','+year);
   } else {
//alert (dateval+" isn't is in valid format YYYY-MM-DD");
      return;
   }
   objYear = objForm[vname+'year'];
   objMonth = objForm[vname+'month'];
   objDay = objForm[vname+'day'];
//affw ('m', objMonth);
//alert (month.replace (/^0+/, ""));
   objMonth.selectedIndex = parseInt (month.replace (/^0+/, ""));
   objDay.selectedIndex = parseInt (day.replace (/^0+/, ""))-1;
   // find out year index
   for (i = 0; i < objYear.options.length; i++) {
      if (objYear.options[i].text == year) {
         break;
      }
   }
   objYear.selectedIndex = i;
   dateval = year+'-'+
   	lpad (month, 2, '0')+'-'+
        lpad (day, 2, '0');
   objForm[vname].value = dateval;
//alert (vname+'='+objForm[vname].value);
   return;
}
function lpad (origstr, totlen) {
   origstr += '';
   if (origstr.length >= totlen) {
      return origstr;
   }
   if (arguments.length > 2) {
      var pad = arguments[2]+'';
   } else {
      var pad = ' ';
   }
   var retstr = origstr;
   var n = Math.floor ((totlen-origstr.length)/pad.length);
//alert ('pad="'+pad+'"; padlen='+pad.length+'; npad='+n);
   for (var i=0; i < n; i++) {
      retstr = pad + retstr;
   }
//alert ('subpad='+(totlen-origstr.length)%pad.length);
   return pad.substr (0, ((totlen-origstr.length)%pad.length)) + retstr;
}
function number_format (num) {
// string number_format (float num [, int decimals [, string dec_point [, string thousands_sep]]])
//alert ('num='+num+';');
   if (arguments.length > 1) {
      var decimals = parseInt (arguments[1]);
   } else {
      var decimals = 0;
   }
   if (arguments.length > 2) {
      var dec_point = arguments[2]+'';
   } else {
      var dec_point = '.';
   }
   if (arguments.length > 3) {
      var thousands_sep = arguments[3]+'';
   } else {
      var thousands_sep = '';
   }
//alert ('th_s='+thousands_sep+';');
   if (decimals == 0 && thousands_sep == '') {
      return Math.round (num)+'';
   }
   // separate units and fraction of absolute value
   var units=Math.floor (Math.abs (num));
   var fraction = Math.abs(num)-units;
//alert ('u='+units+'; f='+fraction+';');
   var factor = Math.pow (10,decimals);
   fraction = Math.round (fraction*factor);
//alert ('fr*factor='+fraction);
   if (fraction >= factor) {
      units++;
      fraction = fraction - factor;
   }
//alert ('fr final='+fraction);
   // format sign
   f_sign = '';
   if (num < 0 && (units > 0 || fraction > 0)) {
      f_sign = '-';
   }
   // format units
   var f_units = '';
   if ((units+0) > 1000 && thousands_sep.length > 0) {
      units += '';
      var n = units.length;
      var revi = n;
//alert ('n='+n+';');
      for (var i = 0; i < n; i++) {
//alert ('fu='+f_units+';');
         f_units += units.charAt(i);
         if ((revi%3) == 1 && revi > 1) {
            f_units += thousands_sep;
         }
         revi--;
      }
   } else {
      f_units = units;
   }
//alert ('fu='+f_units+';');
   // format fraction by lpad()
   return f_sign+f_units+dec_point+lpad (fraction, decimals, '0');
}
function eu2fr(fr) {
   fr = (fr+'').replace (",", ".");
   fr = parseFloat (fr);
   eu = fr*6.55957;
   return number_format (eu, 2);
}
function fr2eu(eu) {
   eu = (eu+'').replace (",", ".");
   eu = parseFloat (eu);
   fr = eu/6.55957;
   return number_format (fr, 2);
}
function add_option(table, val, visitext) {
// var name in form must be req[id_table] or req[listid_table]
//alert ('here add_option');
   var reid = new RegExp ('^req\[id[0-9]*_'+table+'\]\$');
   var relistid = new RegExp ('^req_listid[0-9]*_'+table);
   var inserted;
   var optmp = new Object;
   var optmp2 = new Object;
   var selitems = new Object;
   for (var indf=0; indf < document.forms.length; indf++) {
      f = document.forms[indf];
//affw ('f.'+indf, f.elements);
      for (var ifield = 0; ifield < f.elements.length; ifield++) {
//alert ('proceeding element N° '+ifield+' ('+f.elements[ifield].name+')');
         field = f.elements[ifield];
         if ((reid.test (field.name) || relistid.test (field.name)) &&
	     typeof (field.options) == 'object') {
//alert ('trying to insert '+val+':'+visitext);
            inserted = false;
	    var ops = field.options;
	    var n = ops.length;
            for (var iop=0; iop < n; iop++) {
	       if (ops[iop].selected) {
	          selitems[ops[iop].value] = true;
	       }
               if (!inserted && visitext <= ops[iop].text) {
		  inserted = true;
                  if (val == ops[iop].value) {
//alert (visitext+' already present');
                     break;
                  }
//alert (visitext +'<'+ field.options[iop].text);
//alert ('op['+iop+'].text,value='+ops[iop].text+','+ops[iop].value);
                  optmp.text = ops[iop].text;
		  optmp.value =  ops[iop].value;
                  ops[iop].value = val;
                  ops[iop].text = visitext;
		  ops[iop].selected = false;
               } else if (typeof (optmp.text) != 'undefined') {
                  optmp2.text = ops[iop].text;
		  optmp2.value = ops[iop].value;
                  ops[iop].value = optmp.value;
                  ops[iop].text = optmp.text;
		  ops[iop].selected = false;
                  optmp = optmp2;
               }
            }
//affw ('ops', ops);
            if (typeof (optmp.text) != 'undefined') {
//alert ('iop='+iop);
               var tmp = new Option (optmp.text, optmp.value);
               ops[iop] = tmp;
            } else if (iop == ops.length) {
//alert ('iopl='+iop);
               var tmp = new Option (visitext, val);
	       ops[iop] = tmp;
            }
	    // select items previously selected
	    for (var i = 0; i < ops.length; i++) {
	       if (selitems[ops[i].value]) {
	          ops[i].selected = true;
	       }
	    }
         }
      }
   }
}
function msleep (msec) {
//alert(msec);
   msec += 0;
   if (msec <= 0) {
      return;
   }
   var d = new Date();
   var n;
   while (1) {
      n = new Date();
      if ( n - d >= msec) {
         break;
      }
   }
//alert(n - d);
}
function dirname (path) {
   var separator = "/";
   if (arguments.length > 1) {
      separator = arguments[1]+'';
   }
   var arr = path.split(separator);
   arr.pop();
   return arr.join(separator);
}
function basename (path) {
   var separator = "/";
   if (arguments.length > 1) {
      separator = arguments[1]+'';
   }
   var arr = path.split(separator);
   return arr.pop();
}

