printview = false


function goSearch()
{  
  var s = document.getElementById('search_term')
  if(s)
  {
    if(getOccurrences(location.href,"/") == 3)
      location.href = "search.aspx?term=" + s.value
    else
      location.href = "../search.aspx?term=" + s.value
  }
}


function getOccurrences(s,c)
{
  var n = 0
  for(i = 0; i <= s.length; i++)
    if(s.substring(i,i+1) == c) n = n + 1
  return n
}

function downloadPDF()
{
  alert('yo')
}


function init()
{
	//alert(document.referrer.toUpperCase())
	//if((document.referrer.toUpperCase()).indexOf("FALLON-CURRIE") == -1)	location.href = "http://www.fallon-currie.com"
	if(!document.all) startHighlight()
 
}

function doSearch(lev)
{
	frm = document.getElementById('frmMain')
	if(frm)
	{
		var term = frm.search_term.value
		location.href = lev + 'Search_Results.asp?term=' + term
	}
}

function opacity(id, opacStart, opacEnd, millisec) { 
    //speed for each frame 
    var speed = Math.round(millisec / 100); 
    var timer = 0; 

    //determine the direction for the blending, if start and end are the same nothing happens 
    if(opacStart > opacEnd) { 
        for(i = opacStart; i >= opacEnd; i--) { 
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed)); 
            timer++; 
        } 
    } else if(opacStart < opacEnd) { 
        for(i = opacStart; i <= opacEnd; i++) 
            { 
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed)); 
            timer++; 
        } 
    } 
} 

//change the opacity for different browsers 
function changeOpac(opacity, id) { 
    var object = document.getElementById(id).style; 
    object.opacity = (opacity / 100); 
    object.MozOpacity = (opacity / 100); 
    object.KhtmlOpacity = (opacity / 100); 
    object.filter = "alpha(opacity=" + opacity + ")"; 
} 




function toggleSub(sub)
{
	s = document.getElementById(sub)
	if(s)
		if(s.style.display == "none")
			s.style.display = "block"
		else
			s.style.display = "none"
}


function pop(url,id,args)
{
    window.open(url,id,args)
}
function GetCopy()
{
	return '<span class=cr>&copy;&nbsp;The Glutamate Association ' + GetYear() + '</span>'
}

function GetLinks(lev)
{
	return '<a class=white href="'+lev+'About_TGA.asp">Who we are</a>&nbsp;|&nbsp;<a class=white href="'+lev+'Contact.asp">Contact Us</a>'
}

function dohand(obj)
{
	obj.style.cursor = 'pointer'
}

function printpage()
{
	if(!printview)
	{
		printview = true
		window.print()
	}
	else
	{
		printview = false
	}
		
	toggleview('logo')
	toggleview('header')
	toggleview('nav')
	toggleview('tools')
	toggleview('left')
	toggleview('right')
	toggleview('line_above_footer')
	toggleview('footer')
	toggleview('toolbar')
	toggleview('prev')
	toggleview('next')
	toggleview('printback')
}

function toggleview(id)
{
		o = document.getElementById(id)
		if(o)
			if(o.style.display == 'none')
				o.style.display = 'block'
			else
				o.style.display = 'none'
	}

function GetADate()
{
	var d = new Date()
	var months = new Array()
	months[0] = "January"
	months[1] = "February"
	months[2] = "March"
	months[3] = "April"
	months[4] = "May"
	months[5] = "June"
	months[6] = "July"
	months[7] = "August"
	months[8] = "September"
	months[9] = "October"
	months[10] = "November"
	months[11] = "December"
	return d.getDate() + ' ' + months[d.getMonth()] + ' ' + d.getFullYear()
}



function GetYear()
{
	var d = new Date()
	return d.getFullYear()
}


function preload(imgObj,imgSrc) {
	if (document.images) {
		eval(imgObj+' = new Image()')
		eval(imgObj+'.src = "'+imgSrc+'"')
	}
}

function swapImage(img1, img2){
	img1.src = img2.src
}

function setFocus(id)
{
	el = document.getElementById(id)
	if(el) el.focus()
}

