function isBlank(value)
{
	return (value == null || value == "");
}

function validate_required(field, alerttxt)
{
	with (field)
	{
		if (isBlank(value))
		  {alert(alerttxt); return false;}
		else {return true}
	}
}

function valButton(btn, alerttxt) {
    var cnt = -1;
    for (var i=btn.length-1; i > -1; i--) {
        if (btn[i].checked) {cnt = i; i = -1;}
    }
    if (cnt > -1) return btn[cnt].value;
    else 
      {alert(alerttxt); return null;}
}

function checkYear(fieldYear, highestAge, lowestAge)
{
	var validChars = "0123456789"
	var allValid = true;
	var validGroups = true;
	var decPoints = 0;
	var allNum = "";
	var yearStr = fieldYear.value;
	var currentDate = new Date();
	var currentYear = currentDate.getFullYear();
	var BeginYear = currentYear - highestAge;
	var EndYear = currentYear - lowestAge;
	var returnValue = true;

	for (i = 0;  i < yearStr.length;  i++)
	{
	ch = yearStr.charAt(i);
	for (j = 0;  j < validChars.length;  j++)
		if (ch == validChars.charAt(j))
			break;
	if (j == validChars.length)
	{
		allValid = false;
		break;
	}
	if (ch != ",")
		allNum += ch;
	}
	if (!allValid)
	{
		alert("Please enter only digit characters in the Year field.");
		returnValue = false;
	}
	else
	{
		var chkVal = allNum;
		var prsVal = parseInt(allNum);
		if (chkVal != "" && !(prsVal >= BeginYear && prsVal <= EndYear))
		{
			alert("Please enter a year between " + BeginYear + " and " + EndYear + " in the Year field.");
			returnValue = false;
		}
	}
		
	return returnValue;
}

function daysInFebruary(year){
	var feb = 28;
	if (year % 4 == 0) {feb = 29;}
	return feb;
}

function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function check_date(month, day, year)
{
	var daysInMonth = DaysArray(12)
	if (day > daysInMonth[month]) {alert('Please enter a valid date'); return false;}
	if (month == 2 && day > daysInFebruary(year)) {alert('Not a leap year. Please enter a valid date'); return false;}
	else {return true;}
}

function validate_email(field,alerttxt)
{
	with (field)
	{
		apos=value.indexOf("@");
		dotpos=value.lastIndexOf(".");
		if (!(isBlank(value)) && (apos < 1 || dotpos - apos < 2)) 
		  {alert(alerttxt); return false;}
		else {return true;}
	}
}

function check_tel(tel1, tel2, tel3)
{
	if (isBlank(tel1) && isBlank(tel2) && isBlank(tel3)) {alert('Please enter a contact telephone number.'); return false;}
	else {return true;}
}

function isPhoneNumber(s) 
{
	var validChars = "0123456789(). +-_";
	var allValid = true;
	var allNum = "";
	var returnValue = true;
	
	for (i = 0;  i < s.length;  i++)
	{
		ch = s.charAt(i);
		for (j = 0;  j < validChars.length;  j++)
			if (ch == validChars.charAt(j))
				break;
		if (j == validChars.length)
		{
			allValid = false;
			break;
		}
		if (ch != ",")
			allNum += ch;
	}
	if (!allValid)
	{
		alert("Please enter valid characters in the telephone fields: " + validChars);
		returnValue = false;
	}
	return returnValue;
}

function isCurrency(field) 
{
	var validChars = "0123456789.";
	var allValid = true;
	var allNum = "";
	var returnValue = true;
	var s = field.value;
	var f = 0;
	
	if (s.charAt(0) == "$")
	{
		s = s.substr(1);
		field.value = s;
	}
	
	for (i = 0;  i < s.length;  i++)
	{
		ch = s.charAt(i);
		for (j = 0;  j < validChars.length;  j++)
			if (ch == validChars.charAt(j))
				break;
		if (j == validChars.length)
		{
			allValid = false;
			break;
		}
		if (ch != ",")
			allNum += ch;
	}
	if (!allValid)
	{
		alert("Please enter valid a valid number");
		returnValue = false;
	}
	else if (!isBlank(s))
	{
		f = parseFloat(s);
		field.value = f.toFixed(2);
	}
	
	return returnValue;
}

function validate_checkbox(field, alerttxt)
{
	if (field.checked == false)
	  {alert(alerttxt); return false;}
	else {return true}
}