var xmlhttp;
var oldselection = null;
var page;

function showPage(page)
{
	xmlhttp=GetXmlHttpObject();

  	if (xmlhttp==null)
  	{
    		alert ("Your browser does not support XMLHTTP!");
    		return;
  	}

  	var url="/pages/" + page + ".php";
	window.page = page;

	document.getElementById("pageContent").innerHTML="<p class='center'><br /><br /><br /><br /><br /><img src='/images/loading_animation.gif' /></p>";

 	xmlhttp.onreadystatechange = stateChanged;
 	xmlhttp.open('GET',url,true);
  	xmlhttp.send(null);
}

function stateChanged()
{
  	if (xmlhttp.readyState==4 && xmlhttp.status==200)
  	{
		document.getElementById("pageContent").style.opacity = '0';
		document.getElementById("pageContent").style.filter = 'alpha(opacity = 0)';
  		document.getElementById("pageContent").innerHTML=xmlhttp.responseText;
		fade("pageContent");		
		setSelect(window.page);
  	}
	else if (xmlhttp.readyState == 4 && xmlhttp.status != 200)
  	{
		document.getElementById("pageContent").innerHTML = "<h2 class='center'>Error " + xmlhttp.status + "</h1><br /><p>Something went wrong. We are working on a solution. We apologise for the inconvenience.</p>";
	}
}

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 setSelect(page)
{
	if (oldselection != null) 
	{
		document.getElementById(oldselection).style.fontWeight = 'normal';
		document.getElementById(oldselection).style.textDecoration = 'none';
		//document.getElementById(oldselection).style.backgroundColor = '#ffffff';
	}
	document.getElementById(page).style.fontWeight = 'bold';
	document.getElementById(page).style.textDecoration = 'underline';
	//document.getElementById(page).style.backgroundColor = '#ebf2fc';
	oldselection = page;
}

function setArbitrary(eid)
{
	document.getElementById(eid).style.fontWeight = 'bold';
	document.getElementById(eid).style.textDecoration = 'underline';
	//document.getElementById(eid).style.backgroundColor = '#ebf2fc';
	oldselection = eid;
}

function preload()
{
	loadingImage = new Image(25, 25);
	loadingImage.src = '/images/loading_animation.gif';
}

var  TimeToFade = 300.0;

function fade(eid)
{
  var element = document.getElementById(eid);
  if(element == null)
    return;
   
  if(element.FadeState == null)
  {
    if(element.style.opacity == null
        || element.style.opacity == ''
        || element.style.opacity == '1')
    {
      element.FadeState = 2;
    }
    else
    {
      element.FadeState = -2;
    }
  }
   
  if(element.FadeState == 1 || element.FadeState == -1)
  {
    element.FadeState = element.FadeState == 1 ? 1 : 1;
    element.FadeTimeLeft = TimeToFade - element.FadeTimeLeft;
  }
  else
  {
    element.FadeState = element.FadeState == 2 ? 1 : 1;
    element.FadeTimeLeft = TimeToFade;
    setTimeout("animateFade(" + new Date().getTime() + ",'" + eid + "')", 33);
  }  
}

function  animateFade(lastTick, eid)
{  
  var curTick = new Date().getTime();
  var elapsedTicks = curTick - lastTick;
 
  var element = document.getElementById(eid);
 
  if(element.FadeTimeLeft <= elapsedTicks)
  {
    element.style.opacity = element.FadeState == 1 ? '1' : '0';
    element.style.filter = 'alpha(opacity = '
        + (element.FadeState == 1 ? '100' : '0') + ')';
    element.FadeState = element.FadeState == 1 ? 2 : -2;
    return;
  }
 
  element.FadeTimeLeft -= elapsedTicks;
  var newOpVal = element.FadeTimeLeft/TimeToFade;
  if(element.FadeState == 1)
    newOpVal = 1 - newOpVal;

  element.style.opacity = newOpVal;
  element.style.filter = 'alpha(opacity = ' + (newOpVal*100) + ')';
 
  setTimeout("animateFade(" + curTick + ",'" + eid + "')", 33);
}

function checkLength(form)
{
	if (form.comment.value.length > 500)
	{
        	alert("Text is " + form.comment.value.length + " characters long. Must be 500 characters or less.");
        	return false;
	}
	return true;
}
