
/*

This file contains code for placing an affiliate id sent in the URL
and placing it in a cookie.  You should just call handleAffiliateVisit()
for this functionality.

the id should be sent in the query parameters in the form aff=ID, where ID is a numeric ID.
the cookie will be of the name "ubAffiliate".

The functions to obtain the cookie and place it in the product form
are also contained in this file.
Please call handleAffiliateCheckout().
This function should be safe on a any page, since it just adds a hidden child field
of name "kAffiliate" to an element it finds
with name "prodForm"
*/

//handleAffiliateVisit();
//handleAffiliateCheckout();


/**
*   This is the master function for affiliate checking.
*   This function will check for a query parameter of "aff" with a numeric value.
*   If found, the value will be placed in a cookie called "uBAffiliate".
*/
function handleAffiliateVisit() {

    var queryParams = getQueryParameters();
    var affiliateId = queryParams.aff;

    if (affiliateId == null || affiliateId.length == 0) {
        return;
    }

    buildAffiliateCookie(affiliateId);
}

//
// Build cookie section
//


/**
*   This function grabs all of the name/value parameter pairs from the URL
*   and returns them in an object.  Reference using syntax PARAM_VALUE = obj.PARAM_NAME;
*/
function getQueryParameters() {

    var pairs = new Object(); //the return value

    //location.search is the query string, including the "?".  We substring to strip off the question mark.
    var queryString = location.search.substring(1);

    //create an array of all possible name/value pairs
    var pairArray = queryString.split("&");

    /*
    *   Split each name/value string on "=", make sure we actually got a pair as a result, and throw the
    *   URL-decoded name/values into the pairs object.
    */
    for (var i = 0; i < pairArray.length; i++) {
        var pair = pairArray[i].split("=");
        if (pair.length < 2) {
            continue;
        }
        pairs[unescape(pair[0])] = unescape(pair[1]);
    }

    return pairs;
}

function buildAffiliateCookie(affiliateId) {
    document.cookie = "uBAffiliate=" + affiliateId;
}


//
// Get cookie section
//

/**
*   This is the entry function for handling the affiliate product-to-cart crossover.
*   Simply call it from a product page.
*/
function handleAffiliateCheckout() {
    //get affiliate ID
    var affiliateId = getAffiliateFromCookie();
    if (affiliateId == null) {
        return;
    }

    //set the value in the product form
    var element = createInputElement(affiliateId);
    insertInputElement(element);
}

function createInputElement(affiliateId) {

    var newElement = document.createElement("INPUT");
    newElement.type = "hidden";
    newElement.name = "kAffiliate";
    newElement.value = affiliateId;

    return newElement;
}

function insertInputElement(element) {
    //apparently, Mozilla can't handle getElementById on a name.  Instead, we'll step through
    //each form in the page using insertIntoAllProductForms(element)
    //which has some advantages, anyway, in the case of buy now buttons (all forms will have the id inserted)

    /*var form = document.getElementById("prodForm");
    if (form == null) {
        return;
    }
    //alert("found form");
    //form.insertBefore(element, form.firstChild);
    form.appendChild(element);
    //alert("child appended");*/
    insertElementIntoAllProductForms(element);
}

function insertElementIntoAllProductForms(element) {
    var allForms = document.getElementsByTagName("FORM");
    for (var i = 0; i < allForms.length; i++) {
        if (allForms[i].name == "prodForm") {
            allForms[i].appendChild(element);
        }
    }
}


function getAffiliateFromCookie() {
    return getCookies().uBAffiliate;
}

/**
*   returns an Object containing all of the document's cookies.
*   reference using syntax COOKIE_VALUE = obj.COOKIE_NAME;
*/
function getCookies() {

    var cookiesString = document.cookie;

    var cookiesArray = cookiesString.split(";");

    var cookies = new Object();

    for (var i = 0; i < cookiesArray.length; i++) {
        var pair = cookiesArray[i].split("=");
        if (pair.length < 2) {
            continue;
        }
        cookies[unescape(pair[0])] = unescape(pair[1]);
    }

    return cookies;
}