/**
 * Custom Ero Advertising Tracker specific for Milf.nl
 *
 * This required an completely different approach to Google Conversion and is not
 * part of the standard tracking model used by Islive.
 *
 * Script should track a domain based Cookie using the eroAdEnter command and mark a conversion
 * point with eroAdConvert
 *
 * IMPORTANT: Since this is a domain based Cookie the domain must be provided with the
 * format '.<domain_name_here>' to support sub-domains.
 *
 */



/**
 * Start tracking the `queryKey` (a key in the querystring) as a
 * cookie for `domain`
 */
function eroAdEnter(queryKey, domain) {
	if( trackValue = $.getQuery()[queryKey] ) {
		$.setCookie( queryKey, trackValue, 1, domain );
	}
}

/**
 * Send a conversion on `queryKey` (stored cookie) for `domain`
 * and delete the cookie.
 */
function eroAdConvert(queryKey, domain) {
	if( trackingValue = $.getCookie(queryKey) ) {
		convURL = 'http://tracker.ero-advertising.com/tracking/conv.php';
		params = 'isreal=1&cid=' + trackingValue;

		$.ajax({
			url: convURL,
			data: params,
			complete: function() {
				$.delCookie(queryKey, domain);
			}
		});
	}
}

/**
 * Send a conversion on `queryKey` (stored cookie) for `domain`
 * and delete the cookie.
 */
function eroAdConvertSignup(queryKey, domain) {
	if( trackingValue = $.getCookie(queryKey) ) {
		convURL = 'http://tracker.ero-advertising.com/tracking/conv.php';
		params = 'cid=' + trackingValue;

		$.ajax({
			url: convURL,
			data: params,
			complete: function() {
				$.delCookie(queryKey, domain);
			}
		});
	}
}


/**
 * Some standard API type commands to do the job
 *****************************************************************************
 */

$.setCookie = function(cookieName, val, days, domain) {
	var exdate=new Date();
	exdate.setDate( exdate.getDate() + days );
	var cookie = [
		escape(val),
		( domain )? 'domain=' + domain : "",
		((days==null) ? "" : "expires="+exdate.toUTCString())
	];
	document.cookie = cookieName + "=" + cookie.join(';');
}

$.getCookie = function (c_name) {
	var i,x,y,ARRcookies = document.cookie.split(";");
	for (i=0; i<ARRcookies.length; i++) {
		x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
		y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
		x=x.replace(/^\s+|\s+$/g,"");
		if (x==c_name)
			return unescape(y);
	}
}

$.delCookie = function(cookieName, domain) {
	$.setCookie(cookieName, '', -1, domain);
	if( !$.getCookie(cookieName) ) {
		return true;
	} else {
		return false;
	}
}

$.getQuery = function(){
    qs = document.location.search;
    var params = {};
    var tokens;
    var searchString = document.location.search;
    // strip off the leading '?'
    searchString = searchString.substring(1);
    var nvPairs = searchString.split("&");
    for (i = 0; i < nvPairs.length; i++){
        var nvPair = nvPairs[i].split("=");
        params[nvPair[0]] = nvPair[1];
    }
    return params;
}

