﻿var THANK_YOU_STEP = "ThankYou";
var LOGGED_IN_STEP = "LoggedIn";
var ORDER_PAGE_STEP = "OrderPage";
var BUSINESS_THANK_YOU_STEP = "BusinessThankYou";
var FIRST_PAGE_STEP = "FirstPage";
var REGISTERED_STEP = "Registered";

var AFFILIATE_ID_MICROSOFT = "MSN";
var AFFILIATE_ID_HYVES = "HYV";
var AFFILIATE_ID_ADLANTIC = "ADL";
var AFFILIATE_ID_GOOGLE_ADWORDS = "ADW";
var AFFILIATE_ID_ILSE_MEDIA = "IMX";
var AFFILIATE_ID_DAISYCON = "DSC";
var AFFILIATE_ID_M4N = "M4N";
var AFFILIATE_ID_ZANOX = "ZNX";
var AFFILIATE_ID_OMG = "OMG";
var AFFILIATE_ID_YOUTUBE = "YOU";

var AFFILIATE_COOKIE_NAME = "HNKN_AFFILIATE";

var affiliates = new Array(
    new Array("MSN", 31),
    new Array("HYV", 31),
    new Array("ADL", 31),
    new Array("ADW", 31),
    new Array("IMX", 31),
    new Array("DSC", 31),
    new Array("M4N", 31),
    new Array("ZNX", 31),
    new Array("OMG", 31),
    new Array("YOU", 31));
    
function isAnAffiliate(name)
{
    for (i = 0; i < affiliates.length; i++)
    {
        if (affiliates[i][0] == name)
            return true;
    }
    return false;    
}

function getAffiliateValidityDays(name)
{
    for (i = 0; i < affiliates.length; i++)
    {
        if (affiliates[i][0] == name)
            return affiliates[i][1];
    }
    return -1;
}

function addPixel(src)
{
    var iframe = document.createElement('iframe');        
    iframe.style.width = '0px';
    iframe.style.height = '0px';
    iframe.style.border = '0px';
    //document.body.appendChild(iframe);
    document.getElementById('reftracking').appendChild(iframe);
    iframe.src = src;
}

function addMetrixData(src)
{
    var iframe = document.createElement('iframe');        
    iframe.style.width = '0px';
    iframe.style.height = '0px';
    iframe.style.border = '0px';
    //document.body.appendChild(iframe);
    document.getElementById('metrixdata').appendChild(iframe);
    iframe.src = src;
}

function loadPixels(currentStep, customerID)
{
    var affiliateName = readCookie(AFFILIATE_COOKIE_NAME);
//    if (affiliateName == null || affiliateName.length == 0)
//    {
//        //the user hasn't been redirected to the website through an affiliate
//        return;
//    }
//    if (!isAnAffiliate(affiliateName))
//    {
//        //unknown affiliate
//        return;
//    }  
    
    if (currentStep == FIRST_PAGE_STEP)
    {
        loadPixelsForFirstPageStep(affiliateName, customerID);
    }
    else if (currentStep == THANK_YOU_STEP)
    {
        loadPixelsForThankYouStep(affiliateName, customerID);
    }
    else if (currentStep == REGISTERED_STEP)
    {
        loadPixelsForRegisteredStep(affiliateName, customerID);
    }
    else if (currentStep == ORDER_PAGE_STEP)
    {
        loadPixelsForOrderPageStep(affiliateName, customerID);
    }
    else if (currentStep == BUSINESS_THANK_YOU_STEP)
    {
        loadPixelsforBusinessThankYouStep(affiliateName, customerID);
    }
    else
    {
    //TODO send any errors?
    }
}

function readCookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );


		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}

function loadMetrixData()
{
    //metrix data is loaded on first step
    addMetrixData('../pixels/metrixdata.htm');
}

function loadPixelsForFirstPageStep(affiliateName, customerID)
{
    //ADL pixel loaded always - no matter which the current affiliate is
    addPixel('../pixels/adl_firstpage.htm');
    
    if (affiliateName == AFFILIATE_ID_DAISYCON)
    {
        //addPixel('../pixels/dsc_firstpage.htm');
    }
    else if (affiliateName == AFFILIATE_ID_M4N)
    {
        addPixel('../pixels/m4n_firstpage.htm');
    }
    else if (affiliateName == AFFILIATE_ID_ZANOX)
    {
        addPixel('../pixels/znx_firstpage.htm');        
    }
    else if (affiliateName == AFFILIATE_ID_OMG)
    {
        addPixel('../pixels/omg_firstpage.htm');        
    }    
}

function loadPixelsForThankYouStep(affiliateName, customerID)
{
    if (affiliateName == AFFILIATE_ID_MICROSOFT)
    {
        addPixel('../pixels/msn_thankyou.htm');        
    }   
    else if (affiliateName == AFFILIATE_ID_ILSE_MEDIA)
    {
        addPixel('../pixels/imx_thankyou.htm');
    }
    else if (affiliateName == AFFILIATE_ID_DAISYCON)
    {
        addPixel('../pixels/dsc_thankyou.htm');        
    }
    else if (affiliateName == AFFILIATE_ID_M4N)
    {
        addPixel('../pixels/m4n_thankyou.htm');        
    }
    else if (affiliateName == AFFILIATE_ID_ZANOX)
    {
        var src = '../pixels/znx_thankyou.htm';
        if (customerID != null)
            src += '?customerID=' + customerID;
        addPixel(src);          
    }
    else if (affiliateName == AFFILIATE_ID_OMG)
    {
        addPixel('../pixels/omg_thankyou.htm');        
    }
    //ADL pixels should always be loaded no matter which the current affiliate is    
    addPixel('../pixels/adl_thankyou.htm');
    
    //google pixel should always be loaded no matter which the current affiliate is    
    addPixel('../pixels/adw_thankyou.htm');
}

function loadPixelsForRegisteredStep(affiliateName, customerID)
{
    //ADL pixel loaded always - no matter which the current affiliate is
    addPixel('../pixels/adl_registration.htm');        
}

function loadPixelsForOrderPageStep(affiliateName, customerID)
{
    //ADL pixel loaded always - no matter which the current affiliate is
    addPixel('../pixels/adl_order.htm');
}

function loadPixelsforBusinessThankYouStep(affiliateName, customerID)
{
    //ADL pixel loaded always - no matter which the current affiliate is
    addPixel('../pixels/adl_businessthankyou.htm');
}

//currentStep:
//"ThankYou" - the Thank You step
//"FirstPage" - after age check
//"LoggedIn" - after the user has logged in
//"OrderPage" - order page
//"BusinessThankYou" - business thank you step