﻿var currenturl = window.location.pathname;
var url, pagename, server, channel, pagetype, prop1, prop2, prop3, prop4, prop5, prop6, prop7, prop8, prop9, prop10, campaign, state, zip, events, products, purchaseid, evar1, evar2, evar3, evar4, evar5, evar6, evar7, evar8, evar9, evar10, evar11;
var searchkeywords, searchcount;

$(document).ready(function () {
    if (currenturl.endsWith("/", false) == true)
    { currenturl = currenturl + 'index.aspx'; }

    if (currenturl.indexOf('/search-results.aspx') < 0) {
        currenturl = currenturl + window.location.search;
    }

    if (currenturl.indexOf('/buy-coffee/index.aspx?prod=') == 0) {
        currenturl = window.location.pathname;
    }

    $.ajax({
        type: "GET",
        url: "/omniture-page-definitions.xml",
        datatype: "xml",
        async: false,
        success: function (xml) {
            $(xml).find('Row').each(function () {
                url = $(this).find('URL').text();
                if (url != undefined && url == currenturl) {
                    pagename = $(this).find('s_pageName').text();
                    prop1 = $(this).find('s_prop1').text();
                    prop2 = $(this).find('s_prop2').text();
                    prop3 = $(this).find('s_prop3').text();
                    events = $(this).find('s_events').text();
                    products = $(this).find('s_products').text();
                    evar1 = $(this).find('s_eVar1').text();
                }
            });
        }
    });

    switch (currenturl) {
        case "/search-results.aspx":
        case "/coffee-recipes/search-results.aspx":
            prop4 = searchkeywords;
            prop5 = searchcount;
            events = 'event1';

            if (searchcount == 0) {
                prop4 = 'null:' + searchkeywords;
                prop5 = 'zero';
                events = events + ',event2';
            }
            break;
        case "/buy-coffee/index.aspx":
            var value = $("input[name$=$productid]:checked"); //.attr('id'); 
            if (value != undefined) {
                var ctrlid = value.attr('id');
                if (ctrlid != undefined) {
                    var product = ctrlid.substring(ctrlid.lastIndexOf("_") + 1);
                    evar8 = $.trim($("label[for='" + product + "']").text());
                }
            }

            var zipcode = $("input[id$='_txtZip']");
            if (zipcode != undefined && zipcode.val() != "") {
                evar9 = zipcode.val();
            }

            SetVariablesClientSide();
            break;
        //            var button = $("input[id$='_btnSubmit']");        
        //            if (button != undefined) {        
        //                button.click(function () {        
        //                    var value = $("input[name$=$productid]:checked"); //.attr('id');        
        //                    if (value != undefined) {        
        //                        var ctrlid = value.attr('id');        
        //                        var product = ctrlid.substring(ctrlid.lastIndexOf("_") + 1);        
        //                        evar7 = $("label[for='" + product + "']").text();        
        //                    }        

        //                    var zipcode = $("input[id$='_txtZip']");        
        //                    if (zipcode != undefined) {        
        //                        evar8 = zipcode.val();        
        //                    }        
        //                });        
        //            }        
        //            break;        
        case "/folgerswakin-up-club/index.aspx":
            var formbutton = $("input[id$='_btnSubmit'], input[id$='_btnSubmitDefault']");
            if (formbutton != undefined) {
                formbutton.click(function () {
                    var validsummary = $("div[id$='_vldSummary']");
                    if (validsummary.is(":visible")) {
                        var omniturefield;
                        var errors = '';

                        $("[id$='_pnlForm'] input").each(function () {
                            var fieldval = $(this).val();
                            if (fieldval == '') {
                                omniturefield = $(this).attr('OmnitureVariable');
                                if (omniturefield != undefined) {
                                    errors += omniturefield + ',';
                                }
                            }
                        });

                        //dropdowns.
                        $("[id$='_pnlForm'] select").each(function () {
                            var fieldval = $(this).val();
                            if (fieldval == '-1') {
                                omniturefield = $(this).attr('OmnitureVariable');
                                if (omniturefield != undefined) {
                                    errors += omniturefield + ',';
                                }
                            }
                        });

                        evar1 = "Wakin' Up Club Signup Form";
                        prop7 = errors;
                        events = "event5";
                        SetVariablesClientSide();
                    } else {
                        events = "event4";
                        SetVariablesClientSide();
                    }
                });
            }
            break;
        default:
    }

    s.pageName = pagename
    s.server = server
    s.channel = channel
    s.pageType = pagetype
    s.prop1 = prop1
    s.prop2 = prop2
    s.prop3 = prop3
    s.prop4 = prop4
    s.prop5 = prop5
    s.prop6 = prop6
    s.prop7 = prop7
    s.prop8 = prop8
    s.prop9 = prop9
    s.prop10 = prop10
    s.campaign = campaign
    s.state = state
    s.zip = zip
    s.events = events
    s.products = products
    s.purchaseID = purchaseid
    s.eVar1 = evar1
    s.eVar2 = evar2
    s.eVar3 = evar3
    s.eVar4 = evar4
    s.eVar5 = evar5
    s.eVar6 = evar6
    s.eVar7 = evar7
    s.eVar8 = evar8
    s.eVar9 = evar9
    s.eVar10 = evar10
    s.eVar11 = evar11
    /************* DO NOT ALTER ANYTHING BELOW THIS LINE ! **************/
    var s_code = s.t(); if (s_code) document.write(s_code)
});


function SetVariablesClientSide() {
    s.pageName = pagename
    s.server = server
    s.channel = channel
    s.pageType = pagetype
    s.prop1 = prop1
    s.prop2 = prop2
    s.prop3 = prop3
    s.prop4 = prop4
    s.prop5 = prop5
    s.prop6 = prop6
    s.prop7 = prop7
    s.prop8 = prop8
    s.prop9 = prop9
    s.prop10 = prop10
    s.campaign = campaign
    s.state = state
    s.zip = zip
    s.events = events
    s.products = products
    s.purchaseID = purchaseid
    s.eVar1 = evar1
    s.eVar2 = evar2
    s.eVar3 = evar3
    s.eVar4 = evar4
    s.eVar5 = evar5
    s.eVar6 = evar6
    s.eVar7 = evar7
    s.eVar8 = evar8
    s.eVar9 = evar9
    s.eVar10 = evar10
    s.eVar11 = evar11
    /************* DO NOT ALTER ANYTHING BELOW THIS LINE ! **************/
    var s_code = s.t(); if (s_code) document.write(s_code)
}

String.prototype.endsWith = function(t, i) { 
    if (i==false) { 
        return (t == this.substring(this.length - t.length)); } 
    else { 
        return (t.toLowerCase() == this.substring(this.length - t.length).toLowerCase()); 
    } 
} 
