// NPS Survey Script
// Controls the fireing and data transfer from the 
// Triggering page to the survey popup.


var surpressSurvey = false;

function launchNpsSurvey() {	
	if (hasSurveyFired()) {
		surpressSurvey = true;
	} else {
		surpressSurvey = false;
	}
	
	if (surpressSurvey == false) {
		var npsSurveyUrl = getNpsSurveyUrl();			
		setNpssurveyCookie();
		window.open(npsSurveyUrl, 'NPS_Survey', 'width=515,height=565,scrollbars=yes,toolbar=yes');
	} else {
		// do nothing	
	}
}

function launchSiteExitNpsSurvey(){
if (hasSurveyFired()) {
		surpressSurvey = true;
	} else {
		surpressSurvey = false;
	}
	if (surpressSurvey == false) {
		var npsSurveyUrl = getNpsSurveyUrl();	
		setNpssurveyCookie();
		window.open(npsSurveyUrl, 'NPS_Survey', 'width=515,height=565,scrollbars=yes,toolbar=yes');
	} else {
		// do nothing	
	}
}

// Creates Popup Url with required calling page data
function getNpsSurveyUrl() {
	var tmpDomain = getNpsSurveyDomain();
	var npsUrl = "";
	var callingPageName = getOmniturePageName();
	if (tmpDomain == null || tmpDomain.indexOf("tservicesq.com") > -1) {
		npsUrl = "https://www-test.tservicesq.com/global/popups/floatingFeedback.html";
	} else {
		npsUrl = "http://www.qwest.com/global/popups/floatingFeedback.html";
	}
	
	if (callingPageName != null) {
		npsUrl = npsUrl + "?callingPageName=" + callingPageName;
	} else {
		npsUrl = npsUrl + "?callingPageName=none";
	}
	
	return npsUrl;
}

// Checks to see if npssurvey cookie exists
function hasSurveyFired() {
	var hasCookie = false;
	var cookieValue = getCookie('npssurvey');
	
	if (cookieValue == null) {
		hasCookie = false;
	} else {
		hasCookie = true;
	}
	
	return hasCookie;
}

// Sets the npssurvey session cookie
function setNpssurveyCookie() {
	var domain = getNpsSurveyDomain();
	if(domain == null) {
		domain = '';
	}
	setCookie('npssurvey', 'true', '', '/', domain, '');	
}

// Utility Function to set genaric cookie
// TODO: Replace with global cookie utility script
function setCookie( name, value, expires, path, domain, secure )
{
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );
		
	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires : "" ) +
	( ( path ) ? ";path=" + path : "" ) +
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
}

// Utility Function to get a generic cookie
// TODO: Replace with global cookie utility script
function getCookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );


		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}

function getNpsSurveyDomain() {
	var cookieDomain = document.domain;
	if (cookieDomain.indexOf('qwest.com') > -1) {
		cookieDomain = ".qwest.com";
	} else if (cookieDomain.indexOf("tservicesq.com") > -1) {
		cookieDomain = ".tservicesq.com";
	} else {
		cookieDomain = null;
	}
	return cookieDomain;
}

function getOmniturePageName() {
	var omniturePageName = null;
	if (typeof s != "undefined") {
			if(typeof s.pageName != "undefined") {
				omniturePageName = s.pageName;
				//alert("Using s.pageName");
			}
	} 
			
	if (typeof s_pageName != "undefined") {
			omniturePageName = s_pageName;
			//alert("Using s_pageName");
	}
	
	return omniturePageName;
}

/**
* Adds a function to the window onunload event
*/
function addUnloadEvent(func) {
  var oldOnunload = window.onunload;
  if (typeof window.onunload != 'function') {
    window.onunload = func;
  }
  else {
    window.onunload = function() {
      oldOnunload();
      func();
    }
  }
}