
function goNext() {
    if ((document.getElementById('hd_Next').value == "True"))
    {
        window.location = 'Bienvenida.aspx';
    }
    
    if (document.getElementById ('txt_msg_error').value != "") {
				alert (document.getElementById ('txt_msg_error').value);
			}
}
function checkLogin() {
    var OK = true;

    if ((document.getElementById('txt_user').value != "") && (document.getElementById('txt_pass').value != ""))
    {
		OK = true;
    }
    else
    {
        alert ('Por favor, introduzca usuario y password');
        OK = false;
    }


    if (OK) document.getElementById('FormLogin').submit();
}

function showContacts()
			{
				var Contact, p;

				if(fComponer.txt_to.value != ''){
					p = fComponer.txt_to.vaue;
				}
				Contact = window.showModalDialog('Contactos.aspx', p, 'dialogHeight:370px; dialogWidth:570px; scrolling=no; noresize');
				if(Contact!=null)
				{
				    if (document.getElementById('txt_to').value != '')
				    {
				        document.getElementById('txt_to').value= document.getElementById('txt_to').value + ";" + Contact;
			        }
			        else
			        {
			            document.getElementById('txt_to').value= Contact;
			        }
			    }
			}


function enviar() {
	if (document.getElementById('txt_to').value=="") {
		alert('Se necesitan destinatarios para enviar la tarjeta');
		document.getElementById('txt_to').focus();
		return false;
	}
	if (document.getElementById('txt_mensaje').value=="") {
		alert('El mensaje es un campo obligatorio');
		document.getElementById('txt_mensaje').focus();
		return false;
	}
    document.getElementById('hd_action').value = "S";
    fComponer.submit();
}


function addContacto()
            {
	            obj=document.getElementById('lst_contactosFrom');
	            if (obj.selectedIndex==-1) return;
                for (i=0; opt=obj.options[i]; i++)
                if (opt.selected) {
    	            valor=opt.value; // almacenar value
    	            txt=obj.options[i].text; // almacenar el texto
    	            obj.options[i]=null; // borrar el item si está seleccionado
    	            obj2=document.getElementById('lst_contactosTo');
                    opc = new Option(txt,valor);
    	            eval(obj2.options[obj2.options.length]=opc);
                }
            }


function remContacto ()
            {
                obj=document.getElementById('lst_contactosTo');
	            if (obj.selectedIndex==-1) return;
                for (i=0; opt=obj.options[i]; i++)
                if (opt.selected) {
    	            valor=opt.value; // almacenar value
    	            txt=obj.options[i].text; // almacenar el texto
    	            obj.options[i]=null; // borrar el item si está seleccionado
    	            obj2=document.getElementById('lst_contactosFrom');
                    opc = new Option(txt,valor);
    	            eval(obj2.options[obj2.options.length]=opc);
    	        }
            }


function Send_list()
    {
             var i;
             var iFinal = '';
             var temp = '';
             var myquery1 = "<"
             var myquery2 = ">"

             obj=document.getElementById('lst_contactosTo');
             if (obj.length > 0)
             {
                    for (i=0; obj.length > i ; i++)
                    {
						temp=obj.options[i].text;
						temp=temp.substring(temp.indexOf(myquery1)+myquery1.length,temp.indexOf(myquery2));
                        iFinal += temp;
                        if (i != obj.length - 1)
                             iFinal += '; ';
                    }
             }

		     document.getElementById('hd_contacts').value = iFinal;
		     window.returnValue = iFinal;
		     window.close();
    }


function checkSession()
    {
        if (document.getElementById ('hdLogin').value == "False")
        {
            window.location = "Login.aspx"
        } else {
			if (document.getElementById('hd_action') && document.getElementById ('hd_action').value == "OK_enviado") {
				alert ('La tarjeta ha sido enviada');
			}
        }
    }
function ValidarFecha(txtInput){
	var Fecha= new String(txtInput.value)	// Crea un string
	var RealFecha= new Date()	// Para sacar la fecha de hoy
	
	var Ano= new String(Fecha.substring(Fecha.lastIndexOf("-")+1,Fecha.length))
	
	var Mes= new String(Fecha.substring(Fecha.indexOf("-")+1,Fecha.lastIndexOf("-")))
	
	var Dia= new String(Fecha.substring(0,Fecha.indexOf("-")))

	
	if (isNaN(Ano) || Ano.length<4 || parseFloat(Ano)<1900){
        	alert('Fecha incorrecta')
        	txtInput.focus()
		return true
	}
	
	if (isNaN(Mes) || parseFloat(Mes)<1 || parseFloat(Mes)>12){
		alert('Fecha incorrecta')
		txtInput.focus()
		return true
	}
	
	if (isNaN(Dia) || parseInt(Dia)<1 || parseInt(Dia)>31){
		alert('Fecha incorrecta')
		txtInput.focus()
		return true
	}
	if (Mes==4 || Mes==6 || Mes==9 || Mes==11 || Mes==2) {
		if (Mes==2 && Dia > 28 || Dia>30) {
			alert('Fecha incorrecta')
			txtInput.focus()
			return true
		}
	}
}


function buscar()
    {
		var error = false;
		if (document.getElementById('txt_fdesde').value != "") {
			error = ValidarFecha(document.getElementById('txt_fdesde'));
		}
		if (document.getElementById('txt_fhasta').value != "") {
			error = ValidarFecha(document.getElementById('txt_fhasta'));
		}
		if (!error) {
			document.getElementById('hd_action').value = "buscar";
			document.fBuscar.submit();
		}
    }

function preview() {
	if (document.getElementById('txt_mensaje').value!="") {
		eval('window.open("Preview.aspx?idtarjeta=' + document.getElementById('sel_tarjeta').value + '","Prev","toolbar=0,menubar=0,scrollbars=0,width=970,height=670");');
	} else {
		alert('Para previsualizar es necesario que escribas un texto.');
	}
		
		
}

function eliminar(id) {
	if (confirm("¿Estás seguro que deseas eliminar esta tarjeta?")) {
		document.getElementById('hd_action').value = "eliminar";
		document.getElementById('hd_ideliminar').value = id;
		document.fBuscar.submit();
	}
}

function ordenarContactos(select){
	arrTextos = new Array(); 
	arrValues = new Array(); 
	arrGuardaTextos = new Array(); 
	arrTextos[0]=arrValues[0]=arrGuardaTextos[0]=""; 
	var total = select.length;
	for(i=0; i<total; i++){
		arrTextos[i] = select.options[i].text;
		arrValues[i] = select.options[i].value;
		arrGuardaTextos[i] = select.options[i].text;
	}
	arrTextos.sort();
	for(i=0; i<total; i++){
		select.options[i].text = arrTextos[i];
		for(j=0; j<total; j++){
			if (arrTextos[i] == arrGuardaTextos[j]){
				select.options[i].value = arrValues[j];
				j = select.length;
			}
		}
	}
}