// Site wide Javascript functions. Is included on every page

function include(filename) {
	// Doesn't do anything, as this is handled in
	// the PHP code
}


function includeOrig(filename)
{
	var head = document.getElementsByTagName('head')[0];
	
	script = document.createElement('script');
	script.src =  basepath + "/javascript/" + filename;
	script.type = 'text/javascript';
	head.appendChild(script)
}

function getXmlHttp() {
	var xmlHttp;
	try {
  	// Firefox, Opera 8.0+, Safari
  	xmlHttp=new XMLHttpRequest();
  }	catch (e) {
  	// Internet Explorer
  	try {
    	xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
    	try {
     		xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
     	} catch (e) {
	     	return null;
     	}
    }
  }
  return xmlHttp;
}

function hasClass(ele,cls) {
	return ele.className.match(new RegExp('(\\s|^)'+cls+'(\\s|$)'));
}
 
function addClass(ele,cls) {
	if (!this.hasClass(ele,cls)) ele.className += " "+cls;
}
 
function removeClass(ele,cls) {
	if (hasClass(ele,cls)) {
    	var reg = new RegExp('(\\s|^)'+cls+'(\\s|$)');
		ele.className=ele.className.replace(reg,' ');
	}
}

function ajaxButton(url) {
	var xmlHttp;
	xmlHttp = getXmlHttp();
	if (xmlHttp == null) { // Cannot get object
		return true; // Let the link be pressed	
	}

  xmlHttp.onreadystatechange=function()
    {
    if(xmlHttp.readyState==4)
      {
				window.location.reload();
      }
    }

	xmlHttp.open("GET", url + '&from=Ajax', true);
  xmlHttp.send(null);
  return false;
}

function ajaxSnippet(obj, url) {
	var xmlHttp;
	addClass(obj, "backgroundLoading");
	xmlHttp = getXmlHttp();
  xmlHttp.onreadystatechange=function()
  	{
  	if(xmlHttp.readyState==4)
      {
      	obj.innerHTML = xmlHttp.responseText;
				removeClass(obj, "backgroundLoading");
      }
  	}
	xmlHttp.open("GET", url, true);
  xmlHttp.send(null);
  return false;
}

function myProgramButton(abstractId, action, reload) {
	var xmlHttp;
	xmlHttp = getXmlHttp();
	if (xmlHttp == null) { // Cannot get object
		return true; // Let the link be pressed	
	}

  xmlHttp.onreadystatechange=function()
    {
    if(xmlHttp.readyState==4)
      {
      	if (xmlHttp.responseText == 'removed') {
					linkText = 'Add';
					titleText = 'add to';
				}
      	if (xmlHttp.responseText == 'added') {
					linkText = 'Remove';
					titleText = 'remove from';
				}
				button = document.getElementById('myProgramButton_'+abstractId);
      	button.innerHTML = linkText;
      	button.href = 'program/programPersonalisedChange?abstractId='+ abstractId +'&action='+linkText.toLowerCase();
      	button.title = "Click here to "+ titleText + " your personalised program";
      	if (reload == 1) { 	// On the personalised page we reload to
      											// removed the "removed" abstract"
					window.location.reload();
				}
      }
    }
  xmlHttp.open("GET",'programPersonalisedChange?abstractId=' + abstractId + '&action=' + action + '&from=Ajax', true);
  xmlHttp.send(null);
  return false;
}

function updateAbstractMenusWithInterests(interests) {
	for (var i in interests) {
  	// Update the menu item, if it exists
  	$("li#add_" + interests[i].type + "_" + interests[i].id).css("display", "none");
  	$("li#remove_" + interests[i].type + "_" + interests[i].id).css("display", "inline");
	}
}

function myProgramButtonNew(abstractId, action) {
	var xmlHttp;
	xmlHttp = getXmlHttp();
	if (xmlHttp == null) { // Cannot get object
		return true; // Let the link be pressed	
	}


  xmlHttp.onreadystatechange=function()
    {
    if(xmlHttp.readyState==4)
      {
      	if (xmlHttp.responseText == 'removed') {
      		$('li#add_abstract_'+abstractId).css("display", "inline");
     			$('li#remove_abstract_'+abstractId).css("display", "none");
     			$('td#abstract_'+abstractId).css("visibility", "hidden").css("border", "none"); // Mainly for personalised program
     			$('div#abstract_'+abstractId).css("display", "none"); // Mainly for personalised program
     			$('li#abstract_'+abstractId).css("display", "none"); // Mainly for abstract recommendations
				}
      	if (xmlHttp.responseText == 'added') {
     			$('li#add_abstract_'+abstractId).css("display", "none");
      		$('li#remove_abstract_'+abstractId).css("display", "inline");
     			$('td#abstract_'+abstractId).css("visibility", "visible").css("border", "1px solid"); // Mainly for personalised program
     			$('div#abstract_'+abstractId).css("display", "block"); // Mainly for personalised program
     			$('li#abstract_'+abstractId).css("display", "block"); // Mainly for abstract recommendations
				}
      }
    }
  xmlHttp.open("GET",'programPersonalisedChange?abstractId=' + abstractId + '&action=' + action + '&from=Ajax', true);
  xmlHttp.send(null);
  return false;
}

