function select_all(_v, listName) {
    var lists = document.getElementsByName(listName);
    for(var i=0;i<lists.length;i++) {
        lists[i].checked=_v;
    }
}

function setOptions() {
//    alert ( "setOptions()" );
    var hidden_option_values = document.getElementsByName( "option_value" );
    setPartitions( hidden_option_values );
    setStratificationAndText( hidden_option_values );

    hideModalIfAllSet();
}

function setPartitions( hidden_option_values ) {
    var partition_selects = document.getElementsByName( "partition_select" );
    var partition_set = 0;

    var current_partitions = new Array(partition_selects.length);
    for ( var i=0; i < partition_selects.length; i++ ) {
        current_partitions[i] = partition_selects[i];
    }

    for ( var i=0; i < current_partitions.length; i++ ) {
        var current_partition = current_partitions[i].id;
//        alert( "New partition select(i=" + i + "): " + current_partition );

        var selected = current_partitions[i].selectedIndex;
        if ( current_partitions[i].options[selected].value == "" ) {
//            alert( current_partitions[i].id + " options[selected] empty" );
            var partition_found = 0;
            for ( var j=0; j < hidden_option_values.length; j++ ) {
                var hidden_pieces = hidden_option_values[j].id.split( "_" );
                var hidden_id = hidden_pieces[0];
                var hidden_option = hidden_id + "=" +
                    hidden_option_values[j].value;
//                alert( current_partition + ": New hidden option(j=" +
//                    j + "): "+ hidden_option );

                if ( current_partitions[i].id == hidden_id ) {
                    partition_found = 1;
                    for ( var z=0; z < current_partitions[i].options.length; z++ ) {
                        if ( current_partitions[i].options[z].value == hidden_option_values[j].value ) {
//                            alert( "Setting select" );
                            current_partitions[i].selectedIndex = z;
                            partition_set = 1;
//                            alert ( "Calling partitionChanged(" + current_partitions[i].id + ")" );
                            partitionChanged( current_partitions[i].id );
//                            alert ( "Called partitionChanged" );
                            elementSet( hidden_id );
                            break;
                        }
                    }
                }
                if ( partition_found ) {
                    partition_found = 0;
                    break;
                }
            }
        } else {
//            alert ( current_partitions[i].id + " options[selected] NOT empty" );
              elementSet( current_partitions[i].id );
        }
    }
}

