// Nokia.com Support Survey 2004 Opener
// 9.6.2004 Tom Kostiainen
// Satama Interactive
//
//Nokia.at
//12.01.06
//
// Based on
// Nokia.com Research 2004 KPI Survey Opener
// 13.04.2004 Vesa Kivisto
// Satama Interactive
//
// This JS file is included to relevant templates (defined in the related ICO CR)
// Purpose of this JS file is to (possibly) open up a popup survey when user leaves the
// Research section. If survey is opened once, the user will never get the survey popup again.

window.onerror = null;

// BEGIN: SETTINGS
                        
var surveyURL        = "http://digiumenterprise.com/answer/?sid=38201&chk=CWX78KCW";
var surveyName       = "nokiaatSupportSurvey2004"; // no special characters!!!
var surveyCookieName = "atSupportSurvey_2004_shown";
var lotteryMaxValue  = 5; // 1=always, 2=50%, 3=33%, 5=20%, 10=10%, 50=2%, 100=1%, 200=0.5%

var templateIDs = new Array();

var marketingURLs = new Array(
'support/',
'cgi/dl/DL/',
'nokia.com/cda9?id=923',
'nokia.com/support/tutorials/',
'endusersupport.nokia.com/phones/',
'nds1.nokia.com',
'nokia.com/popupDisclaimer',
'faqsearch/Search');

// END: SETTINGS

var someLinkClicked = false;

// BEGIN CODE ---- DO NOT MODIFY BELOW THIS LINE ------------------------------------
function openSurveyPopup() {
 window.open(surveyURL, surveyName, 'scrollbars=yes,status=no,width=630,height=648')
}

function createSurveyCookie(name, value, days) {
 if(days) {
  var date = new Date();
  date.setTime(date.getTime()+(days*60*60*1000)); //*24*60*60*1000
  var expires = "; expires="+date.toGMTString();
 } else {
  expires = "";
 }
 document.cookie = name+"="+value+expires+"; path=/";
}

function readSurveyCookie(name) {
 var nameEQ = name + "=";
 var ca = document.cookie.split(';');
 for(var i=0;i < ca.length;i++) {
  var c = ca[i];
  while (c.charAt(0)==' ') c = c.substring(1,c.length);
  if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
 }
 return null;
}

function pathMatchesSomeTemplateID(URLpath) {
 // to conform with both IE & Mozilla style split length counter, remove the possible first slash-mark
 if( URLpath.charAt(0) == '/') {
  URLpath = URLpath.substring(1, 9999);
 }
 // Checks the given path's templateID against templateIDs array
 var pathSlashParts = URLpath.toString().split("/");
 if(pathSlashParts.length > 1) {
  if(pathSlashParts[0] == 'nokia') {
   var digitParts = pathSlashParts[1].split(",");
   if(digitParts.length > 3) {
    var templateID  = digitParts[2];
    for(j = 0; j < templateIDs.length; j++) {
     if(templateID == templateIDs[j].toString() ) {
      return true;
     }
    }
   }
  }
 }
 return false;
}


///////////////////////// SUPPORT SURVEY ADD-ONS ////////////////////////////

     
function hasOnClickEvent(linkNumber) {
	// Return false if no previous onClick event exists
    var onclickString = document.links[linkNumber].onclick;
    if(onclickString == null) onclickString = '';
    onclickString = new String(onclickString);
    return (onclickString != '');
}
   
function pathMatchesSomeMarketingURL(url) {
   // Goes through all marketing URLs to see if there is one that matches with the link URL
	for(n = 0; n < marketingURLs.length; n++) {
		if(url.indexOf(marketingURLs[n]) > -1) {
		return true;
		}
	}
	return false;
}

function pathMatchesSomeTemplateIDorMarketingURL(clickedLink) {
 	// Checks the given path's templateID against templateIDs array
 	if(pathMatchesSomeTemplateID(clickedLink.pathname)) return true;
 	// Checks the given path against the marketing URLs array
 	if(pathMatchesSomeMarketingURL(clickedLink.href)) return true;
 	// No matches were found
 	return false;
}

function checkLinksOnload() {
 	// Goes through all links in the page; attach onClick call to surveyOpenerLottery if template ID is not in the list or
 	// URL is not in the template ID format 
 	for(i = 0; i < document.links.length; i++) {	
	  if(!hasOnClickEvent(i)) {	
    	if (!pathMatchesSomeTemplateIDorMarketingURL(document.links[i])) {
		document.links[i].onclick = surveyOpenerLotteryViaLink;
	  	} else {
	  	document.links[i].onclick = plainLinkClick;
	  	}
	  }
  	}
}

function surveyOpenerLottery() {
 	// check if this survey has already been shown
 	if( readSurveyCookie(surveyCookieName) == "yes" ) {
 		// Return without doing anything
 		return;
 	}
	
 	var randNum = Math.round(Math.random()*(lotteryMaxValue-1));
 	if(randNum == 0) {
  		// hit by lottery
  		createSurveyCookie(surveyCookieName, "yes", 180); //expires in 180 days
  		openSurveyPopup();
 	}
}

function surveyOpenerLotteryViaLink() {
   // Set flag when link is clicked
	someLinkClicked = true;
 	surveyOpenerLottery();
}

function plainLinkClick() {
   // Set flag when link is clicked
	someLinkClicked = true;
}

// Attaches checkLinksOnload function to onLoad event 
window.onload = function(){
	checkLinksOnload();
};

// Attaches surveyOpenerLottery function to onUnload event, if a link was not clicked 
window.onunload = function(){
	if(!someLinkClicked) {
 	surveyOpenerLottery();
	}
};
