﻿//-------------------------------------------------------------------------------------------------
//  ChecksDivManager: Responsible for CourtType- and Court- checklists functionality.
//-------------------------------------------------------------------------------------------------

var checksDivManager = 
{
    target: {}, // object containing div and hidden input ID of courts check-list
    //selectedCourtTypes: [], // array of current selected court-types
    //savedCourts: [], // saved courts array in case query was requested for load
    //courtsHid:  function() // hidden input containing current court IDs
    //{
    //  return document.getElementById(checksDivManager.target.hiddenId);   
    //},
    //allCourtsCheck: function()
    //{
    //    return document.getElementById('allCourtsCheck');
    //},
    //allCourtTypesCheck: function()
    //{
    //    return document.getElementById('allCourtTypesCheck')
    //},
    
    // param "courts": csv string of court-ID values to initiate or null.
    // param "courtTypes": array of court-type-ID values to initiate or null.
    //Init: function(divId, hiddenId, courts, courtTypes)
    Init: function(divId, hiddenId)
    {
        this.target = 
        {
            divId: divId,
            hiddenId: hiddenId
        }
        
//        if(courtTypes != '')
//        {
//            courtTypes = eval(courtTypes);
//            this.SetMultipleCourtTypes(courtTypes)
//        }
//        else
//        {
//            // Set all court types
//            this.allCourtTypesCheck().checked = true;  
//            this.SetAllCourtTypes(true);
//        }
        
//        if(courts != '')
//            this.savedCourts = eval(courts);
//        else
//            // Check all courts check
//            this.allCourtsCheck().checked = true;            
    },
    
    // Set saved court values to all
//    ClearCourtValues: function()
//    {
//        checksDivManager.courtsHid().value = '0';
//    },
    
    // Add/Remove a selected court value
//    SetCourtValue: function(value, checked)
//    {
//        var hidden = checksDivManager.courtsHid();
//        
//        if(checksDivManager.allCourtsCheck().checked)
//        {
//            checksDivManager.allCourtsCheck().checked = false;        
//            
//            // allCourtsCheck was checked: Clear the hidden's value of all courts or 0
//            hidden.value = '';
//        }
//        
//        value = value + ',';
//    
//        if(checked)
//        {
//            // add value and comma
//            hidden.value += value;
//        }
//        else
//        {
//            // remove value and comma
//            hidden.value = hidden.value.replace(value, '');
//        }
//    },
    
//    SetCourtType: function(value, checked)
//    {
//        checksDivManager.allCourtTypesCheck().checked = false;
//        
//        if(this.selectedCourtTypes[0] == 0)
//            // Remove 0 from array
//            this.selectedCourtTypes.splice(0,1);
//        
//        if(checked)
//        {
//            // Add value to array
//            this.selectedCourtTypes.push(value);
//        }
//        else
//        {
//            // get index of item
//            var i = ArrayIndexOf(this.selectedCourtTypes, value);
//            
//            if(i >= 0)
//                // Remove value from array
//                this.selectedCourtTypes.splice(i,1);
//        }
//           
//        GetCourtsByCourtType(this.selectedCourtTypes);            
//    },
//    
//    // Set selected court-types array to a specified array of values: used for reloading a query
//    // param "values": array of court-type IDs
//    SetMultipleCourtTypes: function(values)
//    {
//        checksDivManager.allCourtTypesCheck().checked = false;
//        
//        // Set array values
//        this.selectedCourtTypes = values;
//        
//        //
//        // Run through all court-types check-boxes and check al requested checkboxes.
//        //
//        ExecFuncOnElements
//        (
//            'checkbox', 'courtTypeCheck',
//            function(checkbox)
//            {
//                if(ArrayIndexOf(values, checkbox.value) > -1)
//                {
//                    checkbox.checked = true;
//                }
//            }
//        );
//        
//        GetCourtsByCourtType(values);            
//    },
//    
//    // Set selected court-types array to a specified array of values: used for reloading a query
//    // param "values": array of court IDs
//    SetMultipleCourts: function(values)
//    {
//        checksDivManager.allCourtsCheck().checked = false;        
//        var hid = checksDivManager.courtsHid();
//        
//        // clear values
//        hid.value = '';
//        
//        //
//        // Run through all court check-boxes and check al requested checkboxes and add their values to the hidden input.
//        //
//        ExecFuncOnElements
//        (
//            'checkbox', 'listDivCheck',
//            function(checkbox)
//            {
//                if(ArrayIndexOf(values, checkbox.value) > -1)
//                {
//                    checkbox.checked = true;
//                    
//                    // Add court-ID to hidden input
//                    hid.value += checkbox.value + ',';
//                }
//            }
//        );
//    },
//    
//    SetAllCourtTypes: function(isChecked)
//    {
//        if(isChecked)
//        {
//            // Set array to all
//            this.selectedCourtTypes = [0];
//            
//            // Uncheck all
//            ExecFuncOnElements('checkbox', 'courtTypeCheck', function(checkbox){checkbox.checked = false;});
//        }
//        else
//        {
//            // Clear array
//            this.selectedCourtTypes = [];
//        }
//        
//        GetCourtsByCourtType(this.selectedCourtTypes);
//    },
//    
//    // allCourtsCheck has been checked
//    SetAllCourts: function(checked)
//    {
//        if(checked == null)
//            checked = checksDivManager.allCourtsCheck().checked;
//    
//        if(!checked)
//        {
//            // Clear saved court values
//            checksDivManager.courtsHid().value = '';
//            return;
//        }
//        
//        var isAll = checksDivManager.allCourtTypesCheck().checked;
//        var hid = checksDivManager.courtsHid();
//        
//        // Set saved court value to all courts if allCourtTypesCheck checked, else to empty
//        hid.value = isAll ? '0' : '';
//        
//        //
//        // Run through all court-check-boxes and uncheck and, if this is not all court-types, 
//        // add court-IDs to hidden input
//        //
//        ExecFuncOnElements
//        (
//            'checkbox', 'listDivCheck',
//            function(checkbox)
//            {
//                checkbox.checked = false;
//                
//                if(!isAll)
//                    // Add court-ID to hidden input
//                    hid.value += checkbox.value + ',';
//            }
//        );
//    }, 
    
    Validate: function(source, args)
    {
        args.IsValid = true;
    
        if(divManager.IsPanelVisible('dataPanel'))
            args.IsValid = checksDivManager.courtsHid().value !== '';
    }
}

