/************************************************************************/
/* NHMRC JAVASCRIPT ROUTINES                
This javascript file contains routines to switch style sheets from small
font to large font size when the user clicks on the corresponding icon.

There is also a routine to clear the search box at the end of this file.
*/
/************************************************************************/
function mailThisUrl() {
var docTitle = document.title;
var currentUrl = window.location;
var newline = '\n';
window.location ="mailto:?subject=NHMRC%20website:%20"+docTitle+"&body=%0A"+newline+" Page URL%20"+currentUrl;
}

// leaving the web site
function linkAlert(URL){
	
	var newline = '\n';
	var sMessage = "You are about to leave the NHMRC website. If this is your intention please click either: ";
	sMessage = sMessage + newline;
	sMessage = sMessage + "- OK to proceed to new website; or";
	sMessage = sMessage + newline;
	sMessage = sMessage + "- CANCEL to return to the current page.";
	sMessage = sMessage + newline; 
	sMessage = sMessage + "Clicking OK will open the new website in a new browser window.";
	sMessage = sMessage + newline; 
	sMessage = sMessage + newline; 
	sMessage = sMessage + "If, after clicking 'OK', the new page does not appear, please check that you don't have a pop-up blocker turned on";

	if(confirm(sMessage)) {
		window.open.location;	
		
	}
	else{
		
		return false;
		
	}

}
// clear the search box routine  window.open.location = URL;

function clearDefault(el) {
  if (el.defaultValue==el.value) el.value = ""
}

// Preload images

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}


// switch styles sheet functions

function fontsizeup() {
  active = getActiveStyleSheet();
  switch (active) {
    case 'A-' : 
      setActiveStyleSheet('A+');
      break;
	default: 
	  setActiveStyleSheet('A-');
	  break;
  }
}

function fontsizedown() {
  active = getActiveStyleSheet();
  switch (active) {
    case 'A+' : 
      setActiveStyleSheet('A-');
      break;
	  default: 
	  setActiveStyleSheet('A-');
	  break;
  }
}

function setActiveStyleSheet(title) {
  var i, a, main;
  /* BROWSER TEST FOR IE4 */
  if (!(document.getElementById && document.createElement))
  {
	 return "A-";
  } else
  {
  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;
    /* BROWSER TEST FOR IE4 */
   if (!(document.getElementById && document.createElement))
  {
	 return "A-";
  } else
  {
  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() {
  return ('A-');
}

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;
}

window.onload = function(e) {
  var cookie = readCookie("style");
 /* alert (cookie); */
  var title = cookie ? cookie : getPreferredStyleSheet();
  if (title == 'null') {
  title = getPreferredStyleSheet();
  }
 setActiveStyleSheet(title);

}

window.onunload = function(e) {
  var title = getActiveStyleSheet();
  createCookie("style", title, 365);
   
}

var cookie = readCookie("style");
var title = cookie ? cookie : getPreferredStyleSheet();
if (title == 'null') {
  title = getPreferredStyleSheet();
}

setActiveStyleSheet(title);

// JavaScript Code for the search function publications

			function go()
			{
				var doc = document.getElementById("select");
				var navto = (doc.options[doc.selectedIndex]).value;
				window.location = navto;			
			}
			
function IsEmpty(words) {
   if ((words.value.length==0) ||(words.value==null)||(words.value=="Search Publications")||(words.value=="Search")) 
   {
   alert("please enter keywords")
	f.words.focus();
	        return false;}
return true;
   }
function validform(){
f.sort.value="score";

f.words.value=stripCharsInBag(f.words.value, String.fromCharCode(34));

	if (f.method[2].checked){
		f.words.value=String.fromCharCode(34) + f.words.value + String.fromCharCode(34);
		
		f.method[2].value="and";
	}

	
	var dn= new Date();
	f.endday.value=dn.getdate();
	f.endmonth.value=dn.getMonth();
	f.endyear.value=dn.getYear();
	f.startyear.value=dn.getYear();
	
	switch(f.age.value) {
		case(f.age.value="1"): 
			f.startday.value=-7; 
			f.startmonth.value=dn.getMonth();
			break
		case(f.age.value="2"): 
			f.startday.value=dn.getdate();
			f.startmonth.value=-1; 
			break
		case(f.age.value="3"): 
			f.startday.value=dn.getdate();
			f.startmonth.value=-3; 
			break
		case(f.age.value="4"): 
			f.startday.value=dn.getdate();
			f.startmonth.value=-12; 
			break
		default:
			f.startday.value=dn.getdate();
			f.endmonth.value=dn.getMonth();
			f.startyear.value=1900;
		break
	}
}

function stripCharsInBag (s, bag)

{ var i;
var returnString = "";
// Search through string's characters one by one.
// If character is not in bag, append to returnString.

for (i = 0; i < s.length; i++)
{ 
// Check that current character isn't whitespace.
var c = s.charAt(i);
if (bag.indexOf(c) == -1) returnString += c;
}

return returnString;
}
function popUp(URL) {
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=1,width=550,height=400,left = 50,top = 20');");
}
/****************************************Calculator validation*******************************************/
function checkAll(field)
{
for (i = 0; i < field.length; i++)
	field[i].checked = true ;
}

function uncheckAll(field)
{
for (i = 0; i < field.length; i++)
	field[i].checked = false ;
}

/*checkAll(document.calculatorform.vitamins[])*/