﻿
var idleTime = 0;
var sessionTime = 29;
var saveImg = true;

function imageSave(arg) {

    if (arg == "false") {
        saveImg = false
    }
    else {
        saveImg = true;
    }
}

$(function() {
    var flag = true;
    var hasCookies = false;
    var cookieId = "";
    var sessionExpired = false;
    var nameEQ = "ApplicantId" + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) {
            cookieId = (c.substring(nameEQ.length, c.length));
            hasCookies = true;
        }
    }

    $(".btnNext").click(function() {
        flag = false;

        if (saveImg == false) {
            alert('Please make sure your picture is formatted properly and click "save". If you wish to delete your picture, you may do so after saving it.');
        }
        return saveImg;
    });

    $(".submitBtn").click(function() {
        flag = false;
    });

    $(".btnEdit").click(function() {
        flag = false;
    });






    $('#wraper').mousemove(function() {
        idleTime = 0;
    })

    function setSessionTimeOut() {
        idleTime = idleTime + 1;
        if (idleTime > sessionTime) {
            clearCookies(cookieId);
            clearInterval(timer);
            alert("We haven’t heard from you in 30 minutes.  For security purposes your application information has been deleted.  Please start the application process again.");
            sessionExpired = true;
            window.location = "apply.aspx";
        }
        else {
            idleTime = idleTime;
        }
    }

    function setSessionCounter() {
        timer = setInterval(setSessionTimeOut, 60000);
    }

    if (publicProfile != true) setSessionCounter();


    function exitcheck() {
        if (publicProfile != true) {
            if ((hasCookies == true) && (flag == true) && (sessionExpired == false)) {
                if (quit()) {
                    //createCookie("ApplicantId", "", -1);
                    clearCookies(cookieId);
                    return "You application information has been deleted and you will need to start over"
                }
                else {
                    return "Do not press OK unless you want to permanently erase your application information.";
                }
            }
        }
    }

    function quit() {
        if (confirm('Navigating away from this page will cause you to lose all application information that has been entered. Are you sure you want to continue?')) {
            return true;
        }
        else {
            return false;
        }
    }

    function clearCookies(cookieId) {
        targetUrl = "http://qa-nikebackyourblock.mryouth.com/Services/ClearCookies.ashx?cookie=" + cookieId;
        //targetUrl = "http://localhost:8080/Services/ClearCookies.ashx?cookie=" + cookieId;
        //targetUrl = "http://www.nikebackyourblock.com/Services/ClearCookies.ashx?cookie=" + cookieId;
        jQuery.ajax({ url: targetUrl, async: false })
    }


    window.onbeforeunload = exitcheck;


})
