var xmlHttp;
/* this method will get the study centre locations for a given exam body */
function getLocations()
{
    xmlHttp = GetXmlHttpObject();
    /* specify the url of the script we wish to submit the form to in order to get the location */
    var url = "testQuery.asp";
	var qualId = document.getElementById("selectQualification").value;
	if (qualId != "Please select a qualification")
	{
		/* get the value we want to pass to the asp script */
		url = url + "?loc=" + qualId;
		/* appending a random number to the url prevents the page from being cached */
		url = url + "&sid=" + Math.random();
		xmlHttp.onreadystatechange=stateChanged;
		xmlHttp.open("GET", url, true);
		xmlHttp.send(null);
		/*	this line works, and returns the exambodyref value
		document.write(document.getElementById("selectQualification").value);
		document.getElementById("locations").innerHTML = url;*/
	}
	else document.getElementById("locations").innerHTML = "<select disabled='disabled'><option>Please select a qualification</option></select>";
}

function stateChanged()
{
    if (xmlHttp.readyState == 4)
    {
        document.getElementById("locations").innerHTML = xmlHttp.responseText;
    }
}

function GetXmlHttpObject()
{
    var xmlHttp = null;
    try
    {
        // Firefox, Opera, 8.0+, Safari
        xmlHttp = new XMLHttpRequest();
    }
    catch (e)
    {
        // Internet Explorer
        try
        {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e)
        {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    return xmlHttp;
}

function redirectItem()
{
	qual = document.getElementById("selectQualification").options[document.getElementById("selectQualification").selectedIndex].text;
	centre = document.getElementById("selectStudyCentre").value;
	if (centre != "Please select a study centre")
	{
		// find and replace space characters with hyphen characters (for centre variable)
		var length = centre.length;
		var counter = 0;
		var pos = centre.indexOf(" ");
		/* if there are no occurences of spaces, break from the loop (otherwise will be stuck in an eternal loop) */
		if (pos != -1)
		{
			while (pos < length)
			{
				// replace it with a hyphen
				centre = centre.replace(" ", "-");
				// search for any further occurences of spaces
				pos = centre.indexOf(" ");
				// if there are no more occurrences of spaces, break from the loop
				if (pos == -1) break;
			}
		}
		// conditionals for acca filenames
		switch (qual)
		{
			case "ACCA Fulltime":
				qual = "acca-ft";
				window.location = "http://www.bpp.com/studycentres/uk/" + centre + "/courses/" + qual + ".asp";
			break;
			
			case "AAT":
				if (centre == "London-Holborn")
				{
					window.location = "http://www.bpp.com/studycentres/uk/london-holborn";
				}
				else if (centre == "London-Kings-Cross")
				{
					window.location = "http://www.bpp.com/studycentres/uk/london-kings-cross";
				}
				else if (centre == "London-Liverpool-Street")
				{
					window.location = "http://www.bpp.com/studycentres/uk/london-liverpool-street";
				}
				else if (centre == "London-Shepherds-Bush")
				{
					window.location = "http://www.bpp.com/studycentres/uk/london-shepherds-bush";
				}
				else if (centre == "London-Waterloo")
				{
					window.location = "http://www.bpp.com/studycentres/uk/london-waterloo";	
				}
				else
				{
					window.location = "http://www.bpp.com/studycentres/uk/" + centre + "/courses/" + qual + ".asp";	
				}
			break;
			
			case "ACCA":
				qual = "acca-pt";
				if (centre == "Jersey")
				{
					window.location = "http://www.bpp.com/studycentres/uk/jersey";	
				}
				else if (centre == "London-Holborn")
				{
					window.location = "http://www.bpp.com/studycentres/uk/london-holborn";
				}
				else if (centre == "London-Kings-Cross")
				{
					window.location = "http://www.bpp.com/studycentres/uk/london-kings-cross";
				}
				else if (centre == "London-Liverpool-Street")
				{
					window.location = "http://www.bpp.com/studycentres/uk/london-liverpool-street";
				}
				else if (centre == "London-Shepherds-Bush")
				{
					window.location = "http://www.bpp.com/studycentres/uk/london-shepherds-bush";
				}
				else if (centre == "London-Waterloo")
				{
					window.location = "http://www.bpp.com/studycentres/uk/london-waterloo";	
				}
				else
				{
					window.location = "http://www.bpp.com/studycentres/uk/" + centre + "/courses/" + qual + ".asp";
				}
			break;
			
			case "CIMA":
				if (centre == "London-Holborn")
				{
					window.location = "http://www.bpp.com/studycentres/uk/london-holborn";
				}
				else if (centre == "London-Kings-Cross")
				{
					window.location = "http://www.bpp.com/studycentres/uk/london-kings-cross";
				}
				else if (centre == "London-Liverpool-Street")
				{
					window.location = "http://www.bpp.com/studycentres/uk/london-liverpool-street";
				}
				else if (centre == "London-Shepherds-Bush")
				{
					window.location = "http://www.bpp.com/studycentres/uk/london-shepherds-bush";
				}
				else if (centre == "London-Waterloo")
				{
					window.location = "http://www.bpp.com/studycentres/uk/london-waterloo";	
				}
				else
				{
					window.location = "http://www.bpp.com/studycentres/uk/" + centre + "/courses/" + qual + ".asp";	
				}
			break;
			
			case "CAT":
				if (centre == "Guernsey")
				{
					window.location = "http://www.bpp.com/studycentres/uk/guernsey";	
				}
			break;
			
			case "ACA":
				if (centre != "London---Royal-Mint-Court")
				{
				window.location = "http://www.bpp.com/studycentres/uk/" + centre + "/courses/other-courses.asp";
				}
				else window.location = "http://www.bpp.com/external-venues/london-royal-mint-court/default.html";
			break;
			
			case "ICAS":
				window.location = "http://www.bpp.com/studycentres/uk/" + centre + "/courses/other-courses.asp";
			break;
			
			case "ATT":
				window.location = "http://www.bpp.com/studycentres/uk/" + centre + "/courses/other-courses.asp";
			break;
			
			case "CTA":
				window.location = "http://www.bpp.com/studycentres/uk/" + centre + "/courses/other-courses.asp";
			break;
			
			/*default:
				window.location = "http://www.bpp.com/studycentres/uk/" + centre + "/courses/" + qual + ".asp";*/
		}
		/*if (qual != "CAT" && qual != "ICAEW")
		{
			window.location = "http://www.bpp.com/studycentres/uk/" + centre + "/courses/" + qual + ".asp";
			//document.write("http://www.bpp.com/studycentres/uk/" + centre + "/courses/" + qual + ".asp");
		}*/
	}
}