/* 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 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 in the "' + fieldLabel + '" area.');
        result = false;
    }

    return result;
}

/* Function to open new window and display selections */
function openwin(theForm)
{
	OpenWindow=window.open ('', 'newwindow', 
  	'height=1000, width=600, toolbar=no, menubar=no, scrollbars=yes, resizable=yes, location=no, directories=no, status=no')
	OpenWindow.document.write("<title>My Form Entries</title>")
	OpenWindow.document.write("<body>")
        OpenWindow.document.write("1. Name entered is: " + theForm.Full_name.value + "<br>")
        OpenWindow.document.write("2. Subject entered is: " + theForm.subject.value + "<br>")
        OpenWindow.document.write("3. Phone number entered is: " + theForm.Phone_number.value + "<br>")
        OpenWindow.document.write("4. Email Address entered is: " + theForm.Email.value + "<br>")
        OpenWindow.document.write("5. The supplements entered are: <pre>" + theForm.Supps_request.value + "</pre>")
	OpenWindow.document.write("</body>")
	OpenWindow.document.write("</html>")

	OpenWindow.document.close()
	self.name="main"
}    

/* Function to display, confirm and submit form selections */
function confirmSummary(theForm)
{
    openwin(theForm);
    var result = false;
    var answer = confirm("Your order is now displayed in a new window. Please refer to that window \n" 
                       + "and verify the entries are correct! If they are correct then press OK \n" 
                       + "to submit otherwise press Cancel. \n");
       if (answer)
       {    result = true; }
    return result;
}

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

	if (!validRequired(theForm.Full_name,"Your full name"))
		return false;

        if (!validRequired(theForm.subject,"Subject"))
                return false;

        if (!validRequired(theForm.Phone_number,"Your phone number"))
                return false;

	if (!validEmail(theForm.Email,"Your E-mail address",true))
		return false;

        if (!validText(theForm.Supps_request,"Enter supplement list",true))
		return false;

        if (confirmSummary(theForm))       
	{    return true; }
        else 
        {    return false; } 
}
