
	var searchFields = new Array( );
	
	var numFields = 2;
	
	var limitStart = 0;
	
	var xmlHttpRequest;
	
	var content;

	var reloadID;
	
	var	locID;
	var e;
	var end;

	
	function replaceIt(sString, sReplaceThis, sWithThis) { 
if (sReplaceThis != "" && sReplaceThis != sWithThis) { 
var counter = 0; 
var start = 0; 
var before = ""; 
var after = ""; 
while (counter<sString.length) { 
start = sString.indexOf(sReplaceThis, counter); 
if (start == -1) { 
break; 
} else { 
before = sString.substr(0, start); 
after = sString.substr(start + sReplaceThis.length, sString.length); 
sString = before + sWithThis + after; 
counter = before.length + sWithThis.length; 
} 
} 
} 
return sString; 
} 

function setLocations(value)
{

			
		if(value == "")
		{
			locs = document.getElementById('locations');
			while (locs.hasChildNodes())
    			  locs.removeChild(locs.lastChild);
			childnode = document.createElement('option');
			
			childnode.value = "";
			
			childnodename = document.createTextNode("2. Select a Town");
			
			childnode.appendChild(childnodename);
			
			locs.appendChild(childnode);
		}
		else
		{
			
		
			locs = $('locations');
			while (locs.hasChildNodes())
    			locs.removeChild(locs.lastChild);
				  
			
			childnode = document.createElement('option');
			
			childnode.value = "";
			
			childnodename = document.createTextNode("2. Select a Town");
			
			childnode.appendChild(childnodename);
			
			locs.appendChild(childnode);
		
			var url = "http://www.cambrianambulance.co.uk/locationxml.php";
			var xmlData = url+"?classid="+value;
			loadXmlData(xmlData, 'Location', 'loading');
			
		}
	
	
}



function resetLocations(value, loc, lbegin, lend)
{

	
			
		if(value == "")
		{
			locs = document.getElementById('locations');
			 while (locs.hasChildNodes())
    			  locs.removeChild(locs.lastChild);
				  
				  childnode = document.createElement('option');
			
			childnode.value = "";
			
			childnodename = document.createTextNode("2. Select a Town");
			
			childnode.appendChild(childnodename);
			
			locs.appendChild(childnode);
			
		
			
		}
		else
		{
			
		
			locs = document.getElementById('locations');
			 while (locs.hasChildNodes())
    			  locs.removeChild(locs.lastChild);
			childnode = document.createElement('option');
			
			childnode.value = "";
			
			childnodename = document.createTextNode("2. Select a Town");
			
			childnode.appendChild(childnodename);
			
			locs.appendChild(childnode);
		
			locID = loc;
			begin = lbegin;
			end = lend
		//	alert(value);
			var url = "http://www.cambrianambulance.co.uk/locationxml.php";
			var xmlData = url+"?classid="+value;
			loadXmlData(xmlData, 'reLocation', 'loading');
			
		}
	
	
}

/**
   * Create and load the data using the XMLHttpRequest
   */




	
	function LocationResults()
	{
		var resultsDiv = document.getElementById('coursecontent');
		resultsDiv.innerHTML = "";
		
		var resultArray = Array();
		//	 alert("Status is:in  showresults");
		
		var resultsDiv = document.getElementById('content');
		resultsDiv.innerHTML = "";
		
		var items = xmlHttpRequest.responseXML.getElementsByTagName("LOCATION");
		
		locs = document.getElementById('locations');

		isOpen = false;

		for (var i=0; i<items.length; i++)
		{
			var node = items[i];
			var TOWN = node.getElementsByTagName("TOWN")[0].firstChild.nodeValue;
			var ID = node.getElementsByTagName("ID")[0].firstChild.nodeValue;
			var TYPE = node.getElementsByTagName("TYPE")[0].firstChild.nodeValue;
			
			
			TOWN = filterxml(TOWN);
			
			if(TYPE=="open")
			{
				
				isOpen = true;
			childnode = document.createElement('option');
			
			childnode.value = ID
			
			childnodename = document.createTextNode(TOWN);
			
			childnode.appendChild(childnodename);
			
			locs.appendChild(childnode);
			
			}
		}
		
		if(items.length <1 || !isOpen)
		{
			var resultsDiv = document.getElementById('coursecontent');
			resultsDiv.innerHTML = "<b>At the moment there are no planned courses for this class</b>"; 
		}
		
		return;
		
		
			
	}
	
	
	function reLocationResults()
	{
		var resultArray = Array();
		//	 alert("Status is:in  showresults");
		
		var resultsDiv = document.getElementById('coursecontent');
		resultsDiv.innerHTML = "";
		
		var resultsDiv = document.getElementById('content');
		resultsDiv.innerHTML = "";
		
		var items = xmlHttpRequest.responseXML.getElementsByTagName("LOCATION");
		locs = document.getElementById('locations');

		
		
		var counter;
		
		isOpen = false;
		
		for (var i=0; i<items.length; i++)
		{
			var node = items[i];
			var TOWN = node.getElementsByTagName("TOWN")[0].firstChild.nodeValue;
			var ID = node.getElementsByTagName("ID")[0].firstChild.nodeValue;
			try{
			var TYPE = node.getElementsByTagName("TYPE")[0].firstChild.nodeValue;
			}
			catch(err)
			{
				var message = err;
			}

				TOWN = filterxml(TOWN);
			if(TYPE=="open")
			{
				
				isOpen = true;
			childnode = document.createElement('option');
			
			childnode.value = ID
			
			if(reloadID == ID)
			{
				childnode.selected = true;	
			}
			
			childnodename = document.createTextNode(TOWN);
			
			childnode.appendChild(childnodename);
			
			locs.appendChild(childnode);
			}
			
			
			
			
		}
				
	if(items.length <1 || !isOpen)
		{
			var resultsDiv = document.getElementById('coursecontent');
			resultsDiv.innerHTML = "<b>At the moment there are no planned courses for this class</b>"; 
		}
				
		setCalendar(locs.value, begin, end);
			
	}
	


