// JavaScript Document
/*********** AJAX *****************************/
var navegador = navigator.appName;

function nuevoAjax(){
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 abrirVtna(url,ancho,alto)
{
	if (navegador.indexOf('Internet Explorer') == -1){
		left = (screen.width) ? (screen.width-ancho)/2 : 0;
		top = (screen.height) ? (screen.height-alto)/2 : 0;
		window.open(url,'_blank',"width="+ancho+",height="+alto+",left="+left+",top="+top);
	}else{
		window.open(url,'_blank',"width="+ancho+",height="+alto);
	}
}
function volver(){
	//if (navegador.indexOf('Internet Explorer') == -1){
		if (window.opener){
			window.close();
		}else{
			window.history.back();
		}
	/*}else{
		window.location.href='aviso.php?do=list';
	}*/
	/*
	if (window.opener == null || window.opener == 'undefined'){
		if (window.opener !== null)
			window.location.href='aviso.php?do=list';
		else
			window.history.back();
	}
	else{
		window.close();
	}*/
}
/*
function switchEmpresa(objetoOrigen, modificacion){ 
	var modificar;
	if(modificacion == null || modificacion == 'undefined'){ modificar = true; }else{ modificar = modificacion; }
	var empresa = $('mostrarEmpresa');
	var data = objetoOrigen.value;
	var ajax=nuevoAjax();
	var path = '/templates/smarty/templates/admin/ajax/';
	ajax.open("POST", path+"cambiarEmpresa.php",true);
	ajax.onreadystatechange=function() { if (ajax.readyState==4) { empresa.innerHTML = ajax.responseText; } }
	ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajax.send("empresa_id="+data+"&modificar="+modificar);
}
*/
function cambiarMedida(valor)
{
	if (valor == 1)
	{
		document.getElementById('minMedida').innerHTML = 'has.';
		document.getElementById('maxMedida').innerHTML = 'has.';
	}
	else
	{
		document.getElementById('minMedida').innerHTML = 'm2';
		document.getElementById('maxMedida').innerHTML = 'm2';
	}
}
function cambiarSource(id,source)
{
	//alert(source);
	document.getElementById(id).src = source;	
}
function cambiarVisibilidad(id)
{ 
  if (document.getElementById(id).style.display == "block")
	  document.getElementById(id).style.display = "none";
  else
	  document.getElementById(id).style.display = "block"; 
}
function cambiarContenido(obj,contenido1,contenido2)
{
  if (obj.innerHTML == contenido1)
	  obj.innerHTML = contenido2;
  else
	  obj.innerHTML = contenido1;
}
function cambiarImagen(obj,contenido1,contenido2)
{
  if (obj.src.indexOf(contenido1) != -1)
	  obj.src = obj.src.replace(contenido1,contenido2);
  else
	  obj.src = obj.src.replace(contenido2,contenido1);
}
function cambiarBckImagen(obj,contenido1,contenido2)
{
  if (obj.style.backgroundImage.indexOf(contenido1) != -1)
	  obj.style.backgroundImage = obj.style.backgroundImage.replace(contenido1,contenido2);
  else
	  obj.style.backgroundImage = obj.style.backgroundImage.replace(contenido2,contenido1);
}
function cambiarDisplay(id)
{ 
  if (document.getElementById(id).style.display == "none")
	  document.getElementById(id).style.display = "";
  else
	  document.getElementById(id).style.display = "none";
}	
function cambiarImgPrin(idFoto,idPrin)
{ 
	if (idFoto == idPrin)
	{
		$('sp-prin').style.display = "";
		$('a-prin').style.display = "none";
	}
	else
	{
		$('a-prin').style.display = "";
		$('sp-prin').style.display = "none";
	}
}	
function displayValores(valor)
{ 
	if (valor == 2 || valor ==3)
	{
		$('div-ven').style.display = 'none';
		$('div-alq').style.display = '';
	}
	else if (valor == 1)
	{
		$('div-alq').style.display = 'none';
		$('div-ven').style.display = '';
	}
	else
	{
		$('div-alq').style.display = '';
		$('div-ven').style.display = '';
	}
}	
function sendAjaxRequest(obj_ajax,file,obj_respuesta,query_string,texto_carga,obj_carga,method,async,respuesta,path)
{

	if (texto_carga == null)
		texto_carga = "Enviando notificaci&oacute;n...";
	if (path == null)
		path = "./";
	if (method == null)
		method = "POST";
	if (async == null)
		async = true;
	if (obj_carga == null)
		obj_carga = obj_respuesta;
	obj_ajax.open(method, path+file,async);
	obj_ajax.onreadystatechange=function() 
	{ 
		if (obj_ajax.readyState==4) 
		{ 
				if (respuesta == null || respuesta == "txt")
				{
					if (obj_respuesta.type == "text")
						obj_respuesta.value = obj_ajax.responseText;
					else
						obj_respuesta.innerHTML = obj_ajax.responseText;
				}
				else
				{
					if (obj_respuesta.type == "text")
						obj_respuesta.value = obj_ajax.responseXML;
					else
						obj_respuesta.innerHTML = obj_ajax.responseXML;
				}
		} 
		else
		{
			obj_carga.innerHTML = texto_carga;
		}
	}
	if (method == "POST")
		obj_ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	obj_ajax.send(query_string);
}
var nav4 = !window.Event ? true : false;
function IsNumber(evt)
{
	var key = nav4 ? evt.which : evt.keyCode;
	return (key <= 13 || (key >= 48 && key <= 57) /*|| key == 46*/);
}
function tipearCampo(valor,id_destino)
{
	var destino = $(id_destino);
	//alert(destino.type);
	//destino.value = valor.substring(0,5);
	sendAjaxRequest(nuevoAjax(),'ajax.verificarCodemp.php',destino,"codemp="+valor.substring(0,5).toLowerCase(),"Generando código de empresa");
}
function actualizarCombos(combo1,combo2)
{
	combo2.options.length=0;
	inicio = parseInt(combo1.selectedIndex)+1;
	for(i=0;i<combo1.options.length-inicio;i++){
		combo2.options.length++;
		combo2.options[i].value = combo1.options[i+inicio].value;
		combo2.options[i].text = combo1.options[i+inicio].text;
	}
}
function verificarEmail(valor,id)
{
	var ajax=nuevoAjax();
	//var path = './ajax/';
	var path = './';	
	if (valor != "")
	{
		var qs = "email="+valor;
		if (id != null)
			qs += "&id="+id;
		ajax.open("POST", path+"ajax.verificarEmail.php",true);
		ajax.onreadystatechange=function() 
		{ 
			if (ajax.readyState==4) 
			{ 
			//alert(ajax.responseText);
				if (ajax.responseText)
				{
					$('verEmail').style.color = "#F00";
					$('verEmail').innerHTML = "E-mail ya registrado o incorrecto";
					$('email').focus();
					return false;
				}
				else
				{
					$('verEmail').style.color = "green";
					$('verEmail').innerHTML = "E-mail disponible";
					$('enviar').style.display="";
					return true;
				}
			} 
			else
			{
				$('verEmail').innerHTML = "Verificando disponibilidad de email...";
			}
		}
		ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		ajax.send(qs);
	}
}

function verificarCamposContacto()
{
	errores=0;
	str="";
	if ($('mensaje').value == "")
	{
		str += " mensaje,";
		errores++;
	}
	if ($('nombreyapellido').value == "")
	{
		str += " nombre y apellido,";
		errores++;
	}
	if ($('telefono').value == "")
	{
		str += " telefono,";
		errores++;
	}
	if ($('email').value == "")
	{
		str += " email,";
		errores++;
	}
	if (errores>0){
		if (errores>1)
			alert("Faltan completar los siguientes campos:"+str.substring(0,str.length-1));
		else
			alert("Falta completar el campo"+str.substring(0,str.length-1));
	}
	else
	{
		$('form_contacto').submit();
	}
}

function verificarCamposDenuncia()
{
	errores=0;
	str="";
	if ($('propiedad').value == "")
	{
		str += " propiedad,";
		errores++;
	}
	if ($('ubicacion').selectedIndex == "0")
	{
		str += " ubicacion,";
		errores++;
	}
	if ($('inmobiliaria').selectedIndex == "0")
	{
		str += " inmobiliaria,";
		errores++;
	}
	if ($('fecha').value == "")
	{
		str += " dia y hora de guardia,";
		errores++;
	}
	if ($('nombreyapellido').value == "")
	{
		str += " nombre y apellido,";
		errores++;
	}
	if ($('dni').value == "")
	{
		str += " numero de documento,";
		errores++;
	}
	if ($('telefono').value == "")
	{
		str += " telefono,";
		errores++;
	}
	if ($('email').value == "")
	{
		str += " email,";
		errores++;
	}
	if (errores>0){
		if (errores>1)
			alert("Faltan completar los siguientes campos:"+str.substring(0,str.length-1));
		else
			alert("Falta completar el campo"+str.substring(0,str.length-1));
	}
	else
	{
		$('form_denuncias').submit();
	}
}

function verificarCamposAviso()
{
	errores=0;
	str="";
	if ($('tipo_propiedad').selectedIndex == "0")
	{
		str += " tipo de propiedad,";
		errores++;
	}
	if ($('tipo_operacion').selectedIndex == "0")
	{
		str += " tipo de operacion,";
		errores++;
	}
	if ($('ambientes').selectedIndex == "0")
	{
		str += " ambientes,";
		errores++;
	}
	if ($('fecha_publicacion').value == "" || $('fecha_publicacion').value == "00/00/0000")
	{
		str += " fecha de guardia,";
		errores++;
	}
	if ($('antiguedad').value == "")
	{
		str += " antiguedad,";
		errores++;
	}
	if ($('descripcion').value == "")
	{
		str += " descripcion,";
		errores++;
	}
	if ($('superficieCubierta').value == "")
	{
		str += " superficie cubierta,";
		errores++;
	}
	if ($('superficieDescubierta').value == "")
	{
		str += " superficie descubierta,";
		errores++;
	}
	if ($('superficieTotal').value == "")
	{
		str += " superficie total,";
		errores++;
	}
	if (($('div-alq').style.display != 'none' && $('valor_alquiler').value == "") || ($('div-ven').style.display != "none" && $('valor_venta').value == ""))
	{
		str += " valores,";
		errores++;
	}
	if ($('calle').value == "")
	{
		str += " calle,";
		errores++;
	}
	if ($('numero').value == "")
	{
		str += " numero,";
		errores++;
	}
	if (($('localidad').selectedIndex == 0 && $('barrio').selectedIndex == 0))
	{
		str += " barrio,";
		errores++;
	}
	if (errores>0){
		if (errores>1)
			alert("Faltan completar los siguientes campos:"+str.substring(0,str.length-1));
		else
			alert("Falta completar el campo"+str.substring(0,str.length-1));
	}
	else
	{
		$('envio_form_aviso').value = 1;
		$('form_aviso').submit();
	}
}
function verificarCamposEmpresa()
{
/*
	verEmail = verificarEmail($('email'));
	if (verEmail)
	{*/
	errores=0;
	str="";
		if ($('inmo').value==1)
		{
			if ($('contactoNombre').value == "")
			{
				str += " nombre del contacto,";
				errores++;
			}
			if ($('contactoApellido').value == "")
			{
				str += " apellido del contacto,";
				errores++;
			}
			if ($('contactoEmail').value == "")
			{
				str += " email del contacto,";
				errores++;
			}
			if ($('formaPago').selectedIndex == "")
			{
				str += " forma de pago,";
				errores++;
			}
			if ($('tipoContrato').selected == "")
			{
				str += " tipo de contrato,";
				errores++;
			}
			if ($('calle').value == "")
			{
				str += " calle,";
				errores++;
			}
			if ($('telefono').value == "")
			{
				str += " tel&eacute;fono,";
				errores++;
			}
			if ($('numero').value == "")
			{
				str += " numero,";
				errores++;
			}
			if ($('nombre').value == "")
			{
				str += " nombre,";
				errores++;
			}
			if (($('localidad').selectedIndex == 0 && $('barrio').selectedIndex == 0))
			{
				str += " barrio,";
				errores++;
			}
			/*if ($('razonSocial').value == "")
			{
				str += " raz&oacute;n social,";
				errores++;
			}
			if ($('codigoEmpresa').value == "")
			{
				str += " c&oacute;digo de empresa,";
				errores++;
			}
			if ($('cuit').value == "")
			{
				str += " cuit,";
				errores++;
			}*/
			if ($('email').value == "")
			{
				str += " email,";
				errores++;
			}
			if ($('tyc_existe').value == "1" && $('tyc').value == "")
			{
				str += " terminos y condiciones,";
				errores++;
			}
			if ((($('clave').value == "" || $('confClave').value == "") && $('do_get').value == "ins"))
			{
				str += " contrase&ntilde;a,";
				errores++;
			}
			if (errores>0){
				if (errores>1)
					alert("Faltan completar los siguientes campos:"+str.substring(0,str.length-1));
				else
					alert("Falta completar el campo: "+str.substring(0,str.length-1));
			}
			else
			{
				$('envio_form_empresa').value = 1;
				$('form_empresa').submit();
			}
		}else{
			if ($('clave').value == "")
			{
				str += " contrasena,";
				errores++;
			}
			if ($('email').value == "")
			{
				str += " email,";
				errores++;
			}
			if ($('tyc_existe').value == "1" && $('tyc').value == "")
			{
				str += " terminos y condiciones,";
				errores++;
			}
			if (errores>0){
				if (errores>1)
					alert("Faltan completar los siguientes campos:"+str.substring(0,str.length-1));
				else
					alert("Falta completar el campo: "+str.substring(0,str.length-1));
			}
			else
			{
				alert("Su pedido fue enviado exitosamente y la solicitud se mantendra activa durante 30 dias. Luego de ese periodo debera ingresarlo nuevamente.");
				submitEmp();
			}
		}
	//}
}
function submitEmp()
{
	$('envio_form_empresa').value = 1;
	$('form_empresa').submit();
}
function verificarAEstrenar(valor,campo)
{
	if (valor == 0 && valor != "")
		campo.innerHTML = "A estrenar";
	else
		campo.innerHTML = "";
}

function cargarPropiedades()
{ 
	var propiedades = $('propiedades');
	var forms = document.getElementsByTagName('form');
	var i=0;
	while(i<forms.length && forms[i].id != 'form_index')
	{
		i++;
	}
	var form = forms[i];
	i=0;
	var send = "";
	while (i<form.length)
	{
		if ((form[i].value != "" || (form[i].type == "checkbox" && form[i].checked != false)) && form[i].type != "button" && form[i].type != "submit")
		{
			send += "&"+form[i].id+"=";
			if (form[i].type == 'checkbox')
				send += form[i].checked;
			else if (form[i].type == 'radio')
			{
				if (form[i].checked == true)
					send += form[i].value;
			}
			else
				send += form[i].value;
		}
		i++;
	}
	var ajax=nuevoAjax();
	//var path = './ajax/';
	var path = './';	
	ajax.open("POST", path+"ajax.propiedades.php",true);
	ajax.onreadystatechange=function() { if (ajax.readyState==4) { propiedades.innerHTML = ajax.responseText;/*alert(ajax.responseText)*/ } }
	ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajax.send(send.substring(1,send.length));
	
}

function cambiarCheck(obj)
{
  if (obj.value == 1)
	  obj.value = 0;
  else
	  obj.value = 1;
}
function cambiarFavorito(idAviso,idUser)
{ 
	var texto_favorito = $('texto_favorito'+idAviso);
	var seleccionado = 0;
	if (texto_favorito.innerHTML == 'Agregar a Mis Favoritos')
	//if (texto_favorito.innerHTML == '-')
		seleccionado = 1;
	var ajax=nuevoAjax();
	var path = './';	
	ajax.open("POST", path+"ajax.avisoSeleccionado.php",true);
	ajax.onreadystatechange=function() { if (ajax.readyState==4) { if (ajax.responseText == 'ok')cambiarContenido(texto_favorito,"Agregar a Mis Favoritos","Quitar de Mis Favoritos"); } }
	//ajax.onreadystatechange=function() { if (ajax.readyState==4) { if (ajax.responseText == 'ok')cambiarContenido(texto_favorito,"&nbsp;","-"); } }
	ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajax.send("idAviso="+idAviso+"&idUser="+idUser+"&seleccionado="+seleccionado);
}

