// Form validation
var illegalChars = /[\(\)\<\>\,\;\:\\\"\[\]\/\#\*\%\&\$\!\~\=\+]/;

// ******************************
// validates if check box checked
// ******************************
function validateCheckbox(check) {
	if (check.checked == false) {
		return false;
	}
	else {
		return true;
	}
}

function checkAllCheckBoxes(checkBoxSet, alertMessage) {
	for (var i=0; i<checkBoxSet.length; i++) {
		var check1 = validateCheckbox(checkBoxSet[i]);
		if (check1 == true) { 
			alertMessage = "";
			break;
		}
	}
	return alertMessage;
}

// *******************************
// validates if a radio is checked
// *******************************
function validateRadio(check) {
	if (check.checked == false) {
		return false;
	}
	return true;
}

function checkAllRadioButtons(radioButtonSet, alertMessage) {
	for (var i=0; i<radioButtonSet.length; i++) {
		var radio1 = validateRadio(radioButtonSet[i]);
		if (radio1 == true) {
			alertMessage = "";
			break;
		}
	}
	return alertMessage;
}


// validates name
function validateTextAndEmpty(fld,desc) {
	var error = "";
	
	if (fld.value == "" || fld.value == null) {
		fld.style.background = 'yellow';
		error = "Please provide your "+desc+"\n";
		fld.focus();
	}
	else if (fld.value.match(illegalChars)) {
		fld.style.background = 'yellow';
		error = "Your "+desc+" name contains illegal character(s)\n";
		fld.focus();
	} else {
		fld.style.background = 'white';
	}
	return error;

}

function validateDropDown(fld,desc) {
	if (fld.selectedIndex == 0) {
		error = "Please select "+desc+"\n";
		fld.focus();
	}
	else { error = ""; }
	return error;
}



// validates computer description
function validateDescription(fld) {
	var error = "";
	
	if (fld.value == "" || fld.value == null) {
		fld.style.background = 'yellow';
		error = "Please provide a description for this computer";
		fld.focus();
	}
	else if (fld.value.match(illegalChars)) {
		fld.style.background = 'yellow';
		error = "Description field contains illegal character(s)";
		fld.focus();
	} else {
		fld.style.background = 'White';
	}
	return error;

}


// validates email address
function trim(s) {
	return s.replace(/^\s+|\s+$/, '');
}

function validateEmail(fld) {
	var error="";
	var tfld = trim(fld.value);
	var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/;
	//var emailFilter = /^.+@.+\..{2,3,4,6}$/ ;
	
	if (fld.value == "" || fld.value == null) {
		fld.style.background = 'yellow';
		error = "Please provide an email address.\n";
		fld.focus();
	}
	else if (!emailFilter.test(tfld)) {
		fld.style.background = 'yellow';
		error = "Please enter a valid email address.\n";
		fld.focus();
	}
	else if (fld.value.match(illegalChars)) {
		fld.style.background = 'yellow';
		error = "The email address contains illegal character(s).\n";
		fld.focus();
	}
	else {
		fld.style.background = 'white';
	}
	return error;
}

function validateEmailMatch(fld,fld2) {
	var error="";
	var tfld = trim(fld.value);
	if (fld.value != fld2.value) {
		fld.style.background = fld2.style.background = 'yellow';
		error = "Emails do not match\n";
		fld2.focus();
	} else if (fld2.value == "") {
		fld.style.background = fld2.style.background = 'yellow';
		error = "Please re-type your email\n";
		fld2.focus();
	} else {
		fld.style.background = fld2.style.background = 'white';
	}
	return error;
}

// validates letters
function validateSerial(fld,fieldNum) {
	var error = "";
	//var illegalChars = /\W/; // allow letters, numbers, and underscores
	var llegalChars = /^[a-zA-Z]+$/; // allow numbers only
	var illegalChars = /[\W_]/; // allow letters and numbers

	if (fld.value == "") {
	    fld.style.background = 'Yellow';
		fld.focus();
	    error = "Please provide a serial number for field " +fieldNum+".\n";
	} else if (fld.value.length < 4) {
	    fld.style.background = 'Yellow'; 
		fld.focus();
	    error = "Required minimum of 4 characters " +fieldNum+".\n";
	} else if (illegalChars.test(fld.value)) {
	    fld.style.background = 'Yellow'; 
		fld.focus();
	    error = "The serial contains illegal character(s) " +fieldNum+".\n";
	// } else if (!fld.value.match(llegalChars)) {
	// 	fld.style.background = 'Yellow';
	// 	fld.focus();
	// 	error = "The serial requires Alpha characters only.\n";
	} else {
	    fld.style.background = 'White';
	} 
	return error;
}


