
/** Funciones del buscadorillo de dominios:	*/

var busca_tlds = ["com","mx","com.mx","net","org","biz","info","tv"];

function chkIpMiniDom(e) {
	
	
	// document.getElementById("ip_dominio").value = e.keyCode;return;
	
	// Si es esc, cancela
	if(e.keyCode == 27) {
		document.getElementById("ip_dominio").value = "";
		document.getElementById("ip_dominio").style.backgroundColor = "#eee";
		clearMiniDomMenu();
		return;
		
	// Manda el dominio:
	} else if(e.keyCode == 13) {
		var dom = document.getElementById("ip_dominio").value;
		
		if(dom == "") {
			document.getElementById("ip_dominio").style.backgroundColor = "#de8787";
			uI.showElementError("ip_dominio","escribe un nombre", "por favor escribe un nombre de dominio para poderlo buscar");
			return;
			
		} else {
			if(document.getElementById('ip_dominio').selectedMenuNode > -1) {
				var dives = document.getElementById('menu_buscadorito').getElementsByTagName('div');
				if(dives.item(document.getElementById('ip_dominio').selectedMenuNode)) {
					dom = dom + dives.item(document.getElementById('ip_dominio').selectedMenuNode).childNodes.item(1).firstChild.nodeValue;
				}
			}
			if(dom.match(/^[-a-z0-9\.]+\.[a-z]{2,6}$/)) {
				location = '/dominios/?checa='+encodeURIComponent(dom)+'#registro_dominios';
				return;
			} else {
				uI.showElementError("ip_dominio", "dominio invalido", "el dominio que escribiste contiene caracteres invalidos. Por favor asegurate de usar solo a-z 0-9 el guion (-) y el punto")
			}
		}
	}
	
	var sinput = document.getElementById('ip_dominio').value;
	
	if(sinput == "") {
		// Nada
		clearMiniDomMenu();
		document.getElementById("ip_dominio").style.backgroundColor = "#eee";
	} else if(sinput.match(/^[-a-z0-9]+$/)) {
		// Menu
		// Si es flechita pabajo:
		if(e.keyCode == 40) {
			document.getElementById("ip_dominio").selectedMenuNode++;
		// Si es flechita parriba
		} else if(e.keyCode == 38) {
			document.getElementById("ip_dominio").selectedMenuNode--;
		} else {
			document.getElementById("ip_dominio").selectedMenuNode = -1;
		}
		
		buildMiniDomMenu(sinput);
		document.getElementById("ip_dominio").style.backgroundColor = "#eee";
	} else if(sinput.match(/^[-a-z0-9\.]+\.[a-z]{2,6}$/)) {
		// Dominio valido: nada
		document.getElementById("ip_dominio").style.backgroundColor = "#eee";
	} else {
		// Error
		document.getElementById("ip_dominio").selectedMenuNode = -1;
		clearMiniDomMenu();
		document.getElementById("ip_dominio").style.backgroundColor = "#de8787";
	}
}

function clickSendMiniDom() {
	var sinput = document.getElementById('ip_dominio').value;
	if(sinput.match(/^[-a-z0-9\.]+\.[a-z]{2,6}$/)) {
		location = '/dominios/?checa='+encodeURIComponent(sinput)+'#registro_dominios';
	} else {
		uI.showElementError("ip_dominio", "dominio invalido", "el dominio que escribiste contiene caracteres invalidos. Por favor asegurate de usar solo a-z 0-9 el guion (-) y el punto")
	}
}

function clearMiniDomMenu() {
	document.getElementById("menu_buscadorito").style.display = "none";
}

function buildMiniDomMenu(sinput) {
	
	// Si no hay userinterface regresa, no podemos hacer nada
	if(! uI) { return false; }
	
	// Checa cual esta seleccionado, y si hay OOB arregla:
	if(document.getElementById("ip_dominio").selectedMenuNode == -2) {
		document.getElementById("ip_dominio").selectedMenuNode = (busca_tlds.length-1);
		
	} else if(document.getElementById("ip_dominio").selectedMenuNode > (busca_tlds.length-1)) {
		document.getElementById("ip_dominio").selectedMenuNode = -1;
	}
	
	// var sinput = document.getElementById("ip_dominio").value;
	while(document.getElementById("menu_buscadorito").firstChild) {
		document.getElementById("menu_buscadorito").removeChild(document.getElementById("menu_buscadorito").firstChild);
	}
	var i;
	for(i in busca_tlds) {
		var divi = document.createElement("div");
		
		if(i == document.getElementById("ip_dominio").selectedMenuNode) {
			divi.className = "menuSelect";
		}
		
		divi.appendChild(document.createTextNode(sinput));
		var espan = document.createElement("span");
		espan.appendChild(document.createTextNode("." + busca_tlds[i]));
		divi.appendChild(espan);
		document.getElementById("menu_buscadorito").appendChild(divi);
	}
	var coords = uI.findPos(document.getElementById('ip_dominio'));
	document.getElementById("menu_buscadorito").style.top		= (coords[1] + parseInt(document.getElementById("ip_dominio").offsetHeight)) + "px";
	document.getElementById("menu_buscadorito").style.left		= coords[0] + "px";
	document.getElementById("menu_buscadorito").style.width	= parseInt(document.getElementById("ip_dominio").offsetWidth) + "px";
	document.getElementById("menu_buscadorito").style.display = "block";
	
	
}

function focusMiniDom() {
	
	if(document.getElementById('ip_dominio').value == "minombre.com") {
		document.getElementById('ip_dominio').value = "";
		document.getElementById('ip_dominio').style.color = "#333";
		document.getElementById('ip_dominio').style.fontStyle = "normal";
	}
}

function blurMiniDom() {
	if(document.getElementById('ip_dominio').value == "") {
		document.getElementById('ip_dominio').value = "minombre.com";
		document.getElementById('ip_dominio').style.color = "#666";
		document.getElementById('ip_dominio').style.backgroundColor = "#eee";
		document.getElementById('ip_dominio').style.fontStyle = "italic";
	} else {
	}
	clearMiniDomMenu();
}
