function activar_calendario(campo,img){
Calendar.setup({inputField:campo,ifFormat:"%Y-%m-%d",button:img,firstDay : 0});
}


function d2h(d) {return d.toString(16);}

function abrir_galeria(){
	var campo=document.getElementById('CATEGORIA');
	var indice=campo.selectedIndex;
	var valor=campo.options[indice].value;
	location.href='gal.php?CATEGORIA_ID='+valor;
}

function abrir_edit(id,form,modo,pagina){
	var page='../admin/'+form+'_edit.php?id='+id+'&modo='+modo+'&pagina='+pagina;
	window.open(page,'contactos'+id,"width=400,height=500,menubar=no");	
}

function abrir_edit_contactos(id){
	var page='../admin/contactos_edit.php?id='+id;
	window.open(page,'contactos'+id,"width=300,height=300,menubar=no");
}

function MostrarOcultar(div){
	var objeto=document.getElementById(div);
	if(objeto.style.display=='')
		objeto.style.display='none';
	else
		objeto.style.display='';
}




function pulsar(e) {
  tecla = (document.all) ? e.keyCode : e.which;
  if((tecla<48||tecla>57)&&(tecla!=13&&tecla!=46&&tecla!=0&&tecla!=8))
  	alert('unicamente se pueden escribir numeros');


return ((tecla != 13)&&(tecla>=48)&&(tecla<=57)||tecla==0||tecla==8||tecla==46);
}


function enviar(form){
	
	var confirmacion=confirm('Esta seguro que desea que desea realizar este proceso ahora');
	if(confirmacion==true&&form!=''){
		document.getElementById(form).submit();
	}
	
	return confirmacion;

}

function ArmarUrl(campos){
	var nombres=campos.split('-');
	var valores;
	var url='';
	var newvalor='';
	for(i=0;i<nombres.length;i++){
//		alert(i);
		campo=document.getElementById(nombres[i]);
		switch(document.getElementById(nombres[i]).type){

				case 'text':
							valor=campo.value;
							break;
				case 'textarea':
							valor=campo.value;
							break;

				case 'select-one':
							indice=campo.selectedIndex;
							indice=parseInt(indice);
							valor=campo.options[indice].value;
							break;
				case 'hidden':
							valor=campo.value;
							break;
		}
//	alert(document.getElementById(nombres[i]).value);
	//	alert(document.getElementById(nombres[i]).type);
	newvalor="";
	for(j=0;j<valor.length;j++){
		newvalor+="%"+d2h(valor[j].toString().charCodeAt(0));
	}
		url+=nombres[i]+'='+newvalor+"&";
		
	}
//	alert(url);
	return url;
}

/*Refresca determinada parte de la pantalla dependiendo del valor que tenga el objeto oculto pag_en_proceso */
function refrescar(){
	var pagina=document.getElementById('pag_en_proceso').value;
	
		setTimeout("abrir('"+pagina+"','','','contenido')",1000);
		setTimeout("document.getElementById('edicion').innerHTML='';",2000);
}

//Desplega determinada parte de la pantalla despues de determinado tiempo
function desplegar(div,tiempo){
	setTimeout("Effect.toggle('"+div+"','slide');",tiempo);
}

function validar_form(form,campos){
	var	ids=campos.split('-');
	var campo,indice,salir=0;
	for(i=0;i<ids.length;i++){

		campo=document.getElementById(ids[i]);
		switch(document.getElementById(ids[i]).type){
				case 'text':
								if(campo.value==''){
									alert('El campo '+ids[i]+' debe llenarse');
									campo.focus();
									salir=1;
								}
								break;
				case 'textarea':
								if(campo.value==''){
									alert('El campo '+ids[i]+' debe llenarse');
									campo.focus();
									salir=1;									
								}
								break;
				case 'select-one':
								indice=campo.selectedIndex;
								indice=parseInt(indice);
								if(indice==0&&!campo.disabled){
									alert('Debe escoger un valor en el campo '+	ids[i]);
									campo.focus();
									salir=1;									
								}
								break;
		}
		if(salir==1){
				break;
		}
	}
	if(salir!=1){
		validar_otras();
		enviar(form);				
	}
}

