
var oKT, W=window, D=document, Nav, Gec, FF3, Ns, E, EvSrc, aErrScr=[], bKT, qseid, CS, bGr;

function getbyid(id){return D.getElementById(id);}
function StartsWith(S1, S2){return (S1.substring(0, S2.length)==S2);}  
function KT_OMD(e){
var p, Tag, HRef, Site, bChg;

KT_EventInit(e); Tag=EvSrc.tagName;
if (Tag!='A' && Tag!='AREA'){
	if (!Tag) return;
	Tag=EvSrc.parentNode.tagName;
	if (Tag!='A' && Tag!='AREA') return;
	EvSrc=EvSrc.parentNode;
	}

if (oKT.LastEvSrc==EvSrc) return;
oKT.LastEvSrc=EvSrc;

HRef=EvSrc.href;
if ((p=HRef.indexOf('#http:'))!=-1){
	HRef=HRef.substr(p+1); bChg=true;
	}
else{
	if (HRef.indexOf('/go2phar'+'macy.php')!=-1) Site='DC'; 
	else if (HRef.indexOf('/engine/locations/')!=-1) Site='SG'; 

	if (Site){
		if (HRef.indexOf(KT_UId)!=-1) return;
		if (Site=='DC') HRef+='&u='+KT_UId;
		else if (Site=='SG') HRef=HRef.replace('.html', '_u'+KT_UId+(oKT.bSearch ? '_s' : '')+'.html');
		EvSrc.href=HRef; return;
		}
	}

while(true){
	if (!qseid) break;
	if (HRef.indexOf("="+qseid)!=-1) break;
	HRef=HRef.replace(/([&?])(said|trk|tid|data1|keywords|s|cid)=[\d\w]+/, "$1$2="+qseid); 
	bChg=true; //?
	break;
	}

//Copied from pricelist.js!
if (HRef.indexOf('.mpr'+'ixe.')!=-1){
	HRef=HRef.replace(RegExp('\\.mpr'+'ixe\\.(com|net)'), '.e-medi'+'cations.com');
	bChg=true;
	}
if (HRef.indexOf('.e-medi'+'cations.')!=-1){
	var id='2438'+'945';
	if (!HRef.match("[?&]k=")) HRef+=(HRef.indexOf('?')!=-1 ? '&' : '?')+'k='+id;
	else{
		HRef=HRef.replace("=apo", "="+id); HRef=HRef.replace("=ap_32", "="+id);
		}
	bChg=true;
	}

if (bChg) EvSrc.href=HRef;
}

function KT_EventInit(e){
E=(!Gec ? event : e); EvSrc=E[Gec ? 'target' : 'srcElement'];
if (Ns && EvSrc.nodeType==3) EvSrc=EvSrc.parentNode;
} 

function KT_SetHandlers(){

Nav=W.navigator.userAgent; 
Gec=(Nav.indexOf('Gecko')!=-1); FF3=(Gec && Nav.indexOf('Firefox/3')!=-1); 
Ns=(Gec && Nav.indexOf('Netscape')!=-1);

oKT={
	bSearch:KT_SE,
	aEvents:['onmousedown', (!Gec ? 'onactivate' : (FF3 ? 'onkeydown' : 'onfocus'))]
	};

var i, EvName, aEvents=oKT.aEvents;

for(i=0; i<aEvents.length; i++){
	EvName=aEvents[i];
	if (D.attachEvent) D.attachEvent(EvName, KT_OMD);
	else D.addEventListener(EvName.substr(2), KT_OMD, false);
	}
}

function KT_Dec(Str){
var i, Res="";
Res=""; for(i=0; i<Str.length; i++) Res+=String.fromCharCode(Str.charCodeAt(i)-1);
return Res;
}

function KT_EBH(){
return KT_Dec(bGr ? "fchso" : "ktfsspst/gppmqsppgtfswfst");
}

