function imgSwap(oImg) {
	var strOver  = "_on"    // image to be used with mouse over
	var strOff = "_off"     // normal image
	var strImg = oImg.src
	if (strImg.indexOf(strOver) != -1)
		oImg.src = strImg.replace(strOver,strOff)
	else
		oImg.src = strImg.replace(strOff,strOver)
}

function confirmLogout()
{
	var agree=confirm("Sei sicuro di voler uscire dal sistema?");
	if (agree)
		return true ;
	else
		return false ;
}


/***********************************************
* Disable "Enter" key in Form script- By Nurul Fadilah(nurul@REMOVETHISvolmedia.com)
* This notice must stay intact for use
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/

function handleEnter (field, event) {
	var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
	if (keyCode == 13) {
		var i;
		for (i = 0; i < field.form.elements.length; i++)
			if (field == field.form.elements[i])
				break;
		i = (i + 1) % field.form.elements.length;
		field.form.elements[i].focus();
		return false;
	}
	else
	return true;
}

function intercetta() {
	//alert('ciao '+document.links.length);
    for (var i=0; i<document.links.length; i++)
        if (document.links[i].className=="_blank") {
            document.links[i].target="_blank";
        }
}

function poptastic(theurl, h, w, scrollbars) {
	if (typeof(scrollbars) == 'undefined') {
		scrollbars='no';
	}
	newwindow=window.open(theurl,'name','height='+h+', width='+w+',scrollbars='+scrollbars);
	if (window.focus) {
		newwindow.focus()
	}
}

var backImage = new Array();
var img = 1;

function changeBGImage(){

	tmpcont = document.getElementById('container');

	tmpcont.style.background = '#eda92e url(/style/'+backImage[img]+') no-repeat';

	img = img+1;

	if (img >= backImage.length) {
		img = 0;
	}

	//alert (img);

	swap = window.setTimeout('changeBGImage()',10000);

}

function initpage() {
	if (document.forms[0]) {
		for (i=0; i<document.forms[0].elements.length; i++) {
			if (document.forms[0].elements[i].type!='hidden') {
				document.forms[0].elements[i].focus();
				break;
			}
		}
	}

	for (var i=0; i<document.links.length; i++) {
		if (document.links[i].className=="_blank") {
			document.links[i].target="_blank";
		}
	}
}

function countdown(){
	reloadtimeout = reloadtimeout - 1;
	//changelayer_content(reloadtimeout);
	if (reloadtimeout>0){
		Id = window.setTimeout("countdown()",1000);
	}
	else{
		window.location.reload();
	}
}

function changelayer_content(counter){
	var counterlayer;
	var paginone = parent

	msgstring=counter;

	if(document.layers){
		//thisbrowser="NN4";
		//alert ('nn4');
		counterlayer = parent.document.layers[0];
		counterlayer.document.open();
		counterlayer.document.write(msgstring);
		counterlayer.document.close();
	}
	if(document.all){
		//thisbrowser="ie"
		//alert ('ie');
		counterlayer = parent.document.all["jscounter"];
		counterlayer.innerHTML=msgstring;
	}
	if(!document.all && document.getElementById){
		//thisbrowser="NN6";
		//alert ('nn6');
		counterlayer = parent.document.getElementById("jscounter");
		counterlayer.innerHTML = msgstring;
	}
}

//window.onload = intercetta();

var list;
var tickerObj;
var hex = 255;

function fadeText(divId) {
	if(tickerObj) {
		if(hex>0) {
			hex-=5; // increase color darkness
			tickerObj.style.color="rgb("+hex+","+hex+","+hex+")";
			setTimeout("fadeText('" + divId + "')", fadeSpeed);
			var obj=document.getElementById(divId).getElementsByTagName("A");
			if (obj.length>0){
				for (i=0;i<obj.length;i++)
				obj[i].style.color="rgb("+hex+","+hex+",255)";
			}
		}
		else
			hex=255; //reset hex value
	}
}

function initialiseList(divId) {
	tickerObj = document.getElementById(divId);
	if(!tickerObj)
		reportError("Could not find a div element with id \"" + divId + "\"");
	list = tickerObj.childNodes;
	if(list.length <= 0)
		reportError("The div element \"" + divId + "\" does not have any children");
	for (var i=0; i<list.length; i++) {
		var node = list[i];
		if (node.nodeType == 3 && !/\S/.test(node.nodeValue))
		tickerObj.removeChild(node);
	}
	run(divId, 0);
}

function run(divId, count) {
	fadeText(divId);
	list[count].style.display = "block";
	if(count > 0)
		list[count-1].style.display = "none";
	else
		list[list.length-1].style.display = "none";
	count++;
	if(count == list.length)
		count = 0;
	window.setTimeout("run('" + divId + "', " + count+ ")", interval*1000);
}

function reportError(error) {
	alert("The script could not run because you have errors:\n\n" + error);
	return false;
}

function validacodaz(codaz, codnaz) {
	var ok = false;

	if(validapiva(codaz)) {
		ok = 1;
	}
	else if(validatvafr(codaz)) {
		ok = 74;
	}

	if (codnaz != -1 && codnaz != ok) {
		ok = false;
	}

	return ok;
}

function validapiva(sz_Codice) {

	var n_Val,n_Val2,n_Som1=0,n_Som2=0,lcv,pippo;
	if (sz_Codice.length!=11 || isNaN(parseFloat(sz_Codice)) || parseFloat(sz_Codice)<parseFloat(0)) {
		return false;
	}

	for (lcv=0;lcv<9;lcv+=2) {
		n_Val=parseInt(sz_Codice.charAt(lcv));
		n_Som1+=n_Val;

		n_Val2=parseInt(sz_Codice.charAt(lcv+1))*2;

		if (n_Val2 > 9) {
			n_Val2 = n_Val2 - 9;
		}
		//pippo = Math.floor(n_Val2/5) + (n_Val2<<1) % 10;
		n_Som1+=n_Val2;
	}

	var tmp = n_Som1 % 10;

	if (tmp == 0) {
		n_Som2 = 0;
	}
	else {
		n_Som2 = 10 - tmp;
	}

	n_Val=parseInt(sz_Codice.charAt(10));

	if (n_Som2==n_Val) {
		return true;
	}

	return false;
}

function validatvafr(tva) {

	var frcode = tva.substr(0,2);

	if (frcode.toUpperCase() != 'FR') {
		return false;
	}

	//per i caratteri 2 e 3 non si sa un cazzo su come validarli

	var siren = tva.substr(4,12);

	if ((siren.length != 9) || (isNaN(siren))) {
		return false;
	}

	// gli ultimi 9 caratteri invece sono il siren dell'azienda
	var somme = 0;
	var tmp;

	for (var cpt = 0; cpt<siren.length; cpt++) {
		if ((cpt % 2) == 1) { // Les positions paires : 2ème, 4ème, 6ème et 8ème chiffre
			tmp = siren.charAt(cpt) * 2; // On le multiplie par 2
			if (tmp > 9) {
				tmp -= 9;	// Si le résultat est supérieur à 9, on lui soustrait 9
			}
		}
		else {
			tmp = siren.charAt(cpt);
		}
		somme += parseInt(tmp);
	}

	if ((somme % 10) == 0) {
		return true;
	}
	else {
		return false;
	}
}

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	}
	else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}
