// Mostramos las Capas
function mostrar(el)
{
	var obj = document.getElementById(el).style.display;
	if(obj == 'none')
	document.getElementById(el).style.display = ""
	else
	document.getElementById(el).style.display = "none";
}

function cargar_tipology(id,div,ntype)
{
	divLogin = document.getElementById(div);
	var ajax = new XMLHttpRequest();
	ajax.open("GET",'change_tipology.php?id='+id+'&name_type='+ntype,true);
	ajax.onreadystatechange=function()
	{
		if (ajax.readyState == 4)
		{
			divLogin.innerHTML = ajax.responseText;
		}
	}
	ajax.send(null);
}

function printContents(nombre)
{
	var ficha = document.getElementById(nombre);
	var ventimp = window.open(' ', 'popimpr');
	ventimp.document.write(ficha.innerHTML );
	ventimp.document.close();
	ventimp.print( );
	ventimp.close();
}

function chg_location(id)
{
	var divLogin =  document.getElementById('divlocation');
	//ajaxManager('update','change_location.php?id=' + id ,'divlocation','');
	var ajax = new XMLHttpRequest();
	ajax.open("GET",'change_location.php?id=' + id,true);
	ajax.onreadystatechange=function()
	{
		if (ajax.readyState == 4)
		{
			divLogin.innerHTML = ajax.responseText;
		}
	}
	ajax.send(null);
}

function OnlyNumber(e)
{
	var isIE = document.all?true:false;
	var key = (isIE) ? window.event.keyCode : e.which;
	return ((key > 47 && key < 58) || (key == 43) || (key == 45) || (key == 40) || (key == 41) || (key < 33)) ? true:false;
}

function CheckMail(texto)
{
	var mailres = true;
	var cadena = "abcdefghijklmnñopqrstuvwxyzABCDEFGHIJKLMNÑOPQRSTUVWXYZ1234567890@._-";

	var arroba = texto.indexOf("@",0);
	if ((texto.lastIndexOf("@")) != arroba) arroba = -1;

	var punto = texto.lastIndexOf(".");

	for (var contador = 0 ; contador < texto.length ; contador++){
		if (cadena.indexOf(texto.substr(contador, 1),0) == -1){
			mailres = false;
			break;
		}
	}

	if ((arroba > 1) && (arroba + 1 < punto) && (punto + 1 < (texto.length)) && (mailres == true) && (texto.indexOf("..",0) == -1))
	mailres = true;
	else
	{
		alert('Tu mail no es correcto');
		mailres = false;		
	}
	

	return mailres;
}


function send_form()
{
	var tipo = document.getElementById('tipo');
	var metros = document.getElementById('metros');
	var precio = document.getElementById('precio');
	var lugar = document.getElementById('lugar');
	var telefono = document.getElementById('telefono2');
	var nombre = document.getElementById('nombreape');
	var mail = document.getElementById('mail2');
	

	var error = 0;

	if(tipo.value=='')
	{
		alert("Tipo de Vivienda Vacia");
		tipo.focus();
		error=1;
		return false
	}

	if(telefono.value=='')
	{
		alert("Su teléfono esta vacío");
		telefono.focus();
		error=1;
		return false
	}

	if(nombre.value=='')
	{
		alert("Ponga su nombre, por favor.");
		nombre.focus();
		error=1;
		return false
	}

	if(mail.value=='')
	{
		alert("Su mail esta vacío");
		mail.focus();
		error=1;
		return false
	}
	else
	{
		if(!CheckMail(mail.value))
		{
			mail.focus();
			return false;
		}
	}


	var divResultado =  document.getElementById('divfrmnotfound');

	if(error==0)
	{
		var ajax = new XMLHttpRequest();
		ajax.open("GET",'sendmail.php?frm=1&tipo=' + tipo.value +'&metros=' + metros.value +'&precio=' + precio.value + '&lugar=' + lugar.value + '&tlf=' +telefono.value+ '&nombre='+nombre.value+'&mail='+mail.value,true);
		ajax.onreadystatechange=function()
		{
			if (ajax.readyState == 4)
			{
				divResultado.innerHTML = ajax.responseText;

				// Limpiamos form
				tipo.value='';
				metros.value='';
				precio.value='';
				lugar.value='';
				telefono.value='';
				nombre.value='';
				mail.value='';
			}
		}
		ajax.send(null);
	}
}


function send_form2()
{


	var nombre = document.getElementById('nombre');
	var ape = document.getElementById('ape');
	var mail = document.getElementById('mail');
	var dire = document.getElementById('dire');
	var telefono = document.getElementById('tlf');
	var txt = document.getElementById('txt');

	var divResultado =  document.getElementById('divfrmcontact');
	var error = 0;

	if(nombre.value=='')
	{
		alert("Su nombre esta vacío!");
		nombre.focus();
		error=1;
		return false
	}

	if(mail.value=='')
	{
		alert("Su mail esta vacío");
		mail.focus();
		error=1;
		return false
	}
	else
	{
		if(!CheckMail(mail.value))
		{
			mail.focus();
			return false;
		}
	}

	if(telefono.value=='')
	{
		alert("Su teléfono esta vacío");
		telefono.focus();
		error=1;
		return false
	}

	if(txt.value=='')
	{
		alert("Su mensaje esta vacío!");
		txt.focus();
		error=1;
		return false
	}


	if(error==0)
	{
		var ajax = new XMLHttpRequest();
		ajax.open("GET",'sendmail.php?frm=2&nombre=' + nombre.value +'&ape=' + ape.value +'&mail=' + mail.value + '&dire=' + dire.value + '&tlf=' +telefono.value+ '&txt='+txt.value,true);
		ajax.onreadystatechange=function()
		{
			if (ajax.readyState == 4)
			{
				divResultado.innerHTML = ajax.responseText;

				// Limpiamos form
				nombre.value='';
				ape.value='';
				mail.value='';
				dire.value='';
				telefono.value='';
				txt.value='';
			}
		}
		ajax.send(null);
	}
}