function checkDueDateYear(duedateyear, dobyear, duedatemonth, dobmonth)
{
	if (duedateyear.value != dobyear.value)
	{
		if (duedatemonth.value != "12" || dobmonth.value != "01")
		{
			if (duedatemonth.value != "01" || dobmonth.value != "12")
			{
				if (duedatemonth.value != "01" || dobmonth.value != "11")
				{
					if (duedatemonth.value != "11" || dobmonth.value != "01")
					{
						if (duedatemonth.value != "02" || dobmonth.value != "12")
						{
							if (duedatemonth.value != "12" || dobmonth.value != "02")
							{
							return confirm('You entered "' + duedateyear.value + '" for the due date but "' + dobyear.value + '" for the birth date.  Please press Ok if this is correct, or Cancel to correct this information.');
							}
						}
					}
				}
			}
		}	
	}
	return true;
}

function isEmailAddr(email)
{
  var result = false;
  var theStr = new String(email);
  var index = theStr.indexOf("@");
  if (index > 0)
  {
    var pindex = theStr.indexOf(".",index);
    if ((pindex > index+1) && (theStr.length > pindex+1))
	{
		var blockemailindex = theStr.indexOf("gawab.com");
		result = true;
		if (blockemailindex > 0)
		result = false;
	}
  }
  return result;
}

function validRequired(formField,fieldLabel)
{
	var result = true;

	if (formField.value == "")
	{
		alert('Your information has not been submitted. Please enter a value for the "' + fieldLabel +'" field.');
		formField.focus();
		result = false;
	}

	if (formField.value == "?")
	{
		alert('Your information has not been submitted. Please enter a value for the "' + fieldLabel +'" field.');
		formField.focus();
		result = false;
	}

	if (formField.value == " ")
	{
		alert('Your information has not been submitted. Please enter a value for the "' + fieldLabel +'" field.');
		formField.focus();
		result = false;
	}

	return result;
}

function allDigits(str)
{
	return inValidCharSet(str,"0123456789");
}

function allNameChars(str)
{
	return inValidCharSet(str.value,"., -abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'`αινσϊδλόφόρ");
}

function checkName(formField,fieldLabel, required)
{
	if (required && !validRequired(formField,fieldLabel))
		return false;

	if (!allNameChars(formField))
	{
		alert('Your information has not been submitted. The name entered in the field "' + fieldLabel + '" does not appear to be correct, it contains a non-letter character');
		formField.focus();
		return false;
	}

	return true;
}

function checkPullDown(formField,fieldLabel)
{
	if (formField.value == "")
	{
		alert('Your information has not been submitted. Please make a selection from the pulldown menu "' + fieldLabel + '"');
		formField.focus();
		return false;
	}
	return true;
}

function checkPremieOrDue(dueDate, premie)
{
	if (premie.value == "")
	{
		if (dueDate.value == "")
		{
			alert('Your information has not been submitted. Please either provide a due date, or specify whether your child was premature or full term. If you do not remember, please just indicate "dont know" in the "Was Child Premature or Full Term?" field');
			premie.focus();
			return false;
		}
	}
	return true;
}

function inValidCharSet(str,charset)
{
	var result = true;

	// Note: doesn't use regular expressions to avoid early Mac browser bugs
	for (var i=0;i<str.length;i++)
		if (charset.indexOf(str.substr(i,1))<0)
		{
			result = false;
			break;
		}

	return result;
}

function stripCharsNotInBag(s)
{   var i;
    var returnString = "";
    var bag = "0123456789";

    // Search through strings characters one by one.
    // If character is in bag, append to returnString.

    for (i = 0; i < s.value.length; i++)
    {
        // Check that current character isnt whitespace.
        var c = s.value.charAt(i);
        if (bag.indexOf(c) != -1)
        	returnString += c;
    }
    return returnString;
}

function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}

function isDigit(c)
{   return ((c >= "0") && (c <= "9"))
}

function isInteger(s)
{   var i;

    if (isEmpty(s))
       return false;

    // Search through strings characters one by one
    // until we find a non-numeric character.
    // When we do, return false; if we dont, return true.

    for (i = 0; i < s.length; i++)
    {
        // Check that current character is number.
        var c = s.charAt(i);

        if (!isDigit(c)) return false;
    }

    // All characters are numbers.
    return true;
}

