var req;

function loadXMLDoc(url, data)
{
	// branch for native XMLHttpRequest object
	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
		req.onreadystatechange = processReqChange;
		req.open("POST", url, true);
		req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		req.send(data);
		
		return true;
	// branch for IE/Windows ActiveX version
	} else if (window.ActiveXObject) {
		req = new ActiveXObject("Microsoft.XMLHTTP");
		if (req) {
			req.onreadystatechange = processReqChange;
			req.open("POST", url, true);
			req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
			req.send(data);
			
			return true;
		}
	}
	
	return false;
}

function processReqChange() 
{
	// only if req shows "complete"
	if (req.readyState == 4) {
		// only if "OK"
		if (req.status == 200) {
			// ...processing statements go here...
			response = req.responseXML.documentElement;
			
			//alert(req.responseText);
			
			method = response.getElementsByTagName('method')[0].firstChild.data;
			result = response.getElementsByTagName('result')[0].firstChild.data;
			domain = response.getElementsByTagName('domain')[0].firstChild.data;
			full   = response.getElementsByTagName('full')[0].firstChild.data;
			tld    = response.getElementsByTagName('tld')[0].firstChild.data;
			
			eval(method + '(result, domain, tld)');
		} else {
			alert("There was a problem retrieving the XML data:\n" + req.statusText);
		}
	}
}

function checkDomain(action, domain, tld)
{
	var status = document.getElementById('lookupstatus');
	
	if (action == 'query') {
		if (domain == '') {
			alert('No domain name specified!');
			return false;
		}

		// Input mode
		url  = '/scripts/domainlookup.php';
		data = 'form_domain=' + domain + '&form_tld=' + tld + '&ajax=true';

		if (loadXMLDoc(url, data)) {
			document.domainlookupImage.src = '/images/domain_progress.gif';
			return false;
		}
		
		return true;
	} else {
		// Response mode
		if (action == '210') {
			status.innerHTML = '<a href="https://secure.aptushosting.com/modernbill/order/orderwiz.php?submit_domain=register&new_domain='+domain+'&new_tld_extension=com&add_domain='+domain+''+tld+'&domains_added=1&checked_domains[]=register|'+domain+'|com&submit="><img src="/images/domain_available.gif" width="188" height="80" alt=" Domain Available "></a>';
		} else {
			status.innerHTML = '<a href="/domain/whois.html"><img src="/images/domain_taken.gif" width="188" height="80" alt=" Domain Taken "></a>';
		} 
	} 
}