function rollin(obj, c){
	//eval("obj.style.cssText='background-color:" + c + "'")
	eval("obj.style.cssText = 'border:solid 1px #000000'")
	obj.style.cursor = 'pointer'
}
function rollout(obj, c){
	//eval("obj.style.cssText='background-color:" + c + "'")
	eval("obj.style.cssText = 'border:solid 1px #b0b0b0'")
}

function forgottenPassword()
{
	var frm = document.getElementById('frmLogin')
	if(frm)
	{
		if(isEmail(frm.email.value))
		{
			frm.action = document.form1.action + "?mode=forgot"
			frm.submit()
		}
		else
		{
			alert("Please enter an email address")
			frm.email.focus()
		}
	}
}

function isEmail(eml)
{
	if(eml.indexOf("@") > 0 && eml.indexOf(".") > 0)
		return true
	else
		return false
}

function validate(frm)
{
	if(frm)
	{
		if(!isEmail(frm.emailaddr.value))
		{
			alert("Please enter an email address")
			frm.emailaddr.focus()
			return false
		}
		if(frm.name.value == "")
		{
			alert("Please enter your name")
			frm.name.focus()
			return false
		}
		return true
	}
}


function doSetFocus()
{
	frm = document.getElementById('frmData')
	if(frm)frm.firstname.focus()
}

function val_download(frm)
{
	if(frm)
	{
		if(frm.firstname.value == "")
		{
			alert("Please enter a first name")
			frm.firstname.focus()
			return false
		}
		if(frm.familyname.value == "")
		{
			alert("Please enter a last name")
			frm.familyname.focus()
			return false
		}
		if(!isEmail(frm.email.value))
		{
			alert("Please enter an email address")
			frm.email.focus()
			return false
		}
		if(frm.organisation.value == "")
		{
			alert("Please enter an organisation")
			frm.organisation.focus()
			return false
		}
		if(frm.address.value == "")
		{
			alert("Please enter an address")
			frm.address.focus()
			return false
		}
		if(frm.postcode.value == "")
		{
			alert("Please enter a post code")
			frm.postcode.focus()
			return false
		}
		if(frm.country.value == "")
		{
			alert("Please enter a country")
			frm.country.focus()
			return false
		}
		if(frm.tel.value == "")
		{
			alert("Please enter a telephone number")
			frm.tel.focus()
			return false
		}
		return true
	}
}

function val(frm)
{
	if(frm)
	{
		if(frm.firstname.value == "")
		{
			alert("Please enter a first name")
			frm.firstname.focus()
			return false
		}
		if(frm.lastname.value == "")
		{
			alert("Please enter a last name")
			frm.lastname.focus()
			return false
		}
		if(!isEmail(frm.email.value))
		{
			alert("Please enter an email address")
			frm.email.focus()
			return false
		}
		if(frm.message.value == "")
		{
			alert("Please enter a message")
			frm.message.focus()
			return false
		}
		return true
	}
}
function doLogIn()
{
	var frm = document.getElementById('frmLogin')
	if(frm)
	{
		if(!isEmail(frm.email.value))
		{
			alert("Please enter an email address")
			frm.email.focus()
		}
		else if(frm.password.value == "")
		{
			alert("Please enter a password")
			frm.password.focus()
		}
		else
		{
			frm.submit()
		}		
	}
}


function doEditDets()
{
	var frm = document.getElementById('frmEditDets')
	if(frm)
	{
		if(frm.firstname.value == "")
		{
			alert("Please enter a first name")
			frm.firstname.focus()
		}
		else if(frm.surname.value == "")
		{
			alert("Please enter a second name")
			frm.surname.focus()
		}
		else if(!isEmail(frm.email.value))
		{
			alert("Please enter an email address")
			frm.email.focus()
		}
		else if(frm.pwd.value == "")
		{
			alert("Please enter a password")
			frm.pwd.focus()
		}
		else
		{
			frm.submit()
		}		
	}
}


function doEditWebs()
{
	var frm = document.getElementById('frmEditWebs')
	if(frm)
	{
		frm.submit()
	}
}


