/**
******************
* CONTROLLI FORM *
******************
*/
function ControllaTastoNumerico(e,Decimale) {
	var Carattere;

	if (! document.all) {
		Carattere = e.which;
	} else {
		Carattere = e.keyCode;
	}
	if (Carattere != 8 && Carattere != 0 && Carattere != 9 && Carattere != 63272 && Carattere != 25 && Carattere != 63234 && Carattere != 63235) {
		return IsNumeric(String.fromCharCode(Carattere),Decimale);
	}
	return true;
}

function ControllaCampoNumerico(formElement) {
	if (! IsNumeric(formElement.value)) {
		formElement.value="";
	}
}

function ControllaCampoData(formElement) {
	formElement.value=IsDate(formElement.value);
}

function IsEmpty(aTextField) {
	if ((aTextField.value.length==0) ||
		(aTextField.value==null)) {
		return true;
	} else {
		return false;
	}
}

function IsNumeric(sText,Decimale) {

	var ValidChars;
	var IsNumber=true;
	var Char;

	if (Decimale) {
		ValidChars = "0123456789.";
	} else {
		ValidChars = "0123456789";
	}

	for (i = 0; i < sText.length && IsNumber == true; i++) {
		Char = sText.charAt(i);
		if (ValidChars.indexOf(Char) == -1) {
			IsNumber = false;
		}
	}
	return IsNumber;
}
function roundNumber(n,r) {
	var newnumber = Math.round(n*Math.pow(10,r))/Math.pow(10,r);
	return newnumber;
}

function y2k(number) { 
	return (number < 1000) ? number + 1900 : number; 
}



function IsDate(dateStr) {

        var today = new Date();
		var YYYY  = y2k(today.getYear());

		if (dateStr.indexOf("/")<0 && dateStr.indexOf("-")<0 && dateStr.indexOf(".")<0 && IsNumeric(dateStr,false)) {
			if (dateStr.length == 8) {
				dateStr = dateStr.substr(0,2) + "/" + dateStr.substr(2,2) + "/" + dateStr.substr(4,4);
			}
			if (dateStr.length == 6) {
				dateStr = dateStr.substr(0,2) + "/" + dateStr.substr(2,2) + "/" + dateStr.substr(4,2);
			}
			if (dateStr.length == 4) {
				dateStr = dateStr.substr(0,2) + "/" + dateStr.substr(2,2) + "/" + YYYY;
			}
			if (dateStr.length <= 2) {
				dateStr = dateStr + "/" + (today.getMonth() + 1) + "/" + YYYY;
			}
		}

        var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{1,2}|\d{4})$/;
        var matchArray = dateStr.match(datePat); // is the format ok?

        if (matchArray == null) {
		dateStr2 = dateStr + "/" + YYYY;
        	matchArray = dateStr2.match(datePat); // is the format ok?
        	if (matchArray == null) {
			dateStr2 = dateStr + "/" + (today.getMonth() + 1) + "/" + YYYY;
        		matchArray = dateStr2.match(datePat); // is the format ok?
        		if (matchArray == null) {
                		//alert("Please enter date as either mm/dd/yyyy or mm-dd-yyyy.");
                		return "";
			}
		}
        }

        // p@rse date into variables
        day = Number(matchArray[1]);
        month = Number(matchArray[3]);
        year = Number(matchArray[5]);
	if (year < 30) {
		year=year+2000;
	} else {
		if (year < 100) {
			year=year+1900;
		}
	}

        if (month < 1 || month > 12) { // check month range
                //alert("Month must be between 1 and 12.");
                return "";
        }

        if (day < 1 || day > 31) {
                //alert("Day must be between 1 and 31.");
                return "";
        }

        if ((month==4 || month==6 || month==9 || month==11) && day==31) {
                //alert("Month "+month+" doesn`t have 31 days!")
                return "";
        }

        if (month == 2) { // check for february 29th
                var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
                if (day > 29 || (day==29 && !isleap)) {
                        //alert("February " + year + " doesn`t have " + day + " days!");
                        return "";
                }
        }
	dayStr = day + "/";
	monthStr = month + "/";
	if (dayStr.length == 2) {
		dayStr = "0" + dayStr;
	}
	if (monthStr.length == 2) {
		monthStr = "0" + monthStr;
	}
	dateStr = dayStr + monthStr + year;
        return dateStr; // date is valid
}
function getScreenHeight() {
	var winH = 0;

	if (parseInt(navigator.appVersion)>3) {
		if (navigator.appName=="Netscape") {
			winH = window.innerHeight;
		}
		if (navigator.appName.indexOf("Microsoft")!=-1) {
			winH = document.body.offsetHeight;
		}
	}
	return winH;
}
/*
************************************
* FUNZIONI PER INTERAZIONE CON PHP *
************************************
*/
function phpGet(url,frm) {
   if (document.all) {
      x = new ActiveXObject("Msxml2.XMLHTTP");
   } else {
      x = new XMLHttpRequest();
   }
//   if (frm) {
//      x.open("POST",url,true);
//      x.send(getFormValues(frm));
//   } else {
      x.open("GET",url,false);
      x.send(null);
//   }
   return x.responseText;
}
/*
***********
* RICERCA *
***********
*/
function avviaRicerca(query,rif,all,x,y) {

	if (! x) x = 600;
	if (! y) y = 570;

	phpGet(query+'&cod=ricerca&cmp='+rif); // QUERY NELLE VARIABILI DI SESSIONE

	var fld = Array('','','','','','','','','','','','','','','','','','','','');

	if ( all == false ) // NON tutti i record 
	{

		var rs = phpGet('index.php?act=cerca&debug=0&cmp='+rif);
		switch (rs) 
		{
			case 'morerows':
				window.open('index.php?act=cerca&cod=trova&debug=1&r='+rif,'ricerca','width='+x+',height='+y+',left='+(screen.width-x)/2+',top='+(screen.height-y)/2);
				break;
			case 'norows':
				break;
			default:
				var fld = rs.split('|');
		}
	} else { // tutti i record
		window.open('index.php?act=cerca&cod=trova&debug=1&r='+rif,'ricerca','width='+x+',height='+y+',left='+(screen.width-x)/2+',top='+(screen.height-y)/2); // popup con tutti i record della query
	}

	return fld;
}



