function $(id) { return document.getElementById(id); }

function ajax_sender() {
	if (this.request == '') {
		this.request = '?sid='+Math.random();	
	} else {
		this.request = this.request + '&sid='+Math.floor(Math.random()*99999999);	
	}
	this.xmlHttp.open('get', this.page + this.request);
	var xmlHttpObject = this.xmlHttp;
	var target = this.target;
	var type = this.type;
	var onstart = this.onstart;
	var onfinish = this.onfinish;

	try {
		eval(onstart);
	} catch(e) { }

	if (target != '') {
		this.xmlHttp.onreadystatechange = function() {
			if (xmlHttpObject.readyState == 4) {
				switch (type) {
					case 'select': {
						$(target).selectedIndex = find_index(target, xmlHttpObject.responseText);
						break;
					}
					case 'alert': {
						alert(xmlHttpObject.responseText);
						break;
					}
					case 'div': {
						try {
							$(target).innerHTML = xmlHttpObject.responseText;
						} catch(e) { }
						break;
					}
				}
				try {
					eval(onfinish);
				} catch(e) {

				}
			}
		};
	}
	this.xmlHttp.send(null);
}

function ajax(page, request, type, element, onstart, onfinish) {
	if (arguments.length < 2) {
		return;
	}
	this.target = element;
	this.page = page;
	this.type = type;
	if (request == null) {
		this.request = '';
	} else {
		this.request = request;
	}
	this.onstart = onstart;
	this.onfinish = onfinish;
	try {
		// Firefox, Opera 8.0+, Safari
		this.xmlHttp = new XMLHttpRequest();
	} catch (e) {
		// Internet Explorer
		try {
			this.xmlHttp = new ActiveXObject('Msxml2.XMLHTTP');
		} catch (e) {
			try {
				this.xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
			} catch (e) {
				alert('Your browser does not support AJAX!');
				return false;
			}
		}
	}
	this.send = ajax_sender;
	this.send();
}

function search_onstart(id) {
	$(id).innerHTML = '<span>-</span>';
}

function search_onfinish(id) {
	var point = new GLatLng($('result_x_'+id).value, $('result_y_'+id).value);
	var label = '<strong>Server</strong>: ' + $('result_name_'+id).innerHTML + '<br /><strong>DNS Value</strong>: ' + $('result_ip_'+id).innerHTML;
	var type = $('result_ip_'+id).innerHTML;
	var icon = new GIcon(baseIcon);
	switch (type) {
		case 'invalid hostname':
		case '':
			icon.image = "/images/cross.png";
			break;
		default:
			if ($('compare').value != '' && $('compare').value != 'undefined') {
				if (type == $('compare').value) {
					icon.image = "/images/tick.png";
				} else {
					icon.image = "/images/cross.png";
				}
			} else {
				icon.image = "/images/tick.png";
			}
			break;
	}
	$('result_status_icon_'+id).src = icon.image;
	map.addOverlay(createMarker(point, type, label, icon));
	counter++;
	if (counter == 13) {
		$('btnSearch').disabled = false;
	}
}

function search() {
	if ($('hostname').value != '' && $('hostname').value != 'www.example.com' && $('hostname').value != '72.14.207.99') {
		map.clearOverlays();
		counter = 0;
		$('hostname').value = $('hostname').value.replace('http://', '');
		$('hostname').value = $('hostname').value.replace('https://', '');
		fragment = '#'+$('type').value+'/'+$('hostname').value;
		if ($('compare').value != '' && $('compare').value != 'undefined') {
			fragment = fragment+'/'+$('compare').value;
		}
		document.location.hash = fragment;
		try { _gaq.push(['_trackPageview', '/search/'+$('type').value+'/'+$('hostname').value]); } catch(e) { } 
		$('btnSearch').disabled = true;
		for (i = 0; i < 100; i++) {
			try {
				temp = $('result_ip_'+i).innerHTML;
				ajax('/ajax.php', '?id='+i+'&hostname='+$('hostname').value+'&type='+$('type').value, 'div', 'result_ip_'+i, 'search_onstart(\'result_ip_'+i+'\')', 'search_onfinish(\''+i+'\')');
			} catch(e) { }
		}
	}
	return false;
}

function hostname_focus() {
	if ($('hostname').value == 'www.example.com' || $('hostname').value == '72.14.207.99') {
		$('hostname').value = '';
		$('hostname').className = 'hostname_focus';
	}
}

function hostname_blur() {
	if ($('hostname').value == '') {
		$('hostname').className = 'hostname';
		if ($('type').value == 'PTR') {
			$('hostname').value = '72.14.207.99';
		} else {
			$('hostname').value = 'www.example.com';
		}
	}
}

function type_change() {
	if ($('type').value == 'PTR') {
		if ($('hostname').value == 'www.example.com') {
			$('hostname').value = '72.14.207.99';
		}
	} else {
		if ($('hostname').value == '72.14.207.99') {
			$('hostname').value = 'www.example.com';
		}
	}
}

function contact_finish() {
	$('contact').innerHTML = unescape($('contact').innerHTML);
}

function load() {
	$('hostname').value = 'www.example.com';
	$('type').options[0].selected = true;
	$('btnSearch').disabled = false;
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"));
		map.setCenter(new GLatLng(17, 0), 1);
		map.disableDragging();
	}
	if (document.location.hash.length > 1) {
		fragment = document.location.hash.substr(1).split('/');
		$('hostname').value = fragment[1];	
		for (i = 0; i < $('type').options.length; i++) {
			if ($('type').options[i].value == fragment[0]) {
				$('type').options[i].selected = true;
				break;
			}
		}
		$('compare').value = fragment[2];	
		search();
	}
}

function unload() {
	GUnload();
}

function createMarker(point, type, label, icon) {
	// Set up our GMarkerOptions object
	markerOptions = { icon:icon };
	var marker = new GMarker(point, markerOptions);

	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml(label);
	});

	return marker;
}

var counter = 0;
var map;
var baseIcon = new GIcon();
baseIcon.iconSize = new GSize(10, 10);
baseIcon.iconAnchor = new GPoint(5, 5);
baseIcon.infoWindowAnchor = new GPoint(9, 2);
