var ie4 = (document.all && !document.getElementById);
var ie5 = (document.all && document.getElementById);

//Added by Carl Rees to trap ener key for simple search
//26/8/2003
function doKey(e) {   
     if (ie5) {
	     if (event.keyCode == 13) {
		// Trap the carraige return
	        //alert('key 13');
		ret=99
		var searchstring = document.forms[0].search.value;
		if(searchstring==""){
			return false
		}else{
			alert("When you enter your search text on the home page you can see your results in just one click \r by selecting \"site\" or \"person\"");	
		}
		return false
	}
} else {
		//alert('sss');
	}
}

// Courtesy of Carlos 5/11/2003            - Use for Radio button and checkbox validation its cool! not geeky
//**********************************
function getSelectedRadio(buttonGroup) {
   // returns the array number of the selected radio button or -1 if no button is selected
   if (buttonGroup[0]) { // if the button group is an array (one button is not an array)
      for (var i=0; i<buttonGroup.length; i++) {
         if (buttonGroup[i].checked) {
            return i
         }
      }
   } else {
      if (buttonGroup.checked) { return 0; } // if the one button is checked, return zero
   }
   // if we get to this point, no radio button is selected
   return -1;
} // Ends the "getSelectedRadio" function

function getSelectedRadioValue(buttonGroup) {
   // returns the value of the selected radio button or "" if no button is selected
   var i = getSelectedRadio(buttonGroup);
   if (i == -1) {
      return "";
   } else {
      if (buttonGroup[i]) { // Make sure the button group is an array (not just one button)
         return buttonGroup[i].value;
      } else { // The button group is just the one button, and it is checked
         return buttonGroup.value;
      }
   }
} // Ends the "getSelectedRadioValue" function

function getSelectedCheckbox(buttonGroup) {
   // Go through all the check boxes. return an array of all the ones
   // that are selected (their position numbers). if no boxes were checked,
   // returned array will be empty (length will be zero)
   var retArr = new Array();
   var lastElement = 0;
   if (buttonGroup[0]) { // if the button group is an array (one check box is not an array)
      for (var i=0; i<buttonGroup.length; i++) {
         if (buttonGroup[i].checked) {
            retArr.length = lastElement;
            retArr[lastElement] = i;
            lastElement++;
         }
      }
   } else { // There is only one check box (it's not an array)
      if (buttonGroup.checked) { // if the one check box is checked
         retArr.length = lastElement;
         retArr[lastElement] = 0; // return zero as the only array value
      }
   }
   return retArr;
} // Ends the "getSelectedCheckbox" function

function getSelectedCheckboxValue(buttonGroup) {
   // return an array of values selected in the check box group. if no boxes
   // were checked, returned array will be empty (length will be zero)
   var retArr = new Array(); // set up empty array for the return values
   var selectedItems = getSelectedCheckbox(buttonGroup);
   if (selectedItems.length != 0) { // if there was something selected
      retArr.length = selectedItems.length;
      for (var i=0; i<selectedItems.length; i++) {
         if (buttonGroup[selectedItems[i]]) { // Make sure it's an array
            retArr[i] = buttonGroup[selectedItems[i]].value;
         } else { // It's not an array (there's just one check box and it's selected)
            retArr[i] = buttonGroup.value;// return that value
         }
      }
   }
   return retArr;
} // Ends the "getSelectedCheckBoxValue" function


function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}



function swapImg(imgArea, imgName,state, subNo) 
{
  //homeDb = document.forms[0].HomeDb.value ;	
  //taxonomyArea = document.forms[0].TaxonomyArea.value ;

  homeDb = window.document.forms[0].HomeDb.value ;	
  //alert('home db value here ' + window.document.forms[0].HomeDb.value );
  taxonomyArea = window.document.forms[0].TaxonomyArea.value ;					

  if (taxonomyArea == imgName)
  {
    return;  // this image always needs tobe highlighted
  }

  tmp = homeDb + '/vwWebResources/' + imgArea + imgName + state + '/$file/' + imgName + '_' + state + '.gif';

  //alert('in swapImg = ' + tmp);
		
  if(document.images) 
  {
    if (subNo == "" | String(subNo) == "undefined") 
    {	          
      tmpImageName = imgName;					
    }	
    else
    {
	tmpImageName = imgName + subNo;							
    }
			
    document.images[tmpImageName].src = tmp;
  }
}

