
// Определяем браузер пользователя
isNS4 = (document.layers) ? true : false;
isIE4 = (document.all && !document.getElementById) ? true : false;
isIE5 = (document.all && document.getElementById) ? true : false;
isNS6 = (!document.all && document.getElementById) ? true : false;


    function getelementbyid(myid) {
        if (isNS4){
            objElement = document.layers[myid];
        }else if (isIE4) {
            objElement = document.all[myid];
        }else if (isIE5 || isNS6) {
            objElement = document.getElementById(myid);
        };
        return(objElement);
    };


    function copyNode( node ) {
        try {
            var acopy = null;
            if ( node.nodeType == 1 ) {
                acopy = document.createElement(node.nodeName);
                if ( node.attributes ) {
                  var attrs = node.attributes;
                  for (var i = 0; i < attrs.length; i++) {
                      if (attrs[i].nodeValue) {
                          var at = document.createAttribute(attrs[i].nodeName);
                          at.nodeValue = attrs[i].nodeValue;
                          acopy.setAttributeNode(at);
                      };
                  };
                };
                for( var i = 0; i < node.childNodes.length; i++ ) {
                    var child = copyNode(node.childNodes.item(i));
                    if ( child != null ) {
                        acopy.appendChild(child);
                    };
                };
            } else if ( node.nodeType == 2 ) {
            } else if ( node.nodeType == 3 ) {
                acopy = document.createTextNode(node.nodeValue);
            };
            return acopy;
        } catch (e) {
            alert('Caught Exception 1: ' + e.description);
            return null;
        };
    };

    function seek_node( parent, searchName ) {
      if ( parent.nodeName == searchName )
        return parent;
      for ( var i = 0; i < parent.childNodes.length; i++ ) {
        var chilren_found = seek_node( parent.childNodes[i], searchName );
        if ( chilren_found != null )
          return chilren_found;
      }
      return null;
    }

    function seek_node_back( child, searchName ) {
      if ( child.nodeName == searchName )
        return child;
      if ( child.parentNode ) {
        var parent_found = seek_node_back( child.parentNode, searchName );
        if ( parent_found != null )
          return parent_found;
      }
      return null;
    }

    function seek_named_node( parent, attrName ) {
      if ( parent != null ) {
        if ( parent.nodeType == 1 ) {
          if ( parent.getAttribute('name') == attrName )
            return parent;
        };
        for ( var i = 0; i < parent.childNodes.length; i++ ) {
          var chilren_found = seek_named_node( parent.childNodes[i], attrName );
          if ( chilren_found != null )
            return chilren_found;
        };
        return null;
      } else
        return null;
    };

    function copy_form( target_id ) {
      try {
        var source = getelementbyid('sourceForm');
        var target = getelementbyid('target'+target_id);
        while( target.childNodes.length > 0 ) {
          target.removeChild(target.firstChild);
        };
        for( var i = 0; i < source.childNodes.length; i++ ) {
          var child = copyNode(source.childNodes.item(i));
          if ( child != null ) {
            target.appendChild(child);
          };
        };
        return false;
      } catch (e) {
        alert('Caught Exception: ' + e.description);
        return true;
      };
    };

    function note(guest, offer_id, ask_id) {
        var http_request = false;

        if (window.XMLHttpRequest) { // Mozilla, Safari, ...
            http_request = new XMLHttpRequest();
            if (http_request.overrideMimeType) {
                http_request.overrideMimeType('text/xml');
                // See note below about this line
            };
        } else if (window.ActiveXObject) { // IE
            try {
                http_request = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    http_request = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {};
            };
        };

        if (!http_request) {
//            alert('Giving up :( Cannot create an XMLHTTP instance');
            return true;
        };
        query = '?guest_id='+guest+'&offer_id='+offer_id+'&ask_id='+ask_id+'&xml=yes';

        http_request.onreadystatechange = function() { alertContents(http_request,offer_id,ask_id); };
        http_request.open('GET', '/notes/add/'+query, true);
        http_request.send(null);
        return false;
    };


    function alertContents(http_request,offer_id,ask_id) {
        try {
            if (http_request.readyState == 4) {
                if (http_request.status == 200) {
                    var xmldoc = http_request.responseXML;
                    var status_node = xmldoc.getElementsByTagName('status').item(0);
                    if ( status_node.firstChild.data == '0' ) {
                      var notes_node = xmldoc.getElementsByTagName('notes').item(0);
                      var notes_id = 'blocknote_count';
                      if ( offer_id > 0 ) {
                        var add_id = 'add_off_'+offer_id;
                        var added_id = 'added_off_'+offer_id;
                        getelementbyid(add_id).className='display_none';
                        getelementbyid(added_id).className='display';
                        getelementbyid(notes_id).firstChild.data=notes_node.firstChild.data;
                      } else {
                        var add_id = 'add_ask_'+ask_id;
                        var added_id = 'added_ask_'+ask_id;
                        getelementbyid(add_id).className='display_none';
                        getelementbyid(added_id).className='display';
                        getelementbyid(notes_id).firstChild.data=notes_node.firstChild.data;
                      };
                    } else {
                      var comment_node = xmldoc.getElementsByTagName('comments').item(0);
                      alert( comment_node.firstChild.data );
                    };
                } else {
                    alert('There was a problem with the request.');
                };
            };
        } catch( e ) {
            alert('Caught Exception: ' + e.description);
        };
    };