

 
function enviar_previo(){
	
    texto=escape(document.getElementById("textmsj").value);
    remitente=document.getElementById("remitente").value;   
	if (document.getElementById("programado").checked) 
	programado=1;
	else programado=0;
	fecha = document.getElementById("data").value+document.getElementById("hora").value ;
	
	// Datos de env&iacute;o individual
	if ($("dd:not(:first)").is(":hidden"))  {	
	telefon= document.getElementById("telefon").value;	
	
    
	$.getJSON("formato-correcto.php?telefon="+telefon+"&remitente="+remitente,

		function(json){	
		
		
			$("#mensaje").empty();
			if (json==1)   $("#mensaje").append("Se va a realizar un env&iacute;o de  <span id='precio'> </span> SMS. &iquest;Est&aacute; usted seguro? <span id='mensaje'></span><input type='button' id='enviar' name='enviar' alt='Enviar mensaje' value='Enviar mensaje' tabindex='12' class='button' onclick='enviar_datos();'> ");		
			
			else if (json==2)  $("#mensaje").append("El n&uacute;mero escrito en el campo <b>Tel&eacute;fono</b> no tiene el formato correcto. Debe escribir un n&uacute;mero de 11 cifras(incluyendo el prefijo del pa&iacute;s). ");
			else if (json==3)  $("#mensaje").append("El  campo <b>Remitente</b> no tiene el formato correcto. Debe escribir un texto o un n&uacute;mero. (max. 11 caracteres) ");
			else $("#mensaje").append("Error de conexión");
			
		}
	  
	);	
	}
	else {
	
	// Datos de env&iacute;o masivo	
	grupo=document.getElementById("grupo").value;
	provincia=document.getElementById("provincia").value;
	edadMax=document.getElementById("edadmax").value;
	edadMin=document.getElementById("edadmin").value;
	lenguaje=document.getElementById("lenguaje").value;
	
	
	$.getJSON("formato-correcto-masivo.php?grupo="+grupo+"&provincia="+provincia+"&edadmax="+edadMax+"&edadmin="+edadMin+"&lenguaje="+lenguaje+"&texto="+texto+"&remitente="+remitente+"&programado="+programado+"&fecha="+fecha,

		function(json){	
		    if (json==0) {
				$("#mensaje").empty();	
				$("#mensaje").append("No hay ning&uacute;n usuario que cumpla con esas caracter&iacute;sticas");
			
			}
			else{
			$("#mensaje").empty();						
			$("#mensaje").append("Se va a realizar un env&iacute;o de  <span id='precio'>"+json+"</span> SMS. &iquest;Est&aacute; usted seguro?<input type='button' id='enviar' name='enviar' alt='Enviar mensaje' value='Enviar mensaje' tabindex='12' class='button' onclick='enviar_datos();'>"    );
			}
		}
	  
	);	
	
	
	
	
	}	
	
	
	
	cargar_saldo();
	}
	
	
	
	
function enviar_datos( ){
	texto=document.getElementById("textmsj").value;	
	if(document.getElementById("baja").checked) texto=texto + "NO+SMS:"+document.getElementById("bajanum").value;
	//alert(document.getElementById("textmsj").value);
	texto= escape(utf8_encode(texto));
	texto=texto.replace('+', '%2B');
	
	//texto=document.getElementById("textmsj").value;	
    remitente=document.getElementById("remitente").value;   

	
	$("#enviar").attr("disabled", true);

	
	//bajanum= document.getElementById("bajanum").value; 
	
	if (document.getElementById("programado").checked) 
	programado=1;
	else programado=0;
	fecha = document.getElementById("data").value+document.getElementById("hora").value ;
	
	// Datos de env&iacute;o individual
	if ($("dd:not(:first)").is(":hidden"))  {	
	telefon= document.getElementById("telefon").value;	
	
    //alert(texto);
	$.getJSON("enviar.php?telefon="+telefon+"&texto="+texto+"&remitente="+remitente+"&programado="+programado+"&fecha="+fecha,

		function(json){	
		
		
			
			if (json==1)     $("#mensaje").append("<div id='ok'>Enviado correctante </div><br><iframe id='emulator' src='emulator.php?mensaje="+texto+"' width='100%' height='350'> />");	
			else $("#mensaje").append("<div class='error'>Error de env&iacute;o</div>");
			$("#enviar").remove();
		}
	  
	);	
	}
	else {
	
	// Datos de env&iacute;o masivo	
	grupo=document.getElementById("grupo").value;
	provincia=document.getElementById("provincia").value;
	edadMax=document.getElementById("edadmax").value;
	edadMin=document.getElementById("edadmin").value;
	lenguaje=document.getElementById("lenguaje").value;
	
	$.getJSON("enviarmasivo.php?grupo="+grupo+"&provincia="+provincia+"&edadmax="+edadMax+"&edadmin="+edadMin+"&lenguaje="+lenguaje+"&texto="+texto+"&remitente="+remitente+"&programado="+programado+"&fecha="+fecha,

		function(json){	
		
		
			
			
			if (json==1)     $("#mensaje").append("<div id='ok'>Enviados correctante </div><br><iframe id='emulator' src='emulator.php?mensaje="+texto+"' width='100%' height='350'> />");	
			else $("#mensaje").append("<div class='error'>Error de env&iacute;o</div>");
			$("#enviar").remove();
			
		}
	  
	);	
	
	
	
	
	}	
	
	
	
	cargar_saldo();
	
	
	
	
	
	
	}



		


	
	
	
	
	
	
	
	
	
