// JavaScript Document

// Main code checking loop

function checkWholeForm(theForm) {
	var why = "Please check the following items in the form:\n";
	var no_error_why = why;
	
	// Check user's name.
	
	if (theForm.first_name.value == "") {
		why += "  First name\n";
	}
	
	if (theForm.last_name.value == "") {
		why += "  Last name\n";
	}

	// Check company name.
	
	if (theForm.company.value == "") {
		why += "  Company name\n";
	}

	// Check email.

	why += checkEmail(theForm.email.value);
	why += checkRetypeEmail(theForm.email.value,theForm.email2.value);

	// Check if interest timeframe was specified.

	if (theForm.Interest_timeframe__c.value == "") {
		why += "  Purchase Timeframe\n";
	}
	
	// Check if deployment intent was specified.

	if (theForm.Type_of_Relationship__c.value == "") {
		why += "  Type of Deployment\n";
	}
	
	// Check if application areas were checked.

	var at_least_one = false;
	
	for (i=0, n=theForm.interest.length; i<n; i++) {
		if (theForm.interest[i].checked) {
			at_least_one = true;
			break;
		}
	}

	// Not a required entry for this form so commented out.

	/* ---
	if (at_least_one == false)
		why += "  Check at least one application area interest\n";
	--- */

	if (why != no_error_why) {
		alert(why);
		return false;
	}

	// Post-processing after validation succeeds.
	// Assemble list of interests from check boxes.

	var checked_interests = "Checked Interests: ";
	var first_time = true;
	
	if (at_least_one == true) {

		for (i=0, n=theForm.interest.length; i<n; i++) {
			if (theForm.interest[i].checked) {
				if (first_time == true) {
					checked_interests += theForm.interest[i].value;
					first_time = false;
				}
				else {
					checked_interests += ", " + theForm.interest[i].value;
				}
			}
		}
	}

	// If the form has passed validation, concatenate the check box values
	// and text message area content. These then go into one field in Salesforce.com.
	
	if (at_least_one == true) {
		if (theForm.message.value != "") {
			theForm.description.value = checked_interests + "\nComment / Message text:\n" + theForm.message.value;
			return true;
		}
		else {
			theForm.description.value = checked_interests;
			return true;
		}
	}

	if (at_least_one != true) {
		if (theForm.message.value != "") {
			theForm.description.value = theForm.message.value;
			return true;
		}
		else {
			theForm.description.value = "";
			return true;
		}
	}

	return true;
}

// email

function checkEmail (strng) {
	var error="";

	if (strng == "") {
 	 error = "  Email address\n";
	  return error;
	}

  var emailFilter=/^.+@.+\..{2,3}$/;
  if (!(emailFilter.test(strng))) { 
    error = "  Email address\n";
  }
  else {
//test email for illegal characters
		var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
		if (strng.match(illegalChars)) {
			error = "  Email address\n";
		}
	}
return error; 
}

function checkRetypeEmail (strng, strng2) {
	var error = "";
	if (strng2 == "") {
	 error = "  Confirm email address\n";
	 return error;
	}
	if (strng != strng2) {
 	 error = "  Confirm email address\n";
	 return error;
	}
return error;
}