function validar_otras(){
	var pais, depto, ciudad, indice, valor, preguntar=1;
	pais=document.getElementById('CONTACTO_PAIS');
	depto=document.getElementById('CONTACTO_DEPTO');
	ciudad=document.getElementById('CONTACTO_CIUDAD');
	
	indice=pais.selectedIndex;
	valor=pais.options[indice].value;
	if(valor=='otro'){
		if(document.getElementById('OTRO_PAIS').value==''&&preguntar==1){
			document.getElementById('OTRO_PAIS').focus();
			alert('Si escoges otro pais debes escribirlo');
			preguntar=0;
		}
	}
	
	indice=depto.selectedIndex;
	valor=depto.options[indice].value;
	if(valor=='otro'||valor=='0'){
		if(document.getElementById('OTRO_DEPTO').value==''&&preguntar==1){
			alert('Si escoges otro departamento debes escribirlo');
			document.getElementById('OTRO_DEPTO').focus();
			preguntar=0;
		}
	}	
	
	indice=ciudad.selectedIndex;
	valor=ciudad.options[indice].value;
	if(valor=='otro'||valor=='0'){
		if(document.getElementById('OTRA_CIUDAD').value==''&&preguntar==1){
			alert('Si escoges otra ciudad debes escribirla');
			document.getElementById('OTRA_CIUDAD').focus();
			preguntar=0;			
		}
	}	
}

function abrir_deptos(){

	document.getElementById('otro_depto').style.display='none';		
	document.getElementById('otra_ciudad').style.display='none';

	var indice, campo, valor;
	campo=document.getElementById('CONTACTO_PAIS');
	indice=campo.selectedIndex;
	valor=campo.options[indice].value;

	if(valor=='otro'){
		document.getElementById('otro_pais').style.display='';	
		document.getElementById('CONTACTO_DEPTO').disabled='disabled';
		document.getElementById('otro_depto').style.display='';
		document.getElementById('otra_ciudad').style.display='';	
		document.getElementById('CONTACTO_CIUDAD').disabled='disabled';
	}else if(valor=='0'){
		document.getElementById('CONTACTO_DEPTO').disabled='disabled';		
		document.getElementById('otro_pais').style.display='none';		
		document.getElementById('otra_ciudad').style.display='none';	
		document.getElementById('CONTACTO_CIUDAD').disabled='disabled';		
	}
	else{
		document.getElementById('otro_pais').style.display='none';	
		
		
		evento="onchange='abrir_ciudades()'";
		
		abrir('php/return_select.php','','&nombre=CONTACTO_DEPTO&tabla=depto&id=DEPTO_ID&campo_nombre=DEPTO_NOMBRE&campo_orden=DEPTO_NOMBRE&forma=ASC&condicion=(PAIS_ID='+valor+")&evento="+evento,'departamento');
	}
	

}

function abrir_ciudades(){
		document.getElementById('otra_ciudad').style.display='none';
	var indice, campo, valor;
	campo=document.getElementById('CONTACTO_DEPTO');
	indice=campo.selectedIndex;
	valor=campo.options[indice].value;
	if(valor=='otro'){
		document.getElementById('otro_depto').style.display='';	
		document.getElementById('CONTACTO_CIUDAD').disabled='disabled';	
		document.getElementById('otra_ciudad').style.display='none';
		document.getElementById('otra_ciudad').style.display='';			
	}else if(valor=='0'){
		document.getElementById('CONTACTO_CIUDAD').disabled='disabled';		
		document.getElementById('otro_depto').style.display='none';		
		document.getElementById('otra_ciudad').style.display='none';			
	}else{
		document.getElementById('otro_depto').style.display='none';	
		
		
		evento="onchange='valida_ciudad()'";
		nombre='CONTACTO_CIUDAD';
		tabla='ciudad';
		id='CIUDAD_ID';
		campo_nombre='CIUDAD_NOMBRE';
		campo_orden='CIUDAD_NOMBRE';
		forma='ASC';
		condicion='(DEPTO_ID='+valor+')';
		
		abrir('php/return_select.php','','&nombre='+nombre+'&tabla='+tabla+'&id='+id+'&campo_nombre='+campo_nombre+'&campo_orden='+campo_orden+'&forma='+forma+'&condicion='+condicion+'&evento='+evento,'ciudad');
	}
}

function valida_ciudad(){
	var campo, indice, valor;
	campo=document.getElementById('CONTACTO_CIUDAD');
	indice=campo.selectedIndex;
	valor=campo.options[indice].value;
	if(valor=='otro'){
		document.getElementById('otra_ciudad').style.display='';
	}else{
		document.getElementById('otra_ciudad').style.display='none';	
	}
}


