function displayMessage(givenMessage)
{
	document.getElementById('fullpage').innerHTML = givenMessage;

	document.getElementById('backgroundpage').style.visibility='visible';
	document.getElementById('backgroundpage').style.display='block';

	document.getElementById('fullpagewrapper').style.visibility='visible';
	document.getElementById('fullpagewrapper').style.display='block';

	document.getElementById('fullpage').style.visibility='visible';
	document.getElementById('fullpage').style.display='block';
	
}

function displayHelp(givenController, givenFunction)
{


	document.getElementById('backgroundpage').style.visibility='visible';
    document.getElementById('backgroundpage').style.display='block';
    document.getElementById('helpwrapper').style.visibility='visible';
    document.getElementById('helpwrapper').style.display='block';
    document.getElementById('helppage').style.visibility='visible';
    document.getElementById('helppage').style.display='block';
	document.getElementById('helpwrapper').style.width='100%';
	document.getElementById('helppage').style.width='800px';
	document.getElementById('helppage').style.margin='100px auto';
	document.getElementById('minimisebutton').style.visibility='visible';
	getHelp(givenController, givenFunction);
}

function minimiseHelp()
{
	document.getElementById('backgroundpage').style.visibility='hidden';
    document.getElementById('backgroundpage').style.display='';
    document.getElementById('helpwrapper').style.visibility='visible';
    document.getElementById('helpwrapper').style.display='block';
    document.getElementById('helppage').style.visibility='visible';
    document.getElementById('helppage').style.display='block';
	document.getElementById('helpwrapper').style.width='180px';
	document.getElementById('helpwrapper').style.height='230px';
	document.getElementById('helppage').style.width='180px';
	document.getElementById('helppage').style.margin='50px auto';
	document.getElementById('minimisebutton').style.visibility='hidden';
	document.cookie = 'help=1; path=/';
}

function closeHelp()
{
	document.getElementById('backgroundpage').style.visibility='hidden';
    document.getElementById('backgroundpage').style.display='';
    document.getElementById('helpwrapper').style.visibility='hidden';
    document.getElementById('helpwrapper').style.display='';
    document.getElementById('helppage').style.visibility='hidden';
    document.getElementById('helppage').style.display='';
	document.cookie = 'help=0; path=/';
}

function helpCookie(givenController, givenFunction)
{
	var Cookies = new Array();
	var allCookies = document.cookie.split('; ');
	for (var i=0;i<allCookies.length;i++) 
	{
		var cookiePair = allCookies[i].split('=');
		Cookies[cookiePair[0]] = cookiePair[1];
	}

	if (Cookies['help'] == '1')
	{
		minimiseHelp();
		getHelp(givenController, givenFunction);
	}
}

function getHelp(givenController, givenFunction)
{
	if (givenFunction=='')
		givenFunction='index';
	var urlToLoad='/index.php/help/ajax/'+givenController+'/'+givenFunction;
	loadPage(urlToLoad, 'helpcontents', 'GET', null)
	//document.getElementById('helpcontents').innerHTML='givenController='+givenController+' givenFunction='+givenFunction;
}


// AJAX

var globalTarget;
var globalHttpRequest;
var globelLoadURL;
var globalActiveQuery=false;
var globalAticeLock=false;
var globalCallBack=false;

function ToggleDiv(givenDiv, givenOn)
{
        //alert(givenDiv);
        if (givenOn==true)
        {
                document.getElementById(givenDiv).style.visibility='visible';
                document.getElementById(givenDiv).style.display='block';
        }
        else
        {
                document.getElementById(givenDiv).style.visibility='hidden';
                document.getElementById(givenDiv).style.display='none';
        }
}



function PostForm(givenForm, urlToLoad, targetDiv, givenAction, callBack)
{
	if (givenAction == true)
		alterDiv(targetDiv, 'saving...');


	//alert('PostForm('+givenForm+','+urlToLoad+','+targetDiv+')');
	//alert('Lenght of form elements '+givenForm.elements.length);
	var sumbmitScript='';
	for (var i = 0; i < givenForm.elements.length; i++)
	{
		var e = givenForm.elements[i];
		if ((e.type == 'checkbox') && (e.checked != true))
			continue;
		sumbmitScript+='&'+e.name+ '='+escape(e.value)
	}
	//alert(sumbmitScript);
	loadPage (urlToLoad, targetDiv, 'POST', sumbmitScript);

	if (callBack != null)
		globalCallBack=callBack;

	return false;
}


