// Lancio della piattaforma
function launchLMS()
{
    var launch_url = '/LaunchLMS.aspx';
    var width = window.screen.width;
	var height = window.screen.height;
	var win_opt = 'status=yes,location=no,toolbar=no,menubar=no,width=';
	win_opt += (width-10) + ',height=' + (height-75);
	var label = window.open(launch_url ,'label', win_opt);
	label.moveTo(0,0);
	label.focus();
}

// Controllo sul modulo di autenticazione LMS
function loginLMS(id_username, id_password)
{
    var userid = document.getElementById(id_username);
    var password = document.getElementById(id_password);
    
    if(userid.value.replace(/\s/g, '') == '')
    {
        alert('Inserire lo username');
        userid.focus();
        return false;
    }
    
    if(password.value.replace(/\s/g, '') == '')
    {
        alert('Inserire la password')
        password.focus();
        return false;
    }
    return true;
}

// Controllo sul modulo di registrazione alla newsletter
function regNewsletter(id_email)
{
    var email = document.getElementById(id_email);
    
    if(email.value.replace(/\s/g, '') == '')
    {
        alert('Indicare l\'indirizzo e-mail');
        email.focus();
        return false;
    }
    
    // verifico validità indirizzo e-mail
    if(!(checkMailAddress(email.value)))
    {
        alert('Indicare un indirizzo e-mail valido')
        email.focus();
        return false;
    }
    return true;
}

// Controllo sul modulo ricerca corsi (Tipo 3)
function checkSrcCrs(id_txtCorsi)
{
    // Verifico che abbia inserito un termine per la ricerca
    var txtCorsi = document.getElementById(id_txtCorsi).value;
    if(txtCorsi.replace(/\s/g, '') == '')
    {
        alert('Inserire un termine per la ricerca');
        document.getElementById(id_txtCorsi).value = '';
        document.getElementById(id_txtCorsi).focus();
        return false;
    }
    // Almeno di 3 caratteri
    if(txtCorsi.length < 3)
    {
        alert('Inserire un termine di almeno tre caratteri');
        document.getElementById(id_txtCorsi).value = '';
        document.getElementById(id_txtCorsi).focus();
        return false;
    }
    
    // Valorizzo gli elementi e invio in POST
	  document.forms[0].action = "/Default.aspx";
    document.forms[0].elements['tipo'].value = 3;
    document.forms[0].elements['search'].value = txtCorsi;
    document.forms[0].submit();
}

/* 
Controllo aula/online sul motore
di ricerca dei corsi
*/
function chkTypeCrsSrc(id_first_chk, id_second_chk)
{
    var first_chk = document.getElementById(id_first_chk)
    var second_chk = document.getElementById(id_second_chk)

    // Verifico se si sta deselezionando l'elemento   
    if(first_chk.checked == false && second_chk.checked == false)
    {
        first_chk.checked = true;
        alert('E\' necessario specificare almeno una tipologia di corso');
        first_chk.focus();
    }
}

/* 
Funzioni per disabilitare il submit
automatico premendo <enter> 
*/
function disableEnterKey(e)
{
    var key;
    window.event ? key = window.event.keyCode : key = e.which;
    return key != 13;
}

/**
* Aggiorna data e ora in tempo reale
*/
function DateTime(ID_Data, ID_Ora)
{
    // Giorni della settimana
    var Giorni = new Array(7);
	Giorni[0] = "Domenica";
	Giorni[1] = "Lunedì";
	Giorni[2] = "Martedì";
	Giorni[3] = "Mercoledì";
	Giorni[4] = "Giovedì";
	Giorni[5] = "Venerdì";
	Giorni[6] = "Sabato";

    // Mesi dell'anno
	var Mesi = new Array(12);
	Mesi[0] = "Gennaio";
	Mesi[1] = "Febbraio";
	Mesi[2] = "Marzo";
	Mesi[3] = "Aprile";
	Mesi[4] = "Maggio";
	Mesi[5] = "Giugno";
	Mesi[6] = "Luglio";
	Mesi[7] = "Agosto";
	Mesi[8] = "Settembre";
	Mesi[9] = "Ottobre";
	Mesi[10] = "Novembre";
	Mesi[11] = "Dicembre";
	
    var adesso = new Date();
    var mm = adesso.getMonth();
    var yy = adesso.getFullYear();
    var oo = adesso.getHours();
    var mn = adesso.getMinutes();
    var sec = adesso.getSeconds();
    var dd = adesso.getDay();
	var n_day = adesso.getDate();
	
	if(oo < 10) { oo = '0' + oo; }
	if(mn < 10) { mn = '0' + mn; }
	if(sec < 10) { sec = '0' + sec; }
	
	// Stampa a video data e ora
	var txtData = Giorni[dd] + ' ' + n_day + ' ' + Mesi[mm] + ' ' + yy;
	var txtOra = oo + ':' + mn + ':' + sec;
	var Data = document.getElementById(ID_Data);
	var Ora = document.getElementById(ID_Ora);
    Data.innerHTML = txtData;
    Ora.innerHTML = txtOra;
    
    // Richiamo la funzione ogni secondo
    window.setTimeout("DateTime('" + ID_Data + "', '" + ID_Ora + "')", 1000);
}

