// ==UserScript==
// @name          Server Info
// @namespace     http://iescripts.org
// @description	  Display the Websites IP Address, Web Server and last modified time
// @statussize    84
// ==/UserScript==

(function()
{
	var plugin = PRO_plugin(@name);
	plugin.onpagechange = handlePageChange;
	plugin.registerContextMenu("Site uptime/WebServer", menuShowNetcraft);
	plugin.registerContextMenu("Domain Name Owner", menuShowDNSOwner);
	plugin.registerContextMenu("Server Location", menuShowServerLocation);
	
	
	var gTabId = "";
	var gFindIP = "";
	var gFindServer = "";
	/*
	var reqObj = new Object();
	reqObj.request = function(callback, tabId, url, userAgent, lastModify,qtype) {
		var req;
		req = PRO_xmlhttpRequest();
		req.onreadystatechange = function() {
			if (req.readyState == 4) {// completed
				if (req.status < 400) {// only if "OK"
					try {
						callback(req, tabId);
					} catch(err) {
					}
				} else {
					// PRO_log("There was a problem loading data :\n" + req.status+ "/" + req.statusText);
				}
				setTimeout(function() {req.forceClean();req = null;}, 1000);
			}
		}
		try {
			req.open(qtype, url);
			req.setRequestHeader( "User-Agent", userAgent);
			if(lastModify.length > 0)
				req.setRequestHeader( "If-Modified-Since", lastModify);
			req.send(null);
        
		} catch(err){ 
		}
		return req;
	}*/

	// escape the url
	function encodeURL(url)
	{
		return escape(url).replace(/\+/g, '%2B').replace(/\"/g, '%22').replace(/\'/g, '%27');
	}
	
	function menuShowNetcraft(tabId,url) {
		var urlhost = url.split("/");
		var qhost = urlhost[2];
		PRO_openInTab("http://uptime.netcraft.com/up/graph/?host="+qhost,1);
	}

	function menuShowDNSOwner(tabId,url) {
		var urlhost = url.split("/");
		var qhost = urlhost[2];
		PRO_openInTab("http://www.dnsstuff.com/tools/whois.ch?ip="+qhost+"&src=ShowIP",1);
	}

	function menuShowServerLocation(tabId,url) {
		var urlhost = url.split("/");
		var qhost = urlhost[2];
		PRO_openInTab("http://ip2country.esymbian.info/?host="+qhost,1);
	}

	function handlePageChange(tabId, url, state) {
		var urlhost = url.split("/");
		var qhost = urlhost[2];
		gTabId = tabId;

		if(state == 1) {// page loading
			gFindIP =  plugin.getServerIp(qhost);
			plugin.setStatusInfo("", gFindIP, "", gTabId);					
		}/* else if(state == 2) {// page loaded
			gFindIP =  plugin.getServerIp(qhost);
		 	gSIP = gFindIP.split(".");
			gS1 = gSIP[0];
			gS2 = gSIP[0] + gSIP[1];

			if(gS2 == "192168" || gS1 == "10") {
				gFindServer = "Intranet Site";
				processResult();
				return;
			}
			if(plugin.getTabValue(tabId,"serverinfo_oldip") != gFindIP) {
				reqObj.request(processFindServerData, tabId, url, "User-Agent", "Mozilla/4.0 (compatible; Windows XP 5.1)","HEAD");
			}else{
				gFindServer = plugin.getTabValue(tabId,"serverinfo_oldserver");
				processResult();
			}
		}*/
	}

/*
	function processFindServerData(req, tabId) {
		var lastm = req.getResponseHeader('Last-Modified');
		var webserver = req.getResponseHeader('Server');
		
		if(gTabId == tabId) {
			var FindServer = "<b>Web Server:</b> " + webserver + "<br><b>Last Modified:</b> " + lastm;
			gFindServer = FindServer;
			plugin.setTabValue(tabId,"serverinfo_oldserver",gFindServer);
			plugin.setTabValue(tabId,"serverinfo_oldip",gFindIP);
			processResult();
		}else{
		}
	}

	function processResult()
	{
		var tips = gFindServer + "<br><b>IP:</b> " + gFindIP;
		plugin.setStatusInfo("", gFindIP, tips, gTabId);
	}
*/
}
)();


