function isPhoneValid(field, type)
<!--- function checks to see if the phone consists of invalid characters --->
{
	returnValue = true;
	<!--- strip out acceptable non-numeric characters --->
	var stripped = field.value.replace(/[\(\)\.\-\ ]/g, '');
	if (isNaN(stripped)) 
	<!--- if contains illegal characters --->
	{
		returnValue = false;
		alert("The " + type + " contains illegal characters.");
		field.focus();
		field.select();
	}
	else if (stripped.length < 7)
	<!--- if length is less than 7 digits --->
	{
		returnValue = false;
		alert("The " + type + " is not a valid phone number");
		field.focus();
		field.select();
	}
	return returnValue;
}

function formatPhone(field, type)
{
	if (field.value != "")
	{
		formattedString = "";
		if (isPhoneValid(field, type))
		{
			<!--- strip off invalid characters --->
			phoneString = field.value.replace(/[\(\)\.\-\ ]/g, '');
			if (phoneString.length == 8)
			<!--- if only phone number inserted, no area code, mask to xxxx xxxx --->
			{
				firstString = phoneString.substring(0,4);
				secondString = phoneString.substring(4,8);
				formattedString = firstString + " " + secondString;
			}
			else if ((phoneString.length == 10) && (phoneString.substring(0,2) != "04"))
			<!--- if area code inserted as well, none mobile, mask to xx xxxx xxxx --->
			{
				firstString = phoneString.substring(0,2);
				secondString = phoneString.substring(2,6);
				thirdString = phoneString.substring(6,10);
				formattedString = firstString + " " + secondString + " " + thirdString;
			}
			else if ((phoneString.length == 10) && (phoneString.substring(0,2) == "04"))
			<!--- for mobile phone, mask to xxxx xxxxxx --->
			{
				firstString = phoneString.substring(0,4);
				secondString = phoneString.substring(4,7);
				thirdString = phoneString.substring(7,10);
				formattedString = firstString + " " + secondString + " " + thirdString;
			}
			else if (phoneString.length == 11)
			<!--- if contains country code, mask to xxx xxxx xxxx --->
			{
				firstString = phoneString.substring(0,3);
				secondString = phoneString.substring(3,7);
				thirdString = phoneString.substring(7,11);
				formattedString = firstString + " " + secondString + " " + thirdString;			
			}
			if (formattedString != "") 
			{
				<!--- set the mask string to the form --->		
				field.value = formattedString;
			}
		}
	}
}

function formatMobile(field, type)
{
	if (field.value != "")
	{
		<!--- strip off invalid characters --->
		phoneString = field.value.replace(/[\(\)\.\-\ ]/g, '');
		if (phoneString.length == 10)
		<!--- mobile phone must always contain 10 characters --->
		{
			if (isPhoneValid(field, type))
			{
				firstString = phoneString.substring(0,4);
				secondString = phoneString.substring(4,7);
				thirdString = phoneString.substring(7,10);
				formattedString = firstString + " " + secondString + " " + thirdString;		
				<!--- set the mask string to the form --->
				field.value = formattedString;
			}
		}
		else
		{
			returnValue = false;
			alert("The " + type + " is not a valid phone number");
			field.focus();
			field.select();	
		}
	}
}

function isValidEmail(field) 
<!--- function to check for email address --->
{
	str = field.value
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
		alert("The email address entered is invalid")
		return false
	}
	else if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		alert("The email address entered is invalid")
		return false
	}
	else if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
	    alert("The email address entered is invalid")
	    return false
	}
	else if (str.indexOf(at,(lat+1))!=-1){
		alert("The email address entered is invalid")
	    return false
	}
	else if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		alert("The email address entered is invalid")
	    return false
	}
	else if (str.indexOf(dot,(lat+2))==-1){
		alert("The email address entered is invalid")
	    return false
	}	
	else if (str.indexOf(" ")!=-1){
		alert("The email address entered is invalid")
	    return false
	}
	return true			
}

function checkEmail(field)
{
	if (field.value != "")
	{
		if (!isValidEmail(field))
		{
			field.focus();
			field.select();	
		}
	}	
}