//funciones de EldarBerserker XD o sea Erwin San Martin Lassalle XD

function objetoAjax()
{
	var xmlhttp=false;
	try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
		   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			xmlhttp = false;
  		}
	}

	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
		xmlhttp = new XMLHttpRequest();
	}
	return xmlhttp;
}

function llenaSelect(fuente, destino, pagina, parametros)
{
	listBox = document.getElementById(fuente);					
	valor = listBox.options[listBox.selectedIndex].value;
	divContenido = document.getElementById(destino);
	
	ajax=objetoAjax();
	
	ajax.open("POST", pagina+'.php', true);
	
	ajax.onreadystatechange = revisar_content;
	
	function revisar_content()
	{
		if(ajax.readyState == 1)
		{
			divContenido.innerHTML = '<table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td align="center" valign="top" class="piedepagina"><img src="../imagenes/spinner.gif"><br>Procesando Informaci&oacute;n</td></tr></table>';
		}
		
		if(ajax.readyState == 4)
		{
			//alert(ajax.responseText);
			divContenido.innerHTML = ajax.responseText;
		}
	}	
	
	params = fuente+'='+valor;
	if(parametros!=undefined)
	{
			params = params + '&' + parametros;
	}
	ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	ajax.send(params);
}

function accion(div_id, pagina, parametro, form)
{
	divContenido = document.getElementById(div_id);
	
	//alert(parametro);
	
	ajax=objetoAjax();
	
	ajax.open("POST", pagina + '.php', true);
	
	ajax.onreadystatechange = revisar_content;
	
	function revisar_content()
	{
		if(ajax.readyState == 1)
		{
			divContenido.innerHTML = '<table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td align="center" valign="top" class="piedepagina"><img src="../imagenes/spinner.gif"><br>Procesando Informaci&oacute;n</td></tr></table>';
		}
		
		if(ajax.readyState == 4)
		{
			//alert(ajax.responseText);
			divContenido.innerHTML = ajax.responseText;
		}
	} 	
	
	//enviando los valores
	if(parametro != undefined)
	{
		//alert(parametro);
		ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		ajax.send(parametro)
	}	
	else
	{
		//alert('no tiene parametro');
		if (form != undefined)
		{
			ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
			parametros = '';
			formulario = document.getElementById(form);
			
			for(i=0; i<formulario.elements.length; i++)
			{
				 elemento = formulario.elements[i];
				 if(parametros.length>0)
				 {
						parametros = parametros + "&";
				 }
				 parametros = parametros + elemento.name + '=' + encodeURIComponent(elemento.value);
			}
			//alert(parametros);
			ajax.send(parametros);			
		}
		else
		{
			ajax.send('');
		}
	}
}

function cerrarSesion()
{
	sesion = objetoAjax();	
	sesion.open("GET", 'cerrarsesion.php');
	sesion.onreadystatechange = revisar;
	sesion.send('');
	
	function revisar()
	{
		if(sesion.readyState == 4)
		{
			document.location = 'index.php';
		}
	}
}

function revisaUser(username, div_id)
{
	divID = document.getElementById(div_id);
	
	ajax = objetoAjax();	
	ajax.open("POST", 'usr_check.php', true);
	ajax.onreadystatechange = revisar;
		
	function revisar()
	{
		if(ajax.readyState == 4)
		{
			respuesta = ajax.responseText;			
			
			if(respuesta.search(/disponible/) != -1)
			{
				divID.innerHTML = 'Disponible';
				divID.style.background = "#008000";
			}
			else
			{
				divID.innerHTML = 'No Disponible';
				divID.style.background = "#CD2727";
			}		
		}
	}
	
	ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	//enviando los valores
	ajax.send('username=' + username)
	//ajax.send(null);
}


