//---------------------------------
// Validation functions return true or false depending on the type of form field being validated and the field's input
//---------------------------------


// valid if textfield contains something
function validateTextField( field ){
	if( field.value!='' ) return true;
	return false;
}  


// validates a given date from d m y dropdowns
function validateDateDropdown( d, m, y ){
	var d1 = d[d.selectedIndex].value;
	var m1 = m[m.selectedIndex].value;
	var y1 = y[y.selectedIndex].value;
	var testdate = new Date(y1, m1-1, d1);
	var d2 = testdate.getDate();
	var m2 = testdate.getMonth()+1;
	var y2 = testdate.getFullYear();
	if( d1 == d2 && m1 == m2 && y1 == y2) return true;
	return false;
}


// validate a field contains a propper email address
function validateEmailField( field ){
	var email = field.value;
	// must be at least 5 characters long (a@b.c)
	if( email.length < 5 ) return false;
	// can't have any spaces in it
	if( email.indexOf(' ') > -1 ) return false;
	var atpos = email.indexOf('@');
	// must have an '@', precedded by at least 1 character & followed by at least 3
	if( (atpos < 1) || (atpos > email.length - 4) ) return false;
	var dotpos = email.indexOf('.', atpos);
	// must have a '.' after the '@', at least 1 character between them & at least 1 after
	if( (dotpos < 1) || (dotpos > email.length - 2) || (dotpos < atpos + 2) ) return false;
	return true;
}


// Valid if AT LEAST ONE checkbox, radio button, text field or textarea has been filled in
// (does NOT require them ALL to be filled in - just ONE)
function validateComboGroup( group ){
	for(i=0; i<group.length; i++){
		if( (group[i].type == 'radio' || group[i].type == 'checkbox') &&  group[i].checked ){
			return true;
		} else if( (group[i].type == 'text' || group[i].type == 'textarea') && validateTextField(group[i]) ) {
			return true;
		}
	}
	return false;
}
	

// Valid if ALL textfields in group contain something other than nothing.
function validateTextGroup( group ){
	for(i=0; i<group.length; i++){
		if( !validateTextField(group[i]) ) return false;
	}
	return true;
}


// valid if dropdown selection has a value other than nothing
function validateDropDown( dropdown ){
	if( dropdown[dropdown.selectedIndex].value.length > 0 ) return true;
	return false;
}



//---------------------------------
// get functions to return the values of form fields
//---------------------------------


// returns the value of the slected radio button in a group
function getRadioGroupValue( group ){
	// loop through group and return selected
	for(i=0; i<group.length; i++){
		if( group[i].checked ) return group[i].value;
	}
}


// returns the total number of checkboxes selected in a group
function getCheckboxGroupCount( group ){
	var total_selected = 0;
	// loop through group and count selected
	for(i=0; i<group.length; i++){
		if(group[i].checked) total_selected++;
	}
	return total_selected;
}


//--------------------
// behaviour functions to effect and limit certain aspects of the form
//--------------------


// Controls a 'none' option in a checkbox group.
// 'None' option must be the last in the group.
// All checkbox HTML tags must include:
// onClick="limitCheckboxGroupNone(this);"
function limitCheckboxGroupNone( checkbox ){
	// get the checkbox group that this belongs to (clever bit:))
	var checkbox_group = checkbox.form[checkbox.name];
	// if it's the last in the group, it's the 'none' option
	if( checkbox == checkbox_group[checkbox_group.length-1] ){
		// if it's selected, deselect the rest of the group
		if( checkbox.checked ){
			for(i=0; i<checkbox_group.length-1; i++){
				checkbox_group[i].checked = false;
			}
		}
	} else {
		// it's not the 'none' option, so make sure the 'none' option is deselected
		checkbox_group[checkbox_group.length-1].checked = false;
	}
}


// Controls an 'other' textfield option in a radio button group.
// 'Other' textfield must be the last in the group.
// All radio button HTML tags must include:
// onClick="limitRadioGroupOther(this);"
// 'Other' textfield HTML tag must include:
// onFocus="limitRadioGroupOther(this);"
function limitRadioGroupOther( radio ){
	// get the radio group that this belongs to (clever bit:))
	var radio_group = radio.form[radio.name];
	if( radio == radio_group[radio_group.length-1] ){
	// if it's the last in the group, it's the 'other' textfield
		// so deselect all the radio buttons
		for(i=0; i<radio_group.length-1; i++){
			radio_group[i].checked = false;
		}
	} else {
	// it's not the 'other' textfield, so clear the 'other' textfiled option
		radio_group[radio_group.length-1].value = "";
	}
}


// Limits the number of selections in a checkbox group.
function limitCheckboxGroupCount( checkbox, limit ){
	// if selecting...
	if( checkbox.checked ){
		// get the checkbox group this belongs to
		var checkbox_group = checkbox.form[checkbox.name];
		// if maxed out, don't allow any more to be selected
		if( getCheckboxGroupCount( checkbox_group ) > limit ) return false;
	} else {
		return true;
	}
}


// Disable a submit button or image once form has been submitted.
// If passed a number of seconds, will reset after that time (so can be submitted again).
fv_formSubmitted = false;
function limitSubmitOnce( seconds ){
	// if not submitted yet
	if( !fv_formSubmitted ){
		fv_formSubmitted = true;
		// if given a timeout period, reset after n seconds
		if( seconds ) setTimeout( 'fv_formSubmitted = false;', seconds * 1000 );
		return true;
	} else {
		// allready been submitted
		return false;
	}
}