//function GetCourtsByCourtType(courtTypes)
//{
//    ajaxHelper.ShowProgress('GetCourtsByCourtType',checksDivManager.target.divId)
//    
//    if(checksDivManager.allCourtsCheck().checked == false)
//        checksDivManager.savedCourts = eval('[' + checksDivManager.courtsHid().value + ']');
//    
//    SearchEngineService.GetCourtsByCourtType(courtTypes, GetCourtsByCourtType_Complete);    
//}

//function GetCourtsByCourtType_Complete(result)
//{
//    //alert(checksDivManager.savedCourts);
//    document.getElementById(checksDivManager.target.divId).innerHTML = result != null ? result : '';
//    
//    if(checksDivManager.savedCourts && checksDivManager.savedCourts.length > 0)
//    {
//        // Set the court values from the requested query
//        checksDivManager.SetMultipleCourts(checksDivManager.savedCourts);
//        checksDivManager.savedCourts = null;
//    }
//    else if(checksDivManager.allCourtsCheck().checked)
//    {
//        // reset hidden value to new values    
//        checksDivManager.SetAllCourts();
//    }
//}

//-------------------------------------------------------------------------------------------------
//  DivManager: Responsible for all panel functionality as well as general page functionality.
//-------------------------------------------------------------------------------------------------

