var TimeSeriesRules = {
    
    //When selecting a county, populate the dropdown list of police agencies with that county's agencies.
    //Also select the state if not selected.
    'select[name|="upGeogCounty"]': function(el){
        el.onchange=function(){
            var thisindex = document.getElementsByName(this.name)[0].value;
            PopAgency(this.name.charAt(this.name.length-1), thisindex);
            if(this.value != "."){
                var thisstate = document.getElementsByName("upGeogState" + this.id.substring(this.id.length-1,this.id.length))[0];
                if(thisstate.value == ".") thisstate.options[thisstate.options.length-1].selected = true;
            }
        }
    },
    
    //Restriction of five crimes:  Disable inputs once five are selected, enable if four or less selected\
    //Only limit the crime inputs if the crime container is not in the class 'unlimited'.
    '#Inputs_Crime input': function(el){
        var unlimited = (Element.hasClassName('Inputs_Crime','unlimited'));
        if(!unlimited){
            el.onclick=function(){
                var crimechks = document.getElementsBySelector('#Inputs_Crime input[type="checkbox"]');
                var numselected = 0;
                if(crimechks.length > 0){
                    for(var i=0, j=crimechks.length; i<j; i++){
                        numselected += crimechks[i].checked ? 1 : 0;
                    }
                }
                if(numselected > 4){
                    for(var i=0, j=crimechks.length; i<j; i++){
                        crimechks[i].disabled = !crimechks[i].checked;
                    }
                } else {
                    for(var i=0, j=crimechks.length; i<j; i++){
                        crimechks[i].disabled = false;
                    }
                }
            }
        }
    },
    
    //Submission conditions
    'input[type="submit"]': function(el){
        el.onclick=function(){
            var retval = true;
            
            $('ErrorContainer').innerHTML = "";
            $('ErrorContainer').style.display = "none";
            
            //Condition:  Year range between 3 and 10 years, if selecting years.
            if($('upEndYear')){
                var yearrange = parseInt($('upEndYear').value, 10) - parseInt($('upBegYear').value, 10) + 1;
                if( (yearrange < 3) || (yearrange > 10) ){
                    ReportError("Please select between three and ten years.");
                    retval=false;
                }
            }
            
            //Condition:  One crime selected, if selecting crimes.
            if($('Inputs_Crime')){
                var crimeins = document.getElementsBySelector('#Inputs_Crime input');
                if(crimeins.length > 0){
                    var numselected = 0;
                    for(var i=0, j=crimeins.length; i<j; i++){
                        numselected += crimeins[i].checked ? 1 : 0;
                    }
                    if(numselected == 0){
                        ReportError("Please select a crime for analysis.");
                        retval=false;
                    }
                }
            }
            
            //Condition:  
            var geogins = document.getElementsBySelector('#Inputs_Geography select');
            if(geogins && geogins.length > 0){
                var anyselected = false;
                for(var i=0, j=geogins.length; i<j; i++){
                    anyselected = anyselected || geogins[i].value!=".";
                }
                if(!anyselected){
                    ReportError("Please select a geography for analysis.");
                    retval=false;
                }
            }
            return retval;
        };
    }

};
Behaviour.register(TimeSeriesRules);


var RankRules = {
    '#Inputs_GeogType':function(el){
        el.onclick=function(){
            var bDisable = GetRadioVal("upGeogType")=="County";
            var aryRads = document.getElementsByName("upFiltLowCounts");
            var arySizes = document.getElementsByName("upJurisSizes");
            for(var i=0, j=aryRads.length; i<j; i++){
                aryRads[i].disabled = bDisable;
            }
            for(var i=0, j=arySizes.length; i<j; i++){
                arySizes[i].disabled = bDisable;
            }
            return true;
        };
    },
    '#CrimeSelector':function(el){
        el.onclick=function(){ToggleCrimes(true);return false;};
    },
    '#CrimeDeselector':function(el){
        el.onclick=function(){ToggleCrimes(false);return false;};
    }
};
Behaviour.register(RankRules);
Behaviour.addLoadEvent(function(){
    var el = $('Inputs_GeogType');
    if(el) el.onclick();
});

//Set the checked or unchecked property of the checkboxes
function ToggleCrimes(bState){
    var aryCrimeChecks = $$('#Inputs_Crime input[type="checkbox"]');
    aryCrimeChecks.each(function(el){
        el.checked = bState;
    });
}

var RegistrationRules = {
    '#registrationform input[type="submit"]': function(el){
        el.onclick=function(){
            $('ErrorContainer').innerHTML = "";
            $('ErrorContainer').style.display = "none";
            
            var retval = true;
            if($('txtEmail').value == ''){
                ReportError("Please give an e-mail address.");
                retval = false;
            }
            return retval;
        };
    }
};
Behaviour.register(RegistrationRules);

//Prime selection of police agency dropdowns by simulating selection (Firefox preserves form values across page reloads)
Behaviour.addLoadEvent(function(){
    var countyselects = document.getElementsBySelector('select[name|="upGeogCounty"]');
    for(var i=0, j=countyselects.length; i<j; i++){
        PopAgency(countyselects[i].name.charAt(countyselects[i].name.length-1), i+1);
    }
});

function ReportError(strErrorDesc){
    $('ErrorContainer').style.display="block";
    $('ErrorContainer').innerHTML += "<p>" + strErrorDesc + "</p>";
}

//Creates all the options for the agency dropdown list
function PopAgency(CountySelectIndex, locIndex){
    //CountySelectIndex:  Index of the county select element (1-5)
    //locIndex:  Index of the county (1-39)

    var countyid = $F('upGeogCounty'+CountySelectIndex);
    var agencysublist = naryAgencyList[('00'+countyid).slice(-3)]; //County ID's that we're looking for are of the form 00x, or 0xx.
    agencydropdown = $("upGeogAgency"+CountySelectIndex);
    agencydropdown.options.length = 0;
    agencydropdown.options[0] = new Option('<select>', '.', false, true);
    if (countyid.slice(-1) != '.'){
        var newopt;
        for(var i=0, j=agencysublist.length; i<j; i++){
             agencydropdown.options[agencydropdown.options.length] = new Option(agencysublist[i].name, agencysublist[i].id, false, false);
        }
    }
}


