var ModalDialogWindow;
var ModalDialogInterval;
var ModalDialog = new Object;


ModalDialog.value = '';
ModalDialog.eventhandler = '';

var refreshing = false;

function init() {
if (!document.getElementById) return false;
var f = document.getElementById('search_box');


f.setAttribute("autocomplete", "off");

}

function refresh_page(){
	if (refreshing == false) {
		//only run this function once!!!
		refreshing = true;
		document.location.replace(document.location.href);
	}
}

function ModalDialogMaintainFocus()
{
	try
	{
		if (ModalDialogWindow.closed)
		{
			window.clearInterval(ModalDialogInterval);
			eval(ModalDialog.eventhandler);
			return;
		}

//			ModalDialogWindow.focus();
	}
	catch (everything)
	{

	}
}

function ModalDialogRemoveWatch()
{
	ModalDialog.value = '';
	ModalDialog.eventhandler = '';
}

function ModalDialogShow(page_to_show, do_refresh)
{
	var do_refresh = (do_refresh == null) ? true : do_refresh;

	ModalDialogRemoveWatch();

	if(do_refresh === true)
	{
		ModalDialog.eventhandler = "refresh_page()";
	}

	var args='width=800,height=610,left=325,top=300,toolbar=0,';
	args += 'location=0,status=0,menubar=0,scrollbars=1,resizable=0'; 

	ModalDialogWindow=window.open("","",args);
	ModalDialogWindow.document.open('');
	ModalDialogWindow.document.location = page_to_show;
	ModalDialogWindow.document.close();
	ModalDialogWindow.focus();
	ModalDialogInterval = window.setInterval("ModalDialogMaintainFocus()", 5);
}

String.prototype.trim = function()
{
	return this.replace(/^\s+|\s+$/g,"");
}





function tour_edication(){
	element = document.getElementById('explore_nav');
	
	if (element.style.display == "block") {
		//make hidden
		element.style.display = "none";
	}
	else {
		//make visible
		element.style.display = "block";
	}
}












/*functions suggest*/

function perform_auto_complete(table, select_field, against_field, order, element){
	// get the value in the field
	var keywords = encodeURIComponent(element.value.trim());
	
	if (keywords == "") {
		if (document.getElementById("auto_complete_" + element.id)) {
			var auto_complete_div = document.getElementById("auto_complete_" + element.id);
			auto_complete_div.style.visibility = "hidden";
			auto_complete_div.innerHTML = "";
		}
	}
	else {
		url = "/perform_auto_complete.php";
		params = "table=" + table + "&select_field=" + select_field + "&against_field=" + against_field + "&keywords=" + keywords + "&order=" + order + "&target=" + element.id;
		send_ajax(params, url, 'POST', true, 'auto_complete_response');
	}
}

function auto_complete_response(ajax_id){
	var doc = ajax_array[ajax_id].responseXML;
	var suggestion_list = "<ul style='padding:0; margin:0;'>";
	
	if (doc == null) {
		alert("There has been an error getting suggestions.");
		alert(ajax_array[ajax_id].responseText);
	}
	else {			
		//foreach xml entry populare disputes array
		node = doc.getElementsByTagName('suggestion');
		
		target = doc.getElementsByTagName('target')[0].firstChild.nodeValue;
		
		//bolding
		//get current search text
		//get pos/split form end of text and bolden the second entry
		
		for (i = 0; i < node.length; i++) {
			if (node[i].firstChild) {
				suggestion = node[i].firstChild.nodeValue;
				suggestion_list += "<li onclick='load_suggest2(\"" + target + "\",this.innerHTML)'>" + suggestion + "</li>\n";
			}
		}
		suggestion_list += "</ul>";
		auto_complete_div = document.getElementById("auto_complete_" + target);
		
		switch_auto_complete(target, true)
		auto_complete_div.innerHTML = suggestion_list;
	}
}


function load_suggest2(target, value){
	value = strip_tags(value);
	document.getElementById(target).value = value;	
	switch_auto_complete(target, false)
}

function dellay_switch_auto_complete(element_id, show){
	setTimeout("switch_auto_complete('"+element_id+"', '"+show+"')", 200);
}

