//CONTROLLO SE NUMERO CON ALERT/////////////////////////////////////////////////////////////
function checkIsNaN(num, msg)
{
	var charTemp = '';
	var	icount;
	var valueTmp = num.value;
	
	for (icount = 0; icount < valueTmp.length; icount++)
	{
		if ((valueTmp.charAt(icount) != '.') && (valueTmp.charAt(icount) != ','))
		{
			charTemp += valueTmp.charAt(icount);	
		}
	}
	
	if (isNaN(charTemp))
	{
		window.alert('Il valore inserito nel campo "' + msg + '" deve essere un numero');
		num.focus();
	}
}

//VERIFICA CAMPI OBBLIGATORI IN MODULI CON UN SOLO PULSANTE SUBMIT////////////////////
function controlloCampi(nomeform, testo, valore)  
{
	if (nomeform.value == valore)
	{
		window.alert('Attenzione il campo "'+ testo +'" è obbligatorio');
		nomeform.focus();
		return false;
	}
	return true;
}

//VERIFICA CAMPI OBBLIGATORI IN MODULI CON PIU' BUTTONS///////////////////////////////
function controlloCampiButton(nomeform, testo, nomeformSubmit, formAzione)   
{
	nomeformSubmit.azione.value = formAzione
	if (nomeform.value == '')
	{
		window.alert('Attenzione il campo "'+ testo +'" è obbligatorio');
		return false;
	}
	nomeformSubmit.submit();
}

//VERIFICA FORMATO E-MAIL/////////////////////////////////////////////////////////////
//'obbligatorio' è un valore booleano che indica se il cap è obbligatorio
function controlloEmail(nomeform, obbligatorio) 
{
	var sEmail= nomeform.value
	
	if (obbligatorio == true && sEmail.length==0)
	{
			alert ('Attenzione il campo "E-mail" è obbligatorio');
			nomeform.focus();
			return false;
	
	}
	
	if ((sEmail.indexOf("@")== -1 || sEmail.indexOf(".")== -1) && sEmail != "") 
	{
		window.alert ('Il campo "E-mail" è stato compilato in modo errato');
		nomeform.focus();
		return false;
						
	}
	return true;
}

//VERIFICA FORMATO CAP/////////////////////////////////////////////////////////////
//'obbligatorio' è un valore booleano che indica se il cap è obbligatorio
function controlloCap(nomeform, obbligatorio) 
{
	var sCap= nomeform.value
	
	if (obbligatorio == true && sCap.length==0)
	{
			alert ('Attenzione il campo "Cap" è obbligatorio');
			nomeform.focus();
			return false;
	
	}
	
	if ((sCap.length > 0 && sCap.length <5) || isNaN(sCap))
		{
			alert ('Il campo "Cap" è stato compilato in modo errato');
			nomeform.focus();
			return false;
		}
	return true;
}

//CONFERMA CANCELLAZIONE DA FORM//////////////////////////////////////////////////////
function controlloCancellazione(nomeform, testo, nomeformSubmit, formAzione) 
{
	nomeformSubmit.azione.value = formAzione
	if (confirm('Attenzione hai scelto di cancellare "'+ testo +'"'))
	{
		nomeformSubmit.submit();
	}
}

//CONFERMA CANCELLAZIONE DA BUTTON////////////////////////////////////////////////////
function controlloCancButton(actionRedirect, testo)
{
	if (confirm('Attenzione hai scelto di cancellare "'+ testo +'"'))
	{
		document.location.href = actionRedirect;
	}
}

//ROLLOVER SULLE RIGHE ON CHECKBOX////////////////////////////////////////////////////
function changeColor(Rows) 
{
	if (Rows.checked)
	{
		while (Rows.tagName != 'TR')
		{
			Rows = Rows.parentNode;
		}
	Rows.className = 'H';
	}
	else
	{
		while (Rows.tagName != 'TR')
		{
			Rows = Rows.parentNode;
		}
		Rows.className = "";
	}
}

