var re_number = new RegExp("[^0-9]");
var re_text = new RegExp("[^a-zA-Z]");
var re_alphanumeric = new RegExp("[^a-zA-Z0-9\_\.]");

function BOB_checkLengthItem(campo, lunghezza, nome, formo) {
	
	var sValue;	
	if (formo == null) {formo = "bob_formo"}	
	sValue = document.forms[formo].elements[campo].value;
	if (sValue.length > lunghezza) {
		alert ("La lunghezza del campo '" + nome + "' dev'essere minore di " + lunghezza + " caratteri.\n\nTu ne hai digitati " + sValue.length);
		document.forms[formo].elements[campo].focus();
		return false
	} else {
		return true
	}
}

function BOB_RegExpTest(patrn, strng) {
	var regEx = new RegExp(patrn);
	//alert("patrn: " + patrn + "\n strng: " + strng + "\n esito: " + regEx.test(strng));
	return regEx.test(strng)
}

function BOB_checkForm(lingua, formo) {
	
	var i, sName, pt;
	
	// 22/03/2005 - controllo sicurezza input (sono nell'area riservata?)
	var isAR = false;
	var percorso = document.URL.split("/");	
	for (var loop=0; loop < percorso.length; loop++) { if (percorso[loop] == "areariservata") {isAR = true;break};}
	
	if (formo == null) {formo = "bob_formo"}	
	for (i=0;i < document.forms[formo].length; i++) {
		sName = document.forms[formo].elements[i].name;
		sId = document.forms[formo].elements[i].id;
		sValue = document.forms[formo].elements[i].value;
			
		// 25/05/2011 - i campi senza nome non li valido
		if (sName != "") {
			// 18/10/2007 - controllo che il valore non sia solo di spazi
			var trimmed = sValue.replace(/^\s+|\s+$/g, '') ;
			
			// 22/03/2005 - controllo sicurezza input
			if ((trimmed != "") && (!isAR)) {
				if ((sValue.indexOf('\\') > -1) && (sName != "nomefoto")) {
					alert("Carattere '\\' nel campo '" + sName + "' non valido!");
					document.forms[formo].elements[i].focus();
					break
				}
				if ((sValue.indexOf('<') > -1)) {
					alert("Carattere '<' nel campo '" + sName + "' non valido!");
					document.forms[formo].elements[i].focus();
					break
				}
				if ((sValue.indexOf('>') > -1)) {
					alert("Carattere '>' nel campo '" + sName + "' non valido!");
					document.forms[formo].elements[i].focus();
					break
				}
			}
			
			if ((sName.charAt(0) != "_") && (sId.charAt(0) != "_")) {
				//alert("sName: " + sName)
				
				if (trimmed == "") {			
					if (lingua == "ita") alert ("Il campo  [ " + sName + " ]  è obbligatorio!")
					else if (lingua == "spa") alert ("Todas las casillas son obligatorias!")
					else if (lingua == "ing") alert ("Please compile the [ " + sName + " ]  obligatory field!")
					else if (lingua == "ted") alert ("Alle Einträge sind obligatorisch!")
					else if (lingua == "fra") alert ("Tous les champs sont obligatoires!")
					else if (lingua == "por") alert ("Please compile the [ " + sName + " ]  obligatory field!")
					else alert ("...");
					
					document.forms[formo].elements[i].focus();
					
					break
				} else {
					//if (((sName == "email") || (sName == "email_mittente") || (sName == "email_destinatario"))
					// && ((sValue.indexOf("@")==-1) || (sValue.indexOf(".")==-1))
					// ^[\w\-\.]*[\w\.]\@[\w\.]*[\w\-\.]+[\w\-]+[\w]\.+[\w]+[\w $]
					// ^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-]{2,})+\.)+([a-zA-Z0-9]{2,})+$
					//pt = "^([a-zA-Z0-9]{1,})([\\w\-])*([\.])*([\\w\-])*([a-zA-Z0-9]{1,})\@([a-zA-Z0-9]{1,})([\\w\-])*([\.])*([\\w\-])*([a-zA-Z0-9]{1,})\.([a-zA-Z]{2,})$";
					pt = "^([a-zA-Z0-9]{1,})([\\w\-])*([\.])*([\\w\-])*([a-zA-Z0-9]{1,})([\@])([a-zA-Z0-9]{1,})([\\w\-])*([\.])*([\\w\-])*([a-zA-Z0-9]{1,})([\.])([a-zA-Z]{2,})$";
					if (((sName == "EMAIL") || (sName == "email") || (sName == "email_mittente") || (sName == "email_destinatario"))
						&& (!BOB_RegExpTest(pt, sValue))) { 
						if (lingua == "ita") alert ("L'email inserita non è valida!")
						else if (lingua == "spa") alert ("La dirección e-mail no es correcta!")
						else if (lingua == "ing") alert ("Please insert a valid e-mail address!")
						else if (lingua == "ted") alert ("Die eingegebene e-Mail-Adresse ist ungültig")
						else if (lingua == "fra") alert ("Adresse e-mail non valable")
						else if (lingua == "por") alert ("Please insert a valid e-mail address!")
						else alert ("...");
						
						document.forms[formo].elements[i].focus();
						
						break
					}
				}
			}
		}
	}

	if (i == document.forms[formo].length) {
		return true
	} else {
		return false
	}
}


