/************************************************************/
/* This script and many more are available free online at
The JavaScript Source!! http://javascript.internet.com
Created by: Jay Rumsey | http://www.nova.edu/~rumsey/ */
// Used to show or hide fields related to individual versus organizational nominees. 
function ShowReg(op) {
  document.getElementById('Individual').style.display='none';
  document.getElementById('Organization').style.display='none';

  if (op == 1) {
    document.getElementById('Individual').style.display='block';
  }
  if (op == 2) {
    document.getElementById('Organization').style.display='block';
  }
}

// Used to count the words during form completion
function CountWordsLeft(myForm, field, count, no_words, field_id) {
	var text=field.value + " ";
	if(no_words>0) {
        var iwhitespace = /^[^A-Za-z0-9]+/gi; // remove initial whitespace
        var left_trimmedStr = text.replace(iwhitespace, "");
        var na = rExp = /[^A-Za-z0-9]+/gi; // non alphanumeric characters
        var cleanedStr = left_trimmedStr.replace(na, " ");
        var splitString = cleanedStr.split(" "); 
        var word_count = splitString.length -1;
        count.value=no_words-word_count;		
		if (count.value<0) {
			alert ("You have exceeded the maximum number of words for \n" + field_id);
			if (field_id == 'the synopsis.') {
				document.NominationForm.Synopsis.focus();
			} else if (field_id == '1. Describe the mission of your organization.') {
				document.NominationForm.Mission.focus();
			} else if (field_id == '2. Why is your organization nominating this candidate?') {
				document.NominationForm.WhyNominate.focus();
      		} else if (field_id == '3. How long has your nominee been involved with your organization?') {
				document.NominationForm.TimeInvolved.focus();
			} else if (field_id == '4. What other community activities is your nominee involved with?') {
				document.NominationForm.CommunityInvolved.focus();
			} else if (field_id == '5. Additional Comments.') {
				document.NominationForm.AdditionalComments.focus();
			}
		}
    }
}

// Used to validate the phone number during form completion
function validatePhoneNumber(phone, field_id) {
	if (phone.value.search(/\d{3}\-\d{3}\-\d{4}/) == -1 ) {
		alert("Please enter a value for " + field_id + " with the format xxx-xxx-xxxx. Example: 780-555-5555.");
   	}
}

// Used to validate the postal code during form completion
function validatePostalCode(postalCode, field_id) {
	postalCode.value=postalCode.value.toUpperCase();
	if (postalCode.value.search(/^\s*[a-ceghj-npr-tvxy]\d[a-z](\s)\d[a-z]\d\s*$/i) == -1 ) {
		alert("Please enter a value for " + field_id + " with the format xxx xxx. Example: T6G 2H7");
		postalCode.value = 'xxx xxx'; 
   	}
}

// Used to validate categories during form completion
function validateCategory(category) {	
  	if (category.selectedIndex==0) {
		alert("Please select a value for the 'Nomination Category' field.");
    }
}

// Used to validate required text fields during form completion
function validateRequiredText(name) {	
	if (name.value=="") {
		alert ("Please enter a value for the " + name.id + " field.");
	}
}