function alterDiv(targetDiv, newContent)
{
	document.getElementById(targetDiv).innerHTML=newContent;
}


function loadPage(urlToLoad, targetDiv, givenPostType, givenPostVars)
{

	if (globalActiveQuery == true)
	{
		//alert('holding off for'+targetDiv);
		setTimeout('loadPage(\''+urlToLoad+'\',\''+targetDiv+'\',\''+givenPostType+'\',\''+givenPostVars+'\')',50);
		return;	
	}

	//alterDiv(targetDiv, 'loading...');
	//document.getElementById(targetDiv).style.visibility='hidden';


	// We need to serialise this function with the globalActiveQuery ...
	globalActiveQuery=true;
	globalTarget=targetDiv;
	globelLoadURL=urlToLoad;


	if (window.XMLHttpRequest) { // IE7, Mozilla, Safari, ...
		globalHttpRequest = new XMLHttpRequest();
		//globalHttpRequest.overrideMimeType('text/xml');
	} else if (window.ActiveXObject) { // IE
		globalHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		alert('Fail to create XMLHttpRequest() object.\nPlease upgrade your web browser.');
		return;
	}
	
	// Stop IE caching
	var currentTime = new Date();
	urlToLoad=urlToLoad+'/'+currentTime.getTime();
	
	globalHttpRequest.onreadystatechange = loadPageContents;

	if (givenPostType=='GET')
	{
		//alert('loadPage(\''+urlToLoad+'\',\''+targetDiv+'\',\''+givenPostType+'\',\''+givenPostVars+'\')');
		globalHttpRequest.open('GET', urlToLoad, true);
		globalHttpRequest.send(null);
	}
	else
	{
		//alert('loadPage(\''+urlToLoad+'\',\''+targetDiv+'\',\''+givenPostType+'\',\''+givenPostVars+'\')');
		globalHttpRequest.open('POST', urlToLoad, true);
		globalHttpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
		globalHttpRequest.send(givenPostVars);
	}

}

function loadPageContents()
{
	if (globalHttpRequest.readyState == 4)
	{
		if (globalHttpRequest.status == 200)
		{
			if (globalCallBack!=false)
				eval(globalCallBack+'(\''+globalHttpRequest.responseText+'\')');
			else
				document.getElementById(globalTarget).innerHTML=globalHttpRequest.responseText;
		}
		else
		{
			alert('Failed to load url '+globelLoadURL+'\nStatus ' +globalHttpRequest.status+'\nDebug '+globalHttpRequest.statusText+'\nPlease inform the webmaster.\nMany thanks.');
		}
		            
		globalActiveQuery=false;

	}

}


function editCallBack(givenReturnVal)
{

    if (document.form.type.value=='add')
    {
        if (givenReturnVal.substring(0,1) == 't')
        {
            // Find the second semi
            var MatchPos = givenReturnVal.substring(2).indexOf(";"); 
            alterDiv('savingpage', givenReturnVal.substring(3+MatchPos));
            document.form.saved.value=1;
            document.form.id.value=givenReturnVal.substring(2, MatchPos+2);
            document.form.type.value='update';
			document.getElementById('savingpage').className='ajaxok';
        }
        else
        {
            alterDiv('savingpage', givenReturnVal.substring(2));
			document.getElementById('savingpage').className='ajaxfail';
        }
    }
    else
    {
		if (givenReturnVal.substring(0,1) == 't')
		{
			document.getElementById('savingpage').className='ajaxok';
			alterDiv('savingpage', givenReturnVal.substring(2));
		}
		else
		{
			alterDiv('savingpage', givenReturnVal.substring(2));
			document.getElementById('savingpage').className='ajaxfail';
		}
    }
}

