
/*General document sub-object finding routine. Useful for
getting a reference to an html element in Netscape.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
function MM_findObj(n, d) { //v4.01
  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 && d.getElementById) x=d.getElementById(n); return x;
}


function MM_showHideLayers() { //v3.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v='hide')?'hidden':v; }
    obj.visibility=v; if (v=='visible'){obj.display=''}else{obj.display='none';}}
}


/*Navigator 4 resize bug workaround
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);



/*Image handling functions
~~~~~~~~~~~~~~~~~~~~~~~~~~*/
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];}}
}

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 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_showHideLayers() { //v3.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v='hide')?'hidden':v; }
    obj.visibility=v; if (v=='visible'){obj.display=''}else{obj.display='none';}}
}


function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}


function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}


/*Form Validation and Handling Functions
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

 function TrimTextboxValue(Textbox) {
  while(''+Textbox.value.charAt(0)==' ')
    Textbox.value = Textbox.value.substring(1,Textbox.value.length);
  var Length   = Textbox.value.length;
  var LastChar = Textbox.value.charAt(Length - 1);
  while(''+LastChar==' ') {
    Textbox.value = Textbox.value.substring(0,Textbox.value.length-1);
    Length        = Textbox.value.length;
    LastChar      = Textbox.value.charAt(Length - 1);
  }
 }


 function RemoveNonPrintingCharacters(Textbox) {
  var NoPrints = new RegExp("[\n\r\t]", "g");
  Textbox.value = Textbox.value.replace(NoPrints, "");
 }


 function NoBlanks(InpString) {
  var Blank = new RegExp(" ", "g"); //g means replace all, i means ignore case
  var OutString;
  OutString = InpString.replace(Blank, "");
  return OutString;
 }


/*==================================
  Restrict textbox field to numerals
  ==================================*/
//To be called by onKeyUp
  function RestrictToNumeralKey(Textbox) {
    var Text     = Textbox.value;
    var Length   = Text.length;
    var LastChar = Text.charAt(Length - 1);
    if(LastChar.search(/[0-9]/) == -1) Textbox.value = Text.substring(0, Length - 1);
    RemoveNonNumerals(Textbox);
  }

