var xmlhttp_wall = new Array();
var xmlhttp_wall_comment= new Array();
var curr_wall_div;
var curr_wall_comment_id;
var curr_wall_id;
var showwall = true;

setTimeout("refreshWall()",300000);


function refreshWall(){
	 if(document.getElementById("wall_level1").style.display!="none"){
		 var status = true;
		 var f = document.forms.wallform;
		  for (var n=0; n < f.elements.length; n++) {
			  //formElements += n + ":" + f.elements[n].id + "\n";
			  
			  if(f.elements[n].id.indexOf("write_a_comment") > -1){
				  	if(document.getElementById(f.elements[n].id).style.display=="none"){
						setTimeout("refreshWall()",300000);
						status = false;
						
						break;
					}
			  }
		  }
		 
		  if(status){
			  setTimeout("refreshWall()",300000);
			  getWall(0);
		  }
		  
	 }
	 
	

}

function showWallWrite(){
		
		document.getElementById("wall_level1").style.display="none";
		document.getElementById("wall_level2").style.display="block";
		document.getElementById("wallinfo").focus();
}

function cancelWall(){
	
	if(!document.getElementById("wallinfo").disabled){
		document.getElementById("wallinfo").value="";
		document.getElementById("wall_level1").style.display="block";
		document.getElementById("wall_level2").style.display="none";
	}
}

function shareWall(ajaxid){
	
	
	if(!document.getElementById("wallinfo").disabled && document.getElementById("wallinfo").value.trim() != ""){
		showwall = true;
		$data = "wallinfo="+document.getElementById("wallinfo").value;
		curr_wall_div = "wall_level2";
		wallSendRequest("/modules/wall.php",$data+"&action=save",ajaxid);
		//document.getElementById("wall_level2").innerHTML="<img src='/images/loading.gif'>";
		document.getElementById("wall_level2").style.opacity=.5;
		document.getElementById("wallinfo").disabled=true;
	}
	
	//if(!document.getElementById("wallinfo").disabled )
	//document.getElementById("wallinfo").focus();
}

function wallSendRequest(url,param,ajaxid)
{
	xmlhttp_wall[ajaxid]=GetXmlHttpObject();
	if (xmlhttp_wall[ajaxid]==null)
	{
		alert ("Browser does not support HTTP Request");
		return;
	}
	
	
	url=url+"?sid="+Math.random();
	
	xmlhttp_wall[ajaxid].open("POST",url,true);
	xmlhttp_wall[ajaxid].setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp_wall[ajaxid].setRequestHeader("Content-length", param.length);
	xmlhttp_wall[ajaxid].setRequestHeader("Connection", "close");
	
	xmlhttp_wall[ajaxid].onreadystatechange=function(){
		if (xmlhttp_wall[ajaxid].readyState==4)
		{
			xmlhttp_wall[ajaxid] = "";
			
			getWall(ajaxid);
			document.getElementById("wall_level1").style.display="block";
			document.getElementById("wall_level2").style.display="none";
			document.getElementById("wall_level2").disabled=false;
			document.getElementById("wallinfo").value="";
			document.getElementById("wallinfo").disabled=false;
			document.getElementById("wall_level2").style.opacity=1;
		}
	};
	
	
	xmlhttp_wall[ajaxid].send(param);
}

function getWall(ajaxid){
	
	xmlhttp_wall[ajaxid]=GetXmlHttpObject();
	if (xmlhttp_wall[ajaxid]==null)
  {
 	 alert ("Browser does not support HTTP Request");
  	return;
  }

var url = "/modules/wall.php"
url=url+"?sid="+Math.random();

xmlhttp_wall[ajaxid].open("POST",url,true);
xmlhttp_wall[ajaxid].setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp_wall[ajaxid].setRequestHeader("Content-length", 1000);
xmlhttp_wall[ajaxid].setRequestHeader("Connection", "close");
xmlhttp_wall[ajaxid].onreadystatechange=function (){
	if (xmlhttp_wall[ajaxid].readyState==4)
	{
		if(showwall)
			document.getElementById("wall_level3").innerHTML=xmlhttp_wall[ajaxid].responseText;
	}
};

xmlhttp_wall[ajaxid].send("action=getwall");
	
}
/*function stateChanged_wall()
{
	if (xmlhttp_wall.readyState==4)
	{
		
		
		getWall();
		document.getElementById("wall_level1").style.display="block";
		document.getElementById("wall_level2").style.display="none";
		document.getElementById("wall_level2").disabled=false;
		document.getElementById("wallinfo").value="";
		document.getElementById("wallinfo").disabled=false;
		document.getElementById("wall_level2").style.opacity=1;
	}
}*/

