﻿
function JumpToPage(intPage) {
    var strOldLocation;
    strOldLocation = new String(top.location);
    strOldLocation = strOldLocation.replace(/&page=\d*/, '&page=' + intPage);
    strOldLocation = strOldLocation.replace(/\?page=\d*/, '?page=' + intPage);
    if (strOldLocation.indexOf('page=') == -1) {
        if (strOldLocation.indexOf('?') == -1) {
            strOldLocation += '?page=' + intPage;
        } else {
            strOldLocation += '&page=' + intPage;
        }
    }
    top.location = strOldLocation;
}

var c_xhrSave;
var c_lnkSave;

function saveMustRegister(link) {
    c_lnkSave = link;

    var box_msg = document.getElementById('saveConfirmMsg');
    box_msg.style.display = "block";
    
    //Build "you must log in to save jobs." message with clickable "log in"
    var textpre = document.createTextNode("You must ");
    var loginlink = document.createElement("a");
    loginlink.href = "/login.aspx?returnurl=" + encodeURIComponent(location.href) //todo: append current url here...
    loginlink.innerHTML = "log in"
    var textpost = document.createTextNode(" to save jobs. ");

    box_msg.innerHTML = '';
    box_msg.appendChild(textpre);
    box_msg.appendChild(loginlink);
    box_msg.appendChild(textpost);

    var box = document.getElementById('saveJobConfirm');
    box.className = 'saveJobConfirmShow';
    box.style.top = (getOffset(c_lnkSave).top + 20) + 'px';
    box.style.left = (getOffset(c_lnkSave).left - 250) + 'px';
}

function saveJob(link, id) {
    c_lnkSave = link;
    
    c_xhrSave = CreateXmlHttp();
    MakeAjaxCall(c_xhrSave, '/AjaxSaveToMyJobs.aspx?jobid=' + id, saveJobFeedback);
}

function saveJobFeedback() {

    if (c_xhrSave.readyState == 4) {

        if (c_xhrSave.responseText.indexOf('OK') > -1) {
            if (Get_Cookie("atb-savejobs") != "1") {
                //showConfirm("The job has been saved to your \"My Jobs\" page. To access your \"My Jobs\" list, please click on the \"My Jobs\" tab at the top of the page.<div id=\"MyJobsPreview\"></div>");
                showSaveConfirm();
                Set_Cookie("atb-savejobs", "1", 1, "/");
            }
            c_lnkSave.innerHTML = "Saved";
            c_lnkSave.removeAttribute("href");
            c_lnkSave.removeAttribute("onclick");
        } else {
            //showConfirm("Failed to save the job. This could happen if your browser does not accept cookies or after a long period of inactivity.");
            showSaveFail();
        }
    }
}

function showSaveConfirm() {
    document.getElementById('saveConfirm').style.display = 'block';
    showConfirm();
}

function showSaveFail() {
    document.getElementById('saveFail').style.display = 'block';
    showConfirm();
}

function showConfirm(msg) {

//    var box_msg = document.getElementById('saveConfirmMsg');
//    var text = document.createTextNode(msg);
//    box_msg.innerHTML = '';
//    box_msg.appendChild(text);

    var box = document.getElementById('saveJobConfirm');
    box.className = 'saveJobConfirmShow';
    box.style.top = (getOffset(c_lnkSave).top + 20) + 'px';
    box.style.left = (getOffset(c_lnkSave).left - 250) + 'px';

}

function closeConfirm() {
    var box = document.getElementById('saveJobConfirm');
    box.className = 'saveJobConfirmHidden';
}

function getOffset( el ) {
    var _x = 0;
    var _y = 0;
    
    while( el && !isNaN( el.offsetLeft ) && !isNaN( el.offsetTop ) ) {
        _x += el.offsetLeft;
        _y += el.offsetTop;
        el = el.offsetParent;
    }

    return { top: _y, left: _x };
}
