<!--
/*
 * ospcm.js
 *
 * Online Sports v4.0, 10/14/2005
 * COPYRIGHT 1995-2005 GAME ON SPORTS, INC. 
 * ALL RIGHTS RESERVED. U.S.PATENT PENDING
 *
 * The following functions aid in the creation of Coremetrics data tags on Online Sports pages.
 *
 */

var ospUseCMTags = 1 ;
var ospUseCMProduction = 1 ;

function checkUseCMProduction(){

	if ( ospUseCMProduction )
	{
		cmSetProduction() ;
	}
}

//
// Turn on Coremetrics tags if flag is set or if test cookie has been set
//
function checkUseCMTags(){

	var val = 0;
	if ( ospUseCMTags || checkUseCMCookieSet() )
	{
		val = 1 ;
	}
	
	return val ;
}

//
// To activate this test cookie, load page http://onlinesports.com/test/cm/turnoncmtags.html
//
function turnOnCMTags() {
	var today = new Date();
	var expire = new Date();
	var domain = "domain=.onlinesports.com" ;
	var path = "path=/" ;

 
	expire.setTime(today.getTime() + 3600000*24) ;
	document.cookie = "OSPUseCMTags=1;expires=" + expire.toGMTString() + ";" + domain + ";" + path ;
}

//
// To deactivate this test cookie, load page http://onlinesports.com/test/cm/turnoffcmtags.html
//
function turnOffCMTags() {
	var today = new Date();
	var expire = new Date();
	var domain = "domain=.onlinesports.com" ;
	var path = "path=/" ;
 
	expire.setTime(today.getTime() - 3600000*24);
	document.cookie = "OSPUseCMTags=1;expires=" + expire.toGMTString() + ";" + domain + ";" + path ;
}
	

//
// If browser IP address is on the list of valid test addresses then return 1, else return 0
//
function checkUseCMCookieSet(){

	var x = readCookie('OSPUseCMTags') ;

	if (x)
	{
		val = 1 ;
	}
	else {
		val = 0 ;
	}
	
	return val ;
}

function readCookie(name)
{
        var nameEQ = name + "=";
        var ca = document.cookie.split(';');
        for(var i=0;i < ca.length;i++)
        {
                var c = ca[i];
                while (c.charAt(0)==' ') c = c.substring(1,c.length);
                if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
        }
        return null;
}

function ospTagPageCM(){
	
	if ( ! checkUseCMTags() ) {
		return ;
	}
	
	checkUseCMProduction() ;
			
	var page = document.location.href ;
	var pathname = document.location.pathname ;

	if ( page.indexOf("/pages/I,top") >= 0 )
	{
		writeItemListPageCMTags(page) ;
	}
	else if ( page.indexOf("/test/test_akamai.html") >= 0 )
	{
		writeTestAkamaiCookieCMTags(page) ;
	}
	else if ( page.indexOf("/cgi-bin/node.cgi") >= 0 )
	{
		writeNodeItemListPageCMTags(page) ;
	}
	else if ( page.indexOf("/pages/top") >= 0 )
	{
		writeCategoryPageCMTags(page) ;
	}
	else if ( page.indexOf("/pages/I,") >= 0 )
	{
		writeProductPageCMTags(page) ;
	}
	else if ( page.indexOf("onlinesports.com/testsearch") >= 0 )
	{
		writeSearchPageCMTags(page) ;
	}
	else if ( document.title == "Oops" )
	{
		writeErrorPageCMTags(page) ;
	}
	else if ( pathname == "/" || pathname.indexOf("/index.html") >= 0 )
	{
		writeHomePageCMTags(page) ;
	}
	else if ( 	pathname.indexOf("/cgi-bin/review-order") 	>= 0 ||
				pathname.indexOf("/cgi-bin/continue-order") >= 0 ||
				pathname.indexOf("/cgi-bin/place-order") 	>= 0 ||
				pathname.indexOf("/cgi-bin/cart-print") 	>= 0 ||
				pathname.indexOf("/cgi-bin/cart.cgi") >= 0 ||
				pathname.indexOf("/cgi-bin/co-address.cgi") >= 0 ||
				pathname.indexOf("/cgi-bin/co-payment.cgi") >= 0 ||
				pathname.indexOf("/cgi-bin/co-review.cgi") >= 0 ||
				pathname.indexOf("/cgi-bin/co-confirm.cgi") >= 0
	)
	{
		writeCartCMTags(pathname) ;
	}
	else
	{
		writeDefaultPageCMTags(page) ;
	}
}

