/* function: create_ajax_object()
 * creates an ajax object instance
*/
function create_ajax_object() {
	
	// Create request object
	try {
	
		// Firefox etc
		ajaxObj = new XMLHttpRequest();
		
	} catch ( e ) {
	
		// Ie
		try {
		
			ajaxObj = new ActiveXObject("Msxml2.XMLHTTP");
			
		} catch ( e ) {
		
			try {
			
				ajaxObj = new ActiveXObject("Microsoft.XMLHTTP");
				
			} catch ( e ) {
			
				alert ( "Your Browser Does Not Support AJAX! You Cannot Continue");
				
			}
		
		}
		
	}
		
	return ( ajaxObj );
		
}


/* function: show_theatre()
 * shows a list of all HQ theatres
*/
function show_theatre() {

	// Close all boxes
	close_all_search();
	
	// show loading
	document.getElementById ( 'loading' ).style.display = '';

	var ajaxObj = create_ajax_object();
		
	if ( ajaxObj ) {
		
		ajaxObj.onreadystatechange = function() {
			
			if ( ajaxObj.readyState == '4' ) {
				
				var response = ajaxObj.responseText;

				// hide loading
				document.getElementById ( 'loading' ).style.display = 'none';

				// show the container
				document.getElementById ( 'show_theatre' ).style.display = '';
				
				// add html
				document.getElementById ( 'show_theatre' ).innerHTML = response;

					
			}
				
		}
			
	} else {
		
		alert ( 'Ajax object creation failed' );
			
	}
	ajaxObj.open ( 'GET', 'ajax.php?action=get_ajax_hq_theatres', true );
	ajaxObj.send( null );
	
	return ( false );
	
}
	
/* function: show_pantomimes()
 * shows a full display of all available pantomimes
*/
function show_pantomimes() {

	// Close all boxes
	close_all_search();

	// show loading
	document.getElementById ( 'loading' ).style.display = '';

	var ajaxObj = create_ajax_object();
		
	if ( ajaxObj ) {
		
		ajaxObj.onreadystatechange = function() {
			
			if ( ajaxObj.readyState == '4' ) {
				
				var response = ajaxObj.responseText;

				// hide loading
				document.getElementById ( 'loading' ).style.display = 'none';

				// show the container
				document.getElementById ( 'show_pantomimes' ).style.display = '';
				
				// add html
				document.getElementById ( 'show_pantomimes' ).innerHTML = response;

					
			}
				
		}
			
	} else {
		
		alert ( 'Ajax object creation failed' );
			
	}
	ajaxObj.open ( 'GET', 'ajax.php?action=get_ajax_pantomimes', true );
	ajaxObj.send( null );
	
	return ( false );

}

/* function: show_pantomime_theatres ( pantomime_id )
 * shows theatres relating to a selected pantomime 
*/
function show_pantomime_theatres ( pantomime_id ) {

	var ajaxObj = create_ajax_object();		

	// show loading
	document.getElementById ( 'loading' ).style.display = '';

	if ( ajaxObj ) {
		
		ajaxObj.onreadystatechange = function() {
			
			if ( ajaxObj.readyState == '4' ) {
				
				var response = ajaxObj.responseText;

				// hide loading
				document.getElementById ( 'loading' ).style.display = 'none';

				document.getElementById ( 'show_pantomime_theatres' ).innerHTML = response;
				
				// Show the theatres with animation
				animatedcollapse.toggle('show_pantomimes');
				animatedcollapse.toggle('show_pantomime_theatres');

					
			}
				
		}
			
	} else {
		
		alert ( 'Ajax object creation failed' );
			
	}
	ajaxObj.open ( 'GET', 'ajax.php?action=get_ajax_pantomime_theatres&id=' + pantomime_id, true );
	ajaxObj.send( null );
		
}

/* function: show_postcode()
 * shows postcode search area
*/
function show_postcode() {

	// Close all boxes
	close_all_search();

	document.getElementById ( 'postcode_search' ).style.display = '';
	
}

/* function: show_productions()
 * shows the current productions available from HQ
*/
function show_productions() {

	// Close all boxes
	close_all_search();

	// show loading
	document.getElementById ( 'loading' ).style.display = '';

	var ajaxObj = create_ajax_object();
		
	
	if ( ajaxObj ) {
		
		ajaxObj.onreadystatechange = function() {
			
			if ( ajaxObj.readyState == '4' ) {
				
				var response = ajaxObj.responseText;

				// hide loading
				document.getElementById ( 'loading' ).style.display = 'none';

				document.getElementById ( 'show_productions' ).innerHTML = response;
				
				// Show
				document.getElementById ( 'show_productions' ).style.display = '';
	
			}
				
		}
			
	} else {
		
		alert ( 'Ajax object creation failed' );
			
	}
	ajaxObj.open ( 'GET', 'ajax.php?action=get_ajax_productions', true );
	ajaxObj.send( null );

}

/* function: hide_pantomime_theatres()
 * hides all the theatres show by pantomime
*/
function hide_pantomime_theatres () {
		
	animatedcollapse.toggle ( 'show_pantomime_theatres' );
	animatedcollapse.toggle ( 'show_pantomimes' );
			
}

		
/* closes all search areas */
function close_all_search() {

	document.getElementById ( 'postcode_search' ).style.display = 'none';
	document.getElementById ( 'show_theatre' ).style.display = 'none';
	document.getElementById ( 'show_pantomimes' ).style.display = 'none';
	document.getElementById ( 'show_pantomime_theatres' ).style.display = 'none';
	document.getElementById ( 'show_productions' ).style.display = 'none';
	document.getElementById ( 'map' ).style.display = 'none';
	document.getElementById ( 'key' ).style.display = 'none';
}
