//////// DHTML Popup functions
var imgPath = "data/images/";
var currentDialog = "dhtmlpopup";
var arrHistory = new Array();
var nowDraggingDialog = false;
var dlgObject = {};
var offset_x = 0;
var offset_y = 0;
var tempx = 0;
var tempy = 0;
var isIE = (navigator.userAgent.indexOf("MSIE ") > -1);
var isOP = (navigator.userAgent.indexOf("Opera") > -1 || window.opera);
var isAOL = (navigator.userAgent.indexOf("AOL ") > -1);
var isSafari = (navigator.userAgent.indexOf("Safari/") > -1 && navigator.userAgent.indexOf("Chrome") == -1);
var isChrome = (navigator.userAgent.indexOf("Safari/") > -1 && navigator.userAgent.indexOf("Chrome") > -1);
var isNS = (!isIE && !isSafari && !isChrome && navigator.userAgent.indexOf("Mozilla/") > -1);
var popWidth = 0;
var popHeight = 0;

if (!isNS) isNS = (!isIE && !isSafari && !isChrome && navigator.userAgent.indexOf("Gecko") > -1);

function createDialog(dlgName,sTitle,sContent,iWidth,iHeight) {
  if (!dlgName) dlgName = currentDialog;

  var closeImgWidth = 36; // Determines max width of title aswell
  var closeImgHeight = 32 // Determines the height of title, the positioning of the content and the close image aswell
  var sstyle = {
    popup:             'position:absolute;float:left;height:' + iHeight + 'px;width:' + iWidth + 'px;' +
                       'cursor:default;left:0px;top:0px;display:none;padding:0px 0px 0px 0px;' +
                       'margin:0px 0px 0px 0px;border:1px solid;z-index:10;background:navy ' +
                       'url(' + imgPath + 'title.png) repeat-y);' ,
    popup_title:       'background:white url(' + imgPath + 'title.png) repeat-x;z-index:10;' +
                       'padding:0px 0px 0px 0px;margin:0px 0px 0px 0px;' ,
    title:             'text-align:left;position:relative;z-index:11;text-weight:bold;' +
                       'padding:0px 0px 0px 0px;margin:0px 0px 0px 0px;width:' +
                       (iWidth-closeImgWidth) + 'px;height:' + closeImgHeight + 'px;' +
                       'font-size:14pt;cursor:default;' ,
    title_span:        'position:relative;top:2px;left:4px;' ,
    close:             'float:right;position:relative;top:-' + closeImgHeight +
                       'px;right:0px;z-index:12;padding:0px 0px 0px 0px;margin:0px 0px 0px 0px;' ,
    content_container: 'position:relative;height:' + (iHeight-closeImgHeight-2) + 'px;width:' +
                       (iWidth-2) + 'px;left:0px;top:' +
                       ((isIE || isChrome || isSafari)?('-' + (closeImgHeight+1)):"1") +
                       'px;z-index:10;padding:0px 0px 0px 0px;margin:0px 0px 0px 0px;' ,
    content:           'width:' + (iWidth-2) + 'px;height:' + (iHeight-closeImgHeight-2) +
                       'px;padding:0px 0px 0px 0px;margin:0px 0px 0px 0px;overflow:auto;'
  }
  // Build the window. Set colors in a stylesheet.
  var html = '<div id="' + dlgName + '" style="' + sstyle["popup"] + '" onmousedown="toFront(this.id);" onmouseup="stopDrag()" onselectstart="return false;">' +
             '  <div id="' + dlgName + '_popup_title" align="left" style="' + sstyle["popup_title"] + '" onmousedown="initDrag(event);">' +
             '    <div id="' + dlgName + '_title" style="' + sstyle["title"] + '" onmousedown="this.style.cursor=' + "'move'" + ';" onmouseup="this.style.cursor=' + "'default'" + ';">' +
             '      <span style="' + sstyle["title_span"] + '">' + sTitle + '</span>' +
             '    </div>' +
             '    <div id="' + dlgName + '_close" style="' + sstyle["close"] + '">' +
             '      <img src="' + imgPath + 'closei.png" width="' + closeImgWidth + '" ' +
             '           height="' + closeImgHeight + '" onclick="closeDialog()" ' +
             '           onmouseover="this.src=' + "'" + imgPath + 'closea.png' + "'" + ';" ' +
             '           onmouseout="this.src=' + "'" + imgPath + 'closei.png' + "'" + ';">' +
             '    </div>' +
             '  </div>' +
             '  <div id="' + dlgName + '_content_container" style="' + sstyle["content_container"] + '">' +
             '    <div id="'+ dlgName + '_content" style="' + sstyle["content"] + '">' +
             sContent +
             '    </div>' +
             '  </div>' +
             '</div>';
  document.write(html);
  currentDialog = dlgName;
  popWidth = iWidth+"px";
  popHeight = iHeight+"px";

}
function ieDocument(){
  return ((document.documentElement)?document.documentElement:document.body);
}
function dragDrop(e){
  if (isIE&&nowDraggingDialog&&event.button == 1){
    dlgObject[currentDialog].style.left = tempx+event.clientX-offset_x+"px";
    dlgObject[currentDialog].style.top = tempy+event.clientY-offset_y+"px";
  }
  else if (nowDraggingDialog){
    dlgObject[currentDialog].style.left = tempx+e.clientX-offset_x+"px";
    dlgObject[currentDialog].style.top = tempy+e.clientY-offset_y+"px";
  }
}
function initDrag(e){
  tempx = parseInt(dlgObject[currentDialog].style.left,10);
  tempy = parseInt(dlgObject[currentDialog].style.top,10);
  offset_x = (isIE)? event.clientX : e.clientX;
  offset_y = (isIE)? event.clientY : e.clientY;
  nowDraggingDialog=true;
  dlgObject[currentDialog].onmousemove = dragDrop;
}
function toFront(dlgName) {
  dlgObject[dlgName] = document.getElementById(dlgName);
  if (currentDialog != dlgName) {
    dlgObject[dlgName].style.zIndex++;
    document.getElementById(dlgName + "_popup_title").style.zIndex++;
    document.getElementById(dlgName + "_title").style.zIndex++;
    document.getElementById(dlgName + "_close").style.zIndex++;
    document.getElementById(dlgName + "_content_container").style.zIndex++;
    document.getElementById(dlgName + "_content").style.zIndex++;
  }
  currentDialog = dlgName;
  arrHistory.push(dlgName);
  dlgObject[dlgName].focus();
}
function showDialog(dlgName) {
  dlgObject[dlgName] = document.getElementById(dlgName);
  if (currentDialog != dlgName) {
    dlgObject[dlgName].style.zIndex++;
    document.getElementById(dlgName + "_popup_title").style.zIndex++;
    document.getElementById(dlgName + "_title").style.zIndex++;
    document.getElementById(dlgName + "_close").style.zIndex++;
    document.getElementById(dlgName + "_content_container").style.zIndex++;
    document.getElementById(dlgName + "_content").style.zIndex++;
  }
  currentDialog = dlgName;
  arrHistory.push(dlgName);
  centerDialog(dlgName);
  dlgObject[dlgName].style.display="block";
  dlgObject[dlgName].focus();
}
function closeDialog(){
  dlgObject[currentDialog].style.display="none";
  arrHistory.pop();
  currentDialog = arrHistory.pop();
}
function stopDrag(){
  nowDraggingDialog = false;
  dlgObject[currentDialog].onmousemove = null;
}
function centerDialog(dlgName) {
  var screenWidth = (isIE)? ieDocument().clientWidth : window.innerWidth-20;
  var screenHeight = (isIE)? ieDocument().clientHeight : window.innerHeight-20;

  if (!dlgObject[dlgName].style.width) dlgObject[dlgName].style.width = popWidth;
  if (!dlgObject[dlgName].style.height) dlgObject[dlgName].style.height = popHeight;
  dlgObject[dlgName].style.left = ((screenWidth-parseInt(dlgObject[dlgName].style.width,10))/2)+"px";
  dlgObject[dlgName].style.top = ((screenHeight-parseInt(dlgObject[dlgName].style.height,10))/2)+"px";
}
function updateDialog(dlgName,sContent) {
  if (!dlgName) dlgName = currentDialog;
  document.getElementById(dlgName+"_content").innerHTML=sContent;
}
function updateTitle(dlgName,sTitle) {
  if (!dlgName) dlgName=currentDialog;
  document.getElementById(dlgName+"_title").innerHTML=sTitle;
}
function getFileContent(content) {
   var txt = content.toLowerCase();
   var x = txt.indexOf("<body");
   var y = content.length;
   var o = "";

   if (x == -1) x = txt.indexOf("<html");
   if (x == -1) x = txt.indexOf("<xml");
   if (x > -1) {
     x = txt.indexOf(">", x);
     y = txt.lastIndexOf("</body>");
     if (y == -1) y = txt.lastIndexOf("</html>");
     if (y == -1) y = txt.lastIndexOf("</xml>");
     if (y == -1) y = content.length;
   }
   return wipeScripts(content.slice(x + 1, y));
}
function wipeScripts(o) {
  var i,j,scriptFragment="<script[^>]*>([\\S\\s]*?)<\/script>";
  var matchAll = new RegExp(scriptFragment, "img");

  while (o.match(matchAll)) {
    i = o.indexOf("<script");
    j = o.indexOf("<\/script>")+9;
    o = o.substring(0,i)+o.substring(j);
  }
  return o;
}