// Used to validate the entire form before it is submitted
function validateBeforeSubmit2(theform) {
	var missingFields = false;
	var strFieldsNominator = ""; 
	var strFieldsNominee = ""; 
	var strFieldsCount = ""; 
	
	// NOMINATOR Section
	if (theform.NameofOrganization_Nominator.value=="") {
		missingFields = true;
		strFieldsNominator += "Please enter a value for the 'Name of Organization Nominating' field. \n";
	}
	if (theform.FirstNameofContact_Nominator.value=="") {
		missingFields = true;
		strFieldsNominator += "Please enter a value for the 'First Name of Contact' field. \n";
	}
	if (theform.LastNameofContact_Nominator.value=="") {
		missingFields = true;
		strFieldsNominator += "Please enter a value for the 'Last Name of Contact' field. \n"; 
	}
	if (theform.TitleofContact_Nominator.value=="") {
		missingFields = true;
		strFieldsNominator += "Please enter a value for the 'Title of Contact' field. \n";
	}
	if (theform.PhoneNumber_Nominator.value.search(/\d{3}\-\d{3}\-\d{4}/) == -1) { 
		missingFields = true;
		strFieldsNominator += "Please enter a value for the 'Phone Number' field with the format xxx-xxx-xxxx. Example: 780-555-5555. \n";
	}
	if (theform.email.value=="") {
		missingFields = true;
		strFieldsNominator += "Please enter a value for the 'Email Address' field. \n"; 
	}
	if (theform.StreetAddress_Nominator.value=="") {
		missingFields = true;
		strFieldsNominator += "Please enter a value for the 'Street Address' field. \n";
	}
	if (theform.City_Nominator.value=="") {
		missingFields = true;
		strFieldsNominator += "Please enter a value for the 'City' field. \n" 
	}
	theform.PostalCode_Nominator.value=theform.PostalCode_Nominator.value.toUpperCase();
	if (theform.PostalCode_Nominator.value.search(/^\s*[a-ceghj-npr-tvxy]\d[a-z](\s)\d[a-z]\d\s*$/i) == -1 ) {
		theform.PostalCode_Nominator.value = 'xxx xxx'; 
		missingFields = true;		
		strFieldsNominator +=  "Please enter a value for the 'Postal Code' field with the format xxx xxx. Example: T6G 2H7 \n";
	}
	
	// NOMINEE Section 
	if (theform.IndividualOrOrganization.selectedIndex == 0) {
		missingFields = true;	
		strFieldsNominee +=  "Please enter a value for the 'Are you nominating an individual or an organization?' field. \n";
	}
	if (theform.IndividualOrOrganization.selectedIndex == 1) {
		if (theform.FirstName_Nominee.value=="") {
			missingFields = true;	
			strFieldsNominee +=  "Please enter a value for the 'First Name of Nominee' field. \n";
		}
		if (theform.LastName_Nominee.value=="") {
			missingFields = true;	
			strFieldsNominee +=  "Please enter a value for the 'Last Name of Nominee' field. \n";
		}
		if (theform.SelectOneNominationCategory_Individual.selectedIndex==0) { 
			missingFields = true;	
			strFieldsNominee +=  "Please select a value for the 'Select One Nomination Category (Individual)' field. \n";
		}
	} // end Individual 
	if (theform.IndividualOrOrganization.selectedIndex == 2) {
		if (theform.NomineeOrganization_Nominee.value=="") {
			missingFields = true;	
			strFieldsNominee +=  "Please enter a value for the 'Nominee Organization' field. \n";
		}
		if (theform.SelectOneNominationCategory_Organization.selectedIndex==0) { 
			missingFields = true;	
			strFieldsNominee +=  "Please select a value for the 'Select One Nomination Category (Organization)' field. \n";
		}
	} // end Organization 
	if (theform.PhoneNumber_Nominee.value.search(/\d{3}\-\d{3}\-\d{4}/) == -1) { 
		missingFields = true;
		strFieldsNominee += "Please enter a value for the 'Phone Number' field with the format xxx-xxx-xxxx. Example: 780-555-5555. \n"; 
	}
	if (theform.EmailAddress_Nominee.value=="") {
		missingFields = true;
		strFieldsNominee += "Please enter a value for the 'Email Address' field. \n"; 
	}
	if (theform.StreetAddress_Nominee.value=="") {
		missingFields = true;
		strFieldsNominee += "Please enter a value for the 'Street Address' field. \n"; 
	}
	if (theform.City_Nominee.value=="") {
		missingFields = true;
		strFieldsNominee += "Please enter a value for the 'City' field. \n"; 
	}
	theform.PostalCode_Nominee.value=theform.PostalCode_Nominee.value.toUpperCase();
	if (theform.PostalCode_Nominee.value.search(/^\s*[a-ceghj-npr-tvxy]\d[a-z](\s)\d[a-z]\d\s*$/i) == -1 ) {
		theform.PostalCode_Nominee.value = 'xxx xxx'; 
		missingFields = true;		
		strFieldsNominee +=  "Please enter a value for the 'Postal Code' field with the format xxx xxx. Example: T6G 2H7 \n";
	}
	if (theform.countSynopsis.value < 0) {
		missingFields = true;
		strFieldsCount += "Please use less than 150 words for the 'Synopsis' field. \n"; 
	}
	if (theform.countSynopsis.value == 150) {
		missingFields = true;
		strFieldsCount += "Please enter a value for the 'Synopsis' field. \n"; 
	}
	if (theform.countMission.value < 0) {
		missingFields = true;
		strFieldsCount += "Please use less than 250 words for the '1. Describe the mission of your organization' field. \n"; 
	}
	if (theform.countMission.value == 250) {
		missingFields = true;
		strFieldsCount += "Please enter a value for the '1. Describe the mission of your organization' field. \n"; 
	}
	if (theform.countWhyNominate.value < 0) {
		missingFields = true;
		strFieldsCount += "Please use less than 500 words for the '2. Why is your organization nominating this candidate?' field. \n"; 
	}
	if (theform.countWhyNominate.value == 500) {
		missingFields = true;
		strFieldsCount += "Please enter a value for the '2. Why is your organization nominating this candidate? ' field. \n"; 
	}
	if (theform.countTimeInvolved.value < 0) {
		missingFields = true;
		strFieldsCount += "Please use less than 150 words for the '3. How long has your nominee been involved with your organization?' field. \n"; 
	}
	if (theform.countTimeInvolved.value == 150) {
		missingFields = true;
		strFieldsCount += "Please enter a value for the '3. How long has your nominee been involved with your organization?' field. \n"; 
	}
	if (theform.countCommunityInvolved.value < 0) {
		missingFields = true;
		strFieldsCount += "Please use less than 150 words for the '4. What other community activities is your nominee involved with? Community?' field. \n"; 
	}
	if (theform.countCommunityInvolved.value == 150) {
		missingFields = true;
		strFieldsCount += "Please enter a value for the '4. What other community activities is your nominee involved with? Community?' field. \n"; 
	}
	if (theform.countAdditionalComments.value < 0) {
		missingFields = true;
		strFieldsCount += "Please use less than 250 words for the '5. Additional Comments' field. \n"; 
	}
		
	// SUBMIT the form
	if (missingFields == true) {
		var strFields = "";
		
		if (strFieldsNominator != "") {
			strFields += "Nominator Information \n\n" + strFieldsNominator + "\n";
		}
		if (strFieldsNominee != "") {
			strFields += "Nominee Information \n\n" + strFieldsNominee + "\n";
		}
		if (strFieldsCount != "") {
			strFields += "Long Answer Questions \n\n" + strFieldsCount;
		}
		alert(strFields); 
		
		return false; 
	} else { // OR NOT
		return true;
	}
}

