// JavaScript Document

var v = new Array();
v["username"] = 'utente';
v["password"] = 'password';
v["name"] = 'Inserisci qui il tuo Nome*';
v["surname"] = 'Inserisci qui il tuo Cognome*';
v["email"] = 'Inserisci qui la tua Email*';

function InputDel(campo) {
	if (campo.value == v[campo.name]) {
		campo.value= '';
	}	
}

function InputFill(campo) {
	if (campo.value=='') {
		campo.value= v[campo.name];
	}	
}

function PopUp(page,w,h,t,s,m) 
	{
	popwidth = w;
	popheight = h;
    if (!t) {t = 'no'};
	if (!s) {s = 'no'};
	if (!m) {m = 'no'};
	OpenWin = this.open(page, "prn", "toolbar=" + t + ", menubar=" + m +", location=no, scrollbars=" + s + ", resizable=no, width="+popwidth+", height="+popheight+", left=" + Math.floor((screen.width - popwidth) / 2) + ", top=" + Math.floor((screen.height - popheight) / 2));
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
  var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
  if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function controllo_ricerca(theForm){
	if (theForm.SearchedTxt.value == "" || theForm.testo_ricerca.value == " ") {
    	alert("E' necessario inserire il testo da ricercare.\nGrazie.");
    	theForm.SearchedTxt.focus();
    	return (false);
	}
}

function ControlloLogin(theForm){
	if (theForm.username.value == "") {
    	alert("Per favore inserire l'utente.\nGrazie.");
    	theForm.username.focus();
    	return (false);
	}
	if (theForm.password.value == "") {
    	alert("Per favore inserire la password.\nGrazie.");
    	theForm.password.focus();
    	return (false);
	}	
}

function ControlloNewsletter(theForm){
	if (theForm.name.value == "" || theForm.name.value == "Inserisci qui il tuo Nome*") {
    	alert("Per favore inserisci il tuo nome.\nGrazie.");
    	theForm.name.focus();
    	return (false);
	}
	else if(theForm.surname.value == "" || theForm.surname.value == "Inserisci qui il tuo Cognome*") {
    	alert("Per favore inserisci il tuo cognome.\nGrazie.");
    	theForm.surname.focus();
    	return (false);
	}	
	else if (theForm.email.value == "" || theForm.email.value == "Inserisci qui la tua Email*") {
    	alert("Per favore inserisci la tua email.\nGrazie.");
    	theForm.email.focus();
    	return (false);
	}	
	else if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(theForm.email.value))) {
		alert("Indirizzo E-Mail non valido, si prega di verificarlo");
		theForm.email.focus();
		return (false);
	}
	else if (theForm.privacy.checked == false) {
		alert("Per procedere è necessario acconsentire al trattamento dei dati");
		theForm.privacy.focus();
		return (false);
	}
}

function URLEncode(s)
{
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";

	var plaintext = s;
	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			    alert( "Unicode Character '" 
                        + ch 
                        + "' cannot be encoded using standard URL encoding.\n" +
				          "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted." );
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for

	
	return encoded;
};

function URLDecode(s)
{
   // Replace + with ' '
   // Replace %xx with equivalent character
   // Put [ERROR] in output if %xx is invalid.
   var HEXCHARS = "0123456789ABCDEFabcdef"; 
   var encoded = s;
   var plaintext = "";
   var i = 0;
   while (i < encoded.length) {
       var ch = encoded.charAt(i);
	   if (ch == "+") {
	       plaintext += " ";
		   i++;
	   } else if (ch == "%") {
			if (i < (encoded.length-2) 
					&& HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 
					&& HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
				plaintext += unescape( encoded.substr(i,3) );
				i += 3;
			} else {
				alert( 'Bad escape combination near ...' + encoded.substr(i) );
				plaintext += "%[ERROR]";
				i++;
			}
		} else {
		   plaintext += ch;
		   i++;
		}
	} // while
   
   return plaintext;
};

/*
	Developed by Robert Nyman, http://www.robertnyman.com
	Code/licensing: http://code.google.com/p/getelementsbyclassname/
*/	

var getElementsByClassName = function (className, tag, elm){
	if (document.getElementsByClassName) {
		getElementsByClassName = function (className, tag, elm) {
			elm = elm || document;
			var elements = elm.getElementsByClassName(className),
				nodeName = (tag)? new RegExp("\\b" + tag + "\\b", "i") : null,
				returnElements = [],
				current;
			for(var i=0, il=elements.length; i<il; i+=1){
				current = elements[i];
				if(!nodeName || nodeName.test(current.nodeName)) {
					returnElements.push(current);
				}
			}
			return returnElements;
		};
	}
	else if (document.evaluate) {
		getElementsByClassName = function (className, tag, elm) {
			tag = tag || "*";
			elm = elm || document;
			var classes = className.split(" "),
				classesToCheck = "",
				xhtmlNamespace = "http://www.w3.org/1999/xhtml",
				namespaceResolver = (document.documentElement.namespaceURI === xhtmlNamespace)? xhtmlNamespace : null,
				returnElements = [],
				elements,
				node;
			for(var j=0, jl=classes.length; j<jl; j+=1){
				classesToCheck += "[contains(concat(' ', @class, ' '), ' " + classes[j] + " ')]";
			}
			try	{
				elements = document.evaluate(".//" + tag + classesToCheck, elm, namespaceResolver, 0, null);
			}
			catch (e) {
				elements = document.evaluate(".//" + tag + classesToCheck, elm, null, 0, null);
			}
			while ((node = elements.iterateNext())) {
				returnElements.push(node);
			}
			return returnElements;
		};
	}
	else {
		getElementsByClassName = function (className, tag, elm) {
			tag = tag || "*";
			elm = elm || document;
			var classes = className.split(" "),
				classesToCheck = [],
				elements = (tag === "*" && elm.all)? elm.all : elm.getElementsByTagName(tag),
				current,
				returnElements = [],
				match;
			for(var k=0, kl=classes.length; k<kl; k+=1){
				classesToCheck.push(new RegExp("(^|\\s)" + classes[k] + "(\\s|$)"));
			}
			for(var l=0, ll=elements.length; l<ll; l+=1){
				current = elements[l];
				match = false;
				for(var m=0, ml=classesToCheck.length; m<ml; m+=1){
					match = classesToCheck[m].test(current.className);
					if (!match) {
						break;
					}
				}
				if (match) {
					returnElements.push(current);
				}
			}
			return returnElements;
		};
	}
	return getElementsByClassName(className, tag, elm);
};

function Chiocciola() {
document.write('@');
}

function goToDay(day, incr) {
  var oday = new String;
  oday += day;
  var year = oday.replace(/(.{4}).{2}.{2}/, "$1");
  var month = oday.replace(/.{4}(.{2}).{2}/, "$1");
  var day = oday.replace(/.{4}.{2}(.{2})/, "$1");
  var nday = new Date(year, month - 1, day);
  nday.setDate(nday.getDate() + incr);
  nday = nday.getFullYear() * 10000 + (nday.getMonth() + 1) * 100 + nday.getDate();
  document.location.href= "/adon.pl?act=Calendar&Mode=Src&day="+nday;
}
