var idToBeAltered;


function toggleblocks(tagid) {
	var whichpost = document.getElementById(tagid);
	if (whichpost.className == "expandblock") {
	
		if (idToBeAltered != 0) {
			idToBeAltered = 0;
			document.getElementById('txtAnecdote').value = "";
			document.getElementById('writeButton').innerHTML = "Запиши";	
		}
		else {
			whichpost.className="collapseblock";	
		}
	}
	else {
		whichpost.className="expandblock";
		idToBeAltered = 0;
	}
}

function deleteAnec(id){
 var ajaxRequest;
	
 try{
   ajaxRequest = new XMLHttpRequest();
 }catch (e){
   try{
      ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
   }catch (e) {
      try{
         ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
      }catch (e){
         alert("Your browser broke!");
         return false;
      }
   }
 }

 ajaxRequest.onreadystatechange = function(){
   if(ajaxRequest.readyState == 4){
   	
   	  document.getElementById('infobar').style.display = "";
      document.getElementById('infobar').innerHTML = ajaxRequest.responseText;
      document.getElementById('row_' + id).style.display = "none";
   }
 }
 
 ajaxRequest.open("POST", "includes/anecdote.php", true);
 ajaxRequest.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
 ajaxRequest.send("do=delete&id=" + id); 
}



function changeStatusAnec(id){
 var ajaxRequest;
	
 try{
   ajaxRequest = new XMLHttpRequest();
 }catch (e){
   try{
      ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
   }catch (e) {
      try{
         ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
      }catch (e){
         alert("Your browser broke!");
         return false;
      }
   }
 }

 ajaxRequest.onreadystatechange = function(){
   if(ajaxRequest.readyState == 4){
   	  document.getElementById('infobar').style.display = "";
      document.getElementById('infobar').innerHTML = ajaxRequest.responseText;
      
      var elOne = document.getElementById('noactive_' + id);
      if (elOne) {
	  	elOne.innerHTML = "Aктивен";
		elOne.id = 'active_' + id;	
	  } 
	  else {
		  var elTwo = document.getElementById('active_' + id);
	      if (elTwo) {
		  	elTwo.innerHTML = "Неактивен";
			elTwo.id = 'noactive_' + id;	
		  }
	  }
           

   }
 }
 
 ajaxRequest.open("POST", "includes/anecdote.php", true);
 ajaxRequest.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
 ajaxRequest.send("do=change&id=" + id); 
}



function alterTextAnec(id){
 var ajaxRequest;
	
 try{
   ajaxRequest = new XMLHttpRequest();
 }catch (e){
   try{
      ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
   }catch (e) {
      try{
         ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
      }catch (e){
         alert("Your browser broke!");
         return false;
      }
   }
 }

 ajaxRequest.onreadystatechange = function(){
   if(ajaxRequest.readyState == 4){
   		xmlDoc = ajaxRequest.responseXML;
   		
  		document.getElementById('txtBlock').className = "expandblock";
   	 	document.getElementById('infobar').style.display = "";
      	document.getElementById('infobar').innerHTML = xmlDoc.getElementsByTagName('info')[0].firstChild.nodeValue;
		document.getElementById('txtAnecdote').value = xmlDoc.getElementsByTagName('text')[0].firstChild.nodeValue;
		document.getElementById('writeButton').innerHTML = "Промени";
		idToBeAltered = id;
   }
 }
 
 ajaxRequest.open("POST", "includes/anecdote.php", true);
 ajaxRequest.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
 ajaxRequest.send("do=alter&id=" + id); 	

}