// For previewing the form 
function fieldIncomplete(field, popup) {
	if (field=='xxx-xxx-xxxx' || field=='xxx xxx' || field=='Select one...' || field.length==0 || field.selectedIndex==0) {
		popup.document.write("<font color='red'>Please complete this required information.</font>");
	} else {
		popup.document.write(field);
	}
}

function fieldIncompleteIndividual(field, popup) {
	if (field=='xxx-xxx-xxxx' || field=='xxx xxx' || field=='Select one...' || field.length==0 || field.selectedIndex==0) {
		popup.document.write("<font color='red'>Please complete this required information.</font>");
	} else if (field=='1') {
		popup.document.write("Individual");
	} else if (field =='2') {
		popup.document.write("Organization");
	}
}

function previewForm() { 
	popup = window.open('','popup','toolbar=no,menubar=no,width=500,scrollbars=yes,resizable=yes'); 
	popup.document.open(); 
	popup.document.write("<html><head><link href=\"AFPnn4.css\" rel=\"stylesheet\" type=\"text/css\"></head><body>"); 
	popup.document.write("<font size='2' face='Arial, Helvetica, sans-serif'>"); 
	popup.document.write("<a href='javascript:window.close()'>Close Window</a> | "); 
	popup.document.write("<a href='javascript:window.print()'>Print This Page</a>"); 
	popup.document.write("<p><img src='/images/PDbanner.gif'><p><b>NOMINATION FORM</b></p>" ); 
// Nominator
	popup.document.write("<p><b>Nominator Information</b></p>" ); 
	popup.document.write("<p>Name of Organization Nominating <font color='red'>*</font></p>" ); 
	fieldIncomplete(document.NominationForm.NameofOrganization_Nominator.value, popup); 	
	popup.document.write("<p>First Name of Contact <font color='red'>*</font></p>" ); 
	fieldIncomplete(document.NominationForm.FirstNameofContact_Nominator.value, popup); 	
	popup.document.write("<p>Last Name of Contact <font color='red'>*</font></p>" ); 
	fieldIncomplete(document.NominationForm.LastNameofContact_Nominator.value, popup); 	 
	popup.document.write("<p>Title of Contact <font color='red'>*</font></p>" ); 
	fieldIncomplete(document.NominationForm.TitleofContact_Nominator.value, popup); 	
	popup.document.write("<p>Phone Number <font color='red'>*</font></p>" ); 
	fieldIncomplete(document.NominationForm.PhoneNumber_Nominator.value, popup); 	
	popup.document.write("<p>Email Address <font color='red'>*</font></p>" ); 
	fieldIncomplete(document.NominationForm.EmailAddress_Nominator.value, popup); 	
	popup.document.write("<p>Street Address <font color='red'>*</font></p>" ); 
	fieldIncomplete(document.NominationForm.StreetAddress_Nominator.value, popup); 	
	popup.document.write("<p>City <font color='red'>*</font></p>" ); 
	fieldIncomplete(document.NominationForm.City_Nominator.value, popup); 	 
	popup.document.write("<p>Postal Code <font color='red'>*</font></p>" ); 
	fieldIncomplete(document.NominationForm.PostalCode_Nominator.value, popup); 	
// Nominee
	popup.document.write("<p><b>Nominee Information</b></p>" ); 
	popup.document.write("<p>Are you nominating an individual or an organization? <font color='red'>*</font></p>" );
	fieldIncompleteIndividual(document.NominationForm.IndividualOrOrganization.value, popup); 
	if (document.NominationForm.IndividualOrOrganization.selectedIndex == 1) { 
		popup.document.write("<p>First Name of Nominee <font color='red'>*</font></p>" ); 
		fieldIncomplete(document.NominationForm.FirstNameofNominee_Nominee.value, popup); 	
		popup.document.write("<p>Last Name of Nominee <font color='red'>*</font></p>" ); 
		fieldIncomplete(document.NominationForm.LastNameofNominee_Nominee.value, popup); 
		popup.document.write("<p>Select One Nomination Category (Individual) <font color='red'>*</font></p>" ); 
		fieldIncomplete(document.NominationForm.SelectOneNominationCategory_Individual.options[document.NominationForm.SelectOneNominationCategory_Individual.selectedIndex].text, popup);	
	} 
	if (document.NominationForm.IndividualOrOrganization.selectedIndex == 2) {
		popup.document.write("<p>Name of Nominee Organization<font color='red'>*</font></p>" ); 
		fieldIncomplete(document.NominationForm.NomineeOrganization_Nominee.value, popup); 	
		popup.document.write("<p>Select One Nomination Category (Organization) <font color='red'>*</font></p>" ); 
		fieldIncomplete(document.NominationForm.SelectOneNominationCategory_Organization.options[document.NominationForm.SelectOneNominationCategory_Organization.selectedIndex].text, popup);
	}
	popup.document.write("<p>First Name of Contact (if different)</p>" ); 
	popup.document.write(document.NominationForm.FirstNameofContact_Nominee.value);
	popup.document.write("<p>Last Name of Contact (if different)</p>" ); 
	popup.document.write(document.NominationForm.LastNameofContact_Nominee.value);	
	popup.document.write("<p>Phone Number <font color='red'>*</font></p>" ); 
	fieldIncomplete(document.NominationForm.PhoneNumber_Nominee.value, popup); 	
	popup.document.write("<p>Email Address <font color='red'>*</font></p>" ); 
	fieldIncomplete(document.NominationForm.EmailAddress_Nominee.value, popup); 	
	popup.document.write("<p>Street Address <font color='red'>*</font></p>" );
	fieldIncomplete(document.NominationForm.StreetAddress_Nominee.value, popup); 	
	popup.document.write("<p>City <font color='red'>*</font></p>" ); 
	fieldIncomplete(document.NominationForm.City_Nominee.value, popup); 	
	popup.document.write("<p>Postal Code <font color='red'>*</font></p>" ); 
	fieldIncomplete(document.NominationForm.PostalCode_Nominee.value, popup); 

// Nomination
	popup.document.write("<p>Long Answer Questions</p>" ); 
	popup.document.write("<p>Synopsis (may be used for promotional purposes) Max. 150 words <font color='red'>*</font></p>" ); 
	fieldIncomplete(document.NominationForm.Synopsis.value, popup);
	popup.document.write("<p>1. Describe the mission of your organization. Max. 250 words <font color='red'>*</font></p>" ); 
	fieldIncomplete(document.NominationForm.Mission.value, popup);
	popup.document.write("<p>2. Why is your organization nominating this candidate? (Describe the impact the nominee has had on your organization and what specific achievements were accomplished through the nominee’s involvement with your organization.) Max. 500 words <font color='red'>*</font></p>" ); 
	fieldIncomplete(document.NominationForm.WhyNominate.value, popup);
	popup.document.write("<p>3. How long has your nominee been involved with your organization? Max. 150 words <font color='red'>*</font></p>" ); 
	fieldIncomplete(document.NominationForm.TimeInvolved.value, popup);
	popup.document.write("<p>4. What other community activities is your nominee involved with? Community? Max. 150 words <font color='red'>*</font></p>" ); 
	fieldIncomplete(document.NominationForm.CommunityInvolved.value, popup);
	popup.document.write("<p>5. Additional Comments Max. 250 words (optional)</p>" ); 
	popup.document.write(document.NominationForm.AdditionalComments.value); 
	popup.document.write("<p><a href='javascript:window.close()'>Close Window</a> | "); 
	popup.document.write("<a href='javascript:window.print()'>Print This Page</a></p></font></body></html>"); 
	popup.document.close(); 
} 