var hostName = window.location.hostname.toLowerCase();
//alert('hostName = '+hostName);
var pageTitle = "";
var docArray = new Array('pdf','doc','xls','ppt');
var mediaArray = new Array('avi','wmv','qt','mp3','mov');
var urlMediaArray = new Array('www.youtube.com','youtu.be');

//helper function to open popup windows
function popWin( url, title, width, height ) {
  var v_url = url;
  var params='';
  if ( title == "newWindow" || undefined == title ){
    params = 'toolbar=yes,location=yes,status=yes,scrollbars=yes,resizable=yes';
  }
  else{
    params = 'toolbar=yes,location=no,status=yes,scrollbars=yes,resizable=yes';
  }
  //append popup parameters:
  if( width ) params += ',width=' + width + ',height=' + height;
  var newWindow = window.open( v_url, title, params );
} //popWin()
function removeQuestionMark( url ) {
  var v_url = url;
  if( v_url.lastIndexOf("?")!=-1 && v_url.lastIndexOf("?") == ( v_url.length-1 ) ) {
    v_url = v_url.substr( 0, v_url.length-1 );
  } //if
  return v_url;
} //removeQuestionMark()

//load function to check current browser and take action.
  (window.attachEvent) ? window.attachEvent('onload', initCheckers ) : window.addEventListener('load', initCheckers, false);

function initCheckers(){
  try{
  var browser = getBrowser();
  if (browser == "ie6"){
    var cookieData = getCookie("BrowserInfo");
    if(cookieData == null){
     setCookie("BrowserInfo",browser,"","","");
     popWin("http://www-qa1.transunion.com/corporate/ie6redirect.page","",800,600);
    }
  }
  }catch(e){}

  pageTitle = document.getElementsByTagName("title")[0].text;
}
  //=============================COOKIES==============================
function setCookie(sName, sValue, oExpires, sPath, sDomain, bSecure) {
  var sCookie = sName + "=" + encodeURIComponent(sValue);
  if (oExpires) sCookie += "; expires=" + oExpires.toGMTString();
  if (sPath) sCookie += "; path=" + sPath;
  if (sDomain) sCookie += "; domain=" + sDomain;
  if (bSecure) sCookie += "; secure";
  document.cookie = sCookie;
} //setCookie()
function getCookie(sName) {
  var sRE = "(?:; )?" + sName + "=([^;]*);?";
  var oRE = new RegExp(sRE);
  return (oRE.test(document.cookie)) ? decodeURIComponent(RegExp["$1"]) : null;
} //getCookie()
//======================END COOKIES ==================================

function referrerGetCookie() {
  var sRef=getCookie("TUReferrer");
  if(sRef) {
  return sRef;
  } //if sFontIndex
} //referrerGetCookie()

//==========================GOOGLE TRACK METHODS======================
//used by Flash files:
function googleTrack(arg1,arg2,arg3){
  //alert('will invoke Google with these args:\narg1 = |'+arg1+'|\narg2 = |'+arg2+'|\narg3 = |'+arg3+'|');
  trackGeneralEvent(arg1,arg2,arg3);
}

//2011/06/15 pmackie@transunion.com
//Google Analytics (former Fireclick) tracking, following are tracked (what==>how):
//popups==>media, external links==>ad, documents==>download
function track(_link,type,crossDomain) {
  var v_url=_link.href;
  var v_name=_link.name;
  //if no friendly name exists, use href value
  if(v_name=="")v_name=v_url;

  if(crossDomain.toLowerCase() == "cross-domain"){
    //alert('DEBUG B1: will invoke asynch cross-domain code with\nv_url = '+v_url);
    _gaq.push(['_link', v_url]);
	setTimeout('',100);  //delay to make sure the above call has time to finish
  }else{
    //only allow type: ad, media, download. If ad, make sure the link is external
    if((type=='ad' && v_url.match(v_hostname)==null)|| type=='media' || type=='download'){
      trackGeneralEvent('Download',type,v_name);
    }
  }
} //track()

