function mostrarCompania(campo) {
	if (campo.value != '') {
		document.getElementById('formaJuridica').style.display = 'block';
		document.forms[0].formaJuridica.disabled = false;
		document.forms[0].formaJuridica.focus();
	}
	else {
		document.getElementById('formaJuridica').style.display='none';
		document.forms[0].formaJuridica.disabled = true
		document.forms[0].compania.focus();
	}
}

function mostrarProvincia(valor) {
	var _form = document.forms[0];
	if (valor == 'ES') {
		_form.provinciaG.disabled = true;
		document.getElementById('provinciaE').style.display = 'block';
		_form.provinciaE.disabled = false;
		document.getElementById('provinciaG').style.display = 'none';
	}
	else {
		_form.provinciaG.disabled = false;
		document.getElementById('provinciaE').style.display = 'none';
		_form.provinciaE.disabled = true;
		document.getElementById('provinciaG').style.display = 'block';
	}
}

function recuperarContrasenia() {
	var _form = document.loginform;
	var _mensaje = '';
	if (_form.username.value != '') {
		var req = new XMLHttpRequest();
		if (req) {
    	req.onreadystatechange = function() {
      	if (req.readyState == 4 && (req.status == 200 || req.status == 304)) {
      		alert(req.responseText);
      	}
    	};
    	req.open('GET', 'operaciones.php?accion=recuperarContrasenia&email='+_form.username.value);
    	req.send(null);
  	}
	}
	else alert('Debes introducir un email');
}

function flip(elemento) {
	var elem = document.getElementById(elemento);
	elem.style.display = (elem.style.display == 'block') ? 'none' : 'block';
}

function mostrarOcultarDetalles(elemento, accion) {
	var elem = document.getElementById(elemento);
	elem = '<img src="images/'+((accion=='menos')?'menos':'mas')+'.gif" alt="'+((accion=='menos')?'Ocultar detalles':'Mostrar detalles')+'">';
}

function comprobarContrasena(form, campo1, campo2) {
	var _div = document.getElementById('password-error');
	if (campo1.length < 6 || campo2 == '' || campo1 != campo2) {
		form.subm.disabled = true;
		_div.style.display='block';
		
		if (campo1.length < 6) _div.innerHTML = 'La longitud del password es demasiado corta';
		else if (campo1 == '') _div.innerHTML='El password no es v&aacute;lido';
		else if (campo1 != campo2) _div.innerHTML = 'No coinciden los password';
	}
	else {
		form.subm.disabled = false;
		document.getElementById('password-error').style.display = 'none';
	}
}

