
/**
 * Called when something is typed into the textbox
 */
function domainTypeEvent()
{
    var domainName = $('domainName').value;
    $('domainName').value = domainName.toLowerCase().replace(/([^0-9a-z\-])/g, '');

    if (typeof(domainTypeEvent.timeout) != 'undefined') {
        window.clearTimeout(domainTypeEvent.timeout);
    }

    domainTypeEvent.timeout = window.setTimeout('checkDomainName()', 200);
}

/**
 * Called when type timout passed
 */
function checkDomainName(force)
{
    var tlds = getTldsToCheck();

    // Clear the timeouts
    window.clearTimeout(checkDomainName.timeout);
    window.clearTimeout(domainTypeEvent.timeout);

    // Get the domain name
    var domainName = $('domainName').value;

    // If the name did not change, don't do anything
    if (domainName == checkDomainName.checkedDomainName && !force) {
        return;
    }

    checkDomainName.checkedDomainName = domainName;

    // Check the domain name validity
    if (domainName.length < 3) {
        for (var i = 0; i < tlds.length; i++) {
            var tld = tlds[i];

            $('rs_nfo_tld_' + tld).style.height = 'auto';

            $('rs_tld_' + tld).firstChild.style.backgroundColor = '#ece7e2';
            $('rs_nfo_tld_' + tld).innerHTML = '...';
        }

        checkDomainName.validDomainName = false;
        return;
    }

    // Create static variables if not available yet
    if (typeof(checkDomainName.cache) == 'undefined') {
        checkDomainName.cache = new Object();
    }

    if (typeof(checkDomainName.request) == 'undefined') {
        checkDomainName.request = null;
    }

    // If a request is running, abort it
    if (checkDomainName.request != null) {
        checkDomainName.request.transport.abort();
        checkDomainName.request = null
    }

    // Create the initial request
    if ($('message') != null) {
        $('message').style.display = 'none';
        $('results').style.display = 'block';
    }

    var data     = {name: domainName, tlds: {}};
    var uncached = 0;

    for (var i = 0; i < tlds.length; i++) {
        var tld = tlds[i];

        $('rs_nfo_tld_' + tld).style.height = 'auto';

        if (tld == 'de') {
            if (domainName.match(/^[0-9]*$/g) != null) {
                $('rs_tld_' + tld).firstChild.style.backgroundColor = '#dd0821';
                $('rs_nfo_tld_' + tld).innerHTML = lang['invalid'];

                continue;
            }
        }

        if (typeof(checkDomainName.cache[domainName + '.' + tld]) == 'undefined') {
            data.tlds[tld] = 'y';
            uncached++;
        }

        $('rs_tld_' + tld).firstChild.style.backgroundColor = '#ece7e2';
        $('rs_nfo_tld_' + tld).innerHTML = '<img src="media/images/indicator.gif" alt="' + lang['checking'] + '" />';
        $('rs_nfo_tld_' + tld).style.display = 'block';
    }

    // Convert the request to JSON and send it
    if (uncached > 0) {
        var jsonRequest = Object.toJSON(data);

        checkDomainName.request = new Ajax.Request(
            absoluteUri + 'index/domaincheck',
            {
                method:     'post',
                parameters: {'request': jsonRequest},
                onSuccess:  getResults
            }
        );
    } else {
        renderResults(data);
    }


    // Save the domain name and the validity check
    checkDomainName.validDomainName   = true;

    // Activate timeout
    checkDomainName.timeout = window.setTimeout('checkTimeout()', 1000 * 5);
}

/**
 * A check timeout occured
 */
function checkTimeout()
{
    checkDomainName.request.transport.abort();
    checkDomainName.request = null;

    var tlds = getTldsToCheck();

    for (var i = 0; i < tlds.length; i++) {
        var tld = tlds[i];

        $('rs_nfo_tld_' + tld).style.height = 'auto';
        $('rs_tld_' + tld).firstChild.style.backgroundColor = '#dd0821';
        $('rs_nfo_tld_' + tld).innerHTML = lang['timeout'];
    }
}

