// JavaScript Document

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}


function $(id){
	
	return document.getElementById(id);

}


function volverInicio()
{
	window.top.location="index.php";
}


function irSubseccion(id)
{
	
	switch(id)
	{
		
		case 'categoria_listado':			
			loadPage(22);
			break;
		case 'gallery_listado':			
			loadPage(24);
			break;
		case 'link_listado':			
			loadPage(26);
			break;
		case 'available_listado':			
			loadPage(28);
			break;
		
		
				
		
	}
}

function borrarRegistro(id)
{
	
	var oForm = document.getElementById("form_listado");
	oForm.accion.value = "borrar";
	oForm.id.value = id;
	oForm.submit();
	
	
}


function borrar_foto(id,browid,tabla) 
{
	if (confirm('desea borrar la foto? Esta operación es irreversible')) {
		document.getElementById('imagen_'+id).src='/includes/fotito.php?foto=';
		document.getElementById('browser'+browid).disabled='true';
		ajaxManager("load_page",'borrarFoto.php?id='+id+'&tabla='+tabla+'&','respuesta');
	}
}

function borrar_fichero(fichero,tabla,campo,id) 
{
	if (confirm('desea borrar el fichero?')) {
		document.getElementById(fichero).style.visibility='hidden';
		switch(campo)
		{
			case "ficheroEs":
				document.getElementById('boton_es').style.visibility='hidden';
			break;
			case "ficheroEn":
				document.getElementById('boton_en').style.visibility='hidden';
			break;
			case "ficheroCa":
				document.getElementById('boton_ca').style.visibility='hidden';
			break;
		}
		
		ajaxManager("load_page",'borrarFichero.php?fichero='+fichero+'&tabla='+tabla+'&campo='+campo+'&id='+id+'&','respuesta');
	}
}


function cambiarZonas(id,subC) {
	
	var oParam=null;
	var oContenedor='subcategorias';
	if(subC==-1) var oPagina='ajax_subcategorias.php?id='+id+'&';
	else var oPagina='ajax_subcategorias.php?id='+id+'&sub='+subC+'&';
	ajaxManager("load_page",oPagina,oContenedor,oParam);
}

function cambiarOperacion(id) {
	var oParam=null;
	var oContenedor='precios';
	if(id!=3)	var oPagina='ajax_precio.php?accion=compra';
	else var oPagina='ajax_precio.php';
	ajaxManager("load_page",oPagina,oContenedor,oParam);
}

function procesarForm(form)
{
	
	var elem = form.elements;
	var salida = ""; 
	
	for(var i=0; i<elem.length; i++) 
	{

		if(!elem[i].getAttribute('multiple'))
		{
			salida+= elem[i].name + "=" + encodeURI(elem[i].value.replace('&','and')) + "&";   
		}
		else
		{		
			
			salida+=elem[i].name + "=";
			for(j = 0; j < elem[i].options.length; j++) 
			{
				if(elem[i].options[j].selected == true) 
				{
					salida+=elem[i].options[j].value + ",";
				}
			}
			salida=salida.substring(0,salida.length-1);	
			salida+='&';
		}
	}
	return salida;
}


function editarRegistro(id)
{
	
	var oForm = document.getElementById("form_listado");
	var oSeccion=oForm.tipo.value;
	var oContenedor='cms_content';
	var oPagina=oSeccion+'_editar.php?id='+id+'&';
		
	var oParam=null;
	switch (oSeccion)
	{
		case 'available':
			oParam="hideLangTabs();showLangTab(1)";
			break;
		
	}	
	ajaxManager("load_page",oPagina,oContenedor,oParam);
			
}

function irFicha(id,portada)
{	
	if(portada!="")	window.top.location="ficha.php?id="+id+"&oferta";
	else window.top.location="ficha.php?id="+id;
}



function comprobarSeleccion()
{
	var form=document.getElementById('datos');
	if (form.operacion.value=="" || form.tipo.value=="" )
	{
		if (form.operacion.value=="")	alert("Debe elegir un tipo de operación");
		else alert("Debe elegir al menos un tipo de Inmueble");
		
	}
	else{
		ajaxManager('load_page','ajax_amigo.php','contenido_der'); 
		ajaxManager('load_page','ajax_propiedades.php?'+procesarForm(form),'resultados');
	}
}


function validarEmail(valor)
{
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(valor))
	{
		return (true);
	} 
	else 
	{
	   return (false);
	}
}

function enviarMail(form,mailIncorr,todosLosCamp)
{	
	var hola=document.getElementById(form);
	if ( hola.email.value=="" || !validarEmail(hola.email.value) || hola.nombre.value=="" || hola.telefono.value=="" || hola.telefono.value=="" || hola.pais.value=="" )
	{
		if(!validarEmail(hola.email.value))
			alert(mailIncorr);
		else 
			alert(todosLosCamp);
	}
		
	else
	{
		document.getElementById(form).submit();
	}
}

function irUrl(url)
{
	window.top.location.href=url;
}

function enviarForm(form)
{
	var oForm = document.getElementById(form);
	oForm.submit();
	
	
}

