/*
 *	Client-side JavaScript routines for FertRec application.
 */
 
//alert('JavaScript loaded.');

/*	Validation routine for stage 3 form input. */
function validate_3() {
	var returnValue = true;
	var bWarn = false;
	var form = document.mmpCalc;
	var sMsg = "Error!\n", sWarn = "Warning!\n";
	var rowCount = setRowCount();	// Determine number of rows to validate
	
	form.stage.value = 3;
	
	// Check that at least 1 field is being processed
	if (rowCount == 0) {
		returnValue = false;
		sMsg = sMsg + "No field IDs entered.  Field must have an ID to be processed.\n";
	}
	
	// Check that all FieldID/SubfieldID combinations are unique
	var field = new Array(rowCount);
	var subfield = new Array(rowCount);
	var isUnique = true;
	var i, j, rowId;
	for (i = 0; i < field.length; i++) {
		rowId = getRowId(i + 1);
		field[i] = eval('form.field_' + rowId + '.value');
		subfield[i] = eval('form.subfield_' + rowId + '.value');
		for (j = i - 1; j >= 0; j--) {
			if (field[j] == field[i] && subfield[j] == subfield[i]) {
				isUnique = false;
				j = -1;
				i = field.length;
			}
		}
	}
	if (!isUnique) {
		returnValue = false;
		sMsg = sMsg + 'Field ID/Subfield combinations must be unique.\n';
	}
	
	// Display error if applicable; otherwise show warning and confirm whether to proceed.
	if (!returnValue) { alert(sMsg); }
	else if (bWarn) {
		sWarn = sWarn + "Proceed anyway?";
		returnValue = confirm(sWarn);
	}
	
	return returnValue;
}

/*	Validation routine for stage 4 form input. */
function validate_4() {
	var returnValue = true;
	var bWarn = false;
	var form = document.mmpCalc;
	var sMsg = "Error!\n", sWarn = "Warning!\n";
	var rowCount = form.rowCount.value;
		
	form.stage.value = 4;
	
	for (var i = 0; i < rowCount; i++) {
	}
	
	if (!returnValue) { alert(sMsg); }
	else if (bWarn) {
		sWarn = sWarn + "Proceed anyway?";
		returnValue = confirm(sWarn);
	}
	
	return returnValue;
}

/*	Null validation routine for stage 5 form input. */
function validate_5() {
	return true;
}

function print() {
	var form = document.mmpCalc;
	
	form.stage.value = 5;
	form.submit();
	
	return true;
}
