/*========== Backend.js ==========*/
var Backend={};

// login de usuariosa
Backend.login=function(){
	// si user esta vacio, ocultamos el div de loading y borramos el mensaje
	if ($('username').value == '') {
		alert('Debe ingresar su nombre de usuario');
		return;
	}
	if ($('password').value == '') {
		alert('Debe ingresar su contraseña');
		return;
	}
	
	
	// via ajax chequeamos logueamos al user
	// nos va a devolver 1 si se pudo loguear, 0 si no 
	
	Ajax.backendLogin($('username').value,$('password').value,function(response){
		//alert($('username').value);
		//alert(response.login);
		if(response.login == 0){
			alert('Su usuario o contraseña son invalidos');
		}else
		{
			window.location.reload();
		}
	}
	);
}


Backend.bloquear=function(user){
	var estado=$('block'+user).getHTML();
	//alert(estado);
	if(estado.trim()=="Bloquear"){
				Ajax.bloquear(estado,user, function(response){
					alert("Usuario Bloqueado");
					$('block'+user).innerHTML="Desbloquear";
				});}
				else{
					Ajax.bloquear(estado,user, function(response){
					alert("Usuario Desbloqueado");
					$('block'+user).innerHTML="Bloquear";

					});}}

					
Backend.bloquearAdmin=function(user){
	var estado=$('block'+user).getHTML();
	//alert(estado);
	if(estado.trim()=="Bloquear"){
				Ajax.bloquearAdmin(estado,user, function(response){
					alert("Usuario Bloqueado");
					$('block'+user).innerHTML="Desbloquear";
				});}
				else{
					Ajax.bloquearAdmin(estado,user, function(response){
					alert("Usuario Desbloqueado");
					$('block'+user).innerHTML="Bloquear";

					});}}					

Backend.darPermiso=function(modulo){

	//alert(estado);
	if(!$('permiso'+modulo).checked){
				Ajax.setPermiso(modulo,0, function(response){
					alert("Modulo Bloqueado");
					$('permiso'+modulo).checked=false;
				});}
				else{
					Ajax.setPermiso(modulo,1, function(response){
					alert("Modulo Desbloqueado");
					$('permiso'+modulo).checked=true;

					});}}
					
Backend.deleteImagesProduct=function(img,prod_id){
	
	
	if(confirm('desea borrar esta imagen?')){
				Ajax.deleteImagesProduct(img,prod_id ,function(response){
					$('files_forms').innerHTML=response.resultado;
				});}
				}					
	
Backend.ver=function(id){
	var div=$(id).get('html');
	//alert(estado);
	$('info').innerHTML=div;
				}
 function quitaacentos(t){ 
 á="a";é="e";í="i";ó="o";ú="u"; 
 ñ="n";
 acentos=/[áéíóúñ]/g; 
 return t.replace(acentos, 
 function($1){ 
 return eval($1) 
 } 
 ); 
 } 				
Backend.Completar=function(name,descri){
	var value=$('pag_title').value;
	$('pag_name').value=value;
	$('pag_menu').value=value;
	$('file_name').value=value.replace(/ /gi,"-");
	$('file_name').value=quitaacentos($('file_name').value);
	$('pag_description').value=value +" "+ name+" "+descri;
}


Backend.habilitarModulo=function(modulo){

	//alert(estado);
	if(!$('habilitar'+modulo).checked){
				Ajax.setModule(modulo,0, function(response){
					alert("Modulo Deshabilitado");
					$('habilitar'+modulo).checked=false;
				});}
				else{
					Ajax.setModule(modulo,1, function(response){
					alert("Modulo Habilitado");
					$('habilitar'+modulo).checked=true;


					});}}
					
Backend.MostrarContents=function(id){
	var style=$('content_id'+id).getStyle('display');
	if(style=="none"){
		$('Mostrar'+id).innerHTML="Ocultar";
		$('content_id'+id).setStyle('display','block');
		
	}else{
		$('Mostrar'+id).innerHTML="Mostrar";
		$('content_id'+id).setStyle('display','none');
	}
}

Backend.generarUrl=function(url,id){
	var input =new Element('input', {type:'text',value:url,id:'content_url'+id});
	input.set('value',url);
	$('divcontent_url'+id).innerHTML="";
	$('divcontent_url'+id).adopt(input);
	
	

}

