// JavaScript Document

/**
* Show and hide submenu items
*/
function showSubMonth(id)
{
	if(document.getElementById('subMenus'))
	{
		/*hide all the open menus*/
		var allMenus = document.getElementById('subMenus');
		//the id of the menu to toggle
		var subMenu = 'month'+id;
		el=document.getElementById(subMenu);
		
		for(i=0;i<allMenus.childNodes.length;i++)
		{
			if(allMenus.childNodes[i].id == subMenu)
			{
				if(el.style.display!="block")
				{
					el.style.display = "block";
					document.getElementById('instruction2').style.display = "block";
					document.getElementById('instruction1').style.display = "none";
				}
				else
				{
					el.style.display = "none";
					document.getElementById('instruction2').style.display = "none";
					document.getElementById('instruction1').style.display = "block";
				}
			}
			else
				allMenus.childNodes[i].style.display	 = "none";	
		}
	}
}

/**
* Changes the thumbnail image
*/
function loadThumb(id,obj,subMenu)
{
	if(id>0)
	{
		if(document.getElementById('thumbImg'))
		{
			document.getElementById('thumbImg').src = "http://www.rothamsted.bbsrc.ac.uk/broom/grguide/infobase/OpenDoc.php?t=1&id="+id;
		}
		else
		{
			//create a new image
			var x = document.createElement('IMG');
			x.src = "http://www.rothamsted.bbsrc.ac.uk/broom/grguide/infobase/OpenDoc.php?t=1&id="+id;
			x.id = "thumbImg"
			//insert into dom
			document.getElementById('subMenus').insertBefore(x,document.getElementById('subMenus').firstChild);
		}
		document.getElementById('thumbImg').style.display = "block";

		//change the background and text color to default for all options
		var subMenu = 'month'+subMenu+'Menu';
		
		el=document.getElementById(subMenu);
		
		
		
		for(i=0;i<el.childNodes.length;i++)
		{
			el.childNodes[i].firstChild.style.backgroundImage = "url(images/menu.jpg)";
			el.childNodes[i].firstChild.style.color = "#666666";
		}
		
		
		//change the background and the text color of the menu item that triggered this change
		obj.style.backgroundImage = "url(images/menu1.jpg)";
		obj.style.color = "#EEEEEE";
	}
	else
	{
		document.getElementById('thumbImg').parentNode.removeChild('thumbImg');
	}
}

	
var isWorking = false;
var url = "xml.php?id="; // The server-side script
	
/**
* Loads a new report
*/
function loadReport(id)
{
  	if (!isWorking && http) 
	{
    	http.open("GET", url + escape(id)+"&RandomKey=" + Math.random() * Date.parse(new Date()), true);
	    http.onreadystatechange = handleHttpResponse;
    	isWorking = true;
	    http.send(null);
  	}
}






function handleHttpResponse() {
  if (http.readyState == 4) {
    if (http.responseText.indexOf('invalid') == -1) {
// Use the XML DOM to unpack the city and state data 
		var xmlDocument = http.responseXML; 
		
		var title = xmlDocument.getElementsByTagName('title').item(0).firstChild.data;
		var date = xmlDocument.getElementsByTagName('date').item(0).firstChild.data;
		
		//Image
		var imgSrc = "http://www.rothamsted.bbsrc.ac.uk/broom/grguide/infobase/OpenDoc.php?id=";
		//if there is a file tag
		if(xmlDocument.getElementsByTagName('file').item(0))
		{
			imgSrc = imgSrc+xmlDocument.getElementsByTagName('file').item(0).firstChild.data
		}
		else //no image, so use default
		{
			imgSrc = imgSrc + "2358";
		}
		
		document.getElementById('mainImage').src = imgSrc; 

		
		//var file = xmlDocument.getElementsByTagName('file').item(0).firstChild.data;
		
		while(document.getElementById('mainText').hasChildNodes())
		{
			document.getElementById('mainText').removeChild(document.getElementById('mainText').firstChild);	
		}
		
		var content =  xmlDocument.getElementsByTagName('content').item(0).firstChild.data;

		document.getElementById('mainText').innerHTML = content;

		document.getElementById('mainTitle').innerHTML = title; 
		document.getElementById('mainDate').innerHTML = date; 
	   
		showSubMonth(999);
		//document.getElementById('mainText').appendChild(contentNode); 
		isWorking = false;
    }
  }
}
function handleHttpResponse1() {
  if (http.readyState == 4) {
    if (http.responseText.indexOf('invalid') == -1) {
// Use the XML DOM to unpack the city and state data 
		var xmlDocument = http.responseXML; 
		
		var items = xmlDocument.getElementsByTagName('task');

		var box = document.getElementById("taskTitleId");
		box.options.length = 0;
		for(var i = 0;i<items.length;i++)
		{
			var taskId = xmlDocument.getElementsByTagName('taskid').item(i).firstChild.data;
			var title = xmlDocument.getElementsByTagName('title').item(i).firstChild.data;
			var boxlength = box.options.length;
			box.options[boxlength] = new Option(title,taskId);
		}
		isWorking = false;
    }
  }
}

