function indexOfValueInSelectList(value_to_find, select_list)
{
	for (x = 0; x < select_list.length; x++)
	{
		if (select_list[x].value == value_to_find)
			return x;
	}
	
	return -1;
}

function zeroOrIndexOfValueInSelectList(value_to_find, select_list)
{
	index_value = indexOfValueInSelectList(value_to_find, select_list);
	
	if (index_value < 0)
		return 0;
	else
		return index_value;
}

function indexOfCheckedRadioItem(radio_list)
{
	for (x = 0; x < radio_list.length; x++)
	{
		if (radio_list[x].checked)
			return x;
	}
	
	return -1;
}

function setRadioButtonByValue(radio, value_of_radio)
{
	if (radio == null)
		return;
	for (x = 0; x < radio.length; x++)
	{
		if (radio[x].value == value_of_radio)
		{
			radio[x].checked = true;
			return;
		}
	}
}

function valueOfCheckedRadioButton(radio)
{
	for (x = 0; x < radio.length; x++)
	{
		if (radio[x].checked)
			return radio[x].value;
	}
	
	return "";
}

function isValidNumber(number_string)
{
	if (number_string == "" || isNaN(number_string) || number_string.indexOf(" ") >= 0)
		return false;
	else
		return true;
}

function isDate(month, day, year)
{
	if (isNaN(month) || isNaN(day) || isNaN(year))
	{
		return false;
	}
	else
	{
		if (day.length == 1)
			day = '0' + day;

		if (month.length == 1)
			month = '0' + month;

		if (year.length == 2)
		{
			if (year > 49)
				year = '19' + year;
			else
				year = '20' + year;
		}

		if (day.length != 2 || month.length != 2 || year.length != 4)
			return false;

		//check the values
		if (isNaN(day) || day < 0 || day.indexOf(',') != -1 || day.indexOf('.') != -1 || day.indexOf('-') != -1)
			return false;

		if (isNaN(month) || month < 0 || month.indexOf(',') != -1 || month.indexOf('.') != -1 || month.indexOf('-') != -1)
			return false;

		if (isNaN(year) || year < 0 || year.indexOf(',') != -1 || year.indexOf('.') != -1 || year.indexOf('-') != -1)
			return false;

		if (month < 1 || month > 12)
			return false;

		if (day < 1 || day > 31)
			return false;

		//check months with 30 days
		if (month == 4 || month == 6 || month == 9 || month == 11) {
			if (day == 31)
				return false;
		}

		// Check for February and leap year.
		if (month == 2)
		{
			//February can't have a date > 29 at any point.
			if (day > 29)
				return false;

			//If the day is set to the 29th then check for a leap year.
			if (day == 29 && ((year / 4)!= parseInt(year / 4)))
				return false;
		}
	}

	return true;
}

function isFutureDate(current_year, current_month, current_day, check_year, check_month, check_day)
{
	//current_year, current_month, current_day - values for today's date.
	//check_year, check_month, check_day - values for the date we are comparing.
	
	if (check_year > current_year)
	{
		//The year is in the future.
		return true;
	}
	else if (check_year == current_year)
	{
		//It is the current year, check for months.
		if (check_month > current_month)
		{
			//The month is in the future.
			return true;
		}
		else if (check_month == current_month)
		{
			//It is the current month of the current_year.
			if (check_day > current_day)
			{
				//The day is in the future.
				return true;
			}
		}
	}

	return false;
}

/*
This function was retrieved from the following url: http://javascript.internet.com/forms/email-address-validation.html
*/
function isValidEmailAddress(emailStr) {
	var checkTLD = 1;
	var knownDomsPat = /^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
	var emailPat = /^(.+)@(.+)$/;
	var specialChars = "\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
	var validChars = "\[^\\s" + specialChars + "\]";
	var quotedUser = "(\"[^\"]*\")";
	var ipDomainPat = /^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom = validChars + '+';
	var word = "(" + atom + "|" + quotedUser + ")";
	var userPat = new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat = new RegExp("^" + atom + "(\\." + atom + ")*$");

	var matchArray = emailStr.match(emailPat);

	if (matchArray == null)
		return false;

	var user = matchArray[1];
	var domain = matchArray[2];

	for (i = 0; i < user.length; i++)
	{
		if (user.charCodeAt(i) > 127)
			return false;
	}

	for (i = 0; i < domain.length; i++)
	{
		if (domain.charCodeAt(i) > 127)
			return false;
	}

	if (user.match(userPat) == null)
		return false;

	var IPArray = domain.match(ipDomainPat);

	if (IPArray != null)
	{
		for (var i = 1; i <= 4; i++)
		{
			if (IPArray[i] > 255)
				return false;
		}
		
		return true;
	}

	var atomPat = new RegExp("^" + atom + "$");
	var domArr = domain.split(".");
	var len = domArr.length;
	
	for (i = 0; i < len; i++)
	{
		if (domArr[i].search(atomPat) == -1)
			return false;
	}

	if (checkTLD && domArr[domArr.length - 1].length != 2 && domArr[domArr.length - 1].search(knownDomsPat) == -1)
		return false;

	if (len < 2)
		return false;

	return true;
}

