var xmlHttp = createXmlHttpRequestObject();
var serverAddress = "contlink.php";
var updateInterval = 5;
var errorRetryInterval = 30;
var debugMode = true;
var strParamProcess = "tema&sq=Общество";
var teAction = 0;
var tekRazd;
var tekTema;
var checkedButton="";
function createXmlHttpRequestObject() 
{ 
  var xmlHttp;
    xmlHttp = null;
    if (window.XMLHttpRequest) {
        try {
            xmlHttp = new XMLHttpRequest();
        } catch (e){}
    } else if (window.ActiveXObject) {
        try {
            xmlHttp = new ActiveXObject('Msxml2.XMLHTTP');
        } catch (e){
            try {
                xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
            } catch (e){}
        }
    }
  if (!xmlHttp)
    alert("Error creating the XMLHttpRequest object.");
  else 
    return xmlHttp;
}

function display($message)
{
  if ((!(teAction == 8)) && (!(teAction == 5)) && (!(teAction == 99)))
  {  
 	myDiv = document.getElementById("caraz");
	myDiv.innerHTML = tekRazd;
  	myDiv = document.getElementById("catem");
	myDiv.innerHTML = tekTema;	
  }
  if (teAction == 0)
  {
  	myDiv = document.getElementById("mllitems");
	myDiv.innerHTML = "";
  	myDiv = document.getElementById("ContTem");	
  } else {
  if (teAction == 1)
  {
  	myDiv = document.getElementById("mllitems");
  } else {
  if (teAction==3)	
  {
  	myDiv = document.getElementById("mllitems");
	myDiv.innerHTML = "";
  	myDiv = document.getElementById("ContTem");	
	myDiv.innerHTML = "";
	myDiv = document.getElementById("mlrazdel");	
  } else {
  if (teAction == 5)
  {	
  	myDiv = document.getElementById("cbtem");
  } else {
  if (teAction==8)	
  {
    myDiv = document.getElementById("apld");		
  } else { if (teAction==99)
     alert($message);
  }
  }}}}
   
  // display message
  if (!(teAction==99))
    myDiv.innerHTML = $message + "<br />";  
}
function displayError($message)
{
  teAction=99; //error
  display("Ошибка получения данных! Повтор попытки через " +
          errorRetryInterval + " секунд." + 
          (debugMode ? "<br/>" + $message : ""));
  setTimeout("process(\""+strParamProcess+"\");", errorRetryInterval * 1000);
}
function process(pq){
  if (xmlHttp)
  {
    try
    { 
      strParamProcess = pq;
	  var arParam = new Array();
	  var arP = new Array();
	  arParam = strParamProcess.split('&');
	  if (strParamProcess.substr(0,3)=="pld")
	  { 
	    teAction = 8;	// pwd
	  } else {
	  if (strParamProcess.substr(0,4)=="link")
	  {
	  	teAction = 1;
		var ss = arParam[1];
		arP = ss.split('=');
		var sss = arP[1];
		pq = 'link&sq='+encodeURI(sss);
		ss = arParam[2];
		arP = ss.split('=');
		sss = arP[1];
		pq += '&st='+encodeURI(sss);
	  } else {
  	  if (strParamProcess.substr(0,6)=="razdel")
	  {
 		teAction = 3;
	  } else {
  	  if (strParamProcess.substr(0,6)=="cbtema")
	  {
 		teAction = 5;
		var ss = arParam[1];
		arP = ss.split('=');
		var sss = arP[1];
		pq = arParam[0]+'&sq='+encodeURI(sss);		
	  } else {
        teAction = 0;
		var ss = arParam[1];
		arP = ss.split('=');
		var sss = arP[1];
		pq = arParam[0]+'&sq='+encodeURI(sss);
      }}}}
	  SetValRzdTem();	  	
	  GetSel();      
	  pq="action="+pq+checkedButton+"&tess="+document.getElementById("tess").value;
	  display("Минуточку...")
      xmlHttp.open("POST", serverAddress, true);	  
      xmlHttp.setRequestHeader("Accept-Charset", "windows-1251"); // ralp add 5 str: 
      xmlHttp.setRequestHeader("Accept-Language","ru, en");	  
	  xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 	  
      xmlHttp.onreadystatechange = handleGettingNews;
      xmlHttp.send(pq);      
    }
    catch(e)
    {	
	  displayError(e.toString()+' Not send!');
    }      
  }
}
function handleGettingNews() 
{
  if (xmlHttp.readyState == 4) 
  {  
    myDiv.innerHTML = "";
    if (xmlHttp.status == 200) 
    {
      try
      {
        getNews();
      }
      catch(e)
      {
        displayError(e.toString()+' _!!');
      }
    } 
    else
    {
      displayError(xmlHttp.statusText+' _!!!');   
    }
  }
}
function getNews()
{
  var response = xmlHttp.responseText;
  if (response.indexOf("ERRNO") >= 0 
      || response.indexOf("error") >= 0
      || response.length == 0)
    throw(response.length == 0 ? "Server error." : response);
  display(response);
}



function SetValRzdTem(){
  mpa=strParamProcess.split("&");	
  if (teAction==0){
  	tekRazd="<font size=-2> Выбран раздел: </font>&nbsp"+mpa[1].substr(3);
	tekTema="<font class=redfon>&nbsp;&nbsp; Укажите тему&nbsp;&nbsp; </font>";
  } else {
  if (teAction==1){
  	tekRazd="<font size=-2> Выбран раздел: </font>&nbsp;"+mpa[1].substr(3);
	tekTema="<font size=-2>  Тема: </font>&nbsp;"+mpa[2].substr(3);
  } else {
  if (teAction==3){
  	tekRazd="<font class=redfon> Сначала выберите раздел </font>";
	tekTema=" Затем - выберите тему ";
  } else {
  if (teAction==8){
	serverAddress='/slinks/'+serverAddress;
	checkedButton=mpa;		 
  }}}}
}
function GetSel()
{ if (teAction==5){
	checkedButton=""; 
  } else {
      if (teAction==8)
	  {  
	    checkedButton= -8;
	  } else {      
	  checkedButton = -1;
	  for (var i = 0; i < document.fsel.tesel.length; i++)
	    {
	      if (document.fsel.tesel[i].checked=="1") {
	         checkedButton=document.fsel.tesel[i].value}
	    }	
	  if (document.fsel.bestsel)
	    if (document.fsel.bestsel.checked) checkedButton=1+checkedButton;
	  if (document.fsel.privatsel) 
	    if (document.fsel.privatsel.checked) checkedButton= 2+checkedButton;  
	  }
	  checkedButton="&us="+checkedButton;
   } 
}
function _onclickTema(nra)
{
	process("tema&sq="+nra);
}
function _onclickLinks(rzd,tem)
{
	process("link&sq="+rzd+"&st="+tem);
}
function RefreshListRazdelUsel(){
	process("razdel");
}