function setCalendar(locid, month, year)
{
/*
for(i = begin; i < end+86400 ; i=i+86400)
{

		block = document.getElementById(i);
	//	block.style.backgroundImage="url(img/calendarbggrey.gif)";
		//block.style.color = "#000000";
	

}
	
*/	
	if(year == "")
		{
			year = getVar('year');	
			
		}

		if(month == "")
		{
			month = getVar('month');	
			
		}

		if(locid == "")
		{
			
		}
		else
		{
			
			var classid = document.getElementById('class').value;
			var url = "http://www.cambrianambulance.co.uk/calendarxml.php";
			
			var xmlData = url+"?classid="+classid+"&locid="+locid+"&month="+month+"&year="+year;
			loadXmlData(xmlData, 'Calendar','loading');
			
		}
	
	
}

/**
   * Create and load the data using the XMLHttpRequest
   */
function loadcourse(courseid)
{
	
		var url = "http://www.cambrianambulance.co.uk/coursexml.php";
			var xmlData = url+"?courseid="+courseid;
			loadXmlData(xmlData, 'Course','loading');
	
}


function CourseResults()
	{
		
		var resultsDiv = document.getElementById('content');
		resultsDiv.innerHTML = "";
		
		var resultsDiv = document.getElementById('coursecontent');
		resultsDiv.innerHTML = "";
	
		var items = xmlHttpRequest.responseXML.getElementsByTagName("COURSE");
		for (var i=0; i<items.length; i++)
		{
			var node = items[i];
			var DATE = node.getElementsByTagName("DATE")[0].firstChild.nodeValue;
			var NAME = node.getElementsByTagName("NAME")[0].firstChild.nodeValue;
			var PLACES = node.getElementsByTagName("PLACES")[0].firstChild.nodeValue;
			var PRICE = node.getElementsByTagName("PRICE")[0].firstChild.nodeValue;
			var LOCNAME = node.getElementsByTagName("LOCNAME")[0].firstChild.nodeValue;
			var DURATION = node.getElementsByTagName("DURATION")[0].firstChild.nodeValue;
			var DESC = node.getElementsByTagName("DESC")[0].firstChild.nodeValue;
			var ID = node.getElementsByTagName("ID")[0].firstChild.nodeValue;
			var TYPE = node.getElementsByTagName("TYPE")[0].firstChild.nodeValue;
			var STATUS = node.getElementsByTagName("STATUS")[0].firstChild.nodeValue;
			
			NAME = filterxml(NAME);
			DURATION = filterxml(DURATION);
			LOCNAME = filterxml(LOCNAME);
			DESC = filterxml(DESC);
		}
		if(TYPE == "open")
		{
		resultsDiv.innerHTML += "<b>Course:</b> "+NAME+ "<br/><br/>";
		resultsDiv.innerHTML +="<b>Date:</b> "+ DATE+ "<br/><br/>";
		resultsDiv.innerHTML += "<b>Location:</b> "+LOCNAME+ "<br/><br/>";
		resultsDiv.innerHTML += "<b>Places Left:</b> "+PLACES+ "<br/><br/>";
		resultsDiv.innerHTML += "<b>Price P/P:</b> &pound;"+PRICE+ "<br/><br/>";
		resultsDiv.innerHTML += "<b>Duration:</b> "+DURATION+ "<br/><br/>";
		resultsDiv.innerHTML += "<b>Description:</b> "+DESC+ "<br/><br/>";
	
		if(STATUS =="open")
		{
			resultsDiv.innerHTML += "<b><a href='/courseorder/?courseid="+ID+"'>order now</a><br/><br/>";
		}
		else
		{
			resultsDiv.innerHTML += "<b>Sorry, This course is fully booked</b><br/><br/>";
		}
		
		}
	}
	
	function CalendarResults()
	{
		
		
		
		var  coursecontent = document.getElementById('coursecontent');
		
		coursecontent.innerHTML = ""; 
		var  calendar = document.getElementById('calendar');
		
		datedivs = calendar.getElementsByTagName('div');
		
		for (var i=0; i<datedivs.length; i++)
		{
		
		block = datedivs[i];
		block.style.backgroundImage="url(../img/calendarbggrey.gif)";
		block.style.color = "#000000";
		
		
		links = block.getElementsByTagName('a')
			
			if(links.length  == 1)
			{
				for(i = 0; i < links.length; i++)
				{
						date = links[0].childNodes[0].nodeValue ;
				}
			}
			else if(links.length  == 0)
			{
				date = block.innerHTML;
			}
		
		block.innerHTML = date;
		
		}
		
		var resultsDiv = document.getElementById('content');
		resultsDiv.innerHTML = "";
	
		var items = xmlHttpRequest.responseXML.getElementsByTagName("COURSE");
		
	
		var counter;
		
		for (var i=0; i<items.length; i++)
		{
			var node = items[i];
			var DATE = node.getElementsByTagName("DATE")[0].firstChild.nodeValue;
			var ID = node.getElementsByTagName("ID")[0].firstChild.nodeValue;
			var TYPE = node.getElementsByTagName("TYPE")[0].firstChild.nodeValue;
			var STATUS = node.getElementsByTagName("STATUS")[0].firstChild.nodeValue;
			
			
			if(TYPE == "open")
			{
				
			if(STATUS == "full")
			{
			 var color = "red";	
			}
			else
			{
				var color = "green";
			}
			
		
			block = document.getElementById(DATE);
			block.style.backgroundImage="url(../img/calendarbg"+color+".gif)";
			
			links = block.getElementsByTagName('a')
			
			if(links.length  == 1)
			{
				for(i = 0; i < links.length; i++)
				{
						date = links[0].childNodes[0].nodeValue ;
				}
			}
			else if(links.length  == 0)
			{
				date = block.innerHTML;
			}
						
			block.innerHTML = "";
			childlink = document.createElement('a');
			linktext = document.createTextNode(date);
			childlink.appendChild(linktext);
			block.appendChild(childlink);
			links = block.getElementsByTagName('a')
			childlink.id = ID;
		
			childlink.onclick = function(){
			
			loadcourse(this.id);
				
			}
				
			}
			//addEvent(childlink, 'click', childclick, false);	
		
			
		}
	}

	

   function trim(str)
   {
     s = str.replace(/^(\s)*/, '');
     s = s.replace(/(\s)*$/, '');
     return s;
   }
 
 
	function nextmonth(nextlink)
	{
		classid = document.getElementById('class').value
		loc= document.getElementById('locations').value
		
		pagelink = nextlink+"&classid="+classid+"&locid="+loc
		
		document.location.href=pagelink
	}
	
	function prevmonth(prevlink)
	{
		classid = document.getElementById('class').value
		loc= document.getElementById('locations').value
		
		pagelink = prevlink+"&classid="+classid+"&locid="+loc
		document.location.href=pagelink
	}
	
	
	
	
