// Scripts to display non-crawlable Javascript links

// Links to the page given by a splitted URL.
// The arguments are the tokens forming the splitted URL.
function kk_link()
{
   var url = '';
   for (i = 0; i < arguments.length; i++)
   { 
      url = url + arguments[i];
   }
   kk_go(url);
}

// Recompose complete Url from arguments list
// The first argument indicates if this is an absolute url,
// the other arguments are the tokens forming the splitted URL
function kk_recomposeUrl()
{
    var args = arguments[0];

    var absolute = args[0];
    var url = '';
    var startingIndex = 1;

    var sep = '/';
    var middle = '?';

    if (absolute && args.length > 1) {
        // absolute not-empty url, first element is the protocol
    	url = args[1] + ':' + sep;
	startingIndex++;
    }

    // Test if argument 2 already contains a ?
    var isPartialURL = false;
    var arg2 = args[1];
    if (arg2.indexOf('?') != -1) {
	isPartialURL = true;
    }

    var firstElem = true;
    for (i = startingIndex; i < args.length; i++)
    {
	if (args[i] == middle) {
      	    // Starting the query string
      	    i++;
      	    if (i < args.length) {
            	// if query string not empty
            	url += middle + args[i];
            	sep = '&';
      	    }
   	} else {
            // Simply concatenate the argument
	    if (!isPartialURL) {
		url += sep + args[i];
	    } else {
		if (firstElem) {
		    url += args[i];
		    firstElem = false;
		    // Bug #1158751: add "&addedParams=true" to the first argument, mandatory to validate the signature of shopbot URLs
                    url += "&addedParams=true";
		} else {
		    url += '&' + args[i];
		}
 	    }
   	}
    }
    return url;
}

// Javascript function to display non-crawlable Javascript links
// Links to the page given by a splitted URL
function kk_link2()
{
    var url = kk_recomposeUrl(kk_link2.arguments);
    kk_go(url);
}

// Javascript function to display non-crawlable Javascript links
// Links to the page given by an URL encoded using Base64
function kk_link3(url)
{
    var url = kk_decode64(url);
    kk_go(url);
}

var kk_keyStr = "ABCDEFGHIJKLMNOP" +
                "QRSTUVWXYZabcdef" +
                "ghijklmnopqrstuv" +
                "wxyz0123456789+/" +
                "=";

function kk_decode64(input) {
   var output = "";
   var chr1, chr2, chr3 = "";
   var enc1, enc2, enc3, enc4 = "";
   var i = 0;

   // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
   var base64test = /[^A-Za-z0-9\+\/\=]/g;
   if (base64test.exec(input)) {
      return '';
   }
   input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

   do {
      enc1 = kk_keyStr.indexOf(input.charAt(i++));
      enc2 = kk_keyStr.indexOf(input.charAt(i++));
      enc3 = kk_keyStr.indexOf(input.charAt(i++));
      enc4 = kk_keyStr.indexOf(input.charAt(i++));

      chr1 = (enc1 << 2) | (enc2 >> 4);
      chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
      chr3 = ((enc3 & 3) << 6) | enc4;

      output = output + String.fromCharCode(chr1);

      if (enc3 != 64) {
         output = output + String.fromCharCode(chr2);
      }
      if (enc4 != 64) {
         output = output + String.fromCharCode(chr3);
      }

      chr1 = chr2 = chr3 = "";
      enc1 = enc2 = enc3 = enc4 = "";

   } while (i < input.length);

   return output;
}
