/**
* Copyright: 2005 - this.year() SI Works Internet Solutions
* If you have come across this page, its cause you are snooping through code, and are generally a developer as such
* If you would like to use some of the code on this page, please simply email support@siworks.co.za and ask permission
* it would be much appreciated, as we have worked very hard on these fucntions and scripts
* 
* Validation code for adding a vehicle
* @version 1.0
* @author Greg Shiers, Jarratt Ingram (SI Works Internet)
* @copyright: SI Works Internet Solutions 2005 - 2007
* 
*/

var ownername 						= true,
	ownersurname 					= true,
	ownertelephone 					= true,
	owneremail						= true,
	owneragegroup					= false,
	ownerregion						= true,
	owneraddress					= false,
	vehiclebrand					= true,
	vehiclemodel					= true,
	vehicleyear						= true,
	vehiclemileage					= true,
	vehiclecolour					= true,
	vehiclefactoryextras			= false,
	vehiclenonfactoryextras			= false,
	vehicleaccident					= true,
	vehicleoutstandingsettlement	= true,
	amountoffered					= false,
	dealershipoffering				= false,
	comments						= false;
/**
* Validates the adding and editing of a new vehicle registration
* @param ( e ) which form to process this validation through
* @return N/A
* @author Greg Shiers
* @version 1.0
*/
function validate( e ){
	// Set the variables for the script
	var focus_el = null, 
	    msg = '', 
	    prefix = "There have been some errors with your input:\n";
	
	// Make sure that the title is selected
	e.ownername.value 						= e.ownername.value.trim();
	e.ownersurname.value 					= e.ownersurname.value.trim();
	e.ownertelephone.value 					= e.ownertelephone.value.trim();
	e.owneremail.value 						= e.owneremail.value.trim();
	e.owneragegroup.value 					= e.owneragegroup.value.trim();
	e.ownerregion.value 					= e.ownerregion.value.trim();
	e.owneraddress.value 					= e.owneraddress.value.trim();
	e.vehiclebrand.value 					= e.vehiclebrand.value.trim();
	e.vehiclemodel.value 					= e.vehiclemodel.value.trim();
	e.vehicleyear.value 					= e.vehicleyear.value.trim();
	e.vehiclemileage.value 					= e.vehiclemileage.value.trim();
	e.vehiclecolour.value 					= e.vehiclecolour.value.trim();
	e.vehiclefactoryextras.value 			= e.vehiclefactoryextras.value.trim();
	e.vehiclenonfactoryextras.value 		= e.vehiclenonfactoryextras.value.trim();
	e.amountoffered.value 					= e.amountoffered.value.trim();
	e.dealershipoffering.value 				= e.dealershipoffering.value.trim();
	e.comments.value 						= e.comments.value.trim();
	
	// Make sure that the first name is not empty
	if ( ownername && validation.empty ( e.ownername ) ) {
		msg += error[21];
		focus_el = focus_el || e.ownername;
	}
	// Make sure that the surname is not empty
	if ( ownersurname && validation.empty ( e.ownersurname ) ) {
		msg += error[22];
		focus_el = focus_el || e.ownersurname;
	}
	// Make sure that telephone field is filled in
	if ( ownertelephone && validation.empty ( e.ownertelephone ) ) {
		msg += error[1];
		focus_el = focus_el || e.ownertelephone;
	}
	// Make sure that phone number is correctly formatted
	if ( !validation.empty ( e.ownertelephone ) ) {
		if ( !validation.phone_number ( e.ownertelephone ) ) {
			msg += error[2];
			focus_el = focus_el || e.ownertelephone;
		}
	}
	// Make sure that email address is filled in
	if ( owneremail && validation.empty ( e.owneremail ) ) {
		msg += error[3];
		focus_el = focus_el || e.owneremail;
	}

	if ( !validation.empty ( e.owneremail ) ) {
		// Make sure that the email address is valid
		if ( !validation.email ( e.owneremail ) ) {
			msg += error[4];
			focus_el = focus_el || e.owneremail;
		}
	}
	// Make sure the age group is selected
	if ( owneragegroup && validation.selected ( e.owneragegroup ) ) {
		msg += error[31];
		focus_el = focus_el || e.owneragegroup;
	}
	// Make sure the region is selected
	if ( ownerregion && validation.selected ( e.ownerregion ) ) {
		msg += error[32];
		focus_el = focus_el || e.ownerregion;
	}
	// Make sure that owners address is filled in
	if ( owneraddress && validation.empty ( e.owneraddress ) ) {
		msg += error[33];
		focus_el = focus_el || e.owneraddress;
	}
	// Make sure the user selects a region
	if ( vehiclebrand && validation.selected ( e.vehiclebrand ) ) {
		msg += error[34];
		focus_el = focus_el || e.vehiclebrand;
	}
	// Make sure the vehicle model has been filled in.
	if ( vehiclemodel && validation.empty ( e.vehiclemodel ) ) {
		msg += error[35];
		focus_el = focus_el || e.vehiclemodel;
	}
	// Make sure the vehicle year is selected
	if ( vehicleyear && validation.empty ( e.vehicleyear ) ) {
		msg += error[36];
		focus_el = focus_el || e.vehicleyear;
	}
	// Make sure the vehicle mileage is filled in
	if ( vehiclemileage && validation.empty ( e.vehiclemileage ) ) {
		msg += error[38];
		focus_el = focus_el || e.vehiclemileage;
	}
	// Make sure the vehicle mileage is filled in
	if ( !validation.empty ( e.vehiclemileage ) ) {
		if ( !validation.numeric_with_spaces ( e.vehiclemileage ) ) {
			msg += error[39];
			focus_el = focus_el || e.vehiclemileage;
		}
	}
	// Make srue the vehicle colour is filled in
	if ( vehiclecolour && validation.empty ( e.vehiclecolour ) ) {
		msg += error[40];
		focus_el = focus_el || e.vehiclecolour;
	}
	// Make sure the vehicle facotry extras are filled in
	if ( vehiclefactoryextras && validation.empty ( e.vehiclefactoryextras ) ) {
		msg += error[41];
		focus_el = focus_el || e.vehiclefactoryextras;
	}
	// Make sure the vehicle NON facotry extras are filled in
	if ( vehiclenonfactoryextras && validation.empty ( e.vehiclenonfactoryextras ) ) {
		msg += error[42];
		focus_el = focus_el || e.vehiclenonfactoryextras;
	}
	// Make sure the accient radio is checked
	if ( vehicleaccident && !validation.radio ( e.vehicleaccident ) ) {
		msg += error[43];
		focus_el = focus_el || e.vehicleaccident[0];
	}
	// Make sure vehicle outsanding settlement is checked
	if ( vehicleoutstandingsettlement && !validation.radio ( e.vehicleoutstandingsettlement ) ) {
		msg += error[44];
		focus_el = focus_el || e.vehicleoutstandingsettlement[0];
	}
	// Make sure the amount offered is filled in
	if ( amountoffered && validation.empty ( e.amountoffered ) ) {
		msg += error[45];
		focus_el = focus_el || e.amountoffered;
	}
	// Make sure the dealership offered field is filled in
	if ( dealershipoffering && validation.empty ( e.dealershipoffering ) ) {
		msg += error[46];
		focus_el = focus_el || e.dealershipoffering;
	}
	// Make sure you add a comment
	if ( comments && validation.empty ( e.comments ) ) {
		msg += error[47];
		focus_el = focus_el || e.comments;
	}

	if ( msg != '' ) {
		alert(prefix+msg);
			if ( focus_el.focus ) { focus_el.focus(); }
		return false; // If there are errors, dont submit the form.
	}
	return true; // If there are no errors, go ahead and send the form through.
}

// ]]>