errorsDisplayed=false;

// Check the RFI form (1-8 slots plus fname, lname, email) for completeness
//
function checkRequired() {
	
	// Flags to mark the two basic conditions that must be met
	profileIsComplete=true; // assume it's complete unless check finds that it isn't
	
	
	// Clear indicators if users submits a second time after correcting ommissions
	clearAlerts();
	
	// Check that the required profile fields (fname, lname, email) are complete
	for (q=0; ((q<requiredProfile.length)); q++) {
		// Store information about the current question
		var which = requiredProfile[q] [0];
		var type = requiredProfile[q] [1];

		if (!isAnswered(which,type)) {
			profileIsComplete=false;
			showAlert(which); // Show an alert message
		}
	}

	// Return the results of this check. "true" submits the form, "false" does not.
	if (profileIsComplete) {
		return true;
	} else {
		return false;
	}
	
	// Just in case
	return false;
}

function checkSlots() {
	// Assume none are selected
	slotIsSelected=false;
	
	// Check all that at least one slot is selected
	for (i=1; (i<=numberOfVendorSlots && !slotIsSelected); i++) {		
		if (eval("document.forms.rfi.slot" + i + ".checked")) {
			slotIsSelected=true;
		}
	}
	
	// If, after looping thru slots, none are selected then show the slot alert
	if (!slotIsSelected) {
		// Set a message above vendors and show an alert arrow
		document.getElementById('slotSpot').bgColor = "#FFFFDD";
		errorsDisplayed=true;
	}

	// Return the results of this check. "true" submits the form, "false" does not.
	if (slotIsSelected) {
		return true;
	} else {
		// Only scroll if profile is incomplete and at least one slot is selected
		//if (!profileIsComplete && slotIsSelected) location.hash = "profile";
		return false;
	}
	
	// Just in case
	return false;
}


// Check if the specified question (which) has been answered. Must also pass in the 
// question type (questionType), since there are different checks for pulldowns, 
// radio buttons, checkboxes, zip fields or open text fields
//
function isAnswered(questionID, questionType) {

	// PULLDOWN check
	if (questionType=="pulldown") {
		// Completed answer validation for pulldown lists
		if (eval("document.forms." + formName + "." + questionID + ".value != ''")) {
			return true;
		} else {
			// Show a JavaScript alert
			showAlert(questionID);
			return false;
		}
		
	// PROFILEPULLDOWN check (it's empty value is not "" but "0". why? who knows)
	} else if (questionType=="profilePulldown") {
		// Completed answer validation for open text boxes
		if (eval("document.forms." + formName + "." + questionID + ".value == '0'")) {
			// Show a JavaScript alert
			showAlert(questionID);
			return false;
		} else {
			return true;
		}
		
	// TEXTFIELD check it's not empty 
	} else if (questionType=="textfield") {
		// Completed answer validation for open text boxes
		if (eval("document.forms." + formName + "." + questionID + ".value != ''")) {
			return true;
		} else {
			// Show a JavaScript alert
			showAlert(questionID);
			return false;
		}
		
	// EMAIL check for validly formed address
	} else if (questionType=="email") {
		var x = eval("document.forms." + formName + "." + questionID + ".value");
		var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		if (filter.test(x)) {
			return true;
		} else {
			return false;
		}
		
		
	// PHONE check
	} else if (questionType=="phone") {
		// coming soon
		
		
	// ZIP check
	} else if (questionType=="zip") {
		// Use the checkZip() function below to validate ZIP codes
		return checkZip(questionID,formName);
		
		
	// RADIOBUTTON or CHECKBOX check
	} else if (questionType=="radiobutton" || questionType=="checkbox") {
		// Completed answer validation for radio buttons and checkboxes
		for (var j=0; j < eval("document.forms." + formName + "." + questionID + ".length"); j++) {
			if (eval("document.forms." + formName + "." + questionID + "[" + j + "].checked")) {
				return true;
			}
		}
		// Show a JavaScript alert
		showAlert(questionID);
		return false;
	}
	
	return false;
}


// Verify that the entered zip code is at least 5 characters and is all numbers
//
function checkZip(which, formName) {
	// Capture the initially entered value
	var checkStr = eval("document.forms." + formName + "." + which + ".value");

	// Check if only numbers were entered
	var checkOK = "0123456789";
	var isValid = true;
	var allNum = "";
	for (i = 0;  i < checkStr.length;  i++) {
		ch = checkStr.charAt(i);
		for (j = 0;  j < checkOK.length;  j++)
		if (ch == checkOK.charAt(j)) break;
		if (j == checkOK.length) {
			isValid = false;
			break;
		}
		if (ch != ",") allNum += ch;
	}	  

	// Check if 5 digits were entered
	if (checkStr.length != 5) {
		isValid = false;
	}

	if (!isValid) {
		// show an alert message if non-valid characters were found
		showAlert(which);

		eval("document.forms." + formName + "." + which + ".value=''"); // clear the bad field
		eval("document.forms." + formName + "." + which + ".focus()");  // refocus the cursor in the bad field

		return false;
	}

	return true;
}

// Modify the HTML form to indicate which questions are unanswered.
// 	1) show an icon next to the question
// 	2) highlight the question's cell color
//
function showAlert(questionID) {
	// Show the flag for the missing question
	eval('document.' + questionID + '.src="/images/missing_arrow.gif"');

	// Change the background color of the cell
	document.getElementById('cell_1_' + questionID).bgColor = "#FFFFDD";
	document.getElementById('cell_2_' + questionID).bgColor = "#FFFFDD";
	
	errorsDisplayed=true;
}


// Clear existing flags, resetting the page back to normal
//
function clearAlerts() {
	// Cycle through all of the specified required questions
	for (i=0; i<requiredProfile.length; i++) {
		var which = requiredProfile[i] [0];
		var cell_1 = 'cell_1_' + which;
		var cell_2 = 'cell_2_' + which;
		eval('document.' + which + '.src="/images/clear.gif"');
		document.getElementById(cell_1).bgColor = "";
		document.getElementById(cell_2).bgColor = "";
	}
	
	errorsDisplayed=false;
}