//  Nokia.com UserGuide Survey 2006 Opener
//

//
// Based on
// Nokia.com Research 2004 KPI Survey Opener
// 13.04.2004 Vesa Kivistýý
// Satama Interactive
//
// Modified for support survey by
// 9.6.2004 Tom Kostiainen
// 16.6.2006 Markku Mantere
// 19.6.-21.6.2006 Ari-Pekka Lappi
// Satama Interactive

// tweaks 18-19.12.2006 Sakari Laaksonen 
// 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
http://europe.nokia.com/appmanager/UnitedStatesEnglishEUROPE_NOKIA_COM_3/Get_support;JSESSIONID_NCOM=HLsfFHTCJy4yTpd2fMVH2C1JSybypv6fw60lvqtZkn1b47gb9yj5!1883700377?_nfpb=true&_windowLabel=T22930314731155711190871&_pageLabel=P181285&wsrp-urlType=blockingAction&wsrp-url=&wsrp-requiresRewrite=&wsrp-navigationalState=&wsrp-interactionState=_action%3D%2Fpageflows%2FEMEASupportTool%2Fbegin&wsrp-mode=&wsrp-windowState=
                        
var surveyURL        = "https://digiumenterprise.com/answer/?sid=72179&chk=RUXV4A97";
var surveyName       = "nokiacomUserGuideSurvey2006"; // no special characters!!!
var cookieNames = new Array("UserGuideSurvey_2006_shown","SupportSurvey_2004_shown");
//var lotteryMaxValue  = 50; //testing value...
var lotteryMaxValue  = 50; // p_survey_openned = 1/2*(lotteryMaxValue-1) 
                           // 1=always, 2=~50%, 3=25%, 11=5%, 51=1%, 101=0,5%, 201=0,25%
													 // (Values corrected by apl 19.5.2005)mist


//EXEPTION URLS NOT NECCESSARY IN THIS SURVEY
//var templateIDs = new Array('A4162174','A4158012','A4151185','A4151136','A4144769','A4151147','A4164139','A4162152','A4149137','A4144738','A4158098','A4213023','A4164166','A4162308','A4160017','A4144398','A4144789','A4143878','A4143928','A4144733','A4144761','A4176015','A4158045','A4144758','A4143755','A4144008','A4162145','A4144169','A4162325','A4144160','A4144821','A4143851','A4144166','A4143934','A4144141','A4144321','A4151101','A4143692','A4143953','A4143708','A4144100','A4144714','A4209014','A4195034','A4164327','A4144314','A4144700','A4144729','A4144764','A4164151','A4211110','A4241018','A4143632','A4144879','A4144799','A4149114','A4149105','A4174143','A4162318','A4144204','A4143907','A4144014','A4144389','A4160369','A4164027','A4144150','A4144807','A4144814','A4195028','A4143048','A4143069','A4143892','A4143386','A4144093','A4144241','A4144213','A4144352','A4144371','A4144339','A4143671','A4143796','A4144174','A4144196','A4144231','A4144825','A4144832','A4239034','A4144841','A4239028','A4144108','A4143433','A4144305','A4144687','A4164380','A4143233','A4143255','A4144264','A4144851','A4144182');
var templateIDs = new Array();
//MARKETING URLS NOT SECCESSARY IN USER GUIDE SURVEY

// Check marketing URLs on R5 and check if these are still valid after
// R5 version goes out /mma 16.6.2006
// These will apply if browsed via links but not if an user uses bookmarks or 
// write the address to the address bar. (apl 21.6.2006)
var marketingURLs = new Array();
//var marketingURLs = new Array(
''
/*'support/glossaryCDAMaster/',
'include/support/pcsuite_disclaimer.html',
'include/support/mmp_disclaimer.html',
'include/support/software_printable.html',
'mouse2mobile.com/clients/nokia/',
'nokia.com/cda9?id=923',
'nokia.com/support/tutorials/',
'endusersupport.nokia.com/phones/',
'nds1.nokia.com',
'nokia.com/popupDisclaimer',
'faqsearch/Search'
);*/

