function makeDateArray(){
	for (i = 0; i<makeDateArray.arguments.length; i++)
		this[i + 1] = makeDateArray.arguments[i];}
function getCurrentDate(){
	var months	= "";
	var date	= "";
	var day		= "";
	var month	= "";
	var yy		= "";
	var year	= "";
	var datedisplay = "";

	months 		= new makeDateArray('January','February','March','April','May','June','July','August','September','October','November','December');
	date 		= new Date();
	day 		= date.getDate();
	month 		= date.getMonth() + 1;
	yy		= date.getYear();
	year		= (yy < 1900) ? yy + 1900 : yy;
	datedisplay 	= months[month] + " " + day + ", " + year;

	return (datedisplay);}
function makeArray(){
	for (i = 0; i<makeArray.arguments.length; i++)
		this[i + 1] = makeArray.arguments[i];}
// query a buttongroup and find the selected button value
function getSelectedValue(buttonGroup){
	for (var buttonvalue = 0; buttonvalue < buttonGroup.length; buttonvalue++){
		if (buttonGroup[buttonvalue].checked){
			return buttonvalue;}}
	return false;}
// determine whether a text field on a form is empty
function isEmpty(inputStr){
	if (inputStr == null || inputStr == ""){
		return true;}
	return false;}
// determine whether a string contains only positive integers
function isPosInteger(inputValue){
	inputStr = inputValue.toString()
	for (var digit = 0; digit < inputStr.length; digit++){
		var oneChar = inputStr.charAt(digit);
		if (oneChar < "0" || oneChar > "9"){
			return false;}}
	return true;}
// determine whether a field that is supposed to receive only numeric input 
// actually contains only positive integers by calling the "isPosInteger" function
function validateNumber(inputValue){
	var inputStr = "";
	inputStr = (inputValue);
	if (!isPosInteger(inputStr)){
		return true;}
	return false;}
// validate email addresses
function validateEmail(str) {
		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   return false}
		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false}
		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    return false}
		 if (str.indexOf(at,(lat+1))!=-1){
		    return false}
		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return false}
		 if (str.indexOf(dot,(lat+2))==-1){
		    return false}
		 if (str.indexOf(" ")!=-1){
		    return false}
 		 return true}
// begin to validate the form
function validateForm(){
	// establish the variables
	var Form = "";
	var SubmitDate = "";
	var NameValue = "";
	var CompanyValue = "";
	var EmailValue = "";
	var AreaCodeValue = "";
	var PhonePrefixValue = "";
	var PhoneMainValue = "";
	var PhoneExtension = "";
	var Phone_complete = "";
	var PONumber = "";
	var Vendor = "";
	var bFormValidate = "";

	// assign initial values to the variables
	Form = document.forms[0];
	SubmitDate = getCurrentDate();
	NameValue = Form.Name.value;
	CompanyValue = Form.Company.value;
	EmailValue = Form.email.value;
	AreaCodeValue = Form.Area_code.value;
	PhonePrefixValue = Form.Phone_prefix.value;
	PhoneMainValue = Form.Phone_end.value;
	PhoneExtension = Form.Phone_ext.value;
	PONumber = Form.ponumber.value;
	Vendor = Form.vendor.value;
	AlertMessage = "";
	bFormValidate = Boolean(true);

	document.forms[0].recipient.value=EmailValue;
	document.forms[0].Date.value = SubmitDate;


	// establish the beginning message that should be included in the alert message -
	// selection is based on which method of contact the user selects from a set of 
	// radio buttons
		AlertMessage = "We cannot process your information because the\nfollowing items are missing:\n\n";

	// if the "name" field is empty, add to the alert message and set the form validation to "false"
	if (isEmpty(NameValue)){
		AlertMessage += "Your Name\n";
		bFormValidate = false;}

	// if the "company name" field is empty, add to the alert message and set the form validation to "false"
	if (isEmpty(CompanyValue)){
		AlertMessage += "Company's Name\n";
		bFormValidate = false;}

	// if the "eMail address" field is empty, add to the alert message and set the form validation to "false"
	if(isEmpty(EmailValue)){
		AlertMessage += "Your eMail Address\n";
		bFormValidate = false;}
	else{
		if(validateEmail(EmailValue) == false){
			AlertMessage += "A valid eMail Address\n";
			bFormValidate = false;}}
	
	// if any of the "Telephone number" fields are empty, add to the alert message and set the form validation to "false"
	if (isEmpty(AreaCodeValue) || isEmpty(PhonePrefixValue) || isEmpty(PhoneMainValue)){
		AlertMessage += "Telephone Number\n";
		bFormValidate = false;}
	else{
		if (validateNumber(AreaCodeValue) || validateNumber(PhonePrefixValue) || validateNumber(PhoneMainValue)){
			AlertMessage += "Telephone Number\n";
			bFormValidate = false;}
		else{
			Phone_complete = "(" + AreaCodeValue + ") " + PhonePrefixValue + "-" + PhoneMainValue;
			if(!isEmpty(PhoneExtension)){
				Phone_complete += ", Ext. # " + PhoneExtension;}
			document.forms[0].Telephone.value = Phone_complete;}}

	// if the "ponumber " field is empty, add to the alert message and set the form validation to "false"
	if (isEmpty(PONumber)){
		AlertMessage += "Your PO #\n";
		bFormValidate = false;}
	
	// if the "vendor name" field is empty, add to the alert message and set the form validation to "false"
	if (isEmpty(Vendor)){
		AlertMessage += "Vendor's Name\n";
		bFormValidate = false;}	
	
	// if any alert strings were generated, then construct the message and send it in an alert box
	if (bFormValidate == false){
		alert(AlertMessage+ "\n\nPlease provide the missing information and resubmit.");}

	//otherwise, process the form
	if (bFormValidate == true){
		Form.submit();}}