function iniciarSesion(div_id, formulario)
{
	divContenido = document.getElementById(div_id);
	usern = formulario.username.value;
	passw = formulario.password.value;
	
	ajax=objetoAjax();
	
	ajax.open("POST", 'login.php', true);
	ajax.onreadystatechange = revisar_content;

	function revisar_content()
	{
		if(ajax.readyState == 1)
		{
			divContenido.innerHTML = '<table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td align="center" valign="top" class="piedepagina"><img src="../imagenes/spinner.gif"><br>Iniciando Sesi&oacute;n</td></tr></table>';
		}
		
		if(ajax.readyState == 4)
		{
			respuesta = ajax.responseText;			
			
			//divContenido.innerHTML = ajax.responseText;
			if(respuesta.search(/correcto/) != -1)
			{
				document.location = 'cpanel.php';				
			}
			else
			{
				document.location = 'index.php?' + ajax.responseText;
			}		
		}
	} 	
	ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	//enviando los valores
	ajax.send('username=' + usern + '&password=' + passw)
	//ajax.send(null);
}

/*function arreglacaracteres(cadena)
{
	var caracteresraros = Array('á','Á','é','É','í','Í','ó','Ó','ú','Ú','ñ','Ñ');
	var codigoscorrectos = Array('&aacute;','&Aacute;','&eacute;','&Eacute;','&iacute;','&Iacute;', '&oacute;','&Oacute;','&uacute;','&Uacute;','&ntilde;','&Ntilde;');
	
	for(i=0; i<=caracteresraros.length; i++)
	{
		cadena.replace(caracteresraros[i], codigoscorrectos[i]);
	}
	return cadena;
	
}*/

function esconde(div_id)
{
	divVisible = document.getElementById(div_id);
	
	divVisible.style.visibility = "hidden";
	divVisible.style.display = "none";
	
	//new Effect.Scale(divVisible, 0, {scaleX: false, duration: 0.5, queue: 'front'});
}

function muestra(div_id)
{
	divOculto = document.getElementById(div_id);
	
	divOculto.style.visibility = "visible";
	divOculto.style.display = "block";
	//new Effect.Scale(divOculto, 100, {scaleX: false, duration: 0.5, queue: 'front'});
}

function verificaOrden(formulario)
{
	
	
	nombre = formulario.nombre.value;
	fono = formulario.fono.value;
	movil = formulario.movil.value;
	email = formulario.correo.value;
	id_propiedad = formulario.id_propiedad.value;
	//alert(nombre + "\n" + fono + "\n" + movil + "\n" + email + "\n" + mensaje);	
	if(!vacio(nombre))
	{
		 alert("Ingrese su Nombre\n");
		 return false;	
	}
	else if((!vacio(fono) || fono.length<6 ) && (!vacio(movil) || movil.length<8))
	{
		alert("Ingrese el Teléfono o su Celular.\nDebe ingresar uno o ambos.");
	 	return false;
	}
	else if(!validaMail(email))
	{		
	 	return false;
	}
	else
	{			
			accion('mailer_content','mailer',undefined,formulario.id)
			return true;
	}
}

function verificaFormulario(formulario)
{
	
	
	nombre = formulario.nombre.value;
	fono = formulario.fono.value;
	movil = formulario.movil.value;
	email = formulario.correo.value;
	mensaje = formulario.mensaje.value;
	//alert(nombre + "\n" + fono + "\n" + movil + "\n" + email + "\n" + mensaje);	
	if(!vacio(nombre))
	{
		 alert("Ingrese su Nombre\n");
		 return false;	
	}
	else if((!vacio(fono) || fono.length<6 ) && (!vacio(movil) || movil.length<8))
	{
		alert("Ingrese el Teléfono o su Celular.\nDebe ingresar uno o ambos.");
	 	return false;
	}
	else if(!validaMail(email))
	{		
	 	return false;
	}
	else if(!vacio(mensaje))
	{
		alert("Ingrese su consulta o su mensaje.");
	 	return false;
	}
	else
	{			
			//accion('mailer_content','mailer',undefined,formulario.id)
			formulario.submit();
			return true;
	}
}

function vacio(q) 
{  
	for ( i = 0; i < q.length; i++ ) 
	{  
		if ( q.charAt(i) != " " )
		{  
			return true;
		}  
	}  
	return false  
}  
   