function KT_Img(Categ, bNoHandlers){
var uid, C=D.cookie.split('; '), i, c, R="", S, E, 
	RE, SE, Q='', M, SN=0, SE0, Q0, SE2, Q2, bSE, aScr, Src, s=Categ, H, QSe, kth;

if ((aScr=D.getElementsByTagName('SCRIPT')) && (i=aScr.length)) Src=aScr[i-1].src;
//Just in case this scr gets on insur site
kth=KT_Dec("usbdlodporvfs"); //tnc=green
if (!(bGr=(Src.indexOf(kth)!=-1))) kth=KT_Dec("lfztusbdlfs"); //kt=yellow

if (D.getElementById('KTImg')){
	if (W.location.hostname!='www.shortaweek.com' && W.location.href.indexOf('file:')!=0)
		KT_OE("", W.location.href, 0, 142); 
	return;
	}

R=D.referrer; 
if ((i=R.lastIndexOf("#"))!=-1) R=R.substr(0, i); KT_SE='';

if (R && !R.match("^https?://"+W.location.host)){
	//SE & Referrer - see accesslog-parser.php
	S="^https?://([^/]*[.])?"; E="=([^&]+)"; 
	aRE=[
		S+'(google|alltheweb[.]com|ask[.]com|altavista|msn|bing[.]com|alexa[.]com|infoseek|search[.]live[.]com'+
			'|search[.]comcast[.]net)([.][^/]+)?/.*[?&](q)'+E,
		S+'(search[.]yahoo)([.][^/]+)?/.*[?&](p)'+E,
		S+'(aol|search[.]lycos|search[.]netscape|mamma[.]com|hotbot[.]com)([.][^/]+)?/.*[?&](query|encquery)'+E,
		S+'(webcrawler|excite[.]com|infospace[.]com)([.][^/]+)?/.*/(web)/([^/]+)'];
	
	for(i=0; i<aRE.length; i++){
		RE=new RegExp(aRE[i], "i");
	 	if (!(M=R.match(RE))) continue;
		SE=M[2]; Q=M[5]; break;
	 	}
	if (SE){
		if (SE=='search.live.com' || SE=='bing.com' || SE=='Bing.com') SE='msn';
		else if (SE.substr(0, 7)=="search.") SE=SE.substr(7);
		}
	KT_SE=SE; //Real SE
	if (!SE){ //Any site
		M=R.match("^https?://([^/:]+)"); SE=M[1];
		}
	}

if (!s){
	if (Src && (M=Src.match("\\?s=([a-z0])$"))) s=M[1];
	
	if (!s){
		H=D.body.innerHTML;
		if (H.indexOf('engine/locations/')!=-1 || H.indexOf('casino')!=-1) s='b';
		else if (H.indexOf('nsurance')!=-1) s='d';
		else s='a';
		}
	}

//Cookies
for(i=0; i<C.length; i++){
	c=C[i].split("=");
	if (c[0]=='kt_uid') uid=c[1];
	else if (c[0]=='kt_se' && (M=c[1].match("^([^:]+):([^:]+):(.*)"))){
		SN=M[1]; SE0=M[2]; Q0=M[3];
		}
	else if (s=='a' && c[0]=='kt_qseid') qseid=c[1];
	}

bSE=(SE && (SE!=SE0 || Q!=Q0));
if (!uid || bSE){
	if (!uid){
		uid=Math.random().toString().substr(2); KT_SetC("kt_uid", uid);
		}
	if (bSE){
		KT_SetC("kt_se", (++SN)+":"+SE+":"+Q); qseid=0;
		}
	}

if (s=='a'){
	if (SE && Q){SE2=SE; Q2=Q; R="";}
	else if (SE0 && Q0 && !SE && !qseid){SE2=SE0; Q2=Q0;}
	if (SE2 && Q2){
		if (SE2=='google') SE2='g'; else if (SE2=='yahoo') SE2='y'; else if (SE2=='msn') SE2='m';
		QSe=Q2+":"+SE2;
		}
	}

//Do not pass referrer only when we have QSe from current referrer
D.write('<Script Src="http://www.'+kth+'.com/h.php'+'?uid='+uid+'&sn='+SN+'&s='+s+'&url='+
	escape(W.location.href)+'&ref='+escape(R)+'&n='+
	Math.random().toString().substr(2, 4)+(QSe ? '&qse='+QSe : '')+(qseid ? '&qseid=1' : '')+
	'" Id=KTImg Defer Async><'+'/Script>');

KT_UId=uid+"-"+SN;
if (s!='0' && !bNoHandlers) KT_SetHandlers();
if ((s=='0' || s=='d') && (W.location.hostname.match(/insur/) || D.title.match(/insur/i))
	&& !D.title.match(/travel/i)) KT_OE("", W.location.href, 0, 183); //code.js on insur

//Discl.scr. takes too long to load as it is the last one, but KT is loaded in advance as it's from another host (for now)
if (s=='a') setTimeout("PL_WatchDiscl()", IEVer ? 500 : 1500);
}

function KT_SetC(c, v){

if (!CS){
	var DT=new Date(); DT.setUTCSeconds(DT.getUTCSeconds()+259200, 0);
	CS=";expires="+DT.toUTCString()+";path=/";
	}
D.cookie=c+"="+v+CS;
}

