var WSErrorString1 = '';
var WSErrorString2 = '';

function WSValidateForm(theForm) {
	var err1 = null;
	var err2 = null;

	if (B_checkRequired)
		err1 = WSCheckRequired(theForm);

	if (B_checkFilters)
		err2 = WSCheckFilters(theForm);

	if (err1 != null) {
		alert(WSErrorRequired.replace(/%LABEL/, WSErrorString1));
		if (theForm.elements[WSFormInputs[err1]].type == 'text' ||
			theForm.elements[WSFormInputs[err1]].type == 'textarea' || 
			theForm.elements[WSFormInputs[err1]].type == 'password' || 
			theForm.elements[WSFormInputs[err1]].type == 'file' || 
			theForm.elements[WSFormInputs[err1]].type == 'select')
			theForm.elements[WSFormInputs[err1]].focus();
		return false;
	}
	if (err2 != null) {
		alert(WSErrorValid.replace(/%LABEL/, WSErrorString2));
		if (theForm.elements[WSFormInputs[err2]].type == 'text' ||
			theForm.elements[WSFormInputs[err2]].type == 'textarea' || 
			theForm.elements[WSFormInputs[err2]].type == 'password' || 
			theForm.elements[WSFormInputs[err2]].type == 'file' || 
			theForm.elements[WSFormInputs[err2]].type == 'select')
			theForm.elements[WSFormInputs[err2]].focus();
		return false;
	}

	return true;
}

function WSCheckRequired(theForm) {
	var t_str = '';
	var e_id = null;
	WSErrorString1 = '';

	for (var a = 0; a < WSFormRequired.length; a++) {
		theInput = theForm.elements[WSFormInputs[WSFormRequired[a]]];
		t_str = '';

		if (theInput.type == 'select-one' || theInput.type == 'select-multiple') {
			for (var b = 0; b < theInput.options.length; b++) {
				if (theInput.options[b].selected)
					t_str = t_str + theInput.options[b].value;
			}
		}
		else if (theInput.length > 1) {
			//else if (theInput.type == 'checkbox' || theInput.type == 'radio') {
			for (var b = 0; b < theInput.length; b++) {
				if (theInput[b].checked)
					t_str = t_str + theInput[b].value;
			}

		}
		else
			t_str = theInput.value;

		if (t_str.replace(/(^\s*|\s*$)/g, '').length == 0) {
			if (!e_id) e_id = WSFormRequired[a];
			WSErrorString1 += (WSErrorString1.length ? ', ' : '') + WSFormLabels[WSFormRequired[a]];
		}
	}

	return e_id;
}

function WSCheckFilters(theForm) {
	var t_str = '';
	var e_id = null;
	WSErrorString2 = '';

	for (var a = 0; a < WSFormInputs.length; a++) {
		theInput = theForm.elements[WSFormInputs[a]];
		t_str = '';

		if (theInput.type == 'select-one' || theInput.type == 'select-multiple') {
			for (var b = 0; b < theInput.options.length; b++) {
				if (theInput.options[b].selected) {
					t_str = theInput.options[b].value;
					if (t_str.length > 0 && t_str.search(WSFormFilters[a]) == -1)
						return a;
				}
			}
		}
		else if (theInput.length > 1) {
			for (var b = 0; b < theInput.length; b++) {
				if (theInput[b].checked) {
					t_str = theInput[b].value;
					if (t_str.length > 0 && t_str.search(WSFormFilters[a]) == -1)
						return a;
				}
			}

		}
		else {
			theInput.value = theInput.value.replace(/(^\s*|\s*$)/g, '');
			t_str = theInput.value;
			if (t_str.length > 0 && t_str.search(WSFormFilters[a]) == -1) {
				if (!e_id) e_id = a;
				WSErrorString2 += (WSErrorString2.length ? ', ' : '') + WSFormLabels[a];
			}
		}
	}

	return e_id;
}

function WSSetSelected(theInput, theValue) {
	for (var a = 0; a < theInput.options.length; a++) {
		if (theInput.options[a].value == theValue) {
			theInput.options[a].selected = true;
			break;
		}
	}
}

