// Perform client-side validation to quickly give users errors--
// of course, perform server-side as well
//
// 14-Nov-06: jglouie: initial revision
// 13-Jun-07: rriego: revision to correct $error
// 27-Jun-07: jglouie: changed name of price to purchaseprice

function isNumber(str) { 
isprice = /^\d+\.\d{2}$/; 
return isprice.test( str ); 
} 

var errors = new Array();

function validate() {
	var errorlist = document.getElementById("errors");
	errorlist.innerHTML = ""; // clear out the old error list
	errors = new Array(); // clear out the old errors
	
	// check for form errors
	
	// all of the personal form elements...
	if (document.warrantyform.firstname.value == "") { addError("enter a first name"); }
	if (document.warrantyform.lastname.value == "") { addError("enter a last name"); }
	if (document.warrantyform.address.value == "") { addError("enter an address"); }
	if (document.warrantyform.city.value == "") { addError("enter a city"); }

	if (document.warrantyform.zipcode.value == "") { addError("enter a zip code"); }
	if (document.warrantyform.homephone.value == "") { addError("enter a home phone"); }
	if (document.warrantyform.workphone.value == "") { addError("enter a work phone"); } 
	if (document.warrantyform.email.value == "") { addError("enter an email address"); }

	// all of the product information elements
	if (document.warrantyform.purchaseplace.value == "") { addError("enter a purchase place"); }
	if (document.warrantyform.dateofpurchase.value == "") { addError("enter a purchase date"); }
	if (document.warrantyform.vehiclemake.value == "") { addError("enter a vehicle make"); }
	if (document.warrantyform.vehiclemodel.value == "") { addError("enter a vehicle model"); }
	if (document.warrantyform.purchaseprice.value == "") { addError("enter a purchase price"); }
	if (!isNumber(document.warrantyform.purchaseprice.value )) {addError("please enter a price in the form xxxx.xx."); }
	if (document.warrantyform.serialnum1.value == "") { addError("enter a serial number"); }
	
	// ensure only a single model is selected
	var numModels = 0;
	if (document.warrantyform.compustarmodel.value != "--") { numModels++; }
	if (document.warrantyform.compustarpromodel.value != "--") { numModels++; }
	if (document.warrantyform.arcticstartmodel.value != "--") { numModels++; }
	if (document.warrantyform.vizionmodel.value != "--") { numModels++; }
	if (document.warrantyform.nustartmodel.value != "--") { numModels++; }
	
	if (numModels != 1) { addError("select a single model"); }
	
	// display the error message to the user
	if (errors.length > 0) {
		alert("Sorry, but some required information is missing.  Please correct and resubmit.");
		
		// format the error list in HTML
		errorlist.innerHTML = "<p>Please correct the following:<br/>";

		for (var i = 0; i < errors.length; i++) {
			errorlist.innerHTML += ("-- " + errors[i] + "<br/>");
		}
		errorlist.innerHTML += "<br/></p>";
		
		return false;
	}
	
	return true;
}

function addError(message) {
	errors.push(message);
}

