﻿/// <reference path="cookies.js" />
/// <reference path="Ethofy.window.js" />

var isNewCookie;
var currentSurveyId;
var currentSurveyInterval;
var currentAskAgainDays;
var cookieSurveyId;
var cookieSurveyDenied;
var cookieSurveyTaken;

function showSurvey(surveyId, surveyInterval, askAgainDays)
{
   if (navigator == null || navigator.cookiesEnabled == false)
   {
      return;
   }

   if (surveyId == null || surveyInterval == null || askAgainDays == null)
   {
      return;
   }

   if (surveyId == "" || surveyInterval == 0 || askAgainDays == 0)
   {
      return;
   }
   
   currentSurveyId = surveyId;
   currentSurveyInterval = surveyInterval;
   currentAskAgainDays = askAgainDays;

   getCookieValues();

   if (isNewCookie && currentSurveyInterval == surveyGetRandom(1, currentSurveyInterval))
   {
      surveyConfirm();
   }
}

function surveyConfirm()
{
   EPW_Window_show_hide_box(null, "Survey Request", "surveyrequest.htm", 400, 205, "", 1, "img/ethofy-popup-x.gif")
}

function surveyConfirmed()
{
   EPW_Window_hide_other_alone();
   cookieSurveyTaken = "1";
   setCookieValues();
   surveyLoad();
}

function surveyLoad()
{
   var href = "http://survey.ethofytools.com/TakeSurvey.aspx?SurveyID=" + currentSurveyId;
   //var href = "surveycheckout.htm";
   EPW_Window_show_hide_box(null, "Windows Server Catalog Customer Survey", href, 800, 600, "", 1, "img/ethofy-popup-x.gif")
   //window.open(href, '', 'scrollbars=no,menubar=no,height=600,width=800,resizable=yes,toolbar=no,location=no,status=no');
}

function surveyDenied()
{
   EPW_Window_hide_other_alone();
   cookieSurveyDenied = "1";
   setCookieValues();
}

function surveyGetRandom(min, max)
{
   if (min > max)
   {
      return (-1);
   }
   if (min == max)
   {
      return (min);
   }

   var result = min;
   
   for (i = 0; i < 100; i++)
   {
      result = Math.floor(Math.random() * (max - min + 1)) + min;
   }
   
   return result;
}



function getCookieValues()
{
   cookieSurveyId = Cookies.get("surveyId", currentSurveyId);
   cookieSurveyDenied = Cookies.get("surveyDenied", "0");
   cookieSurveyTaken = Cookies.get("surveyTaken", "0");

   if (cookieSurveyId != currentSurveyId)
   {
      cookieSurveyId = currentSurveyId;
      cookieSurveyDenied = "0";
      cookieSurveyTaken = "0"
   }

   isNewCookie = (cookieSurveyDenied == "0" && cookieSurveyTaken == "0") ? true : false;
}

function setCookieValues()
{
   Cookies.set("surveyId", cookieSurveyId + "", 365);
   Cookies.set("surveyDenied", cookieSurveyDenied + "", currentAskAgainDays);
   Cookies.set("surveyTaken", cookieSurveyTaken + "", 365);
}
