// Author: Todd Markelz
//
// Common JavaScript functions for toolbar websites.

/**
 * Redirect user to target while passing on the full querystring
 * @param target -- URL to redirect user to
*/
function trackingRedirect(target) {
  var qs = '';
  var url = String(window.location);
  var index = url.search(/[\#\?]/);
  if (index > 0) qs = url.substring(index);
  
  if (document.images) {
    location.replace(target+qs);
  }
  else {
    location.href = target+qs;
  }
}

/**
 * If refer comes from pack, or 'refer=pack' set in querystring hide download button
 * @param arr -- Array of element ids to hide
 */
function packInit(arr) {
 if (((document.referrer.indexOf("http://pack.google.com") != -1) ||
      (String(window.location).indexOf("refer=pack") != -1)) &&
      (document.getElementById)) {
  var l = arr.length;
  for (var i=0; i < l; i++) {
   document.getElementById(arr[i]).style.display = 'none';
  }
 }
}

/**
 * Set tbbrand input value if it exists in the querystring
 * This function must be called from the page after the download form is drawn
 * TEMP: Disabled for Vista
 * @param form -- Form to update with the tbbrand value
 */
function setBrand(form) {
  // Form field to update
  var field = document.forms[form].elements['tbbrand'];
  var url = String(window.location);
  var match = url.match(/tbbrand\=([A-Z]{4})[&#?]*/);
  var vista = navigator.userAgent.match("Windows NT 6\.0");
  // Only update if tbbrand is in the url and the field exists
  if (match && field && !vista) {
    field.value = match[1];
  }
}

/**
 * Set tbbrand for pack installs
 * This function must be called from the page after the download form is drawn
 * @param form -- Form to update with promo value
 */
function setPackBrand(form) {
  var action = null;
  try {
    action = document.forms[form].action;
  }
  catch(err) {
    // Do nothing
  }
  var url = String(window.location);
  var match = url.match(/tbbrand\=GZ([A-Z]{2})[&#?]*/);
  if (match && action) {
    document.forms[form].action = action + "&promo=" + match[1];
  }
}
