﻿var milisec = 0
//var mastersec = 20000
var mastersec = 45000
if (readCookie('setSurveyTimer') != null && readCookie('setSurveyTimer') > 0) {
var seconds = readCookie('setSurveyTimer') / 1000
} else {
    var seconds = mastersec / 1000
}

var finalcountdown = readCookie('setSurveyTimer')

function runSurvey() {

    //Run Cookie Test
    createCookie('cookieTest', 'yes', 1)

    // If the Ignore cookies is anything but null or true
    //alert("Ignore: " + readCookie('setSurveyIgnore'));
    if (readCookie('setSurveyIgnore') != 'true') {

        //Set cookie box to show current cookie countdown
      //  if (document.getElementById('txtCookieVal') != null) {
       //     document.getElementById('txtCookieVal').value = readCookie('setSurveyTimer');
      //  }

        //Check if user can accept cookies
        if (readCookie('cookieTest') != null) {

            //Set the cookietime variable to whats stored in cookie
            var cookieTime = readCookie('setSurveyTimer');

            //If the cookieTime is anything other than 60 seconds
            if (cookieTime > 0 && cookieTime < mastersec && cookieTime != null) {
                //Do nothing
                //alert(cookieTime);
            } else {
                //alert(cookieTime);
                createCookie('setSurveyTimer', mastersec, 1);
            }

            //Start the 60 second countdown
            countdownTimer();

            //Set the popup window delay to either 60 secs or value of cookie
            var surveyDelay = readCookie('setSurveyTimer')
            setTimeout("openSurveyWindow()", surveyDelay)

        } else {
           
        }

    } else {
       
    }
}

function unloadSurvey() {

    eraseCookie('setSurveyTimer');
    if (readCookie('setSurveyIgnore') != 'true') {
        if (readCookie('setSurveyDelay') != null) {
            createCookie('setSurveyTimer', readCookie('setSurveyDelay'), 1);
            eraseCookie('setSurveyDelay');
        } else {
            createCookie('setSurveyTimer', finalcountdown, 1);
        }
    }
}

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);
}

function closeSurveyWindow() {
    $(function () {
        overlayAPI = window.parent.$("#overlay").overlay({ api: true });
    });

    overlayAPI.close();
}

function openSurveyWindow() {
    //window.open("/httpdocs/survey/Index.aspx", "mywindow", "status=0,toolbar=0,width=640,height=480,scrollbars=1");

    var theframe = $('<iframe frameborder="0" scrolling="auto" height="1000"></iframe>');

    $("#overlay").overlay({

        // custom top position
       top: -60,
	   left: -35,
	  

        // some mask tweaks suitable for facebox-looking dialogs
        mask: {

            // you might also consider a "transparent" color for the mask
            color: '#000',

            // load mask a little faster
            loadSpeed: 200,

            // very transparent
            opacity: 1.5
        },
        effect: 'apple',
        oneInstance: true,
        onBeforeLoad: function() {

            //First get the wrap
            var wrap = this.getOverlay().find(".contentWrap");
            //Get the URL from the trigger
            var link = "/survey/Index.aspx";
            //Add the link and style attributes to the basic iframe
            $(theframe).attr({ src: link, style: 'height:450px; width:600px; border:none;' });
            //Write the iframe into the wrap
            wrap.html(theframe);
		},

        // disable this for modal dialog-type of overlays
        closeOnClick: false,
        close: function () {
            createCookie('setSurveyIgnore', 'true', 1000);
        },
        // load it immediately after the construction
        load: true

    });
}

function countdownTimer() {
    
    if (milisec <= 0) {
        milisec = 9
        seconds -= 1
    }

    if (seconds <= -1) {
        milisec = 0
        seconds += 1
    } else milisec -= 1

    finalcountdown = seconds * 1000
    
   // if (document.getElementById('txtCountDown') != null) {
   //     document.getElementById('txtCountDown').value = seconds;
   // }

   // if (document.getElementById('txtCookieVal') != null) {
  //  document.getElementById('txtCookieVal').value = readCookie('setSurveyTimer');
 //   }
    setTimeout("countdownTimer()", 100)
}

function checkTextAreaMaxLength(textBox, e, length) {

    var mLen = textBox["MaxLength"];
    var trunc = textBox.value;

    if (null == mLen)
        mLen = length;

    var maxLength = parseInt(mLen);
    if (!checkSpecialKeys(e)) {
        if (trunc.length > maxLength - 1) {
            trunc = trunc.substring(0, mLen);
            textBox.value = trunc;


            if (window.event)//IE

                e.returnValue = false;
            else//Firefox
                e.preventDefault();
        }
    }
}

function checkSpecialKeys(e) {
    if (e.keyCode != 8 && e.keyCode != 46 && e.keyCode != 37 && e.keyCode != 38 && e.keyCode != 39 && e.keyCode != 40)
        return false;
    else
        return true;
}

