// Used by hostname and hostnameUnicode
function hostnameOnInput(input) {
	hostnameOnInputDelayed.applyDelayed(null, [input.value]);
}

function hostnameOnInputDelayed(hostname) {
	callServerFunction('hostnameToAddress', [hostname], hostnameToAddressCallback);
}

function hostnameToAddressCallback(hostname, hostnameUnicode, address) {
	var hostnameValue = document.getElementById('hostname').value;
	var hostnameUnicodeValue = document.getElementById('hostnameUnicode').value;
	if (hostname == hostnameValue  ||
	    hostname == hostnameUnicodeValue ||
	    hostnameUnicode == hostnameUnicodeValue ||
	    hostnameUnicode == hostnameValue) {

		currentHostname = hostname;
		currentHostnameUnicode = hostnameUnicode;
		currentAddress = address;
		updateOutput();
	}
}

function addressOnInput() {
	var address = document.getElementById('address').value;
	var n = dottedToInt(address);
	if (n) {
		currentAddress = address;
		currentHostname = false;
		currentHostnameUnicode = false;
		addressOnInputDelayed.applyDelayed(null, [address]);
		updateOutput();
	}
}

function addressOnInputDelayed(address) {
	callServerFunction('addressToHostname', [address], addressToHostnameCallback);
}

function addressToHostnameCallback(address, hostname, hostnameUnicode) {
	if (address == currentAddress) {
		currentHostname = hostname;
		currentHostnameUnicode = hostnameUnicode;
	}
	updateOutput();
}

function integerOnInput() {
	var n = parseInt(document.getElementById('addressInt').value);
	currentAddress = intToDotted(n);
	currentHostname = false;
	currentHostnameUnicode = false;
	addressOnInputDelayed.applyDelayed(null, [currentAddress]);
	updateOutput();
}


function netmaskOnInput() {
	var n = dottedToInt(document.getElementById('netmask').value) >> 0;
	if (typeof n == 'number') {
		for (var i = 0; i < 32; i++) {
			if (n & 1) {
				if (0xFFFFFFFFF >> i == n) {
					currentCidr = 32 - i;
					updateOutput();
				}
				break;
			}
			n = n >> 1;
		}
	}
}

function cidrOnInput() {
	var cidr = parseInt(document.getElementById('cidr').value);
	if (0 <= cidr && cidr <= 32) {
		currentCidr = cidr;
		updateOutput();
	}
}

function updateOutput() {
	var addressInt = dottedToInt(currentAddress);
	var mask = 0xFFFFFFFF << (32 - currentCidr);
	var network = addressInt & mask;
	var broadcast = addressInt | (0xFFFFFFFF ^ mask);
	document.getElementById('address').value = currentAddress ? currentAddress : '';
	document.getElementById('hostname').value = currentHostname ? currentHostname : '';
	document.getElementById('hostnameUnicode').value = currentHostnameUnicode ? currentHostnameUnicode : '';
	document.getElementById('addressInt').value = addressInt ? addressInt : '';
	document.getElementById('netmask').value = intToDotted(mask);
	document.getElementById('cidr').value = currentCidr;
	document.getElementById('outputNetwork').innerHTML = intToDotted(network) + '/' + currentCidr;
	document.getElementById('outputBroadcast').innerHTML = intToDotted(broadcast);
}


var regexpDotted = /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/;
function dottedToInt(dotted) {
	var decimals = regexpDotted.exec(dotted);
	if (!decimals || decimals.length != 5) {
		return false;
	}
	//var n = 0;
	var hex = '';
	for (var i = 1; i <= 4; i++) {
		var d = parseInt(decimals[i]);
		if (isNaN(d) || d > 0xFF) {
			return false;
		}
		//n = (n << 8) + d;
		hex += ('0' + d.toString(16)).right(2);
	}
	return parseInt(hex, 16);
	//return n;
}

function intToDotted(n) {
	var decimals = [];
	for (var i = 0; i < 4; i++) {
		decimals.unshift(n & 0xFF);
		n = n >> 8;
	}
	return decimals.join('.');
}

window.onload = function() {
	updateOutput();
}
