// JavaScript Document
if(navigator.userAgent.toUpperCase().indexOf('FIREFOX') == -1)
{
	var nav4 = window.event ? true : false;
}
else
{
	var nav4 = window.Event ? true : false;
}
/*Funzione che permette solo l'inserimento del caratteri numerici all'interno di una text box*/
function soloNumeri(evt) {
	try
	{	
		var key = nav4 ? evt.which : evt.keyCode;
		return (key < 13 || (key >= 48 && key <= 57))
	}
	catch (e)
	{
	}
}
/////////////////////////////
// PERMETTE LETTERE E NUMERI //
/////////////////////////////
function AceptNumLett(evt){
	var key = nav4 ? evt.which : evt.keyCode; 
	return (key <= 13 || (key >= 65 && key <= 90) || (key >= 97 && key <= 122) || (key >= 48 && key <= 57)); 
} 

/////////////////////////////
// PERMETTE LETTERE E NUMERI + " "//
/////////////////////////////
function AceptNumLettSpazio(evt){
	var key = nav4 ? evt.which : evt.keyCode; 
	return (key <= 13 || key == 32 || (key >= 65 && key <= 90) || (key >= 97 && key <= 122) || (key >= 48 && key <= 57)); 
}

//////////////////////
//Desabilita l'invio//
//////////////////////
function noInvio (evt) {	
	var key = nav4 ? evt.which : evt.keyCode;	
	return (key != 13 );
}
 


var myhttp = null;
var myRNDM = parseInt(Math.random()*999999);
var DivContenitore;
var boosostituzione;
var dasostituire;

/*
La funzione getByAjax, se ne ocupa di fare una riquiesta http tramite java
i parametri in input sono
MyUrl	-	Url di richiesta
DivContenitoreAjax	-	Nome del div dove sara inserito il contenuto
datidasostituire	-	nel caso che questa variabile si diversa da vuoto ('') saranno sostituiti
	i valori alla sinistra dei due punti (:) con il valore alla destra dei due punti (:)
	La formatazione di questo parametro e: [VALORE ORIGINALE]:[NUOVO VALORE][;][VALORE ORIGINALE]:[NUOVO VALORE]
	NB. L'ULTIMO DI QUESTI VALORI "NON DEVE PORTARE" IL PUNTO E VIRGOGA (;)
*/

function getByAjax(MyUrl, DivContenitoreAjax, datidasostituire)
{
	if(myhttp == null)
	{
		myhttp = getXMLHTTPRequest()
		if (MyUrl.indexOf('?') != -1)
		{
			modUrl = MyUrl + '&A=' + myRNDM
		}
		else
		{
			modUrl = MyUrl + '?A=' + myRNDM
		}
		myhttp.open ("GET", modUrl , true);
	
		DivContenitore = DivContenitoreAjax;
		myhttp.onreadystatechange = useHttpResponse;
		if (datidasostituire != null && datidasostituire != '')
		{
			boosostituzione = true;
			dasostituire = datidasostituire;
		} 
		else
		{
			boosostituzione = false;
			dasostituire = '';
		}
		myhttp.send(null);
	}
	else
	{
		setTimeout(
				   function()
				   {
					   getByAjax(MyUrl, DivContenitoreAjax, datidasostituire);
					   }
				   ,500);
	}
}


function getXMLHTTPRequest()
{
	var req = false;
	try 
	{
		req = new XMLHttpRequest();  // ALtri Browser come Firefox
	}
	
	catch(err1)
	{
		try
			{
				req = new ActiveXObject("Msxml2.XMLHTTP"); // vecche I.E.
			}	
		catch(err2)
		{
			try
			{
				req = new ActiveXObject("Microsoft.XMLHTTP"); // altre I.E.
			}
			catch(err3)
			{
				req = false;
			}
		}	
	}
	return req;
}
	
function useHttpResponse () 
{ 
//sono interessato al valore 4 di readyState ovvero caricato
	if (myhttp.readyState == 4) 
	{
		if (myhttp.status == 200) {
			//è OK per cui lancio qualcosa
		
			//recupero le informazioni sottoforma di stringa
			var mytext= myhttp.responseText;
			//Tratto la Stringa
			
			if (boosostituzione == true && dasostituire != '')
			{
				//Prendo tutti i parametri e converto in un array
				arrparametri = '';
				arrparametri = dasostituire.split(';');
				//percorrotutto l'array
				for (posarrasost = 0; posarrasost  < arrparametri.length; posarrasost ++)
				{
					/*Ricavo la posizione dei due punti*/
					pospunti = arrparametri[posarrasost].indexOf(':');
					/*Ricavo il valore originale*/
					valoreoriginale = arrparametri[posarrasost].substring(0, pospunti)
					/*Ricavo il nuovo valore */
					valorenuovo = arrparametri[posarrasost].substring(pospunti+1);
					intIndexOfMatch = -1
					intIndexOfMatch = mytext.indexOf(valoreoriginale);
					while (intIndexOfMatch > 0){
						// Relace out the current instance.
						mytext = mytext.replace(valoreoriginale, valorenuovo)
						// Get the index of any next matching substring.
						intIndexOfMatch = mytext.indexOf(valoreoriginale);
					}
				}
			}
			//alert(mytext);
			document.getElementById(DivContenitore).innerHTML = mytext;
			myhttp = null;
		}
		else
		{
			//Messaggio di errore
			document.getElementById(DivContenitore).innerHTML = "Si è verificato un errore: " + myhttp.statusText;
			myhttp = null;
		}
	}else
	{ 
		// cioè stato è diverso da 4 visualizzo loader
		document.getElementById(DivContenitore).innerHTML = '<div class="carica" align=center><img src="/Admin/Img/ajax-loader.gif"/></div>'
	}
}
	
