
function sort_mx(a, b) 
{
	//alert( 'comparing '+a.host+' and '+b.host);
	if ( a.weight > b.weight ) return 1;
	else
	if ( a.weight < b.weight ) return -1;
	else
	if ( a.host.toLowerCase() > b.host.toLowerCase() ) return 1;
	else
	if ( a.host.toLowerCase() < b.host.toLowerCase() ) return -1;
	else
	return 0;
}

function mx( host, weight )
{
	this.host=host;
	this.weight=parseInt(weight);
}

function lookup_mx( f, onload )
{
	var mxdoc = document.forms[0]; //DOMViewerObj=mxdoc; window.open("/domviewer.html");

	for( i=0; i<mxdoc.mx.length; i++ )
	{
		mxdoc.mx[i].value='';
		mxdoc.mxw[i].value='';
	}

	// get hold of the hidden iframe.
	dns=document.getElementById('mxlookup');

	if ( !onload )
	{
		dns.src='/sp98/customer/acctmgr-checkdomain.php?name='+f.form.newdomain.value;
		return 0;
	}
	else
	if ( mxdoc.newdomain.value.length )
	{
		//DOMViewerObj=dns; window.open("/domviewer.html");
		//text=dns.contentDocument.body.innerHTML;
		text=dns.contentWindow.document.body.innerHTML;

		resetError( mxdoc.newdomain );
		if ( text.length>7 )	// the emptydoc that we load by default= "<p></p>".
		{
			var mxarray=new Array();

			entry=text.split(/\s+/);

			for( i=0; entry[i]!=undefined && entry[i].length>0; i+=2 )
			{
				mxarray[i/2]=new mx(entry[i],entry[i+1]);
			}

			mxarray.sort( sort_mx );
				
			for( i=0; i<mxarray.length && i<mx.length; i++ )
			{
				mxdoc.mx[i].value=mxarray[i].host;
				mxdoc.mxw[i].value=mxarray[i].weight;
			}

			//DOMViewerObj=mxdoc; window.open("/domviewer.html");
		}
		else 
		setError( mxdoc.newdomain, 'Unable to retrieve current MX definitions for "'+mxdoc.newdomain.value+'"' );
	}
}
