// variables to store the checking behavior and error messages for each field

var checkFullName    = true; // Are we checking for a Name;
var msgFullName      = "Please fill in your name.";
var checkAddress      = true; // Are we checking for a Street Address;
var msgStreetAddress  = "Please fill in your mailing address.";
var checkCity         = true; // Are we checking for a City;
var msgCity           = "Please fill in your City.";
var checkState        = true; // Are we checking for a State;
var msgState          = "Please fill in your State.";
var checkZipCode      = true; // Are we checking for a ZipCode;
var msgZipCode        = "Please fill in your ZipCode.";
var checkProperty     = true; // Are we checking for a Property;
var msgProperty       = "Please indicate in what publication you saw our ad.";
var checkPhone = false; // Are we checking for a Phone number;
var msgPhone   = "Please fill in your Phone number.";
var checkEmail        = false; // Are we checking for a Email;
var msgEmail          = "Please fill in your email address.";
var checkChoices      = true; // Are we going to check the number of brochures requested;
var checkMin          = true; // Are we going to check for a minimum number of requested brochures;
var checkMax          = false; // Are we going to check for a maximum number of requested brochures;
var minChoices        = 1; // Minimum number of brochures allowed per request;
var maxChoices        = 3; // Maximum number of brochures allowed per request;
var numChoices        = 0; // Set number of brochures selected to zero; 
var msgMinChoices     = "Please select at least one brochure."; // Message to display if minimum not met;
var msgMaxChoices     = "Please select no more than three brochures."; // Message to display if maximum exceeded;

// (re)initialize the form
function initForm(){
	// store the initial values (blank)
	initCity           = "";
	initFullName       = "";
	initState          = "";
	initStreetAddress  = "";
	initPhone   = "";
	initEmail          = "";
	initZipCode        = "";
	initProperty       = "";
	numChoices         = 0;
	// put initial values in the information form
	document.informationForm.City.value = initCity;
	document.informationForm.FullName.value = initFullName;
	document.informationForm.State.value = initState;
	document.informationForm.StreetAddress.value = initStreetAddress;
	document.informationForm.Phone.value = initPhone;
	document.informationForm.Email.value = initEmail;
	document.informationForm.ZipCode.value = initZipCode;
	document.informationForm.Property.value = initProperty
	// put the initial values in the request form
	document.requestForm.City.value = initCity;
	document.requestForm.FullName.value = initFullName;
	document.requestForm.State.value = initState;
	document.requestForm.StreetAddress.value = initStreetAddress;
	document.requestForm.Phone.value = initPhone;
	document.requestForm.Email.value = initEmail;
	document.requestForm.ZipCode.value = initZipCode;
	document.requestForm.Property.value = initProperty;
	document.requestForm.reset();}

// validate the FullName field
function chkFullName(_in){
	if (_in.value.length > 0){return true;
	}else{alert(msgFullName); document.informationForm.FullName.focus(); return false;}}

// validate the StreetAddress field
function chkStreetAddress(_in){
	if (_in.value.length > 0){return true;
	}else{alert(msgStreetAddress); document.informationForm.StreetAddress.focus(); return false;}}

// validate the City field
function chkCity(_in){
	if (_in.value.length > 0){return true;
	}else{alert(msgCity); document.informationForm.City.focus(); return false;}}

// validate the State field
function chkState(_in){
	if (_in.value.length > 0){return true;
	}else{alert(msgState); document.informationForm.State.focus(); return false;}}

// validate the ZipCode field
function chkZipCode(_in) {
	if (_in.value.length > 0){return true;
	}else{alert(msgZipCode); document.informationForm.ZipCode.focus(); return false;}}

// validate the Phone field
function chkPhone(_in){
	if (_in.value.length > 0){return true;
	}else{alert(msgPhone); document.informationForm.Phone.focus(); return false;}}

// validate the Property field
function chkProperty(_in){
	if (_in.value.length > 0){return true;
	}else{alert(msgProperty); document.informationForm.Property.focus(); return false;}}
	
// validate the Email field
function chkEmail(_in){
	if (_in.value.length > 0){return true;
	}else{alert(msgEmail); document.informationForm.Email.focus(); return false;}}

// validate the brochure choices
function chkChoices(){
	var inRange = true;
	if (checkMin) {if (numChoices < minChoices){inRange = false; alert(msgMinChoices);}else{inrange = true;}}
	if (checkMax) {if (numChoices > maxChoices){inRange = false; alert(msgMaxChoices);}else{inrange = true;}}
	return inRange;
}

// adjust count of selected brocures
function adjCount(_in){
	if (_in.checked){numChoices = numChoices + 1; _in.value="ON"
	}else{numChoices = numChoices - 1; _in.value="OFF"}}

// validate the entries in the form.
function validEntries(){
	var isGood; //internal flag
	// check each field in turn, but only if the previous fields are valid
        isGood = true;
	with (document.requestForm) {
		if (isGood & checkFullName)     isGood = chkFullName(FullName);
		if (isGood & checkAddress)      isGood = chkStreetAddress(StreetAddress);
		if (isGood & checkCity)         isGood = chkCity(City);
		if (isGood & checkState)        isGood = chkState(State);
		if (isGood & checkZipCode)      isGood = chkZipCode(ZipCode);
		if (isGood & checkProperty)     isGood = chkProperty(Property);
		if (isGood & checkPhone) isGood = chkPhone(Phone);
		if (isGood & checkEmail)        isGood = isGood = chkEmail(Email);
		if (isGood & checkChoices)      isGood = chkChoices();}
	return isGood;}

// process the information in the forms
function processForm(){
	// copy the entries in the information form to the request form
	document.requestForm.FullName.value=document.informationForm.FullName.value;
	document.requestForm.StreetAddress.value=document.informationForm.StreetAddress.value;
	document.requestForm.City.value=document.informationForm.City.value;
	document.requestForm.State.value=document.informationForm.State.value;
	document.requestForm.ZipCode.value=document.informationForm.ZipCode.value;
	document.requestForm.Property.value=document.informationForm.Property.value;
	document.requestForm.Phone.value=document.informationForm.Phone.value;
	document.requestForm.Email.value=document.informationForm.Email.value;
	// validate the contact information
	if (validEntries()) {document.requestForm.submit();}}