//<script>

/* Edit functions for AccesSite 2
 * Copyright 2004 Ragfijn Webservice
 * If you wish to use one or more of these functions,
 * Please contact Ragfijn at info@ragfijn.nl 
 */

// Variables: browser detect 
var IE = (document.all) ? true : false;
var N4 = (document.layers) ? true : false;
var N6 = (document.getElementById && !document.all) ? true : false;
// Track Searchable Select Boxes
var SearchableSelectBoxes = new Array;
// Track ResizeObjects
var ResizerObjects = new Array;
// Arrange edit-buttons
window.AsButtons = new Array();

function setContent(objId,html) {
  if (IE) document.all(objId).innerHTML = html;
  else if (N4) { with (document[objId].document) { open(); write(html); close(); } }
  else if (N6) document.getElementById(objId).innerHTML = html;
  else alert("Cannot change html in this browser!");
}
  
function getObject (id) {
  if (N4)      return document[id];
  else if (N6) return document.getElementById(id);
  else         return document.all(id);
}
  
function getStyle (id) {
  if (N4)      return document[id];
  else if (N6) return document.getElementById(id).style;
  else         return document.all(id).style;
}

function ResizeObject (id,x,y,w,h) {
  ResizerObjects[ResizerObjects.length] = [id,x,y,w,h];
  window.onresize = ResizeObjects;
  ResizeObjects();
}

function ResizeObjects () {
  var W = window.innerWidth;
  var H = window.innerHeight;
  if (!W) W = document.body.offsetWidth;
  if (!H) H = document.body.offsetHeight;
  if (W < 10) W = 10;
  if (H < 10) H = 10;
  //winheight -= 90;
  for (var i = 0; i < ResizerObjects.length; i++) {
    var id = ResizerObjects[i][0];
    var x  = ResizerObjects[i][1];
    var y  = ResizerObjects[i][2];
    var w  = ResizerObjects[i][3];
    var h  = ResizerObjects[i][4];
    w = (w ? w : W - x);
    h = (h ? h : H - y);
    if (w < 10) w = 10;
    if (h < 10) h = 10;
    if (IE) {
      document.all[id].style.width  = w;
      document.all[id].style.height = h;
      }
    else {
      document.getElementById(id).style.width  = w;
      document.getElementById(id).style.height = h;
      }
    }
}

/*    function SetBodyheight () {
      if (MO) {
        document.getElementById("DEVBody").style.height = winheight;
        }
      else {
        document.all.DEVBody.style.height = winheight;
        }
      }
*/

function OpenBlankSetting (mode) {
  if (mode) {
    var d = new Date;
    d.setTime(d.getTime() + 300 * 24 * 60 * 60 * 1000)
    setCookie("AS_config_OpenBlankSetting",mode,fixDate(d));
    }
  else {
    mode = getCookie("AS_config_OpenBlankSetting");
    }
  if (mode == "no") mode = false;
  else mode = true;
  return mode;
}

function OpenBlank (title,url,key,icon) {
  // See if current view is in config. If so:
  //   create new frame
  // Else open new window
  var notify = false;
  if ((typeof(url) + "").toLowerCase() == "object") {
    var box = url;
    url = box.value;
    box.selectedIndex = 0;
    if (/\/(new\/|copy\/)/.test(url)) { // "New" url
      key = newkey;
      title = newtitle;
      //$key   = "$type".$AS->GetNewId($type);
      //$title = $AS->GetText('new')." $typenames{$type}";
      }
    if (window.name == "config" && parent.MDIbase.keys[key]) {
      notify = "This object has already been opened. To re-open the object " +
               "(in another view) close the corresponding page first.";
      }
    }
  if (OpenBlankSetting()) {
    if (window.name == "config") {
      new parent.MDIFrame(title,url,key,icon);
      if (notify) {
        alert(notify);
        }
      }
    else {
      if (notify) {
        alert(notify);
        return false;
        }
      window.open(url);
      }
    }
  else {
    location.href = url;
    }
}

function InitStatus (form) {
  //if (form.name) { // Save all data in form
  //  window.AsEditStatus = {name: {}};
  //  }
  window.AsEditStatus = GetStatusString(form);
}

function GetStatusString (form) {
  var html = "";
  var tags = ["INPUT","SELECT","TEXTAREA"];
  for (var t = 0; t < tags.length; t++) {
    //alert(tags[t]);
    var objs = form.all.tags(tags[t])
    for (var o = 0; o < objs.length; o++) { 
      if (objs[o].id != "ExcludeUpdate") html += objs[o].outerHTML;
      //alert(objs[o].tagName + ": id=" + objs[o].id + " name=" + objs[o].name);
      }
    } 
  return html;
}

function CheckStatus (form) {
  if (window.AsEditStatus == GetStatusString(form)) {
    setContent("status","Saved");
    }
  else {
    setContent("status","Changed");
    }
}

/*
function OpenEditWin (url,name) {
  name = name.replace(/\W/g,"");
  // open or get window
  var win = window.open("",name,"fullscreen=no,resizable=yes," +
                        "height=620,width=780,left=10,top=10");
  // see if it already existed
  if (win.IsAsPopupEditWin) {
    // already open, only focus
    win.focus();
    }
  else {
    // navigate to url
    win.location.href = url;
    win.resizeTo(screen.availWidth-300,screen.availHeight);
    win.moveTo(300,0);
    }
}
*/

