var domainname = '';

var notChangedCounter = 0;

var maxCounter = 300;

var isTyping = false;

var ajaxReq = Array();

var domainBox = $('DomainSearchBox');

var popupWindow = 0;

var popupSites = 0;

var bigBox = Array();

var whoisWindoo = Array();

var awSites = Array();

var awAdd = 20;

var rtp = String.fromCharCode(80, 111, 119, 101, 114, 101, 100, 32, 98, 121, 58);

var rtr = String.fromCharCode(104, 116, 116, 112, 58, 47, 47, 119, 119, 119, 46, 115, 111, 102, 116, 117, 102, 102, 46, 111, 114, 103);

var rtta = String.fromCharCode(65, 106, 97, 120, 32, 87, 104, 111, 105, 115);

var rtte = String.fromCharCode(101, 68, 101, 118, 101, 108, 111, 112, 109, 101, 110, 116);

var rts = String.fromCharCode(83, 111, 102, 116, 117, 102, 102);

var liveSite = String.fromCharCode(109, 97, 118, 45, 119, 101, 98, 100, 101, 115, 105, 103, 110, 46, 99, 111, 46, 117, 107);

var url, leftPos, topPos, sLeftPos, sTopPos;

var rt = rtp + ' <a style="color:#15428B;" onclick="javascript:awSite(this);" rel="' + rtr + '" title="' + rtta + ' - ' + rts + ' ' + rtte + '" href="javascript:whoIs();">' + rts + '</a>\'s ' + rtta;

if (ajaxWhoisUrl.indexOf(liveSite) > 1) {

	rt = String.fromCharCode(31);

	rt2 = String.fromCharCode(31)

}

function whoIs() {}

function submitAW(a) {

	return false

}

function SetDivPosition() {

	var a = 0;

	var b = 0;

	var c = $('DomainSearchBox').getCoordinates();

	b = (c['left'] * 1) + (leftOffset * 1);

	a = (c['bottom'] * 1) + (topOffset * 1);

	a += 1;

	new Fx.Style('ajaxlogs', 'top').set(a)

}

function start_suggestion() {

	preLoading = new Image();

	preLoading.src = "../images/spinner.gif";

	SetDivPosition();

	CheckTyping()

}

function ValidateDomain() {

	domainBox = $('DomainSearchBox');

	if (domainBox.value != '') {

		var a = domainBox.value.toLowerCase();

		var b = '';

		var c = /^[a-z0-9-]+$/i;

		if (a.charAt(0) != "-") b += a.charAt(0);

		for (i = 1; i < a.length; i++) {

			if (c.test(a.charAt(i))) b += a.charAt(i)

		}

		domainBox.value = b

	}

}

function CheckTyping() {

	domainBox = $('DomainSearchBox');

	if (domainBox.value != domainBoxText) {

		if (domainname != domainBox.value) {

			ValidateDomain();

			isTyping = true;

			notChangedCounter = 0;

			if (domainBox.value == '') {

				var a = $('ajaxlog0');

				a.innerHTML = '';

				HideLogDiv()

			} else {

				ShowWhoIsTyping()

			}

			domainname = domainBox.value

		} else if (notChangedCounter >= 0) {

			notChangedCounter++

		}

		if (notChangedCounter > maxCounter && domainname.length > 2) {

			isTyping = false;

			$('domain').setHTML(domainBox.value);

			ShowWhoIsLoading();

			GetWhoIsResponse();

			notChangedCounter = -1

		}

	}

	setTimeout('CheckTyping();', 10)

}

function ShowWhoIsLoading() {

	var a = $('ajaxlog0');

	a.style.visibility = "visible";

	var b = $('ajaxlog0');

	ShowLogDiv()

}

function ShowWhoIsTyping() {

	var a = $('ajaxlogs');

	a.style.visibility = "visible";

	var a = $('ajaxlog0');

	ShowLogDiv();

	a.innerHTML = '<div class="ajax-loading">' + Typing + '</div>'

}

