function trim(stringa) {
    return stringa.replace(/^\s+|\s+$/g,"");
}

function countCharRimanenti(elemText, spanId, maxChar) {
    if (elemText.value.length <= maxChar){
	document.getElementById(spanId).innerHTML = " Remains "+(maxChar-elemText.value.length)+" chars";
	return true;
    }
    elemText.value = elemText.value.substring(0, elemText.value.length-1)
    return false;
}

function verifyNumCharCommittees() {
    var strOut = "";
    var tit = trim(document.getElementById('title').value);
    if (tit.length>200)
        strOut += "Title too long (max 200 char)";
    var aut = trim(document.getElementById('author').value);
    if (aut.length>120)
        strOut += "Authors too long (max 120 char)";
    var inst = trim(document.getElementById('institute').value);
    if (inst.length>120)
        strOut += "Institute too long (max 120 char)";
    var abs = trim(document.getElementById('abstract').value);
    if (abs.length>4000)
	strOut += "Abstract too long (max 4000 char)";

    if (strOut == "")
        document.forms[0].submit();
    else
        alert(strOut);	
}

function verifyNumChar() {
    var strOut = "";
    var tit = trim(document.getElementById('title').value);
    if (tit.length>200)
        strOut += "Title too long (max 200 char)";
    var aut = trim(document.getElementById('author').value);
    if (aut.length>120)
        strOut += "Authors too long (max 120 char)";
    var inst = trim(document.getElementById('institute').value);
    if (inst.length>120)
        strOut += "Institute too long (max 120 char)";
    var abs = trim(document.getElementById('abstract').value);
    if (abs.length>1200)
	strOut += "Abstract too long (max 1200 char)";

    if (strOut == "")
        document.forms[0].submit();
    else
        alert(strOut);	
}

function verifyAccomodationForm() {
    var strOut = "";
    var date1;
    var date2;
    var now = new Date();
    if (trim(document.getElementById("check_in").value) == "") {
	strOut += "Data di arrivo obbligatoria\n";
    } else {
	var datearr = document.getElementById('check_in').value.split('-');
	date1 = new Date(datearr[2], datearr[1]-1, datearr[0], 23, 59, 59);
    }
    if (trim(document.getElementById("check_out").value) == "") {
	strOut += "Data di partenza obbligatoria\n";
    }  else {
	var datearr = document.getElementById('check_out').value.split('-');
	date2 = new Date(datearr[2], datearr[1]-1, datearr[0], 23, 59, 59);
    }
    if (trim(document.getElementById("hotel").value) == "") {
	strOut += "Hotel obbligatorio\n";
    }
    if (strOut == "") {
        if (date1.getTime()>date2.getTime()) {
	    alert("Data arrivo maggiore di data partenza");
	} else if(date1.getTime() < now.getTime()) {
            alert("Data arrivo antecedente data odierna");
        }  else if(now.getTime() > date2.getTime()) {
            alert("Data partenza antecedente data odierna");
        } else {
	    document.forms[0].submit();
	}
    } else {
        alert(strOut);
    }
}

function checkFormRegistrazione() {
    var strOut = "";
    if (trim(document.getElementById("cognome").value) == "") {
	strOut += "Cognome non valido \n";
    }
    if (trim(document.getElementById("nome").value) == "") {
	strOut += "Nome non valido \n";
    }
    if (trim(document.getElementById("ente").value) == "") {
	strOut += "Ente non valido \n";
    }
    if (trim(document.getElementById("indirizzo").value) == "") {
	strOut += "Indirizzo non valido \n";
    }
    if (trim(document.getElementById("citta").value) == "") {
	strOut += "Città non valida \n";
    }
    if (trim(document.getElementById("codice").value) == "") {
	strOut += "Codice postale non valido \n";
    }
    if (trim(document.getElementById("nazione").value) == "") {
	strOut += "Nazione non valida \n";
    }
    if (trim(document.getElementById("telefono").value) == "") {
	strOut += "Telefono non valido \n";
    }
    if (trim(document.getElementById("email").value) == "") {
	strOut += "E-Mail non valida \n";
    }
    if (trim(document.getElementById("email").value) != "") {
	var eml = checkEmail(document.getElementById("email"));
	if(!eml)
	    strOut += "Formato E-Mail non valido \n";
    }
    if (document.getElementById("socio1").checked || document.getElementById("socio2").checked || document.getElementById("socio3").checked || document.getElementById("socio4").checked || document.getElementById("socio5").checked) {
	//Ok non famu nenti
    } else {
	strOut += "Selezionare una categoria di appartenenza \n";
    }
    /*if (trim(document.getElementById("abstract").value) == "") {
	strOut += "Titolo abstract non valido \n";
    }*/
    if (strOut=="")
	document.forms[0].submit();
    else 
	alert(strOut);
}

function checkEmail(email) {
    //var email = document.getElementById(’emailaddress’);
    var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    if (!filter.test(email.value)) {
	return false;
    }
    return true;
}