function MM_findObj(n, d) { 
  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_validateForm()
{ 
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for(i=0; i<(args.length-2); i+=3)
  { 
    test=args[i+2];
    val=MM_findObj(args[i]);
    if(val)
    { 
      nm=val.name;
      if((val=val.value)!="")
      {
        if(test.indexOf('isEmail')!=-1)
        { 
          p=val.indexOf('@');
          if (p<1 || p==(val.length-1))
            errors+='- '+nm+' must contain an e-mail address.\n';
        }
        else if(test!='R')
        {
          if(isNaN(val))
          {
            errors+='Only numbers are accepted.\n';
            val.value="";
          }
          if(test.indexOf('inRange') != -1)
          { 
            p=test.indexOf(':');
            min=test.substring(8,p);
            max=test.substring(p+1);
            if(val<min || max<val)
              errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
          }
        }
      }
      else if(test.charAt(0) == 'R')
        errors += '- '+nm+' is required.\n';
    }
  } 
  if(errors)
    alert(errors);
  document.MM_returnValue = (errors == '');
}

function Calendar(value)
{
  window.dateField = value;
  url = 'calendar.jsp';
  window.open(url,'Calendar','WIDTH=210,HEIGHT=210,toolbar=no,menubar=no,resizable=no,directories=no,scrollbars=no,status=no,top='+screen.height*0.6+',left='+screen.width*0.75);
}

function NumberCheck(obj, val)
{
  err = 0;
  check_string = obj.value
  if(check_string.length < val || check_string.length > val)
  {
    err++;    
  }
  if(err > 0)
  {
		err_msg = "Please enter "+ val +" digits";
		alert(err_msg);
		obj.value = "";
		obj.focus();
		return false; 
  }
  else
  {
    return true;
  }
}

function changeDiv(the_div,the_change)
{
  var the_style = getStyleObject(the_div);
  if (the_style != false)
  {
    the_style.display = the_change;
  }
}

function getStyleObject(objectId) {
  if (document.getElementById && document.getElementById(objectId)) {
    return document.getElementById(objectId).style;
  } else if (document.all && document.all(objectId)) {
    return document.all(objectId).style;
  } else {
    return false;
  }
}

function isEmpty(text)
{
  var re = /^\s{1,}$/g; //match any white space including space, tab, form-feed, etc. 
  if((text.value.length==0) || (text.value==null) || ((text.value.search(re)) > -1))
  {
    return true;
  }
  else
  {
    return false;
  }
}

function emailAddressCheck(str)
{
  var at="@";
  var dot=".";
  var lat=str.indexOf(at);
  var lstr=str.length;
  var ldot=str.indexOf(dot);
  var schar="'";
  if(str.indexOf(at)==-1)
  {
    alert("Invalid Email Address.");
    return false;
  }
  if(str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
  {
    alert("Invalid Email Address.");
    return false;
  }
  if(str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
  {
    alert("Invalid Email Address.");
    return false;
  }
  if(str.indexOf(at,(lat+1))!=-1)
  {
    alert("Invalid Email Address.");
    return false;
  }
  if(str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
  {
    alert("Invalid Email Address.");
    return false;
  }
  if(str.indexOf(dot,(lat+2))==-1)
  {
    alert("Invalid Email Address.");
    return false;
  }
  if(str.indexOf(" ")!=-1)
  {
    alert("Invalid Email Address.");
    return false;
  }
  if(str.indexOf(schar)!=-1)
  {
    alert("Invalid Email Address.");
    return false;
  }
  
  return true;
}

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