// write the ptvasn cookie on this domain and the specified corresponding secure domain
function writeAffiliateCookie(domain) {

	// set the cookie expiry date to 30 days
	var expiryDays = 30;
	var expirySeconds = expiryDays * 24 * 60 * 60;
	var today = new Date();
	var expiry = new Date();
	expiry.setDate(today.getDate() + expiryDays);
	
	var ptvasn = getQueryStringParameterValue("ptvasn");
	if (ptvasn != undefined && ptvasn.length > 0) {
		SetCookie("ptvasn", ptvasn, expiry);
		// we may already be on the secure domain
		if (domain && location.protocol != "https:") {
			writeCookieOnSecureDomain("ptvasn", ptvasn, expirySeconds, domain);
		}
	}

}

function writeCookieOnSecureDomain(name, value, expiry, domain) {

	var cookieCrossDomainURL = 'https://' + domain + '/internalLogin/crossDomainCookie';

	if ($.browser.msie) {
		// send the the user to the secure domain, write the cookie, and then redirect back to the current page
		
		cookieCrossDomainURL += "?cookiesLength=1&cookie_1_domain=" + domain + "&cookie_1_name=" + name + "&cookie_1_value=" + value + "&cookie_1_expiry=" + expiry + "&redirPath=" + getHostAndPath();
		location.href = cookieCrossDomainURL;
	} 
	else {
		// Firefox has looser security so we can just use AJAX to connect to the secure domain to write the cookie
		cookieCrossDomainURL += "?cookiesLength=1&cookie_1_domain=" + domain + "&cookie_1_name=" + name + "&cookie_1_value=" + value + "&cookie_1_expiry=" + expiry;

		$.ajax( {
			type : "GET",
			url : cookieCrossDomainURL,
			async : false,
			dataType: "script",
			cache : false
		});
	}

}

function getHostAndPath() {

	return location.protocol + "//" + location.host + location.pathname;

}