function actualizarPuja(dominio) {
	var req = new XMLHttpRequest();
	if (req) {
    req.onreadystatechange = function() {
      if (req.readyState == 4 && (req.status == 200 || req.status == 304)) {
      	var resp = req.responseText;
      	if (resp) {
      		resp = eval('(' + resp + ')');
					var _d = document;
					var _f = _d.pujaForm;
					var _tmp = '';
					if (resp.historial != '0') _tmp += '<a href="historial.php?d=' + dominio + '" title="Mostrar historial de pujas" style="text-decoration: underline">';
					_tmp += resp.historial + ' puja';
					if (resp.historial != 1) _tmp += 's';
					if (resp.historial != '0') _tmp += '</a>';
					_d.getElementById('historial').innerHTML = _tmp;
					
					switch (resp.estadoSubasta) {
						case 'abierta':
							_d.getElementById('estadoSubasta').innerHTML = 'su backorder est&aacute; en subasta';
							_d.getElementById('pujaActual').innerHTML = resp.pujaActual;
							switch (resp.estadoPuja) {
								case 'si':
									_d.getElementById('pujaOK').style.display = 'block';
									_d.getElementById('pujaKO').style.display = 'none';
									_d.getElementById('pujaInfo').style.display = 'none';
									_d.getElementById('pujaOK').innerHTML = 'Actualmente eres el m&aacute;ximo pujador de la subasta para este backorder.';
									break;
								case 'no':
									_d.getElementById('pujaOK').style.display = 'none';
									_d.getElementById('pujaKO').style.display = 'block';
									_d.getElementById('pujaInfo').style.display = 'none';
									_d.getElementById('pujaKO').innerHTML = 'Su puja ha sido superada.';
									break;
							}
							
							_d.getElementById('pujaMinima').innerHTML = (resp.pujaActual == '15') ? (resp.pujaActual) : (parseInt(resp.pujaActual) + 1);
							_d.getElementById('tiempoRestante').innerHTML = resp.tiempoRestante;
							break;
						case 'cerrada':
							switch (resp.estadoPuja) {
								case 'si':
									_d.getElementById('pujaOK').style.display = 'block';
									_d.getElementById('pujaKO').style.display = 'none';
									_d.getElementById('pujaInfo').style.display = 'none';
									_d.getElementById('pujaOK').innerHTML = '&iexcl;Enhorabuena!<br>Eres el ganador de esta subasta.';
									break;
								case 'no':
									_d.getElementById('pujaOK').style.display = 'none';
									_d.getElementById('pujaKO').style.display = 'block';
									_d.getElementById('pujaInfo').style.display = 'none';
									_d.getElementById('pujaKO').innerHTML = 'Su puja ha sido superada.';
									break;
							}
							_f.actualizaPuja.disabled = true;_f.actualizaPuja.type = 'hidden';
							_f.fecha.disabled = true;_f.fecha.type = 'hidden';
							_f.d.disabled = true;_f.d.type = 'hidden';
							_f.checksum.disabled = true;_f.checksum.type = 'hidden';
							_f.mpuja.disabled = true;_f.mpuja.type = 'hidden';
							_f.botonPuja.disabled = true;_f.botonPuja.type = 'hidden';
							
							_d.getElementById('nuevaPuja').innerHTML = '';
							_d.getElementById('botonPuja').innerHTML = '';
							_d.getElementById('tiempoRestanteP').innerHTML = '';
							_d.getElementById('fechaCierre').innerHTML = 'Finalizado:';
							_d.getElementById('estadoSubasta').innerHTML = 'esta subasta est&aacute; cerrada';
							break;
					}
      	}
      }
    };
    req.open('GET', 'operaciones.php?accion=actualizarPuja&d='+dominio);
    req.send(null);
  }
}

function menuDHTML(campo, estado) {
	var _div = document.getElementById(campo);
	_div.style.background = (estado == 'on') ? '#0F0' : '#F00';
}

function abrir(URL, width, height, scroll) {
	if (scroll == 'no') scrol = 'scrollbars=no';
	else scrol = 'scrollbars=yes';
	window.open(URL, 'popup', "width=" + width + ",height=" + height + "," + scrol + ",menubar=no,location=no,resizable=no")
}

function irAPagina(url, max, variable, irA) {
	if (irA != 'NaN' && irA != 0 && irA != 'undefined' && irA != NaN) {
		var voyA = parseInt(irA);
		if (max != 'no' && voyA > max) voyA = max;
		window.location = url + variable + '=' + voyA;
	}
}

function mostrarInfo(rid) {
	var _o = document.getElementById(rid).style;
	_o.visibility = (_o.visibility === 'visible') ? 'hidden' : 'visible';
	_o.display = (_o.display === 'block') ? 'none' : 'block';
}

function mostrarInfoDominio(rid, info) {
	if (!_cargados[rid+info]) {
		$("div#"+info+rid).load("infoDominio.php?dominio="+rid+"&info="+info+"&solo=1");
		_cargados[rid+info] = 1;
	}
	
	mostrarInfo(info+rid);
	/*$("div#"+info+rid).toggle();*/
}

function mostrarAlexa(rid, dominio, alexa) {
	mostrarInfo(rid);
	var _div = document.getElementById(rid);
	var _html = '';
	_html += '<table>';
	_html += '<tr>';
	_html += '<td colspan="2" align="center"><strong>' + dominio + '</strong></td>';
	_html += '</tr>';
	_html += '<tr>';
	_html += '<td>Ranking Alexa:</td>';
	_html += '<td align="right">' + alexa + '</td>';
	_html += '</tr>';
	_html += '</table>';
	_div.innerHTML = _html;
}

