newsi.Survey = function(obj){
	
	var pollRates=new Array();
	this._id="";
	this._groupId="";
	this._isEnabled=true;
	this._isPopUp=true;
	this._isExternalHost=false;	
	this._url="";
	this._pageCountToLaunch=0;
	this._pollRate=0;
	this._cookieHours=0;

	if(obj.id){this._id=obj.id;}
	if(obj.groupId){this._groupId=obj.groupId;}else{this._groupId=this._id;}
	if(obj.isEnabled!=null){this._isEnabled=obj.isEnabled;}
	if(obj.isPopUp!=null){this._isPopUp=obj.isPopUp;}
	if(obj.isExternal!=null){this._isExternalHost=obj.isExternal;}
	if(obj.surveyURL){this._url=obj.surveyURL;}
	if(obj.pageCountToLaunch){this._pageCountToLaunch=obj.pageCountToLaunch;}
	if(obj.pollRate){this._pollRate=obj.pollRate;}
	if(obj.pollNewUser){var pollRateForNewUser=obj.pollNewUser;}else{var pollRateForNewUser=this._pollRate;};
	if(obj.cookieHours){this._cookieHours=obj.cookieHours;}
	
	if (this._isEnabled==false){
		this._clearCookie();
	}else{
		// if no BBC-UID treat as first time visitor
		this._hasBBC_UID=this._testForBBC_UID("BBC-UID");
		// update session cookie which provides pages visited and whether new user
		var newSessionCookieContent=this._updateSessionCookie();	
		this._pagesVisitedCount=parseInt(newSessionCookieContent[this._id]);
		this._isNewUser=newSessionCookieContent.isNewUser;
		// set pollrate - different pollrate for new users
		if (this._isNewUser){this._pollRate=pollRateForNewUser;}
		// if user has visited specific number of pages then run survey decider
		if ((this._pageCountToLaunch==0) || (this._pagesVisitedCount>=this._pageCountToLaunch)){
			this._surveyLaunchDecider(this._pollRate);
		}
		//this._traceDetails();
	}
}

newsi.Survey.prototype.init = function(){
	
}
newsi.Survey.prototype._traceDetails = function(){
	trace("********** SURVEY COOKIE *************");
	trace("Pages visited= "+this._pagesVisitedCount);
	trace("New User= "+this._isNewUser);
	
	trace("id: "+this._id);
	trace("url: "+this._url);
	trace("pollRate: "+this._pollRate);
	trace("isEnabled: "+this._isEnabled);
	trace("isPopUp: "+this._isPopUp);
	trace("pageCountToLaunch: "+this._pageCountToLaunch);
	trace("**********END OF SURVEY COOKIE*************");
}
newsi.Survey.prototype._surveyLaunchDecider = function(pollRate){
	var randomNumber=parseInt(Math.random()*pollRate,10);
	if (randomNumber==0){this._launchSurvey();}
}
newsi.Survey.prototype._launchSurvey=function(){
	var isPreviouslyShown=this._saveSurveyIdToCookie();
	if (isPreviouslyShown==false){
		if(this._isPopUp){
			document.open(this._url,"BBCNewsSurvey", "width=660, height=550, resizable, scrollbars");
		} else {
			// for future - launch in DIV
		}
	}
}


/*
COOKIE FUNCTIONS
*/
newsi.Survey.prototype._testForCookie=function(thisCookie, cookieName){
	if(thisCookie.load(cookieName)){return true;} else {return false;}
}
newsi.Survey.prototype._testForBBC_UID=function(cookieName){
	var cookieContent="";
	var cookie=new newsi.Cookie({name:cookieName,content:cookieContent,path:'/'});
	var hasBBC_UID_Boolean=this._testForCookie(cookie,cookieName);
	
	cookie.load();
	return hasBBC_UID_Boolean;
}
newsi.Survey.prototype._testForNewUser=function(cookieContent){	
	var str=String(cookieContent);
	var n=str.length;
	var lastChar=str.charAt(n-1);
	if (lastChar=="n"){return true;}else{return false;}
}