function cargar_saldo(){

$.getJSON("consultar_saldo.php",

		function(json){	
		
		
			$("#saldo").empty();
			
			
			$("#saldo").append(json);
		
		}
	  
	);	


}






function borrar_contacto(id,user_id)
{	

	$.getJSON("servidor_json.php?op=1&id="+id+"&user_id="+user_id ,

		function(json){			
			
			//Borrar contenido de tabla
			$("#myTbody").empty();			
			
			if (json==null){ $(".caixapla").append("<div class='elem10'>La agenda est&aacute; vac&iacute;a</div>"); 
							 $("#tabla").empty();		
								}
			// Cargamos de nuevo los datos de la tabla 
			for (var j =0; j < json.ID.length; j++) {				
					
				$("#myTbody").append("<tr> <td>"+json.NOMBRE[j]+"</td> <td>"+json.APELLIDOS[j]+"</td><td>"+json.ALIAS[j]+"</td><td>"+json.NUMERO_TEL[j]+"</td><td>"+json.ID_GRUPO[j]+"</td> <td>"+json.PROVINCIA[j]+"</td> <td>"+json.EDAD[j]+"</td> <td>"+json.LENGUAJE[j]+"</td> <td> <a href=# onClick='borrar_contacto("+json.ID[j]+","+json.ID_USUARIO[j]+")'>Borrar</a> </td><td> <a href='edit-contact.php?id="+json.ID[j]+"'>Editar</a> </td> </tr>" );		
				
				
			}	
			stripe();
		
		}
	  
	);	
	
	
}




    function cargar_telefono(){

		document.getElementById("telefon").value=document.getElementById("agenda").value;

	}


	


    // Función para efecto en  tabla
	var stripe = function() {
/* 

	Javascript to style odd/even table rows

	Derived from 'Zebra Tables' by David F. Miller (http://www.alistapart.com/articles/zebratables/)	

	Modified by Jop de Klein, february 2005

	jop at validweb.nl

	http://validweb.nl/artikelen/javascript/better-zebra-tables/

*/

		var tables = document.getElementsByTagName('table');

		for(var x=0;x!=tables.length;x++){

			var table = tables[x];

			if (! table) { return; }

			

			var tbodies = table.getElementsByTagName('tbody');

			

			for (var h = 0; h < tbodies.length; h++) {

				var even = true;

				var trs = tbodies[h].getElementsByTagName('tr');

				

				for (var i = 0; i < trs.length; i++) {

					trs[i].onmouseover=function(){

						this.className += ' ruled'; return false

					}

					trs[i].onmouseout=function(){

						this.className = this.className.replace('ruled', ''); return false

					}

					

					if(even)

						trs[i].className += ' even';

					

					even = !even;

				}

			}

		}

	}

	function utf8_encode ( argString ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Webtoolkit.info (http://www.webtoolkit.info/)
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: sowberry
    // +    tweaked by: Jack
    // +   bugfixed by: Onno Marsman
    // +   improved by: Yves Sucaet
    // +   bugfixed by: Onno Marsman
    // *     example 1: utf8_encode('Kevin van Zonneveld');
    // *     returns 1: 'Kevin van Zonneveld'
 
    var string = (argString+'').replace(/\r\n/g, "\n").replace(/\r/g, "\n");
 
    var utftext = "";
    var start, end;
    var stringl = 0;
 
    start = end = 0;
    stringl = string.length;
    for (var n = 0; n < stringl; n++) {
        var c1 = string.charCodeAt(n);
        var enc = null;
 
        if (c1 < 128) {
            end++;
        } else if((c1 > 127) && (c1 < 2048)) {
            enc = String.fromCharCode((c1 >> 6) | 192) + String.fromCharCode((c1 & 63) | 128);
        } else {
            enc = String.fromCharCode((c1 >> 12) | 224) + String.fromCharCode(((c1 >> 6) & 63) | 128) + String.fromCharCode((c1 & 63) | 128);
        }
        if (enc !== null) {
            if (end > start) {
                utftext += string.substring(start, end);
            }
            utftext += enc;
            start = end = n+1;
        }
    }
 
    if (end > start) {
        utftext += string.substring(start, string.length);
    }
 
    return utftext;
}
 



