//Java Script на главной странице сайта

//подключения объекта xmlHttp, для работы AJAX
var xmlHttp = createXmlHttpRequestObject();
function createXmlHttpRequestObject()
{
	var xmlHttp;
	if(window.ActiveXObject)
	{
		try		{xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");}
		catch (e)	{xmlHttp = false;}
	}
	else
	{
		try		{xmlHttp = new XMLHttpRequest();}
		catch (e)	{xmlHttp = false;}
	}
	if (!xmlHttp)	alert("Error creating the XMLHttpRequest object.");
	else	return xmlHttp;
}

function ShowChangeAuthorRazdel(id)
{
	if(document.getElementById('sbornic_div_'+id).style.display=='none')
		document.getElementById('sbornic_div_'+id).style.display='block';
	else 
		document.getElementById('sbornic_div_'+id).style.display='none';
}

function ShowChangeMainRazdel(id)
{
	if(document.getElementById('catall_div_'+id).style.display=='none')
		document.getElementById('catall_div_'+id).style.display='block';
	else 
		document.getElementById('catall_div_'+id).style.display='none';	
}

function ReadSbornic(id)
{
	if(document.getElementById('id_div_sbor_read'+id).style.display=='none')
	{
		document.getElementById('id_div_sbor_read'+id).style.display='block';
		document.getElementById('id_href_sbor_read'+id).innerHTML='скрыть';
	}
	else
	{
		document.getElementById('id_div_sbor_read'+id).style.display='none';
		document.getElementById('id_href_sbor_read'+id).innerHTML='читать';
	}
}

function ChangeAuthorRazdel(id_poem,id_poet)
{
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
	{		
		var id_razd = document.getElementById('id_sel_sbornic'+id_poem).value;
	  	xmlHttp.open("GET", "/ajax.php?z="+Math.random()+"&do=9&id_poet="+id_poet+"&id_poem="+id_poem+"&id_razd="+id_razd, true);
		xmlHttp.onreadystatechange = handleServerResponseChangeAuthorRazdel;
		xmlHttp.send(null);
	}
	else	setTimeout('ChangeAuthorRazdel();', 1000);
}

function ChangeAuthorRazdelMain(id_poem)
{
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
	{		
		var id_razd = document.getElementById('id_sel_catall'+id_poem).value;
	  	xmlHttp.open("GET", "/ajax.php?z="+Math.random()+"&do=11&id_poem="+id_poem+"&id_razd="+id_razd, true);
		xmlHttp.onreadystatechange = handleServerResponseChangeAuthorRazdel;
		xmlHttp.send(null);
	}
	else	setTimeout('ChangeAuthorRazdelMain();', 1000);
}

function handleServerResponseChangeAuthorRazdel()
{
	if (xmlHttp.readyState == 4)
	{
		if (xmlHttp.status == 200)
		{	
	      	  xmlResponse = xmlHttp.responseXML;
			  xmlDocumentElement = xmlResponse.documentElement;
			  helloMessage = xmlDocumentElement.firstChild.data;
	      	if(helloMessage==1)
		      {
		        document.location.href='/my/';
		      }
		    else
		      {
		      	alert('Ошибка выполнения скрипта');
		      }
		}
		else
			{ alert("There was a problem accessing the server: " +xmlHttp.statusText);}
	}
}

function DelAuthorRazdel(id_razd,id_poet)
{
	if(confirm('Вы действительно хотите удалить сборник?')) {
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
	{		
	  	xmlHttp.open("GET", "/ajax.php?z="+Math.random()+"&do=10&id_razd="+id_razd+"&id_poet="+id_poet, true);
		xmlHttp.onreadystatechange = handleServerResponseChangeAuthorRazdel;
		xmlHttp.send(null);
	}
	else	setTimeout('DelAuthorRazdel();', 1000);
	}
	else {
		return false;
	}
}