﻿// JScript File
var WhoIsXMLHttp = null;
var name = '';
var last_name = '';
var url_split = "getLiveWhoIs.aspx?domain=";
var TextBoxSearch = '';
var notChangedCounter = 0;
var maxCounter = 50;
var isTyping = false;


//window.onload = window_onload; 

function startScript()
{
	if (top.frames.length > 1){top.location=self.location;}
	window_onload();
	InitRss();
	InitSearchProvider(); 
}


function window_onload(e) 
{
	SetDivPosition();
    start_suggestion();
}

function BuyNow(qr, domain)
{	
	if(qr == "")
	{
		DisplayLightBox();
		DisplayModalChooseRegistrar(domain);
	}
	else
	{
		window.open('checkoutRedirect.aspx?qR=' + qr + '&qD=' + domain, 'buy' + domain.split('.')[0].replace('-', '').substring(0, 5));
	}
}

function SetDivPosition()
{
	var top = 0;
	var left = 0;
	var eElement = document.getElementById(TextBoxSearch);
	var outputDiv = document.getElementById('outDiv');
	
	left = DL_GetElementLeft(eElement);
	top = DL_GetElementTop(eElement);
		
	top += getElementHeight(eElement);
	top += 4;
	
	divWidth = getElementWidth(outputDiv);
	txtWidth = getElementWidth(eElement);
	
	left -= (divWidth-txtWidth)/2; 
	
	// Set Position
	
	
	outputDiv.style.left = left;
	outputDiv.style.top = top;
}

function getElementHeight(element) {
  return(element.clientHeight);
}

function getElementWidth(element) {
  return(element.clientWidth);
}

function DL_GetElementLeft(eElement)
{
	if (!eElement && this)                    // if argument is invalid
	{                                         // (not specified, is null or is 0)
		eElement = this;                       // and function is a method
	}                                         // identify the element as the method owner

	var DL_bIE = document.all ? true : false; // initialize var to identify IE

	var nLeftPos = eElement.offsetLeft;       // initialize var to store calculations
	var eParElement = eElement.offsetParent;  // identify first offset parent element

	while (eParElement != null)
	{                                         // move up through element hierarchy
		if(DL_bIE)
		{
			if(eParElement.tagName == "TD")     // if parent a table cell, then...
			{
				nLeftPos += eParElement.clientLeft; // append cell border width to calcs
			}
		}

		nLeftPos += eParElement.offsetLeft;    // append left offset of parent
		eParElement = eParElement.offsetParent; // and move up the element hierarchy
	}                                         // until no more offset parents exist
	return nLeftPos;                          // return the number calculated
}

function DL_GetElementTop(eElement)
{
	if (!eElement && this)                    // if argument is invalid
	{                                         // (not specified, is null or is 0)
		eElement = this;                       // and function is a method
	}                                         // identify the element as the method owner

	var DL_bIE = document.all ? true : false; // initialize var to identify IE

	var nTopPos = eElement.offsetTop;       // initialize var to store calculations
	var eParElement = eElement.offsetParent;  // identify first offset parent element

	while (eParElement != null)
	{                                         // move up through element hierarchy
		if(DL_bIE)
		{
			if(eParElement.tagName == "TD")     // if parent a table cell, then...
			{
				nTopPos += eParElement.clientTop; // append cell border width to calcs
			}
		}

		nTopPos += eParElement.offsetTop;    // append top offset of parent
		eParElement = eParElement.offsetParent; // and move up the element hierarchy
	}                                         // until no more offset parents exist
	return nTopPos;                          // return the number calculated
}



function start_suggestion()
{
	preLoading = new Image(); 
	preLoading.src = "images/loading.gif";
    CheckTyping();
}

