var processing;
var domainResponse;

addLoadEvent(function() {
	processing = getElement('processing');
	domainResponse = getElement('domainResponse');
});

function checkDomain(path) {
	
	var sld = checkForm.check_sld.value;
	
	if(sld == '') {
		domainResponse.style.color = "red";
		domainResponse.innerHTML = "Please enter a domain name";
		domainResponse.style.display = 'block';
	
	} else {
		
		var tld = checkForm.check_tld[checkForm.check_tld.selectedIndex].value;
		
		lockForm(true);
		hideResponse();
		processing.style.display = 'block';
		
		scriptPath = path;
		sendData = 'sld=' + sld + '&tld=' + tld;
		
		processAjax(_checkDomain);
	}
}


function _checkDomain(response) {

	lockForm(false);
	processing.style.display = 'none';
	
	if(response.substr(0,3) == '210') {
		
		// domain is available
		domainArray = response.split('.');
		domainResponse.style.color = "green";
		var domainMessage = "<img src='http://bridaltoolbox.com/images/available.png' alt='' /> " + domainArray[1] + "." + domainArray[2] + " is available";
		
		if(checkForm.sld) domainMessage += " <input type='button' value='Register' onclick=\"addDomain('"+domainArray[1]+"','"+domainArray[2]+"');\" />";
		
		domainResponse.innerHTML = domainMessage;
		
	} else if(response.substr(0,3) == '211') {
		
		// domain is not available
		domainArray = response.split('.');
		domainResponse.style.color = "red";
		domainResponse.innerHTML = "<img src='http://bridaltoolbox.com/images/unavailable.png' alt='' /> " +domainArray[1] + "." + domainArray[2] + " is not available";
		
	} else {
		domainResponse.style.color = "red";
		domainResponse.innerHTML = "An error occurred:<br />" + response;
	}

	domainResponse.style.display = 'block';
}

function hideResponse() {
	
	domainResponse.innerHTML = '';
	domainResponse.style.display = 'none';
}

function lockForm(state) {

	checkForm.domainCheck.disabled = state;
	checkForm.check_sld.disabled = state;
	checkForm.check_tld.disabled = state;
}

function addDomain(sld, tld) {

	checkForm.sld.value = sld;
	checkForm.tld.value = tld;
	checkForm.submit();
}