/////////////////////////////////////////////////////////////////////////////////////////////////////////   CALLING FUNCTIONS
function validateForm(form)
{
    if (!validateFirstName(form.sendersname1.value))
    {
        form.sendersname1.focus()
        return false
    }
    if (!validateLastName(form.sendersname2.value))
    {
        form.sendersname2.focus()
        return false
    }
    if (!validateAddress1(form.address1.value))
    {
        form.address1.focus()
        return false
    }
    if (!validateTown(form.address3.value))
    {
        form.address3.focus()
        return false
    }
    if (!validateCountry(form.country_code.value))
    {
        form.country_code.focus()
        return false
    }
    if (!validatePostCode(form.postcode.value))
    {
        form.postcode.focus()
        return false
    }
    if (!validateTelePhone(form.phone.value))
    {
        form.phone.focus()
        return false
    }
    if (!validateEMail(form.sendersemail.value))
    {
        form.sendersemail.focus()
        return false
    }

    return true
}


//////////////////////////////////////////////////////////////////////////////////////////////////////   isBlank FUNCTION
function isBlank(testStr)
{
    if (testStr.length == 0)
        return true

    for (var i = 0; i <= testStr.length-1; i++)
    {
        if (testStr.charAt(i) != " ")
        {
            return false
        }
    }
    return true
}


/////////////////////////////////////////////////////////////////////////////////////////////////////   CharCheck FUNCTION
function CharCheck(testStr)
{
    if ((testStr.indexOf("!") != -1) || (testStr.indexOf("¬") != -1))
        return true
    
    if ((testStr.indexOf("£") != -1) || (testStr.indexOf("$") != -1))
        return true
    
    if ((testStr.indexOf("%") != -1) || (testStr.indexOf("^") != -1))
        return true
    
    if ((testStr.indexOf("&") != -1) || (testStr.indexOf("*") != -1))
        return true
    
    if ((testStr.indexOf("(") != -1) || (testStr.indexOf(")") != -1))
        return true
    
    if (testStr.indexOf("_") != -1)
        return true
    
    if ((testStr.indexOf("-") != -1) || (testStr.indexOf("=") != -1))
        return true

    if ((testStr.indexOf("{") != -1) || (testStr.indexOf("}") != -1))
        return true

    if ((testStr.indexOf("[") != -1) || (testStr.indexOf("]") != -1))
        return true

    if ((testStr.indexOf(":") != -1) || (testStr.indexOf(";") != -1))
        return true

    if (testStr.indexOf("@") != -1)
        return true

    if ((testStr.indexOf("~") != -1) || (testStr.indexOf("#") != -1))
        return true

    if ((testStr.indexOf("<") != -1) || (testStr.indexOf(",") != -1))
        return true

    if ((testStr.indexOf(">") != -1) || (testStr.indexOf(".") != -1))
        return true

    if ((testStr.indexOf("?") != -1) || (testStr.indexOf("/") != -1))
        return true
    
    if (testStr.indexOf("|") != -1)
        return true

    return false
}

///////////////////////////////////////////////////////////////////////////////////////////////////   FirstN FUNCTION
function validateFirstName(sendersname1)
{
    var test = document.contactsubmission.sendersname1.value
    var size = test.length
    test = test.toUpperCase()

    if (isBlank(sendersname1))
    {
        alert("Enter your first name please.")
        return false
    }

    if (sendersname1.indexOf(" ", 0) != -1)
    {
        alert("Enter your first name please with no spaces.")
        return false
    }

    if (CharCheck(sendersname1))
    {
        alert("Please enter alphabetical characters only. No special characters allowed.")
        return false
    }

    for (var i = 0; i <= size-1; i++)
    {
        if (!(isNaN(test.charAt(i))))
        {
            alert("Please enter alphabetical characters only. No numbers allowed.")
            return false
        }
    }
    return true
}

///////////////////////////////////////////////////////////////////////////////////////////////////   LastN FUNCTION
function validateLastName(sendersname2)
{
    var test = document.contactsubmission.sendersname2.value
    var size = test.length
  	    test = test.toUpperCase()

    if (isBlank(sendersname2))
    {
        alert("Enter your last name please.")
        return false
    }
    if (sendersname2.indexOf(" ", 0) != -1)
    {
        alert("Enter your last name please with no spaces.")
        return false
    }
    if (CharCheck(sendersname2))
    {
        alert("Please enter alphabetical characters only. No special characters allowed.")
        return false
    }
    for (var i = 0; i <= size-1; i++)
    {
        if (!(isNaN(test.charAt(i))))
        {
            alert("Please enter alphabetical characters only. No numbers allowed.")
            return false
        }
    }
    return true
}

///////////////////////////////////////////////////////////////////////////////////////////////////   Address1 FUNCTION
function validateAddress1(address1)
{
	var test = document.contactsubmission.address1.value
	var size = test.length
  		test = test.toUpperCase()

    if (isBlank(address1))
    {
        alert("Enter your address please.")
        return false
    }

    if (CharCheck(address1))
    {
        alert("Please enter alphabetical and numerical characters only. No special characters allowed.")
        return false
    }
    return true
}

///////////////////////////////////////////////////////////////////////////////////////////////////   PostC FUNCTION
function validatePostCode(postcode)
{
    if (isBlank(postcode))
    {
        alert("Enter your postcode please.")
        return false
    }
    return true
}

///////////////////////////////////////////////////////////////////////////////////////////////////   PostC FUNCTION
function validateTown(address3)
{
    if (isBlank(address3))
    {
        alert("Enter your Town please.")
        return false
    }
    return true
}

///////////////////////////////////////////////////////////////////////////////////////////////////   Country FUNCTION
function validateCountry(country_code)
{
    if(isBlank(country_code))
    {
        alert("Select a country please.")
        return false
    }
    return true
}

///////////////////////////////////////////////////////////////////////////////////////////////////   TelNo FUNCTION
function validateTelePhone(phone)
{
    test = document.contactsubmission.phone.value
    size = test.length

    if (isBlank(phone))
    {
        alert("Enter your telephone number please.")
        return false
    }
    if (size < 6)
    {
        alert("Most telephone numbers consist of 6 or more characters.")
        return false
    }
    return true
}

///////////////////////////////////////////////////////////////////////////////////////////////////   Email FUNCTION
function validateEMail(sendersemail)
{
    test = document.contactsubmission.sendersemail.value
    size = test.length

    if (isBlank(sendersemail))
    {
        alert("Enter your email address please.")
        return false
    }

    if (size < 8)
    {
        alert("Most email addresses are a combination of 8 or more characters.")
        return false
    }

    var atsignPos = sendersemail.indexOf("@", 0)

    if (atsignPos == -1)	
    {
        alert("Enter a valid email address with an @ char please.")
        return false
    }

    if (sendersemail.indexOf(".", atsignPos) == -1)
    {
        alert("Enter a valid email address with a . after the @ please.")
        return false
    }
    return true
}
