/***************************************************************
*  Copyright notice
*
*  (C) 2008 Vlatko Surlan <vlatko.surlan@valdez.hr>
*  Sponsored by Valdez business services http://www.valdez.hr/en/
*  All rights reserved
*
*  This script is part of the TYPO3 project. The TYPO3 project is
*  free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*
*  The GNU General Public License can be found at
*  http://www.gnu.org/copyleft/gpl.html.
*
*  This script is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/

function modifyLinks() {
	//alert('modifyLinks');
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	for (var i=anchors.length-1; i>=0; i--) {
		var anchor = anchors[i];
		if (typeof(anchor.href) != 'undefined' &&
			anchor.href.length > 0 &&
			anchor.href.search(document.domain) == -1 &&
			anchor.href != '#') {anchor.onclick = function(){log(document.location, this.href);}
			//alert('Setting: '+'function(){log('+document.location+', '+anchor.href+');');
		}
	}
}

function log(origin, destination) {
	if (destination.search('linkTo_UnCryptMailto') != -1) {
		if (typeof(linkTo_UnCryptMailto) == 'function') {
			destination = destination.match(/'[^']+'/)[0];
			destination = destination.substring(1, destination.length - 1);
			offset = linkTo_UnCryptMailto.toString().match(/decryptString\(s,([^\)]+)/)[1];
			offset = parseInt(offset);
			destination = decryptString(destination,offset);
			//alert('destination: '+destination+' typeof: '+typeof(destination));
		}
	}
	
	//MV 2009_04_26 cut body part from mailto
	nCut = destination.search('&body');
	if (nCut != -1) {	
		destination = destination.substr(0,nCut);
	}
	
	//alert('destination: '+destination+' typeof: '+typeof(destination));
	//MV 2009_04_26 cut cHash part from origin
	
	/*
	nCut = origin.search('&cHash');	
	if (nCut != -1) {	
		origin = origin.substr(0,nCut);
	}	
	*/
	
	var postData = 'origin='+escape(origin)+'&destination='+escape(destination);
	var AJAX = null;
	if (window.XMLHttpRequest) {
		AJAX=new XMLHttpRequest();
	} else {
		AJAX=new ActiveXObject("Microsoft.XMLHTTP");
	}
	if (AJAX==null) { return false; } else {
		//AJAX.onreadystatechange = function() {}                                  
		AJAX.open("POST", "?type=5678", false);
		AJAX.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
		AJAX.setRequestHeader("Content-Length", postData.length);
		AJAX.setRequestHeader("Connection", "close");
		AJAX.send(postData);
		//alert('Sending: '+postData);
	}
}