function setStratificationAndText( hidden_option_values ) {
    for ( var j=0; j < hidden_option_values.length; j++ ) {
        var hidden_pieces = hidden_option_values[j].id.split( "_" );
        var hidden_id = hidden_pieces[0];
        var hidden_option = hidden_id + "=" +
            hidden_option_values[j].value;
//        alert( "New hidden option(j=" + j + "): "+ hidden_option );

        var stratification = document.getElementById( hidden_id + "_status" );
        if ( stratification ) {
//            alert( "New stratification found: " + hidden_id );

            if ( stratificationSet( hidden_id ) == 0 ) {
//                alert( "Unset stratification found: " + hidden_id );

                var hidden_regexp = new RegExp(hidden_id, "i");

                var hidden_regexp_source = hidden_regexp.source;
//                alert( "Regexp created for: " + hidden_regexp_source );

                var links = document.getElementsByTagName( 'A' );
                var url;
                for ( var z=0; z < links.length; z++ ) {
//                    alert( "Found link: " + links[z].pathname );
                    var matches = links[z].pathname.search( /edit/, "i" );
//                    alert( "Matches to edit: " + matches );
                    if ( links[z].pathname.search( /edit/, "i" ) >= 0 ) {
//                        alert( "Link matches edit now checking against " + hidden_regexp_source  );
                        if ( links[z].pathname.search( hidden_regexp ) >= 0 ) {
//                            alert( "Link " + links[z].pathname + " matches setting url." );
                            url = links[z].pathname.replace( /edit-stratification/i,
                                'save-stratification' );
                            if ( url.search( /^\// ) == -1 ) {
                                url = '/' + url;
                            }
                        }
                    }
                }

                if ( url ) {
//                    alert( "hidden_id url: " + url + "; Value: " +
//                        hidden_option_values[j].value );
                    var options = hidden_option_values[j].value.split( ',' );
                    var c_value = "";
                    for ( var i=0; i < options.length; i++ )
                    {
                        c_value = c_value + '&' + hidden_id + '=' + options[i];
                    }
//                    alert( "Sending ajax request: url: " + url + " parameters : element_id=" + hidden_id + c_value );
                    new Ajax.Request( url, {
                        parameters: 'element_id=' + hidden_id + c_value,
                        asynchronous: 1,
                        onComplete: function( request ) {eval(request.responseText)}
                    } );
                }
            } else {
//                alert( "Set stratification found: " + hidden_id );
            }
            elementSet( hidden_id );
        }
        var text = document.getElementById( hidden_id );
        if ( text ) {
            if ( text.type == 'text' ) {
                if ( text.value == "" ) {
                    text.value = hidden_option_values[j].value;
                }
                elementSet( hidden_id );
            }
        }
    }
}

function hideModalIfAllSet() {
//    alert( 'hideModalIfAllSet' );
    if ( checkAllSet() ) {
        hm();
    }
}

function checkAllSet() {
//    alert( 'checkAllSet' );
    var option_sets = document.getElementsByName( 'option_set' );
    for ( var i=0; i < option_sets.length; i++ ) {
//        alert( option_sets[i].id + ': ' + option_sets[i].value );
        if ( option_sets[i].value == 'false' ) {
//            alert( "returning false" );
            return false;
        }
    }

//    alert( "returning true" );
    return true;
}

function elementSet( id ) {
//    alert( "elementSet: " + id );
    var hidden = document.getElementById( id + '_set' );
    if ( hidden ) {
        hidden.value = 'true';
//      alert( id + ": " + hidden.value );
    }
}

function partitionChanged( partition ) {
  var url = '/partition/'+partition+'/'+document.getElementById(partition).value;
  var element_id = 'element_id=' + partition;
  new Ajax.Request( url, {
    parameters: element_id,
    asynchronous: 1,
    onComplete: function( request ) {eval(request.responseText);removeBlank(partition);selectSingle();setOptions()}
  } );
}

function selectSingle() {
    var partition_selects = document.getElementsByName( "partition_select" );
    for( var i=0; i< partition_selects.length; i++ ) {
        if ( partition_selects[i].length == 2 && partition_selects[i].options[0].value == "" ) {
            if ( partition_selects[i].selectedIndex == 0 ) {
                partition_selects[i].selectedIndex = 1;
                partitionChanged( partition_selects[i].id );
            }
        }
    }
}

function getCheckBoxValue( check_name ) {
    var c_value = "";
    var check_box = document.getElementsByName( check_name );
    for ( var i=0; i < check_box.length; i++ )
    {
        if ( check_box[i].checked ) {
            c_value = c_value + '&' + check_name + '=' + check_box[i].value;
        }
    }

    return c_value;
}

function getHiddenValues( hidden_name ) {
    var element_name = hidden_name;
    var h_value = "";
    hidden_name = hidden_name + '_status';
    var span = document.getElementById( hidden_name );
    var hidden = span.getElementsByTagName( 'input' );
    for ( var i=0; i < hidden.length; i++ ) {
        h_value = h_value + '&' + element_name + '=' + hidden[i].value;
    }

    return h_value;
}

function stratificationSet( stratification_name ) {
    var element_name = stratification_name;
    stratification_name = stratification_name + '_status';
    var span = document.getElementById( stratification_name );
//    alert( "stratificationSet: " + stratification_name + " span.nodeName: " + span.nodeName + " span.nodeType: " + span.nodeType );
    for ( i=0; i < span.childNodes.length; i++ ) {
//        alert( "stratificationSet: " + stratification_name + " child.nodeName: " + span.childNodes[i].nodeName + " child.nodeType: " + span.childNodes[i].nodeType );
        if ( span.childNodes[i].nodeName=="INPUT" ) {
            return 1;
        }
    }
    return 0;
}

function getPartitionValues() {
    var p_values = "";
    var partitions = document.getElementsByName( 'partition_select' );
    for ( var i = 0; i < partitions.length; i++ ) {
        p_values = p_values + '&' + partitions[i].id + '=' + partitions[i].value;
    }

    return p_values;
}

function getStratificationValues() {
    var s_values = "";
    var stratifications = document.getElementsByName( 'stratification_select' );
    for ( var i = 0; i < stratifications.length; i++ ) {
        var parentNode = stratifications[i].parentNode;
        var parentNodeId = parentNode.id;
        parentNodeId = parentNodeId.replace(/_status/, '' );
        s_values = s_values + '&' + parentNodeId + '=' + stratifications[i].value;
    }

    var input = document.getElementsByTagName( 'input' );
    for ( var i=0; i < input.length; i++ ) {
       if ( input[i].type == 'checkbox' && input[i].checked ) {
           s_values = s_values + '&' + input[i].name + '=' + input[i].value;
       }
    }

    return s_values;
}

function getTextValues() {
    var t_values = "";
    var texts = document.getElementsByName( 'text_input' );
    for ( var i = 0; i < texts.length; i++ ) {
        if ( texts[i].value != '' ) {
            t_values = t_values + '&' + texts[i].id + '=' + texts[i].value;
        }
    }

    return t_values;
}


function currentlyEditing() {
    if ( document.getElementById( 'editing_stratification' ) ) {
        alert( "Please finish editing stratification." );
        return true;
    } else {
        return false;
    }
}

function removeBlank( select_name ) {
    var select = document.getElementById( select_name );
    if ( select.options[0].value == "" ) {
        select.options[0] = null;
    }
}