/*
The Trim() functions were retrieved from the following url: http://www.vermontsoftware.com/Javascript/trim.html
*/
function LTrim(str)
{
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(0)) != -1) {
      // We have a string with leading blank(s)...

      var j=0, i = s.length;

      // Iterate from the far left of string until we
      // don't have any more whitespace...
      while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
         j++;

      // Get the substring from the first non-whitespace
      // character to the end of the string...
      s = s.substring(j, i);
   }
   return s;
}

function RTrim(str)
{
   // We don't want to trip JUST spaces, but also tabs,
   // line feeds, etc.  Add anything else you want to
   // "trim" here in Whitespace
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
      // We have a string with trailing blank(s)...

      var i = s.length - 1;       // Get length of string

      // Iterate from the far right of string until we
      // don't have any more whitespace...
      while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
         i--;


      // Get the substring from the front of the string to
      // where the last non-whitespace character is...
      s = s.substring(0, i+1);
   }

   return s;
}

function Trim(str)
{
   return RTrim(LTrim(str));
}

function completeUnsizedPopupWindow(url_to_open, window_name)
{
	window_name = window.open(url_to_open, window_name, "TOOLBAR=YES,STATUS=YES,MENUBAR=YES,LOCATION=YES,SCROLLBARS=YES,RESIZABLE=YES");
	window_name.focus();
}

function completeSizedPopupWindow(url_to_open, window_name)
{
	window_name = window.open(url_to_open, window_name, "WIDTH=600,HEIGHT=400,TOOLBAR=YES,STATUS=YES,MENUBAR=YES,LOCATION=YES,SCROLLBARS=YES,RESIZABLE=YES");
	window_name.focus();
}

function basicPopupWindow(url_to_open, window_name)
{
	window_name = window.open(url_to_open, window_name, "WIDTH=600,HEIGHT=400,TOOLBAR=YES,STATUS=YES,MENUBAR=YES,LOCATION=NO,SCROLLBARS=YES,RESIZABLE=YES");
	window_name.focus();
}

function strippedPopupWindow(url_to_open, window_name)
{
	window_name = window.open(url_to_open, window_name, "WIDTH=450,HEIGHT=300,TOOLBAR=NO,STATUS=NO,MENUBAR=NO,LOCATION=NO,SCROLLBARS=YES,RESIZABLE=YES");
	window_name.focus();
}

function popupPopupWindow(term)
{
	strippedPopupWindow("http://www.healthinsurance.com/hi/web/common/pages/popup.aspx?type=popup&term=" + term, "glossary_popup");
}

function glossaryPopupWindow(term)
{
	strippedPopupWindow("http://www.healthinsurance.com/hi/web/common/pages/popup.aspx?type=glossary&term=" + term, "glossary_popup");
}

function Open_Window(url_to_open)
{
	//This function is used for the benefit pdfs that are set up as links.
	window_name = window.open('http://www.ehealthinsurance.com' + url_to_open, 'benefit_pdf_url', "WIDTH=450,HEIGHT=300,TOOLBAR=NO,STATUS=NO,MENUBAR=NO,LOCATION=NO,SCROLLBARS=YES,RESIZABLE=YES");
	window_name.focus();
}

//added by George Partida 12/09/2004
function isEmpty(s) 
{
	var whitespace = " \t\n\r";
	var i;
	if((s == null) || (s.length == 0))
		return true;
	for(i = 0; i < s.length; i++) {
		var c = s.charAt(i);
		if (whitespace.indexOf(c) == -1)
			return false;
	}
	//All characters are whitespace
	return true;
}

function isEmptyOrValidNumber(number_string)
{
	if(isEmpty(number_string) || isValidNumber(number_string))
		return true;
	else
		return false;
}

function isValidPhoneNumber(area_string, prefix_string, suffix_string)
{
	return (isValidNumber(area_string) && area_string.length == 3 && isValidNumber(prefix_string) && prefix_string.length == 3 && isValidNumber(suffix_string) && suffix_string.length == 4)
}

function isValidSSN(lead_string,mid_string,last_string)
{
	return (isValidNumber(lead_string) && lead_string.length == 3 && isValidNumber(mid_string) && mid_string.length == 2 && isValidNumber(last_string) && last_string.length == 4)
}

function isEmptyOrDate(month_value, day_value, year_value)
{
	if(isEmpty(month_value) && isEmpty(day_value) && isEmpty(year_value))
		return true;
	return isDate(month_value,day_value,year_value);
}

function isEmptyOrEmail(email_string)
{
	if(isEmpty(email_string) || isValidEmailAddress(email_string))
		return true;
	else
		return false;
}

function isEmptyOrPhone(area_string, prefix_string, suffix_string)
{
	if((isEmpty(area_string) && isEmpty(prefix_string) && isEmpty(suffix_string)) || isValidPhoneNumber(area_string, prefix_string, suffix_string))
		return true;
	else
		return false;
}

