window.onerror = null;

// BEGIN: SETTINGS
var surveyURL = "http://digiumenterprise.com/answer/?sid=111953&chk=4MWXSCJH";
var surveyName = "nokiacomFAQSurveyForN80N73N80"; // no special characters!!!
var cookieNames = new Array("FAQSurvey_2007_shown");

// END: SETTINGS
// These are used in window.onunload event. 
var someLinkClicked = false;

//Cookie functions
function checkForCookies() {
	if(readSurveyCookie(cookieNames[0]) == "yes" ||  readSurveyCookie(cookieNames[1] == 'yes')) {
		// alert('cookie found');
		// Return without doing anything
		return true;
	} 
    else { return false; }
}

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;
}

// popup functions 
function openSurveyOnLink() {
	someLinkClicked = true;
	if(checkForCookies()) {
		return;
	} else {
		createSurveyCookie(cookieNames[0], "yes", 180); //expires in 180 days
		openSurveyPopup()    
	}	
}

function openSurveyPopup() {
	window.open(surveyURL, surveyName, "scrollbars=yes,status=no,width=630,height=648")
}

//onclick event functions 
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;
}

function checkLinksOnload() {
	// Goes through all links in the page; attach onClick call that opens a popup if cookie is not set.
	for(i = 0; i < document.links.length; i++) {
		// alert(document.links[i].href);
		if(!hasOnClickEvent(i)) {
			document.links[i].onclick =  openSurveyOnLink;
		}
		else {
			var oldFunction  = document.links[i].onclick;
			document.links[i].onclick = function () {  
				// alert(oldFunction);
				oldFunction();
				openSurveyOnLink();
			}
		}
	} 
}

// Attaches checkLinksOnload function to onLoad event 
window.onload = checkLinksOnload;

//if cookie is not set and user closed the page (did not click any links), show the survey popup   
window.onunload = function(){
	if(!someLinkClicked && !checkForCookies) {
		openSurveyPopup();
	}
};