// Controllo di validazione sul form
// di invio segnalazione
function validaSegnalazione(ID_selArgomento, ID_testo)
{
    var selArgomento = document.getElementById(ID_selArgomento);
    var testo = document.getElementById(ID_testo);
    
    if(selArgomento.value == 0)
    {
        alert('Seleziona un argomento');
        selArgomento.focus();
        return false;
    }
    
    if(testo.value.replace(/\s/g, '') == '')
    {
        alert('Inserire il testo della segnalazione');
        testo.focus();
        return false;
    }
}

// Controllo di validazione sul form
// di Help Desk
function validaHelpDesk(ID_Tipologia, ID_Nome, ID_Cognome, ID_Email, ID_Telefono)
{
    var Tipologia = document.getElementById(ID_Tipologia);
    var Nome = document.getElementById(ID_Nome);
    var Cognome = document.getElementById(ID_Cognome);
    var Email = document.getElementById(ID_Email);
    var Telefono = document.getElementById(ID_Telefono)
    
    if(Tipologia.value == 0)
    {
        alert('Seleziona una tipologia');
        Tipologia.focus();
        return false;
    }
    
    if(Nome.value.replace(/\s/g, '') == '')
    {
        alert('Inserire il nome');
        Nome.focus();
        return false;
    }
    
    if(Cognome.value.replace(/\s/g, '') == '')
    {
        alert('Inserire il cognome');
        Cognome.focus();
        return false;
    }
    
    // Ripulisco email e telefono dagli spazi
    var email_text = Email.value.replace(/\s/g, '');
    var tel_text = Telefono.value.replace(/\s/g, '');
    
    // Almeno una tra email e telefono
    if(email_text == '' && tel_text == '')
    {
        alert("Inserire la propria E-mail o il numero di telefono");
        return false;
    }
}

// Setta il tipo di parametro in home page
function setTipo(tipo) { document.forms[0].tipo.value = tipo; }

// Apre il cambio password (solo per Rete Autostrade)
function openChangePsw()
{
    var URL_CHANGE_PSW = 'https://pwdsrv.gruppo.autostrade.it';
	var win_opt = "width=800,height=600,resizable=yes,toolbar=yes,location=yes";
	window.open(URL_CHANGE_PSW, 'changepsw', win_opt);  
}

// Switch tra le tab Corsi e MieiCorsi
function selTabCorsi(id_lnkTuttiCorsi, id_lnkMieiCorsi, id_idxTabCorsi, sel_all)
{
    var gridCorsi = document.getElementById('rowRsCorsi');
    var gridMieiCorsi = document.getElementById('rowRsMieiCorsi');
    var lnkTuttiCorsi = document.getElementById(id_lnkTuttiCorsi);
    var lnkMieiCorsi = document.getElementById(id_lnkMieiCorsi);
    var tab_box_corsi_sx = document.getElementById('tab_box_corsi_sx');
    var tab_box_corsi_1 = document.getElementById('tab_box_corsi_1');
    var tab_box_corsi_2 = document.getElementById('tab_box_corsi_2');
    var tab_box_corsi_3 = document.getElementById('tab_box_corsi_3');
    var pager_corsi = document.getElementById('PagerCorsi');
    var pager_miei_corsi = document.getElementById('PagerMieiCorsi');
    var idxTabCorsi = document.getElementById(id_idxTabCorsi);

    // Questo verifica se il pannello con i tab è esistente
    if(document.getElementById(id_lnkTuttiCorsi))
    {
        // Verifica se è stato selezionato
        // il pannello di tutti i corsi
        if(sel_all == '0')
        {
            lnkTuttiCorsi.style.fontWeight = '';
            lnkMieiCorsi.style.fontWeight = 'bold';
            lnkTuttiCorsi.parentNode.className = '';
            lnkMieiCorsi.parentNode.className = 'tab_corsi';
            tab_box_corsi_sx.className = 'tab_corsi_sx';
            tab_box_corsi_1.style.borderTop = '1px solid #DEDEDE';
            tab_box_corsi_2.style.borderTop = '';
            gridCorsi.style.display = 'none';
            gridMieiCorsi.style.display  = '';
            pager_miei_corsi.style.display = '';
            pager_corsi.style.display = 'none';
            idxTabCorsi.value = 0;
        }
        else
        {
            gridCorsi.style.display = '';
            gridMieiCorsi.style.display  = 'none';
            lnkTuttiCorsi.style.fontWeight = 'bold';
            lnkMieiCorsi.style.fontWeight = '';
            lnkTuttiCorsi.parentNode.className = 'tab_corsi';
            lnkMieiCorsi.parentNode.className = '';
            tab_box_corsi_sx.className = 'tab_corsi_sx2';
            tab_box_corsi_1.style.borderTop = '';
            tab_box_corsi_2.style.borderTop = '1px solid #DEDEDE';
            pager_corsi.style.display = '';
            pager_miei_corsi.style.display = 'none';
            idxTabCorsi.value = 1;
        }
    }
}

