function showMails() {
  showPopup( {urlPage:"../../resources/mailRecipients/frm_recipients.php",titulo:"MAIL RECIPIENTS",ancho:775,alto:360},{modo:'update'});
}
function showAttachs(local,sales) {
  showPopup( {urlPage:"../../resources/mailRecipients/frm_attachs.php",titulo:"MAIL RECIPIENTS",ancho:775,alto:360},{localMarkets:local,srch_sales:sales});
  $("input[type='checkbox']").each(function() { if( this.value!=-1 ) this.checked=false; });  // Deselecciono todos
}
function seleccionarTodos (theElement){
    $("input[type='checkbox']").each(function() {
        if( this.value!=-1 ) this.checked=theElement.checked;
    });
}

function anade(tipo)	{
	
	var nombre=$("#name_"+tipo).attr("value");
	var mail=$("#mail_"+tipo).attr("value");
	var grupo=""+$("#group_"+tipo).attr("value");
	
	var nombreSeleccionado=$("#"+tipo).getSelectedText();
	
	if(grupo=="" || grupo=="undefined") {	
		if(mail.indexOf("@")<=0) {alert("Please check the email address !");mail=""}
		modificaCorreo(tipo,nombre,mail);	
	} else {
    if($("#mailOculto").attr("value")!="") add_to_group(tipo); 		
		else if(confirm("Do you really want to create new or add to GROUP ?")) create_group(tipo);
	}
	$("#name_"+tipo).attr("value","");
	$("#mail_"+tipo).attr("value","");
	$("#group_"+tipo).attr("value","");
	$("#mailOculto").attr("value","");
}
function modificaLista(tipo,nombre,valor) {
  var aux=$("#"+tipo).selectedValues();
  if( $("#"+tipo).containsText(nombre) ) $("#"+tipo).removeOption(aux[0]);
  $("#"+tipo).addOption(valor,nombre);
}
function modificaCorreo(tipo,nombre,valor) {
  var aux=$("#"+tipo).selectedValues();  
  if( $("#"+tipo).containsText(nombre) ) $("#"+tipo).removeOption(aux[0]);
  $("#"+tipo).addOption(valor,nombre);
}
function add_to_group(tipo) {
	var nombres=$("#"+tipo).getSelectedText();
	var nombreLista=$("#group_"+tipo).attr("value");
	var valor=""+$("#mailOculto").attr("value");
	
	if( nombres.length==1 ) { 
     var dato=""+$("#mail_"+tipo).attr("value");     
     if(dato=="" || dato=="undefined") modificaLista(tipo,nombreLista,valor);
	   else if( !$("#"+tipo).containsOption(dato) )
        alert("You should add the EMAIL as a NEW ENTRY before adding to the DISTRIBUTION LIST !");    
  } else {  	
  	$("#"+tipo).removeOption(valor);  

    if(valor.substring(valor.length-1,valor.length)!=",") valor+=",";
    
  	for(var i=0;i<nombres.length;i++) {	
  		valor+=nombres[i]+",";	// Con los nombres escogidos
  	}
  	$("#"+tipo).addOption(valor,nombreLista);
  }
  
}

function create_group(tipo) {
	var nombres=$("#"+tipo).getSelectedText();
	var valores=$("#"+tipo).selectedValues();
	
	var nombre=$("#group_"+tipo).attr("value");
	var valor="";
	for(var i=0;i<nombres.length;i++) {
		valor+=nombres[i]+",";	// Con los nombres
	}
	
	if(valores.length>1) $("#"+tipo).addOption(valor,nombre)
	else alert('Please, select more than one value to create a DISTRIBUTION LIST !');
}

// Chequea si el objeto seleccionado es una lista de distribución= TRUE o un correo simple=FALSE
function isList(tipo,texto,valor) {
  var ok=false;
  var correos=valor.split(",");
  
  ok=ok || (("xx"+valor).indexOf("LISTA")>0);  // Si estaba ya grabada en BBDD viene como VALOR
  ok=ok || (valor=="REMOVE");       // Si estaba ya grabada pro quiere eliminar la lista, vale REMOVE  
  ok=ok || ( (correos.length>1 && correos[0]!=valor)?true:false );  
  return ok;
}

