function stristr( haystack, needle) {
    var pos = 0;
    haystack += '';
    pos = haystack.toLowerCase().indexOf( (needle+'').toLowerCase() );
    if( pos == -1 ){
    	return false;
    }
    else{
	return true;
    }
}

function RecargarDatos(sld,tld,css1,css2,tipo){

	var myConn2 = new XHConn();
	if (!myConn2) alert("XMLHTTP not available. Try a newer/better browser.");
	var RecargarDatosFunc = function (oXML) {
		if (oXML.responseText!=""){
			datos = oXML.responseText.split("###");
			window.document.getElementById("disponibilidad_"+tld).innerHTML = datos[0];
			window.document.getElementById("enlace_"+tld).innerHTML = datos[1];
		}
	};

	RDF(sld,tld,css1,css2);

	function RDF(sld,tld,css1,css2) {
		myConn2.connect("/inc/incDominiosDisponibles.php", "POST", "tld="+tld+"&sld="+sld+"&css1="+css1+"&css2="+css2+"&Tipo="+tipo, RecargarDatosFunc);
	}

}

function borrarWhois(){
	ff = window.document.f_whois;
	ff.Captcha.value = "";
}

function activarValidarWhois(){
	window.document.f_whois.enviar.click();
}

function validarWhois(){

	ff = window.document.f_whois;
	fallo = "";
	if(ff.Captcha.value == ""){ fallo += "· Texto de la imagen.\n"; }
	if(fallo != ""){
		alert("Se ha producido un error en la inserción de los datos del formulario.\nPor favor revise atentamente el/los siguiente/s campo/s:\n\n" + fallo);
	}
	else{
		ff.submit();
	}
	
}

function OcultaWhois(){
	var alto = document.getElementById("whois").offsetHeight;
	document.getElementById("trans").height = alto;
}

function AnyadirCarrito(tld,el,tipo){

	var resultado = el.checked;
	window.document.getElementById("total-carrito").innerHTML = '<img title="" alt="" src="/media/image/web/progress.gif" />';
	
	var myConn2 = new XHConn();
	if (!myConn2) alert("XMLHTTP not available. Try a newer/better browser.");
	
	var AnyadirCarritoFunc = function (oXML) {
		if (oXML.responseText!=""){
			datos = oXML.responseText;
			window.document.getElementById("total-carrito").innerHTML = datos;
		}
	};

	ACF(tld,resultado,tipo);

	function ACF(tld,resultado,tipo) {
		myConn2.connect("/inc/incCarritoAnyadir.php", "POST", "tld="+tld+"&res="+resultado+"&Tipo="+tipo, AnyadirCarritoFunc);
	}


}

function seguirComprando(idioma,pag,tipo){

	var precio = parseInt(window.document.getElementById("total-carrito").innerHTML);
	if(tipo == 1){
		var enlace = "registro";
	} else {
		var enlace = "traslado";
	}

	if(pag == 1){
		pag = "";
	}	
		
	if(pag == "0"){
		alert("Debes de estar registrado para poder continuar con el pedido.");
	} else if(precio == 0){
		alert("Debes elegir al menos un dominio para poder continuar el pedido.");
	} else{
		if(pag > 3){
			var ff = window.document.f_venta;
			var f2 = window.document.f_contrato;
			if(!ff.Venta.checked){
				alert("Debes aceptar nuestras condiciones de venta.");
			} else if((tipo == 2) && (!f2.Contrato.checked)){
				alert("Debes cumplir condiciones para realizar el traslado de dominio.");
			//} else if((tipo == 2) && (!f2.Funciona.checked)){
			//	alert("Debes leer el funcionamiento de cómo realizar el proceso de traslado de dominio.");
			} else {
				if(pag == 5){
					iniciarTPV();
				} else {
					location.href = "/" + idioma + "/dominios_carrito_final.html";
				}
			}
		} else{
			location.href = "/" + idioma + "/dominios_" + enlace + "_carrito" + pag + ".html";
		}
	}

}

function AnyadirCarrito2(id){

	window.document.getElementById("total-carrito").innerHTML = '<img title="" alt="" src="/media/image/web/progress.gif" />';
	
	var myConn2 = new XHConn();
	if (!myConn2) alert("XMLHTTP not available. Try a newer/better browser.");
	
	var AnyadirCarritoFunc2 = function (oXML) {
		if (oXML.responseText!=""){
			datos = oXML.responseText;
			window.document.getElementById("total-carrito").innerHTML = datos;
		}
	};

	ACF2(id);

	function ACF2(id) {
		myConn2.connect("/inc/incCarritoAnyadir2.php", "POST", "Id="+id, AnyadirCarritoFunc2);
	}

}

