function whoisShowHide() {
    if ($("#whois-result").is(':visible')) {
        $("#whois-result").hide();
	$("#whois-expand a").html("развернуть");
    } else {
	$("#whois-result").show();
	$("#whois-expand a").html("свернуть");
    }
}

function whoisShow() {
    if (!$("#whois-result").is(':visible')) {
	$("#whois-result").show();
	$("#whois-expand a").html("свернуть");
    }
}

function whoisHide() {
    if ($("#whois-result").is(':visible')) {
        $("#whois-result").hide();
	$("#whois-expand a").html("развернуть");
    }
}

function whoisPopup(t) {
    if (!$("body").is(".popup-holder")) {
	$("body").append('<div class="popup-holder"><div class="bg"></div><div class="popup"><div class="t">&nbsp;</div><div class="c"><div class="close"><a href="#">&nbsp;</a></div><div class="text scrollable"><p>'+t+'</p></div></div><div class="b">&nbsp;</div></div></div>');
	$("body .popup-holder .close a").click(function() {
	    $("body .popup-holder").replaceWith("");
	    return false;
	});
	if (VSA_initScrollbars) {
	    VSA_initScrollbars();
	}
    }
}

function whoisFilter(n) {
   var Protocols = new Array("ftp://", "http://", "https://", "www.");
   var Prefixes = new Array("www.", "www2.");

    if (!n) return "";
    n = $.trim(n.toLowerCase());
    if (!n) return "";

    for (i=0;i<Prefixes.length;i++) {
    	var pos = n.indexOf(Protocols[i]);
    	if (pos == 0) {
    	    n = n.substr(Protocols[i].length);
    	    pos = n.indexOf("/");
    	    if (pos != -1) {
    	    	n = n.substr(0,pos);
    	    }
    	}
    }
    for (i=0;i<Prefixes.length;i++) {
    	var pos = n.indexOf(Protocols[i]);
    	if (pos == 0) n = n.substr(Protocols[i].length);
    }

    return n;
}

function whoisGetName(n) {
    n = whoisFilter(n);

    if (!n || n.length < 2) {
	return "";
    }    
    if (n[0] == '-' || n[n.length - 1] == '-') {
	return "";
    }
    for (i=0;i<n.length;i++) 
    {
	if (!((n[i] >= 'a' && n[i] <= 'z') ||
	      (n[i] >= '0' && n[i] <= '9'))) 
	{		    
	    if (n[i] == '-' || n[i] == '.') 
	    {
	        var prev = i - ((i > 0) ? 1 : 0);
	        var next = i + ((i<(n.length-1)) ? 1 : 0);
	        if (n[prev] == '-' || n[prev] == '.') return "";
	        if (n[next] == '-' || n[next] == '.') return "";
	    } 
	    else 
	    {
	        return "";
	    }
	}
    }
    return n;
}

function whoisNameLevel(n) {
    var level = 1;

    if (!n) {
	return 0;
    }
    for (i=0;i<n.length;i++) {
	if (n[i] == '.') {
	    level += 1;
	}
    }
    return level;
}

function whoisExtractName(n) {
    if (!n) {
	return "";
    }
    for (i=0;i<n.length;i++) {
	if (n[i] == '.') {
	    return n.substr(0,i);
	}
    }
    return n;
}

function whoisExtractTLD(n) {
    if (!n) {
	return "";
    }
    for (i=0;i<n.length;i++) {
	if (n[i] == '.') {
	    return n.substr(i + 1);
	}
    }
    return "";
}

function ValidateIPAddress(ip) {
    ip = ip.replace( /\s/g, "")
    var re = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/; 
    if (re.test(ip)) {
        var parts = ip.split(".");

        if (parseInt(parseFloat(parts[0])) == 0) return false;
        if (parseInt(parseFloat(parts[3])) == 0) return false;

        for (var i=0; i<parts.length; i++) {
            if (parseInt(parseFloat(parts[i])) > 255) return false;
        }
        return true;
    } 
    return false;
}


function whoisIsIP(n){
   n = whoisFilter(n);
   if (!n) return false;
   return ValidateIPAddress(n);
}

