/* JavaScript Document
* Contains the generic methods
*/

var isIE = document.all ? true : false;
// toggles opacity of cover div
function fadeMainDiv(active, targetDiv) {
    var coverDiv = document.getElementById("coverDiv");
    if (active) {
        var mainDiv = document.getElementById("mainDiv");
        var h1 = targetDiv.clientHeight;
        var h2 = document.documentElement.clientHeight;
        if (h1 > h2) {
            coverDiv.style.height = (h1 + 100) + "px";
            targetDiv.style.top = "50px";
        }
        else {
            var diff = Math.round((h2 - h1) / 2.0);
            coverDiv.style.height = h2 + "px";
            targetDiv.style.top = diff + "px";
        }
        coverDiv.className = "fadeActive";
    } else {
        coverDiv.style.height = "0px";
        coverDiv.className = "hidden";
    }
}
// display the login panel
function showLoginPanel(link) {
    // hides the login error feedback
    document.getElementById("login_error_div").style.display = "none";
    // hides the google map & floating div
    hideGoogleMap();
    hideFloatingDiv();
    // gets target attributes from clicked link & put them into login form
    document.getElementById("targetPageInput").value = link.getAttribute("loginTarget");
    document.getElementById("targetPageInput_reg").value = link.getAttribute("loginTarget");
    var loginClass = "login_Value";
    var loginAction = "candidate_login.aspx";
    var forgotten_div = document.getElementById("forgotten_div");
    // if target is client area
    if (link.getAttribute("loginTarget").indexOf("client") != -1) {
        loginClass = "hidden";
        // aim at client login
        loginAction = "client_login.aspx";
        // hide forgotten & register links
        forgotten_div.style.display = "none";
        document.getElementById("login_register_btn").style.display = "none";
        document.getElementById("loginTargetTitle").innerHTML = "Client&nbsp;";
        document.getElementById("login_caption").innerHTML = "Username";
    // 04/05/2010, Agency login option
    } else if (link.getAttribute("loginTarget").indexOf("agency") != -1) {
        loginClass = "hidden";
        // aim at client login
        loginAction = "agency_login.aspx";
        // hide forgotten & register links
        forgotten_div.style.display = "none";
        document.getElementById("login_register_btn").style.display = "none";
        document.getElementById("loginTargetTitle").innerHTML = "Agency&nbsp;";
        document.getElementById("login_caption").innerHTML = "Email";
    } else {
        forgotten_div.style.display = "";
        document.getElementById("login_register_btn").style.display = "";
        document.getElementById("loginTargetTitle").innerHTML = "Candidate&nbsp;";
        document.getElementById("login_caption").innerHTML = "Email";
    }
    var loginDiv = document.getElementById("loginDiv");
    loginDiv.className = "";
    // fade in the main div
    fadeMainDiv(true, loginDiv);
}
// hide the login panel
function hideLoginPanel() {
    fadeMainDiv(false);
    document.getElementById("loginDiv").className = "hidden";
}

var timer = 0;
var tempArgs;
// starts timer that shows the job preview after half a second
function startJobPreviewTimer() {
    hideFloatingDiv();
    tempArgs = arguments;
    clearTimeout(timer);
    timer = setTimeout("showJobPreview(tempArgs)", 500);
}
// hides all floating objects
function hideAll() {
    hideGoogleMap();
    hideFloatingDiv();
    hideLoginPanel();
}
// shows the job preview
function showJobPreview() {
    document.getElementById("googleDiv").style.display = "none";
    var args = arguments[0];
    // gets y position of link clicked and uses that to calculate position of floating div
    var tempY = findPosY(args[0]);
    var floatingDiv = document.getElementById("floatingDiv");
    var h1 = document.documentElement.clientHeight;
    if (tempY + floatingDiv.clientHeight >= h1) tempY -= 200.0;
    floatingDiv.style.top = tempY + "px";
    for (var i = 0; i < args.length - 1; i++) args[i] = args[i + 1];
    // puts date into correct format
    args[6] = formatDate(args[6]);
    // passes job's details into job preview object
    populateJobPreviewDetails(args);
    // show the job preview object
    showFloatingDiv();
}
// Get Y position of object
function findPosY(obj) {
    var curtop = 0;
    if (obj.offsetParent) {
        while (1) {
            curtop += obj.offsetTop;
            if (!obj.offsetParent) break;
            obj = obj.offsetParent;
        }
    } else if (obj.y) {
        curtop += obj.y;
    }
    return curtop;
}
// shows the google map object
function showGoogleMap(link) {
    var args = arguments[0];
    var googleDiv = document.getElementById("googleDiv");
    googleDiv.style.display = "";
    googleDiv.style.width = document.documentElement.clientWidth;
    document.getElementById("floatingDiv").className = "hidden";
    
    // places the google object 50 pixels from the top of the page
    var h1 = googleDiv.clientHeight;
    var h2 = document.documentElement.clientHeight;
    // accounts for scrolling
    if (h1 > h2) googleDiv.style.top = "50px";
    else {
        var diff = Math.round((h2 - h1) / 2.0);
        googleDiv.style.top = diff + "px";
    }
}
// hides the google map
function hideGoogleMap() {
    var googleDiv = document.getElementById("googleDiv");
    if (googleDiv != null) googleDiv.style.display = "none";
}
// shows the floating div
function showFloatingDiv() {
    document.getElementById("floatingDiv").className = "";
}
// hides the floating div
function hideFloatingDiv() {
    var floatingDiv = document.getElementById("floatingDiv");
    if (floatingDiv != null) floatingDiv.className = "hidden";
}
// formats an inputted amount into a salary string inc. decimals if desired
function formatSalary(input, decimals) {
    if (input == "" || input == "0") return "On Request";
    var val = input * 100;
    var valStr = val + "";

    var output = "";

    if (decimals)  // i.e. if rate of pay
    {
        output = "." + valStr.substring(valStr.length - 2, valStr.length);
    }
    for (var i = 0; i < valStr.length - 2; i++) {
        output = valStr.charAt(valStr.length - 3 - i) + output;
        if ((i + 1) % 3 == 0 && i < valStr.length - 3) output = "," + output;
    }
    return output;
}
// formats an inputted date int to a suitable date string
function formatDate(input) {
    var output = "";
    if (input == " ") return ""
    output += input.substring(6, 8) + "/";
    output += input.substring(4, 6) + "/";
    output += input.substring(0, 4);
    if (output == "//") return "";
    return output;
}
// trims any spaces from the inputted string
function trim(s) {
    return s.replace(/^\s*/, "").replace(/\s*$/, "");
}
// toggle selected object
function showHideTarget(objname) {
    var obj = document.getElementById(objname);
    obj.style.display = obj.style.display == "none" ? "" : "none";
}
