/*This function grabs the value from a passed in field. You should pass in the handle to the field, as in "document.forms[0].MyField". 
The return value is either a string or an array, depending on the field type (a check box would return an array).*/

function getFieldValue(field)
{
   switch(field.type)
   {
      case "text" :
      case "textarea" :
      case "password" :
      case "hidden" :
         return field.value;

      case "select-one" :
         var i = field.selectedIndex;
         if (i == -1)   return "";
         else   return (field.options[i].value == "") ? field.options[i].text : field.options[i].value;

      case "select-multiple" :
         var allChecked = new Array();
         for(i = 0; i < field.options.length; i++)
            if(field.options[i].selected)
               allChecked[allChecked.length] = (field.options[i].value == "") ? field.options[i].text : field.options[i].value;
         return allChecked;

      case "button" :
      case "reset" :
      case "submit" :
         return "";

      case "radio" :
      case "checkbox" :
         if (field.checked) { return field.value; } else { return ""; }
      default :
         if(field[0].type == "radio")
         {
            for (i = 0; i < field.length; i++)
               if (field[i].checked)
                  return field[i].value;

            return "";
         }
         else if(field[0].type == "checkbox")
         {
            var allChecked = new Array();
            for(i = 0; i < field.length; i++)
               if(field[i].checked)
                  allChecked[allChecked.length] = field[i].value;

            return allChecked;
         }
         else
            var str = "";
            for (x in field) { str += x + "\n"; }
            alert("I couldn't figure out what type this field is...\n\n" + field.name + ": ???\n\n\n" + str + "\n\nlength = " + field.length);
         break;
   }
   
   return "";
}

/* Radio Button Group Functions */
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 getSelectedCheckboxStrValue(buttonGroup) {
   // return an string of values, separated by "/",  selected in the check box group. if no boxes
   // were checked, returned string will be empty ("")
   var retArr = new Array(); // set up empty array for the return values
   var retStr = "";
   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
         	if( retStr == "" ){
	         	retStr = buttonGroup[selectedItems[i]].value; 
	        } else {
		        retStr = retStr+ "/" + buttonGroup[selectedItems[i]].value; 
		    }
            
         } else { // It's not an array (there's just one check box and it's selected)
            retStr = buttonGroup.value;// return that value
         }
      }
   }
   return retStr;
} // Ends the "getSelectedCheckBoxStrValue" function

function addToList(listField, newText, newValue) {
   if ( ( newValue == "" ) || ( newText == "" ) ) {
      alert("You cannot add blank values!");
   } else {
      var len = listField.length++; // Increase the size of list and return the size
      listField.options[len].value = newValue;
      listField.options[len].text = newText;
      listField.selectedIndex = len; // Highlight the one just entered (shows the user that it was entered)
   } // Ends the check to see if the value entered on the form is empty
}

function removeFromList(listField) {
   if ( listField.length == -1) {  // If the list is empty
      alert("There are no values which can be removed!");
   } else {
      var selected = listField.selectedIndex;
      if (selected == -1) {
         alert("You must select an entry to be removed!");
      } else {  // Build arrays with the text and values to remain
         var replaceTextArray = new Array(listField.length-1);
         var replaceValueArray = new Array(listField.length-1);
         for (var i = 0; i < listField.length; i++) {
            // Put everything except the selected one into the array
            if ( i < selected) { replaceTextArray[i] = listField.options[i].text; }
            if ( i > selected ) { replaceTextArray[i-1] = listField.options[i].text; }
            if ( i < selected) { replaceValueArray[i] = listField.options[i].value; }
            if ( i > selected ) { replaceValueArray[i-1] = listField.options[i].value; }
         }
         listField.length = replaceTextArray.length;  // Shorten the input list
         for (i = 0; i < replaceTextArray.length; i++) { // Put the array back into the list
            listField.options[i].value = replaceValueArray[i];
            listField.options[i].text = replaceTextArray[i];
         }
      } // Ends the check to make sure something was selected
   } // Ends the check for there being none in the list
}

/* String Functions */
function trimAll(sString){
	
	while (sString.substring(0,1) == ' ')
	{
		sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == ' ')
	{
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
} 

function Right(str, n)
        /***
                IN: str - the string we are RIGHTing
                    n - the number of characters we want to return

                RETVAL: n characters from the right side of the string
        ***/
        {
                if (n <= 0)     // Invalid bound, return blank string
                   return "";
                else if (n > String(str).length)   // Invalid bound, return
                   return str;                     // entire string
                else { // Valid bound, return appropriate substring
                   var iLen = String(str).length;
                   return String(str).substring(iLen, iLen - n);
                }
        }

function isEmpty(inputVal) {
	if (inputVal == null || inputVal=="") {
		return true
	} else { 
		return false
	};
}

/* Return the current date (mm/dd/yyyy) */
function curDate(){
		var currentDate = new Date()
		var day = currentDate.getDate()
		var month = currentDate.getMonth()
		var year = currentDate.getFullYear()
		return month + "/" + day + "/" + year 
}
			
/* Return the current time  (hh:mm am/pm) */
function curTime(){
		var currentTime = new Date()
		var hours = currentTime.getHours()
		var minutes = currentTime.getMinutes()
		var suffix = "AM";
		if (hours >= 12) {
			suffix = "PM";
			hours = hours - 12;
			}
		if (hours == 0) {
			hours = 12;
			}
		if (minutes < 10)
			minutes = "0" + minutes
		return hours + ":" + minutes + " " + suffix 
}

//****************************************************************//
// FUNCTION: isDate (dateStr)                                     //
//                                                                //
// This function takes a string variable and verifies if it is a  //
// valid date or not. Dates must be in the format of mm-dd-yyyy   //
// or mm/dd/yyyy. It checks to make sure the month has the proper //
// number of days, based on the month. The function returns true  //
// if a valid date, false if not.                                 //
//                                                                //
// Day/Month must be 1 or 2 digits, Year must be 2 or 4 digits.   //
//****************************************************************//
function isDate(dateStr) {

  var datePattern = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{2}|\d{4})$/;
  var matchArray = dateStr.match(datePattern);

  // Empty strings are allowed
  if (dateStr == "") { return true; }
  
  //Check valid format
  if (matchArray == null) { return false; }

  month = matchArray[1];
  day   = matchArray[3];
  year  = matchArray[5];

  // check month range
  if (month < 1 || month > 12) { return false; }

  //Check day range
  if (day < 1 || day > 31) { return false; }

  //Check months with 30 days
  if ((month==4 || month==6 || month==9 || month==11) && day>30) { return false; }

  //Check Feb days
  if (month == 2) {
    var leapYr = (year%4 == 0 && (year%100 != 0 || year%400 == 0));
    if (day > 29 || (day>28 && !leapYr)) { return false; }
  }

  return true;
}


// CMR 20793 redirect the page if the position is not longer online
function isOffline(JOAStatus) {
	if (JOAStatus == 'Offline') {
			window.location = "./frmJOAOffline?ReadForm"
	}
}