function KT_QSEId(i){
KT_SetC('kt_qseid', i); qseid=i;
}

function KT_GetParams(){
return '_u'+KT_UId+(oKT.bSearch ? '_s' : '');
}

function KT_OE(Msg, URL, LineNo, EId){

if (URL.substr(0, 5)=='file:') return true;
if (URL.substr(0, 7)=='chrome:') return true;
if (URL.substr(0, 27)=='http://translate.google.com') return true; //G.transl.

//No need to know/show
if (Msg.indexOf('WidgetManager')!=-1) return true;
else if (Msg=='Error loading script'){ //FF
	if (URL.indexOf("http://www.blogger.com/widgets/")==0) return true;
	//Try reload these
	if (URL=="http://www.google-analytics.com/urchin.js"){
		if (!aErrScr[URL]){
			aErrScr[URL]=true; D.write("<Script Src='"+URL+"'></"+"Script>"); return true;
			}
		}
	}

//Just once
if (W.oEBImg) return false;

oEBImg=D.createElement('IMG');
if (!EId) EId=158; if (!LineNo) LineNo=0;
oEBImg.src="http://"+KT_EBH()+".com/handler.php?e="+EId+"&descr="+
	escape(W.location)+(Msg ? 
		KT_Line(Msg)+KT_Line(URL+' line '+LineNo)+KT_Line(W.navigator.userAgent)+
		KT_Line('Ref:'+D.referrer)+KT_Line('<HR>') : '');

return false; //Do show the message for us to see it
}

function InitUA(){

IEVer=0; FFVer=0; Nav=W.navigator.userAgent; 
Gec=(Nav.indexOf('Gecko')!=-1); 
Saf=(Nav.indexOf(' AppleWebKit/')!=-1 && Nav.indexOf(' Safari/')!=-1);
Op=(Nav.indexOf('Opera')!=-1); //Ns=(Gec && Nav.indexOf('Netscape')!=-1); 
Chrome=(Saf && Nav.indexOf(' Chrome/')!=-1);
Quirks=(D.compatMode=='BackCompat');
bDev=(W.location.search.match("[?&]test=1"));

if (!Gec && !Op && !Saf){
	if (D.documentMode){
		IEVer=D.documentMode; if (IEVer==5) IEVer=6;
		}
	else{
		if (Nav.match(/ MSIE (\d+)/)) IEVer=RegExp.$1;
		if (IEVer==7 && Quirks) IE7Q=true; //IEVer=6; ?!
		}
	if (Nav.indexOf('IEMobile')!=-1) IEMob=true;
	}
else if (Gec && Nav.match(/Firefox\/(\d+)/)) FFVer=RegExp.$1;
} 

function KT_Line(Line){return escape("\n"+Line);}

//-----
var WDCnt=0, DisScr, bDisFixed, DscFixNo=0;

function PL_WatchDiscl(bFix){
var oDis, i, o, bDone, next;

while(true){
	oDis=getbyid('Disclaimer'); if (!oDis) break;
	if (oDis.innerHTML.length>10) return;
	
	if (!bFix){
		if (!DisScr){
			o=oDis;
			for(i=0; i<6; i++){
				next=o.nextSibling;
				while(!next){
					o=o.parentNode; next=o.nextSibling;
					}
				o=next;
				if (o.tagName){
					if (o.tagName=='SCRIPT'){
						if (StartsWith(o.src, 'http://'+W.location.hostname+'/commonres/')) DisScr=o;
						break;
						}
					}
				else i--;
				}
			if (!DisScr) break;
			}
		if (IEVer && DisScr.readyState!='loaded') break;
		
		//Make sure
		setTimeout("PL_WatchDiscl(true);", 250); return;
		}
	
	if (1){ //DscFixNo>0
		KT_OE("KT Discl: "+
			" DisDone: "+(W.bDisDone ? "1" : "0")+
			" DscFixNo: "+DscFixNo, W.location.href, 0, 156);
		}

	o=D.createElement('SCRIPT'); D.body.insertBefore(o, null); o.src=DisScr.src; bDisFixed=true;
	DscFixNo++; break;
	}

WDCnt++;
if (WDCnt<30 && DscFixNo<8){
	setTimeout("PL_WatchDiscl()", 250); //30*250=7.5sec
	return;
	}

var Msg;
if (!oDis) Msg='KT Discl: no discl';
else if (!DisScr) Msg='KT Discl: scr not found';
if (Msg) KT_OE(Msg, W.location.href, 0, 156); 
}

//-----

W.onerror=KT_OE;
InitUA(); KT_Img(); 
