
 function validateNewCustomer(){
    var elem;
    var errs=0;
    if (!validate (document.forms.create_account.street_address, 'street_address', '4',' min 4 chars')) errs += 1; 
    if (!validate (document.forms.create_account.postcode, 'postcode', '5', ' Enter your full postcode')) errs += 1; 
    if (!validate (document.forms.create_account.city, 'city', '4', ' min 4 chars')) errs += 1; 
	if (!validate (document.forms.create_account.firstname, 'firstname', '3', ' min 3 chars')) errs += 1;  
	if (!validate (document.forms.create_account.lastname, 'lastname', '3', ' min 3 chars')) errs += 1; 
    if (!validate (document.forms.create_account.telephone, 'telephone', '10', ' min 10 chars')) errs += 1; 
    if (!validateEmail (document.forms.create_account.email_address, 'email_address')) errs += 1; 
    if (!validate (document.forms.create_account.password, 'password', '5', ' min 5 chars')) errs += 1;     
    //if (!validateDate (document.forms.create_account.dob, 'dob')) errs += 1; 
    if (!validateConfPassword (document.forms.create_account.confirmation, document.forms.create_account.password, 'confirmation')) errs += 1;
    
    if(errs >=1){
      msg ("warningErrors", "errorForm", " An error occured while attempting to process your request. Please review errors above and try again.");  
	   	return false;

    }
    
    
	 /* 
    if (!validateInstitution (document.forms.NewScientist.institution, 'institution')) errs += 1; 
    if (!validateAddress (document.forms.NewScientist.institution_address, 'institution_address')) errs += 1; 
    if (!validatePassword (document.forms.NewScientist.pwd, 'pwd')) errs += 1;
    if (!validateConfPassword (document.forms.NewScientist.conf_pwd, document.forms.NewScientist.pwd, 'conf_pwd')) errs += 1;*/
	return (errs==0);
  };
  
  
  
 function validateAddressBook(){
    var elem;
    var errs=0;
    if (!validate (document.forms.create_account.street_address, 'street_address', '4',' min 4 chars')) errs += 1; 
    if (!validate (document.forms.create_account.postcode, 'postcode', '5', ' Enter your full postcode')) errs += 1; 
    if (!validate (document.forms.create_account.city, 'city', '4', ' min 4 chars')) errs += 1; 
	if (!validate (document.forms.create_account.firstname, 'firstname', '3', ' min 3 chars')) errs += 1;  
	if (!validate (document.forms.create_account.lastname, 'lastname', '3', ' min 3 chars')) errs += 1; 
    
    if(errs >=1){
      msg ("warningErrors", "errorForm", " An error occured while attempting to process your request. Please review errors above and try again.");  
	   	return false;

    }
    
    
	 /* 
    if (!validateInstitution (document.forms.NewScientist.institution, 'institution')) errs += 1; 
    if (!validateAddress (document.forms.NewScientist.institution_address, 'institution_address')) errs += 1; 
    if (!validatePassword (document.forms.NewScientist.pwd, 'pwd')) errs += 1;
    if (!validateConfPassword (document.forms.NewScientist.conf_pwd, document.forms.NewScientist.pwd, 'conf_pwd')) errs += 1;*/
	return (errs==0);
  };
  
  
  
  
  //////
  
 function validateCheckout(){
    var elem;
    var errs=0;
    
    var ptype   = document.forms.create_account.cc_type;	
	var i 		= ptype.selectedIndex;  
	var pmethod = ptype.options[i].value;
	
	if(pmethod == 'none'){
     	//if (!validateSlots (document.forms.create_account.comments, 'warningErrors', '5', ' Enter your full postcode')) errs += 1;
		msg ("errorForm", "errorForm", " Please select a payment method"); 
		errs += 1; 
		return false;
	}else if(pmethod == 'COD'){
    	if (!validateSlots (document.forms.create_account.comments, 'errorForm', '1', '')) errs += 1; 
	
	}else{
		// check if can select delivery slot
		if(document.forms.create_account.payment_type.value == 'checkSlot'){
    		if (!validateSlots (document.forms.create_account.comments, 'dSlot', '1', ' Please select a delivery time slot')) errs += 1; 
		}
	/*
   		if (!validateNew (document.forms.create_account.cc_owner, 'cc_owner', '1','*')) errs += 1;     	
   		if (!validateNew (document.forms.create_account.cc_number, 'cc_number', '1','*')) errs += 1;     	
   		if (!validateNew (document.forms.create_account.cvvnumber, 'cvvnumber', '1','*')) errs += 1;     	
   		if (!validateNew (document.forms.create_account.street_billing1, 'street_billing1', '1','*')) errs += 1;     	
   		if (!validateNew (document.forms.create_account.postcode_billing, 'postcode_billing', '1','*')) errs += 1;     	
   		if (!validateNew (document.forms.create_account.city_billing, 'city_billing', '1','*')) errs += 1;     	
    	//if (!validatePMethod (document.forms.create_account.cc_type, 'cc_type', '5', ' Enter your full postcode')) errs += 1; 
*/
	}

/*
	if(pmethod == 'Switch'){
   		if (!validateNew (document.forms.create_account.switch_n, 'switch_n', '1','*')) errs += 1;     	


	}
*/    
    
    if(errs >=1 && pmethod != 'COD'){
      msg ("errorForm", "errorForm", " * An error occured while attempting to process your request. Please review errors above and try again.");  
	  return false;

    }
    
    
	return (errs==0);
  };  
  //////
  
 
 
 
 
 
  function validateForgotten(){
    var elem;
    var errs=0;
    if (!validateEmail (document.forms.pforgotten.email_address, 'email_address')) errs += 1; 
	return (errs==0);
  };
 

 
 
 
  function validateNew(valField, idSpan, size, message){
	if(isEmpty(valField.value)){
      msg (idSpan, "errorForm", " *");  
      return false;
	}else{
	  if (valField.value.length >=size){
          msg (idSpan, "ok", "");  		  
		  return true;
	  }else{
          msg (idSpan, "errorForm", message);  
		return false;
	  }
		
	}
	
}

 
 
 
 
  
  
 function validate(valField, idSpan, size, message){
	if(isEmpty(valField.value)){
      msg (idSpan, "errorForm", " This field is required");  
      return false;
	}else{
	  if (valField.value.length >=size){
          msg (idSpan, "ok", "");  		  
		  return true;
	  }else{
          msg (idSpan, "errorForm", message);  
		return false;
	  }
		
	}
	
}

