/**
 * This file holds functions used in the validation of form fields.
 * Most of these functions used to be repeated throughout the jsp's. 
 */

/**
 * Returns _true_ if validation fails; _false_ otherwise.
 */
function validateTextarea(id, message) {
    var element = document.getElementById(id);
    if (!element) {
        alert("element not found: \"" + id + "\"");
        return true;
    }
    if (!element.value) {
        element.focus();
        alert(message);
        return true;
    }
    return false;
}

/**
  * Returns _true_ if validation fails; _false_ otherwise.
  */
function validateSelect(id, message) {
    var element = document.getElementById(id);
    if (!element) {
        alert("element not found: \"" + id + "\"");
        return true;
    }
    if (!element.value) {
        element.focus();
        alert(message);
        return true;
    }
    return false;
}

/**
 * Returns _true_ if validation fails; _false_ otherwise.
 */
function validateText(id, message) {
    var element = document.getElementById(id);
    if (!element) {
        alert("element not found: \"" + id + "\"");
        return true;
    }
    element.value = StringUtil_trim(element.value);
    if (!element.value) {
        element.focus();
        alert(message);
        return true;
    }
    return false;
}

/**
 * Returns _true_ if validation fails; _false_ otherwise.
 */
function validateEmail(id, optional, message) {
    var element = document.getElementById(id);
    if (!element) {
        alert("element not found: \"" + id + "\"");
        return true;
    }
    element.value = StringUtil_trim(element.value);
    if (!EmailValidation_validate(element.value, optional, false)) {
        element.focus();
        alert(message);
        return true;
    }
    return false;
}

/**
 * Returns _true_ if validation fails; _false_ otherwise.
 */
function validateDate(id, optional, message) {
    var element = document.getElementById(id);
    if (!element) {
        alert("element not found: \"" + id + "\"");
        return true;
    }
    element.value = StringUtil_trim(element.value);
    if (!DateUtil_dateIsInRange(element.value, false, 1900, 2009)) {
        element.focus();
        alert(message);
        return true;
    }
    return false;
}

/* Check if gender has been checked */
function validateGender(message) {

    var maleRadio = document.getElementById("member_gender_male");
    var femaleRadio = document.getElementById("member_gender_female");

    if (!femaleRadio.checked && ! maleRadio.checked) {
        maleRadio.focus();
        alert(message);
        return true;
    }

    return false;
}

/* Make sure the field is numeric */
function validateNumeric(id, message) {
    var element = document.getElementById(id);
    if (!element) {
        alert("element not found: \"" + id + "\"");
        return true;
    }

    if (!element.value) {
        element.focus();
        alert(message);
        return true;
    }

    element.value = StringUtil_trim(element.value);
    if (!DateUtil_isInt(element.value)) {
        element.focus();
        alert(message);
        return true;
    }
    return false;
}

/**
 * Returns _true_ if validation fails; _false_ otherwise.
 */
function validateTime(id, message) {
    var element = document.getElementById(id);
    if (!element) {
        alert("element not found: \"" + id + "\"");
        return true;
    }
    element.value = StringUtil_trim(element.value);
    if (!DateUtil_timeIsValue(element.value)) {
        element.focus();
        alert(message);
        return true;
    }
    return false;
}

/* Clear the best time fields when they receive focus */
function clearBestTime(nation) {
    
    var member_nation_best_time_hours = document.getElementById("member_" + nation + "_best_time_hours");
    member_nation_best_time_hours.onfocus = function() {
        if(this.value=='HRS') {
            this.value='';
        }
    }
    
    var member_nation_best_time_mins = document.getElementById("member_" + nation + "_best_time_mins");
    member_nation_best_time_mins.onfocus = function() {
        if(this.value=='MINS') {
            this.value='';
        }
    }
    
    var member_nation_best_time_secs = document.getElementById("member_" + nation + "_best_time_secs");
    member_nation_best_time_secs.onfocus = function() {
        if(this.value=='SECS') {
            this.value='';
        }
    }
}

