var xmlhttp = false;

function getXmlHttp() {
	try {
	    xmlhttp = new XMLHttpRequest();
	} catch(ee) {
	    try {
	        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	    } catch(e) {
	        try {
	            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	        } catch(E) {
	            xmlhttp = false;
	        }
	    }
	}
}

getXmlHttp();

function replaceAll(string, token, newtoken) {
	while (string.indexOf(token) != -1) {
 		string = string.replace(token, newtoken);
	}
	return string;
}

function limpaResultado() {
	$("resultado_consulta").innerHTML = "";
	$("continuar").style.display = "none";
}

function chamaHref(url) {
	window.location = url;
}

function enterConsultaDominio(nome, extensao, evento) {
    if(document.all) { // Internet Explorer
        tecla = evento.keyCode;
    } else { // Nestcape
        tecla = evento.which;
    }

	if (tecla == 13)
		consultaDominio(nome.id,extensao);
	else
		limpaResultado();
}

function consultaDominio(nome, extensao) {
	var url = "";
	var ext = document.getElementById(extensao).options[document.getElementById(extensao).selectedIndex].text;
	var campoDominio = document.getElementById(nome);
	var dominio = replaceAll(campoDominio.value," ","");
	campoDominio.value = dominio;
	//extensao.substring;
	//if((ext.substring((ext.length)-3))
	//alert(ext.substring((ext.length)-3));
   // alert(ext);		
    
    //consultaDominioBrAjax(dominio + "." + ext);
    
	url = "/whois.php?dominio=" + dominio + "." + ext;

	if (campoDominio.value != "") {
		$("resultado_consulta").innerHTML = "<b>Consultando, aguarde...</b>";
		$("continuar").style.display = "none";
	
		if (xmlhttp.readyState > 0 && xmlhttp.readyState < 4) {
      	  		getXmlHttp();
	    	}
    		
		xmlhttp.open("GET", url, true);
		xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
		xmlhttp.setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate");
		xmlhttp.setRequestHeader("Cache-Control", "post-check=0, pre-check=0");
		xmlhttp.setRequestHeader("Pragma", "no-cache");
		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState == 4) {
				resposta = xmlhttp.responseText;


				if (resposta == "DOMINIO LIVRE" && dominio.length>1) {
					$("resultado_consulta").innerHTML = "<font color='green' style='font-size: 13px;'><b>Disponível para Compra! </b></font><br /> ";
					$("continuar").style.display = "block";
					$("continuar").value = "";
					$("continuar").removeClass("buttonTransfira");
					$("continuar").addClass("buttonAdquira");
					$("continuar").title = "Você poderá adquirir este domínio e reservá-lo imediatamente. \nClique AGORA e garanta que seja seu!!!";
				} else 
					if (resposta == "DOMINIO REGISTRADO")
					{
						$("resultado_consulta").innerHTML = "<font color='red' style='font-size: 13px;'><b>Endereço Indisponível!</b></font><br /> ";
						$("continuar").style.display = "block";
						$("continuar").value = "";
						$("continuar").removeClass("buttonAdquira");
						$("continuar").addClass("buttonTransfira");
						$("continuar").title = "Se o domínio que consultou é seu, poderá transferir sua hospedagem para a UgaBuga. \nClique AGORA e confira nossa qualidade !!!";
					} 
					else 
						if (dominio.length<2)
						{
							$("resultado_consulta").innerHTML = "<font color='red' style='font-size: 13px;'><b>Endereço Indisponível!</b></font><br /> ";
							//$("continuar").style.display = "block";
							//$("continuar").value = "Transfira Já";
							//$("continuar").removeClass("buttonAdquira");
							//$("continuar").addClass("buttonTransfira");
							//$("continuar").title = "Se o domínio que consultou é seu, poderá transferir sua hospedagem para a UgaBuga. \nClique AGORA e confira nossa qualidade !!!";
						}
				
						else {
							alert("Serviço Temporariamente Indisponivel\nTente Novamente em Instantes!!!"); // alert("Erro: " + resposta + "\nEntre em contato com o administrador!");
						}
			}
		};
		xmlhttp.send(null);
	} else {
    		alert("Campo Dominio deve ser preenchido.");
	}
}


function limpaResultadoPedido() {
	$("resultado_consulta_pedido").innerHTML = "";
	//$("continuar_pedido").style.display = "none";
}





function digitandoConsultaDominioPedido(nome, extensao, hidden, evento) {
	limpaResultadoPedido();
	document.getElementById('nomeDominioPedido').style.border = '1px solid #7f9db9';
	if(document.all) { // Internet Explorer
		tecla = evento.keyCode;
	} else { // Nestcape
		tecla = evento.which;
	}
	
	if (tecla >= 65 &&
	    tecla <= 90 ||
	    tecla == 8 ||
	    tecla == 46) {
	    	if (document.getElementById(nome).value == "" ||
	    	    document.getElementById(extensao).options[document.getElementById(extensao).selectedIndex].text == "") {
	    		document.getElementById(hidden).value = "";
	    	} else {
			document.getElementById(hidden).value = document.getElementById(nome).value + "." + document.getElementById(extensao).options[document.getElementById(extensao).selectedIndex].text;	
		}
	}
}

