/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  This function just opens the survey invitation popup window.  Of course the first argument to
  window.open is the url of the invitation, so you can set this to whatever invitation you would like
  using the first argument to openInvitation()
  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

function openInvitation(invitationUrl, title, params)
{
	var newWindow = window.open(invitationUrl, title, params);
	newWindow.focus();
}

function showSurveyInvite(rate, cookieName, surveyExpires, inviteUrl, title, params)
{
    var n = Math.random();
    var percent = 1 / rate;
    percent = Math.round(percent * 100);
    var maxnumber = 100;
    var result = Math.round(n * maxnumber);
    var expirationDate = new Date(surveyExpires);
    // Show the invite if the survey isn't already expired.
    if(new Date() < expirationDate)
    {
        // Not expired, is the result under the specified percentage?
        if (result < percent + 1)
        {
            if (document.cookie.indexOf(cookieName + "=true")==-1)
            {
                // Set a cookie on the user's machine so we only ask them once about the survey.  Cookie expires when survey does.
                document.cookie = cookieName + "=true;host='.marketwatch.com';path=/;expires=" + expirationDate.toString();
                openInvitation(inviteUrl, title, params);
            }
        }
    }
}

function netPromoterSurvey(inviteUrl)
{
	showSurveyInvite('100', 'netpromotersurveypop', 'November 1, 2008', inviteUrl, 'Survey', 'width=350,height=250');
}