/* Function to validate the format of the email addess */
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))
	result = true;
  }
  return result;
}

/* Function to validate the any field */
function validRequired(formField,fieldLabel)
{
	var result = true;
	
	if (formField.value == "")
	{
		alert('Please enter a value for the "' + fieldLabel +'" field.');
		formField.focus();
		result = false;
	}
	
	return result;
}

/* Function to validate radio-buttons */
function validButton(formField,fieldLabel) 
{
  var myOption = -1;
    
  for (i=0; i < formField.length; i++) {
       if (formField[i].checked) {      
           myOption = i;
       }
  }
  if (myOption == -1) {
     alert('You must select a radio button for "' + fieldLabel +'"');
     result = false;
  }
  else {
     result = true;
   }
   return result;
}


/* Function to validate the selection of a single check-box */
//function validCheckBox(formField,fieldLabel)
function validCheckBox(formField)
{
    var result = true;

    if (formField.checked == false) {
        //alert('Select the "' + fieldLabel +'" check-box.');
        //alert('Select the check-box for Q9.');
        result = false;
    }

    return result;
}


/* Function to validate a numerical value */
function allDigits(str)
{
	return inValidCharSet(str,"0123456789");
}

/* Function to validate any string value */
function inValidCharSet(str,charset)
{
	var result = true;

	for (var i=0;i<str.length;i++)
		if (charset.indexOf(str.substr(i,1))<0)
		{
			result = false;
			break;
		}
	
	return result;
}

/* Function to validate the email address field */
function validEmail(formField,fieldLabel,required)
{
	var result = true;
	
	if (required && !validRequired(formField,fieldLabel))
		result = false;

	if (result && ((formField.value.length < 3) || !isEmailAddr(formField.value)) )
	{
		alert("Please enter a complete email address in the form: yourname@yourdomain.com");
		formField.focus();
		result = false;
	}
   
  return result;

}

/* Function to validate if text entered in Text-area */
function validText(formField,fieldLabel) 
{
    var result = true;

    if (formField.value.length == 0) {
        alert('Enter text for "' + fieldLabel + '"');
        result = false;
    }

    return result;
}


/* Function to validate the each field in the form */
function validateFForm(theForm){

	if (!validButton(theForm.Q1,"Q1. How many speakers an evening would you prefer?"))
	{	return false;     }
		
        if (!validButton(theForm.Q2,"Q2. Should we have speakers every meeting?"))
	{	return false;     }

	if ((!validCheckBox(theForm.Q3_1)) && (!validCheckBox(theForm.Q3_2)) && (!validCheckBox(theForm.Q3_3)) && (!validCheckBox(theForm.Q3_4)) && (!validCheckBox(theForm.Q3_5)) && (!validCheckBox(theForm.Q3_6)) && (!validCheckBox(theForm.Q3_7)))
	{	alert('You must select at least one check-box for "Q3. Topics you would like?"'); return false;     }

	if (!validButton(theForm.Q4,"Q4. Would you like to hear more from parents...?"))
	{	return false;     }

	if (!validButton(theForm.Q5,"Q5. Would you like to have a designated period of time...?"))
	{	return false;     }

        if (!validText(theForm.Q6,"Q6. Are there speakers you would like to hear?"))
	{	return false;     }

        if (!validText(theForm.Q7,"Q7. Is there anything that you would like to share?"))
	{	return false;     }

        if (!validText(theForm.Q8,"Q8. What do you enjoy most about the meetings?"))
	{	return false;     }

	if ((!validCheckBox(theForm.Q9_1)) && (!validCheckBox(theForm.Q9_2)) && (!validCheckBox(theForm.Q9_3)) && (!validCheckBox(theForm.Q9_4)) && (!validCheckBox(theForm.Q9_5)) && (!validCheckBox(theForm.Q9_6)) && (!validCheckBox(theForm.Q9_7)))
	{	alert('You must select at least one check-box for "Q9. Reasons for not attending meetings."'); return false;     }
        
        if (!validButton(theForm.Q10,"Q10. If changes were made to the meetings would you be more likely to attend?"))
	{	return false;     }
	
        if (!validButton(theForm.Q11,"Q11. Aware of ROK goals?"))
	{	return false;     }

	return true;
}
