var xmlHttp = createXmlHttpRequestObject();
function createXmlHttpRequestObject()
{
	var xmlHttp;
	
		try
		{
			xmlHttp = new XMLHttpRequest();
		}
		catch (e)
		{
			xmlHttp = false;	
		}

	if (!xmlHttp)
	alert("Ошибка создания объекта XMLHttpRequest");
	else
	return xmlHttp;
}

function oblast2(id)
{

	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
	{
		xmlHttp.open("GET", "/ajax/oblast2.php?id=" + id, true);
		xmlHttp.onreadystatechange = handleServerResponse;
		xmlHttp.send(null);
	}
	else
	{
		setTimeout('oblast2()', 100);
	}
}

function handleServerResponse()
{
	
    if(xmlHttp.readyState == 2)
    {
        document.getElementById("loading").style.display = 'block';
        //alert('1');
    }
    else if (xmlHttp.readyState == 4)
	{
		if (xmlHttp.status == 200)
		{
            xmlResponse = xmlHttp.responseText;
			document.getElementById("oblast").innerHTML = xmlResponse;
            setTimeout('document.getElementById("loading").style.display = "none"', 500);
            //alert('2');
		}
		else
		{
			alert("Возникла ошибка");
		} 
	}
    
  
}


//Вывод города

function city(id)
{

	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
	{
		xmlHttp.open("GET", "/ajax/city.php?id=" + id, true);
		xmlHttp.onreadystatechange = handleServerResponsecity;
		xmlHttp.send(null);
	}
	else
	{
		setTimeout('city()', 100);
	}
}


function handleServerResponsecity()
{
    
    if(xmlHttp.readyState == 2)
    {
        document.getElementById("loading").style.display = 'block';
        //alert('1');
    }
	else if (xmlHttp.readyState == 4)
	{
		if (xmlHttp.status == 200)
		{
			xmlResponse = xmlHttp.responseText;
			document.getElementById("city").innerHTML = xmlResponse;
            setTimeout('document.getElementById("loading").style.display = "none"', 500); 
		}
		else
		{
			alert("Возникла ошибка");
		}
	}
}

//Вывод раздела
function razdel2(id)
{
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
	{
		xmlHttp.open("GET", "/ajax/razdel.php?id=" + id, true);
		xmlHttp.onreadystatechange = handleServerResponserazdel;
		xmlHttp.send(null);
	}
	else
	{
		setTimeout('razdel2()', 100);
	}
    
}


function handleServerResponserazdel()
{
	if (xmlHttp.readyState == 4)
	{
		if (xmlHttp.status == 200)
		{
			xmlResponse = xmlHttp.responseText;
			document.getElementById("razdel2").innerHTML = xmlResponse;
		}
		else
		{
			alert("Возникла ошибка");
		}
	}
}

//Запись в блокнот
function add_notepad(id)
{
		xmlHttp.open("GET", "/ajax/add_notepad.php?id=" + id, true);
		xmlHttp.onreadystatechange = function(){if(xmlHttp.readyState==4){xhrHandler(id)}};
		xmlHttp.send(null);
}

function xhrHandler(id)
{
	xmlResponse = xmlHttp.responseText;
	document.getElementById(id).innerHTML = xmlResponse;
}

//Удаление с блокнота
function del_notepad(id)
{
        xmlHttp.open("GET", "/ajax/del_notepad.php?id=" + id, true);
        xmlHttp.onreadystatechange = function(){if(xmlHttp.readyState==4){xhrHandlerdel(id)}};
        xmlHttp.send(null);
}

function xhrHandlerdel(id)
{
    xmlResponse = xmlHttp.responseText;
    document.getElementById(id).innerHTML = xmlResponse;
}