function compilasotto(obj)
{
	if (document.getElementById('tbElencoSottoCategorie') != null)
	{
		if (obj.checked)
		{
			document.getElementById('tbElencoSottoCategorie').value += obj.value + "; ";
			enabdisabselctcat()

		}
		else
		{
			document.getElementById('tbElencoSottoCategorie').value = document.getElementById('tbElencoSottoCategorie').value.replace(obj.value + '; ', '');
			enabdisabselctcat()
		}
	}
	else
	{
		alert('Manca il campo tbElencoSottoCategorie');
	}
}

function enabdisabselctcat()
{
	if (document.getElementById('tbElencoSottoCategorie'))
	{
		if (document.getElementById('tbElencoSottoCategorie').value != '')
		{
			document.getElementById('Categoria').readonly = true;
		}
		else
		{
			document.getElementById('Categoria').readonly = false;
		}
	}
}

function addLoadEvent(func, param) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') 
	{
		window.onload = function (){func(param)};
	}
	else
	{
		window.onload = function() {
		if (oldonload)
		{
			oldonload();
		}
			func(param);
		}
	}
}


////////////////////////////////////////////////////////////////////////
// FUNZIONE che solo permete l'inseriemnto di una data in un text box //
// UTILIZZO: onKeyUp = "this.value=formateadata(this.value);"        //
//
// 1.0.1 - #002 - 04/05/2005 - sostutito il nome della variabile di nome long in longvar: long generava un errore in Mozilla
//
////////////////////////////////////////////////////////////////////////

function IsNumeric(valor) 
{ 
var log=valor.length; var sw="S"; 
for (x=0; x<log; x++) 
{ v1=valor.substr(x,1); 
v2 = parseInt(v1); 
//Compruebo si es un valor numérico 
if (isNaN(v2)) { sw= "N";} 
} 
if (sw=="S") {return true;} else {return false; } 
} 

var primerslap=false; 
var segundoslap=false; 
function formateadata(fecha) 
{ 
			var longvar = fecha.length; 
			var dia; 
			var mes; 
			var ano; 
			
			if ((longvar>=2) && (primerslap==false)) { dia=fecha.substr(0,2); 
			if ((IsNumeric(dia)==true) && (dia<=31) && (dia!="00")) { fecha=fecha.substr(0,2)+"/"+fecha.substr(3,7); primerslap=true; } 
			else { fecha=""; primerslap=false;} 
			} 
			else 
			{ dia=fecha.substr(0,1); 
			if (IsNumeric(dia)==false) 
			{fecha="";} 
			if ((longvar<=2) && (primerslap=true)) {fecha=fecha.substr(0,1); primerslap=false; } 
			} 
			if ((longvar>=5) && (segundoslap==false)) 
			{ mes=fecha.substr(3,2); 
			if ((IsNumeric(mes)==true) &&(mes<=12) && (mes!="00")) { fecha=fecha.substr(0,5)+"/"+fecha.substr(6,4); segundoslap=true; } 
			else { fecha=fecha.substr(0,3);; segundoslap=false;} 
			} 
			else { if ((longvar<=5) && (segundoslap=true)) { fecha=fecha.substr(0,4); segundoslap=false; } } 
			if (longvar>=7) 
			{ ano=fecha.substr(6,4); 
			if (IsNumeric(ano)==false) { fecha=fecha.substr(0,6); } 
			else { if (longvar==10){ if ((ano==0) || (ano<1900) || (ano>2100)) { fecha=fecha.substr(0,6); } } } 
			} 
			
			if (longvar>=10) 
			{ 
			fecha=fecha.substr(0,10); 
			dia=fecha.substr(0,2); 
			mes=fecha.substr(3,2); 
			ano=fecha.substr(6,4); 
			// Año no viciesto y es febrero y el dia es mayor a 28 
			if ( (ano%4 != 0) && (mes ==02) && (dia > 28) ) { fecha=fecha.substr(0,2)+"/"; } 
			} 
			return (fecha); 
} 

///////////////////////////////////////////////////////////////////
// FUNZIONE CHE REEMPLAZA DELLE STRINGHE 						//
// UTTIOLIZZO: replacestr(stringa,da_sostituire,per_sostituto)	//
// Diego J. M. Gonzalez											//
///////////////////////////////////////////////////////////////////
function replacestr(stringa,da_sostituire,per_sostituto) {
	out = da_sostituire; // replace this
	add = per_sostituto; // with this
	temp = '' + stringa; // temporary holder
	while (temp.indexOf(out)>-1) {
	pos= temp.indexOf(out);
	temp = "" + (temp.substring(0, pos) + add +
	temp.substring((pos + out.length), temp.length));
	}
	return(temp);
}
