
//Function to count the number of characters in the description text box
function DescriptionCharCount() {
	document.addsito.countcharacters.value = document.addsito.descrizione.value.length;
}

//Function to check form is filled in correctly before submitting
function CheckForm () {

	//Intialise variables
	var errorMsg = "";
	var errorMsgLong = "";

		
	//Check for a URL address
	if ((document.addsito.url.value == "http://")||(document.addsito.url.value == "")){
		errorMsg += "\n\tURL \t\t- Inserire una URL valida";
	}
	
	//Check for a URL title
	if (document.addsito.titolo.value == ""){
		errorMsg += "\n\tTitolo \t\t- Inserire il titolo del sito";
	}
	
	
	//Check for a description
	if (document.addsito.descrizione.value == ""){
		errorMsg += "\n\tDescrizione \t- Inserire la descrizione del sito";
	}	
	
	//Check the description length before submiting the form	
	if (document.addsito.descrizione.value.length > 250){
		errorMsgLong += "\n- La descrizione è di " + document.addsito.descrizione.value.length + " caratteri, sono ammessi solo 250 caratteri.";
	}
	
	//Check the word length before submitting
	words = document.addsito.descrizione.value.split(' ');
	for (var loop = 0; loop <= words.length - 1; ++loop){
		if (words[loop].length > 25){
		errorMsgLong += "\n- Una parola nella descrizione contiene " + words[loop].length + " caratteri, accorciare la parola con massimo 25 caratteri.";
		}	
	}	
	
	//Check for Nome
	if (document.addsito.nome.value == ""){
		errorMsg += "\n\tNome \t\t- Inserire il proprio nome";
	}	
	
	//Check for a e-mail address
	if (document.addsito.e_mail.value == ""){
		errorMsg += "\n\tE-mail \t\t- Inserire una E-mail valida";
	}
	
	//Check for HTML tags before submitting the form	
	for (var count = 0; count <= 3; ++count){
		if ((document.addsito.elements[count].value.indexOf("<", 0) >= 0) && (document.addsito.elements[count].value.indexOf(">", 0) >= 0)){
			errorMsgLong += "\n- HTML tags non sono accettati, eliminarli per procedere.";
		}			
	}
	
	//If there is aproblem with the form then display an error
	if ((errorMsg != "")||(errorMsgLong != "")){
		msg = "___________________________________________________________________\n\n";
		msg += "I dati inseriti non sono validi.\n";
		msg += "Controllare i dati per andare avanti.\n";
		msg += "___________________________________________________________________\n\n";
		msg += "I seguenti campi devono essere corretti: -\n";
		
		errorMsg += alert(msg + errorMsg + "\n" + errorMsgLong);
		return false;
	}
	
	return true;
}

