/*
 * PollSubmitClicked() invoked when a poll is submitted from a salary centre page.
*/

function PollSubmitClicked(e) {

    var selection = $RF(document.forms[0], "option");
    
    if (selection != null && selection.length > 0)
    {
        // Do AJAX request
        var url = '/services/Poll.ashx?poll=' + CurrentSurveyID + '&a=' + selection;
        
        $('SalarySurveyAjaxLoader').show();
        $('SurveyBoxContent').hide();
        
        $('SurveyBox').removeClassName('SurveyBox');
        $('SurveyBox').addClassName('SurveyBox prev-survey-results');
        
        new Ajax.Updater('SurveyBox', url,           
            {     
                method:'get',    
                onSuccess: function(transport){ $('SalarySurveyAjaxLoader').hide(); }
            }
        );
    }
    else
    {
        alert('You must select an answer before voting');
    }

	// Cancel the action.
	e.returnValue = false;
}


function PollsSubmitClicked(e, surveyId, surveyKey, urlSettingsType) {

    var selection = $RF(document.forms[0], "option");
    
    if (selection != null && selection.length > 0)
    {
        // Do AJAX request
        var url = '/services/Poll.ashx?poll=' + surveyId + 
			'&a=' + selection + 
			'&sk=' + surveyKey + 
			'&urlTypeId=' + urlSettingsType +
			'&noh2=1';
        
        $('SalarySurveyAjaxLoader').show();
        $(surveyKey + 'Content').hide();
		
        $(surveyKey).removeClassName('SurveyBox');
        $(surveyKey).addClassName('SurveyBox prev-survey-results');
        
        new Ajax.Updater(surveyKey, url,           
            {     
                method:'get',    
                onSuccess: function(transport){ $('SalarySurveyAjaxLoader').hide(); }
            }
        );
    }
    else
    {
        alert('You must select an answer before voting');
    }

	// Cancel the action.
	e.returnValue = false;
}


/*
 * redirect() invoked when user selects sector or/and location from the SectorLocationSelector control.
 * This control cannot use standard button triggering postback as its parent page is cached and every 
 * subsequent call to that page loads content from the cache (and therefore doesn't fire event handlers eg OnLoad())
*/
function redirect()
{	
	var url = '';	
	var SectorsListValue = '';
	var LocationsListValue = '';
	
	var dropDownLists = $('sectorLocationSelector').getElementsByTagName('select');

	for (i = 0; i < dropDownLists.length; i++) 
	{	    
	    var dropDown = dropDownLists[i];
	    if (dropDown.id.match(/(.)*SectorsList+(.)*/)) 
	    {
            SectorsListValue = dropDown.value;
	    }
	    else if (dropDown.id.match(/(.)*LocationsList+(.)*/)) 
	    {
	        LocationsListValue = dropDown.value;
	    }
	}

    if (SectorsListValue.length > 0 && SectorsListValue != 'Choose a sector')
    {
        url = "/salary-centre/" + SectorsListValue;

        if (LocationsListValue.length > 0 && LocationsListValue != 'Choose a location')
        {
            url += "/-/" + LocationsListValue;
        }
        top.location.href = url;
    }
    else
    {
        // Oops, sector is compulsory field, show error message!
        alert('Please choose a sector');
    }
}

/* Toggle sectors in the salary centre table */
function toggleSectors()
{
    $('bottom').toggle();
    $('showAllSectors').hide();
}