function mostrarPageRank(rid, dominio, pr) {
	mostrarInfo(rid);
	var _div = document.getElementById(rid);
	var _html = '';
	_html += '<table>';
	_html += '<tr>';
	_html += '<td colspan="2" align="center"><strong>' + dominio + '</strong></td>';
	_html += '</tr>';
	_html += '<tr>';
	_html += '<td>Pagerank:</td>';
	_html += '<td align="right">' + pr + '</td>';
	_html += '</tr>';
	_html += '</table>';
	_div.innerHTML = _html;
}

function mostrarWhois(rid, dominio) {
	mostrarInfo(rid);
	$("div#"+rid).load("infoDominio.php?"+dominio);
}

function mostrarGoogle(rid, dominio, globales, espanol, espana) {
	mostrarInfo(rid);
	var _div = document.getElementById(rid);
	var _html = '';
	_html += '<table>';
	_html += '<tr>';
	_html += '<td colspan="2" align="center"><strong>' + dominio + '</strong></td>';
	_html += '</tr>';
	_html += '<tr>';
	_html += '<td colspan="2"><em>Busquedas Google:</em></td>';
	_html += '</tr>';
	_html += '<tr>';
	_html += '<td>... globales:</td>';
	_html += '<td align="right">' + globales + '</td>';
	_html += '</tr>';
	_html += '<tr>';
	_html += '<td>... en espa&ntilde;ol:</td>';
	_html += '<td align="right">' + espanol + '</td>';
	_html += '</tr>';
	_html += '<tr>';
	_html += '<td>... en Espa&ntilde;a:</td>';
	_html += '<td align="right">' + espana +  '</td>';
	_html += '</tr>';
	_html += '</table>';
	_div.innerHTML = _html;
}


/*Funciones register.php*/

function mostrarRegistro(formulario) {
	var mostrar = true;
	var j = 0, i = 0, max = 0;
	var tags = new Array();
	var div = document.getElementById('nicHandleRegisterForm');
	
	if (formulario == 'si') {
		mostrar = false;
	}
	
	tags[0] = div.getElementsByTagName('input');;
	tags[1] = div.getElementsByTagName('select');
	
	
	for (; j <= 1; j ++) {
		max = tags[j].length;
		for (; i < max; i ++) {
			tags[j][i].disabled = mostrar;
		}
		i = 0;
	}
	
	j = 0, i = 0, max = 0;
	mostrar = false;
	div = document.getElementById('RegisterForm');
	
	
	if (formulario == 'si') {
		mostrar = true;
	}
	
	tags[0] = div.getElementsByTagName('input');
	tags[1] = div.getElementsByTagName('select');
	
	for (; j <= 1; j ++) {
		max = tags[j].length;
		for (; i < max; i ++) {
			tags[j][i].disabled = mostrar;
		}
		i = 0;
	}
}

function anadirLetraNIF(campo) {
	campo.value = campo.value.replace(/-/, "");
	campo.value = campo.value.replace(/\./, "");
	if (campo.value.length == 8) {
		var letras = 'TRWAGMYFPDXBNJZSQVHLCKE';
		posicion = campo.value % 23;
		campo.value += letras.substring(posicion, posicion + 1);
	}
	return;
}

function validarEmail(email) {
	return (null == /^([a-zA-Z0-9_\-])+(\.([a-zA-Z0-9_\-])+)*@((\[(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5])))\.(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5])))\.(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5])))\.(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5]))\]))|((([a-zA-Z0-9])+(([\-])+([a-zA-Z0-9])+)*\.)+([a-zA-Z])+(([\-])+([a-zA-Z0-9])+)*))$/.exec(email)) ? false : true; 
}

