
// PHP Path
var write_php = 'http://www.h-hakumu.com/cgi-bin/lapis/write.php'; 

// Domain
var domain = document.domain;

// Domain Check
var domainFlag = (write_php.match(domain)) ? true : false;

// Ajax Object
var xmlhttp = createXMLHttp();

// Encode Exists
var encode = (window.encodeURIComponent) ? true : false;

// Arguments
var args = '';

// RegExp Object
var regExp = new RegExp(domain + '|lapis/|ana/');

// Referrer
var referrer = parent.document.referrer;

if(referrer.match(regExp)){referrer = '';}

if(referrer){referrer = (encode) ? encodeURIComponent(referrer) : referrer.replace(/&/g,'::');}

args += '&referrer=' + referrer;

// Document Title
var title = (encode) ? encodeURIComponent(document.title) : '';

args += '&title=' + title;

// Document URL
var url = (encode) ? encodeURIComponent(document.URL) : document.URL.replace(/&/g,'::');

args += '&url=' + url;

// Color Depth
args += '&color_depth=' + screen.colorDepth;

// Screen Width
args += '&screen_width=' + screen.width;

// Screen Height
args += '&screen_height=' + screen.height;

// Client Width
var client_width = (window.innerWidth) ? parent.window.innerWidth : '';

if(!client_width){client_width = (document.documentElement.clientWidth) ? parent.document.documentElement.clientWidth : '';}
if(!client_width){client_width = (document.body.clientWidth) ? parent.document.body.clientWidth : '';}

args += '&client_width=' + client_width;

// Client Height
var client_height = (window.innerHeight) ? parent.window.innerHeight : '';

if(!client_height){client_height = (document.documentElement.clientHeight) ? parent.document.documentElement.clientHeight : '';}
if(!client_height){client_height = (document.body.clientHeight) ? parent.document.body.clientHeight : '';}

args += '&client_height=' + client_height;

// Cookie Enabled
var cookie = (navigator.cookieEnabled) ? navigator.cookieEnabled : '';

args += '&cookie=' + cookie;

// Ajax
if(xmlhttp && domainFlag)
{
	
	// Request URL
	var requestURL = write_php + '?act=js' + args;
	
	// Ajax Request
	requestLapis(requestURL);
	
	// onClick Event
	setEvent();
	
}

// <img>
else
{
	
	// Request URL
	var requestURL = write_php + '?act=img' + args;
	
	// Img Tag Write
	document.write('<img src="' + requestURL + '" width="0" height="0" id="a_img" alt="a_img" />');
	
}

//////////////////////////////////////////////////////////////

function setEvent()
{
	
	if(!document.getElementsByTagName || !xmlhttp){return;}
	
	var obj,link;
	
	var pageExt = /\.(htm|php|pl|cgi|jsp|asp)/i;
	var fileExt = /\.\w{2,4}$/i;
	
	var linksLength = document.getElementsByTagName('a').length;
	
	for(i = 0;i < linksLength;i++)
	{
		
		obj = document.getElementsByTagName('a')[i];
		
		link = obj.href;
		
		if(!link.match(domain)){obj.onmousedown = clickOutLink;}
		
		else if(!link.match(pageExt) && link.match(fileExt)){obj.onmousedown = clickFileLink;}
		
	}
	
}

//////////////////////////////////////////////////////////////

function clickOutLink()
{
	
	var outURL   = (encode) ? encodeURIComponent(this.href) : this.href;
	
	var outTitle = (encode) ? encodeURIComponent(this.innerHTML) : '';
	
	var requestURL = write_php + '?act=out_link&url=' + outURL + '&title=' + outTitle;
	
	requestLapis(requestURL);
	
}

//////////////////////////////////////////////////////////////

function clickFileLink()
{
	
	var fileURL = (encode) ? encodeURIComponent(this.href) : this.href;
	
	var requestURL = write_php + '?act=file_link&url=' + fileURL;
	
	requestLapis(requestURL);
	
}

//////////////////////////////////////////////////////////////

function createXMLHttp()
{
	
	var obj = (window.XMLHttpRequest) ? new XMLHttpRequest() : '';
	
	if(!obj){obj = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : '';}
	
	return obj;
	
}

//////////////////////////////////////////////////////////////

function requestLapis(requestURL)
{
	
	xmlhttp.open('GET', requestURL);
	
	xmlhttp.send(null);
	
}
