var loadimg = '<div style="text-align:center"><img src="/images/loader.gif" /></div>';
var loadimg_small = '<div style="text-align:center"><img src="/images/loader_small.gif" /></div>';
var sid;
var detail_tld;
var img_ok = new Image();
var img_error = new Image();
var img_loader = new Image();
var img_loader_small = new Image();

img_ok.src = '/images/icons/ok.png';
img_error.src = '/images/icons/notok.png';
img_loader.src = '/images/loader.gif';
img_loader_small.src = '/images/loader_small.gif';

$('script').each(function () {
	if (this.src.indexOf("?") != -1) sid = this.src.substr(this.src.indexOf("?")+1);
});

function doSimpleRequest(url, id, show_loader) {
	if (show_loader) $(id).html(loadimg);
	
	var response = $.ajax({
  		url: url,
		data: {sid: sid},
		type: "POST",
		async: false
	}).responseText;
	
	$(id).html(response);
	bindInfoIcon();
}

$(document).ready(function(){	
	// Functie om de link te openen wanneer op rij geklikt
	$(".tld_info").bind('click', function(){		
		location.href = $(this).find('a').attr('href');
	});
		
	$("#dialog").dialog({
		modal: true,
		autoOpen: false,
		resizable: false,
		buttons: {
			Ok: function() {
				$(this).dialog('close');
			}
		}
	});
	
	bindInfoIcon();

});

function processDomainStatus(domain, tld) {
	var _tld = tld.replace(".", "_");
	
	var x = $.ajax({
		beforeSend: function(){ $("#status_" + _tld).html(loadimg_small) },
		cache: false,
		url: "",
		data: { action: "check_availability", domain: domain + "." + tld, sid: sid },
		complete: function(response, status){
			if(status == 'error' || status == 'parsererror') $("#status_" + _tld).html('Fout: ' + status);
		},
		success: function(response){
			var status = $(response).find("status").text();
			if (status == 1000) {
				$("#status_" + _tld).html('<img src="/images/icons/ok.png" width="16" height="16" alt="Beschikbaar" />');
				$("#link_" + _tld).html('<b><a href="/bestellen/domeinregistratie/0/1/' + domain + '.' + tld + '">Registreren</a></b>');
			}
			else if (status == 2000) {
				$("#status_" + _tld).html('<img src="/images/icons/notok.png" width="16" height="16" alt="Niet beschikbaar" />');
				$("#link_" + _tld).html('<b><a href="/bestellen/domeinregistratie/0/2/' + domain + '.' + tld + '">Verhuizen</a></b>');
			}
			else if (status == 2100) {
				$("#status_" + _tld).html('<img src="/images/icons/notok.png" width="16" height="16" alt="Niet mogelijk" />');
				$("#link_" + _tld).html('<b>Incorrecte domeinnaam</b>');
			}
			else {
				$("#status_" + _tld).html('Controlefout');
				$("#link_" + _tld).html('');
			}
		}
	});
}

function bindInfoIcon() {
	$(".info_icon").click(function(){
		$("#dialog").dialog("option", "title", $(this).attr("title"));
		$("#dialog").html($(this).attr("alt").replace(/\\n/g,"<br />"));
		$("#dialog").dialog('open');
	});
}

$(document).unload(function(){
	x.abort();
});
