logging = true;
xsltdebug = true;

function el(id) {
  return document.getElementById(id);
}

function createHTTPRequest() {
  var XMLHTTPREQUEST_MS_PROGIDS = [
      "Msxml2.XMLHTTP.7.0",
      "Msxml2.XMLHTTP.6.0",
      "Msxml2.XMLHTTP.5.0",
      "Msxml2.XMLHTTP.4.0",
      "Msxml2.XMLHTTP.3.0",
      "Msxml2.XMLHTTP",
      "Microsoft.XMLHTTP"];
  var req = false;
        if (window.XMLHttpRequest) { // Mozilla, Safari, ...
            try {
              req = new XMLHttpRequest();
            } catch (e) {
              alert('Cannot create an XMLHTTP instance other');
              return false;
            }
            if (req.overrideMimeType) {
                req.overrideMimeType('text/html');
                // See note below about this line
            }
        } 
        else if (window.ActiveXObject) { // IE
          var i = -1;    
          while (++i < XMLHTTPREQUEST_MS_PROGIDS.length) {
            try {
              req = new ActiveXObject(XMLHTTPREQUEST_MS_PROGIDS[i]);
              i += XMLHTTPREQUEST_MS_PROGIDS.length;
            }
            catch(ex){}
          }
        }
        if (!req) {
            alert('Cannot create an XMLHTTP instance MS');
            return false;
        }
  return req;
}

function traverse(tree) {
        //alert('zacinam');
                el('pok').value = el('pok').value + '<' + tree.nodeName + '>';
                for (var c = tree.firstChild; c; c = c.nextSibling) {
                  if (c.nodeType == DOM_ELEMENT_NODE) {
                    ret = traverse(c);
                    el('pok').value = el('pok').value + '</' + tree.nodeName + '>';
                    if (typeof ret == 'boolean' && !ret) {
                      return false;
                    }
                  }
                }
                el('pok').value = el('pok').value + ' ' + tree.nodeValue;
                return false;
}


function xslt_filter(ixmlurl, ixslurl, htmlph) {
  var xml = false;
  var xslt = false;
  var html = 'html init';
  var xmlhttpRequest;
  var xslhttpRequest;

      el(htmlph).innerHTML = '<table width="100%" height="100%"><tr><td align="center" valign="center"><image src="ajax-loader.gif" border="0"></td></tr></table>';
      /*if ((window.opera) || (document.createElementNS)){
        el(htmlph).innerHTML = '<span>Váš prehliadač nepodporuje XMLHttpRequest.</span>'
      }
      else*/ {

        xmlhttpRequest = createHTTPRequest();
        if (!xmlhttpRequest) {
          return;
        }
        xslhttpRequest = createHTTPRequest();
        if (!xslhttpRequest) {
          return;
        }
        xmlhttpRequest.onreadystatechange = function() { 
          //alert('xml state je ' + xmlhttpRequest.readyState + ' status je ' + xmlhttpRequest.status);
          try {
          if(xmlhttpRequest.readyState  == 4)
          {
              if(xmlhttpRequest.status  == 200){
                //if (document.createElementNS) { 
                  xml= xmlhttpRequest.responseText;
                  xml = xmlParse(xml);
                /*}
                else
                  xml= xmlhttpRequest.responseXML;*/                 
                 //alert(xml);
                 //xslt = xmlParse(xslt);
                 if (xslt) {
                   html = xsltProcess(xml, xslt);
                   //el('pok').value = html;
                   if(el(htmlph)) el(htmlph).innerHTML = html;
                 }
              }
              else { 
                 xml= false;
              }
          }
          }
          catch (e) {
            alert('xml Caught Exception: ' + e.description);
          }
        }; 

        xslhttpRequest.onreadystatechange = function() {
          //alert('xsl state je ' + xslhttpRequest.readyState + ' status je ' + xslhttpRequest.status);
          try {
          if(xslhttpRequest.readyState  == 4)
          {
              if(xslhttpRequest.status  == 200){ 
                 xslt= xslhttpRequest.responseText;
                 //alert(xslt);
                 xslt = xmlParse(xslt);
                 if (xml) {
                   html = xsltProcess(xml, xslt);
                   //el('pok').value = html;
                   if(el(htmlph)) el(htmlph).innerHTML = html;
                 }
              }
              else { 
                 xslt= false;
              }
          }
          }
          catch (e) {
            alert('xsl Caught Exception: ' + e.description);
          }
        }; 

        //xslhttpRequest.open('GET', 'openurl.php?trans=false&url='+encodeURIComponent(ixslurl), true);
        xslhttpRequest.open('GET', ixslurl, true);
        xslhttpRequest.send(null);
        //xmlhttpRequest.open('GET', 'openurl.php?trans=true&cp=1250&url='+encodeURIComponent(ixmlurl), true);
        xmlhttpRequest.open('GET', ixmlurl, true);
        xmlhttpRequest.send(null);
      }
}

function get_url(iurl, htmlph) {
  var xml = false;
  var xslt = false;
  var html = 'html init';
  var httpRequest;

      el(htmlph).innerHTML = '<table width="100%" height="400"><tr><td align="center" valign="center"><image src="ajax-loader.gif" border="0"></td></tr></table>';
        httpRequest = createHTTPRequest();
        if (!httpRequest) {
          return;
        }
        try {
          httpRequest.open('GET', 'openurl.php?trans=true&cp=1250&url='+encodeURIComponent(iurl), true);
          //httpRequest.open('GET', iurl, true);
        }
        catch (e) {
          alert('Caught Exception: ' + e.name + ' ' + e.message);
        }       
        httpRequest.onreadystatechange = function() { 
          //alert('state je ' + httpRequest.readyState + ' status je ' + httpRequest.status);
          try {
            if(httpRequest.readyState  == 4)
            {
              if(httpRequest.status  == 200){
                 if (httpRequest.responseBody){
                   html= BinaryToString(httpRequest.responseBody);
                 }
                 else
                   html= httpRequest.responseText;
                 if(el(htmlph)) el(htmlph).innerHTML = html;
              }
              else { 
                 html= false;
              }
            }
          }
          catch (e) {
            alert('Caught Exception: ' + e.description);
          }
        }; 

        httpRequest.send(null);
}

function cleanxml(ixml, ixsl) {
  cleanvalue(ixml);
  cleanvalue(ixsl);
}

function cleanvalue(id) {
  var x = el(id);
  x.value = x.value.replace(/^\s*/, '').replace(/\n\s*/g, '\n');
}

