var cookienaam = "mijncvg";

var cookieduration = 60*60; //=60 minuten
var cookie1week =   8*86400; //=8 dagen
var cookie1year = 366*86400; //=1 jaar

var currentusercode = "";
var currentuserid = 0;
var currentuserownerid = 0;
var currentuserowneridcode = 0;
var currentallowedidcodes = "";
var currentlicencecode = 0;
var currentbedrijfsnaam = "";
var currentusernaam = "";
var currentdevice = "-1";

var currentonetab    = false;
var currentsingletab = false;

var pNiet    = 0;
var pKijk    = 1;
var pWijzig  = 2;
var pAlles   = 3;
var pAdmin   = 4;
var pDevelop = 5;

var permcount=100;
var perm = new Array();

function getcookie(name) {
  var result = null;
  var xmlHttp_cookie_get=GetXmlHttpObject();
  if (xmlHttp_cookie_get==null) {
    alert ("Browser does not support HTTP Request");
    return;
  }
  var url="./common/common_cookie_get.php";
  url+="?u="+currentuserid; //gebruiker-identificatie
  url+="&nam="+name;
  url+="&owner="+common_owneridcode;
  url+="&sid="+Math.random();
//getid("txt").innerHTML+="<br>{"+url+"}";
  xmlHttp_cookie_get.open("GET",url,false);
  xmlHttp_cookie_get.send(null);
  if (xmlHttp_cookie_get.status==200) {
    s=xmlHttp_cookie_get.responseText;
//getid("txt").innerHTML+='<br>'+s;
    result=s;
  }

/*
  var result = null;
  var mycookie = " "+document.cookie+";";
  var searchname = " "+name+"=";
  var startofcookie = mycookie.indexOf(searchname);
  var endofcookie;
  if (startofcookie!=-1) {
    startofcookie+=searchname.length; // skip past cookie name
    endofcookie=mycookie.indexOf(";",startofcookie);
    result=unescape(mycookie.substring(startofcookie,endofcookie));
  }
*/
  return result;
}

function setcookie(name,value,duration) {
  var result = null;
  var xmlHttp_cookie_set=GetXmlHttpObject();
  if (xmlHttp_cookie_set==null) {
    alert ("Browser does not support HTTP Request");
    return;
  }
  var url="./common/common_cookie_set.php";
  url+="?u="+currentuserid; //gebruiker-identificatie
  url+="&nam="+name;
  url+="&val="+value;
  url+="&dur="+duration;
  url+="&owner="+common_owneridcode;
  url+="&sid="+Math.random();
//getid("txt").innerHTML+="<br>{"+url+"}";
  xmlHttp_cookie_set.open("GET",url,false);
  xmlHttp_cookie_set.send(null);
  if (xmlHttp_cookie_set.status==200) {
    s=xmlHttp_cookie_set.responseText;
//getid("txt").innerHTML+='<br>'+s;
    result=s;
  }
  return result;
  
//  document.cookie=name+"="+value+"; max-age="+duration;
}

function getrealcookie(name) {
  var result = null;
  var mycookie = " "+document.cookie+";";
  var searchname = " "+name+"=";
  var startofcookie = mycookie.indexOf(searchname);
  var endofcookie;
  if (startofcookie!=-1) {
    startofcookie+=searchname.length; // skip past cookie name
    endofcookie=mycookie.indexOf(";",startofcookie);
    result=unescape(mycookie.substring(startofcookie,endofcookie));
  }
  return result;
}

function interpretcookie() {
  var r = false;
  var c = new String(getrealcookie(cookienaam));
  if (c!="null") {
    var a = new Array();
    a=c.split("@");
    currentusercode=a[0];
    currentuserid=a[1];
    currentuserownerid=a[2];
    currentuserowneridcode=a[3];
    currentallowedidcodes=a[4];
    currentlicencecode=a[5];
    currentbedrijfsnaam=URLDecode(a[6]);
    currentusernaam=URLDecode(a[7]);
    r=true;

    setrealcookie(cookienaam,c);
//    document.cookie=cookienaam+"="+c+"; max-age="+(60*60*24*8); //8 dagen
    
    var p = getpermission(currentuserid);
    for (var i=0; i<permcount; i++) {
      perm[i]=veldlijstWaardeDefault(p,i,0)-0;
      if ((perm[i]<0)||(perm[i]>5)) perm[i]=0;
    }
  }

  getdevicecookie();

  c = new String(getrealcookie(cookienaam+'_tab'));
  if (c!="null") {
    var a = new Array();
    a=c.split("@");
    currentonetab=((a[0]-0)==1);
    currentsingletab=((a[1]-0)==1);
  }

  return r;
}

function getdevicecookie() {
  var c = new String(getrealcookie(cookienaam+'_device'));
  if (c!="null") {
    var a = new Array();
    a=c.split("@");
    currentdevice=a[0];
  }
}

var xmlHttp_devicecookie;

function setdevicecookie() {
  xmlHttp_devicecookie=GetXmlHttpObject();
  if (xmlHttp_devicecookie==null) {
    alert ("Browser does not support HTTP Request");
    return;
  }
  var url="./common/common_devicecookie.php";
  url+="?cn="+cookienaam+'_device';
  url+="&cd="+currentdevice;
  url+="&dy="+92;
  url+="&sid="+Math.random();
//getid("txt").innerHTML+="<br>device{"+url+"}";
  xmlHttp_devicecookie.onreadystatechange=stateChanged_devicecookie;
  xmlHttp_devicecookie.open("GET",url,true);
  xmlHttp_devicecookie.send(null);
}