/**
 * Called by AJAX response
 */
function getResults(transport)
{
    window.clearTimeout(checkDomainName.timeout);

    var data = transport.responseText.evalJSON();

    checkDomainName.request = null;

    if (data.name != checkDomainName.checkedDomainName) {
        return;
    }

    renderResults(data);
}

/**
 * Render results
 */
function renderResults(data)
{
    var tlds = getTldsToCheck();

    // Set all results
    var maxHeight = 0;

    for (var i = 0; i < tlds.length; i++) {
        var tld = tlds[i];

        var rs_div_id      = 'rs_tld_' + tld;
        var rs_div_info_id = 'rs_nfo_tld_' + tld;

        // For now, the status is unknown
        var status = 'n';

        // Check if we have the status saved in the cache
        if (typeof(checkDomainName.cache[data.name + '.' + tld]) != 'undefined') {
            status = checkDomainName.cache[data.name + '.' + tld].a;
            sedo   = checkDomainName.cache[data.name + '.' + tld].s;
        } else {
            status = data.tlds[tld].a;
            sedo   = (data.tlds[tld].s == 'y');
            checkDomainName.cache[data.name + '.' + tld] = new Object();
            checkDomainName.cache[data.name + '.' + tld].a = status;
            checkDomainName.cache[data.name + '.' + tld].s = sedo;
        }

        $('rs_nfo_tld_' + tld).style.height = 'auto';

        if (status == 'a') { // Available
            $(rs_div_id).firstChild.style.backgroundColor = '#4ca6fd';
            $(rs_div_info_id).innerHTML = lang['available'];

            if (tldList[tld].length > 0) {
                $(rs_div_info_id).innerHTML += '<br />';
            }

            for (var j = 0; j < tldList[tld].length; j++) {
                var affiliate = tldList[tld][j];

                $(rs_div_info_id).innerHTML += '<br /><a href="' + absoluteUri + 'go/' + affiliate.forward + '" onclick="window.open(this.href); return false;">' + affiliate.name + '</a>';
            }
        } else if (status == 'u') { // Unavailable
            $(rs_div_id).firstChild.style.backgroundColor = '#dd0821';
            $(rs_div_info_id).innerHTML = lang['registered']

            if (sedo) {
                $(rs_div_info_id).innerHTML += '<br /><a href="' + absoluteUri + 'go/sedo/' + escape(data.name) + '.' + tld + '" onclick="window.open(this.href); return false;"><img src="' + absoluteUri + 'media/images/sedo.gif" width="80" height="15" alt="for sale" style="margin: 2px 0;" /></a>';
            } else {
                $(rs_div_info_id).innerHTML += '<br /><img src="' + absoluteUri + 'media/images/gui/blank.png" width="80" height="15" alt="" style="margin: 2px 0;" />';
            }

            $(rs_div_info_id).innerHTML += '<br /><a href="http://www.' + data.name + '.' + tld + '" onclick="window.open(this.href); return false;">' + lang['visit'] + '</a>';


            if (showWhois) {
                $(rs_div_info_id).innerHTML += '<br /><a href="#none" onclick="whois(\'' + data.name + '.' + tld + '\'); return false;">' + lang['whois'] + '</a>';
            }
        } else {
            if ($(rs_div_info_id).innerHTML != lang['invalid']) {
                $(rs_div_id).firstChild.style.backgroundColor = '#ece7e2';
                $(rs_div_info_id).innerHTML = '...';
            }
        }

        maxHeight = Math.max(maxHeight, $(rs_div_info_id).offsetHeight - 20);
    }

    // Align all boxes to the same height
    for (var i = 0; i < tlds.length; i++) {
        var tld = tlds[i];

        $('rs_nfo_tld_' + tld).style.height = maxHeight + 'px';
    }
}
