/* 2007 CIGNA */
/* This file can be referenced directly from a webpage, or through survey.htm */

function GetCookie (name) {
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) {
		var j = i + alen;

		if (document.cookie.substring(i,j) == arg)
			return getCookieVal(j);
			i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break;
	}
	return null;
}

function getCookieVal(offset) {
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1)
	endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}

/* This function prints text to the screen which will allow visitors with popup blockers to access the survey
To initiate, place the following inside your HTML
<script type="text/javascript">displayText()</script>
*/
function displayText() {
	//Test if survey has already been taken, but checking for a cookie
	var redesign_survey = GetCookie('redesign_survey');
	var surveyText = "";

	if (document.cookie && (document.cookie.indexOf('redesign_survey') > -1)) {
		// User has taken survey, do nothing
		return false;
	}

	else {
		// User has not taken the survey, create message.
		surveyText = "<h3><a href='#' onClick='showSurvey()'>CIGNA.com Survey</a></h3><h3>Tell us about you!</h3><p>We're always looking to improve your experience and convenience and would like to get some input that will help us learn about you and your needs. Please <a href='#' onClick='showSurvey()'>take the survey</a> to let us know what you think.</p>";
	}
	// Print the message to the screen.
	document.writeln(surveyText);
	return;
	// -->
}

/* This function should be called either onLoad or onUnLoad. The preferred method is onUnLoad.
<body onUnLoad="survey()">
 */
function survey() {
	// A valid referrer has been set.

	//Cookie to test if survey was taken
	var redesign_survey = GetCookie('redesign_survey');
	if (document.cookie && (document.cookie.indexOf('redesign_survey') > -1)) {
		// User has taken survey, do nothing
		return false;
	}

	else {
		// User has not seen disclaimer window or they have clicked 'Later' button
		window.open('/survey.htm','Survey','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=350,height=210,left=10,top=10');
	}
	// -->
}

function chk_domain(r) {
	// If the user typed in a CIGNA address, no referrer is passed. Set one by default.
	if (!(r)) {r = "https://secure.cigna.com";}

	// Convert to lower case
	r = r.toLowerCase();
	camefrom = r.split(':\/\/');
	camefrom1 = camefrom[1]; // Return a.b.com/x/y/z.html
	mydomain = camefrom1.split('\/'); // Return a.b.com

	// Send clean domain back to chk_ref()
	return mydomain;
}

/* This function should be called when the surbey should NOT be taken if accessed via myCIGNA, or another portal */
function chk_ref() {
	// Grab referring string from browser
	var myreferrer = document.referrer;

	// Call chk_domain to verify or set domain, and clean for processing
	chk_domain(myreferrer);

	//return false; // End function in case of errors

	// Clean domain returned for validating against restricted CIGNA sites

	switch(mydomain[0]) {
		case "s-my.cigna.com":
			break;
		case "my.cigna.com":
			break;

		case "s-sso.corp.cigna.com":
			break;
		case "sso.corp.cigna.com":
			break;

		case "s-cignaaccess.cigna.com":
			break;
		case "cignaaccess.cigna.com":
			break;

		case "s-cignaforhcp.cigna.com":
			break;
		case "cignaforhcp.cigna.com":
			break;

		case "s-secure.cigna.com":
			break;
		case "secure.cigna.com":
			break;

		// Avoid new window, no referrers, which are empty ""
		case "":
			break;

		// Allow new window, no referrers
		//case "": survey();

		// No cases matched the restricted domain strings. Call survey function.
		default: survey();
	}
}

function showSurvey() {
	var allCookies = document.cookie;
	var survey_response = allCookies.indexOf("redesign_survey");

	// Set expiration for 31 days.
	var expireOn = new Date();
	var expireIn = 31;
	expireOn.setTime(expireOn.getTime() + 1000 * 60 * 60 * 24 * expireIn);

	// Build the cookie regardless of 'response'
	var acceptSurvey = escape('redesign_survey') + ';expires=' + expireOn;
	document.cookie = acceptSurvey;

	var surveyURL = 'https://vovici.com/wsb.dll/s/2232g27c15';
	window.open(surveyURL,'SurveyWin','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=760,height=500,left=10,top=10');
	//window.location.reload();
	//window.location.blur();
	return false;
}

function init(){
	//chk_ref(); 	// Validate the user didn't come from a portal
	//survey();	// Ignore site validation
}

//window.onload=init;