//RANDOMLY POPS UP A SURVEY REQUEST FORM.
//
// Params:
//	surveyURL:  fully-qualified URL to survey pop-up
//	frequency:  survey will be offered to 1 out of every <frequency> visitors
//	width:  width, in pixels of the popup window
//	height:  height, in pixels, of the popup window
//	cookieSurveyID:  unique ID to specify which survey the cookie will be set for
//	cookieExpire:  number of days cookie will be set to expire to allow the same visitors to retake the survey
//


function popupSurvey(surveyURL, frequency, width, height, cookieSurveyID, cookieExpire) {
	var rand = Math.random();
	var thisFrequency = 1/frequency;
	var currTime = new Date();
	var expireTime = new Date();
	expireTime.setTime(currTime.getTime() + cookieExpire*3600000*24);
	var style ="_blank";
	var features ="toolbar=no,"+
		"location=no,"+
		"directories=no,"+
		"status=no,"+
		"menubar=no,"+
		"scrollbars=1,"+
		"resizable=1,"+
		"width="+width+","+
		"height="+height+"";

	//alert(rand+"/"+thisFrequency+"/"+frequency);

	if ((getSurveyCookie(cookieSurveyID) == "true") || (getSurveyTempCookie(cookieSurveyID) == "true")) {
		//If cookie or temp cookie for this surveyID is already set, don't do anything...
		return false;
	} else {
		if (rand <= thisFrequency) {
			//Pop survey if frequency is met...
			window.open(surveyURL,style,features);
			//Set full cookie to not re-prompt this user again until expiration date
			document.cookie = "iomCSSurvey_"+cookieSurveyID+"=true;expires="+expireTime.toGMTString()+"";
		}
		//Set temp cookie to not re-prompt this user this session, even if they haven't taken this surveyID yet
		document.cookie = "iomCSSurvey_"+cookieSurveyID+"_temp=true;expires=";
		return true;
	}
}


function getSurveyCookie(csID){
	var cname = "iomCSSurvey_"+csID+ "=";
	var dc = document.cookie;
	if (dc.length > 0) {
		begin = dc.indexOf(cname);
		if (begin != -1) {
			begin += cname.length;
			end = dc.indexOf(";", begin);
			if (end == -1) end = dc.length;
				return unescape(dc.substring(begin, end));
			}
		}
	return null;
}


function getSurveyTempCookie(csID){
	var cname = "iomCSSurvey_"+csID+ "_temp=";
	var dc = document.cookie;
	if (dc.length > 0) {
		begin = dc.indexOf(cname);
		if (begin != -1) {
			begin += cname.length;
			end = dc.indexOf(";", begin);
			if (end == -1) end = dc.length;
				return unescape(dc.substring(begin, end));
			}
		}
	return null;
}


