﻿
var aw_basket;
var aw_mid;
var aw_sale;
var aw_extra;
var aw_parts;
var aw_voucher;
var aw_test;
var awparts ="";
var aw_basket_products="";
var bIsAffiliate;


function AwinProductReporting(UnderlyingTypeId, ProductTypeId, AffiliateFlag, OrderRef, ProductCode, Title, Price, Quantity, CategoryName) {
  
    //check flag in cookie
    if (bIsAffiliate == null) {
        bIsAffiliate = true;

        CheckIsAffiliate();
        
    }

    //Check Commissiongroup
    var parts
    if (bIsAffiliate == true)
        parts = CheckCommisiongroup(UnderlyingTypeId, ProductTypeId, AffiliateFlag);
    else
        parts = GetCommissionGroupForPPC(UnderlyingTypeId, ProductTypeId, AffiliateFlag);
   
    aw_basket_products = aw_basket_products + "AW:P|273|" + OrderRef + "|" + ProductCode + "|" + Title + "|" + Price + "|" + Quantity + "||" + parts + "|"+ CategoryName +"\n";

    awparts = awparts + parts + ":" + Price + "|";

}

function SetAffiliateParameters(Ordertotal, OrderRef, DiscountCode, DeliveryMthodId, Deliverymethod, DelvieryPrice, ProductCode, Title, Price, Quantity) {
    
    var DeliveryType = ((bIsAffiliate) ? "Delivery" : "Delivery_NLR");

    awparts = awparts + DeliveryType + ":" + DelvieryPrice;

    aw_mid = "_mdid"; //fixed
    aw_sale = Ordertotal;
    aw_extra = OrderRef;
    aw_parts = awparts;
    aw_voucher = DiscountCode;
    aw_test = '0';
    affiliatewindowCode();

    $('head').append("<img  src='https://www.awin1.com/sread.img?tt=ns&amp;tv=1.0&amp;merchant=273&amp;amount=" + aw_sale + "&amp;ref=" + aw_extra + "&amp;parts=" + aw_parts + "&amp;testmode=0&amp;vc=" + aw_voucher + "' alt=' ' />");
    //alert("imgs = <img  src='https://www.awin1.com/sread.img?tt=ns&amp;tv=1.0&amp;merchant=273&amp;amount=" + aw_sale + "&amp;ref=" + aw_extra + "&amp;parts=" + aw_parts + "&amp;testmode=0&amp;vc=" + aw_voucher + "' alt=' ' />");

   
   
}



//check flag in cookie
function CheckIsAffiliate() {

    var val = "ppc";
    var nameEQ = "BAGCookie=";

    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) {
            c =c.substring(nameEQ.length, c.length);
            c = c.replace("Name=", "");

            if (c == val)
                bIsAffiliate = false;
        }
    }    
}

//Check Commissiongroup
function CheckCommisiongroup(UnderlyingTypeId, ProductTypeId, AffiliateFlag) {

    var strAwinLabel = "";
    switch (UnderlyingTypeId) {
        /*
        * 701 - Supplier_Tangibles 
        * 702 - In-house_Tangibles
        * 703 - Choice_Vouchers
        * 704 - Consumables
        * 705 - Voucher
        * 706 - PrintNGo
        */ 
        case "701":
            strAwinLabel = "Supplier_Tangible";
            break;

        case "702":
            strAwinLabel = "In-house_Tangible";
            break;

        case "703":
            strAwinLabel = "Choice_Vouchers";
            break;

        case "704":
            strAwinLabel = "Consumables";
            break;

        case "705":
            strAwinLabel = "Voucher";
            break;

        case "706":
            if (AffiliateFlag.Length != 0)
                strAwinLabel = AffiliateFlag + "PRINTNGO";
            else
                strAwinLabel =  "PrintNGo";
            break;

        default:
            strAwinLabel = "DELIVERY";
            break;

    }  
    return strAwinLabel;

}

function GetCommissionGroupForPPC(strUnderlyingTypeId, strProductTypeId, AffiliateFlag) {
    var strAwinLabel = "";
            switch (strUnderlyingTypeId)
            {


                /*
                 * 701 - Supplier_Tangibles 
                 * 702 - In-house_Tangibles
                 * 703 - Choice_Vouchers
                 * 704 - Consumables
                 * 705 - Voucher
                 * 706 - PrintNGo
                 */
                case "701":
                    strAwinLabel = "SUPPLIER_TANGIBLE_NLR";
                    break;

                case "702":
                    strAwinLabel = "IN-HOUSE_TANGIBLE_NLR";
                    break;

                case "703":
                    strAwinLabel = "CHOICE_VOUCHERS_NLR";
                    break;

                case "704":
                    strAwinLabel = "Consumables_NLR";
                    break;

                case "705":
                    strAwinLabel = "VOUCHER_NLR";
                    break;

                case "706":
                    if (AffiliateFlag.Length != 0)
                        strAwinLabel = AffiliateFlag + "PRINTNGO_NLR";
                    else
                        strAwinLabel = "PRINTNGO_NLR";
                    break;                      

                default:
                    strAwinLabel = "DELIVERY_NLR";
                    break;

            }

            return strAwinLabel;
}


function affiliatewindowCode() {
 
    /*** Do not edit below here.         ***/
    call = location.protocol.toLowerCase() + "//www.aw";
    call += "in1.co" + "m/sread.j" + "s?a=" + aw_mid + "&b=" + aw_sale + "&c=" + aw_extra + "&d=" + aw_parts + "&vc=" + aw_voucher;
    call += "&t=" + aw_test + "&l=" + escape(window.location.href) + "&tv=1.0";
    $('head').append("<s" + "cript language=\"JavaS" + "cript\" type=\"text/javas" + "cript\" src=\"" + call + "&tt=js\" id=\"aw_tag\"></s" + "cript>");
    var oImage = new Image;
    oImage.src = call + "&tt=ia";

   // alert("affiliatewindowcode= " + "<s" + "cript language=\"JavaS" + "cript\" type=\"text/javas" + "cript\" src=\"" + call + "&tt=js\" id=\"aw_tag\"></s" + "cript>");
}
