/**
  * $Id: ktrack.js,v 1.2 2006/12/21 15:33:58 fl Exp $
  * Im Querystring des origref kann 
  * ein Parameter cmp stehen, das sind durch . getrennte Kampagnen Strings
  *
**/

// niemals true auf einem Produktivsystem!
var ajaxdebug = false;

/**
 * Seitenaufruf registrieren
 */
function registerPage(options) {
	var url = "/ktrack/reg.php";
	var infoopts = null;

	var myPostBody = "origref=" + encodeURIComponent(document.referrer);
		myPostBody += "&ref=" + encodeURIComponent(window.location.href);
		myPostBody += "&protocol=" + encodeURIComponent(window.location.protocol);
		myPostBody += "&host=" + encodeURIComponent(window.location.hostname);
		myPostBody += "&port=" + encodeURIComponent(window.location.port);
		myPostBody += "&path=" + encodeURIComponent(window.location.pathname);
		myPostBody += "&search=" + encodeURIComponent(window.location.search);

	if (options) {
		if (options.url) 
			url = options.url;
		if (options.area)
			myPostBody += "&area=" + encodeURIComponent(options.area);
		if (options.formsent)
			myPostBody += "&formsent=" + encodeURIComponent(options.formsent); 
		if (options.infoopts)
			infoopts = options.infoopts;
	}

	//alert("registerPage: " + url + " post=" + myPostBody);
	
	var opt = {
	    // Use POST
    	method: 'post',
    	// Send this lovely data
    	postBody: myPostBody,
    	// Handle successful response
    	onSuccess: function(t) {
    		if (ajaxdebug)
        		alert('registerPage: ' + t.responseText);
        	if (infoopts)
        		getKampagnenInfo(infoopts);
    	},
    	// Handle 404
    	on404: function(t) {
    		if (ajaxdebug)
        		alert('registerPage: Error 404: location "' + t.statusText + '" was not found.');
    	},
    	// Handle other errors
    	onFailure: function(t) {
    		if (ajaxdebug)
	        	alert('registerPage: Error ' + t.status + ' -- ' + t.statusText);
    	}
    }

	new Ajax.Request(url, opt);
}

/**
 * Gesammelte Infos abholen und in ein Seitenelement schreiben.
 */
function getKampagnenInfo(options) {
	var url = "/ktrack/data.php";
	var elementId = "formElem";
	
	var myPostBody = "host=" + encodeURIComponent(window.location.hostname);

	if (options) {
		if (options.url) 
			url = options.url;
		if (options.format)
			myPostBody += "&format=" + encodeURIComponent(options.format);
		if (options.prefix)
			myPostBody += "&prefix=" + encodeURIComponent(options.prefix);
		if (options.formsent)
			myPostBody += "&formsent=" + encodeURIComponent(options.formsent); 
		if (options.element)
			elementId = options.element;
	}

	var opt = {
	    // Use POST
    	method: 'post',
    	// Send this lovely data
    	postBody: myPostBody,
    	// Handle successful response
    	onSuccess: function(t) {
    		if (ajaxdebug)
        		alert('getKamapagnenInfos [' + elementId + ']: ' + t.responseText + ' ' + t.status + ' ' + t.statusText);
        	// nix zu tun
    	},
    	// Handle 404
    	on404: function(t) {
    		if (ajaxdebug)
	        	alert('getKamapagnenInfos: Error 404: location "' + t.statusText + '" was not found.');
    	},
    	// Handle other errors
    	onFailure: function(t) {
    		if (ajaxdebug)
	        	alert('getKamapagnenInfos: Error ' + t.status + ' -- ' + t.statusText);
    	}
    }

	new Ajax.Updater(elementId, url, opt);
}


function emptyFields() {
	document.forms.datum.elements.a_tt.value='-';
	document.forms.datum.elements.a_mm.value='-';
	document.forms.datum.elements.a_yy.value='-';
	document.forms.datum.elements.b_tt.value='-';
	document.forms.datum.elements.b_mm.value='-';
	document.forms.datum.elements.b_yy.value='-';
	return false;
}