function comprobarPassword(formulario, extra) {
	var p1 = document.forms[formulario].eval('password' + extra).value;
	var p2 = document.forms[formulario].eval('repassword' + extra).value;
	var _div = document.getElementById('password' + extra + '-error');
	var _div2 = document.getElementById('repassword' + extra + '-error');
	
	if (_div.style.display == 'none') {
		if (p1.length < 6) {
			_div.style.display = 'block';
			_div.innerHTML = 'El campo "PASSWORD" debe tener un m&iacute;nimo de 6 caracteres.';
		}
		else _div.style.display = 'none';
		
		if (p2 != '' && p1 != p2) {
			_div2.style.display = 'block';
			_div2.innerHTML = 'El password y su comprobaci&oacute;n no coinciden.';
		}
		else _div2.style.display = 'none';
	}
}

function controlarCPEspana(pais, valor) {
	if (pais == 'ES') {
		var valorInt = parseInt(valor);
		valorInt += '';
		if (valor.length != 5 || valor != valorInt) return false;
		else return true;
	}
	return true;
}

function validarNifCifNie(doc) {
	//returns: 1 = NIF ok, 2 = CIF ok, 3 = NIE ok, -1 = NIF bad, -2 = CIF bad, -3 = NIE bad, 0 = ??? bad
	//función creada por David Vidal Serra, Copyleft 2005
  doc = doc.toUpperCase();
  
  if (null == /(^[A-Z]{1}[0-9]{7}[A-Z0-9]{1}$|^[T]{1}[A-Z0-9]{8}$)|^[0-9]{8}[A-Z]{1}$/.exec(doc)) return 0;
  var i = 0;
  var num = new Array();
  for (; i < 9; ++ i) {
  	num[i] = doc.substr(i, 1);
  }
  
  var suma = parseInt(num[2]) + parseInt(num[4]) + parseInt(num[6]);
  
  i = 1;
  var tmp = tmp1 = tmp2 = '';
  for (; i < 8; i += 2) {
  	tmp = 2 * parseInt(num[i]) + '';
  	tmp1 = tmp.substr(0, 1);
  	tmp2 = tmp.substr(1, 1);
  	if (tmp2 == '') tmp2 += 0;
  	suma += parseInt(tmp1) + parseInt(tmp2);
  }
  var sumaTmp = suma + '';
  var n = 10 - sumaTmp.substr(sumaTmp.length - 1, 1);
  
  if (null != /^[ABCDEFGHNPQS]{1}/.exec(doc)) {
  	var nTmp = n + '';
  	return (num[8] == String.fromCharCode(64 + n) || num[8] == nTmp.substr(nTmp.length - 1, 1)) ? 2 : -2;
  }
  
  if (null != /^[KLM]{1}/.exec(doc)) {
  	var nTmp = n + '';
  	return (num[8] == String.fromCharCode(64 + n)) ? 2 : -2;
  }
  
  var caracteres = 'TRWAGMYFPDXBNJZSQVHLCKE';
  if (null != /^[0-9]{8}[A-Z]{1}$/.exec(doc)) {
    return (num[8] == caracteres.substr(doc.substr(0, 8) % 23, 1)) ? 1 : -1;
  }
  return 0;
}

