var stopChecking = false;

function checkDomain()
{
	c = document.search;
	domainname = new String(trimString(c.domainname.value));
	var domainPat=new RegExp("^[a-zA-Z0-9\-.]+$");
	
	if(domainname.match(domainPat) == null)
	{
		return showError(c.domainname, E_ENTREDOMAINNAME );
	}
	
	if(c.search)
	{
		c.search.value = JS_CHECKING;
		c.search.disabled = true;
	}
	
	stopChecking = true;
	return true;
}

function checkDomainSelection()
{
	for(var i=0;document.getElementById('radio' + i); i++)
	{
		if(document.getElementById('radio' + i).checked)
		{	
			stopChecking = true;
			return true;
		}
	}
	
	for(i=0;document.getElementById('radio' + i); i++)
	{
		c = document.getElementById('radio' + i);
		if(c.disabled == false)
			return showError(c, E_PLEASESELECTDOMAIN);
	}
	
	alert(E_PLEASESELECTDOMAIN);
	return false;
}

function startChecking(id)
{
	if(document.getElementById('domain' + id) && !stopChecking)
		requestXmlHttp("http://www.u-d.com/whois.php", id, document.searchresult);
}

function updateResult(responseText, id)
{
	
	UpdateText =  document.getElementById('status' + id);

	if(responseText == "AVAILABLE")
	{
		UpdateText.innerHTML = JS_AVAILABLE;
		document.getElementById('radio' + id).disabled= false;
		document.getElementById('label' + id).innerHTML = "<font color='GREEN'>"+ document.getElementById('domain' + id).value +"</font>";
	}
	else
	{
		UpdateText.innerHTML = JS_NOTAVAILABLE;
		document.getElementById('label' + id).innerHTML = "<font color='MAROON'>"+ document.getElementById('domain' + id).value +"</font>";
	}
	startChecking(++id);
}

function requestXmlHttp(url, id, frm)
{
	var http_request = false; 
	
	if (window.XMLHttpRequest)			// Mozilla, Safari,... 
	{
		http_request = new XMLHttpRequest(); 
		if (http_request.overrideMimeType) 
			http_request.overrideMimeType('text/xml'); 
    } 
	else if (window.ActiveXObject)		// IE 
	{
		try 
		{ 
			http_request = new ActiveXObject("Msxml2.XMLHTTP"); 
		} 
		catch (e) 
		{ 
			try 
			{ 
				http_request = new ActiveXObject("Microsoft.XMLHTTP"); 
			} 
		   
			catch (e) {} 
		} 
	} 

	if (!http_request) 
	{ 
		alert("Browser doesn't support this feature.");
		return false; 
	} 
   
	http_request.onreadystatechange = function() 
	{ 
		if (http_request.readyState == 4) 
		{ 
			if (http_request.status == 200) 
			   updateResult(http_request.responseText, id);
			else 
		        alert('There was a problem with the request.(Code: ' + http_request.status + ')'); 
		} 
	} 
	
	http_request.open('POST', url, true); 
	//  DONOT change the next  statement else POST WON'T Work
    http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	
	str = null;
	str = getParameterString(frm, id);
	
	http_request.send(str);
}

function getParameterString(frm, id)
{
	var str = "";
	var sid = frm.sid.value;
	var domain = document.getElementById('domain' + id).value;
	
	str +=  "sid="+ sid +"&domain="+  domain;

	return str;
}
