//*** guestbook - sign_guestbook.asp ***
//*** by Filippo Spadaro www.nienteperniente.it/filippospadaro 

//Fornisce un numero casuale < del numero limit
function randomNumber(limit){
  return Math.floor(Math.random()*limit);
}


//Function to check guestbook form is filled in correctly before submitting
function CheckFormEng (intMsgCharNo, codiceConvalidaGuestbook) {
	//Intialise variables
	var errorMsg = "";
	var errorMsgLong = "";
	//Check for a name
	if (document.frmSignGuestBook.name.value == ""){
		errorMsg += "\n\tName \t\t- Insert your name";
	}
	//Check for a country
	if (document.frmSignGuestBook.citta.value == ""){
		errorMsg += "\n\tTown \t\t- Insert your town";
	}
	//Check for comments
	if (document.frmSignGuestBook.comments.value == ""){
		errorMsg += "\n\tMessage \t\t- Insert your comment to add my Guestbook";
	}
	//Check the description length before submiting the form	
	if (document.frmSignGuestBook.comments.value.length > intMsgCharNo){
		errorMsgLong += "\n- Your comment is too long " + document.frmSignGuestBook.comments.value.length + " caratteri, devi ridurlo a " + intMsgCharNo + " caratteri.";
	}
	//Check the word length before submitting
	words = document.frmSignGuestBook.comments.value.split(' ');
	for (var loop = 0; loop <= words.length - 1; ++loop){
		if (words[loop].length >= 50){
		errorMsgLong += "\n- A world in your comment havs " + words[loop].length + " characters, you must reduce it at least 50 characters.";
		}	
	}	
	//Check for a codice
	if ((document.frmSignGuestBook.codice.value != codiceConvalidaGuestbook)){
		errorMsg += "\n\tCode \t\t- Insert the code correctly";
	}
	//Check for HTML tags before submitting the form	
	/*
	for (var count = 0; count <= 7; ++count){
		if ((document.frmSignGuestBook.elements[count].value.indexOf("<", 0) >= 0) && (document.frmSignGuestBook.elements[count].value.indexOf(">", 0) >= 0)){
			errorMsgLong += "\n- I tag HTML non sono permessi, rimuovi tutti i tag HTML inseriti.";
		}					
	}
	*/
	//If there is aproblem with the form then display an error
	if ((errorMsg != "") || (errorMsgLong != "")){
		msg = "___________________________________________________________________\n\n";
		msg += "Your message was not added for problems with form.\n";
		msg += "Please correct problems and re-send the form. Thanks.\n";
		msg += "___________________________________________________________________\n\n";
		msg += "Following fields are not correct. Errors: \n";
		
		errorMsg += alert(msg + errorMsg + "\n" + errorMsgLong);
		return false;
	}
	return true;
}


//Function to check guestbook form is filled in correctly before submitting
function CheckFormIta (intMsgCharNo, codiceConvalidaGuestbook) {
	//Intialise variables
	var errorMsg = "";
	var errorMsgLong = "";
	//Check for a name
	if (document.frmSignGuestBook.name.value == ""){
		errorMsg += "\n\tNome \t\t- Inserisci il tuo nome";
	}
	//Check for a country
	if (document.frmSignGuestBook.citta.value == ""){
		errorMsg += "\n\tCittà \t\t- Inserisci la tua città";
	}
	//Check for comments
	if (document.frmSignGuestBook.comments.value == ""){
		errorMsg += "\n\tCommento \t- Inserisci un commento da aggiungere al Guestbook";
	}
	//Check the description length before submiting the form	
	if (document.frmSignGuestBook.comments.value.length > intMsgCharNo){
		errorMsgLong += "\n- Il tuo commento è lungo " + document.frmSignGuestBook.comments.value.length + " caratteri, devi ridurlo a " + intMsgCharNo + " caratteri.";
	}
	//Check the word length before submitting
	words = document.frmSignGuestBook.comments.value.split(' ');
	for (var loop = 0; loop <= words.length - 1; ++loop){
		if (words[loop].length >= 50){
		errorMsgLong += "\n- Una parola nel tuo commento contiene " + words[loop].length + " caratteri, devi ridurla a meno di 50 caratteri.";
		}	
	}	
	//Check for a codice
	if ((document.frmSignGuestBook.codice.value != codiceConvalidaGuestbook)){
		errorMsg += "\n\tCodice \t\t- Inserisci correttamente il codice di convalida";
	}
	//Check for HTML tags before submitting the form	
	/*
	for (var count = 0; count <= 7; ++count){
		if ((document.frmSignGuestBook.elements[count].value.indexOf("<", 0) >= 0) && (document.frmSignGuestBook.elements[count].value.indexOf(">", 0) >= 0)){
			errorMsgLong += "\n- I tag HTML non sono permessi, rimuovi tutti i tag HTML inseriti.";
		}					
	}
	*/
	//If there is aproblem with the form then display an error
	if ((errorMsg != "") || (errorMsgLong != "")){
		msg = "___________________________________________________________________\n\n";
		msg += "Il tuo commento non è stato aggiunto a causa di problemi con il form.\n";
		msg += "Per favore correggi i problemi e re-invia il form. Grazie.\n";
		msg += "___________________________________________________________________\n\n";
		msg += "I seguenti campi non sono corretti. Elenco errori: \n";
		
		errorMsg += alert(msg + errorMsg + "\n" + errorMsgLong);
		return false;
	}
	return true;
}


//Function to count the number of characters in the description text box
function DescriptionCharCount() {
	document.frmSignGuestBook.countcharacters.value = document.frmSignGuestBook.comments.value.length;	
}


// Function to add the code for bold italic and underline, to the message
function AddMessageCodeEng(code,promptText, InsertText) {
	if (code != "") {
		insertCode = prompt(promptText + "\n[" + code + "] text [/" + code + "]", InsertText);
			if ((insertCode != null) && (insertCode != "")){
				document.frmSignGuestBook.comments.value += "[" + code + "]" + insertCode + "[/"+ code + "] ";
			}
	}		
	document.frmSignGuestBook.comments.focus();
}


// Function to add the code for bold italic and underline, to the message
function AddMessageCodeIta(code,promptText, InsertText) {
	if (code != "") {
		insertCode = prompt(promptText + "\n[" + code + "] testo [/" + code + "]", InsertText);
			if ((insertCode != null) && (insertCode != "")){
				document.frmSignGuestBook.comments.value += "[" + code + "]" + insertCode + "[/"+ code + "] ";
			}
	}		
	document.frmSignGuestBook.comments.focus();
}

//Function to add the code to the message for the smileys
function AddSmileyIcon(iconCode) {	
		document.frmSignGuestBook.comments.value += iconCode + " ";
		document.frmSignGuestBook.comments.focus();
}

//Function to open pop up window
function openWin(theURL,winName,features) {
  	window.open(theURL,winName,features);
}

//Function to add the code to the message for the smileys
function AddSmileyIcon2(iconCode) {	
	window.opener.document.frmSignGuestBook.comments.value += iconCode + " ";
}

