function validateFormOne(theForm) {
var reason = "";

  reason += validateEmail(theForm.email);
  
  reason += validateEmpty(theForm.name, 'Name');
  reason += validateEmpty(theForm.comments, 'Comments');

  if (reason != "") {
    alert("Some fields need correction:\n\n" + reason);
    return false;
  }

  return true;
}

function validateFormTwo(theForm) {
var reason = "";

  reason += validatePhone(theForm.telephone_demo);
  
  reason += validateEmpty(theForm.name_demo, 'Name');
  reason += validateEmpty(theForm.address_demo01, 'Address Line 1');
  reason += validateEmpty(theForm.address_demo02, 'Address Line 2'); 
  reason += validateEmpty(theForm.postcode_demo, 'Postcode');
      
  if (reason != "") {
    alert("Some fields need correction:\n\n" + reason);
    return false;
  }

  return true;
}

function validateFormThree(theForm) {
var reason = "";

  reason += validateEmail(theForm.email);
  
  reason += validateEmpty(theForm.firstname, 'Firstname');
  reason += validateEmpty(theForm.surname, 'Surname');
  reason += validateEmpty(theForm.address01, 'Address Line 1');
  reason += validateEmpty(theForm.postcode, 'Postcode');
  reason += validateEmpty(theForm.telephone, 'Telephone');
      
  if (reason != "") {
    alert("Some fields need correction:\n\n" + reason);
    return false;
  }

  return true;
}

function validateFormFour(theForm) {
var reason = "";

  reason += validatePhone(theForm.telephone);
  
  reason += validateEmpty(theForm.firstname, 'First Name');
  reason += validateEmpty(theForm.surname, 'Surname');
  reason += validateEmpty(theForm.customernumber, 'Customer number');
  
  reason += validatePhone(theForm.telephone_new);  
  reason += validateEmpty(theForm.name_new, 'Name');
  reason += validateEmpty(theForm.address_new01, 'Address line 1');
  reason += validateEmpty(theForm.postcode_new, 'Postcode');
  
  
  if (reason != "") {
    alert("Some fields need correction:\n\n" + reason);
    return false;
  }

  return true;
}

function validateFormFive(theForm) {
var reason = "";

  reason += validatePhone(theForm.telephone);
  
  reason += validateEmpty(theForm.name, 'Name'); 
  
  if (reason != "") {
    alert("Some fields need correction:\n\n" + reason);
    return false;
  }

  return true;
}

function validateAccForm(theForm) {
var reason = "";
  
  reason += validateEmpty(theForm.username, 'Username');
  
  if (theForm.password.value != theForm.password2.value) {
  	
  	reason += "Passwords do not match";
  
  }

  if (reason != "") {
    alert("Some fields need correction:\n\n" + reason);
    alert(theForm.password.value + "!=" + theForm.password2.value)
    return false;
  }

  return true;
}

function validateEmpty(fld, fldname) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = '#ffff99'; 
        error = "'" + fldname + "' has not been filled in.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;  
}

function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}

function validateEmail(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
   
    if (fld.value == "") {
        fld.style.background = '#ffff99';
        error = "You didn't enter an email address.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = '#ffff99';
        error = "Please enter a valid email address.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = '#ffff99';
        error = "The email address contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validatePhone(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');    

   if (fld.value == "") {
        error = "You didn't enter a phone number.\n";
        fld.style.background = '#ffff99';
    } else if (isNaN(parseInt(stripped))) {
        error = "The phone number contains illegal characters.\n";
        fld.style.background = '#ffff99';
    } /*else if (!(stripped.length < 10)) {
        error = "The phone number is the wrong length. Make sure you included an area code.\n";
        fld.style.background = '#ffff99';
    }*/
    return error;
}

function checkWhoFor() {
	
	var selObj = document.getElementById("who_for");
	var selIndex = selObj.selectedIndex;

	if (selObj.options[selIndex].value == 'Myself') {

		document.getElementById("user_title").selectedIndex = document.getElementById("title").selectedIndex;

		document.getElementById("user_initial").value = document.getElementById("initial").value;
		
		document.getElementById("user_surname").value = document.getElementById("surname").value;
		
		document.getElementById("user_address1").value = document.getElementById("address1").value;
		
		document.getElementById("user_address2").value = document.getElementById("address2").value;
		
		document.getElementById("user_city").value = document.getElementById("city").value;
		
		document.getElementById("user_county").value = document.getElementById("county").value;
		
		document.getElementById("user_postcode").value = document.getElementById("postcode").value;
		
		document.getElementById("user_phone").value = document.getElementById("phone").value;
	
	} else {
	
		document.getElementById("user_title").selectedIndex = 0;

		document.getElementById("user_initial").value = '';
		
		document.getElementById("user_surname").value = '';
		
		document.getElementById("user_address1").value = '';
		
		document.getElementById("user_address2").value = '';
		
		document.getElementById("user_city").value = '';
		
		document.getElementById("user_county").value = '';
		
		document.getElementById("user_postcode").value = '';
		
		document.getElementById("user_phone").value = '';
	
	}

}

function checkDelWhoFor() {
	
	var selObj = document.getElementById("del_who_for");
	var selIndex = selObj.selectedIndex;

	if (selObj.options[selIndex].value == 'Myself') {

		document.getElementById("del_name").value = document.getElementById("surname").value;
		
		document.getElementById("del_address1").value = document.getElementById("address1").value;
		
		document.getElementById("del_address2").value = document.getElementById("address2").value;
		
		document.getElementById("del_city").value = document.getElementById("city").value;
		
		document.getElementById("del_county").value = document.getElementById("county").value;
		
		document.getElementById("del_postcode").value = document.getElementById("postcode").value;
		
		document.getElementById("del_phone").value = document.getElementById("phone").value;
	
	} else if (selObj.options[selIndex].value == 'User') {

		document.getElementById("del_name").value = document.getElementById("user_surname").value;
		
		document.getElementById("del_address1").value = document.getElementById("user_address1").value;
		
		document.getElementById("del_address2").value = document.getElementById("user_address2").value;
		
		document.getElementById("del_city").value = document.getElementById("user_city").value;
		
		document.getElementById("del_county").value = document.getElementById("user_county").value;
		
		document.getElementById("del_postcode").value = document.getElementById("user_postcode").value;
		
		document.getElementById("del_phone").value = document.getElementById("user_phone").value;
	
	} else {

		document.getElementById("del_title").selectedIndex = 0;

		document.getElementById("del_initial").value = '';
		
		document.getElementById("del_surname").value = '';
		
		document.getElementById("del_address1").value = '';
		
		document.getElementById("del_address2").value = '';
		
		document.getElementById("del_city").value = '';
		
		document.getElementById("del_county").value = '';
		
		document.getElementById("del_postcode").value = '';
		
		document.getElementById("del_phone").value = '';

	}

}
