﻿function callService(methodName, parameters, onSuccess) {
    var webMethod = window.location.protocol + '//' + window.location.host + '/' + 'webservice/jobs.asmx/' + methodName;

    $.ajax({
        type: "POST",
        url: webMethod,
        data: parameters,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: onSuccess,
        error: function (request, textStatus, errorThrown) {
        }
    });
}
function findJobs() {
    var location = $('#location').val();
    var sector = $('#sector :selected').text();
    var sectorId = $('#sector').val();
    var sectorLink = '';

    if (sectorId == 43) {
        window.open('http://www.cardearesourcing.com/jobs/');
        return false;
    }

    if (sectorId != 'any') { sectorLink = '/' + encodeURIComponent(sector.replace(/\s/gi, '-').replace('&', 'and').replace('\/','-')) + '/' + sectorId; }
    if (location.toLowerCase() == 'any' && sectorId == 'any') { location = ''; }
    //location = location.replace(',', '').replace(/\s/gi, '-');

    var link = window.location.protocol + '//' + window.location.host + '/' + 'job-search/' + encodeURIComponent(location) + sectorLink;
    window.location = link.toLowerCase();
}
function setJobCount(jobCount) {
    $('#searchbox .highlight').html(jobCount);
}
function populateLocationList(locations, selectedLocation) {
    var $locations = $('#location'); var options = '<option value="any">All Locations</option>'
    $(locations).each(function(index, item) {
        options += '<option value="' + trim(item).toLowerCase().replace('/', '-') + '">' + item + '</option>';
        //options += '<option value="' + item.toLowerCase().replace('/','-') + '">' + item + '</option>';
    });

    $locations.html(options);
    $locations.val(selectedLocation);
}
function populateSubCategories(categoryId, childCategories) {
    var options = '';
    childCategories.each(function (index, item) {
        if (item.parentId == categoryId) {
            options += '<option value="' + item.catId + '">' + item.catTitle + '</option>';
        }
    });
    return options;
}
function populateCategoryList(parentCategories, childCategories, selectedCategory) {
    var $categories = $('#sector'); var options = '<option value="any">All Sectors</option>';
    var $items = $(parentCategories);$childCategories = $(childCategories);
    $items.each(function (index, item) {
        options += '<optgroup label="' + item.catTitle + '">' + populateSubCategories(item.catId, $childCategories) + '</optgroup>'; 
    });

    $categories.html(options);
    $categories.val(selectedCategory);
}
function populateControls(msg) {
    if ((msg != null && msg.d != null) && msg.d.Result) {
        populateCategoryList(msg.d.Data.ParentCategories, msg.d.Data.ChildCategories, msg.d.Data.SelectedCategory);
        populateLocationList(msg.d.Data.Locations, msg.d.Data.SelectedLocation);
        setJobCount(msg.d.Data.ActiveJobCount);
    }
}
function trim(str, chars) {
    return ltrim(rtrim(str, chars), chars);
}
function ltrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
function rtrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

$(document).ready(function () {
    callService('GetJobsInformation', "{}", populateControls);
    $('.submit > img').click(findJobs);
});
