function checkAGEmail(email) {
    // a very simple email validation checking. 
    // you can add more complex email checking if it helps 
    if(email.length <= 0) {
      email.value = 'EMAIL';
      return false;
    }
    var splitted = email.value.match("^(.+)@(.+)$");
    if (splitted == null) {
        email.value = 'EMAIL';
        return false;
    }
    if (splitted[1] != null ) {
        var regexp_user=/^\"?[\w-_\.]*\"?$/;
        if (splitted[1].match(regexp_user) == null) {
            email.value = 'EMAIL';
            return false;
        }
    }
    if(splitted[2] != null) {
        var regexp_domain=/^[\w-\.]*\.[A-Za-z]{2,4}$/;
        if (splitted[2].match(regexp_domain) == null) {
            var regexp_ip =/^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/;
            if (splitted[2].match(regexp_ip) == null) {
                email.value = 'EMAIL';
                return false;
            }
        } // if
        return true;
    }
    email.value = 'EMAIL';
    return false;
}

function clearAGField(element) {
    var elem = element.value;
    if (elem == 'NAME' || elem == 'POSTAL' || elem == 'ADDRESS' || elem == 'AGE' || elem == 'CITY' || elem == 'EMAIL') {
        element.value = '';
    } else if (elem == 'URL (OPTIONAL)') {
        element.value = 'http://';
    }
}

function checkAGName(entrantname) {
    if (entrantname.value.length < 2) {
        entrantname.value = 'NAME';
        return false;
    } else if (entrantname == 'NAME') {
        return false;
    } else {
        return true;
    }
}

function checkAGAddress(address) {
    if (address.value.length < 6) {
        address.value = 'ADDRESS';
        return false;
    } else if (address == 'ADDRESS') {
        return false;
    } else {
        return true;
    }
}

function checkAGCity(city) {
    if (city.value.length < 3) {
        city.value = 'CITY';
        return false;
    } else if (city == 'CITY') {
        return false;
    } else {
        return true;
    }
}

function checkAGState(state) {
    var myindex = state.selectedIndex;
    var SelValue = state.options[myindex].value
    
    if (SelValue.length == 0) {
        state.options[myindex].text = 'STATE';
        return false;
    } else if (state.options[myindex].selected == 'STATE') {
        return false;
    } else {
        return true;
    }
}

function checkAGCategory(category) {
    var myindex = category.selectedIndex;
    var SelValue = category.options[myindex].value
    
    if (SelValue.length == 0) {
        category.options[myindex].text = 'CATEGORY';
        return false;
    } else if (category.options[myindex].selected == 'CATEGORY') {
        return false;
    } else {
        return true;
    }
}

function checkAGPostal(postal) {
    if (postal.value.length < 3) {
        postal.value = 'POSTAL';
        return false;
    } else if (postal.value == 'POSTAL') {
        return false;
    } else {
        return true;
    }
}

function checkAGAge(age) {
    if (age.value.length != 2) {
        age.value = 'AGE';
        return false;
    } else if (age.value == 'AGE') {
        return false;
    } else {
        return true;
    }
}

function checkAGUrl(url) {
    if (url.value.length < 1) {
        url.value = "URL (OPTIONAL)";
        return true;
    } else if (url.value == 'http://') {
        url.value = "URL (OPTIONAL)";
        return true;
    } else if (url == 'URL (OPTIONAL)') {
        return true;
    } else if (url.length > 0) {
        var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
        return regexp.test(url);
    } else {
        return true;
    }
}

function checkAGBio(bio) {
    if (bio.length < 10) {
        return false;
    } else {
        return true;
    }
}

function checkAGSynopsis(synopsis) {
    if (synopsis.length < 10) {
        return false;
    } else {
        return true;
    }
}

function checkTextAreaLimit(element, limit, remaining_element) {
    if (element.value.length > limit) {
        element.value = element.value.substring(0,400);
    }
    if ((400 - element.value.length) == 0) {
        remaining_element.innerHTML = '<span style="color: red;">0</span>'; 
    } else {
        remaining_element.innerHTML = (400 - element.value.length).toString();
    }
}

function areThereQueuedFiles() {
    var x=0;
    var counter=0;
    for (x=0; x<10; x++) {
        if (document.getElementById('ag_file_'+x.toString()).value.length > 4) {
            counter++;
        }
    }

    if (counter > 0) {
        return true;
    } else {
        alert("Unable to submit your entry.  You have not added any files to be submitted. Please ensure that at least one or more file(s) in .jpg, .gif or .png format have been selected for uploading then press the submit button.");
        return false;
    }
}

function isAGFormOkayToSubmit() {
    var name = document.getElementById('id_name');
    var address = document.getElementById('id_address');
    var city = document.getElementById('id_city');
    var state = document.getElementById('id_state');
    var postal = document.getElementById('id_postal');
    var age = document.getElementById('id_age');
    var email = document.getElementById('id_email');
    var url = document.getElementById('id_url');
    var bio = document.getElementById('id_bio');
    var synopsis = document.getElementById('id_synopsis');
    var category = document.getElementById('id_category');
    
    if (checkAGEmail(email) && checkAGUrl(url) && checkAGName(name) && checkAGAddress(address) && checkAGCity(city) && checkAGState(state) && checkAGPostal(postal) && checkAGAge(age) && checkAGBio(bio) && checkAGSynopsis(synopsis) && checkAGCategory(category)) {
        document.getElementById('id_url').value = '';
        return areThereQueuedFiles();
    } else {
        alert("Unable to submit your entry.  Please make sure that all required fields are filled out correctly, that a category has been selected and that any URL (if provided) is valid and starts with 'http://'");
    }
    return false;
}
