function ValidateCalc()
{
	if (fieldIsEmpty( document.forms[0].elements["tbPremium"], "Premium" ) )
	{
		return false
	}
	if (fieldNotMoneyFormat( document.forms[0].elements["tbPremium"], "Premium" ) )
	{
		return false
	}
	var temp = document.forms[0].elements["tbPremium"].value;
	document.forms[0].elements["tbPremium"].value = stripPhrase(stripPhrase(document.forms[0].elements["tbPremium"].value,"$"),",");
	if ( fieldIsNotInt( document.forms[0].elements["tbPremium"], "Premium" ) )
	{
		document.forms[0].elements["tbPremium"].value = temp;
		return false
	}
	document.forms[0].elements["tbPremium"].value = temp;
	if (fieldIsEmpty( document.forms[0].elements["tbMemberYears"], "Years in Ohio Plan" ) )
	{
		return false
	}
	if (fieldIsNotInt( document.forms[0].elements["tbMemberYears"], "Years in Ohio Plan" ) )
	{
		return false
	}
	if (fieldIsNaN( document.forms[0].elements["tbMemberYears"], "Years in Ohio Plan" ) )
	{
		return false
	}
	if (fieldIsEmpty( document.forms[0].elements["tbLossRatio"], "Loss Ratio" ) )
	{
		return false
	}
	if (fieldNotValidPercent( document.forms[0].elements["tbLossRatio"], "Loss Ratio" ) )
	{
		return false
	}
	temp = document.forms[0].elements["tbLossRatio"].value;
	document.forms[0].elements["tbLossRatio"].value = stripPhrase(document.forms[0].elements["tbLossRatio"].value,"%");
	if ( fieldIsNotInt( document.forms[0].elements["tbLossRatio"], "Loss Ratio" ) )
	{
		document.forms[0].elements["tbLossRatio"].value = temp;
		return false
	}
	document.forms[0].elements["tbLossRatio"].value = temp;
	
	if ( document.forms[0].elements["ddRMStatus"].value == -1 )
	{
		alert('The "Risk Management Status" field must be selected.');
		document.forms[0].elements["ddRMStatus"].focus();
		return false
	}
	return true
}
function fieldIsNotInt( theField, fieldLabel )
{
  var result = false;
	if ( theField.value != parseInt(theField.value) )
	{
		alert('The "' + fieldLabel +'" field expects a whole number.');
		theField.focus();
		result = true;
	}
	return result;
}
function fieldIsEmpty( theField, fieldLabel )
{
    var result = false;

	if (theField.value == "")
	{
		alert('The "' + fieldLabel +'" field may not be left blank.');
		theField.focus();
		result = true;
	}
	return result;
}

function fieldNotValidDate( theField, fieldLabel )
{
  var result = false;
  var month = GetSepSubStr( theField.value, "/", 0 )
	var day = GetSepSubStr( theField.value, "/", 1 )
	var year = GetSepSubStr( theField.value, "/", 2 )
	if ( !isValidDate(month, day, year) )
	{
      alert('The "' + fieldLabel +'" is not a valid date.  Format should be MM/DD/YYYY.')
			theField.focus()
			result = true
    }
	return result
}

function fieldExceedsLengthLimit( theField, length, fieldLabel )
{
  var result = false;
  if ( null != theField )
	{
		if ( theField.value.length > length )
		{
		  alert('The "' + fieldLabel +'" is too long.  Please enter a value of length ' + length + ' or less.')
			theField.focus()
			result = true
		}
	}
	return result
}

function multiSelectFieldExceedsSelectLimit(theField, limit, fieldLabel)
{
	var option_count = 0;
	for (var i = 0; i < theField.length; i++)
	{
		if (theField.options[i].selected)
		{
			option_count++;
		}
	}
	if (option_count > limit)
	{
		alert('There are too many options selected in the "' + fieldLabel +'".  Please select no more than ' + limit + '.')
		return true;
	}
	else
	{
		return false;
	}
}


function fieldIsNaN ( theField, fieldLabel )
{
    var result = false;
    if ( isNaN(theField.value) )
    {
      alert('The "' + fieldLabel +'" is not a valid numerical value.')
			theField.focus()
			result = true
    }
	return result
}

function fieldNotMoneyFormat ( theField, fieldLabel )
{
    var result = false;
    if ( isNaN(stripPhrase(stripPhrase(theField.value,"$"),",") ) )
    {
      alert('The "' + fieldLabel +'" is not understandable.  Money fields should be formatted "12,345.67"')
			theField.focus()
			result = true
    }
	return result
}

function fieldNotValidPercent ( theField, fieldLabel )
{
    var result = false;
    if ( isNaN(stripPhrase(theField.value,"%") ) )
    {
      alert('The "' + fieldLabel +'" needs a percentage.')
			theField.focus()
			result = true
    }
	return result
}

//takes commas out of money values, so we can do a rough test for the money fields being numbers.  
//could still have false negatives (such as if they enter a "$" themselves...)
function stripPhrase( src, phrase )
{
	var retValue = src
	while(-1 < retValue.indexOf(phrase))
	{
		var pos = retValue.indexOf(phrase);
		retValue = retValue.substring(0,pos) + retValue.substring(pos+phrase.length,retValue.length);
	}
	return retValue
}



function GetSepSubStr( src, delim, index )
{
	var i = 0;
	while( i <= index )
	{
		var delimPos = src.indexOf(delim)
		if ( index == i )
		{
			if ( -1 < delimPos )
			{
				return src.substring(0,delimPos)
			}
			else
			{
				return src
			}
		}
		src = src.substring(delimPos+delim.length,src.length)
		i++
		//alert( "src revised: " + src  + " index: " + index + " i:" + i)
	}
	return ""
}

function isValidDate( month , day, year )
{
  if ( isNaN(month) )
  {
      alert( 'The value given for Month, must be a valid number.');
      return false;
  }
  if ( isNaN(day) )
  {
      alert( 'The value given for Day in Month, must be a valid number.');
      return false;
  }
  if ( isNaN(year) )
  {
      alert( 'The value given for Year, must be a valid number.');
      return false;
  }

  if ( month < 1 || month > 12 )
  {
      alert( 'The value given for Month, must be between 1 and 12.');
      return false;
  }
  
  //based on month shoudl set max days allowed a month, dependent on month and if lesap year
  var daysInMonthArray = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
  
  if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
      daysInMonthArray[1] = "29"; //leap year
      
  var maxDaysAllowed = daysInMonthArray[month-1];
  
  delete daysInMonthArray;

  if ( day < 1 )
  {
      alert('The value given for Day in Month, must be between at least 1.');
      return false;
  }
  
  if ( day > maxDaysAllowed )
  {
      alert('The value given for Day in Month, must be no more than ' + maxDaysAllowed + ', according to the given month and year.');
      //alert('max allowed' +  maxDaysAllowed);
      return false;
  }
  //alert ( month + '-' + day + '-' + year );

	return true;
}