function stateChanged_devicecookie() {
  if (xmlHttp_devicecookie.readyState==4 || xmlHttp_devicecookie.readyState=="complete") {
    var r = xmlHttp_devicecookie.responseText;
//getid("txt").innerHTML+="<br>device-response{"+r+"}";
  }
}

var xmlHttp_tabcookie;

function settabcookie(cn,cot,cst) {
  if (cot) cot=1; else cot=0;
  if (cst) cst=1; else cst=0;
  xmlHttp_tabcookie=GetXmlHttpObject();
  if (xmlHttp_tabcookie==null) {
    alert ("Browser does not support HTTP Request");
    return;
  }
  var url="./common/common_tabcookie.php";
  url=url+"?cn="+cn+'_tab';
  url=url+"&ot="+cot;
  url=url+"&st="+cst;
  url=url+"&sid="+Math.random();
//getid("txt").innerHTML+="<br>tab{"+url+"}";
  xmlHttp_tabcookie.onreadystatechange=stateChanged_tabcookie;
  xmlHttp_tabcookie.open("GET",url,true);
  xmlHttp_tabcookie.send(null);
}

function stateChanged_tabcookie() {
  if (xmlHttp_tabcookie.readyState==4 || xmlHttp_tabcookie.readyState=="complete") {
    var r = xmlHttp_tabcookie.responseText;
//getid("txt").innerHTML+="<br>tab-response{"+r+"}";
  }
}

var xmlHttp_realcookie;

function setrealcookie(cn,cd) {
  xmlHttp_realcookie=GetXmlHttpObject();
  if (xmlHttp_realcookie==null) {
    alert ("Browser does not support HTTP Request");
    return;
  }
  var url="./common/common_devicecookie.php";
  url+="?cn="+cn;
  url+="&cd="+cd;
  url+="&dy="+8;
  url+="&sid="+Math.random();
//getid("txt").innerHTML+="<br>real{"+url+"}";
  xmlHttp_realcookie.onreadystatechange=stateChanged_realcookie;
  xmlHttp_realcookie.open("GET",url,true);
  xmlHttp_realcookie.send(null);
}

function stateChanged_realcookie() {
  if (xmlHttp_realcookie.readyState==4 || xmlHttp_realcookie.readyState=="complete") {
    var r = xmlHttp_realcookie.responseText;
//getid("txt").innerHTML+="<br>real-response{"+r+"}";
  }
}

/*
// ====================================================================
// URLEncode and URLDecode functions
//
// Copyright Albion Research Ltd. 2002
// http://www.albionresearch.com/
//
// You may copy these functions providing that
// (a) you leave this copyright notice intact, and
// (b) if you use these functions on a publicly accessible
// web site you include a credit somewhere on the web site
// with a link back to http://www.albionresarch.com/
//
// If you find or fix any bugs, please let us know at albionresearch.com
//
// SpecialThanks to Neelesh Thakur for being the first to
// report a bug in URLDecode() - now fixed 2003-02-19.
// ====================================================================
function URLEncode(plaintext) {
  // The Javascript escape and unescape functions do not correspond
  // with what browsers actually do...
  var SAFECHARS = "0123456789" + // Numeric
                  "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + // Alphabetic
                  "abcdefghijklmnopqrstuvwxyz" +
                  "-_.!~*'()"; // RFC2396 Mark characters
  var HEX = "0123456789ABCDEF";
  var encoded = "";
  for (var i = 0; i < plaintext.length; i++ ) {
    var ch = plaintext.charAt(i);
    if (ch == " ") {
      encoded += "+"; // x-www-urlencoded, rather than %20
    } else if (SAFECHARS.indexOf(ch) != -1) {
      encoded += ch;
    } else {
      var charCode = ch.charCodeAt(0);
      if (charCode > 255) {
        alert( "Unicode Character '"
             + ch
             + "' cannot be encoded using standard URL encoding.\n" +
               "(URL encoding only supports 8-bit characters.)\n" +
               "A space (+) will be substituted." );
        encoded += "+";
      } else {
        encoded += "%";
        encoded += HEX.charAt((charCode >> 4) & 0xF);
        encoded += HEX.charAt(charCode & 0xF);
      }
    }
  } // for
  return encoded;
}
*/

function URLDecode(encoded) {
  // Replace + with ' '
  // Replace %xx with equivalent character
  // Put [ERROR] in output if %xx is invalid.
  var HEXCHARS = "0123456789ABCDEFabcdef";
  var plaintext = "";
  var i = 0;
  if (encoded!=null)
  while (i < encoded.length) {
    var ch = encoded.charAt(i);
    if (ch == "+") {
      plaintext += " ";
      i++;
    } else if (ch == "%") {
      if (i < (encoded.length-2)
          && HEXCHARS.indexOf(encoded.charAt(i+1)) != -1
          && HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
        plaintext += unescape( encoded.substr(i,3) );
        i += 3;
      } else {
        alert( 'Bad escape combination near ...' + encoded.substr(i) );
        plaintext += "%[ERROR]";
        i++;
      }
    } else {
      plaintext += ch;
      i++;
    }
  } // while
  return plaintext;
}