function appendToSelect(select, value, content) {
    var opt;
    opt = document.createElement("option");
    opt.value = value;
    opt.appendChild(content);
    select.appendChild(opt);
}



function getHTTPObject() {
  var xmlhttp;
  /*@cc_on
  @if (@_jscript_version >= 5)
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
        xmlhttp = false;
      }
    }
  @else
  xmlhttp = false;
  @end @*/
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
	  xmlhttp.overrideMimeType("text/xml"); 
    } catch (e) {
      xmlhttp = false;
    }
  }
  return xmlhttp;
}
var http = getHTTPObject(); // We create the HTTP Object



var hex=255; // Initial color value.
var STOP = 0;
var bghex=255; // Initial color value.
var bgSTOP = 1;
var globalTaskID = 0;

function fadetextinout(id)
{
	if(hex<255)
	{
		if(bghex<254)
		{
			if(bgSTOP==0)
				stopBgFade(id);
			else
				fadeoutbackground(id);
		}
		else
		{
			if(STOP==0)
				stopfade(id);
			else
			{
				fadetextout(id);
			}
		}
	}
	else
	{
		loadTask(id);
		fadetextin();
	}	
}

function stopfade(id)
{
	STOP = 1;
	fadetextinout(id);
	
}

function stopBgFade(id)
{
	STOP = 1;
	bgSTOP = 1;
	fadetextinout(id);
}

function fadetextin()
{
    if(hex>0 & STOP == 0)
	{
   		hex-=11; // increase color darkness
		document.getElementById(elToFade).style.color="rgb("+hex+","+hex+","+hex+")";
		setTimeout("fadetextin()",50);
	}
	else if(STOP == 0)
	{
		bgSTOP = 0;
		fadeinbackground();
	}
	else
	{
		STOP = 1;
		
	}
}

function fadeinbackground()
{
	if(bghex>0 & bgSTOP == 0)
	{
   		bghex-=11; // increase color darkness
		document.getElementById(elToFade).style.backgroundColor="rgb(255,255,"+bghex+")";
		setTimeout("fadeinbackground()",50);
	}
	else if(bgSTOP == 0)
	{
		bgSTOP = 1;
		fadeoutbackground();
	}
}

function fadeoutbackground(id)
{
    if(bghex<255 & bgSTOP == 1)
	{
   		bghex+=11; // increase color darkness
		document.getElementById(elToFade).style.backgroundColor="rgb(255,255,"+bghex+")";
		setTimeout("fadeoutbackground("+id+")",50);
	}
	else if(STOP ==1)
		fadetextout(id);
	else if(bgSTOP == 1)
	{
		bgSTOP = 0;
	}
}

function fadetextout(id)
{

	if(hex<255 & STOP == 1)
	{
   		hex+=11; // increase color darkness
		document.getElementById(elToFade).style.color="rgb("+hex+","+hex+","+hex+")";
		setTimeout("fadetextout("+id+")",50);
	}
	else if(STOP == 1)
	{
		STOP = 0;
		//alert("id: "+id+"\nhex :"+hex+"\nstop:"+STOP+"\nbghex"+bghex+"\nbgSTOP"+bgSTOP);
		fadetextinout(id);
	}
}