function PlantillaCarrito(id){
	
	var myConn2 = new XHConn();
	if (!myConn2) alert("XMLHTTP not available. Try a newer/better browser.");
	
	var PlantillaCarritoFunc = function (oXML) {
		if (oXML.responseText!=""){
			datos = oXML.responseText;
		}
	};

	PCF(id);

	function PCF(id) {
		myConn2.connect("/inc/incCarritoPlantilla.php", "POST", "Id="+id, PlantillaCarritoFunc);
	}

}

function PromoCarrito(id,tipo,div){

	var valor = tipo.checked;
		
	var myConn2 = new XHConn();
	if (!myConn2) alert("XMLHTTP not available. Try a newer/better browser.");
	
	var PromoCarritoFunc = function (oXML) {
		if (oXML.responseText!=""){
			datos = oXML.responseText;
			if(div != ""){
				if(valor){
					window.document.getElementById(div).style.display = "block";
				} else {
					window.document.getElementById(div).style.display = "none";
				}
			}
		}
	};

	PRCF(id,valor);

	function PRCF(id,valor) {
		myConn2.connect("/inc/incCarritoPromos.php", "POST", "Id="+id+"&Valor="+valor, PromoCarritoFunc);
	}

}

function CodigoPromoEjes(valor){
	
	if(valor == ""){
		alert("Escriba el código de cupón de descuento.");
	} else {
	
		var myConn2 = new XHConn();
		if (!myConn2) alert("XMLHTTP not available. Try a newer/better browser.");
	
		var CodigoPromoEjesFunc = function (oXML) {
			if (oXML.responseText!=""){
				datos = oXML.responseText.split("###");
				if(datos[0] != ""){
					window.document.getElementById("promoCodigo").innerHTML = datos[0];
					location.reload(true);
				} else {
					alert("El código introducido no se encuentra en nuestra base de datos.");
				}
			}
		};

		CPEF(valor);
	
	}

	function CPEF(valor) {
		myConn2.connect("/inc/incCarritoDescuentoEjes.php", "POST", "Valor="+valor, CodigoPromoEjesFunc);
	}
}

function elegirTipo(valor){
	
	var myConn2 = new XHConn();
	if (!myConn2) alert("XMLHTTP not available. Try a newer/better browser.");
	
	var elegirTipoFunc = function (oXML) {
		if (oXML.responseText!=""){
			datos = oXML.responseText;
			window.document.getElementById("carrito-cliente").style.display = "none";
			window.document.getElementById("carrito-nuevo").style.display = "none";
			window.document.getElementById("carrito-confirmar").style.display = "none";
			if(valor == "s"){
				window.document.getElementById("carrito-cliente").style.display = "block";
			} else if(valor == "n"){	
				window.document.getElementById("carrito-nuevo").style.display = "block";
			} else if(valor == "c"){
				window.document.getElementById("carrito-confirmar").style.display = "block";
			}
	
		}
	};

	ETF(valor);

	function ETF(valor) {
		myConn2.connect("/inc/incCarritoElegirCliente.php", "POST", "Valor="+valor, elegirTipoFunc);
	}

}

function mostrarPagos(){
	window.document.getElementById("carrito-pagos").style.display = "block";
}

function mostrarPago(id,tope){

	var myConn2 = new XHConn();
	if (!myConn2) alert("XMLHTTP not available. Try a newer/better browser.");

	var mostrarPagoFunc = function (oXML) {
		if (oXML.responseText!=""){
			datos = oXML.responseText;
			window.document.getElementById("carrito-boton").innerHTML = datos;

			for(i=1;i<=tope;i++){
				window.document.getElementById("carrito-pago-" + i).style.display = "none";
			}
			window.document.getElementById("carrito-pago-"+id).style.display = "block";
	
		}
	};

	MPF(id);

	function MPF(id) {
		myConn2.connect("/inc/incCarritoElegirPago.php", "POST", "Valor="+id, mostrarPagoFunc);
	}
	
}

function mostrarActualizar(){
	window.document.getElementById("carrito-datos-der").style.display = "block";
}

function ocultarActualizar(){
	window.document.getElementById("carrito-datos-der").style.display = "none";
}

function comprobarLoginCliente(texto){
	var letras = "abcdefghijklmnopqrstuvwxyz-ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
	var ok = true;
	if(texto == texto.replace(" ","")){
		for(i=0;i<texto.length;i++){
			if(letras.indexOf(texto.charAt(i),0) == -1){
				ok = false;
			}
		}
	}
	else{
		ok = false;
	}
	return ok;
}

