/* code from qodo.co.uk */
// create as many regular expressions here as you need:
var phoneDigitsOnly = /[0-9\() +.]/g;
var digitsOnly = /[1234567890]/g;
var integerOnly = /[0-9\.]/g;
var alphaOnly = /[A-Z]/g;

function restrictCharacters(myfield, e, restrictionType) {
	if (!e) var e = window.event
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;
	var character = String.fromCharCode(code);

	// if they pressed esc... remove focus from field...
	if (code==27) { this.blur(); return false; }

	// ignore if they are press other keys
	// strange because code: 39 is the down key AND ' key...
	// and DEL also equals .
	if (!e.ctrlKey && code!=9 && code!=8 && code!=36 && code!=37 && code!=38 && code!=39 && (code!=39 || (code==39 && character=="'")) && code!=40) {
		if (character.match(restrictionType)) {
			return true;
		} else {
			return false;
		}
	}
}

function numbersOnly(e) {
	var unicode=e.charCode? e.charCode : e.keyCode;
	if (unicode!=8 && unicode!=9 && unicode!=38){ //if the key isn't the backspace key (which we should allow) 
		if (unicode<48||unicode>57) {return false;}//if not a number return false //disable key press 
	}
}

function validateContactForm(form, errorElement) {
	document.getElementById(errorElement).innerHTML = 'Checking...';
	var name = form.Name.value;
    var message = '';
    var valid = true;

		
	/* 4 NXP */
	if(isBlank(form.Name.value) && !isValidEmail(form.Email.value)) {
		message = 'Please enter a valid email address.';
		valid = false;
	}	

	
	/* 6 XEP */
	if(!isBlank(form.Name.value) && isValidEmail(form.Email.value)) {
		message = 'Please enter your name.';
		valid = false;
	}
	
	/* 7 XXP */
	if(!isBlank(form.Name.value) && !isValidEmail(form.Email.value)) {
		message = 'Please enter your name and a valid email address.';
		valid = false;
	}

	
	/* 1 NEP */
	if(valid) {
		document.getElementById(errorElement).innerHTML = 'Sending...';
		form.submit()
	}
	else {
		document.getElementById(errorElement).innerHTML = message;
	}
}

function isValidEmail(str) {
	return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
}

function isBlank(str) {
	var isNonblank_re    = /\S/;
   	return String (str).search (isNonblank_re) != -1
}


//&& unicode!=40 && unicode!=41 && unicode!=45 && unicode!=46 && unicode!=43 && unicode!=32