function doEditContacts()
{
	var frm = document.getElementById('frmEditContacts')
	if(frm)
	{
		frm.submit()
	}
}


function doSend()
{
	var frm = document.getElementById('frmCompose')
	if(frm)
	{
		if(frm.subject.value == "")
		{
			alert("Please enter a subject")
			frm.subject.focus()
		}
		else if(frm.message.value == "")
		{
			alert("Please enter a message")
			frm.message.focus()
		}
		else
		{
			frm.submit()
		}		
	}
}

function doAddContact()
{
	var frm = document.getElementById('frmAdd')
	if(frm)
	{
		frm.submit()
	}
}






function setActiveStyleSheet(title) {  var i, a, main;
  createCookie("style", title, 365);  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {      a.disabled = true;      if(a.getAttribute("title") == title) a.disabled = false;  
    }  }}
function getActiveStyleSheet() {  var i, a;  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled)
		return a.getAttribute("title");  }  return null;}function getPreferredStyleSheet() {	var i, a;	for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {		if(a.getAttribute("rel").indexOf("style") != -1			&& a.getAttribute("rel").indexOf("alt") == -1			&& a.getAttribute("title")) 
			return a.getAttribute("title");	}	return null;}function createCookie(name,value,days) {	if (days) {		var date = new Date();		date.setTime(date.getTime()+(days*24*60*60*1000));		var expires = "; expires="+date.toGMTString();	}	else expires = "";	document.cookie = name+"="+value+expires+"; path=/";}function readCookie(name) {	var nameEQ = name + "=";	var ca = document.cookie.split(';');	for(var i=0;i < ca.length;i++) {		var c = ca[i];		while (c.charAt(0)==' ') c = c.substring(1,c.length);		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);	}	return null;}
var cookie = readCookie("style");var title = cookie ? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title);

function startHighlight()
{
	var term
	if(location.href.indexOf('?term=') != -1)
	{
		term = location.href.substring(location.href.indexOf('?term=') + 6)
		if(term.length > 1) highlightSearchTerms(term)
	}
}


/*
 * This is the function that actually highlights a text string by
 * adding HTML tags before and after all occurrences of the search
 * term. You can pass your own tags if you'd like, or if the
 * highlightStartTag or highlightEndTag parameters are omitted or
 * are empty strings then the default <font> tags will be used.
 */
function doHighlight(bodyText, searchTerm, highlightStartTag, highlightEndTag) 
{
  // the highlightStartTag and highlightEndTag parameters are optional
  if ((!highlightStartTag) || (!highlightEndTag)) {
    highlightStartTag = "<span style='color:black; font-weight:bold;'>";
    highlightEndTag = "</span>";
  }
  
  // find all occurences of the search term in the given text,
  // and add some "highlight" tags to them (we're not using a
  // regular expression search, because we want to filter out
  // matches that occur within HTML tags and script blocks, so
  // we have to do a little extra validation)
  var newText = "";
  var i = -1;
  var lcSearchTerm = searchTerm.toLowerCase();
  var lcBodyText = bodyText.toLowerCase();
    
  while (bodyText.length > 0) {
    i = lcBodyText.indexOf(lcSearchTerm, i+1);
    if (i < 0) {
      newText += bodyText;
      bodyText = "";
    } else {
      // skip anything inside an HTML tag
      if (bodyText.lastIndexOf(">", i) >= bodyText.lastIndexOf("<", i)) {
        // skip anything inside a <script> block
        if (lcBodyText.lastIndexOf("/script>", i) >= lcBodyText.lastIndexOf("<script", i)) {
          newText += bodyText.substring(0, i) + highlightStartTag + bodyText.substr(i, searchTerm.length) + highlightEndTag;
          bodyText = bodyText.substr(i + searchTerm.length);
          lcBodyText = bodyText.toLowerCase();
          i = -1;
        }
      }
    }
  }
  return newText;
}


/*
 * This is sort of a wrapper function to the doHighlight function.
 * It takes the searchText that you pass, optionally splits it into
 * separate words, and transforms the text on the current web page.
 * Only the "searchText" parameter is required; all other parameters
 * are optional and can be omitted.
 */
