/**
 * @author Kirk Ouimet
 * @website http://www.yougetsignal.com/tools/web-sites-on-web-server/
 * @copyright 2008 Kirk Ouimet Design. All rights reserved.
 */

var key = "";

function setKey() {
	key = prompt("Please enter your API key.", "");
}

/* AJAX request to get reverse IP DNS JSON information
–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*/
var ajaxRequest	= new Ajax.Request('', {});
var reverseIpDomainJsonData;
function reverseIpDomainCheck(remoteAddress) {
	document.getElementById('results').innerHTML = "<p><img src=\"/img/loader.gif\" alt=\"Please wait...\" title=\"Please wait...\" style=\"width: 2.6875em; height: 0.6875em;\" /> Checking "+remoteAddress+". This may take a up to three minutes...</p>"	;
	ajaxRequest.transport.abort(); // Cancel the previous request
	var url = "/tools/web-sites-on-web-server/php/get-web-sites-on-web-server-json-data.php";
	new Ajax.Request(url, {
		method: 'post',
		parameters: {'remoteAddress': remoteAddress, 'key': key},
		onFailure: function(transport) {
			document.getElementById('results').innerHTML = "<p><img src=\"/img/flag_red.gif\" alt=\"\" style=\"height: 1em; width: 1em;\" />&nbsp;<span style=\"color: #DF454B;\">Service currently unavailable.</span></p>";
		},
		onException: function(transport) {
			document.getElementById('results').innerHTML = "<p><img src=\"/img/flag_red.gif\" alt=\"\" style=\"height: 1em; width: 1em;\" />&nbsp;<span style=\"color: #DF454B;\">Service currently unavailable.</span></p>";
		},		
		onSuccess: function(transport) {
			reverseIpDomainJsonData = transport.responseText.evalJSON(); // Place the results into JSON
			if(reverseIpDomainJsonData.status != "Fail" && reverseIpDomainJsonData.domainCount != 0) { // The request was successful
				if(reverseIpDomainJsonData.domainCount > 999) {
					domainCountText = "over ";
				}
				else {
					domainCountText = "";					
				}
				if(reverseIpDomainJsonData.domainCount == 1) {
					domainPluralityText = "domain";
				}
				else {
					domainPluralityText = "domains";					
				}
				if(reverseIpDomainJsonData.remoteAddress != reverseIpDomainJsonData.remoteIpAddress) {
					resultsSummaryHTML = "<p style=\"margin-bottom: .5em;\"><img src=\"/img/flag_green.gif\" alt=\"\" style=\"height: 1em; width: 1em;\" />&nbsp;Found "+domainCountText+"<b>"+reverseIpDomainJsonData.domainCount+"</b> "+domainPluralityText+" hosted on the same web server as <a href=\"http://"+remoteAddress+"\" target=\"_blank\">"+reverseIpDomainJsonData.remoteAddress+"</a> ("+reverseIpDomainJsonData.remoteIpAddress+").</p>";
				}
				else {
					resultsSummaryHTML = "<p style=\"margin-bottom: .5em;\"><img src=\"/img/flag_green.gif\" alt=\"\" style=\"height: 1em; width: 1em;\" />&nbsp;Found "+domainCountText+"<b>"+reverseIpDomainJsonData.domainCount+"</b> "+domainPluralityText+" hosted on the same web server as <a href=\"http://"+remoteAddress+"\" target=\"_blank\">"+reverseIpDomainJsonData.remoteAddress+"</a>.</p>";					
				}
				// List all of the domains
				profaneDomainExists = false;
				resultsHTML = "";
				for(i = 0; i < reverseIpDomainJsonData.domainArray.length; i++) {
					if(reverseIpDomainJsonData.domainArray[i][1] == 1) { // Check to see if it is a profane domain
						resultsHTML += "<p style=\"font-size: .8em; float: left; width: 360px;\"><a href=\"http://"+reverseIpDomainJsonData.domainArray[i][0]+"\" target=\"_blank\" style=\"color: #DF454B;\">"+reverseIpDomainJsonData.domainArray[i][0]+"</a></p>\n";						
						profaneDomainExists = true;
					}
					else {
						resultsHTML += "<p style=\"font-size: .8em; float: left; width: 360px;\"><a href=\"http://"+reverseIpDomainJsonData.domainArray[i][0]+"\" target=\"_blank\">"+reverseIpDomainJsonData.domainArray[i][0]+"</a></p>\n";
					}
				}
				if(profaneDomainExists == true) {
					resultsSummaryHTML = resultsSummaryHTML + "<p style=\"font-size: .8em; margin-bottom: 1em; border: 1px solid #DF454B; padding: .5em;\">It appears that the web server located at "+reverseIpDomainJsonData.remoteIpAddress+" may be hosting one or more web sites with explicit content. The web sites in question are highlighted in red below. There is a possibility that all of the web sites on this web server may be blocked by web filtering software. Search engine rankings for these web sites may be affected as well.</p>";
				}
				document.getElementById('results').innerHTML = resultsSummaryHTML + resultsHTML;
			}
			else { // There was a problem
				document.getElementById('results').innerHTML = "<p><img src=\"/img/flag_red.gif\" alt=\"\" style=\"height: 1em; width: 1em;\" />&nbsp;<span style=\"color: #DF454B;\">" + reverseIpDomainJsonData.message + "</span></p>";
			}
		}
	});
}

/* Submit AJAX request using enter key
–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*/
function submitUsingEnter(e) {
	var characterCode;
	if(e && e.which){ // If which property of event object is supported (NN4)
		e = e;
		characterCode = e.which // Character code is contained in NN4's which property
	}
	else {
		e = event;
		characterCode = e.keyCode; // Character code is contained in IE's keyCode property
	}
	
	if(characterCode == 13){ // If generated character code is equal to ASCII 13 (the enter key)
		reverseIpDomainCheck(document.getElementById('remoteAddress').value);
		return false;
	}
	else {
		return true;
	}
}

/* Function for additional text to appear
–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*/
var showMoreInfoSwitch = "off";
function moreInfoSwitch() {
	if(showMoreInfoSwitch == "off") {
		new Effect.BlindDown('moreToolInfo', { afterFinish: redrawElement, queue: 'end' });
		showMoreInfoSwitch = "on";
		document.getElementById('moreInfoSwitch').innerHTML = "<a onclick=\"moreInfoSwitch();\">Less about this tool</a>.";
	}
	else {
		new Effect.BlindUp('moreToolInfo', { queue: 'end' });		
		showMoreInfoSwitch = "off";
		document.getElementById('moreInfoSwitch').innerHTML = "<a onclick=\"moreInfoSwitch();\">More about this tool</a>.";
	}
}
// I wasted an hour of my life on this. Redraw the element so IE renders the page correctly after the effect completes
function redrawElement() {
	myElement = document.getElementById('moreToolInfo');
	myElement.style.display = 'none';
	myElement.style.display = 'block';
}