function isNumeric(sText)
{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
}

function trim(stringa){
    while (stringa.substring(0,1) == ' '){
        stringa = stringa.substring(1, stringa.length);
    }
    while (stringa.substring(stringa.length-1, stringa.length) == ' '){
        stringa = stringa.substring(0,stringa.length-1);
    }
    return stringa;
}

function mysqlDate()
{
	data = new Date();
	ora =data.getHours();
	minuti=data.getMinutes();
	secondi=data.getSeconds();
	//giorno = data.getDayMonth();
	mese = data.getMonth()+1;
	giorno = data.getDate();
	anno = data.getYear();
	if(giorno<10)giorno="0"+giorno;
	if(mese< 10)mese="0"+mese;
	if(minuti< 10)minuti="0"+minuti;
	if(secondi< 10)secondi="0"+secondi;
	if(anno<1900) anno+=1900;
	if(ora<10)ora="0"+ora; 
	mysqlD = anno+"-"+mese+"-"+giorno+" "+ora+":"+minuti+":"+secondi;
	return mysqlD;
}

function stampaData()
{
	data = new Date();
	ora =data.getHours();
	minuti=data.getMinutes();
	secondi=data.getSeconds();
	giorno = data.getDay();
	mese = data.getMonth();
	date= data.getDate();
	year= data.getYear();
	if(minuti< 10)minuti="0"+minuti;
	if(secondi< 10)secondi="0"+secondi;
	if(year<1900)year=year+1900;
	if(ora<10)ora="0"+ora; 
	if(giorno == 0) giorno = " Domenica "; 
	if(giorno == 1) giorno = " Luned\ì ";
	if(giorno == 2) giorno = " Marted\ì "; 
	if(giorno == 3) giorno = " Mercoled\ì "; 
	if(giorno == 4) giorno = " Gioved\ì ";
	if(giorno == 5) giorno = " Venerd\ì ";
	if(giorno == 6) giorno = " Sabato ";
	if(mese == 0) mese = "Gennaio ";
	if(mese ==1) mese = "Febbraio ";
	if(mese ==2) mese = "Marzo ";
	if(mese ==3) mese = "Aprile ";
	if(mese ==4) mese = "Maggio ";
	if(mese ==5) mese = "Giugno ";
	if(mese ==6) mese = "Luglio ";
	if(mese ==7) mese = "Agosto ";
	if(mese ==8) mese = "Settembre ";
	if(mese ==9) mese = "Ottobre ";
	if(mese ==10) mese = "Novembre ";
	if(mese ==11) mese = "Dicembre";
	document.write(giorno+" "+date+" "+mese+" "+year);

}

function validateReg()
{
	var check_1 = validateField('nome',/^[a-zA-Z_.\^]+([a-zA-Z\-_.\^0-9]+)$/);
	var check_2 = validateField('cognome',/^[a-zA-Z_.\^]+([a-zA-Z\-_.\^0-9]+)$/);
	var check_3 = validateField('email',   /^[a-zA-Z_.]+[a-zA-Z\-_.0-9]+@[a-zA-Z\-_0-9]+\.[a-z]{2,4}$/);
	var check_4 = validateField('emailver',/^[a-zA-Z_.]+[a-zA-Z\-_.0-9]+@[a-zA-Z\-_0-9]+\.[a-z]{2,4}$/);
	var check_5 = validateField('password',/^([a-zA-Z\-_.0-9]+){5,8}$/);
	var check_6 = validateField('passwordver',/^([a-zA-Z\-_.0-9]+){5,8}$/);
	
	var check_7 = validateEquals('email','emailver');
	var check_8 = validateEquals('password','passwordver');
	if (check_1 && check_2 && check_3 && check_4 && check_5 && check_6 && check_7 && check_8) document.getElementById('registrazione').submit();
}

function validateField(field, regexp)
{
	var campo = document.getElementById(field).value;
	if (!regexp.test(campo)) document.getElementById('etichetta_'+field).style.color = 'red'; else document.getElementById('etichetta_'+field).style.color = 'darkblue';
	return regexp.test(campo);
}

