var xmlHttp=false;//createXmlHttpRequest();
var mTimer=null;

var DomainIsFree="<span style='background-color: #00FF00'><b>Dom&auml;nen &auml;r ledig!</b></span>";
var DomainIsAlreadyRegistered="<span style='background-color: #FF0000'><b>Dom&auml;nen &auml;r upptagen!</b></span>";
var CantConnectToWhoisServer="<span style='background-color: #FFCC00'><b>Anslutningen till Whois-servern misslyckades.</b></span>";


function createXmlHttpRequest()
{
    //Mozilla-based browsers
    xmlHttp=false; 
    if(window.XMLHttpRequest)
    {
        xmlHttp = new XMLHttpRequest();
    } 
    else 
	    if (window.ActiveXObject){
		 var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
											"MSXML2.XMLHTTP.5.0",
											"MSXML2.XMLHTTP.4.0",
											"MSXML2.XMLHTTP.3.0",
											"MSXML2.XMLHTTP",
											"Microsoft.XMLHTTP");
			// try every prog id until one works
			for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++) 
			{
			  try 
			  { 
				// try to create XMLHttpRequest object
				xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
			  } 
			  catch (e) {}
			}
	    }
	    
	    
    if(!xmlHttp){
        alert("Can't create XMLHttpRequest object");
        return false;
    }
    else
    	return xmlHttp;
}

function sentRequest()
{
	createXmlHttpRequest();
	
	document.getElementById("ajaxProcess").innerHTML="Status : "+xmlHttp.readyState;

	if (xmlHttp.readyState==4 || xmlHttp.readyState==0)
	{
		xmlHttp.onreadystatechange = handleServerResponse;
		//xmlHttp.open("POST", "data.php", true);
		xmlHttp.open("POST", "whois.php", true);
		xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
		document.getElementById("ajaxProcess").innerHTML="Preparing request";
		document.getElementById("domain").disabled=true;
		document.getElementById("execBut").disabled=true;
		//document.getElementById("results").innerHTML=";
		queryString="";
		queryString+= ("domain=")+encodeURIComponent(document.getElementById("domain").value);
		
		for (i=0; i<=6; i++)
		{
			if (document.getElementById("tlds"+i).checked)
			{
				queryString += ("&tlds["+i+"]=on");
			}
		}
		document.getElementById("ajaxProcess").innerHTML="Sending request ";
		xmlHttp.send(queryString);
		document.getElementById("ajaxProcess").innerHTML="<br>Letar...";
	}
	else 
	{
		document.getElementById("ajaxProcess").innerHTML="Status: "+xmlHttp.readyState;
		mTimer=setTimeout("sentRequest()", 1000);
	}
}

function handleServerResponse() 
{
	if(xmlHttp.readyState == 4) 
	{
		if(xmlHttp.status == 200) 
		{

			xmlResponse=xmlHttp.responseXML;
			xmlDocumentElement=xmlResponse.documentElement;
			response=xmlDocumentElement;
			textOut="<table border=0 cellpadding=3 cellspacing=1 width='550px' style='font-size: 8pt'>";
			if (response==null)
			{
				textOut+="<tr>"
				textOut+="<td align=left><B style='color: #CC0000'> Incorrect Domain</b></td><td align=left></td></td>";
				//alert(":(");
			}
			else
			for (i=0; i<response.childNodes.length; i++)
			{
				var dType="";//response.childNodes[i].attributes[0].nodeValue;
				var sColor="#000000";
				var nDVal=false;
				switch (response.childNodes[i].attributes[0].nodeValue)
				{
					case "1":
						dType=DomainIsFree;
						sColor="#009900";
						nDVal=true;
					break;
					case "2":
						dType=DomainIsAlreadyRegistered;
						sColor="#990000";
					break;
					case "-1":
						dType=CantConnectToWhoisServer;
						sColor="#AE5C47";
					break;
				}


				var DomainName=((response.childNodes[i].textContent==undefined)?response.childNodes[i].text:response.childNodes[i].textContent);
				textOut+="<tr>";
				textOut+="<td align=left><B>"+DomainName+"</b></td><td align=left><span style='color: "+sColor+"'>"+dType+"</span></td>";
				textOut+="<td align=left>";
				if (nDVal)
				  textOut+="<a href='/it/order.php?tariff=web-entry&domain="+DomainName+"'>Best&auml;ll Entry</a>";
				
				textOut+="</td>";

				textOut+="<td align=left>";
				if (nDVal)
				  textOut+="<a href='/it/order.php?tariff=web-medium&domain="+DomainName+"'>Best&auml;ll Medium</a>";
				
				textOut+="</td>";

				textOut+="<td align=left>";
				if (nDVal)
				  textOut+="<a href='/it/order.php?tariff=web-advanced&domain="+DomainName+"'>Best&auml;ll Advanced</a>";
				
				textOut+="</td></tr>";
				
			}
			textOut+="</table>";

			document.getElementById("results").innerHTML = textOut;
			document.getElementById("ajaxProcess").innerHTML="<br>Resultat:<br>";
			document.getElementById("domain").disabled=false;
			document.getElementById("execBut").disabled=false;
			clearTimeout(mTimer);
		}
		else
		{
			document.getElementById("ajaxProcess").innerHTML="HTTP error : "+xmlHttp.statusText;
			document.getElementById("domain").disabled=false;
			document.getElementById("execBut").disabled=false;
		}
	}
}
