﻿//Cookie = name-value pair
//days   = days of cookie expire

//creating Cookie
cookieVisit = "Visit";

function createCookie(days, cookieName) {
    var today = new Date();
    var expires = new Date();
    var iCounter = getSessionTimes()

    expires.setTime(expires.getTime() + (days * 24 * 60 * 60 * 1000));

    if (document.cookie) {
        index = document.cookie.indexOf(cookieName);
    }
    else {
        index = -1;
    }

    if (index == -1) {
        document.cookie = cookieName + "=1; " + " expires=" + expires.toGMTString();
    }
    else {
        countBegin = (document.cookie.indexOf("=", index)) + 1;
        countEnd = document.cookie.indexOf(";", index);
        if (countEnd == -1) {
            countEnd = document.cookie.length;
        }
        count = eval(document.cookie.substring(countBegin, countEnd)) + 1;
        document.cookie = cookieName + "=" + count + "; " + " expires=" + expires.toGMTString();
    }
}

function sessionCookie() {
    if (document.cookie) {
        index = document.cookie.indexOf(cookieVisit);
    }
    else {
        index = -1;
    }

    if (index == -1) {
        document.cookie = cookieVisit + "=1; "
    }
    else {
        countBegin = (document.cookie.indexOf("=", index)) + 1;
        countEnd = document.cookie.indexOf(";", index);
        if (countEnd == -1) {
            countEnd = document.cookie.length;
        }
        count = eval(document.cookie.substring(countBegin, countEnd)) + 1;
        document.cookie = cookieVisit + "=" + count + "; "

    }
}

//geting number of visits per session
function getSessionTimes() {
    count = 0;
    if (document.cookie) {
        index = document.cookie.indexOf(cookieVisit);
        if (index != -1) {
            countbegin = (document.cookie.indexOf("=", index) + 1);
            countend = document.cookie.indexOf(";", index);
            if (countend == -1) {
                countend = document.cookie.length;
            }
            count = document.cookie.substring(countbegin, countend);
        }
    }
    return (count);
}

//geting number of visits
function getTimes(cookieName) {
    count = 1;

    if (document.cookie) {
        index = document.cookie.indexOf(cookieName);
        if (index != -1) {
            countbegin = (document.cookie.indexOf("=", index) + 1);
            countend = document.cookie.indexOf(";", index);
            if (countend == -1) {
                countend = document.cookie.length;
            }
            count = document.cookie.substring(countbegin, countend);
        }
    }
    //}
    return (count);
}

//hide div by div id
function hideDiv(divID) {
    if (document.getElementById) {
        // DOM3 = IE5, NS6 
        document.getElementById(divID).style.visibility = 'hidden';
    }
    else {
        if (document.layers) {
            // Netscape 4 
            document.divID.visibility = 'visible';
        }
        else { // IE 4 >>
            document.all.divID.style.visibility = 'hidden';
        }
    }
    document.getElementById("v_sector").style.visibility = 'visible';
    document.getElementById("v_location").style.visibility = 'visible';
    document.getElementById("divBannerZ").style.visibility = 'visible';
    //document.getElementById("z").style.position = 'none';
    document.getElementById("zTable").style.position = 'absolute';

}

//show div by ID
function showDiv(divID) {
    if (document.getElementById) {
        // DOM3 = IE5, NS6 
        document.getElementById(divID).style.visibility = 'visible';
    }
    else {
        if (document.layers) {
            document.divID.visibility = 'visible';
        }
        else {
            document.all.divID.style.visibility = 'visible';
        }
    }
}

//center a div with width and height (css) on 50% and then adjust the margin-left en top.
function center(divID) {
    var box = document.getElementById(divID);

    box.style.marginLeft = "-550px"; //"-" + parseInt(box.offsetWidth / 2) + "px";
    box.style.marginTop = "40px"; //"-" + parseInt(box.offsetHeight / 2) + "px";

}

//final functions//////////////////////////////////////////////////////////////////////////////////

/*Used to display messages*/
function ShowMessage(message) {
    //Display the popup box
    $(document).ready(function() {
        $.prompt(message, { buttons: { Close: true} });
    });
}

//Creates the cookie and calls the text from the library of what is wanted
function OverlayMessageDisplay(textId) {
    sessionCookie();
    var counterVisit = getSessionTimes();
    //only show first n times; n=3
    if (counterVisit <= 1) {
        createCookie(30, 'diversity-closure-message');
        var Counter = getTimes('diversity-closure-message');
        if (Counter <= 1) {
            //Set the HTML
            var surveyHTML = '';
            surveyHTML = TextLibrary(textId);
            
            //Display the popup box
            $(document).ready(function() {
            $.prompt(surveyHTML, { prefix: 'overlay', buttons: { 'Close window': true} });
            });
        }
    }
}

//The text library
function TextLibrary(textId) {
    var surveyHTML = '';
    switch (textId) {
        case 1:
            surveyHTML += '<h1>Welcome to the new look Milkround.</h1><br />';
            surveyHTML += '<p>We\'ve refreshed our style – on the new Milkround you\'ll find:</p>';
            surveyHTML += '<p>-	New look for simple browsing with a refreshed style.<br />';
            surveyHTML += '- Industry guides with everything you need to know about the industry you want to work in.<br />';
            surveyHTML += '- Diversity Milkround with information about employers championing equal opportunities.<br />';
            surveyHTML += '- Plus the 1,000s of opportunities from student to graduate you\'re used to!</p>';
            surveyHTML += '<p>We hope you enjoy the new look Milkround, and we’d love to hear your thoughts on the new site: email <a href=\'mailto:info@milkround.com\'>info@milkround.com</a> with your views.</p><br />';
            return surveyHTML;
            
        default:
            return surveyHTML;
    }
}
