/**
 *  Zuken Reseller Selection
 *
 *  Used to create a list of resellers, based on target countries
 *  (managed in Sitecore), for the current item's LEADTRIGGER value.
 *
 *  Last update: 2008-06-16 {pf}
 */

//  Wait for DOM to load
$(document).ready( function() {
    
    $("div[id$=preferredResellerRow]").css("display", "none");
    bindResellerCheck();
    
});


//  Find the 'Country' element in the current form and bind resellers
function bindResellerCheck() {
    
    //  Test for existence
    if ($('select[name$=ddlCountry]').length) {
        
        //  Check current selection (if any)
        if ( $('select[name$=ddlCountry]').val() != '' ) {
            checkResellerCountry($('select[name$=ddlCountry]').val());
        }
        
        //  Check selection on change
        $('select[name$=ddlCountry]').bind('change', function() {
            checkResellerCountry($('select[name$=ddlCountry]').val());
        });
        
    }
    
}


//  Establish if the selected country is one we're interested in
function checkResellerCountry(selectedCountryValue) {
    
    var initReseller = null;
    
    if ($('select[name$=ddlPreferredReseller]').val() != '') {
        initReseller = $('select[name$=ddlPreferredReseller] :selected').text().split(': ')[1];
    }
    
    var hideResellers = true;
    
    if ( selectedCountryValue != '' ) {
        for (var i = 0; i < resellerCountries.length; i++) {
            if (resellerCountries[i].countryValue == selectedCountryValue) {
                listResellers($('select[name$=ddlCountry]').val(), initReseller);
                hideResellers = false;
            }
        }
    }
    
    if (hideResellers) {
        hideResellersRow();
    }
    
}


function listResellers(selectedCountryValue, selectedResellerValue) {
    
    //  Check that '#preferredResellerRow' is hidden or '#preferredReseller' isn't already showing the selected country's resellers
    if ( $('div[id$=preferredResellerRow]').css("display") != "block" || $('select[name$=ddlPreferredReseller]').title != selectedCountryValue) {
        
        //  Only create the '#preferredReseller' element once
        if ( $('div[id$=preferredResellerRow]').css("display") != "block" ) {
            $('div[id$=preferredResellerRow]').css("display", "block");
            $('select[name$=ddlPreferredReseller]').attr("class", "required");
        }
        //  ...otherwise retitle and strip out the existing options
        else {
            $("select[name$=ddlPreferredReseller]").title = selectedCountryValue;
            $("select[name$=ddlPreferredReseller] options").remove();
        }
        
        //  Iterate over JSON object of current resellers and list as options
        var resellerOptions = '';
        
        pleaseSelectStr = window.pleaseSelectStr || null;
        
        //  Add null option if user is choosing preferred reseller for 1st time
        if ( pleaseSelectStr != null ) {
            resellerOptions += '<option value="">' + pleaseSelectStr + '</option>';
        }
        
        for (var i = 0; i < resellerCountries.length; i++) {
            if (resellerCountries[i].countryValue == selectedCountryValue) {
                for (var r = 0; r < resellerCountries[i].countryResellers.length; r++) {
                    resellerOptions += '<option value="' + resellerCountries[i].countryResellers[r].resellerValue + '">' + resellerCountries[i].countryResellers[r].resellerName + '</option>';
                }
            }
        }
        
        $("select[name$=ddlPreferredReseller]").html(resellerOptions);
        
        if (selectedResellerValue != null) {
            $("select[name$=ddlPreferredReseller]").val(selectedResellerValue);
        }
        
    }
    
}


//  Hide the reseller list and remove its required status
function hideResellersRow() {
    //  Hide row from user
    $("div[id$=preferredResellerRow]").css("display", "none");
    //  Remove JS validation trigger
    $("select[name$=ddlPreferredReseller]").attr("class", "");
    //  Strip out existing options
    $("select[name$=ddlPreferredReseller] options").remove();
    //  Add null option to pass server-side validation
    $("select[name$=ddlPreferredReseller]").html('<option value=""></option>');
}