newsi.Survey.prototype._saveSurveyIdToCookie=function(){
	var cookieContent=new Object();
	var cookieName="BBCNewsSurvey";
	var cookie=new newsi.Cookie({name:cookieName,content:cookieContent,hours:this._cookieHours,path:'/'});

	var hasCookie=this._testForCookie(cookie, cookieName);
	if(hasCookie){cookieContent=cookie.content;}
	if (cookieContent[this._groupId]){
		return true;
	}else{
		cookieContent[this._groupId]=true;
		var cookie=new newsi.Cookie({name:cookieName,content:cookieContent,hours:this._cookieHours,path:'/'});
		cookie.store();
		return false;
	}
	
}
newsi.Survey.prototype._clearCookie=function(){
	var cookie=new newsi.Cookie({name:"BBCNewsVisitedPageCount",content:"",path:'/'});
	cookie.remove();
	var cookie=new newsi.Cookie({name:"BBCNewsSurvey",content:"",path:'/'});
	cookie.remove();
}
newsi.Survey.prototype._updateSessionCookie=function(){
	var pagesVisited=0;
	var newVisitorTag="";
	var cookieName="BBCNewsVisitedPageCount";
	var cookieContent=new Object();
	cookieContent.isNewUser=false;
	cookieContent[this._id]=0;
	var cookie=new newsi.Cookie({name:cookieName,content:cookieContent,path:'/'});
	// if there is a cookie named BBCNewsVisitedPageCount then grab it's value and convert to integer
	var hasCookie=this._testForCookie(cookie, cookieName);
	if(hasCookie){
		cookieContent=cookie.content;
		if(cookieContent[this._id]){
			pagesVisited=parseInt(cookieContent[this._id]);
		} else {
			pagesVisited=0;
		}
	}else if (this._hasBBC_UID==false){
		cookieContent.isNewUser=true;
	}
	// increment the value and store updated value back to cookie
	pagesVisited++;
	cookieContent[this._id]=pagesVisited;
	cookie=new newsi.Cookie({name:cookieName,content:cookieContent,hours:1,path:'/'});
	cookie.store();
	cookie.load();
	return cookieContent;
}

newsi.surveyInit=function(_userLocation){


	var lifeOfSurvey=180;
	var pollRate=4000;
	var pollRateNewUser=2000;
	var pageCountToLaunch=3;
	var isEnabled=true;
	
	if (_userLocation !="DOMESTIC"){
	//if (this._getEdition()!="Domestic"){
		if(this._getSite()=="sport"){
			gBBCNewsSurveyList["benchmarkingsport"]=new newsi.Survey({id:"benchmarkSurveySport", groupId:"benchmarkSurvey", surveyURL:"http://collections.europarchive.org/tna/20080205132101/http://news.bbc.co.uk/shared/survey/benchmarksurveysport.stm", pollRate:pollRate, pollNewUser:pollRateNewUser, cookieHours:lifeOfSurvey, pageCountToLaunch:pageCountToLaunch, isPopUp:true, isEnabled:isEnabled});
		}else{
			gBBCNewsSurveyList["benchmarking"]=new newsi.Survey({id:"benchmarkSurvey", surveyURL:"http://collections.europarchive.org/tna/20080205132101/http://news.bbc.co.uk/shared/survey/benchmarksurvey.stm", pollRate:pollRate, pollNewUser:pollRateNewUser, cookieHours:lifeOfSurvey, pageCountToLaunch:pageCountToLaunch, isPopUp:true, isEnabled:isEnabled});
		}
	}
}


newsi.surveyInit.prototype=newsi;
newsi.surveyInit.prototype._getSite=function(base){
	if(!base){base="co.uk";}
	if(this.getURL().search(base+"/sport")!=-1||this.getURL().search(base+"/sol")!=-1){return "sport";}
	return "news";
}
newsi.surveyInit.prototype._getEdition=function(){
	var tempCookieObj=new Array();
	var tempCookie=new newsi.Cookie({name:"BBCNewsAudience",content:tempCookieObj});
	tempCookie.load("BBCNewsAudience");
	var tempAudienceString=""+tempCookie.content;
	return tempAudienceString;
}

var gBBCNewsSurveyList=new Array();
//var gBBCNewsSurveyInit=new newsi.surveyInit('<!--#echo var="userLocation" -->');