function comprobarVariable(campo, nulo, nombre) {
	if (nombre == '') nombre = campo.name;
	var _div = document.getElementById(campo.name + '-error');

	if (nulo != 'si' && campo.value == '') {
		_div.style.display = 'block';
		_div.innerHTML = 'El campo "' + nombre.toUpperCase() + '" no puede estar en blanco.';
		return;
	}
	var valido = true;
	switch(campo.name) {
		case 'nombre':
		case 'apellidos':
			if (null != /[^a-zA-Z\s]/.exec(campo.value)) valido = false;
			break;
		case 'compania':
			if (campo.value != '') cargarFormaJuridica();
			break;
		case 'nif':
			anadirLetraNIF(document.forms['regForm'].nif);
			if (validarNifCifNie(document.forms['regForm'].nif.value) != 1) valido = false;
			break;
		case 'cif':
			if (validarNifCifNie(document.forms['regForm'].cif.value) != 2) valido = false;
			break;
		case 'nie':
			if (validarNifCifNie(document.forms['regForm'].cif.value) != 3) valido = false;
			break;
		case 'email':
			if (validarEmail(campo.value) == false) valido = false;
			break;
		case 'cp':
			if (controlarCPEspana(document.forms['regForm'].pais.value, campo.value) == false) valido = false;
			break;
		case 'pais':
			if (campo.value === 'ES') {
				document.getElementById('provinciaG').style.display = 'none';
				document.forms['regForm'].provinciaG.disabled = true;
				document.getElementById('provinciaE').style.display = 'block';
				document.forms['regForm'].provinciaE.disabled = false;
			}
			else {
				document.getElementById('provinciaE').style.display = 'none';
				document.forms['regForm'].provinciaE.disabled = true;
				document.getElementById('provinciaG').style.display = 'block';
				document.forms['regForm'].provinciaG.disabled = false;
			}
			break;
		case 'provinciaE':
			var _val = document.forms['regForm'].cp.value.substr(0, 2);
			if (_val != campo.value) valido = false;
			break;
		case 'tlf':
			if (null == (/(\+)([0-9]{1,4})(\.)([0-9]{6,10})/.exec(campo.value))) valido = false;
			break;
		case 'nicHandle':
			if (null == (/^([a-zA-Z\s\-0-9]{2,})(ESNIC-F4)$/i.exec(campo.value))) valido = false;
			break;
		case 'nicHandleEmpresa':
			if (campo.value != '') {
				if (null == (/^([a-zA-Z\s\-0-9]{2,})(ESNIC-F4)$/i.exec(campo.value))) valido = false;
			}
			break;
		case 'renicHandleEmpresa':
			if (document.forms['regFormNic'].nicHandleEmpresa.value != document.forms['regFormNic'].renicHandleEmpresa.value) valido = false;
			break;
		case 'renicHandle':
			if (document.forms['regFormNic'].nicHandle.value != document.forms['regFormNic'].renicHandle.value) valido = false;
			break;
	}
	
	if (valido == false) {
		_div.style.display = 'block';
		_div.innerHTML = 'El campo "' + nombre.toUpperCase() + '" no es v&aacute;lido.';
	}
	else {
		_div.style.display = 'none';
		_div.innerHTML = '';
	}
}

function cargarCampoForm(aCargar, dondeCargar, filtro) {
	if (!document.getElementById(aCargar) && dondeCargar.value != '' && (filtro == '' || filtro == dondeCargar.value)) {
		var req = new XMLHttpRequest();
		if (req) {
    	req.onreadystatechange = function() {
      	if (req.readyState == 4 && (req.status == 200 || req.status == 304)) {
      		if (!document.getElementById(aCargar)) {
						var _div = document.getElementById(dondeCargar.name);
						var _tmp = dondeCargar.value;
						_div.innerHTML += req.responseText;
						document.forms[0].eval(dondeCargar.name).value = _tmp;
						document.forms[0].eval(aCargar).focus();
					}
      	}
    	};
    	req.open('GET', 'operaciones.php?accion=mostrarDesplegable&campo=' + aCargar);
    	req.send(null);
  	}
	}
	else if (document.getElementById(aCargar) && (filtro == '' || filtro == dondeCargar.value)) {
		switch (aCargar) {
			case 'formaJuridica':
				mostrarCompania(dondeCargar);
				break;
			case 'provinciaE':
				mostrarProvincia(dondeCargar.value);
				break;
		}
	}
}

function cargarFormaJuridica() {
	var fJ = document.getElementById('formaJuridica');
	if (!fJ) {
		var dC = document.getElementById('compania');//Donde vamos a cargar el nuevo div
		var req = new XMLHttpRequest();
		if (req) {
	  	req.onreadystatechange = function() {
	  	 	if (req.readyState == 4 && (req.status == 200 || req.status == 304)) {
	  	 		var form = document.forms[0];
					var tmp = form.eval('compania').value;
					dC.innerHTML += req.responseText;
					form.eval('compania').value = tmp;
					form.eval('formaJuridica').focus();
				}
	   	}
	  };
	  req.open('GET', 'operaciones.php?accion=mostrarDesplegable&campo=formaJuridica');
	  req.send(null);
	  
	  dC.style.display = 'block';
		dC.style.visibility = 'visible';
	}
}

