var xmlHttp_I = createXmlHttpRequestObject_I();
var serverAddress_I = "zapisz.php";
var serverAddress_II = "z_dod.php";
var id;
//tworzy obiekt XMLHttpRequest
function createXmlHttpRequestObject_I()
{
  var xmlHttp;

  try
  {
    xmlHttp = new XMLHttpRequest();	
  }		
  catch(e)
  {
    var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
	 											"MSXML2.XMLHTTP.5.0",
												"MSXML2.XMLHTTP.4.0",
												"MSXML2.XMLHTTP.3.0",
												"MSXML2.XMLHTTP",
												"Microsoft.XMLHTTP");
	for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++)
	{
	  try
	  {
	    xmlHttp= new ActiveObject(XmlHttpVersions[i]);	
	  }	
	  catch (e) 
		{//displayError("ptoblrm");
		}
	}
 }	
 //if(!xmlHttp)
   //displayError("Bł&#353;d podczas tworzenia obiektu XMLHttpVersions.");
 //else
   return xmlHttp;	
}		


//wyswietla komunikat o bledzie 
/*
function displayError($message)
{
  if(showErrors)
  {
    showErrors = false;
	 alert("Wyst&#353;pił bł&#353;d :\n" + $message);	
	 setTimeout("validate();", 10000);
  }	
}*/

function imp(inputValue, fieldID)
{ 	   
  // jeśli obiekt nie jest pusty
  if(xmlHttp_I)
  {
	 if(fieldID)
	 { //displayError(fieldID);	
	   inputValue = encodeURIComponent(inputValue);
		fieldID = encodeURIComponent(fieldID);
		cache.push("inputValue="+inputValue+"&fieldID="+fieldID);
	 }
	 try
	 { 
	   if((xmlHttp_I.readyState == 4 || xmlHttp_I.readyState == 0) && cache.length >0)
		{ id=2;
		  var cacheEntry = cache.shift();
		  //displayError(cacheEntry);	
		  xmlHttp_I.open("GET", serverAddress_I+"?"+cacheEntry, true);	
		  xmlHttp_I.setRequestHeader("Content-Type", "application/x-www-from-urlencoded");
		  xmlHttp_I.onreadystatechange = handleRequestStateChange_I;
		  xmlHttp_I.send(null);					
		}
	 }
	 catch(e)
	 { //displayError("qqq");	 
	   displayError(e.toString());
	 }
  }	
}

function dod(inputValue, fieldID, impreza)
{ 	   
  // jeśli obiekt nie jest pusty
  if(xmlHttp_I)
  {
	 if(fieldID)
	 {
	   inputValue = encodeURIComponent(inputValue);
		fieldID = encodeURIComponent(fieldID);
		impreza = encodeURIComponent(impreza);
		cache.push("inputValue="+inputValue+"&fieldID="+fieldID+"&impreza="+impreza);
	 }
	 try
	 { 
	   if((xmlHttp_I.readyState == 4 || xmlHttp_I.readyState == 0) && cache.length >0)
		{ id=3;
		  var cacheEntry = cache.shift();
		  //displayError(cacheEntry);	
		  xmlHttp_I.open("GET", serverAddress_II+"?"+cacheEntry, true);	
		  xmlHttp_I.setRequestHeader("Content-Type", "application/x-www-from-urlencoded");
		  xmlHttp_I.onreadystatechange = handleRequestStateChange_I;
		  xmlHttp_I.send(null);						
		}
	 }
	 catch(e)
	 { displayError("bb");	 
	   displayError(e.toString());
	 }
  }	
}


//obsługuje odpowiedz HTTP
function handleRequestStateChange_I()
{ 
  if(xmlHttp_I.readyState==4)
  { 
    if(xmlHttp_I.status == 200)
	 {
	   try
		{ 
		  //displayError("Kamil");
		    readResponse_I();
		}
		catch(e)
		{
			 displayError("asdk");	 

		  displayError(e.toString());
		}
	 }	
	 else
	 {
	   displayError("tutaj");
	   displayError(xmlHttp_I.statusText);
	 } 
  }	
}

//czyta odpowiedź z serwera
function readResponse_I()
{ 
  var response = xmlHttp_I.responseText;
  if (response.indexOf("ERRNO") >= 0 || response.indexOf("error:") >=0 || response.length == 0)
    throw(response.length == 0 ? "Server error." : response);
  responseXml = xmlHttp_I.responseXML; //zmien na RESPONSE XML oraz nazwe serwera !!!!
  
  //displayError(responseXml);	
  xmlDoc= responseXml.documentElement;
  if(id==2)
  {		
  result = xmlDoc.getElementsByTagName("result")[0].firstChild.data;
  fieldID = xmlDoc.getElementsByTagName("fieldid")[0].firstChild.data;	
  cena = xmlDoc.getElementsByTagName("cena")[0].firstChild.data;	
	
  //displayError(result);
  message = document.getElementById("opis_imprezy");
  message.innerHTML = result;
	
  message3 = document.getElementById("c");	
  message3.innerHTML = cena;
	
  message1 = document.getElementById("ubezpieczenie");	
  message1.className = (fieldID[0] == "p" ? "error1" : "hidden");
  message1.checked = false; 

	
  message2 = document.getElementById("upo");		
  message2.className = (fieldID[0] == "p" ? "error1" : "hidden");
	
  message1 = document.getElementById("rakiety");		
  message1.className = (fieldID[1] == "r" ? "error1" : "hidden");
  message1.checked = false; 	
	
  message2 = document.getElementById("ro");		
  message2.className = (fieldID[1] == "r" ? "error1" : "hidden");
	
  message2 = document.getElementById("pasp");			
  message2.className = (fieldID[2] == "p" ? "error3" : "hidden");
	
  message2 = document.getElementById("pasp_p");				
  message2.className = (fieldID[2] == "p" ? "error3" : "hidden");
	
  message4 = document.getElementById("pasp_ps");		
  message4.className = "hidden";
	
  //cena = document.getElementById("cena");	
  //cena.innerHTML = result;
	
  //message.className = (result == "0" ? "error" : "hidden");
  //message1.className = "hidden";
		
  setTimeout("imp();", 500);
  }
  if(id==3)
  {
			 //displayError("oooo");	 
	 cena = xmlDoc.getElementsByTagName("cena")[0].firstChild.data;	
	 message3 = document.getElementById("c");	
    message3.innerHTML = cena;
	 setTimeout("dod();", 500);
  }					  					
}