//valida que el campo no este vacio y no tenga solo espacios en blanco  
function valida(texto)
{  
	if( vacio(texto) == false ) 
	{  
		alert("Introduzca un cadena de texto válida.");  
		return false;
	}
	else
	{  
		return true;  
	}  
}

function validaNumeros(e)
{
	tecla = (window.event) ? window.event.keyCode : e.which;
	//alert (tecla);
	if(tecla == 8 || tecla == 0)
	{
		return true;
	}
	patron = /[1234567890]/;
	te = String.fromCharCode(tecla);
	return(patron.test(te));
}

function validaNumerosEspecial(e)
{
	tecla = (window.event) ? window.event.keyCode : e.which;
	//alert (tecla);
	if(tecla == 8 || tecla == 0)
	{
		return true;
	}
	patron = /[1234567890,]/;
	te = String.fromCharCode(tecla);
	return(patron.test(te));
}

function validaMail(txt)
{         
	//expresion regular  
 	if(vacio(txt) == false) 
	{
		alert('Este campo (email) no puede quedar vacío');
		return false;
	}
	else
	{
		var b=/^[^@\s]+@[^@\.\s]+(\.[^@\.\s]+)+$/;
	 
	 if(!b.test(txt))
	 {
	 //comentar la siguiente linea si no se desea que aparezca el alert()  
		 alert("E-mail " + (b.test(txt)?"":"no ") + "válido.")  
	 }
	 //devuelve verdadero si validacion OK, y falso en caso contrario  
		return b.test(txt);
	}
}

function validaMail2(txt)
{         
	//expresion regular  
 	if(!vacio(txt)) 
	{		
		var b=/^[^@\s]+@[^@\.\s]+(\.[^@\.\s]+)+$/;
	 
	 	if(!b.test(txt))
	 	{
	 	//comentar la siguiente linea si no se desea que aparezca el alert()  
		 	alert("E-mail " + (b.test(txt)?"":"no ") + "válido.")  
	 	}
	 //devuelve verdadero si validacion OK, y falso en caso contrario  
		return b.test(txt);
	}
	else
	{
		return(true);
	}
}

function continuaBusqueda(form, iniciox, ordenx)
{
		if(vacio(iniciox))
		{
			form.inicio.value = iniciox;
		}
		else
		{
			form.inicio.value = 0;
		}
		
		form.orden.value = ordenx;
		
		form.submit();
}

function verificaCClave(div, pagina, formulario)
{
	
	
	vieja = formulario.c_vieja.value;
	nueva1 = formulario.c_nueva1.value;
	nueva2 = formulario.c_nueva2.value;
	//alert(nombre + "\n" + fono + "\n" + movil + "\n" + email + "\n" + mensaje);	
	if(!vacio(vieja))
	{
		 alert("Debe ingresar la contraseña anterior.");
		 return false;	
	}	
	else if((!vacio(nueva1)) || (!vacio(nueva2)))
	{		
	 	alert("Debe ingresar la nueva contraseña en los dos campos indicados.\nEstos campos no pueden quedar vacíos.");
		return false;		
	}
	else if(nueva1 != nueva2)
	{
		alert("Los campos de contraseña nueva, no coinciden.\nRevise y vuelva a intentarlo");
	 	return false;
	}
	
	else
	{					
			accion(div, pagina, undefined, formulario.id);
			return true;
	}
}

function comboiframebanner(n_iframe, valor)
{
	frm = document.getElementById(n_iframe);
	
	pagina = "bannermedio.php?id_banner=" + valor;
	frm.src=pagina;
}

function comboiframe(n_iframe, archivo, parametros)
{
	frm = document.getElementById(n_iframe);
	
	pagina = archivo + '.php?' + ((parametros != '') ? parametros : '' );
	//alert(pagina);
	frm.src=pagina;
}

