//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();
	}
}

//EURO CONVERTITORE (Lire -> Euro)///////////////////////////////////////////////////////////////
function convLireEuro(fieldValue, fieldValue1)
{
	var icount;
	var charTemp = '';
	var charTemp1 = '';
	var charTemp2 = '';
	var result;
	var start= 'no';
	var icount1 = 0;
	var icount2 = 0;
	var pLire = fieldValue.value;
	if (pLire == '')
	{
		window.alert('attenzione il campo "prezzo in Lire" è obbligatorio');
		fieldValue.focus();
		return;
	}
	for (icount = 0; icount < pLire.length; icount++)
	{
		if (pLire.charAt(icount) != '.')
		{
			charTemp += pLire.charAt(icount);	
		}
	}
	if (isNaN(charTemp))
	{
		window.alert('Attenzione non hai scritto una cifra valida');
		fieldValue.focus();
		return;
	}
	fieldValue.value = charTemp;
	result = charTemp / 1936.27;
	result = '' + result;
	charTemp = '';
	for (icount = 0; icount < 15; icount++)
	{
		if (result.charAt(icount) == '.')
		{
			start = 'yes';
			charTemp += result.charAt(icount);
		}
		else
		{
			if (start == 'yes')
			{
				if (icount1 == 2)
					break;
				icount1++
			}
			charTemp += result.charAt(icount);
		}			
	}
	for (icount = 0; icount < charTemp.length; icount++)
	{
		if (charTemp.charAt(icount) == '.')
		{
			icount1 = 1;
			charTemp2 += ',';
		}
		else
			charTemp2 += charTemp.charAt(icount);
		
	}
	if (icount1 != 1)
		charTemp2 += ',00';
		
	if (charTemp2.length >= 7)
	{
		result = (charTemp2.length - 3) % 3;
		icount1 = 0;
		for(icount = 0; icount < charTemp2.length; icount++)
		{
			if ((charTemp2.charAt(icount) != ',') && (icount2 == 0))
			{
				if (result > 0)
				{
					if (icount == result)
					{
						charTemp1 += '.';
						result = 0;
						icount1 = 1;
					}
						charTemp1 += charTemp2.charAt(icount);
				}
				else
				{	
					
					if (icount1 == 3)
					{
						charTemp1 += '.';
						icount1 = 0;
					}
						icount1++
						charTemp1 += charTemp2.charAt(icount);	
				}
			}
			else
			{
				icount2 = 1;
				charTemp1 += charTemp2.charAt(icount);
			}
				
		}
	}
	else
		charTemp1 = charTemp2;
	
	
	fieldValue1.value = charTemp1;
	charTemp1 = ''
	charTemp = fieldValue.value + '';
	if (charTemp.length >= 4)
	{
		result = charTemp.length % 3;
		icount1 = 0;
		for(icount = 0; icount < charTemp.length; icount++)
		{
			if (result > 0)
			{
				if (icount == result)
				{
					charTemp1 += '.';
					result = 0;
					icount1 = 1;
				}
					charTemp1 += charTemp.charAt(icount);
			}
			else
			{	
				
				if (icount1 == 3)
				{
					charTemp1 += '.';
					icount1 = 0;
				}
					icount1++
					charTemp1 += charTemp.charAt(icount);	
			}
		}
	}
	else
		charTemp1 = fieldValue.value + '';
	fieldValue.value = charTemp1;
}