function ospSearchPageCMTags(numResults){
	
	if ( ! checkUseCMTags() ) {
		return ;
	}
	
	checkUseCMProduction() ;
	
	var page = document.location.href ;
	
//	alert("numResults: " + numResults) ;

	writeSearchPageCMTags(page, numResults) ;
}
	
function getCrossSell()
{
	var page = document.location.href ;
	var params = "&" + page.split("?")[1] ;
	var cat = ospcmExtractParameter(params, "cmCategoryID");
	if (cat) {
		var category = "&cmCategoryID="+ cat;
	}else {
		var category =  "";
	}
  return category;
}	

function getCategory()
{
	var page = document.location.href ;
	var params = "&" + page.split("?")[1] ;
	var cat = ospcmExtractParameter(params, "cmCategoryID");
	if (cat) {
		var category =  cat;
	}else {
		var category =  "products";
	}
  return category;
}	

function getAkamaiCacheCategoryOld(category)
{
	var akcategory = readCookie('aktest2') ;
	
	if ( akcategory )
	{
		akcategory = akcategory + ":" ;
	}
	else
	{
		akcategory = "" ;
	}
	
	var category = akcategory + category ;

	return category ;
}

function getAkamaiCacheCategory(category)
{
//	alert( "category: " + category) ;

	return category ;
}

function writeTestAkamaiCookieCMTags(page)
{
	category = "products" ;
	category = getAkamaiCacheCategory(category) ;
	
	cmCreatePageviewTag("Test Akamai: " + category, category, null) ;
}

	
function writeProductPageCMTags(page)
{

	item_beg = page.indexOf("/pages/I,") + "/pages/I,".length ;
	item_end = page.indexOf(".html" ) ;
	
	item_num = page.substring(item_beg, item_end) ;
	
	ref = document.referrer ;
	ref_category = "products" ;
	
	if ( ref.indexOf("/pages/I,top") >= 0 )
	{
		ref_beg = ref.indexOf("/pages/I,top") + "/pages/I,".length ;
		ref_end = ref.indexOf(".html") ;

	}
	else if ( ref.indexOf("/pages/top") >= 0 )
	{
		ref_beg = ref.indexOf("/pages/top") + "/pages/".length ;
		ref_end = ref.indexOf(".html") ;
	}
	else if ( ref.indexOf("search.onlinesports.com/") >= 0 )
	{
		ref_beg = 0 ;
		ref_end = 0 ;
		ref_category = "search" ;
	}
	else if ( ref.indexOf("onlinesports.com/index.html") >= 0 )
	{
		ref_beg = 0 ;
		ref_end = 0 ;
		ref_category = "home" ;
	}
	else
	{
		ref_beg = 0 ;
		ref_end = 0 ;
	}
	
	if ( ref_beg && ref_end )
	{
		ref_category = ref.substring(ref_beg, ref_end).split(",").join(":") ;
	}

	ref_category = getAkamaiCacheCategory(ref_category) ;		
	cmCreateProductviewTag(item_num, document.title, ref_category) ;
}

function writeItemListPageCMTags(page){
	
	item_beg = page.indexOf("/pages/I,top") + "/pages/I,".length ;
	item_end = page.indexOf(".html" ) ;
	cat_beg  = page.indexOf("/pages/I,top") + "/pages/I,".length ;
	cat_end  = page.indexOf(".html") ;

	page_name = page.substring(item_beg, item_end) ;
//	page_name = page_name.split(",").join(":") ;
	category  = page.substring(cat_beg, cat_end).split(",").join(":") ;
	
	category = getAkamaiCacheCategory(category) ;		
	cmCreatePageviewTag("Item List: " + category, category, null) ;
}