function comprobarPassCliente(texto){
	var letras = "abcdefghijklmnopqrstuvwxyz-ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.@";
	var ok = true;
	if(texto == texto.replace(" ","")){
		for(i=0;i<texto.length;i++){
			if(letras.indexOf(texto.charAt(i),0) == -1){
				ok = false;
			}
		}
	}
	else{
		ok = false;
	}
	return ok;
}

function validarLogin(tipo){
	
	ff = window.document.f_login;
	fallo = "";
	if(ff.Login.value == ""){ fallo += "· Login.\n"; }
	if(ff.Password.value == ""){ fallo += "· Password.\n"; }
	if(fallo != ""){
		alert("Se ha producido un error en la inserción de los datos del formulario.\nPor favor revise atentamente el/los siguiente/s campo/s:\n\n" + fallo);
	}
	else{
	
		window.document.getElementById("carrito-progress").innerHTML = '<img title="" alt="" src="/media/image/web/progress.gif" />';
		
		var myConn2 = new XHConn();
		if (!myConn2) alert("XMLHTTP not available. Try a newer/better browser.");

		var loginFunc = function (oXML) {
			if (oXML.responseText!=""){
				datos = oXML.responseText.split("###");
				window.document.getElementById("carrito-boton").innerHTML = datos[0];
				if(datos[3] != ""){
					alert(datos[3]);
				} else {
					window.document.getElementById("carrito-datos-izq").innerHTML = datos[1];
					window.document.getElementById("carrito-datos-der").innerHTML = datos[2];
					window.document.getElementById("migas-sesion").innerHTML = datos[4];
					mostrarPagos();
					if(tipo == 2){
						mostrarContrato();
					}
					if(datos[5] == "n"){
						desactivarRecibo();
					} else {
						activarRecibo();
					}
				}
				window.document.getElementById("carrito-progress").innerHTML = '';
			}
		};
	
		LF(ff.Login.value,ff.Password.value,ff.Idioma.value);
	}	

	function LF(login,password,idioma) {
		myConn2.connect("/login.php", "POST", "Login="+login+"&Password="+password+"&Idioma="+idioma, loginFunc);
	}
	
}

function activarValidarLogin(){
	window.document.f_login.enviar.click();
}

function activarRecibo(){
	window.document.f_venta.pago[2].disabled = false;
}

function desactivarRecibo(){
	window.document.f_venta.pago[2].disabled = true;
}

function mostrarContrato(){
	window.document.getElementById("carrito-traslado").style.display = "block";
}
			
function validarRegistro(){
	
	ff = window.document.f_registro;
	fallo = "";
	if(ff.NombreFacturacion.value == ""){ fallo += "· Nombre Facturación.\n"; }
	if(ff.NIF.value == ""){ fallo += "· NIF / CIF / NIE.\n"; }else if(!validarNIF(ff.NIF.value)){ fallo += "· NIF / CIF / NIE incorrecto.\n"; }
	if(ff.DireccionFacturacion.value == ""){ fallo += "· Dirección.\n"; }
	if(ff.CPostal.value == ""){ fallo += "· Código Postal.\n"; }
	if(ff.Poblacion.value == ""){ fallo += "· Población.\n"; }
	if(ff.Provincia.value == ""){ fallo += "· Provincia.\n"; }
	if(ff.Pais.value == "0"){ fallo += "· Pais.\n"; }
	if(ff.Telefono.value == ""){ fallo += "· Teléfono.\n"; }
	if(ff.Email.value == ""){ fallo += "· Email.\n"; }else if(!comprobarEmail(ff.Email.value)){ fallo += "· Email incorrecto.\n"; }
	if(ff.Login.value == ""){ fallo += "· Login deseado.\n"; }else if(!comprobarLoginCliente(ff.Login.value)){ fallo += "· El Login deseado solo puede contener letras, numeros y/o guiones(-).\n"; }
	if(ff.Password.value == ""){ fallo += "· Contraseña.\n"; }else if(!comprobarPassCliente(ff.Password.value)){ fallo += "· La Contraseña solo puede contener letras, numeros, guiones(-), puntos(.) y/o arrobas(@).\n"; }else if(ff.Password.value.length<4 || ff.Password.value.length>18){ fallo += "· La Contraseña debe tener entre 4 y 18 caracteres.\n"; }
	if(ff.Password2.value == ""){ fallo += "· Repetir contraseña.\n"; }else if(ff.Password2.value != ff.Password.value){ fallo += "· Las Contraseñas no son iguales.\n"; }
	if(fallo != ""){
		alert("Se ha producido un error en la inserción de los datos del formulario.\nPor favor revise atentamente el/los siguiente/s campo/s:\n\n" + fallo);
	}
	else{
	
		window.document.getElementById("carrito-progress").innerHTML = '<img title="" alt="" src="/media/image/web/progress.gif" />';
		
		var myConn2 = new XHConn();
		if (!myConn2) alert("XMLHTTP not available. Try a newer/better browser.");

		var registroFunc = function (oXML) {
			if (oXML.responseText!=""){
				datos = oXML.responseText.split("###");
				if(datos[1] != ""){
					alert(datos[1]);
				} else {
					window.document.getElementById("carrito-datos-izq").innerHTML = datos[0];
				}
				window.document.getElementById("carrito-progress").innerHTML = '';
			}
		};

		RF(ff.NombreFacturacion.value,ff.PersonaContacto.value,ff.NIF.value,ff.DireccionFacturacion.value,ff.CPostal.value,ff.Poblacion.value,ff.Provincia.value,ff.Pais.value,ff.Telefono.value,ff.Fax.value,ff.Movil.value,ff.Email.value,ff.Login.value,ff.Password.value,ff.Idioma.value);
	}
	

	function RF(nombre,contacto,nif,direccion,cpostal,poblacion,provincia,pais,telefono,fax,movil,email,login,password,idioma) {
		myConn2.connect("/registro.php", "POST", "Nombre="+nombre+"&PersonaContacto="+contacto+"&NIF="+nif+"&Direccion="+direccion+"&CPostal="+cpostal+"&Poblacion="+poblacion+"&Provincia="+provincia+"&Pais="+pais+"&Telefono="+telefono+"&Fax="+fax+"&Movil="+movil+"&Email="+email+"&Login="+login+"&Password="+password+"&Idioma="+idioma, registroFunc);
	}
	
}

