
var SitesList = {

	defaultClass : 'site',

	init  : function()
	{
		if (document.getElementById('sites') && document.getElementById('sites').getElementsByTagName('li')){
			var childs = document.getElementById('sites').getElementsByTagName('li');
			var classN = SitesList.defaultClass + 'close';
			for (var i = 0; i < childs.length; i++){
				if (childs[i].className == classN) childs[i].onclick = function(){ SitesList.click(this);  };
			}
		}
	},

	click : function(li)
	{
		li.className = SitesList.defaultClass + ((li.className == SitesList.defaultClass) ? 'close' : '');
	}

};

var formLiveReverseIP = {

	box  : false,

	div  : false,

	init : function()
	{
		formLiveReverseIP.box = document.getElementById('form-ip');
		formLiveReverseIP.div = formLiveReverseIP.box.parentNode;
		formLiveReverseIP.box.onkeyup = formLiveReverseIP.check;
		formLiveReverseIP.box.focus();
		formLiveReverseIP.check();
		document.getElementById('search').onsubmit = function(){ return formLiveReverseIP.valid(); };
	},

	valid       : function()
    {
		if (/^[.0-9]+$/.test(formLiveReverseIP.box.value)){
			if (!formLiveReverseIP.validIp()){
				formLiveReverseIP.error('Incorrect IP-address.');
				return false;
			}
		}
		else if (!formLiveReverseIP.validDomain()){
			formLiveReverseIP.error('Incorrect Domain Name or IP-address.');
			return false;
		}
		return true;
	},

	error       : function(msg)
	{
		if (msg){
			document.getElementById('msg').innerHTML = msg;
			document.getElementById('msg').style.display = 'block';
		}
		else
			document.getElementById('msg').style.display = 'none';
	},

	color       : function(color)
	{
		formLiveReverseIP.div.style.backgroundColor = color;
	},

	validIp     : function()
	{
		var arr = /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/.exec(formLiveReverseIP.box.value);
		if (arr){
			for (var i = 1; i < 5; i++) if (parseInt(arr[i]) < 0 || parseInt(arr[i]) > 255) return false;
			return true;
		}
		return false;
	},

    validDomain : function()
	{
		if (/^[0-9a-z]{1}[-.0-9a-z]*?\.[a-z]{2,6}$/i.test(formLiveReverseIP.box.value)){
			var arr = formLiveReverseIP.box.value.split('.');
			for (var i = 0; i < arr.length; i++) if (!arr[i].length) return false;
			return true;
		}
		return false;
	},

	check       : function()
	{
		if (formLiveReverseIP.validIp()){
			formLiveReverseIP.color('rgb(246,156,41)');
		}
		else if (formLiveReverseIP.validDomain()){
			formLiveReverseIP.color('rgb(70,184,229)');
		}
		else{
			formLiveReverseIP.color('rgb(236,71,17)');
		}
	}

};

var formLiveWhoIs = {

	box  : false,

	div  : false,

	init : function()
	{
		formLiveWhoIs.box = document.getElementById('form-domain');
		formLiveWhoIs.div = formLiveWhoIs.box.parentNode;
		formLiveWhoIs.box.onkeyup = formLiveWhoIs.check;
		formLiveWhoIs.box.focus();
		formLiveWhoIs.check();
		document.getElementById('search').onsubmit = function(){ return formLiveWhoIs.valid(); };
	},

	valid       : function()
    {
		if (!formLiveWhoIs.validDomain()){
			formLiveWhoIs.error('Incorrect Domain Name.');
			return false;
		}
		return true;
	},

	error       : function(msg)
	{
		if (msg){
			document.getElementById('msg').innerHTML = msg;
			document.getElementById('msg').style.display = 'block';
		}
		else
			document.getElementById('msg').style.display = 'none';
	},

	color       : function(color)
	{
		formLiveWhoIs.div.style.backgroundColor = color;
	},

	validDomain : function()
	{
		if (/^[0-9a-z]{1}[-0-9.a-z]*?\.[a-z]{2,}$/i.test(formLiveWhoIs.box.value)){
			return true;
		}
		return false;
	},

	check       : function()
	{
		if (formLiveWhoIs.validDomain()){
			formLiveWhoIs.color('rgb(70,184,229)');
		}
		else{
			formLiveWhoIs.color('rgb(236,71,17)');
		}
	}

};

window.onload = function()
{
	SitesList.init();
	if (document.getElementById('form-ip')) formLiveReverseIP.init();
	if (document.getElementById('form-domain')) formLiveWhoIs.init();
};
