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 FaxAreaCodeValue = "";
	var FaxPhonePrefixValue = "";
	var FaxPhoneMainValue = "";
	var Fax_complete = "";
	var Address1Value = "";
	var Address2Value = "";
	var CityValue = "";
	var StateValue = "";
	var Zip1Value = "";
	var Zip2Value = "";
	var InterestCount = new Number(0);
	var InterestList = new String("\n");
	var Address_complete = "";
	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;
	FaxAreaCodeValue = Form.Fax_area_code.value;
	FaxPhonePrefixValue = Form.Fax_phone_prefix.value;
	FaxPhoneMainValue = Form.Fax_phone_end.value;
	Address1Value = Form.Address1.value;
	Address2Value = Form.Address2.value;
	CityValue = Form.City.value;
	StateValue = Form.State.value;
	Zip1Value = Form.Zip.value;
	Zip2Value = Form.Zip_plus.value;
	AlertMessage = "";
	bFormValidate = Boolean(true);

	document.forms[0].recipient.value = EmailValue;
	document.forms[0].Date.value = SubmitDate;

    for (InterestCount = 0; InterestCount < document.forms[0].prod_dd.length; InterestCount++) 
	{
        if (document.forms[0].prod_dd.options[InterestCount].selected) 
		{
			if(InterestCount == 0)
			{
				InterestList += "";
			}
			else
			{
            	InterestList += "                       " + document.forms[0].prod_dd.options[InterestCount].text + "\n";
			}
        }
    }
	document.forms[0].ProdInterests.value = InterestList;
	
	// if the fax number has been entered, create the complete fax number for form submittal purposes
	if (isEmpty(FaxAreaCodeValue) || isEmpty(FaxPhonePrefixValue) || isEmpty(FaxPhoneMainValue))
	{
			document.forms[0].Fax.value = "None submitted";
	}
	else
	{
			Fax_complete = "(" + FaxAreaCodeValue + ") " + FaxPhonePrefixValue + "-" + FaxPhoneMainValue;
			document.forms[0].Fax.value = Fax_complete;
	}

	// create the address if it has been submitted
	if (isEmpty(Address1Value) || isEmpty(CityValue) || isEmpty(StateValue))
	{
			document.forms[0].Address.value = "None submitted";
	}
	else
	{
			Address_complete = Address1Value + "\n";
			if(Address2Value != "")
			{
				Address_complete += "                       " + Address2Value + "\n";
			}
			Address_complete += "                       " + CityValue + ", " + StateValue + "  " + Zip1Value;
			if(Zip2Value != "")
			{
				Address_complete += "-" + Zip2Value;
			}
			Address_complete += "\n";
			document.forms[0].Address.value = Address_complete;
	}

	// 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(document.forms[0].card.checked == true)
	{
		document.forms[0].addmail.value = "yes";
		/*if((document.forms[0].Address.value == "None Submitted")||(isEmpty(Zip1Value))||(isPosInteger(Zip1Value) == false))
		{
			AlertMessage += "You asked that we mail you a line card,\n     but you have supplied an incomplete address\n";
			bFormValidate = false;
		}*/
	}
	else
	{
		document.forms[0].addmail.value = "no";
	}
	
	// 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();
	}
}