function validateEmail(valField, idSpan){
	
	if(isEmpty(valField.value)){
      msg (idSpan, "errorForm", " This field is required");  
      return false;
	}else{
		
  		var regExp = /^[^\s()<>@,;:\/]+@\w[\w\.-]+\.[a-z]{2,}$/i
			  
	  if (regExp.test(valField.value)){
          msg (idSpan, "ok", "");  		  
		  return true;
	  }else{
          msg (idSpan, "errorForm", " Enter a valid email address");  
		return false;
	  }
		
	}
	
}
 
function validateDate(valField, idSpan){
	if(!isEmpty(valField.value)){
		
	var regExp = /^(((0[1-9]|[12]\d|3[01])\/(0[13578]|1[02])\/((1[6-9]|[2-9]\d)\d{2}))|((0[1-9]|[12]\d|30)\/(0[13456789]|1[012])\/((1[6-9]|[2-9]\d)\d{2}))|((0[1-9]|1\d|2[0-8])\/02\/((1[6-9]|[2-9]\d)\d{2}))|(29\/02\/((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$/
	 if (regExp.test(valField.value)){
          msg (idSpan, "ok", "");  		  
		  return true;
	  }else{
          msg (idSpan, "errorForm", " dd/mm/yyyy");  
		return false;
	  }
		
	}
	
}
  
  
function validateConfPassword(valField, valField2, idSpan){
	if(isEmpty(valField.value)){
      msg (idSpan, "errorForm", " This field is required");  
      return false;
	}else{
		
		  if(valField.value == valField2.value){
			  msg (idSpan, "ok", "");  		  
			  return true;
		  }else{
	          msg (idSpan, "errorForm", " Password does not match");  
		  }
		
	}
	
}


function validateSlots(valField, idSpan){

   for(var i = 0; i < valField.length; i++)
	if(valField[i].checked) {
		msg (idSpan, "ok", "");  		  
		return true;
	}else{
	    msg (idSpan, "errorSlot", " * Please select a delivery time slot");  
	}
}








function msg(fld, msgtype, message){
	var elem = document.getElementById(fld);
	elem.firstChild.nodeValue = message;  
	elem.className = msgtype; 

}




function isEmpty(str){
  return (str == null) || (str.length == 0);
}



