/************************************************************************
FUNCTION: changeStyle()
*************************************************************************/
function changeStyle(strTD, strColor){
	var objTD = document.getElementById(strTD);

	objTD.style.backgroundColor = strColor;
}

/************************************************************************
FUNCTION: returnDate()
*************************************************************************/
function returnDate(){
	var dt = new Date();
	var arrDays = new Array(8);
	var arrMonths = new Array(13);
	var strDate = '';

	arrDays[0] = "Sunday";
	arrDays[1] = "Monday";
	arrDays[2] = "Tuesday";
	arrDays[3] = "Wednesday";
	arrDays[4] = "Thursday";
	arrDays[5] = "Friday";
	arrDays[6] = "Saturday";

	arrMonths[0] = "January";
	arrMonths[1] = "February";
	arrMonths[2] = "March";
	arrMonths[3] = "April";
	arrMonths[4] = "May";
	arrMonths[5] = "June";
	arrMonths[6] = "July";
	arrMonths[7] = "August";
	arrMonths[8] = "September";
	arrMonths[9] = "October";
	arrMonths[10] = "November";
	arrMonths[11] = "December";

// with day of week
//	strDate = arrDays[dt.getDay()] + ', ' + arrMonths[dt.getMonth()] + ' ' + dt.getDate() + ', ' + dt.getFullYear();

// without
	strDate = arrMonths[dt.getMonth()] + ' ' + dt.getDate() + ', ' + dt.getFullYear();

	return strDate;
}
/************************************************************************
FUNCTION: giveFocus()
*************************************************************************/
function giveFocus(objElem){
	objElem.focus();
}

/************************************************************************
FUNCTION: isBlank()

Pass this function a string and it will determine whether or not
there is text or only whitespace.
*************************************************************************/
function isBlank(str){

	for (i=0;i<str.length;i++){
		if (str.charAt(i) != " " || str.charAt(i) != "\n" || str.charAt != "\t" || str.charAt != null){
			return false;
		}
	}
	return true;
}

/*************************************************************************
FUNCTION: popUpWindow()
**************************************************************************/
function popUpWindow(strURL, strName, strFeatures){
      return window.open(strURL, strName, strFeatures);
}

/************************************************************************************
FUNCTION: doSubmit_General()

	This function will check to make sure that all of the questions
	on the survey page are answered and that, for checkbox elements,
	the maximum number of responses is not exceeded.
************************************************************************************/
function doSubmit_General(intCheckboxMax){
	var boolIsErr = false;				//Flag for error.
	var strErr = '';					//Custom error message.
	var objForm = document.forms[0];	//Form object.
	var strElementName = '';			//The name of the current element.
	var intCheckboxCount = 0;			//Number of checkboxes checked.
	var arrChecked = new Array();		//Array to hold checked statuses for each radio/checkbox.
	var boolDoCheckbox =  false;		//Flag to tell whether a checkbox or radio button exists on the page.
	var j = -1;

	/***********************************************************************
	Since the form elements are produced dynamically, we don't know
	the names, number, and types of elements that will be displayed.
	Thus, we have to loop over all of the elements on the form and
	perform different checks for each type of element.
	***********************************************************************/
	for (i=0;i<objForm.elements.length;i++){
		/***********************************************************************
		If we are on a new group of form elements, we need to reset
		the name of the form element as well as the string to be evaluated
		and the checkbox counter.
		***********************************************************************/
		if (strElementName != objForm.elements[i].name){
			strElementName = objForm.elements[i].name;
			intCheckboxCount = 0;
			strToEval = '';
			
			/***********************************************************************
			If the current element is a radio button or checkbox, add an item to the
			array that keeps track of whether or not at least one answer for the
			question has been checked, and set it to false. Also, increment the index.
			***********************************************************************/
			if (objForm.elements[i].type == 'checkbox' || objForm.elements[i].type == 'radio'){
				boolDoCheckbox = true;
				j = j+1;
				arrChecked[j] = false;
			}
		}

		/***********************************************************************
		If the current form element is a text box, all we need to do is ensure
		that it has data in it. These are for open-ended questions.
		***********************************************************************/
		if ((objForm.elements[i].type == 'text' || objForm.elements[i].type == 'textarea' || objForm.elements[i].type == 'password') && objForm.elements[i].value == ''){
			boolIsErr = true;
			break;
		/***********************************************************************
		If the current form element is a single-select box, make sure that the
		selected value is not equal to -1.
		***********************************************************************/
		}else if (objForm.elements[i].type == 'select-one'){
			if (objForm.elements[i].options[objForm.elements[i].selectedIndex].value == -1){
				boolIsErr = true;
				break;
			}
		/***********************************************************************
		If the current form element is a multi-select box, make sure that the
		selected value is not equal to -1 and that at least one option is
		selected.
		***********************************************************************/
		}else if (objForm.elements[i].type == 'select-multiple'){
			if (objForm.elements[i].options[objForm.elements[i].selectedIndex].value == -1 || isBlank(objForm.elements[i].options[objForm.elements[i].selectedIndex].value)){
				boolIsErr = true;
				break;
			}
		/***********************************************************************
		If the current form element is a radio button or checkbox...
		***********************************************************************/
		}else if (objForm.elements[i].type == 'radio' || objForm.elements[i].type == 'checkbox'){
			/***********************************************************************
			If the current checkbox/radio button group does not have a checked item,
			check to see if the current box/button is checked. If it is, change the
			flag for this group of boxes/buttons.
			***********************************************************************/
			if (!arrChecked[j]){
				if (objForm.elements[i].checked == true){
					arrChecked[j] = true;
				}
			}
		}
		
		/***********************************************************************
		If the current element is a checkbox, get the value of the corresponding
		hidden field that indicates the maximum number of items that can be
		checked. This DOM reference to this hidden field is stored as a string
		variable and then evaluated using the 'eval' function to store the value
		in a variable.
		***********************************************************************/
		if (objForm.elements[i].type == 'checkbox'){
			/***********************************************************************
			The the max number of allowed items to be checked is less than or equal
			to zero, there is no limit. No more processing needs to occur. If the
			number is greater than zero...
			***********************************************************************/
			if (intCheckboxMax > 0){
				/***********************************************************************
				If the current checkbox is checked, add one to the counter.
				***********************************************************************/
				if (objForm.elements[i].checked){
					intCheckboxCount = intCheckboxCount + 1;
				}

				/***********************************************************************
				If the checkbox is-checked count is greater than the max allowed, raise
				a specific error.
				***********************************************************************/
				if (intCheckboxCount > intCheckboxMax){
					boolIsErr = true;
					strErr += 'There are too many answers selected for one of the questions on this page.';
					break;
				}
			}
		}
	}

	/***********************************************************************
	Loop over the checkbox/radio button array to make sure that each of the
	items in the array is true. If any are false, this indicates that one of
	the questions on the page (groups of boxes/buttons) was not answered, so
	we raise an error.
	***********************************************************************/
	if (boolDoCheckbox){
		for (i=0;i<arrChecked.length;i++){
			if (!arrChecked[i]){
				boolIsErr = true;
				break;
			}
		}
	}

	/***********************************************************************
	If the error flag is set to true, check to see if there is a specific
	error message to display. If there is, display that message; otherwise
	display a generic message.
	***********************************************************************/
	if (boolIsErr){
		if (isBlank(strErr)){
			alert('You must fill out all the fields on this page.\n');
		}else{
			alert(strErr);
		}
		return false;
	}else{
		return true;
	}
}

