//**************************************************************************
//* File:       buystreamTagRadio.js
//* Purpose:    Creates data tags for CBC radio Canada culture section
//* Version:    1.0
//* Updated:    Feb 13, 2003
//**************************************************************************
//* Modifications (date : version : user : description)
//--------------------------------------------------------------------------
//* 13/02/03 : 1.0 : David Reynolds : File created

//**************************************************************************
//* Main Configuration Variables
//**************************************************************************
var bmServer = "asp11.buystream.com"; // buystream server domain name
var bmPath = "/cbc/bin"; // virtual IIS directory
var bmClientID = "cbclive"; // client ID

var bmturl = parent.frames[1].location.toString();
bmturl = bmturl.toLowerCase();

//************************************************************************************
//* Function    : bmEncode(sInput)
//* Version     : 2.1
//* Purpose     : Encodes a string to conform to Buystream's requirements
//* Parameters  : String to convert, boolean to determine if periods should be excluded
//* Returns     : Converted string
//************************************************************************************
function bmEncode(sInput,incPeriods)
{
	sOutput = escape(sInput);
        sOutput = sOutput.replace(/\*/g, "%2A");
        sOutput = sOutput.replace(/\+/g, "%2B");
        sOutput = sOutput.replace(/\-/g, "%2D");
        sOutput = sOutput.replace(/\_/g, "%5F");
        sOutput = sOutput.replace(/\?/g, "%3F");
        sOutput = sOutput.replace(/\ /g, "%20");
		if (incPeriods!=false) {
	        sOutput = sOutput.replace(/\./g, "%2E");
		}
		// this one must be last
    	sOutput = sOutput.replace(/\%/g, "$");
        return sOutput;
} // End of Function

//************************************************************************************
//* Functions   : QueryString(key),QueryString_Parse()
//* Version     : 2.0
//* Purpose     : Parses query srting into array that can be used to quickly
//*               retrieve values via the key. (case insensitive)
//* Parameters  : QueryString key
//* Returns     : QueryString value, or null if no value is present
//************************************************************************************
function QueryString(key)
{
	var value = null;
	for (var i=0;i<QueryString.keys.length;i++)
	{
		if (QueryString.keys[i]==key)
		{
			value = QueryString.values[i];
			break;
		}
	}
	return value;
} // End of Function

function QueryString_Parse()
{
	var query = parent.frames[1].location.search.substring(1);
	var pairs = query.split("&");
	
	for (var i=0;i<pairs.length;i++)
	{
		var pos = pairs[i].indexOf('=');
		if (pos >= 0)
		{
			var argname = pairs[i].substring(0,pos);
			var value = pairs[i].substring(pos+1);
			QueryString.keys[QueryString.keys.length] = argname.toLowerCase();
			QueryString.values[QueryString.values.length] = value.toLowerCase();		
		}
	}
} // End of Function

// Initialise arrays used by the above querystring functions
QueryString.keys = new Array();
QueryString.values = new Array();
QueryString_Parse();

//**************************************************************************
//* Generic Page Tracking
//**************************************************************************

// if data tag variables are set from the page, then encode the variables
// else initialize the variables
if (typeof(bmUniqueID)=="string") {
	bmUniqueID = bmEncode(bmUniqueID);
	bmDN = bmEncode(bmDN);
	bmHP = bmEncode(bmHP,false);
} else {
	// Initialize variables
	var bmUniqueID = ""; // unique ID
	var bmHP = ""; // hierarchy path
	var bmDN = ""; // display name
	
	// every thing is going under sports in the sports section
	bmHP = "radio$20canada.culture";
	
	// cgi var path_info, minus the first "/" character
	// e.g. http://www.example.com/test/test.html => test/index.html
	var bmPathInfo = parent.frames[1].location.pathname.substr(1).toLowerCase();
	
	// array of bmPathInfo items delimited by "/" character
	var bmCategoryA = bmPathInfo.split("/");
	
	// name of the file (with default pages, such as index.html, this may be blank)
	var bmFile = bmCategoryA[bmCategoryA.length-1];
	
	if( bmFile == "" )
	{
		bmDN = "Radio$20Canada$20Culture$20Main$20Page";
		bmUniqueID = "Radio$20Canada$20Culture$20Main$20Page";
	}
	else
	{
		bmDN = "";
		if( parent.frames[1].location.search.substring(1) == null )
		{
			bmUniqueID = bmEncode(bmPathInfo);
		}
		else
		{
			bmUniqueID = bmEncode(QueryString("vidEntite"));
			if( QueryString("section") != null )
			{
				bmHP = "radio$20canada.culture." + bmEncode(QueryString("section"));
			}
		}
	}
	
}
// ensure that bmUniqueID is fully encoded
bmEncode(bmUniqueID);


//make sure values are there
if( bmDN == "" )
{
	bmDN = bmUniqueID;
}

// set bmIval (This is the value of the hidden input item for bm_identityForm)
var bmIval = "uniqueid=" + bmUniqueID + "&dn=" + bmDN + "&hp=" + bmHP;

//**************************************************************************
//* Main Data Tag Generation
//* This creates the bm_identityForm with one hidden input item
//* Screens out any undefined or blank errors via the if statement
//**************************************************************************
if ( (bmIval.search("undefined")+bmIval.search("=&"))<0 && bmHP != "" && bmUniqueID != "" ) {
	document.write("<form name=bm_identityForm>");
	document.write("<input type=hidden name=\"na\" value=\"" + bmIval + "\">");
	document.write("</form>");
	
	// This variable contains the whole jss request URL
	var bmUrlStr = location.protocol+'/'+'/'+bmServer+bmPath+'/jss.exe?pageid=17&clientid='+bmClientID;
	
	// the script tags are broken up to prevent nested script block errors thrown by script interpreters (e.g. PHP,ASP)
	document.write('<scr'+'ipt language="JavaScript" src="' + bmUrlStr + '"><\/sc'+'ript>');
}