function remove(tipo)	{
	var valores=$("#"+tipo).selectedValues();
	var nombres=$("#"+tipo).getSelectedText();
	
	var quitados=""+$("#removedNames").attr("value");
	if(quitados=="undefined") quitados="";
	for(var i=0;i<valores.length;i++) {
    if( valores[i]!=-1 ) {
      $("#"+tipo).removeOption(valores[i]);
      quitados=nombres[i]+"-"+valores[i]+";"+quitados;            
    }
  }
  $("#removedNames").attr("value",quitados);
  alert($("#removedNames").attr("value"));
  limpiaSeleccionCorreo(tipo);
  limpiaSeleccionLista(tipo);  
}
function limpiaSeleccionCorreo(tipo) {
  $("#name_"+tipo).attr("value","");
  $("#mail_"+tipo).attr("value","");
}
function limpiaSeleccionLista(tipo) {
  $("#group_"+tipo).attr("value","");
  $("#mailOculto").attr("value","");
}
function selecciona(tipo) {
	var nombres=$("#"+tipo).getSelectedText();
	var valores=$("#"+tipo).selectedValues();

  limpiaSeleccionCorreo(tipo);
    
  if(valores.length==1 && valores[0]!=-1) {  
   if ( isList(tipo,nombres[0],valores[0]) ) {
    $("#mailOculto").attr("value",valores[valores.length-1]);
    $("#group_"+tipo).attr("value",nombres[valores.length-1]);
   }
   else {
    $("#mail_"+tipo).attr("value",valores[valores.length-1]);
    $("#name_"+tipo).attr("value",nombres[nombres.length-1]);
   }
	}
}

function selectArchivos() {
  var idArchivos="";
  
  $("input[type='checkbox']").each(function () {
     if(this.checked) {
          idArchivos+=this.value+";";
     }
  });
  
  $("#adjuntos").attr("value",idArchivos);

}
function selectPara() {
  var para="";
  
  var nombres=$("#desk").getSelectedText();  
  for(var i=0;i<nombres.length;i++) {
    para+=nombres[i]+";"; 
  }

  nombres=$("#private").getSelectedText();
  for(var i=0;i<nombres.length;i++) {
    para+=nombres[i]+";";
  }
  $("#para").attr("value",para);
}
function save_data() {
	var privateEmail="",privateList="",deskEmail="",deskList="";
	
	var valores=$("#private").getAllValues();
	var textos=$("#private").getAllTexts();
	for(var i=0;i<valores.length;i++) {
		if( !isList("private",textos[i],valores[i]) ) {
			privateEmail+=textos[i]+"-"+valores[i]+";";
		} else {
			privateList+=textos[i]+"-"+valores[i]+";";
		}
	}

	valores=$("#desk").getAllValues();
	textos=$("#desk").getAllTexts();
	for(var i=0;i<valores.length;i++) {	 
		if( !isList("desk",textos[i],valores[i]) ) {
		  //alert("Soy SIMPLE texto="+textos[i]+" valor="+valores[i]);
			deskEmail+=textos[i]+"-"+valores[i]+";";
		} else {
		  //alert("Soy LISTA texto="+textos[i]+" valor="+valores[i]);
			deskList+=textos[i]+"-"+valores[i]+";";
		}
	}	
	//alert($("#removedNames").attr("value"));
	$("#privateEmail").attr("value",privateEmail);
	$("#privateList").attr("value",privateList);
	$("#deskEmail").attr("value",deskEmail);
	$("#deskList").attr("value",deskList);
//	alert("SIMPLES="+$("#deskEmail").attr("value")+"\nLISTAS="+$("#deskList").attr("value"));
	
	$('#formulario').submit();
}

