
/*
    Checks that the string "thefield" is a valid email address and returns "true" or "false"
*/
function isEmail(thefield)
{
    if (thefield == "")
    {
        return false;
    }
    var filterNO = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)|(-@)/;
    var filterYES  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    if (!filterNO.test(thefield) && filterYES.test(thefield))
    {
		return true;
    }
	else
	{
	    return false;
	}
}

/*
    Checks that the string "thefield" is a valid austrlian phone number(with or without area code) and returns "true" or "false"
*/
function isPhoneNo(thefield)
{
    if (thefield == "")
        return false;
    var filterYES = /^(((0[2-9] ?)|(\(0[2-9]\) ?))?([0-9]{4}) ?([0-9]{4}))$/;
    if (filterYES.test(thefield))
        return true;
    else
        return false;
}

/*
    Formats the phone number from the textbox with the name "thefield" to the (02) 1234 1234 format.
    The function can/should be assigned to the onblur event of the textbox  -  onblur="formatPhoneNo(this);"
*/
function formatPhoneNo(thefield)
{
    var filterFormat = /^([0-9]{10})$/;
    if (filterFormat.test(thefield.value))
    {
        thefield.value = "(" + (thefield.value).substr(0,2) + ") " + (thefield.value).substr(2,4) + " " + (thefield.value).substr(6,10);
        return;
    }
    filterFormat = /^([0-9]{8})$/;
    if (filterFormat.test(thefield.value))
    {
        thefield.value = (thefield.value).substr(0,4) + " " + (thefield.value).substr(4,4);
        return;
    }
    filterFormat = /^(0[2-9] [0-9]{8})$/;
    if (filterFormat.test(thefield.value))
    {
        thefield.value = "(" + (thefield.value).substr(0,2) + ") " + (thefield.value).substr(3,4) + " " + (thefield.value).substr(7,11);
        return;
    }
    filterFormat = /^(0[2-9] [0-9]{4} [0-9]{4})$/;
    if (filterFormat.test(thefield.value))
    {
        thefield.value = "(" + (thefield.value).substr(0,2) + ") " + (thefield.value).substr(3,4) + " " + (thefield.value).substr(8,12);
        return;
    }
    filterFormat = /^(0[2-9][0-9]{4} [0-9]{4})$/;
    if (filterFormat.test(thefield.value))
    {
        thefield.value = "(" + (thefield.value).substr(0,2) + ") " + (thefield.value).substr(2,4) + " " + (thefield.value).substr(7,11);
        return;
    }
    filterFormat = /^(\(0[2-9]\)[0-9]{8})$/;
    if (filterFormat.test(thefield.value))
    {
        thefield.value = (thefield.value).substr(0,4) + " " + (thefield.value).substr(4,4) + " " + (thefield.value).substr(8,12);
        return;
    }
    filterFormat = /^(\(0[2-9]\) [0-9]{8})$/;
    if (filterFormat.test(thefield.value))
    {
        thefield.value = (thefield.value).substr(0,5) + (thefield.value).substr(5,4) + " " + (thefield.value).substr(9,13);
        return;
    }
    filterFormat = /^(\(0[2-9]\)[0-9]{4} [0-9]{4})$/;
    if (filterFormat.test(thefield.value))
    {
        thefield.value = (thefield.value).substr(0,4) + " " + (thefield.value).substr(4,4) + " " + (thefield.value).substr(9,13);
        return;
    }
}

/*
    Checks to see if a string is a valid postcode
*/
function isPostCode(thefield)
{
    if (thefield == "")
        return false;
    var filterYES = /^([0-9]{4})$/;
    if (filterYES.test(thefield))
        return true;
    else
        return false;
}

/*
    Impose a maxlength on a textarea. 
    Takes as parameters the field and the max length that needs to be imposed
*/
function textareaMaxLength(thefield, maxlength)
{
    return (thefield.value.length <= maxlength);
}
