
function doalert (message,e) {	// call alert using eithe warning
				// or message, followed by e.name
	if (e.warning)
	   alert(e.warning)
	else
	   alert(message+" "+e.name)
}

function filled (){
// checks a text element to ensure that it is not blank
        if (this.value =="") {
                doalert ("Please fill in your", this)  // say which field it is
                this.focus()                    // go to it
                return false
        } return true
}
function isInt (){
// check a field that should be a number
        var num = parseInt (this.value)
        if (isNaN(num)){
                doalert ("Please use a number for your",this)
                this.focus()     // go to it (ready to correct)
                return false
        }
	this.value = num	// eg. '45 years' to 45 
	return true
}
function isFloat (){
// check a field that should be a number
        var num = parseFloat(this.value)
        if (isNaN(num)){
                doalert ("Please use a number for",this)
                this.focus()     // go to it (ready to correct)
                return false
        }
	this.value = num	// eg. '45.62 parsces' to 45.62 
	return true
}

// specialized functions are possible, for instance, to define a phonenumber as 3+4 digits:

function isPhone() {
        if ( /\d{3}.*\d{4}/.test(this.value) ) return true
                doalert ("Fill in with numbers at least your home", this)  // say which field it is
                this.focus()                    // go to it
                return false
}

function isMail() {
        if ( /.@.+\.../.test(this.value) ) return true
                doalert ("Fill in with @ the", this)  // say which field it is
                this.focus()                    // go to it
                return false
}

function isPostalCode() {
        if ( /^[A-Za-z]\d[A-Za-z] ?\d[A-Za-z]\d$/.test(this.value) ) // Canada
		return true						
        if ( /^\d{5}/.test(this.value) ) return true		     // USA
                doalert ("Fill in Canada or USA ", this)  // say which field it is
                this.focus()                    // go to it
                return false
}

function anythingGoes () {return true}   // won't object to any value

/* For a set of radio buttons, the simple object model doesn't quite work.
      elements.thename is an ARRAY of button objects.
      To be sure one is checked, go through this array, looking for .checked
      to be true */

function choiceMade(colour){
	for (var i=0; i<colour.length; i++)
	     if (colour[i].checked) return true
        if ('string' ===typeof colour.warning) colour[0].warning = colour.warning
	doalert ("Please choose a ", colour[0])
	return false
}

/*   For a select list, you can use .selectedIndex, it is supposed to be -1 if
      nothing is selected. This is true for select - MULTIPLE.
	Experiment shows that for single select, item 0 is initially selected,
      so you will need to make item 0 an unacceptable value, and require
      .selectedIndex to be greater than 0
	We can distinguish between the two by looking at the .type property
*/

function selectionMade() {
	var empty = this.type == 'select-one' ? 0:-1  // value for no selection
	if (this.selectedIndex > empty) return true   // indicates some selection
	doalert('Select a ', this)
	this.focus()
	return false
}

//------------- This function calls the check methods of ALL form elements
//              so ALL elements, including buttons, must have check assigned
//	For radio groups, include this.form.thename as extra arguments
function checkAll (f) {
        var ok = true
        with (f)
          for (var i=0; i<elements.length && ok; i++)
                ok = elements[i].check()
	for (var i = 1; i < checkAll.arguments.length && ok; i++) 
		ok = choiceMade(checkAll.arguments[i]) 
	if (ok) f.submit()
}

function prepareForm (aForm) {
with (aForm) {       // add a default check method to EVERY element
	for (var i=0; i<elements.length; i++)
	   elements[i].check = anythingGoes	//allow as default
	}
}
