//global array that holds all error message information
var errorlist = new Array(0);
function insertErrorValue(strFieldName, msg) {
	//appends the error message listed to the error array. strField name is required, but can be blank.
	var strErrorHTML;
	if (strFieldName=='') {
		strErrorHTML = "<p><span class='alert'>" + msg + "</span></p>";
	} else {
		var strFieldLabel = strFieldName;
		if (strFieldName.substr(strFieldName.length - 1, 1) != ":") {
			strFieldLabel += ":";
		}
		strErrorHTML = "<p><span class='alert bold'>"+strFieldLabel +"&nbsp;&nbsp;</span><span class='alert'>" + msg + "</span></p>";
	}
	if (errorlist[0] == '') {
		errorlist[0] = strErrorHTML;
	} else {
		errorlist[errorlist.length] = strErrorHTML;
	}
}
function resetErrorValue(errorArray) {
	//truncates the error array and removes the first element
	//reinitializes the array for a new validation pass
	errorArray.length = 1;
	errorArray[0] = '';
}
function showErrorBlock(boolShow, arrError, form) {
if (form != "") {
	/*
	boolShow:		if true, show error block and disable submit button
	arrError:		array of error message(s) to show (errorlist)
	*/
	var error = parent.document.getElementById('error_block');
	var error_msg = parent.document.getElementById('error_msg');
	var error_exp = parent.document.getElementById('error_expand');
	var error_col = parent.document.getElementById('error_collapse');
	//var sub_btn = document.getElementById('process_button');
	var i;
	var strError = '';
	if (boolShow) {
		if (arrError.length < 6) {
			//we have 5 or less errors, so show 'simple' bar
			for (i=0; i < arrError.length; i++) {
				strError = strError + arrError[i];
			}
			parent.document.getElementById('error_msg').innerHTML = strError;
			error_exp.style.display = 'none';
			error_col.style.display = 'none';
		} else {
			//greater than 5 errors, show collapsible error list
			var strRest = '<div id="error_gt_five" style="display: none;">'
			//insert two blocks (divs), 1 with first 5 errors, another with rest
			for (i=0; i < 5; i++) {
				strError = strError + arrError[i];
			}
			for (i=5; i < arrError.length; i++) {
				strRest = strRest + arrError[i];
			}
			parent.document.getElementById('error_msg').innerHTML = strError + strRest + '</div>';
			error_col.style.display = 'none';
			error_exp.style.display = 'block';
		}
		error.style.display = 'block';
		location.href="#ErrorBarAnchor";
	} else {
		//hide the entire list and allow submit
		error.style.display = 'none';
		error_exp.style.display = 'none';
		error_col.style.display = 'none';
	}
} else {
	var error = document.getElementById('error_block');
	var error_msg = document.getElementById('error_msg');
	var error_exp = document.getElementById('error_expand');
	var error_col = document.getElementById('error_collapse');
	var i;
	var strError = '';
	if (boolShow) {
		if (arrError.length < 6) {
			//we have 5 or less errors, so show 'simple' bar
			for (i=0; i < arrError.length; i++) {
				strError = strError + arrError[i];
			}
			document.getElementById('error_msg').innerHTML = strError;
			error_exp.style.display = 'none';
			error_col.style.display = 'none';
		} else {
			//greater than 5 errors, show collapsible error list
			var strRest = '<div id="error_gt_five" style="display: none;">'
			//insert two blocks (divs), 1 with first 5 errors, another with rest
			for (i=0; i < 5; i++) {
				strError = strError + arrError[i];
			}
			for (i=5; i < arrError.length; i++) {
				strRest = strRest + arrError[i];
			}
			document.getElementById('error_msg').innerHTML = strError + strRest + '</div>';
			error_col.style.display = 'none';
			error_exp.style.display = 'block';
		}
		error.style.display = 'block';
		window.scrollTo(0,0);
	} else {
		//hide the entire list and allow submit
		error.style.display = 'none';
		error_exp.style.display = 'none';
		error_col.style.display = 'none';
	}
}
} 
function expandErrorList(boolExpand) {
	//this is the function called from the top bar of the error list, that expands/collapses the list
	var error_col = document.getElementById('error_collapse');
	var error_exp = document.getElementById('error_expand');
	var error_sup = document.getElementById('error_gt_five');
	if (boolExpand) {
		error_sup.style.display = 'block';
		error_col.style.display = 'block';
		error_exp.style.display = 'none';
	} else {
		error_sup.style.display = 'none';
		error_col.style.display = 'none';
		error_exp.style.display = 'block';
	}
}
function setFieldErrorHighlight(fld, type) {
	/*
	This function handles setting the 'pink' highlighting of fields that fail validation.
	*/
	var errcolor = '#FFD0D0';
	var fieldtype = type.toLowerCase();
	switch (fieldtype) {
	case 'radio' :
		for(i=0; i < fld.length; i++) {
			fld[i].style.backgroundColor = errcolor;
		}
		break;
	case 'checkbox' :
		for(i=0; i < fld.length; i++) {
			fld[i].style.backgroundColor = errcolor;
		}
		break;
	default :
		switch (fld.name.toUpperCase()) {
			//this switch is to handle editable fields that are not validated in themselves
			//this allows us to highlight the editable ones
			default:
				//normally, the below line will handle all highlighting
				fld.style.backgroundColor = errcolor;
		}
	}
}
function setChkFieldErrorHighlight(fld, type, label_name) {
	/*
	This function handles setting the 'pink' highlighting of checkbox fields that fail validation.
	Requires a label surrounding the checkbox field.
	*/
	var errcolor = '#FFD0D0';
	var fieldtype = type.toLowerCase();
	var hdlLabel = document.getElementById(label_name);
	switch (fieldtype) {
	case 'checkbox' :
		for(i=0; i < fld.length; i++) {
			fld[i].style.backgroundColor = errcolor;
		}
		hdlLabel.style.backgroundColor = errcolor;
		break;
	default :
	
	}
}
function resetErrorHighlights(form) {
	/*
	This function clears all background colors from form elements. 
	Alteration of the colors may need to be made on non-white forms.
	*/
	var element;
	var state;
	var elName;
	for(i=0; i < form.elements.length; i++) {
		state = 'White';
		element = form.elements[i];
		elName = element.tagName.toLowerCase();
		
		if (elName == 'input') {
			etype=element.type.toLowerCase();
			if ((etype=='submit' || etype=='reset' || etype=='button') && state=='White') {
				//buttons' background should be silver, not white
				state='';
			}
			if (etype=='radio') {
				element.style.backgroundColor = ''; 
			} else {
				element.style.backgroundColor = setFieldResetColor(element, "reqfield", "#FFFFB7", state); 
			}
		} else {
			element.style.backgroundColor = setFieldResetColor(element, "reqfield", "#FFFFB7", state); 
		}
	}
}
function resetLabelErrorHighlights(arrLabel) {
	var state;
	var element;
	for(i=0; i < arrLabel.length; i++) {
		state = 'White';
		element = document.getElementById(arrLabel[i]);
		element.style.backgroundColor = state; 
	}
}
function setFieldResetColor(field, className, classColor, normColor) {
	var cn=field.getAttribute('ClassName');
	if (cn=="" | cn==null) {
		var cn=field.getAttribute('class');
	}
	if (cn=="" | cn==null) {
		return normColor;
	} else {
		if (cn.indexOf(className) >= 0) {
			return classColor;
		} else {
			return normColor;
		}
	}
}
