﻿var queryStringSearchItem = 'p';

$(document).ready(function() {
    appendPromo(queryStringSearchItem);
});

function appendPromo(match) {
    var q = String(document.location).split('?')[1];
    var promoCode = getQueryStringElement(match);

    if (!q) { // no query string to append
        //console.log("no query string to append");
        return true;
    } else { // there is a query string 
        // if the promocode exists in the query string, check if it exits in the ancor
        // if the promocode is NOT in the ancor then add
        if (!promoCode) { // no promocode so just return
            //console.log('no data in querystring');
            return false;
        }
        var flag = false;
        //console.log('appending \'' + promoCode + '\'');
        $('a').each(function() {
            try {
                if ($(this).attr('href').length > 0) {
                    // now check if the link already has some other type of query string.
                    //console.log('going to replace');

                    var q2 = $(this).attr('href').indexOf('?');

                    if (q2 == -1) {
                        $(this).attr('href', unescape($(this).attr('href')) + "?" + queryStringSearchItem + "=" + promoCode);
                    } else {
                        $(this).attr('href', unescape($(this).attr('href')) + "&" + queryStringSearchItem + "=" + promoCode);
                    }

                    $(this).attr('href', unescape(fixString(unescape($(this).attr('href')))));
                    flag = true;
                    //console.log($(this).attr('href'));
                }
            } catch (err) {
                //console.log("error");
            }
        });
        return flag;
    }
}

// Changes #foobar?promocode=blabla to ?promocode=blabla#foobar
// Used by faq page.
function fixString(link) {

    var q = String(link).indexOf("#");
    if (q == -1) {
        // there is no hash so return
        return link;
    }
    var hashIndex = String(link).indexOf("#");
    var quesIndex = String(link).indexOf("?");
    var hashComponent = String(link).substr(hashIndex, quesIndex);
    var quesComponent = String(link).substr(quesIndex, link.length);

    var newLink = quesComponent + hashComponent;
    return newLink;
}


// Returns the desired query string
function getQueryStringElement(key, default_) {
    if (default_ == null) default_ = "";
    key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regex = new RegExp("[\\?&]" + key + "=([^&#]*)");
    var qs = regex.exec(window.location.href);
    if (qs == null)
        return default_;
    else
        return qs[1];
}


// Returns the comma delimited query string.
function getQueryString() {
    var querystring = new Array;

    // parse current url into an array with the keys/values
    var q = String(document.location).split('?')[1];

    if (!q) return false;
    q = q.split('&');
    for (var i = 0; i < q.length; i++) {
        // for each key/value, split them at the '='
        // and add them to the qerystring array
        var o = q[i].split('=');
        querystring[o[0]] = o[1];
    }

    // return the querystring
    return querystring;
}
