/* QSI Corporation - Product Configure Form Javascript */

/*

elmCheckbox - the form <input /> element
strSectionId - the string id for the section
strGroupId - the string id for the group
nLimitAdjust - 0 if no affect on group limit, 1 if affects group limit
aNegates - array of items selecting this option will negate

*/

// this is all the shit that has to be done when a checkbox is clicked.
function updateConfigList(elmCheckbox, strSectionId, strGroupId, nLimitAdjust, aNegates) {
	/* check for section limits */
	var elmSectionLimit = document.getElementById(strSectionId + "limit");
	if (elmSectionLimit) {
		// check to see if we are not incrementing / decrementing the section count (a 1 for 1 option switch)
		var elmGroupCount = document.getElementById(strGroupId + "count");
		// if "1 for 1" option swap
		if (parseInt(elmGroupCount.value) == nLimitAdjust) {
			// nullify the limit adjust
			nLimitAdjust = 0;
		}
		// else either incrementing or decremenging
		else {
			var elmSectionCount = document.getElementById(strSectionId + "count");
			// if selecting the option would put us over the limit
			if (parseInt(elmSectionCount.value) + nLimitAdjust > parseInt(elmSectionLimit.value)) {
				// tell the user it aint no good
				alert("Only " + parseInt(elmSectionLimit.value) + " options from " + document.getElementById(strSectionId + "name").value + " may be selected.\nTo select this option, please deselect a different option.");
				// call if off like a bad relationship
				document.getElementById(strGroupId + (document.getElementById(strGroupId + "cur").value)).checked = true;
				elmCheckbox.checked = false;
				return false;
			}
		}
	}
	/* check for option negating */
	if (aNegates.length > 0) {
		// first check to see if any options that would be negated are selected
		var bWillNegate = false;
		for (var i = 0; i < aNegates.length; i++) {
			var strNegatedId = aNegates[i] + document.getElementById(aNegates[i] + "negated").value;
			if (document.getElementById(strNegatedId).checked == false) {
				bWillNegate = true;
				break;
			}
		}
		// if it will negate an option, warn first, allow for "cancel"
		if (bWillNegate) {
			// if they want to go ahead
			if (confirm("Selecting this option negates other selected option(s). Do you wish to continue?")) {
				for (var i = 0; i < aNegates.length; i++) {
					// select the "negated" option
					var strNegatedId = aNegates[i] + document.getElementById(aNegates[i] + "negated").value;
					document.getElementById(strNegatedId).checked = true;
					// update the "negated" group to current option
					document.getElementById(aNegates[i] + "cur").value = document.getElementById(strNegatedId).value;
					// update form highlighting
					for (var ii = 0; ii < document.getElementById(aNegates[i] + "total").value; ii++) document.getElementById(aNegates[i] + ii + "con").className = "configlistdis";
					document.getElementById(strNegatedId + "con").className = "configlistena";
					
				}
			}
			// else they are canceling
			else {
				document.getElementById(strGroupId + (document.getElementById(strGroupId + "cur").value)).checked = true;
				elmCheckbox.checked = false;
				return false;
			}
		}
		// else won't negate an option, keep on keeping on!
	}
	/* else ALL IS GOOD */
	// update group current
	document.getElementById(strGroupId + "cur").value = elmCheckbox.value;
	// update section / group counts
	if (elmSectionLimit) {
		elmSectionCount.value = parseInt(elmSectionCount.value) + nLimitAdjust;
		elmGroupCount.value = parseInt(elmGroupCount.value) + nLimitAdjust;
	}
	// update form highlighting
	for (var i = 0; i < document.getElementById(strGroupId + "total").value; i++) document.getElementById(strGroupId + i + "con").className = "configlistdis";
	document.getElementById(strGroupId + elmCheckbox.value + "con").className = "configlistena";
	// we've passed the test and done it YIPPE!
	return true;
}

// check the form for errors before submitting
function checkConfigureForm(form) {
	var errorStr = "";
	if (!form.ctannualvolume.value) errorStr += "Please enter your estimated annual volume.\n";
	if (!form.ctapplication.value) errorStr += "Please enter a short description of your terminal\'s application.\n";
	if (!form.contactname.value) errorStr += "Please enter your name.\n";
	if (!form.contactcompany.value) errorStr += "Please enter your company.\n";
	if (!form.contactstate.value) errorStr += "Please enter your state/province.\n";
	if (!form.contactcountry.value) errorStr += "Please enter your country.\n";
	if (!form.contactphone.value) errorStr += "Please enter your phone number.\n";
	else {
		var strippedphone = form.contactphone.value.replace(/[\(\)\.\-\ ]/g, '');
		if (isNaN(parseInt(strippedphone))) errorStr += "Please re-enter your phone number using only numbers.\n";
	}
	if (!form.contactemail.value) errorStr += "Please enter an email address.\n";
	else {
		var emailFilter = /^.+@.+\..{2,3}$/;
		if (!(emailFilter.test(form.contactemail.value))) errorStr += "Please enter a valid email address.\n";
		else {
			var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/;
			if (form.contactemail.value.match(illegalChars)) errorStr += "( The email address you entered contains illegal characters. )\n";
		}
	}
	if (errorStr != "") {
		alert (errorStr);
		return false;
	}
	return true;
}