function BOB_submit(lingua, formo) {
	if (formo == null) {formo = "bob_formo"}
	if (BOB_checkForm(lingua, formo)) {
		document.forms[formo].submit();
	}
}

function BOB_saveformo(tabella, lingua) {
	testo = "?";
	if (lingua == "ita") {testo = "Sei sicuro di voler aggiornare quest" + tabella + "?"}

	if (confirm(testo)) {
		if (BOB_checkForm(lingua)) {
			document.bob_formo.submit()
		}
	}
}

function BOB_delart(tabella, lingua) {
	testo1 = "?";
	testo2 = "!";
	if (lingua == "ita") {
		testo1 = "Sei sicuro di voler eliminare quest" + tabella + "?"
		testo2 = "Nessun" + tabella + " selezionata!"
	}

	if (confirm(testo1)) {
		if (document.bob_formo._ID.value == "") {
			alert (testo2);
		} else {
			document.bob_formo._Cosa.value = "Del";
			document.bob_formo.submit()
		}
	}
}

function BOB_openBrWindow(theURL,winName,features) {
  mywin = window.open(theURL,winName,features);
  mywin.focus();
}

function BOB_openPassaparola() {
	BOB_openBrWindow('../portale/passaparola.asp','pspwin','scrollbars=no,width=450,height=360');
}

function BOB_openMailing(a) {
	var s = document.URL;
	var aS = s.split("/");
	var w = "";
	i = -1;
	do {
		i += 1;
		w += aS[i] + "/";
	}
	while ((aS[i] != "portale") && (aS[i] != "messaggero") && (aS[i] != "messaggero_emi"))
	BOB_openBrWindow(w + '../portale/mailing.asp?a=' + a,'malwin','scrollbars=no,width=400,height=300');
}

function BOB_setDiesis() {

	t = document.bob_formo.link_cartella.value;
	switch(t) {
	case "cartoline.asp":
	case "cercoamici.asp":
	case "chat/chat.asp":
	case "chisiamo.asp":
	case "copiaomaggio.asp":
	case "forum_msg.asp":
	case "gioca.asp":
	case "inconfidenza.asp":
	case "sommario.asp":
	case "sfoglia.asp":
	case "sitidascoprire.asp":
	case "spaziolibero/spaziolibero.asp":
	case "speciale.asp":
	case "test.asp":
		document.bob_formo.link.value = t
	break;
	
	case "#":
		document.bob_formo.link.value = "top"
	break;
	}
}