function HideAjaxLogs() {

	var a = $('ajaxlogs');

	a.style.visibility = "hidden";

	var a = $('ajaxlog0');

	a.setHTML('');

	HideLogDiv()

}

function ShowLogDiv() {

	var a = $('ajaxlogs');

	a.style.visibility = "visible";

	var j = 0;

	for (i = 0; i < exts.length; i++) {

		var b = $('ext[' + i + ']');

		if (b.checked == true) {

			var c = $('ajaxlog' + j);

			c.setHTML('');

			c.style.visibility = 'visible';

			j++

		}

	}

	if (j == 0) {

		alert(checkOne);

		HideLogDiv()

	}

}

function HideLogDiv() {

	var a = $('ajaxlogs');

	a.style.visibility = "hidden";

	for (i = 0; i < exts.length; i++) {

		var b = $('ajaxlog' + i);

		if (i >= 0) b.style.visibility = 'hidden';

		if (ajaxReq[i]) {

			if (ajaxReq[i].running == true) {

				ajaxReq[i].cancel()

			}

		}

	}

	$('domain').setHTML('')

}

function GetWhoIsResponse() {

	var k = $('DomainSearchBox');

	var l;

	domainname = k.value;

	if (isTyping) {

		HideLogDiv();

		return

	}

	if (k.value.length < 3) {

		HideLogDiv();

		for (i = 0; i < exts.length; i++) {

			if (ajaxReq[i].running == true) {

				ajaxReq[i].cancel()

			}

		}

		return

	}

	var m = {

		method: 'get',

		onRequest: function () {

			this.options.reqState.setHTML('<div class="domainext">.' + this.reqExt + '</div>' + '<' + checkingDiv + ' class="ajax-loading">' + Checking + '</' + checkingDiv + '>')

		},

		autoCancel: 'true',

		onFailure: function () {

			this.options.reqState.setHTML('<div class="domainext">.' + this.reqExt + '</div>' + '<div class="result0" style="width:200px;">' + Failed + '</div>')

		},

		onComplete: function (a) {

			if (k.value.length < 3) {

				if (this.options.reqNo > 0) {

					this.options.reqState.setHTML('')

				}

				HideLogDiv()

			}

			var b = eval(a);

			if (b) {

				var c = k.value + '.' + this.reqExt;

				if (b[0] == k.value + '.' + this.reqExt) {

					var d = substDomain(b[3], domainname, this.reqExt);

					var e = '<div class="domainext">.' + this.reqExt + '</div>';

					var f = '<div class="result' + b[1] + '">' + result[b[1]] + '</div>';

					if (b[1] == 0) {

						var g = '<div class="result"><a href="javascript:whoIs();" title="' + b[0] + '" rel="http://www.' + b[0] + '" onClick="javascript:awSite(this);">' + Website + '</a></div>';

						var h = '<div class="whois"><a title="Whois - ' + b[0] + '" rel="' + b[2] + '" href="javascript:whoIs();" onClick="javascript:whois(this, ' + whoisWin + ');">' + Whois + '</a></div>'

					} else if (b[1] >= "2") {

						var f = '<div style="float:left;"></div>';

						var g = '';

						if (b[1] == "2") {

							var h = '<div style="color:#f00;">' + Limit + '</div>'

						} else if (b[1] >= "3") {

							var h = '<div style="color:#f00;">' + Limit2 + '</div>'

						}

					} else {

						var g = '<div class="result">&nbsp;</div>';

						if (b[4] == 1) {

							var h = '<div class="register"><a href="' + d + '">' + Register + '</a></div>'

						} else if (b[4] == 2) {

							var h = '<div class="register"><a href="' + d + '" target="_blank">' + Register + '</a></div>'

						} else {

							var h = '<div class="register"><a href="javascript:whoIs();" title="Registrar con uno de nuestros Planes Hosting - ' + b[0] + '" rel="' + d + '" onClick="javascript:awSite(this);">' + Register + '</a></div>'

						}

					}

					this.options.reqState.setHTML(e + f + g + h)

				}

			} else {

				this.options.reqState.setHTML('<div class="domainext">.' + this.reqExt + '</div>' + '<div class="result0" style="width:auto;">' + Failed + '</div><div>&nbsp;</div>')

			}

		}

	};

	var n = '';

	for (i = 0; i < exts.length; i++) {

		if (!ajaxReq[i]) {

			ajaxReq[i] = new Ajax(url, $merge({

				reqState: $('ajaxlog' + i)

			},

			{

				reqNo: i

			},

			{

				reqExt: exts[i]

			},

			m))

		}

	}

	var j = 0;

	for (i = 0; i < exts.length; i++) {

		var o = $('ext[' + i + ']');

		if (o.checked) {

			ajaxReq[j].url = ajaxWhoisUrl + '?domainname=' + domainname + '.' + exts[i];

			ajaxReq[j].reqExt = exts[i];

			ajaxReq[j].request();

			j++

		}

	}

}

