// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

var visible_segments = 1;


function show_next_segment() {
    jQuery("#segments_" + (++visible_segments)).show("normal").slideDown();
    $("new_race_segment_count").value = visible_segments;
}

// directly calls onsubmit handler for remote forms
// using the sprite link-buttons
function remote_sprite_submit(element, non_null_elements, mode) {
   if (valid_dependencies(non_null_elements, mode)) {
       $(element).hide(0);
       $(element).up('form').onsubmit();
    }
}


// submit forms using the sprite link-buttons
function sprite_submit(element, non_null_elements, mode) {
    //alert(non_null_elements);
    if (valid_dependencies(non_null_elements, mode)) {
        $(element).hide(0);
        $(element).up('form').submit();
    }
}

function valid_dependencies(non_null_elements, mode) {
    //alert("need to validate " + non_null_elements.length);
    valid = true;
    true_count = 0;
    for (i = 0; i < non_null_elements.length; i++) {
        name = non_null_elements[i];
        el = $(name);
        if (el == null) {
            alert(name + " is not a valid form element, fix your code")
            valid = false;
        } else {
            if (el.value == null || el.value.length < 1) {                
                valid = false;
            } else {
                //alert(el.value);
                ++true_count
            }
        }     
    }
    //alert("mode is " + mode + " and truth count is " + true_count)
    if ("or" == mode && true_count > 0) {
        valid = true
    }
    if (!valid) {
        //alert("An entry is required to proceed");
        jQuery('#error_block').show('normal');
    } else {
        jQuery('#error_block').hide('normal');
    }
    
    return valid;
}

function incomplete(result) {
    $('write_report_minutes').value = '00';
    $('write_report_hours').value = '00';
    $('write_report_seconds').value = '00';
    $('write_report_dnf_or_dq').value = result;
    document.forms[0].submit();
}