function enviarFormForm(form)
{
	var oForm = document.getElementById(form);
	//window.top.location='comprar.php?accion=comprar&'+procesarForm(oForm);
	oForm.submit();
	
	
}

function irUrlFicha(url)
{
	url=url.replace('size=183','size=400');
	open(url,"miVentana", "width=435,height=315,scrollbars=yes, menubar=no, location=no, resizable=no"); 

}

function irEnlace(url)
{
	open(url,"miVentana", "toolbar=yes;statusbar=yes;scrollbars=yes, menubar=yes, location=yes, resizable=yes"); 

}

function valid(obj)
{
	/*obj.value=obj.value.replace(",",".");
	if(isNaN(obj.value))
	obj.value=obj.value.substring(0,obj.value.length-1);
	else
	return true;*/
	if(isNaN(obj.value)) 
	{	
		obj.value=''; 
	}
	else
	{
		obj.value=parseInt(obj.value);
		if(isNaN(obj.value)) 
		{	
			obj.value=''; 
		}	
		
	}
}



function setSuscriptor(id,obj)

{	var cond = obj.checked;
	
		var chequeos=document.getElementsByTagName("input");
		obj.checked = cond;
		ajaxManager('load_page','ajax_activa.php?id='+id+'&cond='+cond+'&','ajax_result');
	
}

function extensionVerif(archivo)
{
	
	extension = (archivo.substring(archivo.lastIndexOf("."))).toLowerCase(); 
	if(extension==".jpg" || extension==".jpeg" || extension==".JPG" || extension==".JPEG" || archivo=="") return true;
	else
	{
		
		return false;
	
	}
}

function validarForm(formulario)
{
	var form=document.getElementById(formulario);
	switch (formulario)
	{
	case "zapato":
		if (form.articulo_num.value=="" || form.precio.value=="" ||  form.talla_minima.value=="" || form.talla_maxima.value==""|| !extensionVerif(form.foto_chica.value) || !extensionVerif(form.foto_grande.value)) 																	   {
		if(!extensionVerif(form.foto_chica.value) || !extensionVerif(form.foto_grande.value))
		{
			alert('Solo se permiten imagenes del tipo jpg o jpeg');
		}
		else 
		{
			alert("Los campos Precio, Talla minima , Talla maxima, Numero de articulo no pueden ser vacios");
		}
																												   }
		else 
		{
			fs_contenido();
			fs_posicionarImg();
			document.getElementById('zapato').submit();
		}
			
	
	break;
	
	case "gallery":
		if (form.tituloEs.value=="" ) 																	   {
		
			alert("El campo Titulo no puede ser vacios");
		
																												   }
		else 
		{
			fs_contenido();
			fs_posicionarImg();
			document.getElementById('gallery').submit();
		}
	
	break;
	
	
	
	case "link":
		if (form.url.value=="" || form.tituloEs.value==""  || form.tituloEn.value=="" ) 																	   {
				alert("Todos los campos son obligatorios");
		
																												   }
		else 
		{
			fs_contenido();
			fs_posicionarImg();
			document.getElementById('link').submit();
		}
	break;
	
	case "categoria":
		if (form.tituloEs.value=="" || form.tituloEn.value==""  ) 																	   {
				alert("Los campos Titulo Español y Titulo Ingles no pueden ser nulos");
		
																												   }
		else
		{
			fs_contenido();
			fs_posicionarImg();
			document.getElementById('categoria').submit();
		}
	
	break;
	
	
	case "departamento":
		if (form.nombreEs.value==""  ) 																	   {
				alert("Los campos Articulo Numero y precio no pueden ser vacios");
		
																												   }
		else
		{
			fs_contenido();
			fs_posicionarImg();
			document.getElementById('departamento').submit();
		}
	
	break;
	
	case "parametro":
	
			fs_contenido();
			fs_posicionarImg();
			document.getElementById('parametro').submit();
		
	
	break;
	}
}


/* Script de Submit */

var fs_imagenLoading = new Image();
fs_imagenLoading = 'http://www.nuevabalear.com/images/cms/loading8.gif';

function $(id) { return document.getElementById(id) }
function $$$(id) { return document.getElementsByTagName(id) }

var forms=document.getElementsByTagName('form');
var iSubmit=0;

function fs_contenido()
{
	var oBody = document.getElementsByTagName("body").item(0);
	var nodoDiv = document.createElement("div");
	var nodoImagen = document.createElement("img");
	
	nodoDiv.appendChild(document.createTextNode(" "));
	//nodoDiv.setAttribute("style", "position: absolute; width: 140px; height: 100px; background-color: rgb(51, 51, 51); top: 0px; left: 0px; display: none;");
	nodoDiv.style.position = "absolute";
	nodoDiv.style.width = "140px";
	nodoDiv.style.height = "100px";
	nodoDiv.style.backgroundColor = "#333";
	nodoDiv.style.top = "0px";
	nodoDiv.style.left = "0px";
	nodoDiv.style.display = "none";
	nodoDiv.setAttribute("id","fs_img_fondo_oscuro");
	
	nodoImagen.setAttribute("width","140");
	nodoImagen.setAttribute("height","100");
	//nodoImagen.setAttribute("style", "position: absolute; z-index: 10000; top: 0px; left: 0px; display: none;");
	nodoImagen.style.position = "absolute";
	nodoImagen.style.zIndex = "1000";
	nodoImagen.style.top = "0px";
	nodoImagen.style.left = "0px";
	nodoImagen.style.display = "none";
	nodoImagen.setAttribute("src", fs_imagenLoading);
	nodoImagen.setAttribute("id", "fs_img_loading");
	
	oBody.appendChild(nodoDiv);
	oBody.appendChild(nodoImagen);
}

