// JavaScript Document
var miasto_nazwe;
var ostatni;
var test = 0;
var co = 0;
var id_miasta_p = 0;
var val, podziel;
var myArray;

function pokaz_info()
{
	document.getElementById("info_miast").style.display = 'block';
}

function schowaj_info()
{
	setTimeout(function(){
	document.getElementById("info_miast").style.display = 'none';
						}, 1500);
} 

function valid() {
  
  return false;
}


function zaznacz(id_voler)
{
	document.getElementById("id_m" + id_voler).style.background = "#ebeeff";
	
	if(miasto_nazwe > -1 && miasto_nazwe != id_voler)
	{
		document.getElementById("id_m" + miasto_nazwe).style.background = "#fff";
	}
	miasto_nazwe = id_voler;
}

function klik()
{

if(document.getElementById("p_" + miasto_nazwe))
{
	miasto_nazwa = document.getElementById("p_" + miasto_nazwe);
	val = miasto_nazwa.innerHTML;
        val = val.replace(/<\/span>/gi, '');
        val = val.replace(/<span class="wyroznij_miasta">/gi, '');
		val = val.replace(/<span class=wyroznij_miasta>/gi, '');
   	 document.getElementById("miasto").value = val;
	
	id_miasta = document.getElementById("id_miasta_p" + miasto_nazwe);
	var tempArray = id_miasta.innerHTML;
	var ttempArray = tempArray.replace(/_/gi, '"');
	var myString = new String(ttempArray);
	myArray = myString.split(','); 
	document.getElementById("id_miasta_wyslij").value = myArray[0];
	document.getElementById("rensponse").style.display = 'none';
	

	test=3;
}
	
}
function handleKeyUp(e) 
{
  // pobiera zdarzenie
  e = (!e) ? window.event : e;
  // pobiera adresata zdarzenia
  target = (!e.target) ? e.srcElement : e.target;
  if (target.nodeType == 3) 
    target = target.parentNode;
  // pobiera kod znaku naciśniętego klawisza
  code = (e.charCode) ? e.charCode :
       ((e.keyCode) ? e.keyCode :
       ((e.which) ? e.which : 0));
  // sprawdza, czy zdarzenie było typu  keyup
  if (e.type == "keyup") 
  {    
    isKeyUpDownPressed =false; 
    // sprawdza, czy pojawił się któryś z ważnych dla aplikacji znaków
    if ((code < 13 && code != 8) || (code >=14 && code < 32) ||
      (code >= 33 && code <= 46 && code != 38 && code != 40) ||
      (code >= 112 && code <= 123)) 
    {
      // ignoruje pozostałe znaki
    }
    else

    if(code == 13)
    {
		if(miasto_nazwe > -1)
		{
		miasto_nazwa = document.getElementById("p_" + miasto_nazwe);
        val = miasto_nazwa.innerHTML;
		
        val = val.replace(/<\/span>/gi, '');
	
        val = val.replace(/<span class="wyroznij_miasta">/gi, '');
		val = val.replace(/<span class=wyroznij_miasta>/gi, '');
   		document.getElementById("miasto").value = val;
		
		id_miasta = document.getElementById("id_miasta_p" + miasto_nazwe);
			var tempArray = id_miasta.innerHTML;
	var ttempArray = tempArray.replace(/_/gi, '"');
	var myString = new String(ttempArray);
	myArray = myString.split(','); 
	document.getElementById("id_miasta_wyslij").value = myArray[0];

		document.getElementById("rensponse").style.display = 'none';
		test=3;
		}
    }
	else if(code == 40)
    {
		var ile_miast = document.getElementById('ile_miast').value - 1;

		if(miasto_nazwe < ile_miast)
		{
			miasto_nazwe = miasto_nazwe + 1;
		}
		else
		{
			document.getElementById("id_m" + ile_miast).style.background = "#fff";
			miasto_nazwe = 0;
		}
		if(miasto_nazwe > 0)
		{
			miasto_nazwe = miasto_nazwe - 1;
			document.getElementById("id_m" + miasto_nazwe).style.background = "#fff";
			miasto_nazwe = miasto_nazwe + 1;
		}
		
        document.getElementById("id_m" + miasto_nazwe).style.background = "#ebeeff";
	
		id_miasta = document.getElementById("id_miasta_p" + miasto_nazwe);
		
		miasto_nazwa = document.getElementById("p_" + miasto_nazwe);
        val = miasto_nazwa.innerHTML;

       val = val.replace(/<\/span>/gi, '');
	
        val = val.replace(/<span class="wyroznij_miasta">/gi, '');
		val = val.replace(/<span class=wyroznij_miasta>/gi, '');
    document.getElementById("miasto").value = val;
		
		var tempArray = id_miasta.innerHTML;
		var ttempArray = tempArray.replace(/_/gi, '"');
		var myString = new String(ttempArray);
		myArray = myString.split(','); 
		document.getElementById("id_miasta_wyslij").value = myArray[0];
		test=0;

    }
	else
      // naciśnięcie strzałki w góę przenosi nas do poprzedniej podpowiedzi
      if(code == 38)
      {
		  var ile_w_sumie = document.getElementById('ile_miast').value - 1;
		  
		  if(miasto_nazwe > 0)
		  {
			  if(miasto_nazwe == 1)
			  {
				  document.getElementById("id_m" + miasto_nazwe).style.background = "#fff";
			  }
		  	miasto_nazwe = miasto_nazwe - 1;
		  }
		  else
		  {
			  if(miasto_nazwe == 0)
			  {
				  document.getElementById("id_m" + miasto_nazwe).style.background = "#fff";
			  }
			  miasto_nazwe = document.getElementById('ile_miast').value - 1;
		  }
		  
		  
		  
		  if(miasto_nazwe > 0 && ile_w_sumie != miasto_nazwe)
			{
				miasto_nazwe = miasto_nazwe + 1;
				document.getElementById("id_m" + miasto_nazwe).style.background = "#fff";
				miasto_nazwe = miasto_nazwe - 1;
			}
		
         document.getElementById("id_m" + miasto_nazwe).style.background = "#ebeeff";
	
		id_miasta = document.getElementById("id_miasta_p" + miasto_nazwe);
		miasto_nazwa = document.getElementById("p_" + miasto_nazwe);
        val = miasto_nazwa.innerHTML;
	
        val = val.replace(/<\/span>/gi, '');
	
        val = val.replace(/<span class="wyroznij_miasta">/gi, '');
		val = val.replace(/<span class=wyroznij_miasta>/gi, '');
    document.getElementById("miasto").value = val;
		
		var tempArray = id_miasta.innerHTML;
		var ttempArray = tempArray.replace(/_/gi, '"');
		var myString = new String(ttempArray);
		myArray = myString.split(','); 
		document.getElementById("id_miasta_wyslij").value = myArray[0];
		test=0;
    }
   else
    {
		miasto_nazwe = -1;
		
		
		var miasto = $('#miasto').val();

$.ajax({
	   

  type: "POST",
  url: "odpowiedz_miasta.php",

  
 data: 
 {
        miasto: miasto
 },
 beforeSend: function(){
	 document.getElementById("rensponse").style.display = "block";
     $("#rensponse").html('<div id="wysylanie" style="font-size:10px; padding:2px 2px 2px 2px;  top: -5px; position:relative;" >Trwa wysylanie zapytania. Czekaj...</div>');
   },

  success: function(data)
  {
	 document.getElementById("rensponse").style.display = "block";
    $("#rensponse").html(data);
  }
});

	  
			
    }
  }
}
function ukryj_miasta()
{
	document.getElementById("rensponse").style.display = 'none';
}




