/*
#######################################

NetConstruct Form Checker.

Contains checkInputValidity() function.

Last modified 23/03/2003 - Christopher Hobbis
(included trim function)

#######################################
#######################################


E.G. onSubmit="return checkInputValidity(this)"


//checks for form input
function checkInputValidity(myForm) {
    var errors = "";

	 if (trim(myForm.firstname.value) == "") {
        errors = errors + "Please provide your first name\n\n";
    }
    if (!checkPhone(trim(myForm.phone.value))) {
        errors += "Please provide a valid Phone Number\n\n";
    }
    if (!checkEmail(trim(myForm.email.value))) {
        errors += "Please provide a valid email address\n\n";
    }
    if (!checkCheckbox(myForm.checkboxname)) {
	        errors += myForm.checkboxname.name + " must be checked before continuing\n\n";
    }
	var testCheckboxes = new Array(myForm.machinedtimber,myForm.treatedtimber,myForm.applications);

	if(!checkCheckBoxes(1,3,testCheckboxes) && myForm.SendInfoOn.value == '') {
			errors += "Please indicate what you require from the Information Request section\n\n";
	}

    if (errors != "") {
        alert(errors);
        return false;
    }
    return true;
}

#######################################
*/



//checks for valid phone number
function checkPhone(number) {

 re = /\s/g;
 number = number.replace(re, '');


	var legal_chars = '()-+. ';
	var number_chars = '0123456789';
	var newnum = '';

	for ( i = 0; i<number.length; i++ ) {
		c = number.charAt(i);
			if (number_chars.indexOf(c ) != -1 ) {
				newnum += c;
			} else if (legal_chars.indexOf( c ) == -1 ) {
		return false;
		}
	}

	if ( newnum.length <  11) {
		return false;
	}
		return true;
	}


//checks for valid email
function checkEmail(email)	{

 re = /\s/g;
 email = email.replace(re, '');


  	var emailReg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/; 	// not valid
	var emailReg2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,6}|[0-9]{1,3})(\]?)$/; 	// valid

	if (!(!emailReg1.test(email) && emailReg2.test(email))) {	// if syntax is valid
		return false;
		}
	return true;
	}


//Check checkbox
function checkCheckbox(checkboxPath) {
	if(checkboxPath.checked) {
	return true;
	}
	return false;
}


//Check number of checboxes ticked. nameArray is an array of the checkboxes i.e. document.form.checkboxname
function checkCheckBoxes(min,max,nameArray) {
	var numChecked = 0;
	 for (i=0; i<nameArray.length; i++) {
		if (nameArray[i].type == 'checkbox' && nameArray[i].checked) numChecked++;
	 }

	if(numChecked <= max && numChecked >= min) {
		return true;
		}
	return false;
}



//-------------------------------------------------------------------------------
//Helper functions

function ltrim ( s )
{
	return s.replace( /^\s*/, "" );
}

function rtrim ( s )
{
	return s.replace( /\s*$/, "" );
}

//Combine the rtrim() and ltrim() functions to make the trim() function, which just wraps both calls together:

function trim ( s )
{
	return rtrim(ltrim(s));
}

