
    function brochuresSelected() {
      var checked = $$('*:checked');
      var cnt = checked.length;

      if ( cnt == 0 ) {
        alert("Please select a brochure");
        return false;
      }    

      return true;
    }
  
    // check that at least one brochure has been selected and that all the required fields are filled out
    function validateform() {
      var inpName = "";
      var errors = [];
      if ( brochuresSelected() == false) {
        return false;
      }

      var inputs = $$( "input[type='text']" );

      var exclude = [ "county", "tel" ] ; // fields which aren't required

      inputs.each( function( inp ) {

        if( inp.value == "" ){           
            if( !exclude.contains( inp.name.toLowerCase() ) ){
              inpName = makeHumanReadable( inp.name );
              errors.push( inpName + " is a required field" );
            }
        }
      });
      
      if( errors.length > 0 ) {
        alert( errors.join( "\n" ) );
        return false;
      }
      
      return true;
    }
    
    // makes the input names human readable so they can be used when displaying validation messages
    function makeHumanReadable( text ) {
      text = text.toLowerCase().capitalize();
      if( text == "Name" ){
      text = "First Name"
      }
      if( text == "Address1" ){
      text = "House Number/Name"
      }
      if( text == "Address2" ){
      text = "Street"
      }
      if( text == "Town" ){
      text = "Town/City"
      }
      return text
    }
    
    
    
    // populate the address fields with values from the postcode lookup
    function populateValues(streetname,streetnum,postcode,county,town,state,addressid){
		if ( $chk( streetname ) ) {
			streetname = streetname.replace (/%/g," ");				
			$( 'address2' ).value = streetname;
		}
		
		if ( $chk( streetnum ) ) {
			streetnum = streetnum.replace (/%/g," ");	
			$( 'address1' ).value = streetnum;
		}
				
		if ( $chk( town ) ) {					
			town = town.replace (/%/g," ");			
			$( 'town' ).value = town;	
		}
				
		if ( $chk( postcode ) ) {					
			postcode = postcode.replace (/%/g," ");				
			$( 'POSTCODE' ).value = postcode;		
		}
		
		if ( $chk( state ) ) {					
			state = state.replace (/%/g," ");				
			$( 'COUNTY' ).value = state;						
		}	
		
		if ( $chk( addressid ) ) {					
			addressid = addressid.replace (/%/g," ");				
			$( 'addressid' ).value = addressid;						
		}					
	}