function trackLinks(sender,trackingTitle){
  var href = sender.href;
  if( href.lastIndexOf("?")!=-1 && href.lastIndexOf("?") == ( href.length - 1 ) ) {
    href = href.substr( 0, href.length - 1 );
  }

  trackingTitle = trackingTitle.replace(/<br.*?>/gi, ' ');


  /* this code is used to check three condition whether track object is link,document or media file.
  if object is link the value is passed as 'Link ' + link path
  for Document value is 'Document ' + document name
  for Media value is 'Media ' + media file name as first argument */

  var fileName = href.substr(href.lastIndexOf('/')+1);
  var param,found = false;
  var callTrackEvent = true;

  if(fileName != null && fileName.length > 0){

    /*Condition checking for document */
    var extension = fileName.substr(fileName.indexOf('.') + 1);
    extension = extension.indexOf('?') > 0 ? extension.substring(0,extension.indexOf('?')) : extension ;
    if(extension != null && extension.length >0){
      for(i = 0;i < docArray.length;i++){
        if(docArray[i].toLowerCase() == extension.toLowerCase()){
          //param = 'Document ' + fileName;
          param = 'Document';
          found = true;
          break;
        }
      }
      /* Condition checking for media file,if it is not document */
      if(found == false){
        for(i = 0;i < mediaArray.length;i++){
          if(mediaArray[i].toLowerCase() == extension.toLowerCase()){
            //param = 'Media ' + fileName;
            param = 'Media';
            found = true;
            break;
          }
        }
      }

      /* Condition checking for external media file */
      if(found == false){
        for(i = 0;i < urlMediaArray.length;i++){
          if(href.toLowerCase().indexOf(urlMediaArray[i].toLowerCase()) != -1){
            param = 'Media';
            found = true;
            break;
          }
        }
      }
    }
    /* Considered as normal link if it is neither document nor link */
    if(found == false){
      if(href.indexOf("http") == -1 || href.toLowerCase().indexOf(hostName) != -1){
        callTrackEvent = false;
      }
      else{
        param = 'Link';
      }
    }
  }
  else{
      if(href.indexOf("http") == -1 || href.toLowerCase().indexOf(hostName) != -1){
        callTrackEvent = false;
      }
      else{
        param = 'Link';
      }
  }
  if(callTrackEvent == true){
    trackGeneralEvent(param,pageTitle,trackingTitle);
  }
  /*else{
    alert('Internal Link');
  }*/

}//trackLinks()

/*This method is use for tracking Locator events */
/*Possible parameter values are
  1. CountryName, Launguage if it is link.
  2. 'Button','Close' if close button click.
  3. 'Button','Open' if open button click.
  4. 'Link','Global Capabilities' if go to Global Capabilities button click.
*/
function trackLocator(param2,param3){
  var param1 = 'Locator';
  trackGeneralEvent(param1,param2,param3);
}//trackLocator()


function trackPageTools(category){
  var keywordElement = document.getElementById('searchKeywordValue');
  var keywordValue = '';
  if(keywordElement){
    keywordValue = keywordElement.innerHTML;
  }
  trackGeneralEvent(category,pageTitle,keywordValue);
}//trackPageTools()
function trackAccordion(title){
  var category = 'Accordion';
  trackGeneralEvent(category,pageTitle,title);
}//trackAccordion()

/* This method is use to track Form Abandonment */
function trackInput(sender,formName,fieldLabel) {
  var category = 'Forms';
  trackGeneralEvent(category,formName,fieldLabel);
}//trackInput()
function trackStep(formName, buttonKeyword){
  var category = 'Forms';
  trackGeneralEvent(category,formName,buttonKeyword);
}//trackStep()

function trackAccordionPrint(category,keywordValue){
  trackGeneralEvent(category,pageTitle,keywordValue);
}//trackAccordionPrint()

function trackLogin(href,trackingTitle){
  var category = 'Link';
  //trackingTitle = trackingTitle.replace(/&nbsp;/gi,' ');
  if(href.indexOf("http") != -1 && href.toLowerCase().indexOf(hostName) == -1){
    trackGeneralEvent(category,pageTitle,href);
  }
}//trackLogin()
function trackNews(sender){
  trackLinks(sender, sender.href,'');
}//trackNews()

function trackGeneralEvent(eventcategory,eventaction,eventlabel){
 //alert('CATEGORY :: ' + eventcategory + '\n ACTION :: ' + eventaction + '\n LABEL :: ' + eventlabel);
// alert('will invoke asynchronous Google Tracking trackEvent method\nwith following arguments:\n\nEvent Category = '+eventcategory+'\nEvent Action = '+eventaction+'\nEvent Label = '+eventlabel);
 //_gaq.push(['_trackGeneralEvent',eventcategory,eventaction,eventlabel]);
 _gaq.push(['_trackEvent',eventcategory,eventaction,eventlabel]);
 setTimeout('',100);  //delay to make sure the above call has time to finish
}//trackGeneralEvent()

/* cross-domain tracking and popup */
function cdt_popup(link,title,width,height) {
  _gaq.push(function() {
  var pageTracker = _gat._getTrackerByName();
  popWin(pageTracker._getLinkerUrl(link.href),title,width,height);
});
//return false;
}

//==========================END GOOGLE TRACK METHODS==================