var divManager = 
{
    poolTypes: 
    {
        0: { type: 'All', byname: ''},
        1: { type: 'Laws', byname: 'שם החוק'},
        2: { type: 'Verdicts', byname: ''},
        3: { type: 'Plees', byname: ''},
        4: { type: 'Bills', byname: 'שם הצעת חוק'},
        5: { type: 'LegalGuideAndPapers', byname: 'ערך משפטי'},
        6: { type: 'FormsAndContracts', byname: 'שם הטופס/החוזה'}
    },
    
    panels: ['subjectsPanel','maagarimPanel','wordsPanel','datePanel'],
    
    poolPanels: 
    {
        All: ['wordsPanel','maagarimPanel','subjectsPanel','datePanel'],
        Laws: ['titlePanel','wordsPanel','datePanel',],
        Verdicts: ['dataPanel','wordsPanel','datePanel','refsPanel'],
        Bills: ['titlePanel','subjectsPanel','datePanel'],
        LegalGuideAndPapers: ['titlePanel','subjectsPanel','wordsPanel','datePanel','refsPanel'],
        Plees: ['dataPanel','subjectsPanel','wordsPanel','datePanel','refsPanel'],
        FormsAndContracts: ['titlePanel','subjectsPanel']
    },
    
    // ID of hidden input the saves the current poolId
    poolHiddenId: '',       
    
    poolHidden: 
    {
        Get: function()
        {
            return document.getElementById(divManager.poolHiddenId).value;
        },
        Set: function(value)
        {
            document.getElementById(divManager.poolHiddenId).value = value;
        }
    },
    
    Init: function(poolHiddenId)
    {                        
        var poolId = 0;
        this.poolHiddenId = poolHiddenId;
        this.poolHidden.Set(poolId);
    
        $j(".panel").css("display", "inline");
        this.ShowPoolPanels(poolId, false);
    },
    
//    Init: function(maagarimDropId, poolHiddenId, byNameSmartDropKey)
//    {
//        var maagarimDrop = document.getElementById(maagarimDropId);    
//        var poolId = maagarimDrop.options[maagarimDrop.selectedIndex].value; //Get current poolId
//        
//        this.byNameSmartDropKey = byNameSmartDropKey;
//        this.maagarimDropId = maagarimDropId;
//        this.poolHiddenId = poolHiddenId;
//        this.poolHidden.Set(poolId);
//    
//        $j(".panel").css("display", "inline");
//        this.ShowPoolPanels(poolId, false);
//    },
    
    // param "doClearDrops": True to clear all smartDrop selections, else false.
    ShowPoolPanels: function(poolId, doClearDrops)
    {
        divManager.poolHidden.Set(poolId);
        //document.getElementById('byNameSpan').innerHTML = divManager.poolTypes[poolId].byname;  
        
        // Get poolType
        var poolType = divManager.poolTypes[poolId].type;

        if(doClearDrops)
            // Clear all smartDrop selections
            smartDropManager.ClearAllDrops();
        
        // Change the byNameSmartDrop's poolType
        //smartDropManager.ChangeDropPool(divManager.byNameSmartDropKey, poolType);
        
        var displayedPanels = divManager.poolPanels[poolType];
        
        // Display and hide appropriate panels by poolType
        for(var i = 0; i < divManager.panels.length; i++)
        {
            var panel = divManager.panels[i];
            var displayed = false;
        
            for(var j = 0; j < displayedPanels.length; j++)
            {
                if(panel == displayedPanels[j])
                {
                    displayed = true;
                    break;
                }
            }
            
            document.getElementById(panel).style.display = displayed ? 'inline' : 'none';      
        }
        
//        if(divManager.IsPanelVisible('subjectsPanel'))
//        {
//            // Set categories control poolId
//            selectsManager.SetPool(poolId);
//        }
        
        var briefsCheckSpan = document.getElementById('briefsCheckSpan');
        var byNameDiv = document.getElementById('byNameDiv');
        var authorRow = document.getElementById('authorRow');
        var ministriesRow = document.getElementById('ministriesRow');
        
        //briefsCheckSpan.style.display = 'none';
        //byNameDiv.style.display = 'inline';
        //authorRow.style.display = 'none';
        //ministriesRow.style.display = 'none';
        
        switch(poolType)
        {
            case 'Verdicts':
                briefsCheckSpan.style.display = '';
                break;    
              
            case 'FormsAndContracts':
                // Show נמען drop down
                ministriesRow.style.display = '';
                break;
              
            case 'LegalGuideAndPapers':
                // Show author smart-drop
                authorRow.style.display = '';
                break;
        }
    },
    
    IsPanelVisible: function(panelName)
    {
        var poolId = divManager.poolHidden.Get();
        var poolType = divManager.poolTypes[poolId].type;
        var panels = divManager.poolPanels[poolType];
        var s = panels.toString();
        return s.indexOf(panelName) > -1;
    },
    
    // param "maagarimDropId": send maagarimDropId, else null
    ResetForm: function()
    {
        // Save current maagarId (for IE)
        var maagarId = divManager.poolHidden.Get();
    
        // Call ResetForm in CommonScripts.js
	    ResetForm();
        
        // return maagarimDrop to saved index
        //document.getElementById(divManager.maagarimDropId).value = maagarId;
        
        // Reset maagarId (for IE)
        divManager.poolHidden.Set(maagarId);
        
        // Reset courts and courtType check lists
        //checksDivManager.SetAllCourtTypes(true);
        
        // Reset selected categoryId to 0.
        //selectsManager.SetCategory(0);
    }
}

