// all JS functions for this site

// choose element 'elm' from list with 'contact's.
// opens a pop-up window to take your pick out of something.
// noheadfoot disables the standard inclusion of header and footer images
// elm holds the name of the inputfield that should be updated with the key (id) of the record.
// This field is usually hidden.
//
// If for example this field is named "contact", then the popup also tries to
// update a field with the name "contacttext" with the description in the record
//
function choose($which, $elm, $and)
{
//  alert(document.form[$elm].value);
  window.open($which + 
                '.php?noheadfoot=1&elm=' + $elm + 
                '&search='  + document.form[$elm].value +
                '&and=' + $and,
        "choose",
        "toolbar=no,menubar=no,hotkeys=yes,scrollbars=yes,resizable=yes," +
        "dependent=yes,width=500,height=600,ScreenX=600,ScreenY=50,status=no,location=no");
}

function choose_purchase($which, $elm)
{
  $field = document.form[$which].value;
  window.open('choose-purchase.php?noheadfoot=1&elm=' + $elm + 
                '&search=' + document.form[$elm].value +
                '&contact=' + document.form[$field].value,
        "choose" + $which,
        "toolbar=no,menubar=no,hotkeys=yes,scrollbars=yes,resizable=yes," +
        "dependent=yes,width=500,height=600,ScreenX=500,ScreenY=50,status=no,location=no");
}

function popup_help($which)
{
  window.open('popup-help.php?noheadfoot=1&which=' + $which, 
        "Help",
        "toolbar=no,menubar=no,hotkeys=yes,scrollbars=yes,resizable=yes," +
        "dependent=yes,width=400,height=300,ScreenX=300,ScreenY=50,status=no,location=no");
}

function reboot_server($which, $name)
{
  isok = confirm("Are you REALLY sure you want to reboot " + $name + "?", 2);
  if (isok) {
    window.open("site-reboot-server.php?noheadfoot=1&id=" + $which,
          "Reboot",
          "toolbar=no,menubar=no,hotkeys=yes,scrollbars=yes,resizable=yes," +
          "dependent=yes,width=400,height=300,ScreenX=300,ScreenY=50,status=no,location=no");
  } 
}

function popup($which, $id)
{
  window.open("site-popup-" + $which + ".php?noheadfoot=1&" + $id,
        "Detail" + $which,
        "toolbar=no,menubar=no,hotkeys=yes,scrollbars=yes,resizable=yes," +
        "dependent=yes,width=500,height=600,ScreenX=500,ScreenY=50,status=no,location=no");
}

function cgi($which, $value)
{
  window.open("/cgi-bin/" + $which + "?" + $value, "cgi",
        "toolbar=no,menubar=no,scrollbars=yes,resizable=yes," +
        "width=500,height=600,left=600,top=50,status=no,location=no");
}

// find first editable field, and set focus to it
function FocusFirst() {
  if (document.form == undefined) return;
  if (document.form.elements == undefined) return;
  if (document.form.elements.length == undefined) return; // because select box with purchase in task should not have focus
  for (var i = 0; i < document.form.elements.length; i++) {
    if (document.form.elements[i].type == 'hidden') continue;
    if (document.form.elements[i].disabled) continue;
    if (document.form.elements[i].name == 'sub') continue;
    document.form.elements[i].focus();
    return;
  }
}

// isElfproef returns true if the given accountnumber succeeds the elfproef 
// algorithm.
function isElfproef(rekeningnummer)
{
  var len, sum, i;

  sum = 0;
  len = rekeningnummer.length;

  // each digit is multiplied by it's inversed offset and added to the
  // previous digit.
  for (i = 0; i < len; i++)
    sum += (rekeningnummer.charAt(i)-'0') * (len - i);
  // dividing the sum through 11 must not leave a value
  return ((sum % 11) == 0);
}

function checkBank(objname, msg)
{
  var field = objname;

  if (field.value.substr(0, 1) == 'P') return(true);
  if (isElfproef(field.value)) return(true);
  alert(msg);
  field.select();
  field.focus();
  return(false);
}

function checkPrice(objname)
{
  var field = objname;

  field.value = parseMoney(field.value, ".");
}

