//var url = "http://192.168.0.103/web_site/JS/locative/site_data.php?";
var url = "check_page.php?";


function handleHttpResponse() {
  if (httpXMLobj.readyState == 4) {
    if (httpXMLobj.responseText.indexOf('invalid') == -1) {
      alert('-->' + httpXMLobj.responseText + '<--');
      var elems = httpXMLobj.responseText.split('|');
      if(elems[0] =='BAD_PAGE') {
          document.getElementById('create_page_info').innerHTML = "Page Name already in use: " + elems[1]; ;
          document.getElementById('create_page_info').style.display = 'block';
      }
      else {
        setName();
        document.getElementById('editform').submit();
          // document.getElementById('create_page_info').style.display = 'block';
          // document.getElementById('create_page_info').innerHTML = "ready to Write: " + elems[1]; 
      }

      isWorking = false;
    }
  }
}

var isWorking = false;
function update_avail(fname, lat, lng) {
  qstr = "fname=" + fname + "&lat=" + lat + "&lng=" + lng;
  qstr = qstr.replace(/\&amp;/g,"&");
  if (!isWorking && httpXMLobj) {      
    httpXMLobj.open("GET", url + qstr, true);
    httpXMLobj.onreadystatechange = handleHttpResponse;
    isWorking = true;
    httpXMLobj.send(null);
  }
}

    function search_CSAuthor(author) {
      var url = CS_authors + "?";
      var qstr = "auth=" + author;

      var httpXMLobj = getHTTPObject();
      if (httpXMLobj) {      
        httpXMLobj.open("GET", url + qstr, true);   
        httpXMLobj.onreadystatechange = function() {
            if (httpXMLobj.readyState == 4) {
                if (httpXMLobj.responseText.indexOf('invalid') == -1) {
                    var authors =  httpXMLobj.responseText;
                    if(authors) {
                      authors = format_CSauthors(authors);
                      document.getElementById('authq__out').innerHTML=authors;
                      document.getElementById("authq_control").style.display="block";
                    }
                   
               }
            }
        };
        httpXMLobj.send(null);
      }
    }


function format_CSauthors(authors) {
   var results = authors.split(/\n/);
   var authors_array = new Array();
   for(var i=0; i < results.length; i++) {
      var namespaces = results[i].split('/');
      namespaces.shift();  
      namespaces.shift();        
      var namespace = namespaces.join(':');
      namespace = namespace.replace(/\.txt$/,"");  
      authors_array[i] = '<a href="doku.php?id=' + namespace + '">' + namespace + '</a>';
   }

   return  authors_array.join('<br>');
}

function getHTTPObject() {

  var xmlhttp;
  /*@cc_on
  @if (@_jscript_version >= 5)
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
        xmlhttp = false;
      }
    }
  @else
  xmlhttp = false;
  @end @*/
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
    } catch (e) {
      xmlhttp = false;
    }
  }
  return xmlhttp;
}
//var httpXMLobj;
var httpXMLobj = getHTTPObject(); // We create the HTTP Object

