/*
 * $History: surveyform.js $
 * 
 * *****************  Version 4  *****************
 * User: Alaing       Date: 11/12/02   Time: 6:03p
 * Updated in $/survey app/source code/web
 * LCN 3103; change validatation of Brands and Media
 * 
 * *****************  Version 3  *****************
 * User: Alaing       Date: 11/11/02   Time: 6:28p
 * Updated in $/survey app/source code/web
 * LCN 3103; change validation of all "Other" fields
 * 
 * *****************  Version 2  *****************
 * User: Alaing       Date: 11/08/02   Time: 5:03p
 * Updated in $/survey app/source code/web
 * LCN 3101; fix vehicle model year and trim blanks off all text fields
 * 
 * *****************  Version 1  *****************
 * User: Alaing       Date: 9/11/02    Time: 11:49a
 * Created in $/survey app/source code/web
 * 
 *
 * $Revision: 4 $
 * $Date: 11/12/02 6:03p $
 * $Author: Alaing $
 */

function validateForm() {

	var form = document.surveyForm;

	for (j = 0; j < form.elements.length; j++) {
		if (form.elements[j].type == "text") {
			form.elements[j].value = Trim(form.elements[j].value);
		}
	}
	if ((form.tread.value == "none selected" || form.tread.value == "other") && form.other_tread.value == "") {
		alert("Please select or enter a tread.");
		form.tread.focus();
		return false;
	}
	else if (validateMiles(form.tire_miles) == true && 
		validatePurchaseCount(form.purchase_count) == true &&
		validateCarYear(form.car_year) == true && 
		validatePurchaseDate(form.purchased_month, form.purchased_year) == true &&
		validatePurchaseDateComplete(form.purchased_month, form.purchased_year) == true ) {
		return true;
	}
	else {
		return false;
	}
}



function eraseRow(radioGroup) {
	for (var j = 0; j < radioGroup.length; j++) {
		radioGroup[j].checked = false;
		radioGroup[j].value = "";
	}
}



function clearOtherRows(buttonGroup, columnName) {
	for (var j = 0; j < buttonGroup.length; j++) {
		if (buttonGroup[j].value == columnName) {
			buttonGroup[j].checked = false;
			break;
		}
	}
}



function clearColumn(columnName, excludingRowName) {
	var form = document.surveyForm;
	
	if (excludingRowName != 'importance1') {
		clearOtherRows(form.importance1, columnName);
	}
	
	if (excludingRowName != 'importance2') {
		clearOtherRows(form.importance2, columnName);
	}
	
	if (excludingRowName != 'importance3') {
		clearOtherRows(form.importance3, columnName);
	}
	
	if (excludingRowName != 'importance4') {
		clearOtherRows(form.importance4, columnName);
	}
	
	if (excludingRowName != 'importance5') {
		clearOtherRows(form.importance5, columnName);
	}
	
	if (excludingRowName != 'importance6') {
		clearOtherRows(form.importance6, columnName);
	}
	
	if (excludingRowName != 'importance7') {
		clearOtherRows(form.importance7, columnName);
	}
}



function validateYear(yearField, year2000Switch) {

	// this function checks the year in either 2 or 4 digit format and always
	// changes the field to the 4 digit year.

	var year;

	yearField.value = Trim(yearField.value);
	year = yearField.value;

	var pattern;
	var matchArray;

	pattern = /^\s*$/;			// is the field empty?
	matchArray = year.match(pattern);
	if (matchArray != null) {
		return false;
	}

	if (isNaN(year)) {	// is the field a number?
		alert("Date is not in a valid format. The valid format is YY or YYYY.");
		yearField.focus();
		return false;
	}

	pattern = /^((19|20)?\d{2})$/;
	matchArray = year.match(pattern);	// is the format YY or YYYY?
	if (matchArray == null)
	{
		alert("Date is not in a valid format. The valid format is YY or YYYY.");
		yearField.focus();
		return false;
	}



	if (year <= (year2000Switch - 2000)) { 
		year = "20" + year;
	}
	else if (year <= 99) {
		year = "19" + year;
	}



	yearField.value = year;

	return true;

}



function validateInt(intField) {

	var intValue = intField.value;

	var pattern;
	var matchArray;

	pattern = /^\s*$/;			// is the field empty?
	matchArray = intValue.match(pattern);
	if (matchArray != null) {
		return false;
	}



	if (isNaN(intValue)) {	// is the field a number?
		alert('"' + intValue + '" is not in a valid number.');
		intField.focus();
		return false;
	}


	return true;

}



function validateMiles(milesField) {

	if (milesField.value == "") {
		return true;
	}
	else if (validateInt(milesField)) {
		var miles = parseFloat(milesField.value);
		if (miles <= 0) {
			alert("Please enter a number greater than zero.");
			milesField.focus();
			return false;
		}
		else {
			milesField.value = miles;
		}
	}
	else {
		return false;
	}

	return true;
}



function validatePurchaseCount(purchField) {

	if (purchField.value == "") {
		return true;
	}
	else if (validateInt(purchField)) {
		var purch = parseInt(purchField.value, 10);
		if (purch <= 0 || isNaN(purch)) {
			alert("Please enter a number greater than zero.");
			purchField.focus();
			return false;
		}
		else {
			purchField.value = purch;
		}
	}
	else {
		return false;
	}

	return true;
}