/*function stateChanged_wall_get()
{
	if (xmlhttp_wall.readyState==4)
	{
		
		
		if(showwall)
		document.getElementById("wall_level3").innerHTML=xmlhttp_wall.responseText;
	
	}
}*/


function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  return new XMLHttpRequest();
  }
if (window.ActiveXObject)
  {
  // code for IE6, IE5
  return new ActiveXObject("Microsoft.XMLHTTP");
  }
return null;
}


if(typeof String.prototype.trim !== 'function') {
  String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/, ''); 
  }
}


function showWallComment(wall_comment_id){
	showwall = false;
	document.getElementById('write_a_comment'+wall_comment_id).style.display="none";
	document.getElementById('commentwindow'+wall_comment_id).style.display="block";
	document.getElementById('comment'+wall_comment_id).focus();
	
}

function cancelWallComment(wall_comment_id){
	showwall = true;
	document.getElementById('write_a_comment'+wall_comment_id).style.display="block";
	document.getElementById('commentwindow'+wall_comment_id).style.display="none";
	document.getElementById('comment'+wall_comment_id).value="";
}


function addWallComment(wall_id,ajaxid){
	
	
	
	if(document.getElementById('comment'+wall_id).value.trim() != ""){
	
		var comment = document.getElementById('comment'+wall_id).value;
		
		document.getElementById('commentwindow'+wall_id).innerHTML="<img src='/images/loading.gif'>";
		
		xmlhttp_wall_comment[ajaxid]=GetXmlHttpObject();
		if (xmlhttp_wall_comment[ajaxid]==null)
		  {
		  alert ("Browser does not support HTTP Request");
		  return;
		  }
		var url="/modules/wall.php";
		
		url=url+"?sid="+Math.random();
		
		var param = "comment="+comment+"&wall_id="+wall_id+"&action=addcomment";
	
		
		xmlhttp_wall_comment[ajaxid].open("POST",url,true);
		xmlhttp_wall_comment[ajaxid].setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlhttp_wall_comment[ajaxid].setRequestHeader("Content-length", param.length);
		xmlhttp_wall_comment[ajaxid].setRequestHeader("Connection", "close");
		
		xmlhttp_wall_comment[ajaxid].onreadystatechange=function (){
			if (xmlhttp_wall_comment[ajaxid].readyState==4)
			{
				xmlhttp_wall_comment[ajaxid]="";
				getWallComments(wall_id,ajaxid);
				
			}
		};
		
		xmlhttp_wall_comment[ajaxid].send(param);
	}
	
}

/*function stateChanged_wall_comment()
{
	if (xmlhttp_wall_comment.readyState==4)
	{
		
		getWallComments();
		//getWall();
		/*document.getElementById("wall_level1").style.display="block";
		document.getElementById("wall_level2").style.display="none";
		document.getElementById("wall_level2").disabled=false;
		document.getElementById("wallinfo").value="";
		document.getElementById("wallinfo").disabled=false;
		document.getElementById("wall_level2").style.opacity=1;
		
		//alert("done");
	}
}*/

function getWallComments(wall_id,ajaxid){
	
	
	
	xmlhttp_wall_comment[ajaxid]=GetXmlHttpObject();
	if (xmlhttp_wall_comment[ajaxid]==null)
	  {
	  alert ("Browser does not support HTTP Request");
	  return;
	  }
	var url="/modules/wall.php";
	
	url=url+"?sid="+Math.random();
	
	
	
	var param = "wall_id="+wall_id+"&action=getcomments&ajax_id="+ajaxid;
	
	xmlhttp_wall_comment[ajaxid].open("POST",url,true);
	xmlhttp_wall_comment[ajaxid].setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp_wall_comment[ajaxid].setRequestHeader("Content-length", param.length);
	xmlhttp_wall_comment[ajaxid].setRequestHeader("Connection", "close");
	
	xmlhttp_wall_comment[ajaxid].onreadystatechange=function (){
		
		if (xmlhttp_wall_comment[ajaxid].readyState==4)
		{
			
			document.getElementById("commentArea"+wall_id).innerHTML=xmlhttp_wall_comment[ajaxid].responseText;
			
		}
	};

	xmlhttp_wall_comment[ajaxid].send(param);
}