function debugSwapImg(imgArea, imgName,state, subNo) 
{
  homeDb = window.document.forms[0].HomeDb.value ;	
  tmp = homeDb + '/vwWebResources/' + imgArea + imgName + state + '/$file/' + imgName + '_' + state + '.gif';
				
  if(document.images) 
  {
    if (subNo == "" | String(subNo) == "undefined") 
    {	          
      tmpImageName = imgName;	
      alert('No subno: ' + 'in swapImg = ' + tmp);
    }	
    else
    {
	tmpImageName = imgName + subNo;				
	alert(tmpImageName + ' :--' + 'in swapImg = ' + tmp);
    }
			
    document.images[tmpImageName].src = tmp;
  }
}

function highlightLevel1Taxonomy()
{
  // highlight appropriate level 1 taxonomy image to indicate which Db user is in

  homeDb = window.document.forms[0].HomeDb.value ;	
  taxonomyArea = window.document.forms[0].TaxonomyArea.value ;	

  if (taxonomyArea != "iris")
  { 
    if (taxonomyArea == "schemes")  // compensate for Db being called schemes instead of scheme
    { 
       taxonomyArea = "scheme";
    }

    tmp = homeDb + '/vwWebResources/GlobalNav' + taxonomyArea + "Over" + '/$file/' +  taxonomyArea + '_Over.gif';  
    document.images[taxonomyArea].src = tmp
  }

  //alert('Taxonomy Area = <' + taxonomyArea + '> - image url =<' +  tmp + '>');
}


//function bookmarkthisURL()
//
//{
//  window.external.AddFavorite(window.location.href, "IRIS - ")  
//}

function bookmarkthisURL()
{
		 if (document.forms[0].eGif_Title)
  		{
		var title = "iris - " + document.forms[0].eGif_Title.value; 
		}
		else
		{
		var title = "iris - ";
		}
	

title = title.replace("\\", " - ") // checks for ugly \ in taxonomy
title = title.replace("\\", " - ") // checks for ugly \ in taxonomy
title = title.replace("\\", " - ") // checks for ugly \ in taxonomy
title = title.replace("\\", " - ") // checks for ugly \ in taxonomy
title = title.replace("\\", " - ") // checks for ugly \ in taxonomy
title = title.replace("\\", " - ") // checks for ugly \ in taxonomy
title = title.replace("\\", " - ") // checks for ugly \ in taxonomy
title = title.replace("\\", " - ") // checks for ugly \ in taxonomy
title = title.replace("\\", " - ") // checks for ugly \ in taxonomy
title = title.replace("\\", " - ") // checks for ugly \ in taxonomy
title = title.replace("\\", " - ") // checks for ugly \ in taxonomy
//I know it's ugly, but title.search won't allow search for \!
 
if (title.length > 60)
	{
	title = title.substring(0,60);
	}
else
{
//
}
	window.external.AddFavorite(window.location.href, title);
}

function ReplaceChar(text,char,newchar){
	var st=new String(text);
	var s=st.split(char);
	var j=s.join(newchar);
	return j;
}

function emailThisURL()
{
  var newtextstring=ReplaceChar(window.location.href,"%20", "+");
  //alert(' url is ' +newtextstring);
  var link = "mailto:?body=" + newtextstring;
  location = link
}


function LogUserInOut()
{ 
  // logs user in or out depend upon the existing LoginOption field
  var loginOption = document.forms[0].LoginOption.value;
  var urlpath = window.location.pathname;
  var thisURL = window.location.href;
  
  // get URL to .nsf being used
  dbpath = urlpath.substring(0,(urlpath.lastIndexOf(".nsf")+4));

	var pos=dbpath.indexOf(".nsf")
	if (pos>=0)
	{
	} else {
		dbpath = window.location.pathname+"intranet/iris.nsf"
	}
    
  if (loginOption == "Log in")
  {
     newURL =  dbpath+ "?login&redirectto="+thisURL;    
     //newURL =  dbpath+ "?login";
  }
  else
  {      
     newURL = dbpath+ "?logout&redirectto="+thisURL;
     alert('Log out - completed');
  }
     
   parent.top.location.href = newURL;   
}
function getScreenWidth()
{ 
if (self.screen) {     // for NN4 and IE4
        width = screen.width
        height = screen.height
// Testing this first prevents firing the slow Java of NN4
}
else if (self.java) {   // for NN3 with enabled Java
       var jkit = java.awt.Toolkit.getDefaultToolkit();
       var scrsize = jkit.getScreenSize();       
       width = scrsize.width; 
       height = scrsize.height; 
}
else{
 width = height = '?' // N2, E3, N3 w/Java off, probably Opera and WebTV
}
	return width;
}

