//version 1.1
//this javascript code is used to validate the form
//the form is also validated server side in case javascript is disabled or circumvented
//if changes are made to the validation process on the server side, they must also be made here
//the best way to fix this would be to combine it all on the server side using hijax

//the return value boolean - determines whether or not the form will be submitted
var rVal = true;

//all the inputs, textareas and selects on the form
var theFields;

//adding a trim function - http://javascript.crockford.com/remedial.html
String.prototype.trim = function () {
	return this.replace(/^\s*(\S*(\s+\S+)*)\s*$/, "$1");
};

//autotabbing function for phone number fields
//put this on onkeyup event
//maxlength must be specified
function autoTab(currentField, nextField) {
	if(currentField.value.length == currentField.maxLength) {
		nextField.focus();
	}
}

//function to clear form (reset to original state)
//called when reset is pressed
function clearAll() {
	//reset all fields manually
	//start at 1 to skip hidden field
	
	for(var i = 1; i < document.forms[0].length; i++) {
		if((document.forms[0].elements[i].type == "text") || (document.forms[0].elements[i].type == "textarea")) {
			
			document.forms[0].elements[i].value = "";
			
		} else if(document.forms[0].elements[i].type == "select-one") {
			document.forms[0].elements[i].selectedIndex = 0;
		} else if(document.forms[0].elements[i].name == "sources[]") {
			document.forms[0].elements[i].checked = false;
		}
	}
	
	document.forms[0].elements.warranty[1].checked = true;
	document.forms[0].elements.exchange[1].checked = true;
	document.forms[0].elements.reported[1].checked = true;
	
	resetBackgrounds();
	agencyEnabled(false);
	document.forms[0].elements.category.focus();
	
	return false; //if the form tries to clear itself, it will reinsert all the POST values
}

//enables/disables the fields below "reported yes/no"
function agencyEnabled(enableMe) {
	document.forms[0].elements.agency.disabled = !enableMe;
	document.forms[0].elements.agencycity.disabled = !enableMe;
	document.forms[0].elements.agencystate.disabled = !enableMe;
	document.forms[0].elements.submonth.disabled = !enableMe;
	document.forms[0].elements.subday.disabled = !enableMe;
	document.forms[0].elements.subyear.disabled = !enableMe;
	document.forms[0].elements.filenum.disabled = !enableMe;
}

//takes a field and trims its value
function trimField(theField) {
	theField.value = theField.value.trim();
}

//checks the specified field for length
//maxLength specifies the maximum allowed length as an integer
function checkLength(theField, maxLength) {
	if(theField.value.length > maxLength) {
		theField.value = theField.value.substring(0, maxLength);
	}
}

//highlights the input and sets rVal to false
function invalidate(theInput) {
	theInput.style.backgroundColor = "yellow";
	rVal = false;
}

//checks to see if a field has text input other than whitespace
//calls invalidate() if true
function checkField(theField) {
	trimField(theField);
	if(theField.value == "") {
		invalidate(theField);
	} else {
		theField.style.backgroundColor = "#FFF";
	}
}

//check to make sure that at least one source is checked
function checkSources() {
	var theSources = document.getElementsByName("sources[]");
	var theLabel = document.getElementById("sourcesTried");
	if(!(theSources[0].checked || theSources[1].checked)) {
		rVal = false;
		theSources[0].parentNode.style.backgroundColor = "yellow";
		theSources[1].parentNode.style.backgroundColor = "yellow";
	} else {
		theSources[0].parentNode.style.backgroundColor = "";
		theSources[1].parentNode.style.backgroundColor = "";
	}
}

//check category and other fields
function checkCatOther(theField) {
	if(theField.name == "category") {
		checkField(theField);
		
		if(theField.value == "Other") {
			document.forms[0].elements.other.disabled = false;
			document.forms[0].elements.other.focus();
		} else {
			document.forms[0].elements.other.style.backgroundColor = "";
			document.forms[0].elements.other.disabled = true;
		}
	} else if(theField.name == "other") {
		if(document.forms[0].elements.category.value == "Other") {
			checkField(theField);
		} else {
			theField.style.backgroundColor = "";
		}
	}
}

//check for partnum and/or part desc
function checkPartnumDesc(theField) {
	if((theField.name == "partnum") || (theField.name == "desc")) {
		trimField(theField);
		if((document.forms[0].elements.partnum.value == "") && (document.forms[0].elements.desc.value == "")) {
			invalidate(theField);
		} else {
			document.forms[0].elements.partnum.style.backgroundColor = "#FFF";
			document.forms[0].elements.desc.style.backgroundColor = "#FFF";
		}
	}
}

//checks the agency date
//similar to checkPhone function
function checkAgencyDate() {
	var theMonth = document.forms[0].elements.submonth;
	var theDay = document.forms[0].elements.subday;
	var theYear = document.forms[0].elements.subyear;
	
	if((theMonth.value != "") || (theDay.value != "") || (theYear.value != "")) {
		
		if(theMonth.value == "") {
			invalidate(theMonth);
		} else {
			theMonth.style.backgroundColor = "#FFF";
		}
		
		if(theDay.value == "") {
			invalidate(theDay);
		} else {
			theDay.style.backgroundColor = "#FFF";
		}
		
		if(theYear.value == "") {
			invalidate(theYear);
		} else {
			theYear.style.backgroundColor = "#FFF";
		}
		
	} else {
		theMonth.style.backgroundColor = "#FFF";
		theDay.style.backgroundColor = "#FFF";
		theYear.style.backgroundColor = "#FFF";
	}
}