// Separate procedure numbers into appropriate textboxes
/*function FixProcNums(numTxt, monthId, yearId)
{
    var parts = numTxt.value.split('-');
    
//    if(parts.length == 1)
//        parts = parts.split('/');
        
    if(parts.length == 3)
    {
        numTxt.value = parts[0];
        document.getElementById(monthId).value = parts[1];
        document.getElementById(yearId).value = parts[2];
        
//        if(parts.length == 2)
//        {
//            yearTxt.value = parts[1];
//        }
//        else if(parts.length == 3)
//        {
//            document.getElementById(monthId).value = parts[1];
//            yearTxt.value = parts[2];
//        }
    }
}*/

//-------------------------------------------------------------------------------------------------
//  Validator: Responsible for validation as well as the date functionality
//-------------------------------------------------------------------------------------------------

var validator = 
{
    fromDateId: '',
    toDateId: '',
    selectId: '',

    Init: function(fromDateId, toDateId, selectId)
    {
        this.fromDateId = fromDateId;
        this.toDateId = toDateId;
        this.selectId = selectId;
    },

    ValidateMaagarim: function(source, args)
    {
        if(divManager.IsPanelVisible('maagarimPanel') == false)
        {
            args.IsValid = true;
            return;
        }
        
        args.IsValid = false;
        
        // Run through all maagarimCheckList checkboxes and see if at least one is checked
        ExecFuncOnElements('checkbox', 'maagarimCheckList', function(checkbox)
        {
            if(checkbox.checked)
                // at least one maagar is checked: Valid
                args.IsValid = true;
        });
    },
    
    ValidateFromDate: function (source, args)
    {
        args.IsValid = validator.ValidateDate(source, document.getElementById(validator.fromDateId));
    },
    
    ValidateToDate: function (source, args)
    {
        args.IsValid = validator.ValidateDate(source, document.getElementById(validator.toDateId));
    },
    
    ValidateDate: function(source, args)
    {
        if(document.getElementById(validator.selectId).value !== 'Custom')
            return true;
    
        if(divManager.IsPanelVisible('datePanel'))
        {
            args.IsValid = false;
        
            var parts = args.value.split('/');
            
            if(parts.length == 3)
            {
                args.IsValid = parts[0] <= 31 && parts[1] <= 12 && parts[2] <= (new Date()).getFullYear() && parts[2] >= 1900;
            }
        }
        else
        {
            args.IsValid = true;        
        }
        
        return args.IsValid;
    }, 
    
    SetDateRange: function()
    {
        var select = document.getElementById(validator.selectId);
        
        if(document.getElementById(validator.toDateId).value == '' && document.getElementById(validator.fromDateId).value == '')
            select.value = 'All';
        else
            select.value = 'Custom';
    }
}