//EURO CONVERTITORE (Euro -> Lire)///////////////////////////////////////////////////////////////
function convEuroLire(fieldValue, fieldValue1)
{
	var icount;
	var charTemp = '';
	var charTemp1 = '';
	var result;
	var start= 'no';
	var icount1 = 0;
	var icount2 = 0;
	var pEuro = fieldValue.value;
	if (pEuro == '')
	{
		window.alert('Attenzione il campo "prezzo in Euro" è obbligatorio');
		fieldValue.focus();
		return;
	}
	for (icount = 0; icount < pEuro.length; icount++)
	{
		if (pEuro.charAt(icount) == ',')
		{
			charTemp += '.'
		}
		else
			charTemp += pEuro.charAt(icount);
	}
	if (isNaN(charTemp))
	{
		window.alert('Attenzione non hai scritto una cifra valida, usa uno di questi formati 1500.23 - 1500,23');
		fieldValue.focus();
		return;
	}
	result = charTemp * 1936.27;
	pEuro = charTemp;
	result = '' + result;
	charTemp = '';
	for (icount = 0; icount < 15; icount++)
	{
		if (result.charAt(icount) != '.')
		{
			charTemp += result.charAt(icount);
		}
		else
			break;					
	}
	if (charTemp.length >= 4)
	{
		result = charTemp.length % 3;
		icount1 = 0;
		for(icount = 0; icount < charTemp.length; icount++)
		{
			if (result > 0)
			{
				if (icount == result)
				{
					charTemp1 += '.';
					result = 0;
					icount1 = 1;
				}
					charTemp1 += charTemp.charAt(icount);
			}
			else
			{	
				
				if (icount1 == 3)
				{
					charTemp1 += '.';
					icount1 = 0;
				}
					icount1++;
					charTemp1 += charTemp.charAt(icount);	
			}
		}
	}
	else
		charTemp1 = charTemp;
	fieldValue1.value = charTemp1;
	charTemp1 = '';
	charTemp2 = '';
	
	for (icount = 0; icount < pEuro.length; icount++)
	{
		if (pEuro.charAt(icount) == '.')
		{
			icount1 = 1;
			charTemp2 += ',';
		}
		else
			charTemp2 += pEuro.charAt(icount);
		
	}
	if (icount1 != 1)
		charTemp2 += ',00';
		
	if (charTemp2.length >= 7)
	{
		
		result = (charTemp2.length - 3) % 3;
		icount1 = 0;
		for(icount = 0; icount < charTemp2.length; icount++)
		{
			if ((charTemp2.charAt(icount) != ',') && (icount2 == 0))
			{
				if (result > 0)
				{
					if (icount == result)
					{
						charTemp1 += '.';
						result = 0;
						icount1 = 1;
					}
						charTemp1 += charTemp2.charAt(icount);
				}
				else
				{	
					
					if (icount1 == 3)
					{
						charTemp1 += '.';
						icount1 = 0;
					}
						icount1++
						charTemp1 += charTemp2.charAt(icount);	
				}
			}
			else
			{
				icount2 = 1;
				charTemp1 += charTemp2.charAt(icount);
			}
				
		}
	}
	else
		charTemp1 = charTemp2;
	
	
	fieldValue.value = charTemp1;
	
}

//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 <B> ----> code = 'B'
//per il tag <div align="center"> ---> 'DIV' 
//Per il TAG <FONT> si utilizza la funzione AddColorCode()
//NEGLI ALTRI CASI SI INSERISCE IL COLLEGAMENTO AL VIDEO ASSOCIATO ALL'ARTICOLO
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 al sito?")){
							sString= '<a href="' + insertCode + '" target="_blank">' + 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 <B>
	if (code == "B") {
		formatText(sForm, '<b>', '</b>');	
	}
	//Per il TAG <I>
	if (code == "I") {
		formatText(sForm, '<i>', '</i>');	
	}
	//Per il TAG <DIV align="right">
	if (code == "DIVR") {
		formatText(sForm, '<p class=FONT12TIT align=right>', '</p>');	
	}
	//Per il TAG <DIV align="center">
	if (code == "DIV") {
		formatText(sForm, '<div align=center>', '</div>');	
	}
	
	//Per il collegamento al video degli articoli
	if (!isNaN(code) && (code > 1000)) {	
	
		sString = '<a href="http://www.alghero.tv/video.asp?id='+ code + '">Guarda il video</a>';
		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();
}


//RESTITUISCE IL VALORE NUMERICO IN DUE CIFRE (es 1 --> 01)
function PadWithZero(val) 
{
	if (parseInt(val) < 10)
	{
	  return "0" + val;
	}
	return val;
 }