// menu functions
function MM_goToURL() { //v3.0
  var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
  for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v3.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

// functions for creating random passwords
function getRandomNum(lbound, ubound) {
  return (Math.floor(Math.random() * (ubound - lbound)) + lbound);
}

function getPassword(length) {
  var rc = "";
  var charSet = "23456789abcdefghijkmnopqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ";  

  for (var idx = 0; idx < length; ++idx) {
    rc = rc + charSet.charAt(getRandomNum(0, charSet.length));
  }
  return rc;
}

function newpass(field) {
  field.value = getPassword(8);
  return(false);
}

function restorepass(field, oldfield)
{
  field.value = oldfield.value;
  return(false);
}

function reload_page()
{
var sURL = unescape(window.location.pathname);
    window.location.reload(true);
}

// Courtesy Patrick Corcoran <patrick@taylor.org>.
//
function parseDec(val,places,sep) {

  // This function takes two arguments:
  //   (string || number)  val
  //      (integer)  places
  //       (string)  sep
  // val is the numeric string or number to parse
  // places represents the number of decimal
  // places to return at the end of the parse.
  // sep is an optional string to be used to separate
  // the whole units from the decimal units (default: '.')

  val = '' + val;
    // Implicitly cast val to (string)
  
  if (!sep) {
    sep = '.';
    // If separator isn't specified, then use a decimal point '.'
  }
  
  if (!places) { places = 0; }
  places = parseInt(places);
    // Make sure places is an integer
  
  if (!parseInt(val) && val.indexOf(sep) == -1) {
    // If val is null, zero, NaN, or not specified, then
    // assume val to be zero.  Add 'places' number of zeros after
    // the separator 'sep', and then return the value.  We're done here.
    val = '0';
    if (places > 0) {
      val += sep;
      while (val.substring((val.indexOf(sep))).length <= places) {
        val += '0';
      }
    }
    return val;
  }
  
  if ((val.indexOf('.') > -1) && (sep != '.')) {
    val = val.substring(0,val.indexOf('.')) + sep + val.substring(val.indexOf('.')+1);
      // If we're using a separator other than '.' then convert now.
  }
    
  if (val.indexOf(sep) > -1) {
    // If our val has a separator, then cut our value
    // into pre and post 'decimal' based upon the separator.
    pre = val.substring(0,val.indexOf(sep));
    post = val.substring(val.indexOf(sep)+1);
  } else {
    // Otherwise pre gets everything and post gets nothing.
    pre = val;
    post = '';
  }
  
  if (places > 0) {
    // If we're dealing with a decimal then...
    
    post = post.substring(0,(places+1));
      // We care most about the digit after 'places'
    
    if (post.length > places) {
      // If we have trailing decimal places then...
      
      //alert (parseInt(post.substring(post.length - 1)));

      if ( parseInt(post.substring(post.length - 1)) > 4 ) {
        post = '' + Math.round(parseInt(post) / 10);
        //post = '' + post.substring(0,post.length - 2) + (1/Math.pow(10,places));
        //post = ('' + post.substring(0,post.length - 2)) + (parseInt(post.substring(post.length - 1)) + 1);
      } else {
        post = '' + Math.round(parseInt(post));
      }
    }
    
    if (post.length > places) {
      post = '' + Math.round(parseInt(post.substring(0,places)));
    } else if (post.length < places) {
      while (post.length < places) {
        post += '0';
      }
    }
  
  } else {

    if (parseInt((post.substring(0,1))) > 4) {
      pre = '' + (parseInt(pre) + 1);
    } else {
      pre = '' + (parseInt(pre));     
    }
    post = '';
  }
  
  sep = (post.length > 0) ? sep : '';
    // Should we use a separator?

  if (pre.length == 0) {
    pre = '0';
  }

  val = pre + sep + post;
    // Rebuild val

  return val;
}

function parseMoney(val,sep) {

  // Specialized version of parseDec useful for
  // parsing money-related data.  Arguments:
  //   (string || number)  val
  //       (string)  sep
  // val is the monetary value to be parsed,
  // sep is an optional decimal separator (default: '.')
  
  return parseDec(val,2,sep);
}

function sepToDec(val,sep) {

  val = '' + val;

  if ((val.indexOf(sep) > -1) && (sep != '.')) {
    val = val.substring(0,val.indexOf(sep)) + '.' + val.substring(val.indexOf(sep)+1);
  }
  
  return val;
}

function decToSep(val,sep) {

  val = '' + val;
  sep = '' + sep;

  if ((val.indexOf('.') > -1) && (sep.length > 0)) {
    val = val.substring(0,val.indexOf('.')) + sep + val.substring(val.indexOf('.')+1);
  }

  return val;
}

function makeObject(){
  var x;

  if (window.XMLHttpRequest) {
    x = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    x = new ActiveXObject("Microsoft.XMLHTTP");
  }

  return x;
}

var request = makeObject();
var resultplace;

function launch(url, method, handler, resultarea) {
  request.open(''+method+'', ''+url+'');
  resultplace = resultarea;
  request.onreadystatechange = handler;
  request.send(null);
}

function parseInfo(){
  if(request.readyState == 1) {
//    document.getElementById(resultplace).innerHTML = 'Calling you...';
  }
  if(request.readyState == 4) {
    var answer = request.responseText;
    document.getElementById(resultplace).innerHTML = answer;
  }
}

function handleOriginate() {
  // needs: request
  //        resultplace
  // objects.

  if(request.readyState == 1) {
    document.getElementById(resultplace).innerHTML = 'Calling you...';
  }
  if(request.readyState == 4) {
    var answer = request.responseText;
    document.getElementById(resultplace).innerHTML = 'Calling other party...';
    setTimeout('document.getElementById(resultplace).innerHTML = \'&nbsp;\';', 5000);
  }
}

// floating window

