// Nokia.com Support Survey 2004 Opener
// 9.6.2004 Tom Kostiainen
// 16.6.2006 Markku Mantere
// 19.6.-21.6.2006 Ari-Pekka Lappi
// 18.12.2007 Sakari Laaksonen ->
// removed the template checks and fixed a bug with onload functions and IE popups
//
// Satama Interactive
//
// Based on
// Nokia.com Research 2004 KPI Survey Opener
// 13.04.2004 Vesa Kivistö
// 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=156640&chk=UXBNY3J8";
var surveyName       = "supportsurvey200801"; // no special characters!!!
var cookieName = "supportsurvey_200801_shown";


var lotteryMaxValue  = 1601; // 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)
// END: SETTINGS
// These are used in window.onunload event. 
var someLinkClicked = false;
var formSubmited = false;
var marketingURLs = new Array();
var templateIDs = new Array();

// 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;
}



///////////////////////// SUPPORT SURVEY ADD-ONS ////////////////////////////

     
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;
}



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)) {
        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   	
	 for(i = 0; i < document.forms.length; i++) {
	   setOnSubmitEvent(i);
 	}
}

function surveyOpenerLottery() {
  // check if this survey has already been shown
  
  if(readSurveyCookie(cookieName) ){
       // Return without doing anything
       
      return;
  }
	
	var randNum = Math.round(Math.random()*(lotteryMaxValue-1));
  if(randNum == 0) {
      // hit by lottery
      createSurveyCookie(cookieName, "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 
//preserve any previous functions that  may already be added prior to running this script
var oldFunc = window.onload;
window.onload = function() {
  if(typeof oldFunc == 'function') {
    oldFunc();
  } 
	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();
     }
};
