document.observe("dom:loaded", function() {
	search_tlds.each(function(e, i) {
		var post_body = 'search_tld=' + e;

		if (Object.isString(js_domains)) {
			post_body += '&domain=' + encodeURIComponent(js_domains);
		} else {
			js_domains[i].each(function(d) {
				post_body += '&domain[]=' + encodeURIComponent(d);
			});
		}

		new Ajax.Request('/system/domain/ajax_domain_get_availability/', {
			postBody: post_body,
			onSuccess: process_ajax_response
		});
	});
});

function process_ajax_response(response) {
   	var search_results = response.responseJSON;

    for (var key in search_results) {
		if (search_results.hasOwnProperty(key)) {
			setTimeout("show_availability('" + key + "', '" + search_results[key] + "')", Math.random() * 500);
		}
    }
}

function show_availability(destination, search_response) {
	var status = $(destination + '_image');
	var checkbox = $(destination + '_span');

	if (status == null || checkbox == null) {
		return;
	}

	if (search_response == 1) {
		status.update('Available');
		status.addClassName('status');

		checkbox.show();
		checkbox.down('input').disabled = false;
	} else {
		status.update('Registered');
		status.addClassName('registration_red');

		checkbox.hide();

		var input = checkbox.down('input');

		input.disabled = true;
		input.checked = false;
	}
}

function form_check() {
	selected_object = $$('input.domain_checkbox').find(function(e) {
		return e.checked;
	});

	if (typeof selected_object == 'undefined') {
		alert('Please select at least one domain');

		return false;
	} else {
		return true;
	}
}

function select_all(obj, total) {
	$$('input.domain_checkbox').each(function(e) {
		e.checked = obj.checked;
	});
}