function mostrarIdentificacion(valor) {
	switch (valor) {
		case '1':
			document.getElementById('nif').style.display = 'block';
			//document.getElementById('cif').style.display = 
			document.getElementById('nie').style.display = document.getElementById('otro').style.display = 'none';
			//document.getElementById('cif-error').innerHTML = 
			document.getElementById('nie-error').innerHTML = document.getElementById('otro-error').innerHTML = '';
			document.forms['regForm'].nif.disabled = false;
			//document.forms['regForm'].cif.disabled = 
			document.forms['regForm'].nie.disabled = document.forms['regForm'].otro.disabled = true;
			document.forms['regForm'].nif.focus();
			break;
		case '2':
//			document.getElementById('nif').style.display = document.getElementById('nie').style.display = document.getElementById('otro').style.display = 'none';
//			document.getElementById('nif-error').innerHTML = document.getElementById('nie-error').innerHTML = document.getElementById('otro-error').innerHTML = '';
//			document.getElementById('cif').style.display = 'block';
//			document.forms['regForm'].nif.disabled = document.forms['regForm'].nie.disabled = document.forms['regForm'].otro.disabled = true;
//			document.forms['regForm'].cif.disabled = false;
//			document.forms['regForm'].cif.focus();
//			break;
//		case '3':
//			document.getElementById('cif').style.display = 
			document.getElementById('nif').style.display = document.getElementById('otro').style.display = 'none';
//			document.getElementById('cif-error').innerHTML = 
			document.getElementById('nif-error').innerHTML = document.getElementById('otro-error').innerHTML = '';
			document.getElementById('nie').style.display = 'block';
//			document.forms['regForm'].cif.disabled = 
			document.forms['regForm'].nif.disabled = document.forms['regForm'].otro.disabled = true;
			document.forms['regForm'].nie.disabled = false;
			document.forms['regForm'].nie.focus();
			break;
		default:
//			document.getElementById('cif').style.display = 
			document.getElementById('nif').style.display = document.getElementById('nie').style.display = 'none';
//			document.getElementById('cif-error').innerHTML = 
			document.getElementById('nif-error').innerHTML = document.getElementById('nie-error').innerHTML = '';
			document.getElementById('otro').style.display = 'block';
//			document.forms['regForm'].cif.disabled = 
			document.forms['regForm'].nif.disabled = document.forms['regForm'].nie.disabled = true;
			document.forms['regForm'].otro.disabled = false;
			document.forms['regForm'].otro.focus();
			break;
	}
}

function procesarDatosRegistro() {
	//Comprobamos que todos los campos están rellenados.
	var _form = document.forms['regForm'];
	var i = 0;
	var max = _form.length;

	_form.subm.value = 'Procesando datos...';
	_form.subm.disabled = true;
	return true;
}

function comprobarDNS(accion) {
	var fDNS = document.Fdns;
	switch (accion) {
		case 1:
			if (fDNS.n1.value != '' && fDNS.n2.value != '' && fDNS.i1.value != '' && fDNS.i2.value != '') fDNS.dDNS.checked = false;
			else {
				fDNS.n1.value = fDNS.n2.value = fDNS.i1.value = fDNS.i2.value = '';
				fDNS.dDNS.checked = true;
			}
			break;
		case 2:
			if (fDNS.dDNS.checked == true) {
				fDNS.n1.value = fDNS.n2.value = fDNS.i1.value = fDNS.i2.value = '';
			}
			break;
	}
}
/*Búsqueda avanzada*/
function borrarHistorialBusqueda() {
	var respuesta = confirm('¿Desea realmente borrar esta búsqueda?');
	if (respuesta === true) {
		document.historialBusqueda.borrar.value='Procesando datos...';
		document.historialBusqueda.submit();
		document.historialBusqueda.borrar.disabled=true;
	}
}

