// JavaScript Document
function URLEncode(thetext)
{
	// The Javascript escape and unescape functions do not correspond
	// with what browsers actually do...
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";

	var plaintext = thetext;
	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			    alert( "Unicode Character '" 
                        + ch 
                        + "' cannot be encoded using standard URL encoding.\n" +
				          "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted." );
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for
 
	return encoded;
};

// ############################################
// call this function to do an async ajax call - pauses all other browser requests until the div has been replaced
// ############################################
function asyncXMLHTTPPost(url) {
  var statusDisplay ;

  var xmlHttpReq = false;
  var self = this;
  var tempString ;
  
  
  if (url.substring(0,1) == "/" || url.substring(0,1) == "#" || url.indexOf("bbrailjobs.com") > -1 || url.indexOf("togetherwe.co.uk") > -1 || url.indexOf("javascript:") > -1)
  { 
	return false;
  }
  else
  {
	 if (window.XMLHttpRequest) {
    // Mozilla/Safari
    self.xmlHttpReq = new XMLHttpRequest();
  }
  else if (window.ActiveXObject) {
    // IE
    self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
  }

  var params = "url="+URLEncode(url)

  self.xmlHttpReq.open("POST","/update_tracker.asp", false);
  self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  self.xmlHttpReq.setRequestHeader("Content-length", params.length);
  self.xmlHttpReq.setRequestHeader("Connection", "close");
  self.xmlHttpReq.send(params);
    if (self.xmlHttpReq.readyState == 4) {
     //responseText = self.xmlHttpReq.responseText ;
     //ajaxCallBack(divname, responseText) ;
    }  
  }
 
}




function LinkChecker(e) 
{ 

var the_href;
if( !e )
{
 //event details wasnt sent via the function (firefox)
    //and therefore we need to look in the event register
    if( window.event )
 //Internet Explorer 
 {
      the_href = event.srcElement.href;
	  the_type = event.srcElement.tagName;
	  the_parent = event.srcElement.parentNode;
    } 
 else
 //total failure, we have no way of referencing the event
 {
      return;
    }
}
else
{
  the_href = e.target.href;
  the_type = e.target.tagName;
  the_parent = e.target.parentNode;
}

if (the_type == 'A' || the_type == 'AREA')
{
	asyncXMLHTTPPost(the_href);
}
else if (the_type == 'IMG')
{
 the_parent_type = the_parent.tagName;
 if (the_parent_type == 'A')
 {
 the_parent_href = the_parent.href;
 asyncXMLHTTPPost(the_parent_href);
 }
}
else
{
return;
}
}

if (document.captureEvents && Event.CLICK) 
{
 document.captureEvents(Event.CLICK);
}
document.onclick=LinkChecker;