function switch_auto_complete(element_id, show){
	if (document.getElementById("auto_complete_" + element_id)) {
		
		auto_complete_div = document.getElementById("auto_complete_" + element_id);
		
		if (show === true) {
			auto_complete_div.style.visibility = "visible";
		}
		else {
			auto_complete_div.style.visibility = "hidden";
			auto_complete_div.innerHTML = "";
		}
	}
}




//FUNCTION FROM PHP.js library
function strip_tags(str, allowed_tags){

	var key = '', allowed = false;
	var matches = [];
	var allowed_array = [];
	var allowed_tag = '';
	var i = 0;
	var k = '';
	var html = '';
	
	var replacer = function(search, replace, str){
		return str.split(search).join(replace);
	};
	
	// Build allowes tags associative array
	if (allowed_tags) {
		allowed_array = allowed_tags.match(/([a-zA-Z]+)/gi);
	}
	
	str += '';
	
	// Match tags
	matches = str.match(/(<\/?[\S][^>]*>)/gi);
	
	// Go through all HTML tags
	for (key in matches) {
		if (isNaN(key)) {
			// IE7 Hack
			continue;
		}
		
		// Save HTML tag
		html = matches[key].toString();
		
		// Is tag not in allowed list? Remove from str!
		allowed = false;
		
		// Go through all allowed tags
		for (k in allowed_array) {
			// Init
			allowed_tag = allowed_array[k];
			i = -1;
			
			if (i != 0) {
				i = html.toLowerCase().indexOf('<' + allowed_tag + '>');
			}
			if (i != 0) {
				i = html.toLowerCase().indexOf('<' + allowed_tag + ' ');
			}
			if (i != 0) {
				i = html.toLowerCase().indexOf('</' + allowed_tag);
			}
			
			// Determine
			if (i == 0) {
				allowed = true;
				break;
			}
		}
		
		if (!allowed) {
			str = replacer(html, "", str); // Custom replace. No regexing
		}
	}
	
	return str;
}











/*functions_ajax_chris.js*/

//Ajax = get_ajax();
var ajax_array = new Array();
var ajax_controller = new Array();
ajax_controller[0] = 0; //0 = availiable, 1 = taken
//when a send ajax is called
//loop through the length of ajax_controller, 1 = in use, 0 = availiable
//if it cant find one thats 0, push to the end. when ajax send_complete ajax = 0 ajax_array[] - get_ajax()


var responce;
var overlay;

