/**
 * Depends on advajax.js
 *
 * @param jobId
 */
function getBaseUrl()
{
    var partialProtocol = "://";
    var protocol = "";
    var thisHref = window.location.href;
    var indexOfPartialProtocol = thisHref.indexOf(partialProtocol);
    if (indexOfPartialProtocol > -1)
    {
        protocol = thisHref.substring(0, (indexOfPartialProtocol + partialProtocol.length));
        thisHref = thisHref.substring((indexOfPartialProtocol + partialProtocol.length), thisHref.length);
        var indexOfFirstForwardSlash = thisHref.indexOf("/");
        if (indexOfFirstForwardSlash > -1)
        {
            thisHref = thisHref.substring(0, (indexOfFirstForwardSlash+1));
        }
        thisHref = protocol + thisHref;
    }
    return thisHref;
}

function saveJobById(jobId)
{
    var saveJobUrl = getBaseUrl() + "myjobs/savejob?type=100&id=" + jobId;
    advAJAX.get({
        url: saveJobUrl,
        onSuccess: function(obj)
        {
            jobSaved(jobId, obj.responseText);
        },
        onError: function(obj)
        {
            alert("An error occurred while trying to save this job.");
        }

    });
}

/**
 * Depends on advajax.js
 *
 * @param jobId
 */
function deleteJobById(savedJobTrackingId,jobId)
{
    var deleteJobUrl = getBaseUrl() + "myjobs/delete?id=" + savedJobTrackingId;
    advAJAX.get({
        url: deleteJobUrl,
        onSuccess: function(obj)
        {
            jobRemoved(jobId);
        },
        onError: function(obj)
        {
            alert("An error occurred while trying to delete this job.");
        }
    });
}

function jobSaved(jobId,rawJSONJobTrackingId)
{
    var jsonJobTrackingId = YAHOO.lang.JSON.parse(rawJSONJobTrackingId);
    if (jsonJobTrackingId && jsonJobTrackingId.length == 1)
    {
        var jobTrackingId = jsonJobTrackingId[0];
        var viewJobSavedStatus = document.getElementById("viewJobSavedStatus");
        if (viewJobSavedStatus)
        {
            viewJobSavedStatus.style.display = "";
            viewJobSavedStatus.innerHTML = "<strong>Status:</strong> Saved (<a href=\"#\" onclick=\"deleteJobById('" + jobTrackingId + "', '" + jobId + "');return false;\">Remove</a>)";
        }
        var inactiveSaveJobButton = document.getElementById("inactiveSaveJobButton");
        if (inactiveSaveJobButton)
        {
            inactiveSaveJobButton.style.display = "";
        }
        var activeSaveJobButton = document.getElementById("activeSaveJobButton");
        if (activeSaveJobButton)
        {
            activeSaveJobButton.style.display = "none";
        }
    }
}

function jobRemoved(jobId)
{
    var viewJobSavedStatus = document.getElementById("viewJobSavedStatus");
    if (viewJobSavedStatus)
    {
        viewJobSavedStatus.style.display = "none";
    }
    var inactiveSaveJobButton = document.getElementById("inactiveSaveJobButton");
    if (inactiveSaveJobButton)
    {
        inactiveSaveJobButton.style.display = "none";
    }
    var activeSaveJobButton = document.getElementById("activeSaveJobButton");
    if (activeSaveJobButton)
    {
        activeSaveJobButton.style.display = "";
    }
}

