// Floating Window Definition declaration

/* Constructor */ function InvitationSurveyManager(vInstanceVariableName, vSurveyName, vOpenInvitationDelay, vSurveyNotAnwseredReinvitationDelay, vSurveyAnwseredReinvitationDelay, vSurveyWindow)
{
    // Execution parameters 
    this.InstanceVariableName = vInstanceVariableName;
    this.SurveyName = vSurveyName;
    this.SurveyNotAnsweredReinvitationDelay = vSurveyNotAnwseredReinvitationDelay;  // In days
    this.SurveyAnsweredReinvitationDelay = vSurveyAnwseredReinvitationDelay;        // In days
    this.OpenInvitationDelay = vOpenInvitationDelay * 1000;

    // Invitation survey window
    this.SurveyWindow = vSurveyWindow;
    
    // Constants
    this.InvitationCookieName = "SurveyInvitation" + vSurveyName;
    
    this.WaitDocumentLoadComplete();
}

/* Private */ InvitationSurveyManager.prototype.WaitDocumentLoadComplete = function()
{
    if ((typeof(document.readyState) == "undefined" || /loaded|complete/.test(document.readyState)) && this.SurveyWindow.IsReady)
    {
        //On regarde si le sondage doit attendre et ne pas popper.
        if (typeof SondageDoitAttendreAvantPop == 'undefined' || SondageDoitAttendreAvantPop == false) {
            this.Execute();
        }
    }
    else
    {
        setTimeout(this.InstanceVariableName + ".WaitDocumentLoadComplete();", 333);
    }
}

/* Private */ InvitationSurveyManager.prototype.GetCookieExpirationDate = function(vIsSurveyAnswered)
{
	var reinvitationDelay = -1;

	if (vIsSurveyAnswered)
	{
	    reinvitationDelay = this.SurveyAnsweredReinvitationDelay;
	}
	else
	{
	    reinvitationDelay = this.SurveyNotAnsweredReinvitationDelay;
	}
	
	var reinvitationDate = new Date();
	
	reinvitationDate.setDate(reinvitationDate.getDate() + reinvitationDelay);
	
	return reinvitationDate;
}

/* Private */ InvitationSurveyManager.prototype.GetSurveyAnsweredIndicateur = function(vIsSurveyAnswered)
{
    var indicateur = null;

	if (vIsSurveyAnswered)
	{
	    indicateur = "T";
	}
	else
	{
	    indicateur = "F";
	}
	
	return indicateur;
}

/* Private */ InvitationSurveyManager.prototype.CreateSurveyCookie = function(vIsSurveyAnswered)
{
    var expirationDate = this.GetCookieExpirationDate(vIsSurveyAnswered);
	var indicator = this.GetSurveyAnsweredIndicateur(vIsSurveyAnswered);

	document.cookie = this.InvitationCookieName + "=" + indicator + "; expires=" + expirationDate.toGMTString() + "; path=/; domain=." + this.GetDomainWithoutSubDomain() + ";";
}

/* Private */
InvitationSurveyManager.prototype.GetDomainWithoutSubDomain = function()
{
    var parts = document.domain.split(".");

    return parts[parts.length - 2] + "." + parts[parts.length - 1];
}

/* Private */ InvitationSurveyManager.prototype.HasInvitationAlreadyBeenAsked = function()
{
    var pattern = new RegExp(this.InvitationCookieName + "=[A-Za-z0-9 -_];?");
    var cookieInvitation = document.cookie.match(pattern) + "";
    
	if(cookieInvitation == '' || cookieInvitation == null || cookieInvitation.length == 0)
	{
        return false;	
	}

	var equalPosition = cookieInvitation.indexOf("=");
	if (equalPosition == -1 || equalPosition >= cookieInvitation.length)
	{
	    return false;
	}
	
	var indicator = cookieInvitation.substring(equalPosition + 1, equalPosition + 2);
	if (this.GetSurveyAnsweredIndicateur(true) == indicator || this.GetSurveyAnsweredIndicateur(false) == indicator)
	{
        return true;
	}
	else
	{
        return false;
	}
}

/* private */ InvitationSurveyManager.prototype.IsCookieSupportedByClient = function()
{
    var cookieEnabled = (navigator.cookieEnabled) ? true : false;

    //if not IE4+ nor NS6+
    if (typeof navigator.cookieEnabled == "undefined" && !cookieEnabled)
    { 
        document.cookie = "testcookie";
        cookieEnabled = (document.cookie.indexOf("testcookie") != -1) ? true : false;
    }

    return cookieEnabled;
}

/* Private */ InvitationSurveyManager.prototype.Execute = function()
{
	if (!this.IsCookieSupportedByClient())
	{
		return;
	}

	if(this.HasInvitationAlreadyBeenAsked())
	{
        return;
	}

	this.CreateSurveyCookie(false);
	
	// Preload the page content.
	this.SurveyWindow.PreLoadPageContent();
	
    setTimeout(this.SurveyWindow.InstanceVariableName + ".Open()", this.OpenInvitationDelay);
    
}

/* Public */ InvitationSurveyManager.prototype.SurveyAnswered = function()
{
    this.CreateSurveyCookie(true);
}


