$(document).ready(function(){	
	
	var surveyPageUrl = window.location.hostname;
	if (surveyPageUrl.indexOf('uat.') == 0) {
		surveyPageUrl = surveyPageUrl.replace('uat.','');
	} else if (surveyPageUrl.indexOf('prod.') == 0) {
		surveyPageUrl = surveyPageUrl.replace('prod.','');
	}
						   
	$('a').click(function(e){
		var elementUrl = $(this).attr('href');
		var relation = $(this).attr('rel');
		if((elementUrl != undefined) && (relation != 'external') && (relation != 'popup') && (relation != 'newWindow') && (relation != 'quoteWindow')) {
			if ( (elementUrl.indexOf('rac.co.uk') != -1) || (elementUrl.indexOf('search.rac') != -1) || (elementUrl.indexOf(surveyPageUrl) != -1) || (elementUrl.indexOf('/') == 0) )
            {
				// unbind only the statisfaction survey
				$(window).unbind('unload', satisfaction_survey_setup);
			} 
		}				  
	});
	
	
	$('form').submit(function(e){
		var elementUrl = $(this).attr('action');
		
		if(elementUrl != undefined) {
			if ( (elementUrl == '') || (elementUrl == '.') || (elementUrl.indexOf('rac.co.uk') != -1) || (elementUrl.indexOf('search.rac') != -1) || (elementUrl.indexOf(surveyPageUrl) != -1) || (elementUrl.indexOf('/') == 0)  || (elementUrl.indexOf('#') == 0) )
            {
				// unbind only the statisfaction survey
				$(window).unbind('unload', satisfaction_survey_setup);
			} 
		}				  
	});
	
	
function satisfaction_survey_setup(){

	//***		USER SETTINGS		***//

	surveyName = "http://www.avalonasks.co.uk/rac_online_satisfaction_survey.htm";	//Can be a full URL or a reference name
	topLeftX = 0;
	topLeftY = 0;
delaySeconds = 0; //Number of seconds delay before opening popup. DOES NOT WORK IF CALLED FROM onUnLoad
	pickRandom = true;
	ratioAsked = 10;	//Ratio of people visiting against those being shown questionnaire
	optionList = '';
	//optionList += ',fullscreen=yes';	//Full-screen mode (IE 4 onwards only)
	//optionList += ',scrollbars=yes';	//Scrollbars
	//optionList += ',menubar=yes';	//Menu bar (File, Edit, View etc.)
	//optionList += ',location=yes';	//Location field to enter  URL
	//optionList += ',resizable=yes';	//Allow user to resize window.

	//***		END OF USER SETTINGS		***//

	if ((parseInt(Math.random() * ratioAsked)) == 1 || pickRandom == false)
	{
		var width = 960;
		var pageSize = "";
		var leftAndTop = "";
		if (parseFloat(navigator.appVersion) >= 4)
			width = screen.width;
		if (width < 700)
			pageSize = 'width=400,height=420';	//Screen resolution < 800x600
		else
			pageSize = 'width=900,height=800';	//Screen resolution 800x600 or above
		if (navigator.appName == "Netscape")
			leftAndTop = ",screenX=" + topLeftX + ",screenY=" + topLeftY;
		else
			leftAndTop = ",left=" + topLeftX + ",top=" + topLeftY;
		if (delaySeconds > 0)
			window.setTimeout("window.open('" + surveyName + "','','" + pageSize + leftAndTop + optionList + "');", delaySeconds * 1000);
		else
			window.open(surveyName,'',pageSize + leftAndTop + optionList);
		return true;
	}
}
	
// bind the satisfaction survey using a function so can remove only this occurance of the unload event later	
$(window).bind('unload', satisfaction_survey_setup);

function get_cookie(Name) {
	var search = Name + "="
	var returnvalue = "";
	if (document.cookie.length > 0) {
		offset = document.cookie.indexOf(search)
	    if (offset != -1) { // if cookie exists
			offset += search.length
			// set index of beginning of value
			end = document.cookie.indexOf(";", offset);
			// set index of end of cookie value
			if (end == -1)
				end = document.cookie.length;
		returnvalue=unescape(document.cookie.substring(offset, end))
		}
	}
	return returnvalue;
}

function loadpopup(){
	//***		USER SETTINGS		***//
	dropCookie = true;
	//***		END OF USER SETTINGS		***//

	if (get_cookie('popped')=='')
	{
		if (openpopup() && dropCookie) {
			var expireDate = new Date();
			expireDate.setFullYear(expireDate.getFullYear()+1);
			document.cookie="popped=yes;expires=" + expireDate.toGMTString();
		}
	}
}

});