function adminregyciu(accion)
{	
	anterior = document.getElementById('anterior');	
	//alert(accion);
	//alert(anterior.value);
	if(accion != anterior.value)
	{
		if(accion != 'nada')
		{
			if(anterior.value != 'nada')
			{
				accion_anterior = document.getElementById(anterior.value);
				new Effect.BlindUp(accion_anterior);		
			}
			
			accion_nueva = document.getElementById(accion);
			new Effect.BlindDown(accion_nueva);
			anterior.value = accion;
		}
		else
		{
			if(anterior.value != 'nada')
			{
				accion_anterior = document.getElementById(anterior.value);
				new Effect.BlindUp(accion_anterior);		
			}
			
			anterior.value = accion;
			//alert(accion);
		}
	}
}

function actualizaregyciu(valor)
{
	objeto = document.getElementById('region');
	objeto.selectedIndex = (valor - 1);
	objeto.focus();
}

function mminfo(valor, boton)
{
	boton_muestra = document.getElementById('div_show_' + valor);
	boton_esconde = document.getElementById('div_hide_' + valor);
	div_datos = document.getElementById('datos_prop_' + valor);
	
	if(boton == 'muestra')
	{
		boton_muestra.style.display = 'none';
		boton_esconde.style.display = 'block';
		new Effect.BlindDown(div_datos);
	}
	
	if(boton == 'esconde')
	{
		boton_muestra.style.display = 'block';
		boton_esconde.style.display = 'none';
		new Effect.BlindUp(div_datos);
	}
}

function mminfocorr(valor, boton)
{
	boton_muestra = document.getElementById('div_show_' + valor);
	boton_esconde = document.getElementById('div_hide_' + valor);
	div_datos = document.getElementById('datos_corr_' + valor);
	
	if(boton == 'muestra')
	{
		boton_muestra.style.display = 'none';
		boton_esconde.style.display = 'block';
		new Effect.BlindDown(div_datos);
	}
	
	if(boton == 'esconde')
	{
		boton_muestra.style.display = 'block';
		boton_esconde.style.display = 'none';
		new Effect.BlindUp(div_datos);
	}
}

function mminfoorden(valor, boton)
{
	boton_muestra = document.getElementById('div_show_' + valor);
	boton_esconde = document.getElementById('div_hide_' + valor);
	div_datos = document.getElementById('datos_orden_' + valor);
	
	if(boton == 'muestra')
	{
		boton_muestra.style.display = 'none';
		boton_esconde.style.display = 'block';
		new Effect.BlindDown(div_datos);
	}
	
	if(boton == 'esconde')
	{
		boton_muestra.style.display = 'block';
		boton_esconde.style.display = 'none';
		new Effect.BlindUp(div_datos);
	}
}

function eliminaorden(valor)
{
	div_container = document.getElementById('orden_' + valor);
	id_orden = valor;
	
	//new Effect.BlindUp(div_container);
	
	orden = objetoAjax();	
	orden.open("POST", 'eliminaorden.php', true);
	orden.onreadystatechange = revisar;	
	
	function revisar()
	{
		if(orden.readyState == 4)
		{
			new Effect.BlindUp(div_container);
		}
	}
	
	orden.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	orden.send('id_orden='+valor);
}

function revisaform(form)
{
	nombre = form.nombre.value;
	ciudad = form.ciudad.value;
	fono = form.telefono.value;			
	movil = form.fonomovil.value;
	email = form.correo.value;
	mensaje = form.mensaje.value;
	
	if(!vacio(nombre) ||  !vacio(ciudad)  ||  (!vacio(fono) &&  !vacio(movil)) ||  !vacio(email) ||  !vacio(mensaje))
	{
		alert('Debe ingresar todos los datos marcados con un *');
		return false;
	}
	else if(!validaMail(email))
	{
		alert('El email ingresado no parece válido.\nCorrijalo y vuelva a intentarlo nuevamente');
		return false;
	}
	else
	{
		//this.submit();
		accion('enviamensaje', 'mailer_corredor2', undefined, form.id);
		//return true;
	}
}