function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    {
    c_start=c_start + c_name.length+1;
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    }
  }
return "";
}
 
function setCookie(c_name,value,expireDays,expireMinutes)
{
var exdate=new Date();
exdate.setDate(exdate.getDate());
if (expireMinutes > 0)
            {exdate.setMinutes(exdate.getMinutes()+expireMinutes);}
if (expireDays > 0)
            {exdate.setDate(exdate.getDate()+expireDays);}
document.cookie=c_name+ "=" +escape(value)+
((expireDays==null) ? "" : ";expires="+exdate.toGMTString());
}
 
// Let's go take the survey!
function takeSurvey() {
	// Open a new window to the survey
	// at the end of the window statement .blur will force
	// the new window to the background with most browsers
	window.open('http://www.surveymonkey.com/s/ZBBTDRR','','width=1050,height=700,left=200,top=200,scrollbars=yes');
	// Forces the current window to remain on top
	setCookie('BGE_SurveyFlag',1,30,0);
}

// Do we have a survey cookie?
function checkCookie() {
	surveyPageCount = getCookie('BGE_SurveyPageCount');
	
	if( surveyPageCount == 0 || surveyPageCount == null ) { // Nope
		surveyPageCount = parseInt( 1 );
		setCookie( 'BGE_SurveyPageCount', 1, 30, 0 );
	} else if (surveyPageCount <= 5) {
		surveyPageCount = parseInt(surveyPageCount) + 1;
		setCookie( 'BGE_SurveyPageCount', surveyPageCount, 30, 0 );
	} else {
		// 5 Page view threshold before asking user to take a survey
		surveyFlag = getCookie('BGE_SurveyFlag');
		if( surveyFlag == 0 || surveyFlag == null ) {
			setCookie( 'BGE_SurveyFlag', 1, 0, 600 );
			OpenWindow = window.open("", "newwin", "height=386, width=600,left=200,top=200,toolbar=no,scrollbars=no,menubar=no");
			if( OpenWindow != undefined && OpenWindow != null ) { // No Popup Blocker
				OpenWindow.document.write("<html><head>")
				OpenWindow.document.write("<title>PeakRewards&#x2120;</title>");
				OpenWindow.document.write("<style>span.smark { font-family: \"Lucida Sans Unicode\", \"Lucida Sans Regular\", \"Arial Unicode\", \"Arial Unicode MS\", \"Calibri\" !important;line-height: 0 !important; font-size: 80% !important;}</style>");
				OpenWindow.document.write( "<style type=\"text/css\" src=\"http://peakrewards.bgesmartenergy.com/default/basic\"></style>" );
				OpenWindow.document.write( "<style type=\"text/css\" src=\"http://peakrewards.bgesmartenergy.com/peakrewards/pr-css\"></style>" );
				OpenWindow.document.write( "</head>" );
				OpenWindow.document.write("<body style=\"background-color: #efefef;\"><div style=\"font-family:arial,helvetica,sans-serif; font-size:small;\">");
				OpenWindow.document.write("<h2>BGE Smart Energy Savers Program<span class=\"smark\">&#x2120;</span></h2>");
				OpenWindow.document.write("<p><strong>Before you go...</strong></p>" );
				OpenWindow.document.write("<p>Can you spare a few minutes to tell us about your experience on our site? Good, bad, satisfying, frustrating? Your feedback really matters to us. We would like to make our site work for you &mdash; and others like you. Please answer the following short survey and let us know what you think.</p>");
				OpenWindow.document.write("<p>Thank you!</p><p>The BGE Smart Energy Savers Program<span class=\"smark\">&#x2120;</span> Team</p>");
				OpenWindow.document.write("<div style='text-align:center;'><br /><input type='button' id='btnYes' value='Yes' />");
				OpenWindow.document.write("<input type='button' id='btnMaybe' value='Maybe Later' />");
				OpenWindow.document.write("<input type='button' id='btnNever' value='Never' /></div>");
				OpenWindow.document.write("</div></body>");
				OpenWindow.document.write("</html>");
				OpenWindow.document.getElementById('btnYes').onclick = function(){OpenWindow.close();takeSurvey();};
				OpenWindow.document.getElementById('btnMaybe').onclick = function(){OpenWindow.close();setCookie('BGE_SurveyFlag',1,1,0);};
				OpenWindow.document.getElementById('btnNever').onclick = function(){OpenWindow.close();setCookie('BGE_SurveyFlag',1,30,0);};
				OpenWindow.blur();
				window.focus();
			}
		}
	}
}

checkCookie();
