function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function resultError (request)
{
	alert('Error ' + request.status + ' -- ' + request.statusText + ' -- ' + request.responseText);
}

function updateStatus (tld, status)
{
	document.getElementById(tld+"status").innerHTML = status;
}

function checkdomain (domain)
{	
	document.getElementById("results").innerHTML = domainCheckTable(domain);
	
	for(i=0; i<tlds.length; i++)
	{
		setTimeout("newRequest('"+domain+"', '"+tlds[i]['shortCode']+"')", 250 * ((i/0.3) + 1));
	}
}

function newRequest (domain, tld)
{
	updateStatus (tld, '<img src="/images/busywaiting.gif" alt="hold on, busy checking..." /> checking...');
	
	new Ajax.Request("/lounge/dmchk/"+domain+"/"+tld+"/", 
	{
		method:"get",
		asynchronous:true,
		onSuccess:showDomainStatus,
		onFailure:resultError
	});
}

function showDomainStatus (request)
{	
	if (request.readyState==4) {
		var jsonData = eval('('+request.responseText+')');
		
		updateStatus (jsonData.tld, jsonData.status);
	}
}

function domainCheckTable (domain)
{
	var output = '<table width="100%">';
	output += '<tr><td width="30%"><b>domein</b></td><td width="30%"><b>status</b></td><td width="10%"><b>prijs</b></td>';
	output += '<td width="10%"><b>periode</b></td>';
	output += '<td width="20%"><b>eenmalige kosten</b></td>';
	output += '</tr>';
	
	for(i=0; i<tlds.length; i++)
	{
		output += domainCheckTableItem (domain, tlds[i]["shortCode"], tlds[i]["priceOnce"], tlds[i]["pricePeriodic"], tlds[i]["period"], '<img src="/images/queued.png" alt="I\'m in the queue... hold on!" /> queued');
	}
	
	output += "</table>";
	
	return output;
}

function domainCheckTableItem (domain, tld, priceOnce, pricePeriodic, period, status)
{
	var output = '<tr><td>'+domain+'.'+tld+'</td><td>';
	output += '<span id="'+tld+'status">'+status+'</span></td>';
	output += '<td>'+pricePeriodic+'</td><td>'+period+'</td>';
	output += '<td>'+priceOnce+'</td></tr>';
	
	return output;
}

function list (order, type)
{
	new Ajax.Request("/lounge/js/prijslijst/"+order+"/"+type+"/", 
	{
		method:"get",
		asynchronous:true,
		onSuccess:showPrices,
		onFailure:resultError
	});
}

function exoticList (order, type)
{
	new Ajax.Request("/lounge/js/prijslijstexotisch/"+order+"/"+type+"/", 
	{
		method:"get",
		asynchronous:true,
		onSuccess:showExoticPrices,
		onFailure:resultError
	});
}
function showExoticPrices (request)
{	
	if (request.readyState==4) {
		document.getElementById("orderresult").innerHTML = '';
		
		var jsonData = eval('('+request.responseText+')');
		
		var output = '<table width="100%">';
				
		if(request.responseText.length==2)
		{
			output += '<tr><td>Er is een fout opgetreden</td></tr>';
		}else
		{
			output += displayList ("<strong><a name=\"ext\" href=\"javascript:exoticList('shortCode', 'ASC');\">domeinextensie</a>/<a href=\"javascript:exoticList('orderNo', 'ASC');\">populariteit</a>:</strong>", "<strong><a href=\"javascript:exoticList('pricePeriodic', 'ASC');\">je betaalt per jaar</a>:</strong>", "<strong><a href=\"javascript:exoticList('priceOnce ASC, pricePeriodic', 'ASC');\">en &#233;&#233;nmalig</a>:</strong>");
		}
		
		for(i=0; i<jsonData.length; i++)
		{
			output += displayList (jsonData[i].ext, jsonData[i].price, jsonData[i].once);
		}
		
		output += "</table>";
		
		document.getElementById("orderresult").innerHTML = output;
	}
}
function displayList (ext, price, once)
{
	var output = "<tr>";
	output += "<td>"+ext+"</td>";
	output += "<td>"+price+"</td>";
	output += "<td>"+once+"</td>";
	output += "</tr>";
	
	return output;
}

function showPrices (request)
{	
	if (request.readyState==4) {
		document.getElementById("orderresult").innerHTML = '';
		
		var jsonData = eval('('+request.responseText+')');
		
		var output = '<table width="100%">';
				
		if(request.responseText.length==2)
		{
			output += '<tr><td>Er is een fout opgetreden</td></tr>';
		}else
		{
			output += displayList ("<strong><a name=\"ext\" href=\"javascript:list('shortCode', 'ASC');\">domeinextensie</a>/<a href=\"javascript:list('orderNo', 'ASC');\">populariteit</a>:</strong>", "<strong><a href=\"javascript:list('pricePeriodic', 'ASC');\">je betaalt per jaar</a>:</strong>", "<strong><a href=\"javascript:list('priceOnce ASC, pricePeriodic', 'ASC');\">en &#233;&#233;nmalig</a>:</strong>");
		}
		
		for(i=0; i<jsonData.length; i++)
		{
			output += displayList (jsonData[i].ext, jsonData[i].price, jsonData[i].once);
		}
		
		output += "</table>";
		
		document.getElementById("orderresult").innerHTML = output;
	}
}

function startLytebox(href, title, rel) {
      var anchor = this.document.createElement('a');
      anchor.setAttribute('title', title);
      anchor.setAttribute('href', href);
      anchor.setAttribute('rel', 'lyteframe');
      anchor.setAttribute('rev', 'width: 415px; height: 400px; scrolling: auto;');
      myLytebox.start(anchor, false, true);
}
