﻿// File JScript
function setMaxLength() {
	var x = document.getElementsByTagName('textarea');
	var counter = document.createElement('div');
	counter.className = 'counter';
	for (var i=0;i<x.length;i++) {
		if (x[i].getAttribute('maxlength')) {
			var counterClone = counter.cloneNode(true);
			counterClone.relatedElement = x[i];
			counterClone.innerHTML = '<span>0</span>/'+x[i].getAttribute('maxlength');
			x[i].parentNode.insertBefore(counterClone,x[i].nextSibling);
			x[i].relatedElement = counterClone.getElementsByTagName('span')[0];

			x[i].onkeyup = x[i].onchange = checkMaxLength;
			x[i].onkeyup();
		}
	}
}

function checkMaxLength() {
	var maxLength = this.getAttribute('maxlength');
	var currentLength = this.value.length;
	if (currentLength > maxLength)
	{
	    alert("Hai raggiunto il numero massimo di caratteri utilizzabili.")
	    this.value= String(this.value).substring(0,maxLength);
		this.relatedElement.firstChild.nodeValue = maxLength;
	}
	else
	    this.relatedElement.firstChild.nodeValue = currentLength;
	// not innerHTML
}

function Preview()
{        
    var inseguitore = "";
    var inseguito = ""; 
    
    radioObj = document.forms['card'].elements['rbInseguitore'];
    radioLength = radioObj.length;
    
    for(var i = 0; i < radioLength; i++) {
	    if(radioObj[i].checked) {
		    inseguitore = radioObj[i].value;
		    break;
	    }
    }
    
    if (inseguitore == "")
    {
        alert("Selezionare il personaggio che inseguirà..");
        return;
    }
    
    radioObj = document.forms['card'].elements['rbInseguito'];
    radioLength = radioObj.length;
    
    for(var i = 0; i < radioLength; i++) {
	    if(radioObj[i].checked) {
		    inseguito = radioObj[i].value;
		    break;
	    }
    }

    if (inseguito == "")
    {
        alert("Selezionare il personaggio che deve essere inseguito..");
        return;
    }

    strTesto = document.getElementById('txtMessaggio').value;
    
    if (strTesto == "") 
    {
        alert("Immettere il testo del messaggio..");
        return;
    }
    else
    {
        alertstr = "";
        allvalid = true;
        var checkstr = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+-/*,.'@~#%$£€!µ°±(){}[ ]";
		var badchars = "";
		for (var j=0; j<strTesto.length; j++) {
			for (var k=0; k<checkstr.length; k++) {
				if (strTesto.charAt(j) == checkstr.charAt(k)) {
					break;
				}
			}
			if (k == checkstr.length) {
				for (var l=0; l<badchars.length; l++) {
					if (strTesto.charAt(j) == badchars.charAt(l)) {
						break;
					}
				}

				if (l == badchars.length) {
					badchars += strTesto.charAt(j);
				}						allvalid = false;
			}
		}
		if (!allvalid) {
			alertstr = "Il testo immesso contiene i seguenti caratteri non consentiti...\n\t" + badchars + "\n\n";
			alert(alertstr);
		    return;
		}
    }
    document.getElementById('ifraPreview').src="cards/preview.aspx?inseguito=" + inseguito + "&inseguitore=" + inseguitore + "&testo=" + strTesto;
}    

function SetupForm()
{
    Nifty("div#divTestata","normal top");
    Nifty("div#divFasciaTitolo","normal bottom");
    Nifty("div#divFasciaPersonaggio","normal top");
    Nifty("div#divFasciaMessaggio","normal bottom");
    Nifty("div#divFooter","normal bottom");
}

function ControllaMail(strName)
{
    
    EmailAddr = document.getElementById(strName).value;
    Filtro = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-]{2,})+\.)+([a-zA-Z0-9]{2,})+$/;

    if (Filtro.test(EmailAddr))
        return true;
    else
        return false;
}

function ControllaRadio(nome_radio)
{
    value = "";
    radioObj = document.forms['FormRegistrazione'].elements[nome_radio];
    radioLength = radioObj.length;
    
    for(var i = 0; i < radioLength; i++) {
	    if(radioObj[i].checked) {
		    value = radioObj[i].value;
		    break;
	    }
    }    
    
    return value;
}

function ControllaCombo(nome_combo)
{
    return (document.forms['card'].elements[nome_combo].value);
}


function ControllaForm()
{
    var strMessaggio = "";
    
    if (ControllaRadio('rblPrivacy') == "NON ACCETTO")
    {
        alert("Non è possibile proseguire se non viene accettato il trattamento dei dati personali");
        return false;
    }
    
    // dati
    if (document.getElementById('txtNome').value == "") strMessaggio += "- nome\n\r";
    if (document.getElementById('txtCognome').value == "") strMessaggio += "- cognome\n\r";
    if (document.getElementById('txtIndirizzo').value == "") strMessaggio += "- indirizzo\n\r";
    if (document.getElementById('txtCitta').value == "") strMessaggio += "- città\n\r";
    if (document.getElementById('txtProvincia').value == "") strMessaggio += "- provincia\n\r";
    if (document.getElementById('txtCAP').value == "") strMessaggio += "- CAP\n\r";
    if (document.getElementById('txtTelefono').value == "") strMessaggio += "- telefono\n\r";
    if (document.getElementById('txtProfessione').value == "") strMessaggio += "- professione\n\r";
    if (ControllaRadio('ente') == false) strMessaggio += "- tipo ente\n\r";
	if (ControllaRadio('ente') != "privato")
	{
   	 if (document.getElementById('txtEnte').value == "") strMessaggio += "- nome ente / qualifica\n\r";
	}
    if (document.getElementById('txtProfessione').value == "") strMessaggio += "- professione\n\r";
    if (document.getElementById('txtUsername').value == "") strMessaggio += "- username\n\r";
  /*  if (document.getElementById('txtPassword').value == "") strMessaggio += "- password\n\r"; */
    if (ControllaMail('txtEmail') == false) strMessaggio += "- email\n\r";
    if (ControllaRadio('rblPrivacy') == "")  strMessaggio += "- trattamento dati personali\n\r";
    
    if (strMessaggio.length > 0)
    {
        alert("I seguenti dati risultano mancanti o incorretti:\n\r" + strMessaggio);
        return false;
    }
    else
    {   
        return true;
    }
}

function SvuotaRadio(nome_radio)
{
    value = "";
    radioObj = document.forms['FormRegistrazione'].elements[nome_radio];
    radioLength = radioObj.length;
    
    for(var i = 0; i < radioLength; i++) {
	    radioObj[i].checked = false;
    }    
    
    return true;
}

function SvuotaForm()
{
    document.getElementById('txtNome').value = "";
    document.getElementById('txtCognome').value = "";
    document.getElementById('txtIndirizzo').value = "";
    document.getElementById('txtCitta').value = "";
    document.getElementById('txtProvincia').value = "";
    document.getElementById('txtCAP').value = "";
    document.getElementById('txtTelefono').value = "";
    document.getElementById('txtProfessione').value = "";
    document.getElementById('txtEmail').value = "";
    document.getElementById('txtUsername').value = "";
   /*  document.getElementById('txtPassword').value = "";*/
    document.getElementById('txtEmail').value = "";
    document.getElementById('txtEnte').value = "";  
    SvuotaRadio('rblPrivacy');  
    SvuotaRadio('ente');  
}