/*  MAGNIFICO!!! ROLLOVER 
	basta assegnare alla tabella la class 'ruler' e automaticamente
	si avranno i rollover sulle righe della tabella
    tableruler()
    written by Chris Heilmann for alistapart.
    enables a rollover of rows for each table with the className "hlrows"
*/
function tableruler()
{
    if (document.getElementById && document.createTextNode)
    {
        var tables=document.getElementsByTagName('table');
        for (var i=0;i<tables.length;i++)
        {
            if(tables[i].className=='ruler')
            {
                var trs=tables[i].getElementsByTagName('tr');
                for(var j=0;j<trs.length;j++)
                {
                    if(trs[j].parentNode.nodeName=='TBODY')
                    {
                        trs[j].onmouseover=function(){this.className='ruled';return false}
                        trs[j].onmouseout=function(){this.className='';return false}
                    }
                }
            }
        }
    }
}



function reset_form_color(t)
{
	for( i=0 ; i < t.elements.length ; i++ ){
        if( (t.elements[i].type == 'select-one') || (t.elements[i].type=='text') )
            t.elements[i].style.backgroundColor='#D8E2EC';
    }
}

function getControl(c)
{
        c.style.backgroundColor='#ff9900';
		c.focus();
}


//-------------------------------------------------------------
// RESTITUISCE IL NUMERO DI OCCORRENZE DI UN VALORE IN UN ARRAY
//-------------------------------------------------------------
function findValue(val,arr)
{
	var cnt=0;
	//if( arr.length != 0 )
		for (i=0; i<arr.length;i++)
		{
			if(val == arr[i])
			{
				cnt++;
			}

		}

	return cnt;
}


//-----------------
// LUNGHEZZA CAMPI 
//-----------------

function check_len(nome,lung)
{
	var l = nome.value.length;
 
	if ( l < lung  && l !=0 )
	{
		alert("Attenzione: \nla lunghezza del campo e\' minore di " + lung + " caratteri");
		return false;
	}
	return true;
}

//-----------------
// VALIDITA' EMAIL
//----------------

function check_mail(campo)
{
	var rx = /^([a-za-z0-9_\.\-])+\@(([a-za-z0-9\-]{2,})+\.)+([a-za-z0-9]{2,})+$/;

	var mail= eval("document.frm." + campo).value;

	if ( mail != "" &&  !rx.test(mail)  ) {
		alert ("Attenzione: l'indirizzo "+mail+" non è valido");
		return false;		
	}
	return true;
}


function isArray(a) {
    return isObject(a) && a.constructor == Array;
}


function isObject(a) {
    return (a && typeof a == 'object') || isFunction(a);
}

function isFunction(a) {
    return typeof a == 'function';
}

//==============================
//=========== POPUP ============
//==============================


PositionX = 100;
PositionY = 100;

defaultWidth  = 500;
defaultHeight = 500;

// Set autoclose true to have the window close automatically
// Set autoclose false to allow multiple popup windows
var AutoClose = true;

// Do not edit below this line...
// ================================
if (parseInt(navigator.appVersion.charAt(0))>=4){
   var isNN=(navigator.appName=="Netscape")?1:0;
   var isIE=(navigator.appName.indexOf("Microsoft")!=-1)?1:0;}
   var optNN='scrollbars=no,width='+defaultWidth+',height='+defaultHeight+',left='+PositionX+',top='+PositionY;
   var optIE='scrollbars=no,width=150,height=100,left='+PositionX+',top='+PositionY;

function popImage(imageURL,imageTitle,resize){

                if (isNN){imgWin=window.open('about:blank','',optNN);}
                if (isIE){imgWin=window.open('about:blank','',optIE);}
                with (imgWin.document){
                writeln('<html><head><title>Loading...</title><style type=\'text/css\'>body{margin:0px;}</style>');writeln('<sc'+'ript>');
                writeln('var isNN,isIE;');writeln('if (parseInt(navigator.appVersion.charAt(0))>=4){');
                writeln('isNN=(navigator.appName=="Netscape")?1:0;');writeln('isIE=(navigator.appName.indexOf("Microsoft")!=-1)?1:0;}');
                writeln('function reSizeToImage(){');writeln('if (isIE){');writeln('window.resizeTo(100,100);');
                writeln('width=100-(document.body.clientWidth-document.images[0].width);');
                writeln('height=100-(document.body.clientHeight-document.images[0].height);');
                writeln('window.resizeTo(width,height);}');writeln('if (isNN){');
                writeln('window.innerWidth=document.images["cat_img"].width;');writeln('window.innerHeight=document.images["cat_img"].height;}}');
                writeln('function doTitle(){document.title="'+imageTitle+'";}');writeln('</sc'+'ript>');
                      if (!AutoClose) writeln('</head><body bgcolor=000000 scroll="no" onload="reSizeToImage();doTitle();self.focus()">')
                else writeln('</head><body bgcolor=000000 scroll="no" onload="reSizeToImage();doTitle();self.focus()" onblur="self.close()">');
                writeln('<img onclick="javascrpit:window.close()" name="cat_img" src='+imageURL+' style="display:block"></body></html>');
                close();
                }
}




window.onload=function(){
	tableruler(); // rollover tabelle 
}