function busquedaAvanzada(campo) {
	document.fbuscadorAvanzado.accion.value = 'buscar';
	campo.value = 'Buscando...';
	document.fbuscadorAvanzado.submit();
	campo.disabled = true;
}

function guardarBusqueda(campo) {
	if (document.fbuscadorAvanzado.gbus.value != '') {
		document.fbuscadorAvanzado.accion.value = 'guardar';
		campo.value = 'Procesando datos...';
		document.fbuscadorAvanzado.submit();
		campo.disabled = true;
	}
	else {
		alert('Debe introducir una descripción para la búsqueda.');
	}
}

function ocultarBuscador(campo) {
	if (document.getElementById('divHistorialBusqueda')) flip('divHistorialBusqueda');
	if (document.getElementById('divBuscadorAvanzado')) flip('divBuscadorAvanzado');
	
	if (document.getElementById('divBuscadorAvanzado').style.display === 'none') {
		campo.innerHTML = '<img src="images/expand.jpg" alt="Mostrar panel de búsquedas"> Mostrar panel de búsquedas';
		campo.title = 'Ocultar panel de búsquedas';
	}
	else {
		campo.innerHTML = '<img src="images/expand2.jpg" alt="Ocultar panel de búsquedas"> Ocultar panel de búsquedas';
		campo.title = 'Ocultar panel de búsquedas'
	}
}

function datosEmpresa(activado) {
	var _ff = document.regForm;
	if (activado == true) {
		_ff.compania.disabled = _ff.direccionEmpresa.disabled = _ff.municipioEmpresa.disabled = _ff.paisEmpresa.disabled = _ff.cpEmpresa.disabled = _ff.tlfEmpresa.disabled = _ff.provinciaGEmpresa.disabled = false;
	}
	else {
		_ff.compania.disabled = _ff.direccionEmpresa.disabled = _ff.municipioEmpresa.disabled = _ff.paisEmpresa.disabled = _ff.cpEmpresa.disabled = _ff.tlfEmpresa.disabled = _ff.provinciaGEmpresa.disabled = true;
	}
}

function borrarPuja(r) {
  var i = r.parentNode.parentNode.rowIndex
  document.getElementById('tablaPujas').deleteRow(i)
}

function aplicarPujaGlobal(valor) {
	var _form = document.pujaForm;
	
	var i = 0;
	var max = _form.length;
	
	for (; i < max; i ++) {
		if (null != (/^(mpuja)(.*)$/i.exec(_form[i].name))) {
			_form[i].value = valor;
		}
	}
}

function seleccionaTodos(formulario) {
	var max = formulario.length;
	var i = 0;
	for (; i < max; i ++) {
		if (formulario[i].type == 'checkbox' && formulario[i].checked == false) {
			formulario[i].checked = true;
		}
	}
}

function invertirSeleccion(formulario) {
	var max = formulario.length;
	var i = 0;
	for (; i < max; i ++) {
		if (formulario[i].type == 'checkbox') {
			formulario[i].checked = (formulario[i].checked == false) ? true : false;
		}
	}
}

