function Srvy(wt, postshop, dprt, dest, pinf)
{
	var p_top = (screen.height-250)/2;
	var p_left = (screen.width-250)/2;
	var url = '/pub/agent.dll?qscr=srvy&fshp=' + postshop + '&spwt=' + wt;
	if (dprt != null) {url = url + '&dprt=' + escape(dprt);}
	if (dest != null) {url = url + '&dest=' + escape(dest);}
	if (pinf != null) {url = url + '&pinf=' + escape(pinf);}
	var srvy_pop = window.open(url,'srvy','top='+p_top+',left='+p_left+',location=no,toolbar=no,directories=no,menubar=no,resizable=no,scrollbars=no,status=no,width=332,height=362');
	if (srvy_pop)
	{
		if (postshop)
		{
			srvy_pop.blur();
			window.focus();
		}
		else
		{
			srvy_pop.focus();
		}
	}
}
//----------------------------------------------------------------------------------------------------//
function QualifiedForSurvey(wt, postshop, window, rate, dprt, dest, pinf)
{
	/*
	parameters:
		wt			- wizard type mask
		postshop	- if true, then check post-shopping mask; if false, check post-purchase mask;
		window		- allowed frequency between surveys in days
		rate		- the sampling rate
		dprt		- comma delimited list of origins
		dest		- comma delimited list of destinations
		pinf		- product information - airline code, hotel id, car vendor code, etc...
	*/
	if (IsArranging(GCV('user')))	//check that we are not in arranger mode; return false if we are arranging
	{
		return false;
	}

	if (ShownSurveyBefore(window))	//check lsrvy to see if we have been shown a survey within window
	{
		return false;
	}

	var srvysVal = GCV('srvys');	//check srvys to see if we have already been checked in the current session for a given LOB
	var a_srvysVal = srvysVal.split(",");
	if (a_srvysVal.length == 3)
	{
		var shoppingMask = a_srvysVal[1];
		var purchaseMask = a_srvysVal[2];
	}
	else
	{	//cookie does not exist of is not valid
		var shoppingMask = 0;
		var purchaseMask = 0;
	}
	
	var mask = 1 << wt
	var bSampledBefore = postshop ? ((mask & shoppingMask) ? true : false) : ((mask & purchaseMask) ? true : false);
	if (bSampledBefore)
	{
		return false;
	}
	
	//update srvys to indicate that this LOB has been sampled
	postshop ? (shoppingMask |= mask) : (purchaseMask |= mask);
	setCookie('srvys','v.1,' + shoppingMask + ',' + purchaseMask,'','/','','');

	var rndMax = 1;
	var rndNum = Math.random();
	var sampleRate = ((rndMax/10000.00) * rate);
	if (rndNum < sampleRate)
	{
		Srvy(wt, postshop, dprt, dest, pinf);
	}
}
//----------------------------------------------------------------------------------------------------//
function y2k(num) {return (num<1000) ? num+1900 : num;}
//----------------------------------------------------------------------------------------------------//
function setCookie(name, value, expires, path, domain, secure){	document.cookie = name + '=' + escape(value) + ((expires)?(';expires=' + expires):'') + ((path)?';path=' + path:'') + ((domain)?';domain=' + domain:'') + ((secure && (secure == true))?'; secure':''); // Set the cookie, adding any parameters that were specified.}
//----------------------------------------------------------------------------------------------------//
function DDiff(d1,d2)
{
    var diff = Date.UTC(y2k(d1.getYear()),d1.getMonth(),d1.getDate(),0,0,0) -
			   Date.UTC(y2k(d2.getYear()),d2.getMonth(),d2.getDate(),0,0,0);
    return diff/1000/60/60/24;
}
//----------------------------------------------------------------------------------------------------//
function ShownSurveyBefore(window)
{	
	curDate = new Date();
	var lscStr = GCV('lsrvy');
	if (lscStr != "")
	{
		var dateStr = lscStr;
		var start = lscStr.indexOf(",");
		if (start != -1) {start += 1; dateStr = lscStr.substring(start, lscStr.length);}
		var lscDate = new Date(dateStr);
		return (DDiff(curDate,lscDate) < window);
	}
	else
	{
		return false;
	}
}
//----------------------------------------------------------------------------------------------------//
function IsArranging(val)
{
	if (val == '')
		return false;
	
	var a_val = val.split(',');
	if (a_val.length >= 2)
	{
		return ((a_val[1] >= 1) ? true : false);
	}
	else
	{
		return false;
	}
}
//----------------------------------------------------------------------------------------------------//
function GCV(cookie)
{
	var firstChar, lastChar, fullCookie;
	var cv = '';
	
	fullCookie = document.cookie;
	if (fullCookie == '')
		return returnVal;
		
	firstChar = fullCookie.indexOf(cookie) // find the start of 'srvys'
	var equalChar = firstChar + cookie.length;
	if ((firstChar != -1) && (fullCookie.charAt(equalChar) == '='))
	{	//found the cookie
		firstChar += cookie.length + 1;	// skip 'cookie' and '='
		lastChar = fullCookie.indexOf(';', firstChar); // Find the end of the value string (i.e. the next ';')
		if (lastChar == -1)			lastChar = fullCookie.length;
		cv = unescape(fullCookie.substring(firstChar, lastChar));
	}

	return cv;
}