/* Toggle the 5 marathon fields depending on whether the user clicked on the checkbox or not */
function validateRunBefore(nation) {

    var member_nation_never_run_before = document.getElementById("member_" + nation + "_never_run_before");
    member_nation_never_run_before.onclick = function() {
        var member_nation_last_five = document.getElementById("member_" + nation + "_last_five");
        if (member_nation_last_five.style.display == 'none') {
            member_nation_last_five.style.display = 'block';
        } else {
            member_nation_last_five.style.display = 'none';
        }
    }
}

/* Clear the time field for each marathon when that field receives focus */
function clearMarathonTime(nation) {

    var member_nation_marathon_time_0 = document.getElementById("member_" + nation + "_marathon_time_0");
    member_nation_marathon_time_0.onfocus = function() {
        if(this.value=='HRS:MINS:SECS') {
            this.value='';
        }
    }
    
    var member_nation_marathon_time_1 = document.getElementById("member_" + nation + "_marathon_time_1");
    member_nation_marathon_time_1.onfocus = function() {
        if(this.value=='HRS:MINS:SECS') {
            this.value='';
        }
    }
    
    var member_nation_marathon_time_2 = document.getElementById("member_" + nation + "_marathon_time_2");
    member_nation_marathon_time_2.onfocus = function() {
        if(this.value=='HRS:MINS:SECS') {
            this.value='';
        }
    }
    
    var member_nation_marathon_time_3 = document.getElementById("member_" + nation + "_marathon_time_3");
    member_nation_marathon_time_3.onfocus = function() {
        if(this.value=='HRS:MINS:SECS') {
            this.value='';
        }
    }
    
    var member_nation_marathon_time_4 = document.getElementById("member_" + nation + "_marathon_time_4");
    member_nation_marathon_time_4.onfocus = function() {
        if(this.value=='HRS:MINS:SECS') {
            this.value='';
        }
    }
}

/**
 * Returns _true_ if validation fails; _false_ otherwise.
 */
function validateMarathon(marathon, id, marathonMsg, cityMsg, countryMsg, yearMsg, timeMsg) {

    var name = document.getElementById("member_" + marathon + "_marathon_name_" + id);
    var city = document.getElementById("member_" + marathon + "_marathon_city_" + id);
    var country = document.getElementById("member_" + marathon + "_marathon_country_" + id);
    var year = document.getElementById("member_" + marathon + "_marathon_year_" + id);
    var time = document.getElementById("member_" + marathon + "_marathon_time_" + id);

    if (name.value != '' || city.value != '' || country.value != '' || year.value != '' || (time.value != '' && time.value != 'HRS:MINS:SECS')) {
        if (validateText("member_" + marathon + "_marathon_name_" + id, marathonMsg)) return true;
        if (validateText("member_" + marathon + "_marathon_city_" + id, cityMsg)) return true;
        if (validateSelect("member_" + marathon + "_marathon_country_" + id, countryMsg)) return true;
        if (validateNumeric("member_" + marathon + "_marathon_year_" + id, yearMsg)) return true;
        if (validateTime("member_" + marathon + "_marathon_time_" + id, timeMsg)) return true;
    }

    return false;
}

/**
 * Clean the fields holding the last marathon time.
 */
function cleanLastMarathonBestTime() {

    for (var i = 0; i < 5; i++) {
        if (document.getElementById("member_nairobi_marathon_time_" + i).value == "HRS:MINS:SECS") {
            document.getElementById("member_nairobi_marathon_time_" + i).value = "";
        }

        if (document.getElementById("member_singapore_marathon_time_" + i).value == "HRS:MINS:SECS") {
            document.getElementById("member_singapore_marathon_time_" + i).value = "";
        }

        if (document.getElementById("member_mumbai_marathon_time_" + i).value == "HRS:MINS:SECS") {
            document.getElementById("member_mumbai_marathon_time_" + i).value = "";
        }
        if (document.getElementById("member_hongkong_marathon_time_" + i).value == "HRS:MINS:SECS") {
            document.getElementById("member_hongkong_marathon_time_" + i).value = "";
        }
    }
}