var SELECTS_URL = "/cgi-bin/selects.cgi";
var QUERY_URL = "/cgi-bin/institutions.cgi";

var xhttp = undefined;
if (window.XMLHttpRequest)
 xhttp = new XMLHttpRequest();
else xhttp=new ActiveXObject("Microsoft.XMLHTTP");

function getData(dataSource, divID){
    if(xhttp) {
     var obj = document.getElementById(divID);
     xhttp.open("GET", dataSource,false);
     xhttp.send(null);
     obj.innerHTML = xhttp.responseText;

/*     xhttp.onreadystatechange = function(){
      if (xhttp.readyState == 4 && xhttp.status == 200) {
        obj.innerHTML = xhttp.responseText;
      }
     }*/
    }
}

function get_selects(select){
    if(xhttp){
        xhttp.open("GET",SELECTS_URL+"?type="+select,false);
        xhttp.send(null);
        return xhttp.responseText;

    }
    return null;
}

function show_dbdata(id){
    var district,number,instype;
    var query;
    district = document.getElementById("district").value;
    number = document.getElementById("number").value;
    instype = document.getElementById("instype").value;
    if (id) district=id;
    query = QUERY_URL+"?district="+district+"&number="+number+"&instype="+instype;
    if(xhttp){
        xhttp.open("GET",query);
        xhttp.send(null);
        xhttp.onreadystatechange = function(){
          if (xhttp.readyState == 4 && xhttp.status == 200) {
             // document.getElementById(table_id).innerHTML = xhttp.responseText;
              window_h=window.open('','_blank','width=1024,height=600,menubar=no,scrollbars=yes,resizable=yes,toolbar=no,location=no,top=50,left=50');
              window_h.document.writeln('<html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="content-type" content="text/html; charset=utf8" /><meta http-equiv="Content-Language" content="ru"><link href="http://tehnopark.org/wp-content/themes/tehnopark/style.css" rel="stylesheet" type="text/css" media="screen" /></head><body>');
              window_h.document.writeln('<div id="table">');
              window_h.document.writeln(xhttp.responseText);
              window_h.document.writeln('</div></body></html>');
              window_h.focus()
//              window_h.document.getElementById('table').innerHTML;
          }
        }

    }
    form = document.getElementById("card").reset();
}

function show_district(id) {show_dbdata(id);}

function show_all(){
    var query;
    query = QUERY_URL+"?district=all"
    if(xhttp){
        xhttp.open("GET",query);
        xhttp.send(null);
        xhttp.onreadystatechange = function(){
          if (xhttp.readyState == 4 && xhttp.status == 200) {
             // document.getElementById(table_id).innerHTML = xhttp.responseText;
              window_h=window.open('','_blank','width=1024,height=600,menubar=no,scrollbars=yes,resizable=yes,toolbar=no,location=no,top=50,left=50');
              window_h.document.writeln('<html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="content-type" content="text/html; charset=utf8" /><meta http-equiv="Content-Language" content="ru"><link href="http://tehnopark.org/wp-content/themes/tehnopark/style.css" rel="stylesheet" type="text/css" media="screen" /></head><body>');
              window_h.document.writeln('<div id="table">');
              window_h.document.writeln(xhttp.responseText);
              window_h.document.writeln('</div></body></html>');
              window_h.focus()
//              window_h.document.getElementById('table').innerHTML;
          }
        }

    }
    form = document.getElementById("card").reset();
}

function clear_fields(){
    var obj;
    obj = document.getElementById("district");
    obj.selectedIndex = 0;
    obj = document.getElementById("instype");
    obj.selectedIndex = 0;
}

function clear_number(){
    var obj;
    obj = document.getElementById("number");
    obj.selectedIndex = 0;
}

function uvao_map(){
    districts=new Array();
    districts["Выхино-Жулебино"]=1;
    districts["Капотня"]=2;
    districts["Кузьминки"]=3;
    districts["Лефортово"]=4;
    districts["Люблино"]=5;
    districts["Марьино"]=6;
    districts["Некрасовка"]=7;
    districts["Нижегородский"]=8;
    districts["Печатники"]=9;
    districts["Рязанский"]=10;
    districts["Текстильщики"]=11;
    districts["Южнопортовый"]=12;

    image=new Array("vykhino-zhulebino.png","kapotnia.png","kuzminki.png","lefortovo.png","lyublino.png","maryino.png","nekrasovka.png",
"nizhegorodsky.png","pechatniki.png","ryazansky.png","tekstilschiki.png","yuzhnoportovy.png");

    num_arears=document.getElementsByTagName("area").length;
    for(i=0;i<num_arears;i++){
	area=document.getElementsByTagName("area")[i];
	area.onclick=function(event){show_district(districts[this.alt])}
	area.onmouseover=function(){
	    document.getElementById("uvao_districts").src="/wp-content/themes/tehnopark/images/" + image[districts[this.alt]-1];
	}
	area.onmouseout=function(){
	    document.getElementById("uvao_districts").src="/wp-content/themes/tehnopark/images/uvao_districts.png";
	}
	
    }
}

function testlib(){ alert("library loaded!");}

