function trim(str){
	return str.replace(/^\s+|\s+$/g,'');
}

function isEmail(checkEmail) {
	if ((checkEmail.indexOf('@') < 0) || ((checkEmail.charAt(checkEmail.length-4) != '.') && (checkEmail.charAt(checkEmail.length-3) != '.'))){
		return false;
	} else {
		return true;
	}
}

function isPhone(str) {
	if(str.search("^[0-9]{10}$|^\(0[1-9]{1}\)[0-9]{8}$|^[0-9]{8}$|^[0-9]{4}[ ][0-9]{3}[ ][0-9]{3}$|^\(0[1-9]{1}\)[ ][0-9]{4}[ ][0-9]{4}$|^[0-9]{4}[ ][0-9]{4}$") == -1){
		return false;
	} else {
		return true;
	}
}
	
function checkForm(type) {

	var cfirst;
	var clast;
	var cphone;
	var cemail;
	
	if(type == 'referal') {
		with(window.document.referal) {
			cfirst  = firstName;
			clast   = lastName;
			cphone  = phone;
			cemail  = email;
		}
	} else {
		with(window.document.appointment) {
			cfirst  = firstName;
			clast   = lastName;
			cphone  = phone;
			cemail  = email;
		}
	}

	if (trim(cfirst.value) == '') {
		alert('Please enter your first name');
		cfirst.focus();
		return false;
	} else if(trim(clast.value) == '') {
		alert('Please enter your last name');
		clast.focus();
		return false;
	} else if(trim(cphone.value) == '' || isPhone(cphone.value) == false) {
		alert('Your phone number is not valid');
		cphone.focus();
		return false;
	} else if(trim(cemail.value) == '' || isEmail(cemail.value) == false) {
		alert('Email address is not valid');
		cemail.focus();
		return false;
	} 
}

function validateForm(type) {
	if (checkForm(type) == true) {
		if(type == 'referal'){
			form.method="get";
			form.target="_self";
			form.action="invitation.php";
			form.submit();
		} else {
			form.method="get";
			form.target="_self";
			form.action="process.php";
			form.submit();
		}
	}
}