/*
#######################################

NetConstruct Form Checker.

Contains checkInputValidity() function.

Last modified 23/03/2003 - Christopher Hobbis
(included trim function)

#######################################
#######################################


E.G. 

<a href="javascript:void(checkInputValidity())"><img src="/images/btn_submit.gif" vspace="6" alt="Submit Form" width="121" height="19" border="0"></a>


#######################################

function checkInputValidity() {

    var errors = new String();
        errors = "";
	  if ((document.emailform.how_we_can_help.value == "please make selection")||(document.emailform.how_we_can_help.value == "")) {
        errors += "Please make a selection\n\n";
    }
   	if (document.emailform.name.value == "") {
        errors += "Please provide your name\n\n";
    }
   	if (document.emailform.position.value == "") {
        errors += "Please provide your position\n\n";
    }
   	if (document.emailform.organisation.value == "") {
        errors += "Please provide your organisation\n\n";
    }
   	if (document.emailform.phone.value == "") {
        errors += "Please provide your telephone number\n\n";
    }
    if (!checkEmail(document.emailform.email.value)) {
        errors += "Please can you provide a vaild email address\n\n";
    }
		if (errors != "") {
        alert(errors);
        return false;
    } else {
		document.emailform.submit();
		return true;
		}
		
	alert("Errors are: " + errors);	
}


#######################################
*/



//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));
}



//an here is the function used on the enquiry page...


function checkInputValidity() {

    var errors = new String();
        errors = "";
	  if ((document.emailform.how_we_can_help.value == "please make selection")||(document.emailform.how_we_can_help.value == "")) {
        errors += "Please make a selection\n\n";
    }
   	if (document.emailform.name.value == "") {
        errors += "Please provide your name\n\n";
    }
   	if (document.emailform.position.value == "") {
        errors += "Please provide your position\n\n";
    }
   	if (document.emailform.organisation.value == "") {
        errors += "Please provide your organisation\n\n";
    }
   	if (document.emailform.phone.value == "") {
        errors += "Please provide your telephone number\n\n";
    }
    if (!checkEmail(document.emailform.email.value)) {
        errors += "Please can you provide a vaild email address\n\n";
    }
		if (errors != "") {
        alert(errors);
        return false;
    } else {
		document.emailform.submit();
		return true;
		}
		
	alert("Errors are: " + errors);	
}
