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 verifyNumChar() {
    var strOut = "";
    var tit = trim(document.getElementById('title').value);
    if (tit.length>400)
        strOut += "Title too long (max 400 char)";
    var aut = trim(document.getElementById('author').value);
    if (aut.length>400)
        strOut += "Authors too long (max 400 char)";
    var inst = trim(document.getElementById('institute').value);
    if (inst.length>400)
        strOut += "Institute too long (max 400 char)";
    var abs = trim(document.getElementById('abstract').value);
    if (abs.length>3000)
        strOut += "Abstract too long (max 3000 char)";

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

function validateRegistrationForm() { 
    var strOut = "";
    if (trim(document.getElementById("cognome").value) == "") {
        strOut += "Last name is not valid \n";
    }
    if (trim(document.getElementById("nome").value) == "") {
        strOut += "First Name is not valid \n";
    }
    if (trim(document.getElementById("ente").value) == "") {
        strOut += "University/Company is not valid \n";
    }
    if (trim(document.getElementById("indirizzo").value) == "") {
        strOut += "Address is not valid \n";
    }
    if (trim(document.getElementById("citta").value) == "") {
        strOut += "City is not valid \n";
    }
    if (trim(document.getElementById("codice_postale").value) == "") {
        strOut += "Zip Code is not valid \n";
    }
    if (trim(document.getElementById("nazione").value) == "") {
        strOut += "Country is not valid \n";
    }
    if (trim(document.getElementById("telefono").value) == "") {
        strOut += "Phone/Fax is not valid \n";
    }
    if (trim(document.getElementById("email").value) == "") {
        strOut += "E-Mail is not valid \n";
    }
    if (trim(document.getElementById("email").value) != "") {
        var eml = checkEmail(document.getElementById("email"));
        if(!eml)
            strOut += "E-Mail format is not valid \n";
    }
    if (trim(document.getElementById("c_email").value) == "") {
        strOut += "Confirm E-Mail is not valid \n";
    }
    if (trim(document.getElementById("c_email").value) != "") {
        var eml = checkEmail(document.getElementById("c_email"));
        if(!eml)
            strOut += "Confirm E-Mail format is not valid \n";
    }
    if ( trim(document.getElementById("email").value) != "" && trim(document.getElementById("c_email").value) != "" && trim(document.getElementById("email").value)!=trim(document.getElementById("c_email").value)) {
	strOut += "EMail and confirm EMail mismatch \n";
    }
    if (document.getElementById("participant1").checked || document.getElementById("participant2").checked || document.getElementById("participant3").checked) {
	//Ok non famu nenti
    } else {
        strOut += "Select partecipant type \n";
    }
    if (document.getElementById("companion").checked) {
	if (trim(document.getElementById("acc_nome").value) == "") {
	    strOut += "Accompanying persons first name is not valid \n";
	}
	if (trim(document.getElementById("acc_cognome").value) == "") {
	    strOut += "Accompanying persons last name is not valid \n";
	}
    } else if (!document.getElementById("companion").checked) {
	if (trim(document.getElementById("acc_nome").value) != "" || trim(document.getElementById("acc_cognome").value) != "") {
	    strOut += "You must select Accompanying persons checkbox\n";
	}
    }
    if (!document.getElementById("companion").checked && document.getElementById("dinner2").checked){
	strOut += "Accompanying persons dinner is selected and accompanying persons is not selected \n";
    }
    if (!document.getElementById("companion").checked && document.getElementById("excursion2").checked){
	strOut += "Accompanying persons excursion is selected and accompanying persons is not selected \n";
    }
      
 if (strOut=="")
        document.forms[1].submit();
    else
        alert(strOut);        
}

function checkEmail(email) {
    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;
}