﻿var cookieName = 'InsureForYou-Quote-SessionId';
var cookieLifeTime = 1;

function ClientValidateCreateCntrl() {
    createCookie(cookieName, '00000000-0000-0000-0000-000000000000', cookieLifeTime);    
}
function ClientValidateCreate() {

    var cookieId = document.forms[0].hdSessionId.value;
    var cookiePreviousId = document.forms[0].hdSessionId_Previous.value;
    var currentCookie = readCookie(cookieName);
    var currentCookiePrevious = readCookie(cookieName + '-Previous');
    
    if (cookiePreviousId != '') {
        if (cookiePreviousId != currentCookie && cookiePreviousId != currentCookiePrevious) {
            window.open('/ValidationError.aspx', '_top');
            return false;
        }
        createCookie(cookieName + '-Previous', cookiePreviousId, cookieLifeTime);
    }    
    
    createCookie(cookieName, cookieId, cookieLifeTime);

    if (readCookie(cookieName) == null) {
        window.alert('To protect your privacy on this website, cookies need to be enabled in your browser settings.');
        return false;
    }
}
function ClientValidateCreateMobile(sessionVarId, sessionPreviousVarId) {

    var cookieId = eval('document.forms[0].' + sessionVarId + '.value');
    var cookiePreviousId = eval('document.forms[0].' + sessionPreviousVarId + '.value');
    var currentCookie = readCookie(cookieName);
    var currentCookiePrevious = readCookie(cookieName + '-Previous');

    if (cookiePreviousId != '') {
        if (cookiePreviousId != currentCookie && cookiePreviousId != currentCookiePrevious) {
            window.open('/ValidationError.aspx', '_top');
            return false;
        }
        createCookie(cookieName + '-Previous', cookiePreviousId, cookieLifeTime);
    }

    createCookie(cookieName, cookieId, cookieLifeTime);

    if (readCookie(cookieName) == null) {
        window.alert('To protect your privacy on this website, cookies need to be enabled in your browser settings.');
        return false;
    }
}
function ClientValidateCreateQuickQuote(sessionId) {

    createCookie(cookieName, sessionId, cookieLifeTime);

    if (readCookie(cookieName) == null) {
        window.alert('To protect your privacy on this website, cookies need to be enabled in your browser settings.');
        return false;
    }
}


function ClientValidateCheck() {

    var cookieId = document.forms[0].hdSessionId.value;
    var currentCookie = readCookie(cookieName);

    var hiddenPnls = $('.QuoteDetails');
    
    if (currentCookie == null) {
        window.alert('To protect your privacy on this website, cookies need to be enabled in your browser settings.');
        return false;
    }

    if (currentCookie != cookieId && currentCookie != '00000000-0000-0000-0000-000000000000') {
        //alert(currentCookie + ' != ' + cookieId);
        window.open('/ValidationError.aspx', '_top');
        return false;
    }

    hiddenPnls.each(function(index) {
        $(this).show();
    });

    createCookie(cookieName, cookieId, cookieLifeTime);

    return true;
}
function clearCookie() {
    eraseCookie(cookieName);
}

function createCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}
function readCookie(name) {
    var nameEQ = name + "=";
    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) return c.substring(nameEQ.length, c.length);
    }
    return null;
}
function eraseCookie(name) {
    createCookie(name, "", -1);
}