var http_request1 = "";		
var response = false;

//FUNZIONE PRE CREARE IL BOX AJAX PER LE ULTIME NOTIZIE
function NewsBox(url) {
				
	if (window.XMLHttpRequest) {
		http_request1 = new XMLHttpRequest();
	} else if(window.ActiveXObject) {
		http_request1 = new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		alert("Attenzione si è verificato un problema con l\' oggetto XMLHTTP!");
		return false;
	}
	
	http_request1.open('GET', url, true);

	http_request1.setRequestHeader("Content-Type", "text/plain; charset=ISO-8859-1");
	http_request1.setRequestHeader("Charset","ISO-8859-1");
	http_request1.setRequestHeader("Encoding","ISO-8859-1");

	http_request1.send(null);
	
	http_request1.onreadystatechange = function() {
		
		if (http_request1.readyState == 4 && http_request1.status == 200) {	
					
			if(http_request1.responseXML){
					
				response = true;
				ScrollBox("");
										
			}	
		}
		else
		{
			document.getElementById("titolo1").innerHTML = "<img src='http://www.alghero.tv/immagini/loading.gif'>";
			document.getElementById("titolo2").innerHTML = "<img src='http://www.alghero.tv/immagini/loading.gif'>";
		}	
	
	};	
}

var position = 0;
var temporizza = 0;

//FUNZIONE PER PERMETTERE LO SCORRIMENTEO AVANTI/INDIETRO DELLE ULTIME NOTIZIE
function ScrollBox(dir) {
	
	clearTimeout(temporizza); 
	
	if (response == true){
				
		var items = http_request1.responseXML.getElementsByTagName("item");
											
		if (dir == "next") {
			position ++;						
			if (position >= items.length){
				position = 0;
			}						
		}					
		else if (dir == "prev")	{
			position = position - 3;
			if (position < 0)	{
				position = items.length - 2;						
			}													
		}
		else{
			position = 0;
		}
		
		var link = items[position].getElementsByTagName("link").item(0).firstChild.data;
		var title = items[position].getElementsByTagName("title").item(0).firstChild.data;
		var description = items[position].getElementsByTagName("description").item(0).firstChild.data;
		var category = items[position].getElementsByTagName("category").item(0).firstChild.data;
		var pubDate = items[position].getElementsByTagName("pubDate").item(0).firstChild.data;
		var data = new Date(pubDate);
		
		document.getElementById("titolo1").innerHTML = "<a onmouseover=\"this.style.textDecoration='underline'\" onmouseout=\"this.style.textDecoration='none'\" style=\"color:#000000; text-decoration:none\" href=\""+ link +"\">"+ title +"</a>"; 
		document.getElementById("description1").innerHTML = description;
		document.getElementById("category1").innerHTML = (category).toUpperCase();
		document.getElementById("date1").innerHTML = PadWithZero(data.getDate()) + "-" + PadWithZero(data.getMonth() + 1) + "-" + data.getFullYear();
							
		position ++;

		link = items[position].getElementsByTagName("link").item(0).firstChild.data;
		title = items[position].getElementsByTagName("title").item(0).firstChild.data;
		description = items[position].getElementsByTagName("description").item(0).firstChild.data;
		category = items[position].getElementsByTagName("category").item(0).firstChild.data;
		pubDate = items[position].getElementsByTagName("pubDate").item(0).firstChild.data;
		data = new Date(pubDate);
							
		document.getElementById("titolo2").innerHTML = "<a onmouseover=\"this.style.textDecoration='underline'\" onmouseout=\"this.style.textDecoration='none'\" style=\"color:#000000; text-decoration:none\" href=\""+ link +"\">"+ title +"</a>"; 
		document.getElementById("description2").innerHTML = description;
		document.getElementById("category2").innerHTML = (category).toUpperCase();
		document.getElementById("date2").innerHTML = PadWithZero(data.getDate()) + "-" + PadWithZero(data.getMonth() + 1) + "-" + data.getFullYear();			
		
		temporizza = setTimeout('ScrollBox("next")',10000);
	}									
}
		