//////// Ajax functions
var Ajax = {};
var Post = {};

Ajax.Request = function(method, action, query, callback, tagID) {
  this.tagID = tagID;
  this.callbackMethod = callback;
  try {
    this.request = new XMLHttpRequest();
    if (this.request.overrideMimeType) {
      this.request.overrideMimeType('text/xml');
    }
  } catch(e) {
    try {
      this.request = new ActiveXObject("Msxml2.XMLHTTP");
    } catch(e) {
      try {
        this.request = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {
        alert("Procedure afgebroken. Uw browser ondersteunt geen AJAX.\n\nProcedure terminated. Your browser does not support AJAX.");
        return false;
      }
    }
  }
  this.request.onreadystatechange = function() { Ajax.checkReadyState(); };
  if (method.toLowerCase() == 'get') { action = action + "?" + query; }
  this.request.open(method, action, true);
  this.request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  this.request.send(query);
};

Ajax.checkReadyState = function() {
  switch (this.request.readyState)  {
    case 1:
      break;
    case 2:
      break;
    case 3:
      break;
    case 4:
      try {
        this.callbackMethod(this.request.responseXML.documentElement, this.tagID);
      } catch (e) {
        document.getElementById(this.tagID).innerHTML =
          '<p>Er heeft zich een onbekende fout voorgedaan bij ' +
          'het verzenden van het formulier.<br />' +
          '<br />Het bericht is niet verstuurd.<hr /></p>';
      }
  }
};

function preSend(form) {
  form.Email.value = form.Email.value.toLowerCase();
  form.SafetyCode.value = form.SafetyCode.value.toUpperCase();
  window.scroll(0,0);
}

Post.Send = function(form, tagID) {
  preSend(form);
  var query = Post.buildQuery(form);
  Ajax.Request(form.method, form.action, query, Post.OnResponse, tagID);
};

Post.Express = function(obj, handler, tagID) {
  var method="post", query="", key;

  for (key in obj) {
    query += ((query != "")? "&" : "") + key + "=" + obj[key];
  }
  Ajax.Request(method, handler, query, Post.OnResponse, tagID);
  window.scroll(0,0);
};

Post.buildQuery = function(form) {
  var query = "";
  if (!form.elements||form.elements.length===0) {
    return "";
  }
  for (var i=0; i<form.elements.length; i++) {
    var key = form.elements[i].name;
    var value = Post.getElementValue(form.elements[i]);
    if(key && value) {
      query += ((query!="")?"&":"") + key + "=" + value;
    }
  }
  return query;
};

Post.getElementValue = function(formElement) {
  var type, x;

  if (formElement.length !== null) {
    try {
      type = formElement[0].type;
    } catch (e) {
      type = formElement.type;
    }
  }
  if ((typeof(type) == 'undefined') || (type === 0)) {
    type = formElement.type;
  }
  switch(type) {
    case 'radio':
      if (formElement.checked === true) {
        if (formElement.value == "") return formElement.checked;
        return formElement.value;
      }
      return false;
    case 'select-one':
      for(x=0; x < formElement.length; x++) {
        if (formElement[x].selected === true) {
          if (formElement[x].value == "") return formElement[x].innerHTML;
          return formElement[x].value;
        }
      }
      return false;
    case 'select-multiple':
      var myArray = [];
      for(x=0; x < formElement.length; x++) {
        if (formElement[x].selected === true)  {
          if (formElement.value == "") myArray[myArray.length] = formElement.innerHTML;
          else myArray[myArray.length] = formElement[x].value;
        }
      }
      return myArray;
    case 'checkbox':
      if (formElement.checked && formElement.value != "") return formElement.value;
      return formElement.checked;
    case 'hidden':
      return formElement.value;
    case 'text':
      return formElement.value;
    case 'textarea':
      if (formElement.value) return formElement.value;
      else if (formElement.innerText) return formElement.innerText;
           else if (formElement.innerHTML) return formElement.innerHTML;
                else return;
    default:
      return;
  }
};

Post.OnResponse = function(xml,tagID) {
  var results = document.getElementById(tagID);

  //Create a 'div' to contain the servers response
  if (!results && tagID) {
    results = document.createElement('div');
    results.id = tagID;
    document.getElementsByTagName('body')[0].appendChild(results)
  }
  if (results) {
    var str = Post.EvaluateResponse(xml.firstChild.nodeValue);
    alert(str.split("<br>").join("\n").replace("<hr>",""));
    //results.innerHTML = Post.EvaluateResponse(xml.firstChild.nodeValue);
    window.scroll(0,0);
  }
};

Post.EvaluateResponse = function(o) {
  var i,j,f,r,scriptFragment='<script[^>]*>([\\S\\s]*?)<\/script>';
  var matchAll = new RegExp(scriptFragment, 'img');
  var matchOne = new RegExp(scriptFragment, 'im');

  while (o.match(matchAll)) {
    f = o.match(matchOne)[1];
    r = eval(f);
    i = o.indexOf("<script");
    j = o.indexOf("<\/script>")+9;
    o = o.substring(0,i)+((r)?r:"")+o.substring(j);
  }
  return o;
};

//////// Tabel functions
function newRowActions(obj) {
  //Put all new actions to be taken on adding a new table row here.
  var oid = obj.id;
  var nwid = obj.value;
  var idxs = oid.lastIndexOf("C") + 1;
  var idxe = oid.lastIndexOf("_");
  var num = parseInt(oid.substring(idxs,idxe));
  var srch = "C" + num + "_";

  inputToLabel(obj);
  num++;
  oid = (obj.id.substring(0,obj.id.lastIndexOf("_") + 1) + num).replace(srch,("C" + num + "_"));
  obj = document.getElementById(oid);
  if (obj) {
    obj.id = nwid;
    obj.name = nwid;
  }
}
function inputToLabel(obj) {
  if (obj.value == "") {
    obj.focus();
    alert("Een waarde is verplicht.");
  } else inputToText(obj);
}
function inputToText(obj) {
  var oid = obj.id;
  oid = oid.substring(0,oid.indexOf("_"));
  try {
    document.getElementById(oid).innerHTML = obj.value;
  } catch (e) {
    oid = obj.id;
    oid = oid.substring(0,oid.lastIndexOf("_"));
    document.getElementById(oid).innerHTML = obj.value;
  }
}
function addTblRow(tbl,cells) {
  if (typeof(tbl) != 'object') tbl = document.getElementById(tbl);

  var cell = null,ofocus = null;
  var row = addRow(tbl);

  if (row) {
    var rnum = tbl.tBodies[0].rows.length;
    for (i=0;i<cells;i++) {
      cell=addCell(row);
      if (cell) {
        addCellContent(tbl,cell,newCellContent[tbl.id]["cell" +(i + 1)]["content"],i + 1);
        if (newCellContent[tbl.id]["cell" +(i + 1)]["focus"]) ofocus = document.getElementById(cell.id + "_" + (i + 1));
      }
    }
    if (ofocus) ofocus.focus();
  } else alert('Rij niet toegevoegd');
}
function addRow(tbl) {
  // Rows must have an ID of [table_ID].R[row_number]
  if (typeof(tbl) != 'object') tbl = document.getElementById(tbl);

  var len = tbl.tBodies[0].rows[tbl.tBodies[0].rows.length - 1].id;
  var row = tbl.tBodies[0].insertRow(-1);

  len = parseInt(len.substring(len.indexOf("R") + 1)) + 1;
  while (document.getElementById(tbl.id + ".R" + len)) len++;
  if (row) row.id = tbl.id + ".R" + len;
  return row;
}
function addCell(row,content) {
  // Cells must have an ID of [table_ID].R[row_number]C[column_number]
  if (typeof(row) != 'object') row = document.getElementById(row);

  var rlen = row.cells.length;
  var cell = null;

  cell = row.insertCell(rlen);
  rlen++;
  cell.id = row.id + "C" + rlen;
  cell.style.verticalAlign = "top";
  return cell;
}
function addCellContent(tbl,cell,content,idx) {
  if (typeof(cell) != 'object') cell = document.getElementById(cell);

  var t = "",rid="",tid="";

  tid = cell.id.substring(0,cell.id.indexOf("."));
  rid = cell.id.substring(cell.id.indexOf(".")+1);
  rid = tid + "." + rid.substring(0,rid.indexOf("C"));
  t = newCellContent[tbl.id]["cell" + idx]["content"];
  t = t.replace(/\%name/ig,cell.id + "_" + idx);
  t = t.replace(/\%id/ig,cell.id + "_" + idx);
  t = t.replace(/\%tbl/ig,tid);
  t = t.replace(/\%row/ig,rid);
  cell.innerHTML = t;
}
function deleteAllRows(tblID) {
  var tbl = document.getElementById(tblID);
  for (var i=tbl.tBodies[0].rows.length-1; i>0; i--) {
    tbl.tBodies[0].deleteRow(i);
  }
}
function deleteRow(tblID, rowID) {
  var tbl = null;

  tbl = document.getElementById(tblID);
  for (var i=tbl.tBodies[0].rows.length-1; i>0; i--) {
    if (tbl.tBodies[0].rows[i].id == rowID) {
      tbl.tBodies[0].deleteRow(i);
      idx = i; //save deleted row number
      i = 0; //make sure terminate condition = true
    }
  }
}

//////// Miscallaneous functions
function browserIs(browser) {
  var isIE=(navigator.userAgent.indexOf("MSIE ") > -1);
  var isOP=(navigator.userAgent.indexOf("Opera") > -1 || window.opera);
  var isAOL=(navigator.userAgent.indexOf("AOL ") > -1);
  var isSafari = (navigator.userAgent.indexOf("Safari/") > -1 && navigator.userAgent.indexOf("Chrome") == -1);
  var isChrome = (navigator.userAgent.indexOf("Safari/") > -1 && navigator.userAgent.indexOf("Chrome") > -1);
  var isNS=(!isIE && !isSafari && !isChrome && navigator.userAgent.indexOf("Mozilla/") > -1);

  if (!isNS) isNS=(!isIE && !isSafari && !isChrome && navigator.userAgent.indexOf("Gecko") > -1);

  browser = browser.toLowerCase();
  if (isIE && browser == "ie") return true;
  else if (isOP && browser == "opera") return true;
  else if (isAOL && browser == "aol") return true;
  else if (isSafari && browser == "safari") return true;
  else if (isChrome && browser == "chrome") return true;
  else if (isNS && browser == "mozilla") return true;
  else return false;
}
function isEmpty(val) {
  return !val && val !== 0 && val !== '0';
}
function ObjectID(n) {
  var obj, doc;
  var i,j,k;

  if (!doc||doc==null) doc=document;
	if (doc.getElementById) obj=doc.getElementById(n);
  if (!obj&&doc.all) obj=doc.all[n];
  for(i=0;!obj&&doc.layers&&i<doc.layers.length;i++) {
    if (n==doc.layers[i].id) obj=doc.layers[i];
    if (!obj) obj=getObjectID(n,doc.layers[i].document);
  }
  if (!obj) obj=doc[n];
  if (!obj) obj=eval('document.' + n);
  return (obj)?obj:null;
}
function begins(sSource,sFind,sep) {
  if (sFind=="") return true;
  if (sSource=="") return false;
  if (!sep||sep==null) sep=";";

  var record=sFind.split(sep);
  for (var i in record) if (sSource.substr(0,record[i].length)==record[i]) return true;
  return false;
}
function trim(str,cutoff) {
  if (!cutoff||cutoff==null) cutoff="\\s";
  cutoff = "/^"+cutoff+"*|"+cutoff+"*$/g";
  return (str)? str.replace(cutoff,"") : str;
}
function include(file, addTo) {
  if (document.createElement && document.getElementsByTagName) {
    var head = document.getElementsByTagName('head')[0];
    var body = document.getElementsByTagName('body')[0];
    var script = document.createElement('script');

    script.setAttribute('type', 'text/javascript');
    script.setAttribute('src', file);
    if (addTo == "body") body.appendChild(script);
    else head.appendChild(script);
  }
}
function htmlInclude(id, url) {
  var req = createXMLHttpRequest();
  var element = document.getElementById(id);
  if (!element) return;
  if (req) {
    req.open('GET', url, false);
    req.send(null);
    element.innerHTML = req.responseText;
  } else element.innerHTML = "Your browser does not support XMLHTTPRequest objects.<br />Uw browser ondersteunt geen XMLHTTPRequest objecten.";
}
function createXMLHttpRequest() {
  function tryToCreateObject () {
    var r;
    for (var i=0,l=arguments.length;i<l;i++) {
      var f = arguments[i];
      try {
        r = f();
        break;
      } catch (err) {
        r = null;
      }
    }
    return r;
  }
  return this.tryToCreateObject (
    function() {return new XMLHttpRequest()},
    function() {return new ActiveXObject('Msxml2.XMLHTTP')},
    function() {return new ActiveXObject('Microsoft.XMLHTTP')}
    ) || false;
}
function sha1(msg) {
  function rotate_left(n,s) {
    var t4 = ( n<<s ) | (n>>>(32-s));
    return t4;
  };
  function lsb_hex(val) {
    var str="";
    var i;
    var vh;
    var vl;

    for( i=0; i<=6; i+=2 ) {
      vh = (val>>>(i*4+4))&0x0f;
      vl = (val>>>(i*4))&0x0f;
      str += vh.toString(16) + vl.toString(16);
    }
    return str;
  };
  function cvt_hex(val) {
    var str="";
    var i;
    var v;

    for( i=7; i>=0; i-- ) {
      v = (val>>>(i*4))&0x0f;
      str += v.toString(16);
    }
    return str;
  };
  function Utf8Encode(string) {
    string = string.replace(/\r\n/g,"\n");
    var utftext = "";

    for (var n = 0; n < string.length; n++) {
      var c = string.charCodeAt(n);
      if (c < 128) {
        utftext += String.fromCharCode(c);
      } else if((c > 127) && (c < 2048)) {
        utftext += String.fromCharCode((c >> 6) | 192);
        utftext += String.fromCharCode((c & 63) | 128);
      } else {
        utftext += String.fromCharCode((c >> 12) | 224);
        utftext += String.fromCharCode(((c >> 6) & 63) | 128);
        utftext += String.fromCharCode((c & 63) | 128);
      }
    }
    return utftext;
  };

  var blockstart;
  var i, j;
  var W = new Array(80);
  var H0 = 0x67452301;
  var H1 = 0xEFCDAB89;
  var H2 = 0x98BADCFE;
  var H3 = 0x10325476;
  var H4 = 0xC3D2E1F0;
  var A, B, C, D, E;
  var temp;

  msg = Utf8Encode(msg);

  var msg_len = msg.length;
  var word_array = new Array();
  for( i=0; i<msg_len-3; i+=4 ) {
    j = msg.charCodeAt(i)<<24 | msg.charCodeAt(i+1)<<16 |
    msg.charCodeAt(i+2)<<8 | msg.charCodeAt(i+3);
    word_array.push( j );
  }
  switch( msg_len % 4 ) {
    case 0:
      i = 0x080000000;
      break;
    case 1:
      i = msg.charCodeAt(msg_len-1)<<24 | 0x0800000;
      break;
     case 2:
      i = msg.charCodeAt(msg_len-2)<<24 | msg.charCodeAt(msg_len-1)<<16 | 0x08000;
      break;
     case 3:
      i = msg.charCodeAt(msg_len-3)<<24 | msg.charCodeAt(msg_len-2)<<16 | msg.charCodeAt(msg_len-1)<<8  | 0x80;
      break;
  }

  word_array.push( i );
  while( (word_array.length % 16) != 14 ) word_array.push( 0 );
  word_array.push( msg_len>>>29 );
  word_array.push( (msg_len<<3)&0x0ffffffff );

  for ( blockstart=0; blockstart<word_array.length; blockstart+=16 ) {
    for( i=0; i<16; i++ ) W[i] = word_array[blockstart+i];
    for( i=16; i<=79; i++ ) W[i] = rotate_left(W[i-3] ^ W[i-8] ^ W[i-14] ^ W[i-16], 1);

    A = H0; B = H1; C = H2; D = H3; E = H4;
    for( i= 0; i<=19; i++ ) {
      temp = (rotate_left(A,5) + ((B&C) | (~B&D)) + E + W[i] + 0x5A827999) & 0x0ffffffff;
      E = D; D = C; C = rotate_left(B,30); B = A; A = temp;
    }
    for( i=20; i<=39; i++ ) {
      temp = (rotate_left(A,5) + (B ^ C ^ D) + E + W[i] + 0x6ED9EBA1) & 0x0ffffffff;
      E = D; D = C;  C = rotate_left(B,30); B = A;  A = temp;
    }
    for( i=40; i<=59; i++ ) {
      temp = (rotate_left(A,5) + ((B&C) | (B&D) | (C&D)) + E + W[i] + 0x8F1BBCDC) & 0x0ffffffff;
      E = D; D = C; C = rotate_left(B,30); B = A; A = temp;
    }
    for( i=60; i<=79; i++ ) {
      temp = (rotate_left(A,5) + (B ^ C ^ D) + E + W[i] + 0xCA62C1D6) & 0x0ffffffff;
      E = D; D = C; C = rotate_left(B,30); B = A; A = temp;
    }
    H0 = (H0 + A) & 0x0ffffffff;
    H1 = (H1 + B) & 0x0ffffffff;
    H2 = (H2 + C) & 0x0ffffffff;
    H3 = (H3 + D) & 0x0ffffffff;
    H4 = (H4 + E) & 0x0ffffffff;
   }
   var temp = cvt_hex(H0) + cvt_hex(H1) + cvt_hex(H2) + cvt_hex(H3) + cvt_hex(H4);
   return temp.toLowerCase();
}
function formDataChanged(form) {
  for (var i = 0; i < form.elements.length; i++) {
    var element = form.elements[i];
    var type = element.type;
    switch (type) {
      case "checkbox":
      case "radio":
        if (element.checked != element.defaultChecked) return true;
      break;
      case "select-one":
      case "select-multiple":
        for (var j = 0; j < element.options.length; j++) {
          if (element.options[j].selected != element.options[j].defaultSelected) return true;
        }
      break;
      default:
        if (element.value != element.defaultValue) return true;
      break;
    }
  }
  return false;
}