function whois_click() {
	var PriceList = null;
	var szDomain = "";
	var szName = "";
	var szTLD = "";
	var iLevel = 0;
	var iFieldId = 0;
	
	szDomain = $("#whois-name").val();
	if (!szDomain) {
	    return false;
	}
	if (whoisIsIP(szDomain) == true) {
	    $("#whois-list").html("");
   	    whoisHide();

	    $.ajax({
	        type: "POST",
		url: "/whois.php",
		data: "action=who&ip="+whoisFilter(szDomain),
		dataType: "text",
		success: function(m){
		    whoisPopup(m);
		}
	    });
	    return true;
	}
	szDomain = whoisGetName(szDomain);
	if (!szDomain) {
	    return false;
	}
	iLevel = whoisNameLevel(szDomain);
	if (iLevel == 0 || iLevel > 3) {
	    return false;
	}
	szName = whoisExtractName(szDomain);
	if (!szName) {
	    return false;
	}
	szTLD = whoisExtractTLD(szDomain);
	if (!szTLD && iLevel > 1) {
	    return false;
	}

	if (!PriceJSON) {
	    return false;
	}

	PriceList = $.parseJSON(PriceJSON);
	if (!PriceList || !PriceList["domains"]) {
	    return false;
	}

	$("#whois-list").html("");

	whoisShow();
	
	function whoisWho(argName,argTLD) {
	    if (!argName || !argTLD) {
		return false;
	    }
	    $.ajax({
	        type: "POST",
		url: "/whois.php",
		data: "action=who&name="+argName+"&tld="+argTLD,
		dataType: "text",
		success: function(m){
		    whoisPopup(m);
		}
	    });
	}

	function whoisAdd(argName,argTLD) {
	    var argTLDup = "";
	    var priceTLD = "0.00";
	    var iLiId = "";
	    var classLi = "";
	    
	    if (!argName || !argTLD) {
		return false;
	    }

	    argTLDup = argTLD.toUpperCase();

	    for(i=0;i<PriceList["domains"].length;i++)
	    {	
		if (PriceList["domains"][i] && 
		    PriceList["domains"][i]["new"] && 
		    PriceList["domains"][i]["new"][argTLDup])
		{
		    if (parseInt(PriceList["domains"][i]["new"][argTLDup]) < parseInt(priceTLD) || parseInt(priceTLD) == 0)
		    {
			priceTLD = PriceList["domains"][i]["new"][argTLDup];
		    }
		}
	    }

	    if (parseInt(priceTLD) == 0) {
	        return false;
	    }

	    iLiId = "whois_li_" + iFieldId;
	    classLi = ((iFieldId&1)?"odd":"even");
 	    iFieldId += 1;

	    $('<li id="'+iLiId+'" class="'+classLi+'"><span class="name">'+argName+'.'+argTLD+'</span>' +
	      '<span class="status none">&nbsp;</span><span class="price">от '+priceTLD+' руб.</span>' + 
	      '<span class="action">&nbsp;</span></li>').appendTo("#whois-list");

	    $.ajax({
	        type: "POST",
		url: "/whois.php",
		data: "name="+argName+"&tld="+argTLD,
		dataType: "JSON",
		success: function(m){
		    var AnswerJSON = $.parseJSON(m);
		    var free = false;
		    var busy = false;
		    
		    if (AnswerJSON && AnswerJSON["Result"] == "OK") {
		    	free = (AnswerJSON["Data"] == "FREE");
		    	busy = (AnswerJSON["Data"] == "BORROWED");
		    }

		    if (busy == true)
		    {
			$("#"+iLiId+" span.status").removeClass("none");
			$("#"+iLiId+" span.status").addClass("busy");
			$("#"+iLiId+" span.status").text("занят");
			$("#"+iLiId+" span.action").html('<a href="#">whois</a>');
			$("#"+iLiId+" span.action").addClass('whois');
			$("#"+iLiId+" span.action a").click(function () {
			    whoisPopup(AnswerJSON["Info"]);
			    //whoisWho(argName,argTLD);
			    return false;
			});
		    }
		    else if (free == true)
		    {
			$("#"+iLiId+" span.status").removeClass("none");
			$("#"+iLiId+" span.status").addClass("free");
			$("#"+iLiId+" span.status").text("свободен");
			$("#"+iLiId+" span.action").html('<a href="#">купить</a>');
			$("#"+iLiId+" span.action").addClass('buy');
			$("#"+iLiId+" span.action a").click(function () {
			    document.location.replace("https://my.ahost.ru/manager/billmgr?func=register&welcomfunc=domain.order&project=1");
			});
		    }
		}
	    });
	}

	whoisAdd(szName, szTLD);

	$("#whois-select .col .row input").each(function() {
	    var label = null;
	    var TLD = "";
	    
	    label = $("label[for="+$(this).attr("id")+"]");
	    if ($(label).hasClass('checkboxAreaCheckedLabel')) {
	    	TLD = $(label).html();
	    	if (!szTLD || TLD != szTLD) {
		    whoisAdd(szName, TLD);
	    	}
	    }
	});
}

