

   $(document).ready(function(){ 
        $(document).pngFix(); 
		$("#column-three").accordion();
		
		$('table tr:even').addClass('even');
		$('#content .poll-box table tr td:first').addClass('first');
		
		$('ul#nav li:last').addClass('last');

    });


    function urlencode(str) {
        return escape(str).replace(/\+/g, '%2B').replace(/%20/g, '+').replace(/\*/g, '%2A').replace(/\//g, '%2F').replace(/@/g, '%40');
    }

    function urldecode(str) {
        return unescape(str.replace('+', ' '));
    }

    function searchAdvanced() {
        
        if (!validateAdvancedSearch())
            return;
    
        var action = "";

        var keywords = $('#KeywordsAdvanced').val();
        if (keywords != '' && keywords.length > 0 && keywords != "keywords") {
            if (action == '')
                action = "K=" + urlencode(keywords);
            else
                action = action + "&K=" + urlencode(keywords);
        }
                    
            
        var categories = document.getElementById('Categories');
        var noCategoriesSelected = 0;
        var SelectedIds;
        var SelectedName;
        for (var i = 1; i < categories.options.length; i++) {
            if (categories.options[i].selected == true) {
                noCategoriesSelected = noCategoriesSelected + 1;
                if (SelectedIds == '' || SelectedIds == null)
                    SelectedIds = categories.options[i].value;
                else
                    SelectedIds = SelectedIds + ',' + categories.options[i].value;
                if (SelectedName == '' || SelectedName == null)
                    SelectedName = categories.options[i].text;
                else
                    SelectedName = SelectedName + ',' + categories.options[i].text;
            }
        }
        if (noCategoriesSelected > 1) {
            if (action == '')
                action = "C=" + SelectedIds;
            else
                action = action + "&C=" + SelectedIds;
        }
        if (noCategoriesSelected == 1) {
            if (action == '')
                action = "CN=" + urlencode(SelectedName);
            else
                action = action + "&CN=" + urlencode(SelectedName);
        }

        var occupations = document.getElementById('Occupations');
        var noOccupationsSelected = 0;
        var SelectedRoleIds;
        for (var i = 1; i < occupations.options.length; i++) {
            if (occupations.options[i].selected == true) {
                noOccupationsSelected = noOccupationsSelected + 1;
                if (SelectedRoleIds == '' || SelectedRoleIds == null)
                    SelectedRoleIds = occupations.options[i].value;
                else
                    SelectedRoleIds = SelectedRoleIds + ',' + occupations.options[i].value;
            }
        }
        if (noOccupationsSelected > 0) {
            if (action == '')
                action = "O=" + SelectedRoleIds;
            else
                action = action + "&O=" + SelectedRoleIds;
        }

        var locations = document.getElementById('Locations');
        var noLocationsSelected = 0;
        var SelectedLocationIds;
        for (var i = 1; i < locations.options.length; i++) {
            if (locations.options[i].selected == true) {
                noLocationsSelected = noLocationsSelected + 1;
                if (SelectedLocationIds == '' || SelectedLocationIds == null)
                    SelectedLocationIds = locations.options[i].value;
                else
                    SelectedLocationIds = SelectedLocationIds + ',' + locations.options[i].value;
            }
        }
        if (noLocationsSelected > 0) {
            if (action == '')
                action = "L=" + SelectedLocationIds;
            else
                action = action + "&L=" + SelectedLocationIds;
        }

        if ($('#SalaryRange').val() != '') {
            if (action == '')
                action = "SR=" + $('#SalaryRange').val();
            else
                action = action + "&SR=" + $('#SalaryRange').val();
        }

        if ($('#ExperienceLevels').val() != '') {
            if (action == '')
                action = "EL=" + $('#ExperienceLevels').val();
            else
                action = action + "&EL=" + $('#ExperienceLevels').val();
        }

        var empTypes = "";
        for (var i = 1; i <= 7; i++) {
            if (document.getElementById('EmploymentTypes' + i.toString()).checked) {
                if (empTypes == "")
                    empTypes += i.toString();
                else
                    empTypes += "," + i.toString();
            }
        }
        if (empTypes != "") {
            if (action == '')
                action = "ET=" + empTypes;
            else
                action = action + "&ET=" + empTypes;
        }

        if ($('#SortBy').val() != '') {
            if (action == '')
                action = "sortby=" + $('#SortBy').val() + "&sortdir=asc";
            else
                action = action + "&sortby=" + $('#SortBy').val() + "&sortdir=asc";
        }

        if ($('#Freshness').val() != '') {
            if (action == '')
                action = "F=" + $('#Freshness').val();
            else
                action = action + "&F=" + $('#Freshness').val();
        }


        if ($("#CreateEmailAlert:checked").val() == "true") {
            
            $.post("/Jobs/CreateAlert",{title: $.trim($('#EmailAlertTitle').val()) },  function(data) {
                if (action == "")
                    document.AdvancedsearchForm.action = "/Jobs/SearchResultsLite?All+CPL+Jobs";
                else
                    document.AdvancedsearchForm.action = "/Jobs/SearchResultsLite?" + action;
                document.AdvancedsearchForm.submit();
            });
        }
        else
        {
                if (action == "")
                    document.AdvancedsearchForm.action = "/Jobs/SearchResultsLite?All+CPL+Jobs";
                else
                    document.AdvancedsearchForm.action = "/Jobs/SearchResultsLite?" + action;
                document.AdvancedsearchForm.submit();
        }

    }

    function fillOccupations() {
        var catIds = $('#Categories').val();
        $('#Occupations').load('/Jobs/ListRolesByCategory?catIds=' + catIds);
    }

    function validateAdvancedSearch() {
        var isValid = validateForm();
        var mbox = new AgileFx.MessageBox();
        var keywords = $('#KeywordsAdvanced').val();
        if ($("#CreateEmailAlert:checked").val() == "true") {
            var title = $.trim($('#EmailAlertTitle').val());

            if (title == null || title == '') {
                isValid = false;
                mbox.addError("A Title is required for the Email Alert");
            }
        }

        if (keywords.indexOf("<") > -1 || keywords.indexOf("[") > -1) {
            alert("You are not allowed to use '<' or '[' character.");
            isValid = false;
        }

        return isValid;
    }

    function enableEmailAlert() {
        if ($("#CreateEmailAlert:checked").val() == "true") {
            $("#EmailAlertTitle").show();
        } else {
            $("#EmailAlertTitle").hide();
        }
    }
    
        
    function search() {
        var keywords = $('#SearchKeywords').val();

        if (keywords != '' && keywords.length < 2) {
            alert("Please enter a valid keyword to search.");
            return;
        }
        if (keywords.indexOf("<") > -1 || keywords.indexOf("[") > -1) {
            alert("You are not allowed to use '<' or '[' character.");
            return;
        }
        
        var ddlCateg = document.getElementById('SearchJobCategory');
        var category = ddlCateg.options[ddlCateg.selectedIndex].text;
        
        var location = $('#SearchLocation').val();
        
        var action = "";

        if (keywords != '' && keywords.length > 0 && keywords!="keywords") {
            if (action == '')
                action = "K=" + urlencode(keywords) + "&sortby=date&sortdir=desc";
            else
                action = action + "&K=" + urlencode(keywords) + "&sortby=date&sortdir=desc";
        }

        if (category != '' && category.length > 0 && $('#SearchJobCategory').val()) {
            if (action == '')
                action = "CN=" + urlencode(category);
            else
                action = action + "&CN=" + urlencode(category);
        }

        if (location != '') {
            if (action == '')
                action = "L=" + urlencode(location);
            else
                action = action + "&L=" + urlencode(location);
        }                
        
        if (action=="")
            document.searchForm.action = "/Jobs/SearchResultsLite?All+CPL+Jobs" + action;
        else
            document.searchForm.action = "/Jobs/SearchResultsLite?" + action;
        document.searchForm.submit();
    }
    
   
    
    