function validateCarYear(carYearField) {

	// makes sure the model year of the car does not exceed more than 1 year past
	// today's date.

	carYearField.value = Trim(carYearField.value);
	var carYear = parseInt(carYearField.value);
	var todaysDate = new Date();
	var maxYear = todaysDate.getFullYear() + 2;	// allow for next 2 model years

	if (carYearField.value == "") {
		return true;
	}
	else if (validateYear(carYearField, maxYear)) {
		if (carYear > maxYear) {
			alert ("The vehicle model year must be less than or equal to " + maxYear);
			return false;
		}
	}

	return true;
}



function validatePurchaseDate(purchMonthField, purchYearField) {

	// makes sure the purchase year of the tires is not in the future.

	if (purchMonthField.value == "none selected" && purchYearField.value == "") {
		return true;
	}

	var purchYear;
	var purchMonth;
	var todaysDate = new Date();
	var maxYear = todaysDate.getFullYear();
	var maxMonth = todaysDate.getMonth() + 1;

	if (validateYear(purchYearField, maxYear)) {
		purchYear = parseInt(purchYearField.value);
		if (purchYear > maxYear) {
			alert ("The Purchase Date cannot be in the future.");
			purchYearField.focus();
			return false;
		}
		else if (purchYear < 1970) {	// why 1970? because java Date type only supports dates starting from Jan 1, 1970
			alert ("The Purchase Year must be greater than or equal to 1970.");
			purchYearField.focus();
			return false;
		}
		else if (purchYear == maxYear) {
			purchMonth = purchMonthField.value;
			if (purchMonth == "Jan")
				purchMonth = 1;
			else if (purchMonth == "Feb")
				purchMonth = 2;
			else if (purchMonth == "Mar")
				purchMonth = 3;
			else if (purchMonth == "Apr")
				purchMonth = 4;
			else if (purchMonth == "May")
				purchMonth = 5;
			else if (purchMonth == "Jun")
				purchMonth = 6;
			else if (purchMonth == "Jul")
				purchMonth = 7;
			else if (purchMonth == "Aug")
				purchMonth = 8;
			else if (purchMonth == "Sep")
				purchMonth = 9;
			else if (purchMonth == "Oct")
				purchMonth = 10;
			else if (purchMonth == "Nov")
				purchMonth = 11;
			else if (purchMonth == "Dec")
				purchMonth = 12;

			if (purchMonth > maxMonth) {
				alert ("The Purchase Date cannot be in the future.");
				purchMonthField.focus();
				return false;
			}
		}
		// else purchYear < maxYear so the month does not matter
		
	}

	
	return true;
}



function validateTextBox(textField) {
	textField.value = Trim(textField.value);

	var text = textField.value;
	var len = text.length;
	if (len > 2000) {
		alert ("Please restrict the comments to less than 2,000 characters.");
		textField.focus();
		return false;
	}

	return true;
}



function validatePurchaseDateComplete(purchMonthField, purchYearField) {

	// make sure both month and year fields are either both completed or both empty

	if (	(purchMonthField.value == "none selected" && purchYearField.value != "")
		|| 
		(purchMonthField.value != "none selected" && purchYearField.value == "")
		) {

		alert("Please enter both the purchase month and year.");
		if (purchMonthField.value == "none selected")
			purchMonthField.focus();
		else
			purchYearField.focus();
		return false;
	}

	return true;

}



function validateTread() {
	var form = document.surveyForm;

	if (form.tread.value != "none selected" && form.tread.value != "other") {
		form.other_tread.value = "";
	}
}



function validateOtherTread() {
	var form = document.surveyForm;

	form.other_tread.value = Trim(form.other_tread.value);

	if (form.other_tread.value != "") {
		form.tread.value = "other";
	}
}



function validatePurchasedFrom() {
	var form = document.surveyForm;

	for (var j = 0; j < (form.purchased_from.length - 1); j++) {
		if (form.purchased_from[j].checked == true) {
			form.purchased_from_other.value = "";
			break;
		}
	}
}



function validatePurchasedFromOther() {
	var form = document.surveyForm;

	form.purchased_from_other.value = Trim(form.purchased_from_other.value);

	if (form.purchased_from_other.value != "") {
		for (var j = 0; j < form.purchased_from.length; j++) {
			if (form.purchased_from[j].value == "other") {
				form.purchased_from[j].checked = true;
				break;
			}
		}
	}
}



function validateOtherBrandsAndMedia(other_checkbox, other_textbox, none_checkbox) {

	other_textbox.value = Trim(other_textbox.value);

	if (other_textbox.value != "") {
		other_checkbox.checked = true;
	}
	else {
		other_checkbox.checked = false;
	}

	none_checkbox.checked = false;
}



function validateBrandsAndMedia(none_checkbox) {
	none_checkbox.checked = false;
}



function clearCheckboxGroup(group_name, other_textbox, none_checkbox) {
	var form = document.surveyForm;
	var len = group_name.length;
	var checkbox;
	var cbName;
	var oldCheckboxChecked = none_checkbox.checked;

	for (var j = 0; j < form.elements.length; j++) {
		if (form.elements[j].type == "checkbox") {
			checkbox = form.elements[j];
			cbName = checkbox.name;
			if (cbName.substring(0, len) == group_name) {
				checkbox.checked = false;
			}
		}
	}

	none_checkbox.checked = oldCheckboxChecked;
	other_textbox.value = "";
}