function activarValidarRegistro(){
	window.document.f_registro.enviar.click();
}

function validarConfirmar(){
	
	ff = window.document.f_confirmar;
	fallo = "";
	if(ff.Codigo.value == ""){ fallo += "· Código.\n"; }
	if(fallo != ""){
		alert("Se ha producido un error en la inserción de los datos del formulario.\nPor favor revise atentamente el/los siguiente/s campo/s:\n\n" + fallo);
	}
	else{
	
		window.document.getElementById("carrito-progress").innerHTML = '<img title="" alt="" src="/media/image/web/progress.gif" />';
		
		var myConn2 = new XHConn();
		if (!myConn2) alert("XMLHTTP not available. Try a newer/better browser.");

		var confirmarFunc = function (oXML) {
			if (oXML.responseText!=""){
				datos = oXML.responseText.split("###");
				window.document.getElementById("carrito-boton").innerHTML = datos[0];
				if(datos[3] != ""){
					alert(datos[3]);
				} else {
					window.document.getElementById("carrito-datos-izq").innerHTML = datos[1];
					window.document.getElementById("carrito-datos-der").innerHTML = datos[2];
					window.document.getElementById("migas-sesion").innerHTML = datos[4];
					mostrarPagos();
				}
				window.document.getElementById("carrito-progress").innerHTML = '';
			}
		};
	
		CF(ff.Codigo.value,ff.Idioma.value);
	}	

	function CF(codigo,idioma) {
		myConn2.connect("/confirmar.php", "POST", "Codigo="+codigo+"&Idioma="+idioma, confirmarFunc);
	}
	
}

function activarConfirmar(){
	window.document.f_confirmar.enviar.click();
}