function get_ajax(){
	//incase the controller is full, the ajax id is the length
	var ajax_id = ajax_controller.length;
	
	for (i = 0; i < ajax_controller.length; i++) {
		if (ajax_controller[i] == 0) {
			ajax_id = i;
			break;
		}
	}
	
	ajax_controller[ajax_id] = 1;
	
	//create the ajax request
	if (window.XMLHttpRequest) { // Non-IE browsers
		ajax_array[ajax_id] = new XMLHttpRequest();
	}
	else if (window.ActiveXObject) { // IE
		ajax_array[ajax_id] = new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	return ajax_id;
}


function send_ajax(params, url, type, get_reply, callback_function){
	//when a send ajax is called
	//loop through the length of ajax_controller, 1 = in use, 0 = availiable
	//if it cant find one thats 0, push to the end. when ajax send_complete ajax = 0 ajax[] - get_ajax()
	
	var ajax_id = get_ajax();
	ajax_id = parseInt(ajax_id);
	ajax_array[ajax_id].open(type, url, true);
	responce = "";
	
	params += "&ajax_id=" + ajax_id
	
	//Send the proper header information along with the request
	ajax_array[ajax_id].setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	ajax_array[ajax_id].setRequestHeader("Content-length", params.length);
	ajax_array[ajax_id].setRequestHeader("Connection", "close");
	
	
	if (get_reply) {
		if (callback_function) {
			ajax_array[ajax_id].onreadystatechange = function(event){
				if (ajax_ready(ajax_id)) {
					eval(callback_function + "(" + ajax_id + ")");
					ajax_null(ajax_id);
				}
			}
		}
		else {
			ajax_array[ajax_id].onreadystatechange = function(){//Call a function when the state changes.
				if (ajax_ready(ajax_id)) {
					responce = ajax_array[ajax_id].responseText;
					alert(responce);
					ajax_null(ajax_id);
				}
			}
		}
	}
	//send the request
	ajax_array[ajax_id].send(params);
	
	return ajax_id;
}


function ajax_ready(ajax_id){
	if (ajax_array[ajax_id].readyState == 4 && ajax_array[ajax_id].status == 200) {
		return true;
	}
	else {
		return false;
	}
}

function ajax_null(ajax_id){
	ajax_controller[ajax_id] = 0;
}




function gotoPage(thepage)
{
	var page_url = document.location.search.split("&pn=");
	document.location.search = page_url[0] + "&pn=" + thepage;
}

function getXmlHttpRequestObject()
{
	if (window.XMLHttpRequest)
	{
		return new XMLHttpRequest();
	}
	else if(window.ActiveXObject)
	{
		return new ActiveXObject("Microsoft.XMLHTTP");
	}
	else
	{
		return false;
	}
}

var xml_request = getXmlHttpRequestObject();


function renew_session()
{
		xml_request.open("POST", "renew_session.php", true);
		xml_request.send(null);		
}

// renew the session every 10 minutes
var renew_session_timer = setInterval("renew_session()", 600000);

function ShowDiv2(button, element_id)
{
		
	var element = document.getElementById(element_id);
	element.style.position = "relative";
	//3 different background color statements
	//so they work in all browsers
	element.style.bgColor = "#E9EDD1";
	element.style.background = "#E9EDD1";
	element.style.backgroundColor = "#E9EDD1";
	element.style.display = "block";

}

function jsrsArrayFromString(s, delim){
  // rebuild an array returned from server as string
  // optional delimiter defaults to ~
  var d = (delim == null)? '~' : delim;
  return s.split(d);
} 


function ShowDiv(button, element_id)
{
	button.focus();
	//Hide the start div - this tells people how to use the map
	var start = document.getElementById('Mentoring');
	
	start.style.display = "none";
	
	var delim = "~";
	
	var arr = 'Campus_Visits~Mentoring~Master_Classes~Student_Ambassadors~IAG_Activities_Events~Summer_Schools__HEFCE_funded_~Residentials~Healthcare_Strand_activities~Staff_Development~Partnership_Marketing___Comms~Curriculum_Development~Transition_Progression_Support~Revision_Study_Sessions~Aimhigher_Associate_Scheme';

	
	//convert into an array
	var array = jsrsArrayFromString(arr, delim);
	
	//Loop through that array turning the display of each of the divs to none
	for (i=0;i<array.length;i++)
	{
		var tmp = document.getElementById(array[i]);
		tmp.style.display = "none";
	} 
	
	//Then turn the div on for area you just hovered over 
	var element = document.getElementById(element_id);
	
	element.style.display = "block";


}

function HideDiv(button, element_id){
	//Hide the start div - this tells people how to use the map
	var start = document.getElementById('Mentoring');
	
	start.style.display = "none";
	
	var delim = "~";
	
	var arr = 'Campus_Visits~Mentoring~Master_Classes~Student_Ambassadors~IAG_Activities_Events~Summer_Schools__HEFCE_funded_~Residentials~Healthcare_Strand_activities~Staff_Development~Partnership_Marketing___Comms~Curriculum_Development~Transition_Progression_Support~Revision_Study_Sessions~Aimhigher_Associate_Scheme';

	
	//convert into an array
	var array = jsrsArrayFromString(arr, delim);
	
	//Loop through that array turning the display of each of the divs to none
	for (i=0;i<array.length;i++)
	{
		var tmp = document.getElementById(array[i]);
		tmp.style.display = "none";
	} 
	
	//Then turn the div on for area you just hovered over 
	var element = document.getElementById(element_id);
	
	//element.style.display = "block";
}

function click_on_box(node){
	if(node != ""){
		var url = 'box_click.php';
		var params = "node="+node;
		send_ajax(params, url, 'POST', false);
	
	}
}