//To be called by onBlur
  function RemoveNonNumerals(Textbox) {
    Textbox.value = Textbox.value.replace(/[^0-9]/g,'');
  }

 function ShadowCheckbox(FieldName, BooleanFormat) {
/*************************************************
 Since a checkbox doesn't return a name=value pair to the server unless it's checked, a hidden field is associated with each 
 checkbox to always return a  value. The value assigned may be in any of the forms 1/0, true/false, yes/no, or on/off
 depending on the optional input "BooleanFormat" value, which should be a string, one of "1/0", "true/false", etc. 
 A naming convention must be followed for the checkbox. For a hidden field named "SomeYesNoField", the checkbox must be 
 named "frmSomeYesNoField".
 Example call: <input type=checkbox name="frmSomeBooleanField" onClick="ShadowCheckbox('SomeBooleanField', 'Yes/No')">
               <input type=hidden   name="SomeBooleanField">
 This function is called from the onClick event of the checkbox.
*/

  var BooleanVals = new Array();
  BooleanVals[0] = "true";
  BooleanVals[1] = "false";
  if (BooleanFormat != "") {
    if (BooleanFormat.search('/') != -1) BooleanVals = BooleanFormat.split('/',2);
  }

  var TheForm = document.forms[0];
  if (eval("TheForm.frm"+FieldName+".checked")) {
    eval("TheForm."+FieldName+".value = '"+BooleanVals[0]+"'");
  }else{
    eval("TheForm."+FieldName+".value = '"+BooleanVals[1]+"'");
  }
 }


 function AssignHiddenValue(FieldName, OptionType) {
/*
 This function assigns the value of a hidden field based on the item selected from a list or from a set
 of radio buttons and the value of a textbox labeled "Other". The hidden field represents the actual 
 field value, while the listbox/radio-buttons and "other" textbox constitute a means to select a field
 value from a set of options, or to specify an alternative value if the desired value is not in the set.

 A naming convention must be followed. For a hidden field named "SomeHiddenField", the list must be named 
 "frmSomeHiddenFieldOptions", and the textbox containing the "other" value must be named "frmSomeHiddenFieldOther".
 This function is called from the onBlur events of the listbox/radio-buttons and the textbox.
*/
  if (!OptionType) {OptionType='';}
  var TheForm = document.forms[0];
  var ListValue;
  if (OptionType == '') {
    ListValue = eval("TheForm.frm"+FieldName+"Options.options[TheForm.frm"+FieldName+"Options.selectedIndex].value");
  }
  else if (OptionType == 'radio') {
    ListValue = eval("TheForm.frm"+FieldName+"Options.value");
  }

  if (ListValue == "Other") {
    eval("TheForm."+FieldName+".value = TheForm.frm"+FieldName+"Other.value");
  }else{
    eval("TheForm."+FieldName+".value = ListValue");
    if (eval("TheForm.frm"+FieldName+"Other.value != ''")) {
      eval("TheForm.frm"+FieldName+"Other.value = ''");
    }
  }
 }



  function SelectRadioBtnByHiddenValue(FieldName, TheForm) {
    var HiddenFieldValue;
    eval('HiddenFieldValue = TheForm.'+FieldName+'.value');
    var TheOptionGroup;
    eval('TheOptionGroup = TheForm.frm'+FieldName);
    for (var n=1; n<=TheOptionGroup.length; n++) {
      if (TheOptionGroup[n-1].value == HiddenFieldValue) {
        TheOptionGroup.selectedIndex = n-1;
        break;
      }
    }
  }




 function ValidateEmail(Email) {
  var checkbeg = /^[.+]/;       //Match one or more characters, at the beginning
  var checkmid = /@[\w\-]+\./;  //Match an @ sign, followed by A-Z, a-z, 0-9, _, or - repeated any number of times
  var checkend = /\..{2,3}$/;   //Match a dot followed by 2 or three characters, at the end
  if( (Email.search(checkbeg) != -1) || (Email.search(checkmid) == -1) || (Email.search(checkend) == -1) ) {
    return false;
  }else{
    return true;
  }
 }


 function ValidateDate(Textbox) {
  if (Textbox.value == '') return;
  if (!IsADate(Textbox.value)) {
    alert(Textbox.value+' is not a valid date');
    Textbox.focus();
  }
 }

 function IsADate(InpToTest) {
  var aDate = new Date(InpToTest);
  if (aDate.getDay()) {
    return true;
  }else{
    return false;
  }
 }


  function IsANumber(InpNumber) {
    if (parseFloat(InpNumber)) {
      return true;
    }else{
      return false;
    }
  }


  function IsEven(InpNumber) {
    if (!IsANumber(InpNumber)) {
      alert("Error: Input to function IsEven is not a number");
      return;
    }
    var NumBy2_real = parseFloat(InpNumber/2.);
    var NumBy2_int  = parseInt(InpNumber/2);
    var Remainder   = NumBy2_real - NumBy2_int;
    if (Remainder == 0) {
      return true;
    }else{
      return false;
    }
  }


  function IsOdd(InpNumber) {
    if (!IsANumber(InpNumber)) {
      alert("Error: Input to function IsEven is not a number");
      return;
    }
    var NumBy2_real = parseFloat(InpNumber/2.);
    var NumBy2_int  = parseInt(InpNumber/2);
    var Remainder   = NumBy2_real - NumBy2_int;
    if (Remainder != 0) {
      return true;
    }else{
      return false;
    }
  }


  function OneZero(IsTrue) {
   if (IsTrue) return "1";
   else return "0";
  }


  function TF(IsOne) {
   if (IsOne==1) return true;
   else return false;
  }


//Return a single value representing the selection from an option group
  function OptionGroupValue(OptionGroup) {
   var NumItems = OptionGroup.length;
   for (var n=1; n<=NumItems; n++) {
    if (OptionGroup[n-1].checked) {
     return OptionGroup[n-1].value;
    }
   }
  }


  function IndexOfListValue(List, Val) {
   var NumItems = List.length;
   for (var n=1; n<=NumItems; n++) {
    if (List[n-1].value == Val) return n-1;
   }
   return -1;
  }


  function SetClass(ElementID, InpClassName) {
    var Elem = document.getElementById(ElementID);
    Elem.className = InpClassName;
  }


//###################################
//Publications Bibliography functions
//###################################

  function SelectAllPubs() {
    if (document.PubsSelectorForm.frmAllPubs.checked == true) {
      DeSelectAll('TypeIDs');
      DeSelectAll('YearIDs');
      DeSelectAll('AuthorIDs');
      DeSelectAll('SubjectIDs');
      DeSelectAllTitles();
      DeSelectAll('SourceIDs');
    }
  }

//===============================================
//Clear all the items in a list.
//The list must have the 'multiple' attribute set
//===============================================
  function DeSelectAll(ListName) {
    var ListObj;
    eval('ListObj = document.PubsSelectorForm.'+ListName);
    for (var n=1; n<=ListObj.length; n++) {
      ListObj.options[n-1].selected = false;
    }
  }

  function DeSelectAllTitles() {
    document.PubsSelectorForm.TitleWord1.value = ""
    document.PubsSelectorForm.TitleWord2.value = ""
  }

  function GoToPage(PageInc) {
    var PageNum = parseInt(document.PageForm.PageNum.value) + PageInc;
    document.PageForm.PageNum.value = PageNum;
    document.PageForm.submit();
  }
