//show advanced sort popup
function showAdvancedSort(ddlReportViewClientID, ddlSortByClientID, listingCategoryFilter, previousAdvancedSort) {

    var result;
    var features;

    //setup pop-up
	var ie7Height = 350;
	var ie7Width = 363;
    features = "dialogHeight: " + AdjustPopupHeight(ie7Height, "px");
    features += ";dialogWidth: " + AdjustPopupWidth(ie7Width, "px");
    features += ";center: 1";
    features += ";help: 0";
    features += ";status: 0";
    features += ";resizable: 0";
    features += ";scroll: 0";
    
    //get the current report id the user has set
    var currentReportEnumID = document.getElementById(ddlReportViewClientID).value
    
    //get the current primary sort id the user has set
    var currentPrimarySortEnumID = document.getElementById(ddlSortByClientID).value
        
    //display the popup, passing in needed QS values
    result = window.showModalDialog(RelativeWebDirectoryPath + "/Report/Popups/AdvancedSort.aspx?curRptID=" + currentReportEnumID + "&curPrimSortID=" + currentPrimarySortEnumID + "&ListCatFilter=" + listingCategoryFilter + "&PrevAdvSort=" + previousAdvancedSort, null, features);
  
    //check if the user cancelled or X'd out of the window
    if ((result=='cancel')||(result==null)) {
        //Do not do a PostBack
        return false;
    }    
    //Set what our new sort is
    document.getElementById("hdnAdvancedSortPipeDelimitedString").value = result;
    return true;
    
}

//close advanced sort popup
function ClosePopUp(returnValue)
{
    window.returnValue = returnValue;
    
    window.close();    
}

function ProcessAdvancedSort(primarySortControlID, primarySortAscendingControlID, primarySortDescendingControlID, secondarySortControlID, secondarySortAscendingControlID, secondarySortDescendingControlID, tertiarySortControlID, tertiarySortAscendingControlID, tertiarySortDescendingControlID)
{
    //Get our sorts
    var primarySortID = document.getElementById(primarySortControlID).value;
    var secondarySortID = document.getElementById(secondarySortControlID).value;
    var tertiarySortID = document.getElementById(tertiarySortControlID).value;
    
    //Validate the sort selection
    if (ValidateSort(primarySortID,secondarySortID,tertiarySortID)) {
    
        //process directionals
        var primarySortDirectionID;
        if (document.getElementById(primarySortAscendingControlID).checked)
            primarySortDirectionID = document.getElementById(primarySortAscendingControlID).value
        else
            primarySortDirectionID = document.getElementById(primarySortDescendingControlID).value
            
        var secondarySortDirectionID;
        if (document.getElementById(secondarySortAscendingControlID).checked)
            secondarySortDirectionID = document.getElementById(secondarySortAscendingControlID).value
        else
            secondarySortDirectionID = document.getElementById(secondarySortDescendingControlID).value
              
        var tertiarySortDirectionID;
            if (document.getElementById(tertiarySortAscendingControlID).checked)
            tertiarySortDirectionID = document.getElementById(tertiarySortAscendingControlID).value
        else
            tertiarySortDirectionID = document.getElementById(tertiarySortDescendingControlID).value
        
        //create our return string, depending on what has been set
        var pipeDelimitedSortString = primarySortID + '|' + primarySortDirectionID;
        if (secondarySortID != 'undefined') {
            
            pipeDelimitedSortString += '|' + secondarySortID + '|' + secondarySortDirectionID;
            
            if (tertiarySortID != 'undefined') {
                pipeDelimitedSortString += '|' + tertiarySortID + '|' + tertiarySortDirectionID
            }
        }
       
        //alert(pipeDelimitedSortString);
        //return false;
        
        ClosePopUp(pipeDelimitedSortString);
        return true;
        
    } else {

        //display our error
        divError.style.display = "inline";
        //don't close the popup
        return false;
        
    }
}

function StartUpToggle(secondarySortControlID, secondarySortAscendingControlID, secondarySortDescendingControlID, tertiarySortControlID, tertiarySortAscendingControlID, tertiarySortDescendingControlID)
{
    //take care of tertiary first
    ToggleTertiarySort(tertiarySortControlID, tertiarySortAscendingControlID, tertiarySortDescendingControlID);
    
    //take care of secondary next
    ToggleSecondarySort(secondarySortControlID, secondarySortAscendingControlID, secondarySortDescendingControlID, tertiarySortControlID, tertiarySortAscendingControlID, tertiarySortDescendingControlID);

}

function ToggleSecondarySort(secondarySortControlID, secondarySortAscendingControlID, secondarySortDescendingControlID, tertiarySortControlID, tertiarySortAscendingControlID, tertiarySortDescendingControlID)
{
    var secondarySortControl = document.getElementById(secondarySortControlID);
    var disabledState;
    
    if (secondarySortControl.value == 'undefined') {
        disabledState = true
    } else {
        disabledState = false
    }   
    
    //toggle the directionals for secondary sort
    document.getElementById(secondarySortAscendingControlID).disabled = disabledState;    
    document.getElementById(secondarySortDescendingControlID).disabled = disabledState;

    //toggle all of tertiary sort dropdown 
    var tertiarySortControl = document.getElementById(tertiarySortControlID);
    tertiarySortControl.disabled = disabledState;
    //if tertiary sort is not undefined, then toggle the tertiary directionals as well
    if (tertiarySortControl.value != 'undefined') {
        document.getElementById(tertiarySortAscendingControlID).disabled = disabledState;    
        document.getElementById(tertiarySortDescendingControlID).disabled = disabledState;
    }
}

function ToggleTertiarySort(tertiarySortControlID, tertiarySortAscendingControlID, tertiarySortDescendingControlID)
{
    var tertiarySortControl = document.getElementById(tertiarySortControlID);
    var disabledState;
    
    if (tertiarySortControl.value == 'undefined') {
        disabledState = true
    } else {
        disabledState = false
    }   

    //toggle the directionals for Tertiary sort
    document.getElementById(tertiarySortAscendingControlID).disabled = disabledState;    
    document.getElementById(tertiarySortDescendingControlID).disabled = disabledState;
}

function ValidateSort(primarySortID, secondarySortID, tertiarySortID)
{
    //If we have a secondary, it can't equal the primary
    if (secondarySortID != 'undefined') {
        if (primarySortID == secondarySortID) {
            return false;
        }    
    }
    
    //If we have a tertiary, it can't equal the primary or secondary.
    if (tertiarySortID != 'undefined') {
        if (primarySortID == tertiarySortID) {
            return false;
        }    
        if (secondarySortID == tertiarySortID) {
            return false;
        }            
    }    
    
    return true;    
}