//FUNZIONE PER CREARE IL BOX DELE NOTIZIE PIU' LETTE
function Letti(page, datepart) 
{
	if (window.XMLHttpRequest) {
		http_request = new XMLHttpRequest();
	} else if(window.ActiveXObject) {
		http_request = new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		return false;
	}
	
	var url= page + "?datepart="+ datepart;
	url= url + "&sid="+Math.random();
				
	http_request.open('GET', url, true);	
	
	http_request.setRequestHeader("Content-Type", "text/plain; charset=ISO-8859-1");
	http_request.setRequestHeader("Charset","ISO-8859-1");
	http_request.setRequestHeader("Encoding","ISO-8859-1");
	
	http_request.send(null);
	
	if (datepart == "w") {
		
		document.getElementById('week').style.border = "solid black 1px";
		document.getElementById('month').style.border = "solid black 0px";
		document.getElementById('all').style.border = "solid black 0px";
	}
	else if (datepart == "m") {
		
		document.getElementById('week').style.border = "solid black 0px";
		document.getElementById('month').style.border = "solid black 1px";
		document.getElementById('all').style.border = "solid black 0px";
		
	}
	else if (datepart == "a") {
		
		document.getElementById('week').style.border = "solid black 0px";
		document.getElementById('month').style.border = "solid black 0px";
		document.getElementById('all').style.border = "solid black 1px";
		
	}
	else{
		
		document.getElementById('week').style.border = "solid black 0px";
		document.getElementById('month').style.border = "solid black 0px";
		document.getElementById('all').style.border = "solid black 0px";
	
	}
	
	
	http_request.onreadystatechange = function() {
		
		if (http_request.readyState == 4 && http_request.status == 200) {	

			if(http_request.responseText != ""){
								
				document.getElementById('letti').innerHTML=http_request.responseText;	
				if (navigator.appName == "Netscape"){
					document.getElementById('letti').innerHTML = document.getElementById('letti').innerHTML + "<br /><br /><br /><br /><br /><br /><br />";
				}
										
			}	
			else {
								
				document.getElementById('letti').innerHTML= "Servizio momentaneamente non disponibile";
			
			}
									
		}
		else
		{
			document.getElementById('letti').innerHTML= "<img src='http://www.alghero.tv/immagini/loading.gif'>";
		}	
				
	};
}

//funzione per creare il campo per i suggerimenti
function Galleria(div, page, str) 
{		
	if (window.XMLHttpRequest) {
		http_request = new XMLHttpRequest();
	} else if(window.ActiveXObject) {
		http_request = new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		return false;
	}
	
	var url= page +"?q="+ str;
	url=url+"&sid="+Math.random();
				
	http_request.open('GET', url, true);						
	http_request.send(null);
	
	http_request.onreadystatechange = function() {
		
				
	if (http_request.readyState == 4 && http_request.status == 200) {	
								
		var xmldoc = http_request.responseXML;
		var images = xmldoc.getElementsByTagName("image");					

			
		var html= "<br />Risultati ricerca immagini in galleria:<br />"
		html += "<select name='cbGalleria' class='inputbox' size='10' onchange='DisplayImageGallery();' multiple='multiple'>";
		
		for (var i=0; i<images.length; i++) 
		{
			
			html += "<option value='"+ images.item(i).selectSingleNode("id").text + "'>" + images.item(i).selectSingleNode("foto").text + "</option>";					
		}				
		
		html += "</select>&nbsp;"
		
		html += "<input class='button' type='button' value='+' onclick='addSelectedToList()' title='Aggiungi'/>"
		
		div.innerHTML = html;
											
	}
	else
	{
		div.style.visibility="visible";
		div.innerHTML= "<img src='http://www.alghero.tv/immagini/loading.gif'>";
	}	

		
	};
}
//////////////////////////////////////////////////////////////////////////////////