function highlightSearchTerms(searchText, treatAsPhrase, warnOnFailure, highlightStartTag, highlightEndTag)
{
  // if the treatAsPhrase parameter is true, then we should search for 
  // the entire phrase that was entered; otherwise, we will split the
  // search string so that each word is searched for and highlighted
  // individually
  if (treatAsPhrase) {
    searchArray = [searchText];
  } else {
    searchArray = searchText.split(" ");
  }
  
  if (!document.body || typeof(document.body.innerHTML) == "undefined") {
    if (warnOnFailure) {
      alert("Sorry, for some reason the text of this page is unavailable. Searching will not work.");
    }
    return false;
  }
  
  var bodyText = document.body.innerHTML;
  for (var i = 0; i < searchArray.length; i++) {
    bodyText = doHighlight(bodyText, searchArray[i], highlightStartTag, highlightEndTag);
  }
  
  document.body.innerHTML = bodyText;
  return true;
}


/*
 * This displays a dialog box that allows a user to enter their own
 * search terms to highlight on the page, and then passes the search
 * text or phrase to the highlightSearchTerms function. All parameters
 * are optional.
 */
function searchPrompt(defaultText, treatAsPhrase, textColor, bgColor)
{
  // This function prompts the user for any words that should
  // be highlighted on this web page
  if (!defaultText) {
    defaultText = "";
  }
  
  // we can optionally use our own highlight tag values
  if ((!textColor) || (!bgColor)) {
    highlightStartTag = "";
    highlightEndTag = "";
  } else {
    highlightStartTag = "<font style='color:" + textColor + "; background-color:" + bgColor + ";'>";
    highlightEndTag = "</font>";
  }
  
  if (treatAsPhrase) {
    promptText = "Please enter the phrase you'd like to search for:";
  } else {
    promptText = "Please enter the words you'd like to search for, separated by spaces:";
  }
  
  searchText = prompt(promptText, defaultText);

  if (!searchText)  {
    alert("No search terms were entered. Exiting function.");
    return false;
  }
  
  return highlightSearchTerms(searchText, treatAsPhrase, true, highlightStartTag, highlightEndTag);
}


/*
 * This function takes a referer/referrer string and parses it
 * to determine if it contains any search terms. If it does, the
 * search terms are passed to the highlightSearchTerms function
 * so they can be highlighted on the current page.
 */
function highlightGoogleSearchTerms(referrer)
{
  // This function has only been very lightly tested against
  // typical Google search URLs. If you wanted the Google search
  // terms to be automatically highlighted on a page, you could
  // call the function in the onload event of your <body> tag, 
  // like this:
  //   <body onload='highlightGoogleSearchTerms(document.referrer);'>
  
  //var referrer = document.referrer;
  if (!referrer) {
    return false;
  }
  
  var queryPrefix = "q=";
  var startPos = referrer.toLowerCase().indexOf(queryPrefix);
  if ((startPos < 0) || (startPos + queryPrefix.length == referrer.length)) {
    return false;
  }
  
  var endPos = referrer.indexOf("&", startPos);
  if (endPos < 0) {
    endPos = referrer.length;
  }
  
  var queryString = referrer.substring(startPos + queryPrefix.length, endPos);
  // fix the space characters
  queryString = queryString.replace(/%20/gi, " ");
  queryString = queryString.replace(/\+/gi, " ");
  // remove the quotes (if you're really creative, you could search for the
  // terms within the quotes as phrases, and everything else as single terms)
  queryString = queryString.replace(/%22/gi, "");
  queryString = queryString.replace(/\"/gi, "");
  
  return highlightSearchTerms(queryString, false);
}


/*
 * This function is just an easy way to test the highlightGoogleSearchTerms
 * function.
 */
function testHighlightGoogleSearchTerms()
{
  var referrerString = "http://www.google.com/search?q=javascript%20highlight&start=0";
  referrerString = prompt("Test the following referrer string:", referrerString);
  return highlightGoogleSearchTerms(referrerString);
}