/****************************************************************************************************
FUNCTION:	padNumberWithZeroes()

		This function will accept one string and one number as parameters. The string value
		will be the base of the return value; the number will indicate how long the string
		should be. If the length of the string is less than the value of the number, zeroes
		will be placed in front of the string to the specified length.
****************************************************************************************************/
function padNumberWithZeroes(strNumber, intPlaces){
	var strReturnValue = '';

	strNumber.toString();

	if (intPlaces <= strNumber.length){
		return strNumber;
	}else{
		for (i=0;i<intPlaces-strNumber.length;i++){
			strReturnValue = '0' + strNumber;
		}

		return strReturnValue;
	}
}

/************************************************************************
FUNCTIONS: start, stop, expires (timer script)
*************************************************************************/
var timerID = null;
var timerOn = false;
var TipTimeout = 1200;  // 20 minutes 
var startTime = new Date();
var millisecExp = startTime.getTime() + 1000 * TipTimeout;
//
function stop() {
   if (timerOn) {
      clearTimeout(timerID);
      timerOn = false;
   }
}
function start() {
   stop();
   expires();
}
function expires() {
   var now = new Date();
   var millisecNow = now.getTime();
   var millisecLeft = millisecExp - millisecNow;
   if (millisecLeft <= 0) {
      timerOn = false;
      window.status = "Your session has expired.";
      // document.clock.countdown.value = "*** Expired ***";
      return;
   }
   minLeft = Math.floor(millisecLeft/60000);
   secLeft = Math.floor(millisecLeft/1000) - minLeft*60;
   timeLeft = minLeft + " minute";
   if (minLeft != 1) {
      timeLeft += "s ";
   } else {
      timeLeft += " ";
   }
   timeLeft += secLeft + " second";
   if (secLeft != 1) {
      timeLeft += "s";
   }
   // document.clock.countdown.value = timeLeft;
   window.status = "Your session will expire in " + timeLeft;
   timerID = setTimeout("expires()",1000);
   timerOn = true;
}

/************************************************************************
FUNCTION: windowOpen()
*************************************************************************/
function windowOpen(window_url) {
	firsthalf = 'toolbar=yes,status=no,scrollbars=yes,menubar=yes,resizable=yes'
	secondhalf = ',directories=no,location=no,width=550,height=400'
	whole = firsthalf + secondhalf
	helpWin = window.open(window_url,'',whole);

	if (document.images) { 
		if (helpWin) helpWin.focus()
	}
}

function targetNew(strURL){
	window.open(strURL);
}

/************************************************************************
FUNCTION: togglePrint()
          Sets display of left pane (with navigation options) on or off
          Also sets display of unneeded text (e.g. info paragraphs) if
          present
*************************************************************************/
function togglePrint() {
   var leftPane = document.getElementById('leftpane');
   var printText = document.getElementById('print');
   var infoTable = document.getElementById('infoTable');
   if (printText.firstChild.data == '[Print version]') {
      leftPane.style.display = 'none';
      printText.firstChild.data = '[Screen version]';
      if (infoTable != null) {
         infoTable.style.display = 'none';
      }
   } else {
      leftPane.style.display = '';
      printText.firstChild.data = '[Print version]';
      if (infoTable != null) {
         infoTable.style.display = '';
      }
   }
}