function isUSPhoneNumber(formField,fieldLabel)
{
    if (formField.value.length > 0)
    {
    stripednum = stripCharsNotInBag(formField);
    result = (isInteger(stripednum) && stripednum.length == 10);

    if(!result)
    {
    	alert('Your information has not been submitted. Please enter a complete ' + fieldLabel+ ' number in the form ###-###-####');
		formField.focus();
		result = false;
    }
    }

  return result;
}

function checkWeight(formField, fieldName)
{
	var result = true;
	
	if (formField.value.length > 0)
	{
		if (!allDigits(formField.value))
		{
		alert('Your information has not been submitted.  Please provide only numbers in the Birth Weight "'+fieldName+'" field');
		formField.focus();
		result = false;
		}
	}
	
	//if(theForm.wgtunit == lbs)
	//{
		//wgtunit = formField*2.20462;
	//}
	
	return result;
}

function checkGestAge(formField, fieldName)
{
	var result = true;
	
	if (formField.value.length > 0)
	{
		if (!allDigits(formField.value))
		{
		alert('Your information has not been submitted.  Please provide only numbers in the "'+fieldName+'" field');
		formField.focus();
		result = false;
		}
		if (formField.value > 45)
		{
		alert('Your information has not been submitted.  Please provide '+fieldName+' in weeks (full term is around 40 weeks)');
		formField.focus();
		result = false;
		}
		if (formField.value < 25)
		{
		alert('Your information has not been submitted.  Please provide '+fieldName+' in weeks (full term is around 40 weeks)');
		formField.focus();
		result = false;
		}
	}
	
	return result;
}
	
function isRequiredUSPhoneNumber(formField,fieldLabel, required)
{
	var stripednum = "";
	var result = true;

    if (required && !validRequired(formField,fieldLabel))
		return false;

    return isUSPhoneNumber(formField,fieldLabel)
}

	
function validEmail(formField)
{
	var result = true;

	if (formField.value.length > 0)
	{
	if (result && ((formField.value.length < 3) || !isEmailAddr(formField.value)) )
	{
		alert("Your information has not been submitted. The Email address provided does not appear to be correct. If you would like to provide an email address, please use the form: yourname@yourdomain.com");
		formField.focus();
		result = false;
	}
	}

   return result;

}

function validZip(formField,fieldLabel,required)
{
	var result = true;

	if (required && !validRequired(formField,fieldLabel))
		result = false;

 	if (result)
 	{
 		if (!allDigits(formField.value) || formField.value.length != 5)
 		{
 			alert('Your information has not been submitted. Please enter a five digit number for the "' + fieldLabel +'" field.');
			formField.focus();
			result = false;
		}
	}

	return result;
}

function validateForm(theForm)
{
	if (!checkName(theForm.childfirstname,"Child's First Name",true))
		return false;

	if (!checkName(theForm.childlastname,"Child's Last Name",true))
		return false;

	if (!checkPullDown(theForm.gender,"Child's Gender"))
		return false;

	if (!checkPullDown(theForm.dobmm,"Child's Date of Birth, Month"))
		return false;

	if (!checkPullDown(theForm.dobdd,"Child's Date of Birth, Day"))
		return false;

	if (!checkPullDown(theForm.dobyyyy,"Child's Date of Birth, Year"))
		return false;
	
	if (!checkGestAge(theForm.gestationalageAdd, "gestational age"))
		return false;
		
	if (!checkPremieOrDue(theForm.duedatemm, theForm.premieAdd))
		return false;

	if (!checkDueDateYear(theForm.duedateyyyy, theForm.dobyyyy, theForm.duedatemm, theForm.dobmm))
		return false;

	if (!checkWeight(theForm.birthweightlb, "lb"))
		return false;
		
	if (!checkWeight(theForm.birthweightoz, "oz"))
		return false;
		
	if (!checkWeight(theForm.birthweightkg, "kg"))
		return false;

	if (!checkName(theForm.parentfirstname1,"Parent First Name",true))
		return false;

	if (!checkName(theForm.parentlastname1,"Parent Last Name",true))
		return false;

	if (!validRequired(theForm.streetaddress,"Street Address"))
		return false;

	if (!checkName(theForm.city,"City",true))
		return false;

	if (!checkName(theForm.state,"State",true))
		return false;

	if (!validZip(theForm.zip,"Zip",true))
		return false;

	if (!isRequiredUSPhoneNumber(theForm.phonenumber1,"Telephone", true))
		return false;
		
	if (!isUSPhoneNumber(theForm.phonenumber2,"Second Telephone", true))
		return false;

	if (!validEmail(theForm.email))
		return false;

	return true;
}