function writeNodeItemListPageCMTags(page){
	
	var category = 	ospcmExtractParameter( page, "id" );

	category  = category.split(",").join(":") ;

	category = getAkamaiCacheCategory(category) ;			
	cmCreatePageviewTag("Node Item List: " + category, category, null) ;
}


function writeCategoryPageCMTags(page){

	item_beg = page.indexOf("/pages/top") ;
	item_end = page.indexOf(".html" ) + ".html".length ;
	cat_beg  = page.indexOf("/pages/top") + "/pages/".length ;
	cat_end  = page.indexOf(".html") ;

	page_name = page.substring(item_beg, item_end) ;
	category  = page.substring(cat_beg, cat_end).split(",").join(":") ;

	category = getAkamaiCacheCategory(category) ;		
	cmCreatePageviewTag("Category: " + category, category, null) ;
}

function writeHomePageCMTags(page){

	var category = getAkamaiCacheCategory("home") ;		
	cmCreateTechPropsTag("HOME PAGE", category) ;
}

//
// Eventually add numResults as second parameter
//
function writeSearchPageCMTags(page, numResults){
	var params = "&" + page.split("?")[1] ;
	

	var u1index = ospcmIndexOfParameter( params, "u1") ;
	
	var u1attr 	= ospcmExtractParameter( params, "u1" ) ;
//	alert( "u1attr: " + u1attr + "  u1attr.length = " + u1attr.length + "u1 index= " + u1index) ;

	if ( u1attr.length == 0 )
	{
		u1attr = "query" ;
	}
		
	var searchterm = ospcmExtractParameter( params, u1attr ) ;	
	var wholesearch = searchterm;
	for(i = 2; i < 11; i++) {
		var att = ospcmExtractParameter( params, "u" + i ) ;
		
		if (att.length != 0) {
			var val = ospcmExtractParameter( params, att );
			
			 wholesearch = wholesearch + " > " + val;
		}
	
	}
	var success_string ;
	
	if ( numResults > 0 )
	{
		success_string = "Successful" ;
	}
	else
	{
		success_string = "Unsuccessful" ;
	}
	
//	alert("Search Results: " + success_string + ", search " + wholesearch + " " + numResults);
	var category = getAkamaiCacheCategory("search") ;		
	cmCreatePageviewTag("Search Results: " + success_string, category, wholesearch, numResults) ;
}


function ospcmExtractSearchTerm(params, termNum)
{	
	var termAttr 	= ospcmExtractParameter( params, "u" + termNum ) ;
//	alert( "termNum: " + termNum + "  termAttr: " + termAttr) ;

	var returnValue = "" ;
	if ( termAttr.length > 0 )
	{
		returnValue	= ospcmExtractParameter( params, termAttr ) ;
	}

	return returnValue ;
}

function writeMessagePageCMTags(message){
	
	message = escape(message) ;
//	alert("Writing message: " + message) ;
	
	var category = getAkamaiCacheCategory("error") ;		
	cmCreatePageviewTag("Message: " + message, category, null) ;
}

function writeErrorPageCMTags(page){
	var page_name = document.location.pathname ;
	
	var category = getAkamaiCacheCategory("error") ;		
	cmCreateErrorTag("404 Error", category) ;
}

