﻿/* dichiaro l'oggetto XMLHttpRequest */
var nrequests = 0;

function BOB_CreateXmlHttpReq() {
	var xmlhttp = null;
	try {
		xmlhttp = new XMLHttpRequest();
		} catch(e) {
		try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlhttp;
}

function BOB_creaHTML(myHtml, nomefile) {
	// se sono alla prima richiesta apro il DIV di lavori in corso
	if (nrequests == 0) {BOB_displayDiv("working");}
	
	var requester = BOB_CreateXmlHttpReq ();
	//requester.onreadystatechange = function () {BOB_creaFile(requester,nomefile);} valido x chiamata asincrona
	//alert(myHtml);
	//requester.async = false;
	requester.open("GET", myHtml, false);
	requester.send(null);
	BOB_creaFile(requester,nomefile);
}

function BOB_creaFile(requester, nomefile) {
	if (requester.readyState == 4) {
		if (requester.status == 200) {
			var params = requester.responseText;
			//alert(params);
			//alert(nomefile);
			requester = BOB_CreateXmlHttpReq();
			//requester.onreadystatechange = function () {BOB_creatoFile(requester, nomefile);} valido x chiamata asincrona
			//requester.async = false;
			requester.open("POST", "../crea_pagina.asp?nome=" + nomefile, false);
			requester.setRequestHeader("Content-Type","text/plain");
			requester.setRequestHeader("Content-length", params.length);
			requester.setRequestHeader("Connection", "close");
			requester.send(params);
			nrequests += 1;
			BOB_creatoFile(requester, nomefile);
		}
		else if (requester.status != 0) { alert("Impossibile elaborare l'HTML dell'articolo!\nErr: " + requester.status); }
	}
}

function BOB_creatoFile(requester, nomefile) {
	if (requester.readyState == 4) {
		if (requester.status == 200) {
			//alert("<br/>File creato: " + nomefile)
			nrequests -= 1;
			if (nrequests == 0) {
				// nascondo il DIV di lavoro in corso
				BOB_hideDiv("working");
				//alert("Elaborazione completata");
			}
		}
		else if (requester.status != 0) { alert("Impossibile creare il file '" + nomefile + "'!\nErr: " + requester.status); }
	}
}

function BOB_createDiv(d,h,c) {
	var divTag = document.createElement("div");
	divTag.id = d;
	divTag.className = c;
	divTag.innerHTML = h;
	document.body.appendChild(divTag);
}

function BOB_removeDiv(d) {
	var divTag = document.getElementById(d);
	document.body.removeChild(divTag);
}

function BOB_displayDiv(d) {
	var divTag = document.getElementById(d);
	divTag.style.display = "block";
}

function BOB_hideDiv(d) {
	var divTag = document.getElementById(d);
	divTag.style.display = "none";
}

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;
	
	// 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;
		
		// 22/03/2005 - controllo sicurezza input
		if ((sValue != "") && (!isAR)) {
			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 (sValue == "") {			
				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 = "Sei sicuro di voler aggiornare quest" + tabella + "?";
	//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('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] != "ita") && (aS[i] != "fra") && (aS[i] != "ing") && (aS[i] != "ted") && (aS[i] != "spa") && (aS[i] != "por"))
	BOB_openBrWindow(w + 'mailing/mailing.asp?a=' + a,'malwin','scrollbars=no,width=400,height=300');
}

function BOB_setDiesis() {
	if (document.bob_formo.link_cartella.value == "#") {document.bob_formo.link.value = "top"}
}

function BOB_setInternalPage(p,i,t) {
	document.bob_formo.link_cartella.value = p;	
	document.bob_formo.link.value = "pagina.asp?ID=" + i + "&T=" + t;
}

function BOB_nocopertina(oggetto, path) {
	oggetto.src= path + '/upload/catalogo/copertina.gif';
	oggetto.border=0;
	oggetto.width=1;
}

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 BOB_replace(sText, chr1, chr2)
{
	var Char;
	var newText;
	newText = "";

	for (i = 0; i < sText.length; i++) {
		Char = sText.charAt(i); 
		if (Char == chr1) {
			newText += chr2;
		} else {
			newText += Char;
		}
	}
	
	return newText;
}

function BOB_showhidedescrizione(cosa,chi) {
	// nascondo tutti gli altri
	if (cosa == "show") {
		var n = document.fnd.righe.value;	
		for (var i=1;i<n;i++) {
			if (i != chi) {
				BOB_showhidedescrizione('hide',i);
			}
		}
	}
	
	// visualizzo quello cliccato
	var myD;
	var mybtnD;
	var myimgD;
	myD = document.getElementById("descrizione" + chi);
	mybtnD = document.getElementById("btn_descrizione" + chi);
	myimgD = document.getElementById("img_descrizione" + chi);
	if (cosa == "show") {
		myD.className = "descrizione_on";
		myimgD.src = "../../gfx/descrizione_off.gif";
		myimgD.alt = "Nascondi descrizione articolo";
		mybtnD.onclick = function () {BOB_showhidedescrizione('hide',chi);}
	} else {
		myD.className = "descrizione_off";
		myimgD.src = "../../gfx/descrizione_on.gif";
		myimgD.alt = "Visualizza descrizione articolo";
		mybtnD.onclick = function () {BOB_showhidedescrizione('show',chi);}
	}
}


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;
}
