//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 ChangePosition(x,y,width)
{
   document.getElementById('id_red_strelka').style.marginLeft = x;
   //document.getElementById('id_red_block').style.marginLeft = x;
   document.getElementById('id_fon_menu').style.marginLeft = y;
   document.getElementById('id_fon_menu').style.width = width;
}

function CheckFeedback()
{
  if(document.getElementById('id_who_name').value=='' || document.getElementById('id_who_mail').value=='' || document.getElementById('id_comment').value=='')
    {
      alert('Все поля обязательны для заполнения');
      return false;
    }
  if(document.getElementById('id_proverka').value != document.getElementById('id_proverka2').value)
	  {
	   alert('Проверочное число введено неправильно');
	      return false;
	  }
  return true;
}

//Получение списка поэтов и последних добавленных стихотворений
function GetPoemsAndPoets()
{
   if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
	 {		
		  xmlHttp.open("GET", "/ajax.php?z="+Math.random()+"&do=2", true);
		  xmlHttp.onreadystatechange = handleServerResponseGetPoemsAndPoets;
		  GetLastPoems();
		  xmlHttp.send(null);
	 }
	 else	setTimeout('GetPoemsAndPoets()', 1000);
}
function handleServerResponseGetPoemsAndPoets()
{
   if (xmlHttp.readyState == 4)
	 {
		  if (xmlHttp.status == 200)
		    {	
			     //helloMessage = xmlHttp.responseText;
			     xmlResponse = xmlHttp.responseXML;
			     xmlDocumentElement = xmlResponse.documentElement;
			     helloMessage = xmlDocumentElement.firstChild.data;
			     var ar=helloMessage.split('|||');
			     var len=ar.length;
			     var str_poets='<b>Авторы произвед<span class="under_line">ений:</span></b><br>';
			     for (i=(len-2); i>=0; i--)
				     {
				       var ar2=ar[i].split('+++');
				       if(i==0)
				          str_poets += '<a href="/poet/'+ar2[0]+'/">'+ar2[1]+'</a>';
				       else
				          str_poets += '<a href="/poet/'+ar2[0]+'/">'+ar2[1]+'</a>,';
				     }
				   document.getElementById('id_authors_text').innerHTML = str_poets;
		    }
		  else
			  {alert("There was a problem accessing the server: " +xmlHttp.statusText);}
	 }
}

//Получение последних стихотворений
function GetLastPoems()
{
   if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
	 {		
	    xmlHttp.open("GET", "/ajax.php?z="+Math.random()+"&do=3", true);
		  xmlHttp.onreadystatechange = handleServerResponseGetLastPoems;
		  xmlHttp.send(null);
	 }
	 else	setTimeout('GetLastPoems()', 1000);
}
function handleServerResponseGetLastPoems()
{
   if (xmlHttp.readyState == 4)
	 {
		  if (xmlHttp.status == 200)
		    {	
			     //helloMessage = xmlHttp.responseText;
			     xmlResponse = xmlHttp.responseXML;
			     xmlDocumentElement = xmlResponse.documentElement;
			     helloMessage = xmlDocumentElement.firstChild.data;
			     var ar=helloMessage.split('|||');
			     var len=ar.length;
			     var str_poems='<div class="last_poems_text"> <b>Последние стихотвор<span class="under_line">ения:</span></b><br>';
			     for (i=(len-2); i>=0; i--)
			       {
			         var ar2=ar[i].split('+++');
			         str_poems += '<a href="/poem/'+ar2[0]+'/">'+ar2[1]+'</a><br>';
			       }
			     document.getElementById('last_poems_text').innerHTML = str_poems;
		    }
		  else
			  {alert("There was a problem accessing the server: " +xmlHttp.statusText);}
	 }
}

//Вход в личный кабинет
function EnterInMy()
{
  Dialog.confirm($('login').innerHTML, {className:"alphacube", width:400, okLabel: "войти", cancelLabel: "закрыть",onOk:function(win){
		var login = document.getElementById('id_login').value;
		var password = document.getElementById('id_password').value;
		new Ajax.Request('/ajax.php?name='+name, {parameters:'login='+login+'&password='+password+'&do=6', onSuccess:handlerFunc, onFailure:errFunc});
	}});
}

function handlerFunc(t)
{
	xmlResponse = t.responseXML;
	xmlDocumentElement = xmlResponse.documentElement;
	helloMessage = xmlDocumentElement.firstChild.data;
	if (helloMessage==1)
		{
		  document.location.href='/';
			return true;
		}
	else
		{
			$('login_error_msg').innerHTML='Вы неверно ввели логин или пароль. Попробуйте еще раз.'; $('login_error_msg').show(); Windows.focusedWindow.updateHeight(); new Effect.Shake(Windows.focusedWindow.getId()); return false;
		}
}

function errFunc()
{
	alert('Ошибка выполнения запроса');
}
