function Validator (sessionid) {

   var _cb;
   var _handler;
   var _sessionid;

   _sessionid = sessionid;

   this.execute = function (id, value, callback) {

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

      if (_handler) {

         _cb = callback;

         var hostname = window.location.hostname.toString ();

         _handler.open ("POST", "http://" + hostname + "/perl/mxmd/webchat/validate", true);
         _handler.onreadystatechange = onresponse;
         _handler.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');

         _handler.send ("<validate><task>validate</task><sessionid>" + _sessionid + "</sessionid><data><id>" + id + "</id><text><![CDATA[" + value + "]]></text></data></validate>");

      } else {

         showMsg ("An error occured. Please try again later");
      }
   }

   function onresponse () {

      if (_handler.readyState != 4 || _handler.status != 200)
         return;

      var xml = _handler.responseXML;
      if (xml == null) {
         showMsg ('Unexpected error, please try again later');
         return;
      }

      if (! findNode (xml, 'ok')) {
         showMsg ('Unexpected error, please try again later');
         return;
      }

      var id = findNodeValue (xml, 'id');
      var status = findNodeValue (xml, 'status');
      var msg    = findNodeValue (xml, 'msg');

      if (_cb)
        _cb ({'id': id, 'status': status, 'msg': msg, 'number': findNodeValue (xml, 'number'), 'isuser': findNodeValue (xml, 'isuser'), 'credits': findNodeValue (xml, 'credits'), 'usersid': findNodeValue (xml, 'usersid')});
   }

   function findNodeValue (node, tag) { try { return node.getElementsByTagName (tag)[0].firstChild.nodeValue; } catch (err) { return null; } }
   function findAttrValue (node, tag) { try { return node.attributes.getNamedItem (tag).value; } catch (err) { return null; } }
   function findNode (node, tag) { try { return node.getElementsByTagName (tag)[0]; } catch (err) { return null; } }
}