/*function stateChanged_wall_comment_get()
{
	if (xmlhttp_wall_comment.readyState==4)
	{
		
		document.getElementById("commentArea"+curr_wall_id).innerHTML=xmlhttp_wall_comment.responseText;
		
	}
}*/

function viewAllComments(wall_id,ajaxid){
	
	document.getElementById('commentArea'+wall_id).innerHTML="<img src='/images/loading.gif'>";
	
	xmlhttp_wall_comment[ajaxid]=GetXmlHttpObject();
	if (xmlhttp_wall_comment[ajaxid]==null)
	  {
	  alert ("Browser does not support HTTP Request");
	  return;
	  }
	var url="/modules/wall.php";
	
	url=url+"?sid="+Math.random();
	curr_wall_id=wall_id;
	var param = "wall_id="+wall_id+"&action=getallcomments&ajax_id="+ajaxid;
	
	xmlhttp_wall_comment[ajaxid].open("POST",url,true);
	xmlhttp_wall_comment[ajaxid].setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp_wall_comment[ajaxid].setRequestHeader("Content-length", param.length);
	xmlhttp_wall_comment[ajaxid].setRequestHeader("Connection", "close");
	
	xmlhttp_wall_comment[ajaxid].onreadystatechange=function (){
		if (xmlhttp_wall_comment[ajaxid].readyState==4)
		{
			
			document.getElementById("commentArea"+wall_id).innerHTML=xmlhttp_wall_comment[ajaxid].responseText;
			
		}
	};
	
	xmlhttp_wall_comment[ajaxid].send(param);
}

function removeWall(wall_id,ajaxid){
	if(confirm("Are you sure you want to remove")){
		removeWallConfirmed(wall_id,ajaxid);
	}
}

function removeWallConfirmed(wall_id,ajaxid){
	
	document.getElementById('wall_container'+wall_id).innerHTML="<img src='/images/loading.gif'>";
	
	xmlhttp_wall[ajaxid]=GetXmlHttpObject();
	if (xmlhttp_wall[ajaxid]==null)
	  {
	  alert ("Browser does not support HTTP Request");
	  return;
	  }
	var url="/modules/wall.php";
	
	url=url+"?sid="+Math.random();
	
	var param = "wall_id="+wall_id+"&action=removewall";
	
	xmlhttp_wall[ajaxid].open("POST",url,true);
	xmlhttp_wall[ajaxid].setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp_wall[ajaxid].setRequestHeader("Content-length", param.length);
	xmlhttp_wall[ajaxid].setRequestHeader("Connection", "close");
	
	xmlhttp_wall[ajaxid].onreadystatechange=function(){
		if (xmlhttp_wall[ajaxid].readyState==4)
		{
			
			getWall(ajaxid);
			
		}
	};
	
	xmlhttp_wall[ajaxid].send(param);
}


/*function stateChanged_wall_remove()
{
	if (xmlhttp_wall.readyState==4)
	{
		
		getWall();
		
	}
}*/

function removeWallComment(wall_comment_id,wall_id,ajaxid){
	if(confirm("Are you sure you want to remove this comment")){
		removeWallCommentConfirmed(wall_comment_id,wall_id,ajaxid);
	}
}

function removeWallCommentConfirmed(wall_comment_id,wall_id,ajaxid){
	
	document.getElementById('commentArea'+wall_id).innerHTML="<img src='/images/loading.gif'>";
	
	xmlhttp_wall_comment[ajaxid]=GetXmlHttpObject();
	if (xmlhttp_wall_comment[ajaxid]==null)
	  {
	  alert ("Browser does not support HTTP Request");
	  return;
	  }
	var url="/modules/wall.php";
	
	url=url+"?sid="+Math.random();
	curr_wall_id = wall_id;
	var param = "wall_comment_id="+wall_comment_id+"&action=removewallcomment";
	
	xmlhttp_wall_comment[ajaxid].open("POST",url,true);
	xmlhttp_wall_comment[ajaxid].setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp_wall_comment[ajaxid].setRequestHeader("Content-length", param.length);
	xmlhttp_wall_comment[ajaxid].setRequestHeader("Connection", "close");
	
	xmlhttp_wall_comment[ajaxid].onreadystatechange=function (){
		if (xmlhttp_wall_comment[ajaxid].readyState==4)
		{
			
			getWallComments(wall_id,ajaxid);
			
		}
	};
	
	xmlhttp_wall_comment[ajaxid].send(param);
}

/*function stateChanged_wall_comment_remove()
{
	if (xmlhttp_wall.readyState==4)
	{
		
		getWallComments();
		
	}
}*/

