/// <summary>
/// Single javascript include that initiates an AJAX call to load a third party script through an
/// http proxy to avoid cross-site scripting protections.
/// </summary>

// Verify that the ComScore Beacon is only sent once.
if (typeof (window.comScoreFooterCalled) == "undefined") {
	window.comScoreFooterCalled = true;
	
	// Create the URL that will load the 3rd part script file.
	var beaconjsURL = "/CrossDomainContentProxy.ashx?content=" +
						(document.location.protocol == "https:" ? "https://sb" : "http://b") +
						".scorecardresearch.com/beacon.js";
						
	// Create the XmlHTTPRequest object, attempting to use the versions available from W3C standards
	// based browsers, then IE6, and finally IE5.
	var xmlHttp;
	try {
		xmlHttp = new XMLHttpRequest(); // W3C standard
	}
	catch (tryMSIE) {
		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); // IE6
		}
		catch (altMSIE) {
			try {
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); // IE5
			}
			catch (allFailed) {
				xmlHttp = false; // Browser is not AJAX capable.
			}
		}
	}

	// Only continue if the browser is AJAX capable. 
	if (xmlHttp) {
		// Set the ready state change event handler to continue the processing once the file has been loaded.
		xmlHttp.onreadystatechange = function() {
			// Only run when readyState equals 4, (request complete.)
			if (xmlHttp.readyState == 4) {
				// Ensure that the AJAX call was successful.
				if (xmlHttp.status == 200) {
					// Ensure the content returned is a string.
					if (typeof (xmlHttp.responseText) == "string") {
						// Evaluate the returned content to render the script into the DOM.
						eval(xmlHttp.responseText);
					}

					// Fire beacon if the script was loaded successfully.
					if (typeof (COMSCORE) != "undefined") {
						loadBeacon( COMSCORE );
					}
				}
			}
		}
		
		// Fire AJAX call.
		xmlHttp.open("GET", beaconjsURL, true);
		xmlHttp.send(null);
	}
}

/// <summary>
/// Composes then executes the function call that will trigger the ComScore beacon.
/// </summary>
function loadBeacon( COMSCORE ) {
	// Remove 'http://'
	viewingURL = document.URL.substring(document.URL.indexOf('//') + 2);
	
	// Encode query string separators
	viewingURL = escape(viewingURL);
	// Fire the beacon.
	COMSCORE.beacon({
		c1: 2,
		c2: 6035262,
		c3: document.location.host,
		c4: viewingURL,
		c5: "",
		c6: "",
		c15: ""
	});

}