//checks the phone number pattern for the 3 fields
//parameter is the root name of the fields to check
//for example: "subPhone" for subPhone1, subPhone2 and subPhone3
function checkPhone(phoneName) {
	eval("var p1 = document.forms[0].elements."+phoneName+"1;");
	eval("var p2 = document.forms[0].elements."+phoneName+"2;");
	eval("var p3 = document.forms[0].elements."+phoneName+"3;");
	
	trimField(p1);
	trimField(p2);
	trimField(p3);
	
	if((p1.value != "") || (p2.value != "") || (p3.value != "")) {
		
		if(!p1.value.match(/\d{3}/)) {
			invalidate(p1);
		} else {
			p1.style.backgroundColor = "#FFF";
		}
		
		if(!p2.value.match(/\d{3}/)) {
			invalidate(p2);
		} else {
			p2.style.backgroundColor = "#FFF";
		}
		
		if(!p3.value.match(/\d{4}/)) {
			invalidate(p3);
		} else {
			p3.style.backgroundColor = "#FFF";
		}
	} else {
		p1.style.backgroundColor = "#FFF";
		p2.style.backgroundColor = "#FFF";
		p3.style.backgroundColor = "#FFF";
	}
}

//checks to make sure the zip code is made up of 5 numbers
function checkZip(zipField) {
	trimField(zipField);
	if(zipField.value != "") {
		if(!zipField.value.match(/\d{5}/)) {
			invalidate(zipField);
		} else {
			zipField.style.backgroundColor = "#FFF";
		}
	} else {
		zipField.style.backgroundColor = "#FFF";
	}
}

//changes all the highlighting back to white
function resetBackgrounds() {
	for(var i=0; i < document.forms[0].length; i++) {
		if(document.forms[0].elements[i].style.backgroundColor == "yellow") {
			document.forms[0].elements[i].style.backgroundColor = "#FFF";
		} else if(document.forms[0].elements[i].name == "sources[]") {
			document.forms[0].elements[i].parentNode.style.backgroundColor = "";
		}
	}
}

//function to validate the form before sending
function validate() {
	rVal = true; //reset return value
	
	//trim all extra white space
	for(var i=0; i < document.forms[0].elements.length; i++) {
		if((document.forms[0].elements[i].type == "text") || (document.forms[0].elements[i].type == "textarea")) {
			trimField(document.forms[0].elements[i]);
		}
	}
	
	//change everything back to white
	resetBackgrounds();
	
	//check the required fields
	checkCatOther(document.forms[0].elements.category);
	checkCatOther(document.forms[0].elements.other);
	checkField(document.forms[0].elements.brand);
	checkField(document.forms[0].elements.model);
	checkPartnumDesc(document.forms[0].elements.partnum);
	checkPartnumDesc(document.forms[0].elements.desc);
	checkField(document.forms[0].elements.month);
	checkField(document.forms[0].elements.year);
	checkSources();
	
	//check the nonrequired fields for accuracy
	checkAgencyDate();
	checkPhone("ownerPhone");
	checkPhone("subPhone");
	checkZip(document.forms[0].elements.zip);
	
	//if validation failed, inform the user
	if(!rVal) {
		alert("The form could not be submitted as is.\nPlease make sure all required fields are completed\nand all fields are filled out properly.");
	}
	
	return rVal;
}

//highlights the parent of the input
function highlight(theField) {
	//clear all other highlights first
	for(var i=0; i<theFields.length; i++) {
		if((theFields[i].name == "reported") || (theFields[i].name == "submonth") 
			|| (theFields[i].name == "subday") || (theFields[i].name == "subyear")) {
			
			theFields[i].parentNode.parentNode.style.backgroundColor = "";
		} else if(theFields[i].name == "sources[]") {
			document.getElementById("sourcesTried").style.backgroundColor = "";
		} else {
			theFields[i].parentNode.style.backgroundColor = "";
		}
	}
	
	if(theField.name == "sources[]") {
		document.getElementById("sourcesTried").style.backgroundColor = "#e1e1e1";
	} else {
		if((theField.name == "reported") || (theField.name == "submonth") 
			|| (theField.name == "subday") || (theField.name == "subyear")) {
			
			theField = theField.parentNode;
		}
		theField.parentNode.style.backgroundColor = "#e1e1e1";
	}
}

//calls the function to set the fields enabled or disabled
//prepares the form
function start() {
	if(document.forms[0].elements.reported[0].checked == true) {
		agencyEnabled(true);
	} else {
		agencyEnabled(false);
	}
	
	if(document.forms[0].elements.category.value == "other") {
		document.forms[0].elements.other.disabled = false;
	} else {
		document.forms[0].elements.other.disabled = true;
	}
	
	//create an array storing all the fields
	theFields = new Array();
	
	var theInputs = document.forms[0].getElementsByTagName("input");
	for(var i=0; i<theInputs.length; i++) {
		if((theInputs[i].getAttribute("type") != "reset") && (theInputs[i].getAttribute("type") != "submit")) {
			theFields.push(theInputs[i]);
		}
	}
	
	var theSelects = document.forms[0].getElementsByTagName("select");
	for(var i=0; i<theSelects.length; i++) {
		theFields.push(theSelects[i]);
	}
	
	var theTextareas = document.forms[0].getElementsByTagName("textarea");
	for(var i=0; i<theTextareas.length; i++) {
		theFields.push(theTextareas[i]);
	}
	
	//attach event handlers for onfocus
	for(var i=0; i<theFields.length; i++) {
		theFields[i].onfocus = function() {highlight(this);};
	}
	
	//start at the top
	document.forms[0].elements.category.focus();
}

window.onload = start;