function fs_fixSubmit()
{
	for(i=0;i<forms.length;i++)
	{
		forms[i].onsubmit = fs_newsubmit;
	}
}

function fs_newsubmit()
{
	fs_posicionarImg();
	return true;
}

function fs_posicionarImg()
{
	var capaImg = "fs_img_loading";
	var myWidth = 0, myHeight = 0;
  	if( typeof( window.innerWidth ) == 'number' ) 
	{
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	 } 
	 else if (document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) 
	 {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	 } 
	 else if ( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) 
	 {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	 }
	$('fs_img_fondo_oscuro').style.display="block";
	$('fs_img_fondo_oscuro').style.width = myWidth+ "px";
	$('fs_img_fondo_oscuro').style.height = myHeight + "px";
	$('fs_img_fondo_oscuro').style.opacity = "0.5";
	$('fs_img_fondo_oscuro').style.filter = "alpha(opacity=50)";
	$(capaImg).style.display = "block";
	$(capaImg).style.left = ((myWidth-140)/2) + "px";
	$(capaImg).style.top = ((myHeight-100)/2) + "px";
}

function addListener(obj,evt,func) 
{
    if (obj.attachEvent)
        obj.attachEvent('on' + evt, func);

    else 
    {
        if (obj.addEventListener) 
        {
            obj.addEventListener(evt,func,false);
        }
        else 
        {
            if (obj.eval) 
            {
                obj["on"+evt] = func;
            } 
            else 
            {
                alert("Error adding listener\nObject:"+obj+"ID:"+obj.id+" Name:"+obj.name+" TagName:"+obj.tagName+"\nEvent:"+evt);
            }
        }
    }
}

addListener(window,"load",fs_contenido);
addListener(window,"load",fs_fixSubmit);



function editarCategoriaPadre(id)
{
	var oContenedor='cms_content';
	var oPagina='categoria_padre_editar.php?id='+id+'&';
	ajaxManager("load_page",oPagina,oContenedor);
}

function editarCategoriaHijo(id)
{
	var oContenedor='cms_content';
	var oPagina='categoria_hijo_editar.php?id='+id+'&';
	ajaxManager("load_page",oPagina,oContenedor);
}

function hideLangTabs() 
{
	for(i=1;i<=4;i++)
	{
		try
		{
			document.getElementById('cms_lang_select_tab_' + i).className='cms_lang_select_tab';
			document.getElementById('cms_lang_tab_' + i).style.display='none';
		}
		catch(ex)
		{
			alert("hideLangTabs: ("+i+") " + ex);
		}
	}
}

function showLangTab(id)
{
	hideLangTabs();
	try
	{
		document.getElementById('cms_lang_tab_' + id).style.display='block';
		document.getElementById('cms_lang_select_tab_' + id).className='cms_lang_select_tab_active';
	}
	catch(ex)
	{
		alert("showLangTab: ("+id+") " + ex);
	}
}

/* Fin del script de submit */

/*******************************
	Script del Menu
*******************************/

function showMenu()
{
	
	
	if($('submenu').style.display == "block") 
	{
		$('submenu').style.display = "none";
		
	}
	else
	{
		$('submenu').style.display = "block";
		
	}
}

function hideMenus()
{
	try
	{
		
			$('submenu').style.display = "none";
		
	}
	catch(e)
	{}
}
/*********/




function descarga(archivo)
{
	window.top.location="download.php?f="+archivo;
}

function legal()
{
	//url=url.replace('size=183','size=400');
	open('politica.php',"miVentana", "width=390,height=315,scrollbars=yes, menubar=no, location=no, resizable=no"); 

}

function paginar(url,pagina)
{
	ajaxManager('load_page',url+'.php?pagina='+pagina+'&','cms_content');
}

function cambiarFoto(foto)
{	
	document.getElementById('miFoto').src="/includes/fotito.php?foto=" + foto + "&size=460";
	document.getElementById('miFoto').title = foto ;
	
}

function popFoto(foto)
{	
	open('foto.php?foto='+foto,"miVentana", "scrollbars=no, menubar=no, location=no, resizable=no"); 
	
}





function popup(foto)
 {
	 window.open('fotos/'+foto,'popup','location=0,status=0,scrollbars=0');
 }
 
 function popupAbrir(foto)
 {
	 window.open(foto,'popup','location=0,status=0,scrollbars=0');
 }



function garantia()
 {
	 window.open('scagel-guarantee.php','popup','width=410;location=0,status=0,scrollbars=yes');
 }