function CreateSearchableSelectBox (list,box) {
  var p = new Object;
  p.list = list;
  p.box = box;
  var n = SearchableSelectBoxes.length;
  SearchableSelectBoxes[n] = p;
  box.onkeyup = new Function("SearchSelectBox(" + n + ");");
}

function SearchSelectBox (n) {
  var p = SearchableSelectBoxes[n];
  var v = p.box.value;
  // search list
  for (var i = 0; i < p.list.options.length; i++) {
    if (p.list.options[i].text.substr(0,v.length).toLowerCase() == v.toLowerCase()) {
      p.list.selectedIndex = i;
      break;
      }
    }
}

/* Make this object oriented too
- one "DropdownBox" object, contents may differ
  (or make that all configurable: list/multi(with check)/multi level/sort
- hide underlying selectboxes,iframes and objects
- make sure fits on screen, else create scrollbars
- multibrowser
*/

function ShowDropDownList (id,oButton) {
  var oDiv = getObject(id); 
  if (oDiv.contains(event.srcElement)) return false;
  var oStyle = getStyle(id);
  if (oStyle.display == '') {
    oStyle.display='none';
    oButton.innerHTML='&#9660;';
    oDiv.releaseCapture();
    }
  else {
    oStyle.display='';
    oButton.innerHTML='&#9650;';
    oDiv.setCapture(false);
    }
}


function CheckListItem (box,item,mode,sepchar,callback) {
  // callback: some code to eval() when done (as event)
  if (!sepchar) sepchar = ",";
  //var box = document.form[name];
  var value = box.value;
  var regexp;
  eval("regexp = /\\b" + item + "\\b/");
  if (mode) { // add group
    if (!regexp.test(value)) {
      value += sepchar + item;
      }
    }
  else { // remove group
    value = value.replace(regexp,"");
    }
  eval("regexp = /(^"+sepchar+"|"+sepchar+"$)/g");
  value = value.replace(regexp,"");
  eval("regexp = /"+sepchar+sepchar+"/g");
  value = value.replace(regexp,sepchar);
  box.value = value;
  if (callback) {
    eval(callback);
    }
}

function RecordSelectValue2Check (form,name,value,multi,sepchar) {
  if (!sepchar) sepchar = ",";
  // uncheck all
  var array = getObject("dropdownbox_" + name + "0").all.tags("input");
  if (array.length == null) array = [array];
  for (var i = 0; i < array.length; i++) {
    if (array[i].type == 'checkbox') {
      array[i].checked = false;
      }
    }
  var single = (multi == "" || multi == "0");
  var r = new RegExp(sepchar);
  if (single && r.test(value)) {
    alert("Invalid value");
    }
  else if (value != "") {
    var id = value.split(sepchar);
    for (var i = 0; i < id.length; i++) {
      //alert ("form = " + form);
      if (id[i] && form["check" + name + id[i]]) { 
        form["check" + name + id[i]].checked = true;
        }
      }
    }
}

function RecordSelectCheck (form,name,id,mode,multi,sepchar) {
  if (!sepchar) sepchar = ",";

  var single = (multi == "" || multi == "0");
  var r = new RegExp(sepchar);
  if (single) {
    // uncheck all but selected
    var array = getObject("dropdownbox_" + name + "0").all.tags("input");
    if (array.length == null) array = [array];
    for (var i = 0; i < array.length; i++) {
      if (array[i].type == 'checkbox') {
        array[i].checked = false;
        }
      }
    // re-check box
    //alert("mode = " + mode);
    if (mode) {
      form["check" + name + id].checked = true;
      form[name].value = id;
      }
    else {
      form[name].value = "";
      }
    }
  else {
    CheckListItem (form[name],id,mode,sepchar);
    }
}

// This function is not used I think

function OrderList(list,index,to) {
  //var list = document.form.list;
  var total = list.options.length-1;
  if (index == -1) return false;
  if (to == +1 && index == total) return false;
  if (to == -1 && index == 0) return false;
  var items = new Array;
  var values = new Array;
  for (i = total; i >= 0; i--) {
    items[i] = list.options[i].text;
    values[i] = list.options[i].value;
    }
  for (i = total; i >= 0; i--) {
    if (index == i) {
      list.options[i + to] = new Option(items[i],values[i + to], 0, 1);
      list.options[i] = new Option(items[i + to], values[i]);
      i--;
      }
    else {
      list.options[i] = new Option(items[i], values[i]);
      }
    }
  list.focus();
  return true;
}

// Cookie script

// name - name of the cookie
// value - value of the cookie
// [expires] - expiration date of the cookie (defaults to end of current session)
// [path] - path for which the cookie is valid (defaults to path of calling document)
// [domain] - domain for which the cookie is valid (defaults to domain of calling document)
// [secure] - Boolean value indicating if the cookie transmission requires a secure transmission
// * an argument defaults when it is assigned null as a placeholder
// * a null placeholder is not required for trailing omitted arguments
function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}

// name - name of the desired cookie
// * return string containing value of specified cookie or null if cookie does not exist
function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}

// name - name of the cookie
// [path] - path of the cookie (must be same as path used to create cookie)
// [domain] - domain of the cookie (must be same as domain used to create cookie)
// * path and domain default if assigned null or omitted if no explicit argument proceeds
function deleteCookie(name, path, domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" + 
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

// date - any instance of the Date object
// * hand all instances of the Date object to this function for "repairs"
function fixDate(date) {
  var base = new Date(0);
  var skew = base.getTime();
  if (skew > 0)
    date.setTime(date.getTime() - skew);
}
