tipo = 0;
ruta = '/';
function visible(valor, valor2)
{
	if(document.getElementById(valor).style.visibility == 'visible')
	{
		document.getElementById(valor).style.visibility = 'hidden';
	}
	else
	{
		document.getElementById(valor).style.visibility = 'visible';
		contacto(valor2);
	}
}
function contacto(valor)
{
	envioInfo(valor);
}
function CreateXmlHttp()
{
    var xmlHttp;

	// Probamos con IE
    try
    {
        // Funcionará para JavaScript 5.0
        xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch(e)
    {
        try
        {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch(oc)
        {
            xmlHttp = null;
        }
    }

    // Si no se trataba de un IE, probamos con esto
    if(!xmlHttp && typeof XMLHttpRequest != "undefined")
    {
        xmlHttp = new XMLHttpRequest();
    }

    return xmlHttp;
}
function envioInfo(txt)
{
    // 1.- Creamos el objeto xmlHttpRequest
    var xmlHttp = CreateXmlHttp();

    // 2.- Definimos la llamada para hacer un simple GET.
    var ajaxRequest = ruta + 'includes/contacto.php?anuncio=' + txt;

    // 4.- Enviar
    xmlHttp.open("GET", ajaxRequest, true);
    xmlHttp.send("");
}
function mostrar(valor)
{
	if(valor == 3)
	{
		tipo = 1;
	}
	else if(valor == 2 || valor == 4 || (valor >= 11 && valor <=17))
	{
		tipo = 2;
	}
	else
	{
		tipo = 0;
	}

	switch(tipo)
	{
		case 1:
			display('coches', true);
			display('inmuebles', false);
			display('dormitorios', false);
			break;
		case 2:
			display('coches', false);
			display('inmuebles', true);
			display('dormitorios', valor != 12 && valor != 13 && valor != 14);
			break;
		default:
		case 0:
			display('coches', false);
			display('inmuebles', false);
			display('dormitorios', false);
			break;
	}
}

function IsNumeric(sText)
{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

   for (i = 0; i < sText.length && IsNumber == true; i++)
      {
      Char = sText.charAt(i);
      if (ValidChars.indexOf(Char) == -1)
         {
         IsNumber = false;
         }
      }
   return IsNumber;
}

function comprobarcontacto()
{
	if(document.contacto.captcha.value == "")
	{
		alert("Debes introducir el código de verificación.");
		return;
	}
	if(document.contacto.nombre.value == "" || document.contacto.email.value == "" || (document.contacto.mensaje != null && document.contacto.mensaje.value == ""))
	{
		alert("Debes de rellenar todos los campos del formulario de contacto.");
		return;
	}
	document.contacto.submit();
}
function comprobar()
{
	valido = 1;
	numero = 1;
	if(document.frm.captcha.value == "")
	{
		alert("Debes introducir el código de seguridad.");
		return;
	}
	switch(tipo)
	{
		case 1:
			if(document.frm.km.value == "" || document.frm.marcas.value == 0)
			{
				valido = 0;
			}
			if(!IsNumeric(document.frm.km.value))
			{
				numero = 0;
			}
		break;
		case 2:
			if(document.frm.dormitorios.value == "" || document.frm.metros.value == "")
			{
				valido = 0;
			}
			if(!IsNumeric(document.frm.dormitorios.value) && !IsNumeric(document.frm.metros.value))
			{
				numero = 0;
			}
		break;
		default:
		case 0:
		break;
	}
	if((document.frm.categorias.value == 0 || document.frm.ciudad.value == 0 || document.frm.precio.value == "" && document.frm.demanda[1].checked != false) || document.frm.descripcion.value == "" || document.frm.email.value == "" || document.frm.contrasena.value == "")
	{
		valido = 0;
	}
	if(!IsNumeric(document.frm.precio.value))
	{
		numero = 0;
	}
	if(valido == 1 && numero == 1)
	{
		document.frm.submit();
	}
	else
	{
		if(numero == 0)
		{
			alert('Has introducido caracteres no numéricos en campos numéricos.');
		}
		if(valido == 0)
		{
			alert('Compruebe que ha introducido todos los datos correctamente.');
		}
	}
}

function popUp(URL)
{
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=800,height=500');");
}

function display(obj_id, valor)
{
	var obj = document.getElementById(obj_id);
	if(obj != null)
	{
		obj.style.display = valor ? '' : 'none';
		obj.style.visibility = valor ? 'visible' : 'hidden';
	}
}