// *** CHECK DATE VALIDATION ***

function chkdate(objName) {
var strDatestyle = "US"; //United States date style
//var strDatestyle = "EU";  //European date style
var strDate;
var strDateArray;
var strDay;
var strMonth;
var strYear;
var intday;
var intMonth;
var intYear;
var booFound = false;
var datefield = objName;
var strSeparatorArray = new Array("-"," ","/",".");
var intElementNr;
var err = 0;
var strMonthArray = new Array(12);
strMonthArray[0] = "Jan";
strMonthArray[1] = "Feb";
strMonthArray[2] = "Mar";
strMonthArray[3] = "Apr";
strMonthArray[4] = "May";
strMonthArray[5] = "Jun";
strMonthArray[6] = "Jul";
strMonthArray[7] = "Aug";
strMonthArray[8] = "Sep";
strMonthArray[9] = "Oct";
strMonthArray[10] = "Nov";
strMonthArray[11] = "Dec";
strDate = datefield.value;
if (strDate.length < 1) {
return true;
}
for (intElementNr = 0; intElementNr < strSeparatorArray.length; intElementNr++) {
if (strDate.indexOf(strSeparatorArray[intElementNr]) != -1) {
strDateArray = strDate.split(strSeparatorArray[intElementNr]);
if (strDateArray.length != 3) {
err = 1;
return false;
}
else {
strDay = strDateArray[0];
strMonth = strDateArray[1];
strYear = strDateArray[2];
}
booFound = true;
   }
}
if (booFound == false) {
if (strDate.length>5) {
strDay = strDate.substr(0, 2);
strMonth = strDate.substr(2, 2);
strYear = strDate.substr(4);
   }
}
if (strYear.length == 2) {
strYear = '20' + strYear;
}
// US style
if (strDatestyle == "US") {
strTemp = strDay;
strDay = strMonth;
strMonth = strTemp;
}
intday = parseInt(strDay, 10);
if (isNaN(intday)) {
err = 2;
return false;
}
intMonth = parseInt(strMonth, 10);
if (isNaN(intMonth)) {
for (i = 0;i<12;i++) {
if (strMonth.toUpperCase() == strMonthArray[i].toUpperCase()) {
intMonth = i+1;
strMonth = strMonthArray[i];
i = 12;
   }
}
if (isNaN(intMonth)) {
err = 3;
return false;
   }
}
intYear = parseInt(strYear, 10);
if (isNaN(intYear)) {
err = 4;
return false;
}
if (intMonth>12 || intMonth<1) {
err = 5;
return false;
}
if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intday > 31 || intday < 1)) {
err = 6;
return false;
}
if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intday < 1)) {
err = 7;
return false;
}
if (intMonth == 2) {
if (intday < 1) {
err = 8;
return false;
}
if (LeapYear(intYear) == true) {
if (intday > 29) {
err = 9;
return false;
}
}
else {
if (intday > 28) {
err = 10;
return false;
}
}
}
if (strDatestyle == "US") {
datefield.value = strMonthArray[intMonth-1] + " " + intday+" " + strYear;
}
else {
datefield.value = intday + " " + strMonthArray[intMonth-1] + " " + strYear;
}
return true;
}
function LeapYear(intYear) {
if (intYear % 100 == 0) {
if (intYear % 400 == 0) { return true; }
}
else {
if ((intYear % 4) == 0) { return true; }
}
return false;
}
function doDateCheck(from, to) {
if (Date.parse(from.value) <= Date.parse(to.value)) {
alert("The dates are valid.");
}
else {
if (from.value == "" || to.value == "") 
alert("Both dates must be entered.");
else 
alert("To date must occur after the from date.");
   }
}
