
function isDecimal(parm) {
	if (parm.indexOf(".") == -1) {return false;}
	else {
		// found decimal point
		var prefix = parm.substring(0, parm.indexOf("."));
		var suffix = parm.substring(parm.indexOf(".") + 1);
		if (!isNumeric(prefix, false) || !isNumeric(suffix, false) ) {
			return false;
		}
		else return true;
	}
} 

function oneOnly(parm, chr, must) {
	var atPos = parm.indexOf(chr, 0);
	if (atPos == -1) {return !must;}
	if (parm.indexOf(chr, atPos + 1) > - 1) {return false;}
	return true;
}

// Check that a string contains only numbers
function isNumeric(string, ignoreWhiteSpace) {
   if (string.search) {
      if ((ignoreWhiteSpace && string.search(/[^\d\s]/) != -1) || (!ignoreWhiteSpace && string.search(/\D/) != -1)) return false;
   }
   return true;
}

// Remove leading and trailing whitespace from a string
function trimWhitespace(string) {
   var newString  = '';
   var substring  = '';
   beginningFound = false;
   
   // copy characters over to a new string
   // retain whitespace characters if they are between other characters
   for (var i = 0; i < string.length; i++) {
      
      // copy non-whitespace characters
      if (string.charAt(i) != ' ' && string.charCodeAt(i) != 9) {
         
         // if the temporary string contains some whitespace characters, copy them first
         if (substring != '') {
            newString += substring;
            substring = '';
         }
         newString += string.charAt(i);
         if (beginningFound == false) beginningFound = true;
      }
      
      // hold whitespace characters in a temporary string if they follow a non-whitespace character
      else if (beginningFound == true) substring += string.charAt(i);
   }
   return newString;
}

function getNumberOfSelectedOptions(theSelectFieldName) {
	var NumberOfSelectedOptions = 0;
	for (elementIndex = 0; elementIndex < theSelectFieldName.options.length; elementIndex++) {
		if (theSelectFieldName.options[elementIndex].selected) {
			NumberOfSelectedOptions++;
		}
	}
	return NumberOfSelectedOptions;
}

function getSelectedDropdown(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].selected) {
            retArr.length = lastElement;
            retArr[lastElement] = i;
            lastElement++;
         }
      }
   } else { // There is only one check box (it's not an array)
      if (buttonGroup.selected) { // if the one check box is checked
         retArr.length = lastElement;
         retArr[lastElement] = 0; // return zero as the only array value
      }
   }
   return retArr;
} // Ends the "getSelectedDropdown" function

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 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

// Check that an email address is valid based on RFC 821 (?)
function isValidEmail(address) {
   if (address != '' && address.search) {
      if (address.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1) return true;
      else return false;
   }
   
   // allow empty strings to return true - screen these with either a 'required' test or a 'length' test
   else return true;
}

// Check that an email address has the form something@something.something
// This is a stricter standard than RFC 821 (?) which allows addresses like postmaster@localhost
function isValidEmailStrict(address) {
   if (isValidEmail(address) == false) return false;
   var domain = address.substring(address.indexOf('@') + 1);
   if (domain.indexOf('.') == -1) return false;
   if (domain.indexOf('.') == 0 || domain.indexOf('.') == domain.length - 1) return false;
   return true;
}

var photoCards = new Array();
	photoCards[0] = '<img src="../Images/photocard-001.jpg" width="679" height="283" alt="Get Started" />';
	photoCards[1] = '<img src="../Images/photocard-002.jpg" width="679" height="283" alt="Get Started" />';
	photoCards[2] = '<img src="../Images/photocard-003.jpg" width="679" height="283" alt="Get Started" />';
	photoCards[3] = '<img src="../Images/photocard-004.jpg" width="679" height="283" alt="Get Started" />';

function rotateElement(element, aRotationSrc, interval, cookieId, cookieIntervalStart) {
	var now = new Date().getTime();
	// ditch if object doesn't exist
	var oObj; if( (oObj = document.getElementById(element)) == null) return false;
	var expires = now + 60*60*24*1000;
	var rNum, sSeries;
	
	if(getCookie(cookieId)) {
		// get the display interval start for this particular element
		// and the last series in the rotation to be displayed
		var currentSeries = getCookie(cookieId);
		var intervalStart = getCookie(cookieIntervalStart);
		
		// interval has expired - get a fresh replacement in the series
		if(new Number(intervalStart) + interval < now) {
			
			// remove the series last displayed from the source array
			aRotationSrc.splice(currentSeries, 1);
			
			// create a new temp array to restore indexing after splice
			tmpSrc = new Array();
			for(i in aRotationSrc) tmpSrc.push(aRotationSrc[i]);

			// random number in new tips.length range
			rNum = getRandomNum(0, aRotationSrc.length);

			// get the new series from the tmp array
			sSeries = tmpSrc[rNum];				
			
			//alert("EXPIRED:\n rNum: " + rNum + "\n" + "sSeries: " + sSeries + "\n" + "intervalStart: " + intervalStart + "\n" + "interval: " + interval + "\n" + "now: " + now);
			//alert(element + " " + rNum + " " + aRotationSrc.length );

			// record new series as active + set session start time
			setCookie(cookieId, rNum);
			setCookie(cookieIntervalStart, now);
			
		} else {
			sSeries = aRotationSrc[currentSeries];
			//alert("has not expired: " + sSeries + intervalStart + " " + interval + " " + now);
		}
	// first time visit - no cookie set
	} else {
		
		rNum = getRandomNum(0, aRotationSrc.length);
		sSeries = aRotationSrc[rNum];

		// record new currently active tip + time session starts
		setCookie(cookieId, rNum);
		setCookie(cookieIntervalStart, now);
	}
	document.write(sSeries);
}

function getInterval(hours) {
	return 60*60*hours*1000;
}

function rotationInit() {
	rotateElement('TODContainer', tips, getInterval(4), 'TIP_OF_DAY', 'TIP_OF_DAY_STARTTIME');
}

// Write a cookie value
function setCookie(name, value, expires, path, domain, secure) {
   path = "/";
   var curCookie = name + '=' + escape(value) + ((expires) ? '; expires=' + expires.toGMTString() : '') + ((path) ? '; path=' + path : '') + ((domain) ? '; domain=' + domain : '') + ((secure) ? '; secure' : '');
   document.cookie = curCookie;
}

// Retrieve a named cookie value
function getCookie(name) {
   var dc = document.cookie;

   // find beginning of cookie value in document.cookie
   var prefix = name + "=";
   var begin = dc.indexOf("; " + prefix);
   if (begin == -1) {
      begin = dc.indexOf(prefix);
      if (begin != 0) return null;
   }
   else begin += 2;

   // find end of cookie value
   var end = document.cookie.indexOf(";", begin);
   if (end == -1) end = dc.length;

   // return cookie value
   return unescape(dc.substring(begin + prefix.length, end));
}


// Delete a named cookie value
function deleteCookie(name, path, domain) {
   var value = getCookie(name);
   if (value != null) document.cookie = name + '=' + ((path) ? '; path=' + path : '') + ((domain) ? '; domain=' + domain : '') + '; expires=Thu, 01-Jan-70 00:00:01 GMT';
   return value;
}

function getRandomNum(min, max) {
	return Math.floor(Math.random() * (max - min) + min);
}

