var xmlHttp= createXmlHttpRequestObject();
	
var serverAdress1="validacja-baza.php";
var serverAdress2="validacja.php";

var showErrors="true";

var cache = new Array();

function createXmlHttpRequestObject(){
	var xmlHttp;

	try{
		xmlHttp = new XMLHttpRequest();
	}
	catch(e){
		var XmlHttpVersion = 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<XmlHttpVersion.length && ! xmlHttp; i++)
		{
			try{
				xmlHttp = new ActiveXObject(XmlHttpVersion[i]);
			}
			catch(e){
			}
		}
	}

	if(!xmlHttp)
		alert("Błąd podczas tworzenia obiektu XMLHttpRequest");
	else
		return xmlHttp;

}

function displayError($message){
	if(showError){
		alert("Wystąpił błąd: \n" + $message);
		setTimeout('validate()', 10000);
	}
}

function validate(inputValue, fieldID, pole){
	if(xmlHttp){
		if(fieldID){
			
			inputValue=encodeURIComponent(inputValue);
			fieldID=encodeURIComponent(fieldID);
			cache.push("?inputValue=" + inputValue + "&fieldID=" + fieldID);
			try{
				if((xmlHttp.readyState==4 || xmlHttp.readyState==0) && cache.length>0){

					var cacheEntry=cache.shift();
					if(cacheEntry){
						if(pole==1){
							xmlHttp.open("GET", serverAdress1 + cacheEntry, true);
						}
						else if(pole==2){
							xmlHttp.open("GET", serverAdress2 + cacheEntry, true);
						}

						xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
						xmlHttp.onreadystatechange=handleRequestStateChange;
						xmlHttp.send(null);
					}
				}
			}
			catch(e){
				displayError(e.toString());
			}
		}
	}
}

function handleRequestStateChange(){
	if(xmlHttp.readyState==4 && xmlHttp.status==200){
		try{
			readResponse();
		}
		catch(e){
			displayError(e.toString());
		}
	}
	else{
		displayError(xmlResponse.statusText);
	}
}


function readResponse(){

	var response = xmlHttp.responseText;
	if(response.indexOf("ERRNO")>=0 || response.indexOf("error:")>=0 || response.length==0)
		throw(response.length==0 ? "Servererror." :response);

		xmlResponse = xmlHttp.responseXML;


		xmlDoc = xmlResponse.documentElement;

		result = xmlDoc.getElementsByTagName("result")[0].firstChild.data;
				
		fieldID = xmlDoc.getElementsByTagName("fieldid")[0].firstChild.data;
		if(result==0){
			document.getElementById(fieldID + "_error").className = "widoczny";
			document.getElementById(fieldID + "_correct").className = "hidden";
			document.getElementById(fieldID + "_error2").className="widoczny";
		}
		else if(result==2){
			document.getElementById(fieldID + "_error").className = "widoczny";
			document.getElementById(fieldID + "_correct").className = "hidden";
			document.getElementById(fieldID + "_error2").className="hidden";
		}
		else{
			document.getElementById(fieldID + "_correct").className = "widoczny";
			document.getElementById(fieldID + "_error").className = "hidden";
			document.getElementById(fieldID + "_error2").className="hidden";
		}
		setTimeout('validate()', 1000)
		
}

function podkategoria(id){
	
	if(xmlHttp.readyState==4 || xmlHttp.readyState==0){
		var parametry="?kat="+id;
		xmlHttp.open("GET", "inc/podkat.php"+parametry, true);
		xmlHttp.onreadystatechange=handleServerResponse;
		xmlHttp.send(null);
	}
}

function blad(id, numer_bledu){
	if(xmlHttp.readyState==4 || xmlHttp.readyState==0){
		var parametry="?id_ogloszenia="+id+"&numer_bledu="+numer_bledu;
		xmlHttp.open("GET", "inc/blad.php"+parametry, true);
		xmlHttp.onreadystatechange=handleServerResponse;
		xmlHttp.send(null);
		alert("Dziękujemy. Zgłoszenie zostało przyjęte");
	}
}

function przypomnijKod(id){
	if(xmlHttp.readyState==4 || xmlHttp.readyState==0){
		var parametry="?id_ogloszenia="+id;
		xmlHttp.open("GET", "inc/przypomnij_kod.php"+parametry, true);
		xmlHttp.onreadystatechange=handleServerResponse;
		xmlHttp.send(null);
		alert("Kod został wysłany na podany w ogłoszeniu e-mail");
	}
}

function handleServerResponse(){

	if(xmlHttp.readyState==4){

		if(xmlHttp.status==200){

			xmlResponse = xmlHttp.responseXML;
			xmlRoot = xmlResponse.documentElement;
			valueArray=xmlRoot.getElementsByTagName("value");
			nazwaArray=xmlRoot.getElementsByTagName("nazwa");

			var html="<select name=\"podkategoria\">";
			for(var i=0; i<valueArray.length; i++){
				html+="<option value=\""+valueArray.item(i).firstChild.data + "\">" + nazwaArray.item(i).firstChild.data + "</option>";
			}
			html+="</select>";
			document.getElementById("wybor_kategorii").innerHTML=html;
			
			
		}
		else
			alert(xmlHttp.status);
	}
}