/*
javascript for log adcontrol clicks and conversions.
Copyright (c) 2006 by team in medias.
http://www.inmedias.de/
*/

function URLencode(sStr) {
	return escape(sStr).replace(/\+/g, '%2B').replace(/\"/g,'%22').replace(/\'/g, '%27').replace(/\//g,'%2F');
}

var AdControl_expr = new Date();
//AdControl_expr.setTime(AdControl_expr.getTime() + (120*3600000));
// find keyword in cookies
var AdControl_Keyword = "";
if (document.cookie.indexOf("Keyword=") != -1) {
	AdControl_p = document.cookie.indexOf(" Keyword=") + 1;
	AdControl_p1 = document.cookie.indexOf(";", AdControl_p+7);
	if (AdControl_p1 == -1) AdControl_p1 = document.cookie.length;
	AdControl_Keyword = document.cookie.substring(AdControl_p+8, AdControl_p1);
}
// find adid
var AdControl_AdID = "";
var AdControl_p = location.href.indexOf("?AID=");
if (AdControl_p == -1) AdControl_p = location.href.indexOf("&dID=");
if (AdControl_p != -1) {
	var AdControl_p1 = location.href.indexOf("&", AdControl_p + 6);
	if (AdControl_p1 == -1) AdControl_p1 = location.href.length ;
	AdControl_AdID = location.href.substring(AdControl_p+6, AdControl_p1);
//	document.cookie = "AdID=" + AdControl_AdID + ";expires=" + AdControl_expr.toGMTString() + ";path=/";
	document.write("<script language='JavaScript' src='" + location.protocol + "//adcontrol.dev.inmedias.biz/scripts/index.cfm?fa=SetAdIDCookie&ID=" + AdControl_AdID + "'></script>");
}
// find Banner ID
var AdControl_AdBID = "";
var AdControl_p = location.href.indexOf("?AdBID=");
if (AdControl_p == -1) AdControl_p = location.href.indexOf("&AdBID=");
if (AdControl_p != -1) {
	var AdControl_p1 = location.href.indexOf("&", AdControl_p + 7);
	if (AdControl_p1 == -1) AdControl_p1 = location.href.length ;
	AdControl_AdBID = location.href.substring(AdControl_p+7, AdControl_p1);
}

// if we have AID param, then we don't have to count click
if (AdControl_AdID == "") {
	var AdControl_p = location.href.indexOf("?AdID=");
	if (AdControl_p == -1) AdControl_p = location.href.indexOf("&AdID=");
	if (AdControl_p != -1) {
		var AdControl_p1 = location.href.indexOf("&", AdControl_p + 6);
		if (AdControl_p1 == -1) AdControl_p1 = location.href.length ;
		AdControl_AdID = location.href.substring(AdControl_p+6, AdControl_p1);
//		document.cookie = "AdID=" + AdControl_AdID + ";expires=" + AdControl_expr.toGMTString();
		document.write("<script language='JavaScript' src='" + location.protocol + "//adcontrol.dev.inmedias.biz/scripts/index.cfm?fa=SetAdIDCookie&ID=" + AdControl_AdID + "'></script>");
		document.write("<script language='JavaScript' src='" + location.protocol + "//adcontrol.dev.inmedias.biz/scripts/index.cfm?jsmode=true&ID=" + AdControl_AdID +"&BannerID=" + AdControl_AdBID +"&Keyword=" + AdControl_Keyword + "&Referrer=" + URLencode(document.referrer) + "'></script>");
	}
}
if (AdControl_AdID != "" || document.cookie.indexOf("AdID=") != -1) {
	if (AdControl_AdID == "") {
		AdControl_p = document.cookie.indexOf(" AdID=") + 1;
		AdControl_p1 = document.cookie.indexOf(";", AdControl_p+4);
		if (AdControl_p1 == -1) AdControl_p1 = document.cookie.length;
		AdControl_AdID = document.cookie.substring(AdControl_p+5, AdControl_p1);
	}
	if (AdControl_AdID != "") {
		// if conversion sum exists, then we have conversion.
		// else may be this page should be logged as formularaufruf
		if (typeof AdControl_ConversionSum != "undefined")
			document.write("<script language='JavaScript' src='" + location.protocol + "//adcontrol.dev.inmedias.biz/scripts/index.cfm?fa=conversion&jsmode=true&ID=" + AdControl_AdID +"&Keyword=" + AdControl_Keyword + "&Summe=" + AdControl_ConversionSum + "&Page=" + URLencode(location.href) + "&Referrer=" + URLencode(document.referrer) + "'></script>");
		else
			document.write("<script language='JavaScript' src='" + location.protocol + "//adcontrol.dev.inmedias.biz/scripts/index.cfm?fa=aufruf&jsmode=true&ID=" + AdControl_AdID +"&Keyword=" + AdControl_Keyword + "&Page=" + URLencode(location.href) + "&Referrer=" + URLencode(document.referrer) + "'></script>");
	}
}
// handle keywords
if (AdControl_AdID == "" && document.referrer != "")
	document.write("<script language='JavaScript' src='" + location.protocol + "//adcontrol.dev.inmedias.biz/scripts/index.cfm?fa=keyword&ID=" + AdControl_AdID + "&Page=" + URLencode(location.href) + "&referrer=" + URLencode(document.referrer) + "&host=" + location.host + "'></script>");
// check - have we to show popup or not
function getCookie(name) {
	var start = document.cookie.indexOf(name + "=");
	var len = start + name.length + 1;
	if (!start && name != document.cookie.substring(0, name.length)) return 0;
	if (start == -1) return 0;
	var end = document.cookie.indexOf(";", len);
	if (end == -1) end = document.cookie.length;
	return unescape(document.cookie.substring(len, end));
}
var AdControl_PopupAt = Number(getCookie("AdControlPopupAt"));
if (!isNaN(AdControl_PopupAt) && AdControl_PopupAt > 0) {
	AdControl_now = new Date();
	AdControl_delay = AdControl_PopupAt - AdControl_now.valueOf();
	if (AdControl_delay >= 0)
		document.write("<script language='JavaScript' src='" + location.protocol + "//adcontrol.dev.inmedias.biz/scripts/index.cfm?fa=showpopup&KeywordID=" + getCookie("AdControlKeywordID") + "&Delay=" + AdControl_delay + "'></script>");
}