function initpage()
{



classes = getVar('classid');
loc = getVar('locid');
begin = getVar('month');
end = getVar('end');
	reloadID = loc;
		//alert(classes);
	if(classes != "")
	{
	var resultsDiv = document.getElementById('content');
		resetLocations(classes, loc, begin, end);
		
	
		
	//	setCalendar(loc, begin, end);
		
	}
	

	
}	

function loadcalendar(page)
{

classid = document.getElementById('class').value
loc= document.getElementById('locations').value
	
	
	
		pagelink = page+"?classid="+classid+"&locid="+loc
		document.location.href=pagelink
	
}

function getVar(name)
         {
         get_string = document.location.search;         
         return_value = '';
         
         do { //This loop is made to catch all instances of any get variable.
            name_index = get_string.indexOf(name + '=');
            
            if(name_index != -1)
              {
              get_string = get_string.substr(name_index + name.length + 1, get_string.length - name_index);
              
              end_of_value = get_string.indexOf('&');
              if(end_of_value != -1)                
                value = get_string.substr(0, end_of_value);                
              else                
                value = get_string;                
                
              if(return_value == '' || value == '')
                 return_value += value;
              else
                 return_value += ', ' + value;
              }
            } while(name_index != -1)
            
         //Restores all the blank spaces.
         space = return_value.indexOf('+');
         while(space != -1)
              { 
              return_value = return_value.substr(0, space) + ' ' + 
              return_value.substr(space + 1, return_value.length);
							 
              space = return_value.indexOf('+');
              }
          
		  if(return_value == document.location.search)
		  {
			return_value = "";  
		  }
         return(return_value);        
         }

function filterxml(result)
{
	result = replaceIt(result, "[space]","&nbsp;");
	result = replaceIt(result, "[pound]","&pound;");
	result = replaceIt(result, "[amp]","&amp;");
	result = replaceIt(result, "[block]","");
	result = replaceIt(result, "[oneqoute]","&rsquo;");
	result = replaceIt(result, "[and]","&");
	result = replaceIt(result, "[quot]","&quot;");
	result = replaceIt(result, "  ","");	
	
	return result
}
		 
addEvent(window, 'load', initpage, false);