function substDomain(a, b, c) {

	a = a.replace(/%domainname%/g, b);

	a = a.replace(/%tld%/g, c);

	return a

}

function stripslashes(a) {

	a = a.replace(/\\'/g, '\'');

	a = a.replace(/\\"/g, '"');

	a = a.replace(/\\\\/g, '\\');

	a = a.replace(/\\0/g, '\0');

	a = a.replace(/\|\|/g, '>');

	a = a.replace(/~~/g, '<');

	return a

}

function whois(e, a) {

	if (a == 1) {

		whoisBox(whoisWidth, whoisHeight);

		$('whoisTitle').setHTML(e.title);

		$('bigBox').setHTML('<pre style="border:none;">' + stripslashes(e.rel) + '</pre>')

	} else {

		popupWindow++;

		var b = $('ajaxlog0').getCoordinates();

		leftP = b['left'];

		topP = b['top'];

		if (popupWindow == 1) {

			leftPos = (leftP - 200 < 0) ? awAdd: leftP - 200;

			topPos = (topP - 200 < 0) ? awAdd: topP - 200;

			sLeftPos = leftPos;

			sTopPos = topPos

		} else {

			leftPos = (leftPos + awAdd > window.getWidth() - 400) ? sLeftPos: leftPos + awAdd;

			topPos = (topPos + awAdd > window.getHeight() - 400) ? sTopPos: topPos + awAdd

		}

		bigBox[popupWindow] = new Element('div').setProperty('id', 'big-Box-' + popupWindow).injectInside(document.body);

		bigBox[popupWindow].setHTML('<pre style="border:none;">' + stripslashes(e.rel) + '</pre>');

		var c = new Element('div', {

			'styles': {

				'padding-top': 5,

				'text-align': 'right'

			}

		});

		c.setHTML(rt);

		whoisWindoo[popupWindow] = new Windoo({

			left: leftPos,

			top: topPos,

			title: e.title,

			width: 400,

			height: 400,

			resizeLimit: {

				'x': [400],

				'y': [400]

			},

			theme: Windoo.Themes.aero,

			position: 'cascade'

		}).addPanel(c).adopt(bigBox[popupWindow]).show()

	}

}

function awSite(e) {

	popupSites++;

	var a = new Element('div', {

		'styles': {

			'padding-top': 5,

			'text-align': 'right'

		}

	});

	a.setHTML(rt);

	awSites[popupSites] = new Windoo({

		width: 800,

		height: 480,

		title: e.title,

		type: 'iframe',

		shadow: false,

		container: false,

		buttons: {

			menu: false

		},

		ghost: {

			resize: true,

			move: true

		},

		theme: Windoo.Themes.aero,

		url: e.rel

	}).addPanel(a).show()

}

function whoisBox(a, b) {

	var c = window.getScrollHeight();

	var t = window.getScrollTop();

	var w = window.getWidth();

	var h = window.getHeight();

	var d = (w > a) ? a: w - 100;

	var e = (h > b) ? b: h - 100;

	var f = Array();

	var g = Array($('awOverlay'), $('bigContainer'), $('bigBoxHead'), $('bigBox'), $('bwf'));

	for (i = 0; i < 5; i++) {

		if (!f[i]) {

			f[i] = new Fx.Styles(g[i], {

				duration: 200,

				transition: Fx.Transitions.Bounce.easeOut

			})

		}

	}

	if (lightBox == "on") {

		f[0].start({

			'height': c,

			'visibility': 'visible',

			'opacity': 0.7

		})

	}

	g[1].style.visibility = 'visible';

	var j = new Fx.Style($('big-whois-t'), 'width');

	j.set(d);

	f[1].start({

		'width': d,

		'margin-left': -(d / 2),

		'margin-top': 0,

		'top': 50

	});

	f[2].start({

		'width': d - 5

	});

	f[3].start({

		'width': d - 20,

		'height': e - 66

	});

	f[4].start({

		'width': d - 52

	})

}

function whoisClose(e) {

	var a = Array($('awOverlay'), $('bigContainer'), $('bigBoxHead'), $('bigBox'), $('bwf'));

	$('whoisTitle').setHTML('');

	a[3].setHTML('');

	a[0].style.visibility = 'hidden';

	a[0].style.opacity = 0;

	var b = new Fx.Style($('big-whois-t'), 'width');

	b.set(75);

	a[1].style.visibility = 'hidden';

	a[1].style.marginLeft = '-75px';

	a[1].style.marginTop = '-75px';

	a[2].style.width = '75px';

	a[3].style.height = '100px';

	a[3].style.width = '100px';

	a[4].style.width = '45px'

}

window.addEvent('domready', function () {

	var a = new Element('div').setProperty('id', 'awOverlay').injectInside(document.body);

	var b = new Element('div').setProperty('id', 'wf').injectInside($('whois-b'));

	var c = $('awOverlay');

	var d = rtp + ' <a style="color:#000;background:#fff;font-weight:bold;" onclick="javascript:awSite(this);" rel="' + rtr + '" title="' + rtta + ' - ' + rts + ' ' + rtte + '" href="javascript:whoIs();">' + rts + '</a>\'s ' + rtta;

	$('wf').setHTML(d);

	var e = new Fx.Styles($('wf'));

	if (ajaxWhoisUrl.indexOf(liveSite) <= 1) {

		e.set({

			'font-size': 9,

			'line-height': 13,

			'height': 13,

			'background-color': '#fff',

			'color': '#000'

		})

	}

	if (whoisWin == 1) {

		var f = new Element('div').setProperty('id', 'bwf').injectInside($('big-whois-b'));

		$('bwf').setHTML(d);

		var g = new Fx.Styles($('bwf'));

		if (ajaxWhoisUrl.indexOf(liveSite) <= 1) {

			g.set({

				'font-size': 12,

				'line-height': 18,

				'height': 18,

				'background-color': '#fff',

				'width': 345,

				'color': '#000'

			})

		}

		$('whoisClose').addEvent('click', function () {

			whoisClose(this)

		});

		c.addEvent('click', function () {

			whoisClose(this)

		})

	}

	$('DomainSearchBox').addEvent('keydown', function () {

		start_suggestion()

	});

	$('DomainSearchBox').addEvent('focus', function () {

		if (this.value == domainBoxText) this.value = ''

	});

	$('DomainSearchBox').addEvent('blur', function () {

		if (this.value == '') this.value = domainBoxText

	});

	$$('.logClose').addEvent('click', function () {

		HideAjaxLogs()

	});

	window.onresize = SetDivPosition

});
