// To check if all the fields in the form are correctly filled.
// Validation - Reports if all the fields are filled out correctly

function checkaddcar()
{
 if (testformaddcar()) return true;
 else return false;	
}		

function testformaddcar()
{

 /* check for email field */
 if (document.addcar.username.value == "")
 {
  alert("The email field is blank.\n\nPlease enter the email address we have on record.");
  document.addcar.username.focus();      
  return false;       
 }   

 // Return false if e-mail field does not contain a '@' and '.' .

 if (document.addcar.username.value.indexOf ('@',0) == -1 || document.addcar.username.value.indexOf ('.',0) == -1)
 {      
  alert("The email address you have entered is invalid.\n\nPlease re-enter the correct email address we have on record.");      		
  document.addcar.username.focus();      
  document.addcar.username.select();      
  return false;      
 }  	

 /* check for password field */

 var password = document.addcar.password.value;
 if (password == "")
 {
  alert("The password field is blank.\n\nPlease enter the password we have on record.");
  document.addcar.password.focus();
  return false;
 }

 var password = document.addcar.password.value;
 for (var i = 0; i < password.length; i++)
 {
  var ch = password.substring(i,i+1);
  if ((ch < "a" || "z" < ch) && (ch < "A" || "Z" < ch) && (ch != ' ') && (ch < "0" || "9" < ch))
  {         
   alert("The password you have entered is invalid.\n\nPlease re-enter the correct password we have on record.");         			
   document.addcar.password.focus();         
   document.addcar.password.select();         
   return false;         
  }      
 }

 var model = document.addcar.model.selectedIndex;
 if (document.addcar.model.options[model].value == '')
 {
  alert('Please select your car make');       				
  document.addcar.model.focus();
  return false;
 }

 var color = document.addcar.color.value;
 for (var i = 0; i < color.length; i++)
 {
  var ch = color.substring(i,i+1);
  if ((ch < "a" || "z" < ch) && (ch < "A" || "Z" < ch) && (ch != ' '))
  {         
   alert("The color field cannot contain numbers or special characters.\n\nPlease re-enter the color correctly.");         			
   document.addcar.color.focus();         
   document.addcar.color.select();         
   return false;         
  }      
 }

 var place = document.addcar.place.selectedIndex;
 if (document.addcar.place.options[place].value == '')
 {
  alert('Please select your city/town');       				
  document.addcar.place.focus();
  return false;
 }

 var vyear = document.addcar.vyear.selectedIndex;
 if (document.addcar.vyear.options[vyear].value == '')
 {
  alert('Please select the year of manufacture');       				
  document.addcar.vyear.focus();
  return false;
 }

 var km = document.addcar.km.selectedIndex;
 if (document.addcar.km.options[km].value == '')
 {
  alert('Please select the number of kilometres the car has run');       				
  document.addcar.km.focus();
  return false;
 }

 var particulars = document.addcar.particulars.value;
 for (var i = 0; i < particulars.length; i++)
 {
  var count = i;
  if (count > 254)
  {         
   alert("The description exceeds the permissible limit - please keep it brief.");         			
   document.addcar.particulars.focus();         
   document.addcar.particulars.select();         
   return false;         
  }      
 }

 // Return false if characters in price field are not 0-9

 var price = document.addcar.price.value;
 for (var i = 0; i < price.length; i++)
 {
  var ch = price.substring(i,i+1);
  if ((ch < "0") || ("9" < ch))
  {         
   alert("The price field has to be a whole number.\n\nPlease re-enter your minimum expected price correctly and without commas.");         			
   document.addcar.price.focus();         
   document.addcar.price.select();         
   return false;         
  }      
 }

 return true;
}