function BOB_setInternalPage(i,t) {
	document.bob_formo.link_cartella.value = "/portale/";	
	document.bob_formo.link.value = "pagina.asp?ID=" + i + "&T=" + t;
}

function BOB_setForum() {
	document.bob_formo.link_cartella.value = "/portale/";	
	document.bob_formo.link.value = "forum.asp?ID=" + document.bob_formo._forum.value
}

function BOB_openAlmanacco(i, id) {
	if (id == null) {id = ""}
	if (i == 1) { // santo
		san=window.open('almanacco/sagiorno.asp?ID=' + id,'sanwin','scrollbars=yes,top=0,left=0,width=360,height=500');
		san.focus();
	}
	if (i == 2) { // aforisma
		afor=window.open('almanacco/afgiorno.asp?ID=' + id,'afowin','scrollbars=yes,top=0,left=0,width=360,height=500');
		afor.focus();
	}
	if (i == 3) { // calendario
		cal=window.open('almanacco/caliturgico.asp?ID=' + id,'calwin','scrollbars=yes,top=0,left=0,width=360,height=500');
		cal.focus();
	}
}

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 IsDate(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;
}

// -----------------------------------------------------
// Rilevamento FLASH
// -----------------------------------------------------

var MM_contentVersion = 6;
var plugin = (navigator.mimeTypes && navigator.mimeTypes["application/x-shockwave-flash"]) ? navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin : 0;
if ( plugin ) {
	var words = navigator.plugins["Shockwave Flash"].description.split(" ");
	for (var i = 0; i < words.length; ++i) {
		if (isNaN(parseInt(words[i]))) continue;
		var MM_PluginVersion = words[i]; 
	}
	var MM_FlashCanPlay = MM_PluginVersion >= MM_contentVersion;
}
else if (navigator.userAgent && navigator.userAgent.indexOf("MSIE")>=0 
   && (navigator.appVersion.indexOf("Win") != -1)) {
	document.write('<SCR' + 'IPT LANGUAGE=VBScript\> \n'); //FS hide this from IE4.5 Mac by splitting the tag
	document.write('on error resume next \n');
	document.write('MM_FlashCanPlay = ( IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash." & MM_contentVersion)))\n');
	document.write('</SCR' + 'IPT\> \n');
}


function IsValidKey(sText)
{
	alert(sText);
	var ValidChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMOPQRSTUVWXYZ:;,.+-*()[]{}èéòàùì'%&=?!/\@#^0123456789 ";
	var IsValidKey=true;
	var Char;
	
	for (i = 0; i < sText.length && IsValidKey == true; i++) {
		Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) {IsValidKey = false;}
	}
	
	return IsValidKey;
}


function BOB_controllacarattere (e, cosa) {
	
	var key = window.event ? e.keyCode : e.which;		
	//alert(key);
	var esito = false;
	
	switch(cosa) {
	case "euro":
		// permetto solo i caratteri (0-9) e la virgola
		if ((key == 8) || (key == 44) || ((key >= 48) && (key <= 57))) {
			esito = true
		}
		break;
		
	case "numerico":
		// permetto solo i caratteri (0-9) e il backspace
		if ((key == 8) || ((key >= 48) && (key <= 57))) {
			esito = true
		}
		break;
	
	case "alfanumerico":
		// permetto solo i caratteri (0-9) (A-Z) (a-z), il backspace e l'underscore
		if ((key == 95) || (key == 8) || ((key >= 48) && (key <= 57)) || ((key >= 65) && (key <= 90)) || ((key >= 97) && (key <= 122))) {
			esito = true
		}
		break;
	}
	
	return esito		
}



function BOB_updateNChars(campo,tp,nchars) {
	sValue = document.bob_formo.elements[campo].value;
	campo = "_N" + campo;
	document.bob_formo.elements[campo].value = nchars-sValue.length-tp;
}