function writeCartCMTags(pathname){

	var page_name = "Unknown Cart Page" ;
	
	if ( pathname.indexOf("/cgi-bin/review-order") 	>= 0 ) {
		page_name = "Checkout Step 1 - review-order" ;
	}
	else if ( pathname.indexOf("/cgi-bin/continue-order") 	>= 0 ) {
		page_name = "Checkout Step 2 - continue-order" ;
	}
	else if ( pathname.indexOf("/cgi-bin/place-order") 	>= 0 ) {
		page_name = "Checkout Step 3 - place-order" ;
	}
	else if ( pathname.indexOf("/cgi-bin/cart-print") 	>= 0 ) {
		page_name = "Cart Print" ;
	}
	else if ( pathname.indexOf("/cgi-bin/cart-add-file") 	>= 0 ) {
		page_name = "Cart Add" ;
	}
	else if ( pathname.indexOf("/cgi-bin/cart.cgi") 	>= 0 ) {
		page_name = "/cgi-bin/cart.cgi" ;
//		page_name = "Shopping Cart" ;
	}
	else if ( pathname.indexOf("/cgi-bin/co-address.cgi") 	>= 0 ) {
		page_name = "/cgi-bin/co-address.cgi" ;
//		page_name = "Checkout Step 1 - co-address.cgi" ;
	}
	else if ( pathname.indexOf("/cgi-bin/co-payment.cgi") 	>= 0 ) {
		page_name = "/cgi-bin/co-payment.cgi" ;
//		page_name = "Checkout Step 2 - co-payment.cgi" ;
	}
	else if ( pathname.indexOf("/cgi-bin/co-review.cgi") 	>= 0 ) {
		page_name = "/cgi-bin/co-review.cgi" ;
//		page_name = "Checkout Step 3 - co-review.cgi" ;
	}
	else if ( pathname.indexOf("/cgi-bin/co-confirm.cgi") 	>= 0 ) {
		page_name = "/cgi-bin/co-confirm.cgi" ;
//		page_name = "Checkout Step 4 - co-confirm.cgi" ;
	}
	else{
		page_name = "Unknown Cart Page: " + pathname ;
	}

	var category = getAkamaiCacheCategory("cart") ;	
	cmCreatePageviewTag(page_name, category, null) ;
}

function writeDefaultPageCMTags(page){
	var page_name = document.location.pathname ;
	
	var category = getAkamaiCacheCategory("static") ;	
	cmCreatePageviewTag(page_name, category, null) ;
}

function ospcmCreateShopAction5Tag(sku, description, quantity, price, category) {

	if ( ! checkUseCMTags() ) {
		return ;
	}
	
	checkUseCMProduction() ;
			
//	alert("Calling cmCreateShopAction5Tag(\"" +sku + "\",\"" + description + "\",\"" + quantity + "\",\"" + price + "\",\"" + category+ "\")" ) ;
	cmCreateShopAction5Tag(sku, description, quantity, price, category) ;
}

function ospcmDisplayShop5s() {

	if ( ! checkUseCMTags() ) {
		return ;
	}
	
	checkUseCMProduction() ;
			
	cmDisplayShop5s() ;
}

function ospcmCreateShopAction9Tag(sku, description, quantity, price, customer, order, subtotal, category) {

	if ( ! checkUseCMTags() ) {
		return ;
	}
	
	checkUseCMProduction() ;
			
	cmCreateShopAction9Tag(sku, description, quantity, price, customer, order, subtotal, category) ;
}

function ospcmDisplayShop9s() {

	if ( ! checkUseCMTags() ) {
		return ;
	}
	
	checkUseCMProduction() ;
			
//	alert("Inside ospcmDisplayShop9s") ;
	cmDisplayShop9s() ;
}

function ospcmCreateOrderTag(order, subtotal, shipping, customer, city, state, zip) {

	if ( ! checkUseCMTags() ) {
		return ;
	}
	
	checkUseCMProduction() ;
			
	cmCreateOrderTag(order, subtotal, shipping, customer, city, state, zip) ;
}

function ospcmErrorTag() {

	if ( ! checkUseCMTags() ) {
		return ;
	}
	
	checkUseCMProduction() ;
			
	cmCreateOrderTag(order, subtotal, shipping, customer, city, state, zip) ;
}


/*
 * Checks to see if a parameter exists in the request search string.
 * Returns 0 if not found or the index of the first character of the
 * name in the name-value pair if found.
 */
function ospcmIndexOfParameter (params, parameter) {
        return params.indexOf("&" + parameter) + 1 ;
}


/*
 * Extracts the value of a name-value pair in the request search string.
 */
function ospcmExtractParameter (params, parameter) {
	var par_beg = ospcmIndexOfParameter(params, parameter) ;
	
	
	
    if ( par_beg == 0) {
        return "";
    }

	var par_end = params.indexOf("&", par_beg) ;

	if (par_end == -1) {
		par_end = params.length;
	}
	
	var par_middle = params.indexOf("=", par_beg);
	return params.substring(par_middle + 1, par_end);
	
}