function pujaIndividual(dominio, boton) {
	boton.disabled = true;
	var req = new XMLHttpRequest();
	if (req) {
	 	req.onreadystatechange = function() {
	 	 	if (req.readyState == 4 && (req.status == 200 || req.status == 304)) {
	 	 		var resp = eval('(' + req.responseText + ')');
	 	 		document.getElementById('e'+dominio).className = resp.estado;
	 	 		document.getElementById('e'+dominio).innerHTML = resp.msg;
	 	 		document.getElementById('pr'+dominio).innerHTML = resp.precio;
	 	 		document.getElementById('m'+dominio).innerHTML = resp.max;
	 	 		document.getElementById('t'+dominio).innerHTML = resp.tiempoQueda;
	 	 		
	 	 		if (resp.ampliacionTexto != undefined) {
	 	 			document.getElementById('t'+dominio).innerHTML += '<br><span id="fase" class="' + resp.ampliacion + '">Ampliaci&oacute;n: ' + resp.ampliacionTexto + '</span> <a style="font-size:80%;text-decoration:underline;cursor:pointer;" onclick="abrir(\'info.php?tipo=ampliacion\', 300, 350, \'si\')">[?]</a>';
				}
	 	 		
	 	 		document.getElementById('pu'+dominio).innerHTML = resp.pujas;
	 	 		document.pujaForm['mpuja['+dominio+']'].value = resp.pujaMin;
			}
			boton.disabled = false;
	  };
	  req.open('GET', 'detallesAjax.php?p='+dominio+'&mpuja='+document.pujaForm['mpuja['+dominio+']'].value);
	  req.send(null);
	}
}

function actualizarPujaIndividual(dominio) {
	var req = new XMLHttpRequest();
	if (req) {
	 	req.onreadystatechange = function() {
	 	 	if (req.readyState == 4 && (req.status == 200 || req.status == 304)) {
	 	 		var resp = eval('(' + req.responseText + ')');
	 	 		document.getElementById('e'+dominio).className = resp.estado;
	 	 		document.getElementById('e'+dominio).innerHTML = resp.msg;
	 	 		document.getElementById('pr'+dominio).innerHTML = resp.precio;
	 	 		document.getElementById('m'+dominio).innerHTML = resp.max;
	 	 		document.getElementById('t'+dominio).innerHTML = resp.tiempoQueda;
	 	 		
	 	 		if (resp.ampliacionTexto != undefined) {
	 	 			document.getElementById('t'+dominio).innerHTML += '<br><span id="fase' +dominio+ '" class="' + resp.ampliacion + '">Ampliaci&oacute;n: ' + resp.ampliacionTexto + '</span> <a style="font-size:80%;text-decoration:underline;cursor:pointer;" onclick="abrir(\'info.php?tipo=ampliacion\', 300, 350, \'si\')">[?]</a>';
				}
	 	 		
	 	 		document.getElementById('pu'+dominio).innerHTML = resp.pujas;
	 	 		document.pujaForm['mpuja['+dominio+']'].value = resp.pujaMin;
	 	 		
	 	 		if (resp.tiempoQueda == '') {
	 	 			document.getElementById('lp'+dominio).innerHTML = '';
	 	 			document.getElementById('f'+dominio).innerHTML = 'Cerrada';
	 	 		}
	 	 		return;
			}
	  };
	  req.open('GET', 'operaciones.php?accion=actualizarPuja&p='+dominio);
	  req.send(null);
	}
	
}

function actualizarPujas() {
	var _tabla = document.getElementById('tablaPujas');
	var _tr = _tabla.getElementsByTagName('tr');
	var i = 0;
	var max = _tr.length;
	var _id = '';
	for (; i < max; i ++) {
		if (_tr[i].id != 'undefined' && _tr[i].id != '') {
			_id = _tr[i].id.substr(1);
			if (document.pujaForm['mpuja['+_id+']'] && document.pujaForm['mpuja['+_id+']'].disabled != true) {
				actualizarPujaIndividual(_id);
			}
		}
	}
}

function cancelarPujaInicial(dominio, linea) {
	var req = new XMLHttpRequest();
	if (req) {
	 	req.onreadystatechange = function() {
	 	 	if (req.readyState == 4 && (req.status == 200 || req.status == 304)) {
	 	 		if (req.responseText == 'OK') {
	 	 			alert('La puja ha sido cancelada');
	 	 			borrarPuja(linea);
	 	 		}
	 	 		else {
	 	 			alert('La puja no ha podido ser cancelada. Si desea saber el motivo, contacte con info[@]backorders.es indicando el dominio y la hora del suceo.');
	 	 		}
			}
	  };
	  req.open('GET', 'operaciones.php?accion=borrarPuja&d=' + dominio);
	  req.send(null);
	}
}