function CheckTyping ()
{
	textBox = document.getElementById(TextBoxSearch);
    if(textBox.value != "Type in a domain, keyword or short sentence")    
	{
		// Changed?
		if (name != textBox.value)
		{ 
			isTyping = true;
			notChangedCounter = 0;
			// Is Empty?
			if (textBox.value == '')
			{
				var outputDiv = document.getElementById('ContentDiv');
				outputDiv.innerHTML = '';				
				HideOutDiv();
				HideLoading();
			}
			else
			{
				// Show Typing and reset counter
				ShowWhoIsTyping();
			}
			// refresh name
			name = textBox.value;
		}
		else if(notChangedCounter >=0)
		{
			notChangedCounter++;
		}
		
		if(notChangedCounter > maxCounter && name != '')
		{
			isTyping = false;
			ShowWhoIsLoading();			
			GetWhoIsResponse(url_split+name);
			notChangedCounter = -1;
		}
    }
    setTimeout ('CheckTyping();', 10);
}

function InitWhoIsXMLHttp()
{
    var A = null;
    try
    {
	    A = new ActiveXObject('Msxml2.XMLHTTP')
    }
    catch(e)
    {
	    try
	    {
		    A = new ActiveXObject('Microsoft.XMLHTTP')
	    }
	    catch(oc)
	    {
	        A = null
	    }
    }

    if(!A && typeof(XMLHttpRequest) != 'undefined')
    {
        A = new XMLHttpRequest()
    }

    return A
}

function ShowWhoIsLoading()
{
    /*var obj = document.getElementById('div_loading');
    obj.style.visibility = 'visible';*/
    var outputDiv = document.getElementById('ContentDiv');
	outputDiv.innerHTML = '<span class="Loading"><img src="images/loading.gif" /> Checking...<span>';
    ShowOutDiv();
}
function ShowWhoIsTyping()
{
    /*var obj = document.getElementById('div_loading');
    obj.style.visibility = 'visible';*/
    var outputDiv = document.getElementById('ContentDiv');
	outputDiv.innerHTML = '<span class="Loading"><img src="images/loading.gif" /> Typing...<span>';
    ShowOutDiv();
}

function HideLoading()
{
   /* var obj = document.getElementById('div_loading');
    obj.style.visibility = 'hidden';*/
   HideOutDiv();
}

function ShowOutDiv()
{
    var obj = document.getElementById('outDiv');
    obj.style.visibility = 'visible';
    SetDivPosition();
}

function HideOutDiv()
{
    var obj = document.getElementById('outDiv');
    obj.style.visibility = 'hidden';
}

function GetWhoIsResponse(url)
{
	if(isTyping)
		return;
	    
    if(WhoIsXMLHttp && (WhoIsXMLHttp.readyState != 0))
    {
	    WhoIsXMLHttp.abort()
    }

    WhoIsXMLHttp = InitWhoIsXMLHttp();

    if(WhoIsXMLHttp)
    {
		
        WhoIsXMLHttp.open("GET",url,true);
    	
	    //ShowWhoIsLoading();
			
	    WhoIsXMLHttp.onreadystatechange = function()
	    {
		    if(WhoIsXMLHttp.readyState==4)
    		{
    			if(WhoIsXMLHttp.status == 200)
    			{
    				if (WhoIsXMLHttp.responseText)
    				{
						var resp = WhoIsXMLHttp.responseText;
						
						textBox = document.getElementById(TextBoxSearch);
						if(textBox.value == '')
						{
							var outputDiv = document.getElementById('ContentDiv');
							outputDiv.innerHTML = '';
							
							HideOutDiv();
							HideLoading();
							return;
						}
						var outputDiv = document.getElementById('ContentDiv');
						
						var content = eval(resp);
												
						if(content[2] == textBox.value)
						{
							outputDiv.innerHTML = content[0];
							ShowOutDiv();
						}
						if(content[1] == "True")
						{							
							setTimeout ("GetWhoIsResponse('"+url_split+textBox.value+"');", 500);
						}
					}
	            }
	            else 
	            {
					/*var outputDiv = document.getElementById('ContentDiv');
					outputDiv.innerHTML = "<span class='LiveWhoIs'>"+WhoIsXMLHttp.statusText+"</span>";*/
					ShowOutDiv();
	            }
	            
		    }
		    
	    };
	    WhoIsXMLHttp.send(null);

    }
}