/* funzioni per scrollare le news */

    /***********************************************
    * Cross browser Marquee II- © Dynamic Drive (www.dynamicdrive.com)
    * This notice MUST stay intact for legal use
    * Visit http://www.dynamicdrive.com/ for this script and 100s more.
    ***********************************************/
    var delayb4scroll=2000; //Specify initial delay before marquee starts to scroll on page (2000=2 seconds)
    var marqueespeed=1;     //Specify marquee scroll speed (larger is faster 1-10)
    var pauseit=1;          //Pause marquee onMousever (0=no. 1=yes)?
    ////NO NEED TO EDIT BELOW THIS LINE////////////
    var copyspeed=marqueespeed;
    var pausespeed=(pauseit==0)? copyspeed: 0
    var actualheight='';
    
    function scrollmarquee()
    {
        if (parseInt(cross_marquee.style.top)>(actualheight*(-1)+8))
        {
            cross_marquee.style.top=parseInt(cross_marquee.style.top)-copyspeed+"px"
            // metto in pausa per rallentare lo scroll della news
            pausecomp(50)
        }
        else
        {
            cross_marquee.style.top=parseInt(marqueeheight)+8+"px"
        }
    }
    function initializemarquee()
    {
        cross_marquee=document.getElementById("vmarquee")
        cross_marquee.style.top=0
        marqueeheight=document.getElementById("marqueecontainer").offsetHeight
        actualheight=cross_marquee.offsetHeight
        if (window.opera || navigator.userAgent.indexOf("Netscape/7")!=-1)
        { //if Opera or Netscape 7x, add scrollbars to scroll and exit
            cross_marquee.style.height=marqueeheight+"px"
            cross_marquee.style.overflow="scroll"
            return
        }
        setTimeout('lefttime=setInterval("scrollmarquee()",30)', delayb4scroll)
    }
    
    function pausecomp(millis) 
    {
        var date = new Date();
        var curDate = null;

        do { curDate = new Date(); } 
        while(curDate-date < millis);
    } 


/******************************************************************************
* Functions To Check an EMail Address syntax validity
******************************************************************************/

function checkMailAddress(emailStr)
{
	emailStr = emailStr.trim();
    var emailPat=/^(.+)@(.+)$/;
    var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
    var validChars="\[^\\s" + specialChars + "\]";
    var quotedUser="(\"[^\"]*\")";
    var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
    var atom=validChars + '+';
    var word="(" + atom + "|" + quotedUser + ")";
    var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
    var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
    var matchArray=emailStr.match(emailPat);
    if (matchArray==null)
    {
        //alert("L'indirizzo email non sembra corretto. Controlla.")
        return false;
    }
    var user=matchArray[1];
    var domain=matchArray[2];
    
    if (user.match(userPat)==null)
    {
        //alert("Il nome utente non sembra corretto.")
        return false;
    }
    
    var IPArray=domain.match(ipDomainPat);
    if (IPArray!=null)
    {
        for (var i=1;i<=4;i++)
        {
            if (IPArray[i]>255)
            {
                //alert("L'IP di destinazione non è corretto.")
                return false;
            }
        }
        return true;
    }
    
    var domainArray=domain.match(domainPat);
    if (domainArray==null)
    {
        //alert("Il nome del dominio non sembra valido.")
        return false;
    }
    
    var atomPat=new RegExp(atom,"g");
    var domArr=domain.match(atomPat);
    var len=domArr.length;
    if (domArr[domArr.length-1].length<2 || domArr[domArr.length-1].length>3)
    {
        /*alert("L'indirizzo email deve terminare con un 
        dominio di tre lettere (.com, .net, etc...) o con due lettere 
        per i domini nazionali (.it, .fr, etc..).")*/
        return false;
    }
    
    if (len<2)
    {
        //var errStr="Questo indirizzo non presenta il nome dell'host!"
        //alert(errStr)
        return false;
    }

	var Filtro = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-]{2,})+\.)+([a-zA-Z0-9]{2,})+$/;
	return Filtro.test(emailStr);

}

// dovrebbe controllare solo che ci siano il carattere @ e . e poi che nel caso l'estensione del dominio sia di 3 char questa sia esattamente tra quelle ammesse altrimenti se è di 2 tutto va bene...
function checkMailAddress2(mail) 
{
  return mail.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\..{2,2}))$)\b/gi);
}

String.prototype.trim = function() {
  var x=this;
  x=x.replace(/^\s*(.*)/, "$1");
  x=x.replace(/(.*?)\s*$/, "$1");
  return x;
}