Backend.MostrarCampos=function(){
	
	if($('etiqueta').get('html')=="Mostrar Campos"){
		$('etiqueta').set('html',"Ocultar Campos");
		
	}else
	{
		$('etiqueta').set('html',"Mostrar Campos");
	}
	$$('.secundario').each(function (d){
	
		var style=d.getStyle('display');
		if(style=="none"){
				d.setStyle('display','');
				d.setStyle('color','#FF0000');
			}
			else{
				d.setStyle('display','none');
				
			}
		
	});
}
Backend.GuardarContent=function(id){
	var title=$('content_title'+id).value;
	var leyenda=$('content_legend'+id).value;
	var descri=$('content_description'+id).value;
	var url=$('content_url'+id).value;
	var aling="center";
	
	$$('.aling'+id).each(function (d){
		
		if(d.checked){
			aling=d.value;
		}
	});
	var tam=1;
	$$('.resizes'+id).each(function (d){
		
		if(d.checked){
			tam=d.value;
		}
	});
	Ajax.GuardarContent(id,title,leyenda,descri,url,tam,aling,function(response){
					
					alert('se han modificado los datos');
				});
}
Backend.deleteContents=function(id,page,post){
	if(confirm('¿Desea eliminar este archivo?')){
	Ajax.deleteContents(id,page,post,function(response){
		$('files_forms').innerHTML="";
			$('files_forms').innerHTML=response.resultado;	
					
				});
	}
}

					
Backend.SavePage=function(){
	
	if($('page_id').value==0){
	if($('pag_title').value!=""){
	 	Ajax.SavePage($('pag_title').value,function(response)
	 	{
			//$('urlArchivos').setStyle('display','');
			var id=response.id;
			$('page_id').value=id;
			$('cc_id').value=id;
			
			//var pop="window.open('/admin/Contents/?page="+id+"&popup=true','Contenidos','scrollbars=yes,toolbar=no,menubar=no,status=no,width=900,height=700')";
			//$('urlArchivos').innerHTML='<a class="boton" href="javascript:void(0);" onclick="'+pop+'">Archivos</a>';	
					
				});
	}else{
		alert('Debe ingresar el titulo de la pagina');
	}
	}
}										

				
					
Backend.CompletarContacto=function(){
	
	var value=$('configcontact_from').value;
	$('configcontact_to').value=value;
	
}

//
Backend.SaveBlog=function() {
   if (parseInt($('cc_id').value) == 0) {
      if ($('blogpost_title').value.toString() != '') {
         Ajax.SaveBlog($('blogpost_title').value.toString(), function(response) {
            var id = response.id;
            $('cc_id').value = id;  
         });
      }
      else {
         alert('Debe ingresar el título de la entrada.')
      }
   }
}

Backend.BlogDefaultImage=function(post, content) {
   Ajax.BlogDefaultImage(post, content, function (response) {
      if (response.done) {
         alert('La imagen ha sido correctamente establecida como portada.');
      }
      else {
         alert('Hubo un error al establecer la imagen como portada.');
      }
   });
}

Backend.setLanguage=function(idioma){

	//alert(estado);
	if(!$('setIdioma'+idioma).checked){
				Ajax.setIdioma(idioma,0, function(response){
					alert("El idioma ahora no esta disponible");
					$('setIdioma'+language).checked=false;
				});}
				else{
					Ajax.setIdioma(idioma,1, function(response){
					alert("El idioma ahora es visible");
					$('setIdioma'+language).checked=true;


					});}}
					
Backend.BlogGetCategories=function() {
   var selected = parseInt($('blogpost_language').value);
   var post_id = parseInt($('cc_id').value);
   
   Ajax.BlogGetCategories(selected, post_id, function(response){
      
      $('categorias_list').empty();
      
      $each(response.data,function(item,index,object) {
         var el = new Element('input', {
            'type': 'checkbox',
            'name': 'blogpost_category[]',
            'id': 'blogpost_category'+index,
            'value': item.id
         });
         
         var el_txt = new Element('span', {
            'text': item.name+' '
         });
         
         $('categorias_list').adopt(el);
         $('categorias_list').adopt(el_txt);
         
      });
     
   });
}