function writeAnec(id){
 var ajaxRequest;
	
 try{
   ajaxRequest = new XMLHttpRequest();
 }catch (e){
   try{
      ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
   }catch (e) {
      try{
         ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
      }catch (e){
         alert("Your browser broke!");
         return false;
      }
   }
 }


 ajaxRequest.onreadystatechange = function(){
	if(ajaxRequest.readyState == 4){
				
	xmlDoc = ajaxRequest.responseXML;
	
	// Put down new anecdote
	if (id == 0 && xmlDoc.getElementsByTagName('error')[0].firstChild.nodeValue == 0) {
		var theNewRow = document.createElement('tr');	
		for (var i = 0; i < 7; i++) {		
			var theNewCell = document.createElement('td');
			if (i == 0) {
				theNewCell.appendChild(document.createTextNode(xmlDoc.getElementsByTagName('id')[0].firstChild.nodeValue));	
			}
			if (i == 1) {
				theNewCell.innerHTML = '<a href="javascript:alterTextAnec(' + xmlDoc.getElementsByTagName('id')[0].firstChild.nodeValue + ')">' +
				xmlDoc.getElementsByTagName('text')[0].firstChild.nodeValue + '</a>';
				theNewCell.setAttribute('id', 'txt');	
			}
			if (i == 2) {
				var aLink = document.createElement('a'); 
				aLink.setAttribute('id', 'active_' + xmlDoc.getElementsByTagName('id')[0].firstChild.nodeValue);
				aLink.setAttribute('href', 'javascript:changeStatusAnec(' + xmlDoc.getElementsByTagName('id')[0].firstChild.nodeValue + ')');
				aLink.appendChild(document.createTextNode("Aктивен"));
				theNewCell.appendChild(aLink);	
			}
			if (i == 3) {
				theNewCell.appendChild(document.createTextNode("0"));	
			}
			if (i == 4) {
				var boldTag = document.createElement('b');
				boldTag.appendChild(document.createTextNode("0"));
				theNewCell.appendChild(boldTag);	
			}
			if (i == 5) {
				theNewCell.appendChild(document.createTextNode(xmlDoc.getElementsByTagName('dateadded')[0].firstChild.nodeValue));	
			}
			if (i == 6) {
				var bLink = document.createElement('a'); 
				bLink.setAttribute("href", "javascript:showDialog('Потвърждение', 'Сигурни ли сте, че искате да изтриете избрания виц?', 'prompt', 0, 'deleteAnec('," + xmlDoc.getElementsByTagName('id')[0].firstChild.nodeValue + ");");			
				bLink.appendChild(document.createTextNode("Изтрии"));
				theNewCell.appendChild(bLink);	
			}
			theNewRow.appendChild(theNewCell);	
		}
		
		theNewRow.setAttribute('id', 'row_' + xmlDoc.getElementsByTagName('id')[0].firstChild.nodeValue);
		theNewRow.style.backgroundColor = "#E8EFE6";
					
		if (document.getElementById('tableHeader').nextSibling) {
			document.getElementById('tableAnecdotes').insertBefore(theNewRow, document.getElementById('tableHeader').nextSibling);	
		} else {
			document.getElementById('tableAnecdotes').appendChild(theNewRow);	
		}
	}
	
	//Alter an existing anecdote
	if (id != 0 && xmlDoc.getElementsByTagName('error')[0].firstChild.nodeValue == 0) {
		document.getElementById('row_' + id).style.backgroundColor = "#E8EFE6";
		document.getElementById('row_' + id).childNodes[1].innerHTML =  
		'<a href="javascript:alterTextAnec(' + id + ')">' + xmlDoc.getElementsByTagName('text')[0].firstChild.nodeValue + '</a>';
		
		
		
	} 	  	
	  	  	  		  		  	
		document.getElementById('infobar').style.display = ""; 
		document.getElementById('infobar').innerHTML = xmlDoc.getElementsByTagName('info')[0].firstChild.nodeValue;
		document.getElementById('txtAnecdote').value = "";
		document.getElementById('txtBlock').className = "collapseblock";
		document.getElementById('writeButton').innerHTML = "Запиши";
   }
 }

 var postStr = "do=write&id=" + id + "&txtAnecdote=" + encodeURI(document.getElementById("txtAnecdote").value);
 ajaxRequest.open("POST", "includes/anecdote.php", true);
 ajaxRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
 ajaxRequest.setRequestHeader("Content-length", postStr.length);
 ajaxRequest.setRequestHeader("Connection", "close");
 ajaxRequest.send(postStr);
}
