/**************************************************
Append the first 11 chars of clickid to the refcode
**************************************************/
/* GetQueryStringParam is defined in commonJS.js */
var clickid = GetQueryStringParam( "clickid" );
var currentRef = GetQueryStringParam( "ref" );

/* proceed only if clickid is a string greater than 11 chars and if ref contains linkshare */
if ( clickid != "" && clickid.length > 11 && currentRef.toUpperCase().match("LINKSHARE") != null ) {
    AppendToRef( clickid, currentRef );
}
function AppendToRef( c, ref ) {
    var toAppend = c.slice(0, 11);

    /* check if refcode already has the clickID appended.*/
    if ( ref.toUpperCase().match( toAppend.toUpperCase() ) != null ) {
        return;
    }
        
    /* get all the hyperlinks on the page */
    var allAnchors = document.getElementsByTagName("a");
    for ( i=0; i<allAnchors.length; i++ ) {
        var thisLink = allAnchors[i].getAttribute( "href" );
        if ( thisLink != null ) {
            /* Do a regex replace of ref */
            /* search for ref and stop at first occurence of & */
            var re = new RegExp( "(ref=.+?&)", "i" );
            
            var newLink = thisLink.replace( re, "ref=" + ref + "_" + toAppend + "&"); 

            allAnchors[i].href = newLink;        
        }
    }
}