//ROLLOVER SULLE RIGHE ON MOUSEOVER///////////////////////////////////////////////////
function changeColor1(Rows, set) 
{
				
	if ((set == 1) || (set == 3))
	{
		while (Rows.tagName != 'TR')
		{
			Rows = Rows.parentNode;
		}
		Rows.className = 'H';
	}
	else if (set == 2)
	{
		while (Rows.tagName != 'TR')
		{
			Rows = Rows.parentNode;
		}
		Rows.className = "";
	}
}

//FUZIONE  CHE INSERISCE I TAG HTML IN UN CAMPO DI TESTO /////////////////////////
//per gli URL --> code = 'URL'
//per le e-mail --> code = '@'
//per il tag <br /> --> code = 'BR'
//per le immagini --> code = 'IMG'
//per il tag strong ----> code = 'STRONG'
//Per il TAG <FONT> si utilizza la funzione AddColorCode()
function AddColorCode(code, sForm) {
	sString = '<font color="' + code + '">'
	formatText(sForm, sString, '</font>');	
}

//Per gli altri TAG
function AddCode(code, sForm) {

	//Per i link
	if (code == "URL") {
		insertText = prompt("Inserisci il testo del collegamento:", "");
							
			if ((insertText != null) && (insertText != "") && (code == "URL")){
				insertCode = prompt("Inserisci l'indirizzo del sito:", "http://");
									
					if ((insertCode != null) && (insertCode != "") && (insertCode != "http://")){
						if (confirm("Il link è esterno?")){					
							sString= '<a href="' + insertCode + '">' + insertText + '</a>';
						}
						else {
							sString= '<a href="' + insertCode + '">' + insertText + '</a>';
						}
						formatText(sForm, '', sString);
					}
			}
	}
	
	//Per le e-mail
	if (code == "@") {
		insertCode = prompt("Inserisci l' e-mail:", "");
			
		if ((insertCode != null) && (insertCode != "")){					
			sString = '<a href="mailto:' + insertCode + '">' + insertCode + '</a>';
			formatText(sForm, '', sString);
		}
		
	}
	
	//Per il TAG <BR />
	if ((code == "BR") || (code == 13)) {
		formatText(sForm, '', '<br />');	
	}

	//Per il TAG <IMG>
	if (code == "IMG") {	
		insertCode = prompt("Inserisci il nome della foto con l'estensione:", "");
					
			if ((insertCode != null) && (insertCode != "")){					
				sString = '<img src="'+insertCode+'" alt="" />';
				formatText(sForm, '', sString);
			}			
	}
	
	//Per inserire una foto di un prodotto
	if (code == "PRO") {	
		insertCode = prompt("Inserisci il nome della foto del prodotto con l'estensione:", "");
					
			if ((insertCode != null) && (insertCode != "")){					
				sString = '<img src="http://www.kasatua.com/visualizzazione/immagini/'+insertCode+'" alt="" />';
				formatText(sForm, '', sString);
			}			
	}
	
	//Per il TAG <B>
	if (code == "STRONG") {
		formatText(sForm, '<strong>', '</strong>');	
	}
	
	//Per inserire la barra nelle Newsletter
	if (code == "BARRA") {	
		sString = '<br /><br /><br /><img src="http://www.kasatua.com/images/barra_news.gif" alt="" /><br /><br />';
		formatText(sForm, '', sString);
	}			
		
}

//Inserisce i tags specificati all'inizio e alla fine del testo selezionato
function formatText(textEl, tagstart, tagend) {
	if (textEl.createTextRange && textEl.caretPos) {
	var caretPos = textEl.caretPos;
	caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? tagstart + textE1 + tagend : tagstart + textE1 + tagend;
	textEl.focus();		
	}
}

//Ottiene la posizione di una porzione di testo selezionato
function getActiveText(textEl) {
	if (textEl.createTextRange)
	textEl.caretPos = document.selection.createRange().duplicate();
	textE1 = (document.all) ? document.selection.createRange().text : document.getSelection();
}

//Apre una finestra di popup
function openBrWindow(theURL,winName,features) {
	window.open(theURL,winName,features);
}
		
//////////////////////////////////////////////////////////////////////////////////