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;
}

function ajaxCall(path,query,successFn){
	//new http request object
	var http;
	http=GetXmlHttpObject();
	var url = path;
	var params = query;
	http.open("POST", url, true);

	//Send the proper header information along with the request
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http.setRequestHeader("Content-length", params.length);
	http.setRequestHeader("Connection", "close");

	http.onreadystatechange = function() {
		if(http.readyState == 4 && http.status == 200) {
			successFn(http.responseText);
		}
	}
	http.send(params);
}

function json_decode(json) {
	var bits = eval("(" + json + ")");
	return bits;
}

function showDiv(id) {
	var hideBox = document.getElementById(id);
	if(hideBox.style.display == 'none')
		{
			hideBox.style.display = '';
		}
	else
		{
			hideBox.style.display = 'none';
		}

}

function getDeets(){

	document.getElementById('wait').innerHTML = '<img src="includes/img/loader_sm.gif" />';
	
	var selOther = document.other.id;
	var selOpt = selOther.options[selOther.selectedIndex].value;
	
	var url = 'includes/ajax/getdeets.php';
	var query_string = 'id=' + selOpt;
	ajaxCall(url,query_string,
	function(x){
		document.getElementById('wait').innerHTML = '';
		document.getElementById('deets').innerHTML = x;
	}
	);
}

function getComments(id){
	var url = 'includes/ajax/getcomments.php';
	var query_string = 'id=' + id + '&type=programme';
	
	var comments = document.getElementById('box_' + id);
	var allInputs = comments.getElementsByTagName('input');
		if(allInputs.length == 0)
			{
				ajaxCall(url,query_string,
				function(x){
					var bits = json_decode(x);
					if(bits.success)
						{
							
							comments.innerHTML += bits.comments;
						
							/*
							for(var m in bits.comments)
								{
									var commentBox = document.createElement('div');
									commentBox.className = 'comment_box';

									var user = document.createElement('span');
									user.className = 'user';
									var userText = document.createTextNode(bits.comments[m].user);
									user.appendChild(userText);

									var date = document.createElement('span');
									date.className = 'date';
									var dateText = document.createTextNode(bits.comments[m].date);
									date.appendChild(dateText);

									var blurb = document.createElement('span');
									blurb.className = 'blurb';
									blurbText = document.createTextNode(bits.comments[m].blurb);
									blurb.appendChild(blurbText);

									commentBox.appendChild(user);
									commentBox.appendChild(blurb);
									commentBox.appendChild(date);

									comments.appendChild(commentBox);
								}
							*/
						}
   
					//add comment maker.
					var add_box = document.createElement('div');
					add_box.className = 'comment_add_box';
   
   
					var addText = document.createElement('input');
					addText.setAttribute('type','text');
					addText.setAttribute('name','msg');
   
   
					var link = document.createElement('a');
   
					link.href = 'javascript:addComment(' + id + ')';
					var linkText = document.createTextNode('Share');
					link.appendChild(linkText);
   
					add_box.appendChild(addText);
					add_box.appendChild(link);
   
					comments.appendChild(add_box);

				}
				);
			}
}

function addComment(id){
	var comments = document.getElementById('box_' + id);
	var inputs = comments.getElementsByTagName('input');
	
	if(inputs[0] != '')
		{
			var url = 'includes/ajax/savecomment.php';
			var query_string = 'id=' + id + '&msg=' + inputs[0].value + '&type=programme';
			ajaxCall(url,query_string,
			function(x){
					bits = json_decode(x);

						var commentBox = document.createElement('div');
						commentBox.className = 'comment_box';
   
						var user = document.createElement('span');
						user.className = 'user';
						var userText = document.createTextNode(bits.user);
						user.appendChild(userText);
   
						var date = document.createElement('span');
						date.className = 'date';
						var dateText = document.createTextNode(bits.date);
						date.appendChild(dateText);
   
						var blurb = document.createElement('span');
						blurb.className = 'blurb';
						blurbText = document.createTextNode(bits.blurb);
						blurb.appendChild(blurbText);
   
						commentBox.appendChild(user);
						commentBox.appendChild(blurb);
						commentBox.appendChild(date);

					
					var allDivs = comments.getElementsByTagName('div');
					comments.insertBefore(commentBox,allDivs[allDivs.length -  1]);
					
					//reset input box
					inputs[0].value = '';
			});
		}
}
