var klickPixelStack = new Array();

function formsubmit(action) {
	if (action) {
		$('form').attr('action',action);
	}
	$('form')[0].submit();
}

function formsubmitform(form) {
	if (form) {
		$('#'+form).remove();
		$('form').append('<input type="hidden" name="'+form+'" value="submitted" />');
		formsubmit();
	}
}

function getRandomInt() {
	return Math.round(Math.random() * 10000000);
}

function clickIvw(agof, prod) {
	var ivwUrl	= 'http://aumospo.ivwbox.de/cgi-bin/ivw/';
	var pixel	= null;
		
	if (true === prod) {
		ivwUrl += 'CP/';
	} else {
		ivwUrl += 'XP/'; // testpixel ;-)
	}

	ivwUrl += agof + '?d=' + getRandomInt();
	pixel		= new Image();
	pixel.src	= ivwUrl;
	klickPixelStack.push(pixel);
}

function clickPot() {
	var pixel	= null;
	
	var resolution = window.screen.width + 'x';
    resolution += window.screen.height + 'x';
	resolution += window.screen.colorDepth + 'bit';
                 
	var potUrl	= 'http://ams.statistik.motorpresse.de/image.php?client_id=1';
	potUrl += '&document=' + globalAgofCode + ';' + encodeURI(window.location.pathname);
	potUrl += '&document_url=' + base64_encode(document.URL);
	potUrl += '&referer=' + base64_encode(document.referrer);
	potUrl += '&add_data[]=resolution::' + resolution;
	potUrl += '&random=' + getRandomInt();
	pixel		= new Image();
	pixel.src	= potUrl;
	klickPixelStack.push(pixel);
}

function base64_encode(decStr) {
  var base64s = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
  var bits;
  var dual;
  var i = 0;
  var encOut = '';

  while(decStr.length >= i + 3) {
    bits = (decStr.charCodeAt(i++) & 0xff) <<16 |
           (decStr.charCodeAt(i++) & 0xff) <<8 |
            decStr.charCodeAt(i++) & 0xff;

    encOut += base64s.charAt((bits & 0x00fc0000) >>18) +
              base64s.charAt((bits & 0x0003f000) >>12) +
              base64s.charAt((bits & 0x00000fc0) >> 6) +
              base64s.charAt((bits & 0x0000003f));
  }

  if(decStr.length -i > 0 && decStr.length -i < 3) {
    dual = Boolean(decStr.length -i -1);

    bits = ((decStr.charCodeAt(i++) & 0xff) <<16) |
           (dual ? (decStr.charCodeAt(i) & 0xff) <<8 : 0);

    encOut += base64s.charAt((bits & 0x00fc0000) >>18) +
              base64s.charAt((bits & 0x0003f000) >>12) +
              (dual ? base64s.charAt((bits & 0x00000fc0) >>6) : '=') +
              '=';
  }

  return(encOut);
}

function content_click(agof, prod){
	clickIvw(agof, prod);// globalAgofCode kommt aus headanalytics.tpl
	//urchinTracker(window.location.pathname);
	clickPot();
}
