function addChurch_validate()
{
    var errors = false;
    
    with (document.formAddChurch.churchname)
    {
         with (document.formAddChurch.churchname)
         {
            if (!validateRequired("custom-form-churchname-error", value))
                errors = true;    
         }     
    }
    
    with (document.formAddChurch.address)
    {
        if (!validateRequired("custom-form-address-error", value))
                errors = true;
    }
    
    with (document.formAddChurch.city)
    {
        if (!validateRequired("custom-form-city-error", value))
                errors = true;
    }
    
    with (document.formAddChurch.state)
    {
        if (!validateRequired("custom-form-state-error", value))
                errors = true;
    }
    
    with (document.formAddChurch.zip)
    {
        if (!validateRequired("custom-form-zip-error", value))
                errors = true;
    }
    
    with (document.formAddChurch.phone)
    {
        if (!validateRequired("custom-form-phone-error", value))
                errors = true;
    }
    
    with (document.formAddChurch.email)
    {
        if (!validateRequired("custom-form-email-error", value))
        {
            errors = true;
        }
        else if (!validateEmailAddress(value))
        {
            document.getElementById("custom-form-email-error").innerHTML = "Invalid email address"
            document.getElementById("custom-form-email-error").style.height = "15px"; 
            errors = true;            
        }
        else
        {
            document.getElementById("custom-form-email-error").innerHTML = "";  
            document.getElementById("custom-form-email-error").style.height = "0px";
        }   
    }
    
    with (document.formAddChurch.website)
    {  
        if (value != null && value != "" && !validateUrl(value))
        {
            document.getElementById("custom-form-website-error").innerHTML = "Invalid web address"
            document.getElementById("custom-form-website-error").style.height = "15px"; 
            errors = true;            
        }
        else
        {
            document.getElementById("custom-form-website-error").innerHTML = ""; 
            document.getElementById("custom-form-website-error").style.height = "0px";
        }         
    }
    
    with (document.formAddChurch.government)
    {
        if (!validateRequired("custom-form-government-error", value))
                errors = true;     
    }
    
    with (document.formAddChurch.worshipstyle)
    {
        if (!validateRequired("custom-form-worshipstyle-error", value))
                errors = true;     
    }
    
    with (document.formAddChurch.description)
    {
        if (!validateRequired("custom-form-description-error", value))
                errors = true;
    }
    
    with (document.formAddChurch.accesscode)
    {
        if (!validateRequired("custom-form-accesscode-error", value))
                errors = true;
    }   
    
    if (!errors) 
        document.formAddChurch.submit();   
}

function validateRequired(elementName, value)
{
    document.getElementById(elementName).innerHTML = "";
    document.getElementById(elementName).style.height = "0px";
        
    if (value == null || value == "")
    {
        document.getElementById(elementName).innerHTML = "This field is required"
        document.getElementById(elementName).style.height = "15px";
        return false;    
    }     
    
    return true;
}

function validateEmailAddress(emailaddress) 
{
    var at = "@";
    var dot = ".";
    var lat = emailaddress.indexOf(at);
    var lstr = emailaddress.length;
    var ldot = emailaddress.indexOf(dot);

    if (emailaddress.indexOf(at) == -1 || emailaddress.indexOf(dot) == -1)
        return false;

    if (emailaddress.indexOf(at) == 0 || emailaddress.indexOf(at) == lstr)
        return false;

    if (emailaddress.indexOf(dot) == 0 || (emailaddress.indexOf(dot) + 1) == lstr)
        return false;

    if (emailaddress.indexOf(at, (lat + 1))!= -1)
        return false;

    if (emailaddress.substring(lat - 1, lat) == dot || emailaddress.substring(lat + 1,lat + 2) == dot)
        return false;

    if (emailaddress.indexOf(dot, (lat + 2)) == -1)
        return false;

    if (emailaddress.indexOf(" ") != -1)
        return false;

    return true;                    
}

function validateUrl(url) 
{
    var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
    return regexp.test(url);
}

function setState(elementName, value) 
{
    var element = document.getElementById(elementName);
    for (var i = 0; i < element.length; i++) 
    {
        if(element[i].value == value)
            element.selectedIndex = i;
    }
}
