/* Javascript for Customer Satisfaction Survey Overlay */


/* Browser Sniff */
var ua = navigator.userAgent.toLowerCase();
ie6 = (ua.indexOf("msie 6") != -1)?true:false;


/********************************************************************************

Name:     		fnSurveyInit()
Description: 	Initialise the survey - run all the things that need to be done 
				at the start
 
*********************************************************************************/

function  fnSurveyInit() {
	if (sSurveyShowPopup == "true") {		
		fnSurveyOn(); // turn on the survey	
		if(ie6){
			s=document.getElementById('surveyOn');
			s.style.top='147px';
			s.style.left='267px';			
			fnOpacity('surveyOn', 0, 100, 0);		// fade the survey in.		
		}
		else {
			// just before fading the survey in, put it back on screen. Still with opacity 0.
			setTimeout("s=document.getElementById('surveyOn');s.style.top='147px';s.style.left='267px';",1000); 		
			setTimeout("fnOpacity('surveyOn', 0, 100, 1000);",2000); // fade the survey in.		
		}
	}
}


/********************************************************************************

Name:     		fnSurveyOn()
Description: 	Changes the ID on the wrapping element from surveyOff to surveyOn
				This applies the styles in triumphJS.css and shows the overlay
 
*********************************************************************************/

function fnSurveyOn() {
	if(targ = document.getElementById("surveyOff")) {
		targ.id = "surveyOn";
	}
}




/********************************************************************************

Name:     		fnSurveyOff()
Description: 	Changes the ID on the wrapping element from surveyOn to surveyOff
				This applies the styles in triumphContent.css and hides the 
				overlay. Also pass the switch case to fnSetSurveyCookie() and 
				turn survey off or suspend it with cookies

@param cookie	The switch case to fnSetSurveyCookie() - 'off' or 'later'
				
*********************************************************************************/	
	
function fnSurveyOff(surveyStatus, cookieName) {
	fnOpacity('surveyOn', 100, 0, 500); // fade it out
	
	if(surveyStatus) {						
		fnSetSurveyCookie(surveyStatus, cookieName);		// set the cookie to off or later
	}
		
	// change id to surveyOff, but wait longer than fade time above (param 4 of fnOpacity).
	setTimeout('targ = document.getElementById("surveyOn"); targ.id = "surveyOff";',1000); 
	// for ie's sake, set survey off screen by inline style
	setTimeout("s=document.getElementById('surveyOff');s.style.top='5000px';s.style.left='-5000px';",1001); 
	
}		




/********************************************************************************

Name:	     		fnOpacity()
Description: 		Set the opacity of the element with id, over set time.
					Fade's in or out depending on values passed into function				 

@param id			The Id of the element to be faded
@param opamyCookie	Start opacity (0-100)
@param opacEnd		End opacity	(0-100)
@param millisec		Time it will take to fade in/out in ms		

*********************************************************************************/	

function fnOpacity(id, opamyCookie, opacEnd, millisec) {
	
	if(!id) return;
	
	document.getElementById("surveyWrapper").style.display = "block";
	
    //speed for each frame
    var speed = Math.round(millisec / 100);
    var timer = 0;

    //determine the direction of the fade. if start and end are the same, nothing happens
    if(opamyCookie > opacEnd) {
        for(i = opamyCookie; i >= opacEnd; i--) {
            setTimeout("fnChangeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    } else if(opamyCookie < opacEnd) {
        for(i = opamyCookie; i <= opacEnd; i++)
            {
            setTimeout("fnChangeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    }
}

/********************************************************************************

Name:	     		fnChangeOpac()
Description: 		Set the opacity of the element with id, over set time.
					Fade's in or out depending on values passed into function				 

@param opacity		Final opacity value (0-100)
@param id			The Id of the element to have the opacity applied to 

*********************************************************************************/		

function fnChangeOpac(opacity, id) {
    var myId = document.getElementById(id).style;
    myId.opacity = (opacity / 100);
    myId.MozOpacity = (opacity / 100);
    myId.KhtmlOpacity = (opacity / 100);
    myId.filter = "alpha(opacity=" + opacity + ")";
} 


/********************************************************************************

Name:	     		fnGetCookie()
Description: 		Get the cookie named as @param cName	 

@param cName		Cookie name
@param value		Value to be set
@param expiredays	Number of days until expiration of cookie

*********************************************************************************/	

function fnSetCookie(cName,value,expiredays) {
	var exdate=new Date()
	exdate.setDate(exdate.getDate()+expiredays)
	document.cookie=cName+ "=" +unescape(value)+";path=/;"+((expiredays==null) ? "" : ";expires="+exdate.toGMTString())
}

	
/********************************************************************************

Name:	     		fnGetCookie()
Description: 		Get the cookie named as @param cName	 

@param cName		Cookie name

*********************************************************************************/	

function fnGetCookie(cName) {
	if (document.cookie.length>0) {
		myCookie=document.cookie.indexOf(cName + "=")
		if (myCookie!=-1)	{
			myCookie=myCookie + cName.length+1
			cEnd=document.cookie.indexOf(";",myCookie)
			if (cEnd==-1) cEnd=document.cookie.length
				return unescape(document.cookie.substring(myCookie,cEnd))
			}
		}
	return ""
}	


/********************************************************************************

Name:	     		fnSetSurveyCookie()
Description: 		Amend the value of the cookie named as @param cName	
					according to the reason 'myCase'

@param myCase		The switch case - the reason why the cookie is being changed

*********************************************************************************/			

function fnSetSurveyCookie(myCase, cookieName) {		
	switch(myCase) {			
		case "later":
		  fnSetCookie(cookieName, "showPopup=true&pagesBrowsed=0",90) 	// reset the cookie page counter to 1
		  break	  
		  
		case "off":
		  fnSetCookie(cookieName, "showPopup=false&"+fnGetCookie(cookieName).split('&')[1],90)						// turn off the survey, no expiry date
		  break
	}
}		