function validarActualizar(){
	
	ff = window.document.f_actualizar;
	fallo = "";
	if(ff.NombreFacturacion.value == ""){ fallo += "· Nombre Facturación.\n"; }
	if(ff.NIF.value == ""){ fallo += "· NIF / CIF / NIE.\n"; }else if(!validarNIF(ff.NIF.value)){ fallo += "· NIF / CIF / NIE incorrecto.\n"; }
	if(ff.DireccionFacturacion.value == ""){ fallo += "· Dirección.\n"; }
	if(ff.CPostal.value == ""){ fallo += "· Código Postal.\n"; }
	if(ff.Poblacion.value == ""){ fallo += "· Población.\n"; }
	if(ff.Provincia.value == ""){ fallo += "· Provincia.\n"; }
	if(ff.Pais.value == "0"){ fallo += "· Pais.\n"; }
	if(ff.Telefono.value == ""){ fallo += "· Teléfono.\n"; }
	if(ff.Email.value == ""){ fallo += "· Email.\n"; }else if(!comprobarEmail(ff.Email.value)){ fallo += "· Email incorrecto.\n"; }
	if(fallo != ""){
		alert("Se ha producido un error en la inserción de los datos del formulario.\nPor favor revise atentamente el/los siguiente/s campo/s:\n\n" + fallo);
	}
	else{
	
		window.document.getElementById("carrito-progress").innerHTML = '<img title="" alt="" src="/media/image/web/progress.gif" />';
		
		var myConn2 = new XHConn();
		if (!myConn2) alert("XMLHTTP not available. Try a newer/better browser.");

		var actualizarFunc = function (oXML) {
			if (oXML.responseText!=""){
				datos = oXML.responseText.split("###");
				window.document.getElementById("carrito-datos-izq").innerHTML = datos[0];
				window.document.getElementById("carrito-datos-der").innerHTML = datos[1];
				ocultarActualizar();
				window.document.getElementById("carrito-progress").innerHTML = '';
			}
		};
	
		AF(ff.NombreFacturacion.value,ff.PersonaContacto.value,ff.NIF.value,ff.DireccionFacturacion.value,ff.CPostal.value,ff.Poblacion.value,ff.Provincia.value,ff.Pais.value,ff.Telefono.value,ff.Movil.value,ff.Fax.value,ff.Email.value,ff.Idioma.value);
	}	

	function AF(nombre,personaContacto,nif,direccion,cpostal,poblacion,provincia,pais,telefono,fax,movil,email,idioma) {
		myConn2.connect("/actualizar.php", "POST", "Nombre="+nombre+"&PersonaContacto="+personaContacto+"&NIF="+nif+"&Direccion="+direccion+"&CPostal="+cpostal+"&Poblacion="+poblacion+"&Provincia="+provincia+"&Pais="+pais+"&Telefono="+telefono+"&Movil="+movil+"&Fax="+fax+"&Email="+email+"&Idioma="+idioma, actualizarFunc);
	}
	
}

function activarValidarActualizar(){
	window.document.f_actualizar.enviar.click();
}

function iniciarTPV(){

	ff = window.document.f_venta;

	var tipo = 0;

	/*
	if(getRadio(ff.tpv)!="1" && getRadio(ff.tpv)!="2"){
		alert("Debe elegir un tipo de pasarela de pago con tarjeta para continuar con la compra.");
	} else {

		for(i=0;i<ff.tpv.length;i++){
	       		if(ff.tpv[i].checked)
		  		break;
		}
		tipo = ff.tpv[i].value;
	}
	*/


	if(ff.tpv.value != "1"){
		alert("Debe elegir un tipo de pasarela de pago con tarjeta para continuar con la compra.");
	} else {
		tipo = ff.tpv.value;
	}
	
	if(tipo != 0){
		
		var myConn = new XHConn();
		if (!myConn) alert("XMLHTTP no disponible. Inténtelo con un nuevo navegador.");

		var include_terminado = function (oXML) {
			if (oXML.responseText!=""){
				datos = oXML.responseText;
				segueixCompra(datos,tipo);
			}
		};

		myConn.connect("/inc/incCarritoTPV.php", "GET", "Tipo="+tipo, include_terminado);
	
	}

}
			
function segueixCompra(datos,tipo){

	var aux_datos = datos.split("###");
	
	if(tipo == 1){
		document.fr_cesta.action = aux_datos[0];
		document.fr_cesta.order.value = aux_datos[1];
		document.fr_cesta.store.value = aux_datos[2];
		document.fr_cesta.submit();
	} else {
		/*
		document.fr_compra.action = aux_datos[0];
		document.fr_compra.Ds_Merchant_Order.value = aux_datos[1];
		document.fr_compra.Ds_Merchant_Amount.value = aux_datos[2];
		document.fr_compra.Ds_Merchant_Currency.value = aux_datos[3];
		document.fr_compra.Ds_Merchant_MerchantCode.value = aux_datos[4];
		document.fr_compra.Ds_Merchant_MerchantName.value = aux_datos[5];
		document.fr_compra.Ds_Merchant_MerchantURL.value = aux_datos[6];
		document.fr_compra.Ds_Merchant_UrlOK.value = aux_datos[7];
		document.fr_compra.Ds_Merchant_UrlKO.value = aux_datos[8];
		document.fr_compra.Ds_Merchant_ConsumerLanguage.value = aux_datos[9];
		document.fr_compra.Ds_Merchant_Terminal.value = aux_datos[10];
		document.fr_compra.Ds_Merchant_MerchantSignature.value = aux_datos[11];
		document.fr_compra.Ds_Merchant_TransactionType.value = aux_datos[12];
		document.fr_compra.submit();
		*/
	}

}