function extensaoConsultaDominioPedido(nome, extensao, hidden) {
	if (document.getElementById(nome).value == "" ||
	    document.getElementById(extensao).options[document.getElementById(extensao).selectedIndex].text == "") {
		document.getElementById(hidden).value = "";
	} else {
		document.getElementById(hidden).value = document.getElementById(nome).value + "." + document.getElementById(extensao).options[document.getElementById(extensao).selectedIndex].text;
	}
}


function consultaDominioPedido(nome, extensao, hidden) {
	var url = "";
	var ext = document.getElementById(extensao).options[document.getElementById(extensao).selectedIndex].text;
	var campoDominio = document.getElementById(nome);
	var dominio = replaceAll(campoDominio.value," ","");
	campoDominio.value = dominio;

	
	url = "/whois.php?dominio=" + dominio + "." + ext;

	if (campoDominio.value != "") {
		$("resultado_consulta_pedido").innerHTML = "<b>Consultando, aguarde...</b>";
	
		if (xmlhttp.readyState > 0 && xmlhttp.readyState < 4) {
      	  		getXmlHttp();
	    	}
    		
		xmlhttp.open("GET", url, true);
		xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
		xmlhttp.setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate");
		xmlhttp.setRequestHeader("Cache-Control", "post-check=0, pre-check=0");
		xmlhttp.setRequestHeader("Pragma", "no-cache");
		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState == 4) {
				resposta = xmlhttp.responseText;

				if (resposta == "DOMINIO LIVRE") {
					$("resultado_consulta_pedido").innerHTML = "<font color='green' style='font-size: 13px;'><b>Disponível para Compra! </b></font><br /> ";
				} else if (resposta == "DOMINIO REGISTRADO")
				{
					$("resultado_consulta_pedido").innerHTML = "<font color='red' style='font-size: 13px;'><b>Endereço Indisponível!</b></font><br />  ";
				} else {
					alert("Serviço Temporariamente Indisponivel\nTente Novamente em Instantes!!!");
				}
			}
		};
		xmlhttp.send(null);
	} else {
    		alert("Campo Dominio deve ser preenchido.");
	}
}

function continuar(nome, extensao, objeto) {
	var ext = document.getElementById(extensao).options[document.getElementById(extensao).selectedIndex].text;
	var idExt = document.getElementById(extensao).options[document.getElementById(extensao).selectedIndex].value;
	var campoDominio = document.getElementById(nome);
	var dominio = replaceAll(campoDominio.value," ","");
	
	//alert('ext:'+ext+' idExt:'+idExt+' campoDom:'+campoDominio+'dominio:'+dominio);
	//alert('index:'+document.getElementById(extensao).selectedIndex+' value:');
    //TODO O botão redireciona para o produto correspondente à extensão escolhida. Ticket 850.
	var a = ext.split(".");
	//alert(a[a.length-1]+'obj value:'+objeto.value);
    
	if (a[a.length-1] == "br") {
		//var tmp_1='/index.php?page=shop.product_details&flypage=flypage.tpl&product_id=71&category_id=21&option=com_virtuemart&Itemid=1&dominio=' + dominio + '&ext=' + ext + '&idExt=' + document.getElementById(extensao).selectedIndex;
		//alert('nacional');
		//window.location.href = '/index.php?page=shop.product_details&flypage=flypage.tpl&product_id=71&category_id=21&option=com_virtuemart&Itemid=1&dominio=' + dominio + '&ext=' + ext + '&idExt=' + document.getElementById(extensao).selectedIndex;
		window.location.href = '/index.php?page=shop.product_details&flypage=flypage.tpl&product_id=71&category_id=21&option=com_virtuemart&Itemid=1&dominio=' + dominio + '&ext=' + ext + '&idExt=' + idExt;
	}
	
	else {
		//alert('internacional');
		//window.location.href = '/index.php?page=shop.product_details&flypage=flypage.tpl&product_id=156&category_id=21&option=com_virtuemart&Itemid=1&dominio=' + dominio + '&ext=' + ext + '&idExt=' + document.getElementById(extensao).selectedIndex;
		window.location.href = '/index.php?page=shop.product_details&flypage=flypage.tpl&product_id=30&category_id=21&option=com_virtuemart&Itemid=1&dominio=' + dominio + '&ext=' + ext + '&idExt=' + idExt;
	}
}

function consultaDominioBrAjax(domain ) {
	
	//var theEl = $(el);
	
	var callback = {
		success : function(responseText) {
			alert(responseText);
		}
	}
	var opt = {
	    // Use POST
	    method: 'get',
	    //data: $('consultaDominioFormHidden'),
	    //postBody: escape('qr='+domain),
	    // Handle successful response
	    onComplete: callback.success
    }
	//new Ajax( 'http://localhost/', opt ).request();
	//new Ajax( 'http://localhost/phpmyadmin', opt ).request();
}
