var req;
function loadXMLDoc(url){
	
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChange;
        req.open("GET",url, true);
        req.send(null);
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChange;
            req.open("GET",url, true);
            req.send();
        }
    }
}

function processReqChange(){
	if (req.readyState == 4){
	
		if (req.status == 200 || (req.status>300 && req.status<400)){
			var a=req.responseXML;
			var root = a.documentElement;
			var type=a.getElementsByTagName("type");
			
			var cs=type[0].getAttribute("t");
			
			if(cs=="authors"){
				var input_id=type[0].getAttribute("input_id");
				var town=a.getElementsByTagName("item");
				var obj=document.getElementById('selauthor['+input_id+']');
				var contt=document.getElementById('contauthor['+input_id+']');
				obj.style.display='block';
				contt.innerHTML='';
				
				for(var i=0;i<town.length;i++){
					
					var title_node=document.createTextNode(town[i].getAttribute('name')+" "+town[i].getAttribute('surname')) ;
					
					var author_id=town[i].getAttribute('author_id');
					
					var div = document.createElement('div');
			
					div.className='item';
					//div.setAttribute("onClick","selTown(this)");
					div.appendChild(title_node);
					div.onclick = new Function('selAuthor(this,"'+input_id+'","'+author_id+'")');
					div.onmouseout = new Function('outdiv(this)');
					div.onmouseover = new Function('adiva(this,"'+input_id+'")');
					
					contt.appendChild(div);
				}
			}
			if(cs=="author"){
				var items=a.getElementsByTagName("item");
				var obj=document.getElementById('mp3s');
				var searchmp3h=document.getElementById('searchmp3h');
				obj.style.display='block';
				obj.innerHTML='';
				if(items.length==0){
					searchmp3h.innerHTML="<h1>Пошук не дав результату</h1>";
				}
				else{
					searchmp3h.innerHTML="<h1>Результат пошуку</h1>";
				}
				searchmp3h.style.display="block";
				for(var i=0;i<items.length;i++){
				
					var login=items[i].getAttribute('login');
					var name=items[i].getAttribute('name');
					var surname=items[i].getAttribute('surname');
					var info=items[i].getAttribute('small_info');
					var author_link=items[i].getAttribute('author_link');
					var div = document.createElement('div');
					div.className='mp3item';
					div.innerHTML+='<h2><a href="'+author_link+'">'+name+' '+surname+'</a></h2>';
					div.innerHTML+='<p>'+info+'</p>';
					if(i%4==0){
						var ln = document.createElement('div');
						ln.className="h16 clear-left";
						obj.appendChild(ln);
					}
					obj.appendChild(div);
				}
				
			}
			if(cs=='showtown'){
				var town=a.getElementsByTagName("town");
				var obj=document.getElementById('seltown');
				var contt=document.getElementById('conttown');
        if(town.length>0){
  				obj.style.display='block';
  				contt.innerHTML='';
  				for(var i=0;i<town.length;i++){
  					var title=town[i].getAttribute('title');
  					var city_id=town[i].getAttribute('cityid');

  					var div = document.createElement('div');
  					div.className='item';
  					//div.setAttribute("onClick","selTown(this)");
  					div.onclick = new Function('selTown(this,'+city_id+')');
  					div.onmouseout = new Function('outdiv(this)');
  					div.onmouseover = new Function('odiv(this)');
  					div.innerHTML=title;
  					contt.appendChild(div);
  				}
        }
        else{
          hid();
        }

			}
			
			if(cs=='showplaces'){
				var town=a.getElementsByTagName("item");
				var obj=document.getElementById('selplace');
				var error=document.getElementById('error');
				var contt=document.getElementById('contplace');
				var parent_div=document.getElementById('parent_div');
				parent_div.style.display='block';
				
				obj.style.display='block';
				error.style.display="none";
				contt.innerHTML='';
				for(var i=0;i<town.length;i++){
					var title=town[i].getAttribute('title');
					var place_id=town[i].getAttribute('place_id');
					var div = document.createElement('div');
					var hidden = document.createElement('input');
					hidden.type="hidden";
					hidden.value=place_id;
					hidden.id="hplace_id";
					hidden.name="hplace_id";
					
					div.className='item';
					//div.setAttribute("onClick","selTown(this)");
					div.onclick = new Function('selPlace(this)');
					div.onmouseout = new Function('outdiva(this)');
					div.onmouseover = new Function('odiva(this)');
					
					div.innerHTML=title;
					contt.appendChild(div);
					contt.appendChild(hidden);
				}
			}
			if(cs=='showmp3'){
				var items=a.getElementsByTagName("item");
				var obj=document.getElementById('mp3s');
				var searchmp3h=document.getElementById('searchmp3h');
				searchmp3h.style.display="block";
				if(items.length==0){
					searchmp3h.innerHTML="<h1>Пошук не дав результату</h1>";
				}
				else{
					searchmp3h.innerHTML="<h1>Результат пошуку</h1>";
				}
				obj.style.display="block";
				obj.innerHTML='';
				for(var i=0;i<items.length;i++){
					var title=items[i].getAttribute('title');
					var login=items[i].getAttribute('login');
					var author_link=items[i].getAttribute('author_link');
					var name=items[i].getAttribute('name');
					var surname=items[i].getAttribute('surname');
					var info=items[i].getAttribute('info');
					var genres=items[i].getAttribute('keywords');
					var content_id=items[i].getAttribute('content_id');
					var filename=items[i].getAttribute('filename');
					
					link="/download/"+content_id+"/"+filename;
					//author_link="/authors/"+login;
					
					var div = document.createElement('div');
					div.className='last-mp3item';
					div.innerHTML+='<h2><a href="'+author_link+'">'+name+' '+surname+'</a></h2>';
					div.innerHTML+='<img src="/res/gfx/mp3.gif" width="8" height="10" />';
					div.innerHTML+='<a href="'+link+'">'+title+'</a>';
					div.innerHTML+='<span>'+genres+' '+info+'</span>';
					if(i%4==0){
						var ln = document.createElement('div');
						ln.className="h16 clear-left";
						obj.appendChild(ln);
					}
					obj.appendChild(div);
				}
				//var contt=document.getElementById('conttown');				
			}
			if(cs=="showgoods"){
				var items=a.getElementsByTagName("item");
				var obj=document.getElementById('mp3s');
				var searchgoods=document.getElementById('searchmp3h');
				searchgoods.style.display="block";
				if(items.length==0){
					searchgoods.innerHTML="<h1>Пошук не дав результату</h1>";
				}
				else{
					searchgoods.innerHTML="<h1>Результат пошуку</h1>";
				}
				obj.style.display="block";
				obj.innerHTML='';
				for(var i=0;i<items.length;i++){
					var titlea=items[i].getAttribute('title');
					var login=items[i].getAttribute('login');
					var name=items[i].getAttribute('name');
					var good_price=items[i].getAttribute('price');
					var surname=items[i].getAttribute('surname');
					var info=items[i].getAttribute('info');
					//var genres=items[i].getAttribute('keywords');
					var content_id=items[i].getAttribute('good_id');
					var image_link=items[i].getAttribute('image_link');
					var width=items[i].getAttribute('width_smaller');
					var height=items[i].getAttribute('height_smaller');
					var good_link=items[i].getAttribute('good_link');
					var section_title=items[i].getAttribute('section_title');
					//var filename=items[i].getAttribute('title');
					
//					link="/download/"+content_id+"/"+filename;
					//author_link="/authors/"+login;
					
					var div = document.createElement('div');
					var a_img = document.createElement('a');
					a_img.href=good_link;
					var a_title = document.createElement('a');
					var img=document.createElement('img');
					var section=document.createTextNode(section_title);
					var price=document.createTextNode(good_price);
					var span=document.createElement('span');
					var author=document.createTextNode(name+' '+surname);
					var title=document.createTextNode(titlea);
					
//					var title=document.createTextNode(titlea);
					var auth_span=document.createElement('span');
					var span_price=document.createElement('span');
					//var div_clear = document.sreateElement("div");
					//div_clear.className()="clear_left";
					
					img.src=image_link;
					img.width=width;
					img.height=height;
					img.alt=titlea;
					a_img.className="img";
					//a_img.appendChild(img);
					a_title.href=good_link;
					//a_title.appendChild(title);
					a_title.innerHTML=titlea;
					span.className='cat';
					span.appendChild(section);
					auth_span.className='author';
					auth_span.appendChild(author);
					span_price.className="price";
					span_price.appendChild(price);
					div.className='item';
					if(img.src){
						div.appendChild(a_img);
					}
					div.appendChild(span);
					div.appendChild(auth_span);
					div.appendChild(a_title);
					div.appendChild(span_price);
					/*if(i%2==0){
						var ln = document.createElement('div');
						ln.className="h16 clear-left";
						obj.appendChild(ln);
					}*/
					obj.appendChild(div);
				}
			}
		} 
		else {
			alert("There was a problem retrieving the XML data:\n" + req.statusText);
		}
	}
}