function controllaEmail(field)
{
	var emailRE = /^[a-zA-Z_.]+[a-zA-Z\-_.0-9]+@[a-zA-Z\-_0-9]+\.[a-z]{2,4}$/;//new RegExp(emailStr,"");
	var campo = document.getElementById(field).value;
	if (emailRE.test(campo)) document.getElementById('richiesta_info').submit(); else document.getElementById('etichetta_email').style.color = 'red';
}

function validateEquals(field1, field2)
{
	var test = document.getElementById(field1).value != document.getElementById(field2).value;
	if (test) document.getElementById('etichetta_'+field2).style.color = 'red';
	return !test;
}

/***********************************************
* Textarea Maxlength script- © Dynamic Drive (www.dynamicdrive.com)
* This notice must stay intact for legal use.
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/

function ismaxlength(obj){
	//if(charCount == undefined) charCount=0;
	//charCount++;
	var mlength = obj.getAttribute ? parseInt(obj.getAttribute("maxlength")) : ""
	if (obj.getAttribute && obj.value.length>mlength) obj.value=obj.value.substring(0,mlength)
	//document.write(charCount);
}

function diventaTifoso(squadra,codice){
	if (confirm("Vuoi davvero diventare tifoso di "+squadra+" ?"))
	{
		//parent.window.location = "index.php?nav=9&men=1";
		parent.window.location.replace("index.php?azione=diventa_tifoso&codicesq="+codice+"&nav=5&men=1");
	}
	else
	{
	};
}

function pagamento(nominativo_utente, codice_utente, codice_offerta){
	if (confirm("Stai per segnare come pagato l'utente "+nominativo_utente+". Questa azione sarà definitiva. Sei sicuro?"))
	{
		location.replace("index.php?azione=pagamento&codiceUt="+codice_utente+"&codiceOff="+codice_offerta+"&nav=6&men=2");
	}
	else
	{
	};
}

function changePage(position, menu)
{
	var a = position==0?document.getElementById('page_upper').value:document.getElementById('page_lower').value;
	location.replace('index.php?azione=match_exact&page='+a+'&nav=3&men='+menu);
}


function transform(tagStrRE, tagType, testo)
{
	//tagType = 1; //simple form [tag]...[/tag] ---> <tag>...</tag>
	//tag = "\\[b\\][^.]+\\[\\/b\\]"; // [b]qualuqunquecosa[/b]
	tagRE = tagType==4? new RegExp(tagStrRE,""):new RegExp(tagStrRE,"i");
	styleDef = false;
	objectDef = false;
	do
	{
		matching = testo.match(tagRE);
		exists = (matching==null)?false:true;
		if (exists)
		{
			matchingLength = matching[0].length;
			openingIndex = matching[0].indexOf("]"); //sotto l'ipotesi della corrispondenza trovata, il carattere ] identifica la fine del tag d'apertutra
			openingTag = matching[0].substr(1,openingIndex-1);//.toLowerCase();
			lowerMatching = tagType!=4?matching[0].toLowerCase():matching[0];
			tagName = tagType!=4?openingTag.split("=")[0].toLowerCase():openingTag.split("=")[0];
			//document.write(tagName);
			closingIndex = lowerMatching.indexOf("[/"+tagName+"]");
			content = matching[0].substr(openingIndex+1, closingIndex-openingIndex-1);
			restStartIndex = closingIndex+1+tagName.length+1+1;
			rest = matching[0].substr(restStartIndex, matchingLength-restStartIndex);
			//document.write(matching[0]+" CONTENT="+content+" EFFTAGNAME="+effectiveTagName+" "+closingIndex+" "+restStartIndex);
			if (tagName == "img")
			{
				tagName = tagName;
				attValue = content;
				attName = "src";
			}
			if (tagName == "youtube")
			{
				objectDef = true;
				tagName = "embed";
				attValue = "http://www.youtube.com/v/"+content;
				attName = "src";
			}	
			if (tagName == "url" || tagName == "email")
			{
				primoSimboloUGUALE_index = openingTag.indexOf("=");
				lunghezzaEffettiva = openingTag.length - (primoSimboloUGUALE_index+1);
				finalPart = openingTag.substr(primoSimboloUGUALE_index+1,lunghezzaEffettiva);
				//splitted = openingTag.split("=");
				//attValue = tagName =="email"?"mailto:"+splitted[1]:splitted[1];
				//document.write(openingTag+"----"+finalPart);
				attValue = tagName =="email"?"mailto:"+finalPart:finalPart;
				tagName = "a";
				attName = "target='_parent' href";
			}
			if (tagName == "color")
			{
				styleDef = true;
				splitted = openingTag.split("=");
				tagName = "span";
				attValue = splitted[1];
				attName = "color";
			}
			if (tagName == "size")
			{
				styleDef = true;
				splitted = openingTag.split("=");
				tagName = "span";
				attValue = splitted[1];
				attName = "font-size";
			}
			
			//if (tagType==4) tagType=3;
			if (tagName == ":)")
			{
				tagName = "img";
				attValue = "grafica/01.png.gif";
				attName = "src";
			}
			else
			if (tagName == ":(")
			{
				tagName = "img";
				attValue = "grafica/02.png.gif";
				attName = "src";
			}
			else
			if (tagName == ":o")
			{
				tagName = "img";
				attValue = "grafica/03.png.gif";
				attName = "src";
			}
			else
			if (tagName == ":p")
			{
				tagName = "img";
				attValue = "grafica/04.png.gif";
				attName = "src";
			}
			else
			if (tagName == ":D")
			{
				tagName = "img";
				attValue = "grafica/05.png.gif";
				attName = "src";
			}
			else
			if (tagName == ":!")
			{
				tagName = "img";
				attValue = "grafica/06.png.gif";
				attName = "src";
			}
			else
			if (tagName == ":O")
			{
				tagName = "img";
				attValue = "grafica/07.png.gif";
				attName = "src";
			}
			else
			if (tagName == "8)")
			{
				tagName = "img";
				attValue = "grafica/08.png.gif";
				attName = "src";
			}
			else
			if (tagName == ";)")
			{
				tagName = "img";
				attValue = "grafica/09.png.gif";
				attName = "src";
			}

			//procedura di trasformazione
			switch (tagType)
			{
				default: //primo tipo
					
						opening = "<"+tagName+">";
						closing = "</"+tagName+">";

						sostituto = opening+content+closing+rest;
						break;
				case 2: // <tag>content</tag> ---> <tag att='content'></tag>
						opening = "<"+tagName+" "+attName+"='"+attValue+"'>";
						closing = "</"+tagName+">";
						if (objectDef)//width='425' height='344'
						{
							sostituto = "<object>"+opening+closing+"</object>"+rest;
						}
						else
						{
							sostituto = opening+closing+rest;
						}
						break;
				case 3: // <tag1=val>content</tag1> ---> <tag2 att='val'>content</tag2>
						if (styleDef)
						{
							opening = "<"+tagName+" style='"+attName+":"+attValue+"'>";
						}
						else
						{
							opening = "<"+tagName+" "+attName+"='"+attValue+"'>";
						}
						closing = "</"+tagName+">";

						sostituto = opening+content+closing+rest;
						//document.write(matching[0]+"<br>---"+content);
						break;
				case 4: // <tag1=val>content</tag1> ---> <tag2 att='val'>content</tag2>
						if (styleDef)
						{
							opening = "<"+tagName+" style='"+attName+":"+attValue+"'>";
						}
						else
						{
							opening = "<"+tagName+" "+attName+"='"+attValue+"'>";
						}
						closing = "</"+tagName+">";

						sostituto = opening+content+closing+rest;
						//document.write(matching[0]+"<br>---"+content);
						break;
			}
			
			
			//sostituzione
			testo = testo.replace(matching[0], sostituto);

		}
	}
	while (exists);
	return testo;
}

function invio(formName, areaName, areaBBName, bbEna)
{
	if (trim(document.getElementById("titolo").value)!="" && trim(document.getElementById(areaName).value)!="")
	{
		var testo = document.getElementById(areaName).value;
		document.getElementById(areaBBName).value = testo;
		//Trasforma tutte le < ... > in stringa vuota
		RE = /<[^\[\]<>]+>/ig;
		testo = testo.replace(RE, "");
		document.getElementById(areaName).value = testo;
		document.getElementById(areaBBName).value = testo;

		if (bbEna)
		{
			testo = transform("\\[b\\][^\\n]+\\[\\/b\\]",1,testo);
			document.getElementById(areaName).value = testo;
			testo = transform("\\[(i|I)\\][^\\n]+\\[\\/(i|I)\\]",1,testo);
			document.getElementById(areaName).value = testo;
			testo = transform("\\[(u|U)\\][^\\n]+\\[\\/(u|U)\\]",1,testo);
			document.getElementById(areaName).value = testo;
			//tolta dopo il +) la stringa \\.[a-zA-Z]{2,5} dalla trasformazione sottostante
			testo = transform("(\\[img\\])(http|https):\\/\\/[a-zA-Z0-9]+([\\-\\.\\_\\/]{1}[~a-zA-Z0-9=&?#%]+)+(\\/)*(\\[\\/img\\])",2,testo);
			document.getElementById(areaName).value = testo;
			
		    testo = transform("(\\[url=(http|https):\\/\\/[a-zA-Z0-9]+([\\-\\.\\_\\/]{1}[~a-zA-Z0-9=&?#%]+)+(\\/)*\\])[^\\n]+(\\[\\/url\\])",3,testo);
			document.getElementById(areaName).value = testo;

			
			testo = transform("(\\[color=(#\\w{6}|red|orange|yellow|lightgreen|green|cyan|blue|darkblue|purple|magenta|pink|darkgray|gray|black |white)\\])[^\\n]+(\\[\\/color\\])",3,testo);
			document.getElementById(areaName).value = testo;

			testo = transform("(\\[size=\\d+px\\])[^\\n]+(\\[\\/size\\])",3,testo);
			document.getElementById(areaName).value = testo;
			
			testo = transform("(\\[youtube\\])[\\w\\-]+(\\[\\/youtube\\])",2,testo);
			document.getElementById(areaName).value = testo;
			
			testo = transform("(\\[email=[a-zA-Z_.]+[a-zA-Z\\-_.0-9]+@[a-zA-Z\\-_0-9]+\\.[a-z]{2,4}\\])[^\\n]+(\\[\\/email\\])",3,testo);
			document.getElementById(areaName).value = testo;
			
			testo = transform("(\\[:\\)\\])",4,testo);
			document.getElementById(areaName).value = testo;
			
			testo = transform("(\\[:\\(\\])",4,testo);
			document.getElementById(areaName).value = testo;

			testo = transform("(\\[:o\\])",4,testo);
			document.getElementById(areaName).value = testo;

			testo = transform("(\\[:p\\])",4,testo);
			document.getElementById(areaName).value = testo;

			
			testo = transform("(\\[:D\\])",4,testo);
			document.getElementById(areaName).value = testo;

			testo = transform("(\\[:!\\])",4,testo);
			document.getElementById(areaName).value = testo;

			testo = transform("(\\[:O\\])",4,testo);
			document.getElementById(areaName).value = testo;
			
			testo = transform("(\\[8\\)\\])",4,testo);
			document.getElementById(areaName).value = testo;
			
			testo = transform("(\\[;\\)\\])",4,testo);
			document.getElementById(areaName).value = testo;
		}
/**/
		//Trasforma tutte le \n in <br>
		RE = /\n/ig;
		testo = testo.replace(RE, "<br>");
		document.getElementById(areaName).value = testo;
		
		document.getElementById(formName).submit();
	}
	else alert ("Titolo o corpo del testo mancante.");
}

function br2nl(stringa)
{
	RE = new RegExp("<br>", "ig");
	return stringa.replace(RE, "\n")
}

function nl2br(stringa)
{
	RE = new RegExp("\n", "ig");
	return stringa.replace(RE, "<br>")
}