// END: SETTINGS
// These are used in window.onunload event. 
var someLinkClicked = false;
var formSubmited = 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();
    var expirationTime = 24*60*60*1000*180;  
    date.setTime(date.getTime() + expirationTime) //(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;
}

//not really neccessary in user guide survey
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);
  }
  
  // This is the new implementation for R5 /mma 16.6.2006
  // This is totally based the assumption that all URLs are simple A-link types
  // i.e. http://www.europe.nokia.com/A4143001
  //
  // Errorous braches fixed. /apl 19.6.2006  

  if(URLpath.charAt(0) == 'A'){
    for(j = 0; j < templateIDs.length; j++) {
      if(URLpath.toString() == templateIDs[j].toString() ) {
        return true;
      }
    }
  }
  return false;
}


//////////////////////// USERGUIDE SURVEY MODIFICATIONS //////////////////////
//19.12.2006

///////////////////////// SUPPORT SURVEY ADD-ONS ////////////////////////////
//n/a
     
function hasOnClickEvent(linkNumber) {
    // Return false if no previous onClick event exists
    // Code simplified. (apl 20.6.2006)
    var onclickString = document.links[linkNumber].onclick;
    if(onclickString == null) return false;
    return true;
}

// This this is used to prevent the script from opening the survey 
// popup. 
// Function asserts "formSubmited=true" behind every form's 
// onSubmit-event. Unfotunatelu, currently this won't apply 
// to the forms loaded after page is loaded.  
function setOnSubmitEvent(formNumber) {
    // if there is already onSubmin-event handler in the element,
    // no changes will be made. 
    var onSubmit = document.forms[formNumber].onsubmit;
    if(onSubmit == null) {
        document.forms[formNumber].onsubmit = setFormSubmited;
        return true;
    } 
    return false;
}

function setFormSubmited() {
    formSubmited = true;
}

// Return true if address contain any of the strings in 
// marketingURLs array.
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;
}

// Return true if link is either on of A-links listed in array 
// templateIDs or is in marketin URLs array. 
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() {
 	//Modified by Sakke Laaksonen, Satama Interactive (Naviatech)
   //19.12.2006
   //fixed some previous faulty logics w/ Ari Lappi and commented unneccessary features.
   
   // 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 = plainLinkClick;
	    
	    } else {*/
       
       if(!hasOnClickEvent(i)) {
//this could use a tweak. Is it possible to combine existing functions with new ones using anonymous functions or something?
	   document.links[i].onclick=  surveyOpenerLotteryViaLink; }
 	}
}

function checkFormsOnload() {
   // Goes through all forms in the page; prevent the script from opening popup 
	 // if a form is submitted. /apl 20.6.2006
   //NOTE. does not always work. _   	
	 for(i = 0; i < document.forms.length; i++) {
	   setOnSubmitEvent(i);
	   
 	}
}

function surveyOpenerLottery() {
  // check if this survey has already been shown
  
  if(readSurveyCookie(cookieNames[0]) == "yes" ||  readSurveyCookie(cookieNames[1] == 'yes')) {
      //alert('cookie found');
      // Return without doing anything
      return;
  }
	
	var randNum = Math.round(Math.random()*(lotteryMaxValue-1));
  if(randNum == 0) {
      // hit by lottery
      createSurveyCookie(cookieNames[0], "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();
	checkFormsOnload();
};

// Attaches surveyOpenerLottery function to onUnload event, if a link was not clicked or 
// any form submitted. 
//  
// Note: unfortunately this doesn't apply to some applications and survey may open
// even thought it should not. (apl 21.6.2006)   
window.onunload = function(){
     if(!someLinkClicked && !formSubmited) {
          surveyOpenerLottery();
     }
};
