var errorMessage = ""
var error = 0

function validateForm(form) {
 errorMessage = "";
 error = 0;

 var password = document.getElementById("pword");
 var pwordConf = document.getElementById("pwordConf");

  validateNotNull(document.getElementById("personalEmail"), "Please supply an Email address");
  validateNotNull(document.getElementById("firstname"), "Please enter your first name");
  validateNotNull(document.getElementById("lastname"), "Please enter your Surname");
  validateNotNull(document.getElementById("contactName"), "Please enter your name");
  validateNotNull(document.getElementById("billingAddr1"), "Please supply the first line of your address");
  validateNotNull(document.getElementById("billingTown"), "Please supply your town");
  validateNotNull(document.getElementById("billingCounty"), "Please suppy your county/state");
  validateNotNull(document.getElementById("billingPostcode"), "Please supply your postcode/zip");
  validateNotNull(document.getElementById("deliveryAddr1"), "Please supply the first line of your address");
  validateNotNull(document.getElementById("deliveryTown"), "Please supply your town");
  validateNotNull(document.getElementById("deliveryCounty"), "Please suppy your county/state");
  validateNotNull(document.getElementById("deliveryPostcode"), "Please supply your postcode/zip");
  validateChecked(document.getElementById("tac"), "Please agree to the terms and conditions");
  validateNotNull(document.getElementById("enquiry"), "Please enter your enquiry details");
 
  if (password && pwordConf) {
  if (password.value != pwordConf.value){
  error ++;
  errorMessage += "Passwords do not match, please try again" + "\r\n";
  password.style.backgroundColor = '#F5DFD8';
  pwordConf.style.backgroundColor = '#F5DFD8';
  password.value = ""
  pwordConf.value = ""
 }
 else if (password.value == ""){
  error ++;
  errorMessage += "Please supply a Password" + "\r\n";
  password.style.backgroundColor = '#F5DFD8';
  pwordConf.style.backgroundColor = '#F5DFD8';

 }
  else if (password.value.length < 6){
  error ++;
  errorMessage += "Password must be at least 6 characters" + "\r\n";
  password.style.backgroundColor = '#F5DFD8';
  pwordConf.style.backgroundColor = '#F5DFD8';
  password.value = ""
  pwordConf.value = ""
 }
 }
 
 if (error > 0){
  alert(errorMessage);
  return false;
 }
 else
  return true;
 
 
 }

function validateNotNull(field, message){
  if (field) {
  if (field.value ==""){
  field.style.backgroundColor = '#F5DFD8';
  errorMessage += message + "\r\n";
  error ++;
 }
 else{  field.style.backgroundColor = '#FFFFFF';}
 }
}

function validateChecked(field, message){
  if (field) {
  if (!(field.checked)){
  errorMessage += message + "\r\n";
  error ++;
 }}
}



function motDates(){

if (document.getElementById("motDate"))
{
var m_names = new Array("January", "February", "March", 
"April", "May", "June", "July", "August", "September", 
"October", "November", "December");
var m_days = new Array("1","2","3","4","5","6","7","8","9","10","11","12","13","14","15",
"16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31");
var motDate = document.getElementById("motDate");
var motMonth = document.getElementById("motMonth");

//Existing users have an their selected value as the first field. Store this fact as defaultUsed
var defaultUsed = 0;
if (motMonth.options.length == 1 || motMonth.options.length == 13)
	defaultUsed = 1;


if(motDate.options.length < 31 + defaultUsed)
{
	for (a=0; a<31; a++)
	{
		if (motDate.options[a+defaultUsed] == undefined)
		{
			try {
    			motDate.add(new Option(m_days[a], m_days[a]), null); // standards compliant; doesn't work in IE
		 		 }
		  	catch(ex) {
		  		motDate.add(new Option(m_days[a], m_days[a])); // IE only
		  		}
		}
	}
}

if(motMonth.options.length==0+defaultUsed)
{
	for (a=0; a<12; a++)
	{
	try {
    	motMonth.add(new Option(m_names[a], m_names[a]), null); // standards compliant; doesn't work in IE
		}
	catch(ex) {
		 motMonth.add(new Option(m_names[a], m_names[a])); // IE only
		  }
	}
}
//Clear the first value if it is no longer used
var selectedMonth = motMonth.options[motMonth.options.selectedIndex].text;
var selectedDate = motDate.options[motDate.options.selectedIndex].text;
var firstMonth =  motMonth.options[0].text;
var secondMonth =  motMonth.options[1].text;
var firstDate =  motDate.options[0].text;
var secondDate =  motDate.options[1].text;
if (((selectedMonth != firstMonth) && (firstMonth != "January" || secondMonth == "January")) || (firstMonth == secondMonth))
	{
	motMonth.remove(0);
	defaultUsed = 0;
	}
if (((selectedDate != firstDate) && (firstDate != "1" || secondDate == "1")) || (firstDate == secondDate))
	motDate.remove(0)

	if (selectedMonth == "February")//February
	{
		if (motDate.options.selectedIndex > 28+defaultUsed)
			motDate.options.selectedIndex = 28+defaultUsed
		for (a=30; a>28; a--)
		if (motDate.options[a+defaultUsed])
		{
			motDate.remove(a+defaultUsed);
		}
	}
	if (selectedMonth == "April" || selectedMonth == "June" || selectedMonth == "September" || selectedMonth == "November")//April, June, September & November
	{
		if (motDate.options.selectedIndex == 30+defaultUsed)
			motDate.options.selectedIndex = 29+defaultUsed
		if (motDate.options[30+defaultUsed])
		{
			motDate.remove(30+defaultUsed);
		}
	}
}
}

function pageLoad() {
motDates();
if (document.getElementById("make"))
	popCarForm();
}

function productDetail(frm) {
	if ((frm.prodcode.value) && (frm.prodcode.value != '')) {
		frm.action = "/Product Pages/sub/Details";
		frm.submit();
		}
	}
