
//POSIZIONE MOUSE
if (document.layers) { // Netscape
    document.captureEvents(Event.MOUSEMOVE);
    document.onmousemove = captureMousePosition;
} else if (document.all) { // Internet Explorer
    document.onmousemove = captureMousePosition;
} else if (document.getElementById) { // Netcsape 6
    document.onmousemove = captureMousePosition;
}
// Global variables
xMousePos = 0; // Horizontal position of the mouse on the screen
yMousePos = 0; // Vertical position of the mouse on the screen
xMousePosMax = 0; // Width of the page
yMousePosMax = 0; // Height of the page

function captureMousePosition(e) {
    if (document.layers) {
        // When the page scrolls in Netscape, the event's mouse position
        // reflects the absolute position on the screen. innerHight/Width
        // is the position from the top/left of the screen that the user is
        // looking at. pageX/YOffset is the amount that the user has
        // scrolled into the page. So the values will be in relation to
        // each other as the total offsets into the page, no matter if
        // the user has scrolled or not.
        xMousePos = e.pageX;
        yMousePos = e.pageY;
        xMousePosMax = window.innerWidth+window.pageXOffset;
        yMousePosMax = window.innerHeight+window.pageYOffset;
    } else if (document.all) {
        // When the page scrolls in IE, the event's mouse position
        // reflects the position from the top/left of the screen the
        // user is looking at. scrollLeft/Top is the amount the user
        // has scrolled into the page. clientWidth/Height is the height/
        // width of the current page the user is looking at. So, to be
        // consistent with Netscape (above), add the scroll offsets to
        // both so we end up with an absolute value on the page, no
        // matter if the user has scrolled or not.
        xMousePos = window.event.x+document.body.scrollLeft;
        yMousePos = window.event.y+document.body.scrollTop;
        xMousePosMax = document.body.clientWidth+document.body.scrollLeft;
        yMousePosMax = document.body.clientHeight+document.body.scrollTop;
    } else if (document.getElementById) {
        // Netscape 6 behaves the same as Netscape 4 in this regard
        xMousePos = e.pageX;
        yMousePos = e.pageY;
        xMousePosMax = window.innerWidth+window.pageXOffset;
        yMousePosMax = window.innerHeight+window.pageYOffset;
    }
}


function trim(stringa){
	while (stringa.substring(0,1) == ' '){
	    stringa = stringa.substring(1, stringa.length);
	}
	
	while (stringa.substring(stringa.length-1, stringa.length) == ' '){
		stringa = stringa.substring(0,stringa.length-1);
	}
    return stringa;
}

//ritorna la larghezza del browser
function getBrowserWidth ( ) {
    if ( window.innerWidth ) { return window.innerWidth; }
    else if ( document.documentElement && document.documentElement.clientWidth != 0 ) { return document.documentElement.clientWidth; }
    else if ( document.body ) { return document.body.clientWidth; }
    return 0;
}

function getBrowserHeight(){
	var x,y;
	var test1 = document.body.scrollHeight;
	var test2 = document.body.offsetHeight;
	// all but Explorer Mac
	if (test1 > test2) {
		x = document.body.scrollWidth;
		y = document.body.scrollHeight;
     }
     else{ // Explorer Mac; would also work in Explorer 6 Strict, Mozilla and Safari
		x = document.body.offsetWidth;
		y = document.body.offsetHeight;
	}
	return y;
}
//! Ritorna il codice HTML di una tabella cornice
function getCorniceCode(PATHIMG,dim,percentX,percentY,html,textAlign,valign){
	var width   = getBrowserWidth();
	var height  = getBrowserHeight();
	var code = "<table cellpadding = 0 cellspacing = 0><tr>"; 
	code += "<td width = "+ dim + "  height = "+ dim +" background = " + PATHIMG + "index/headerNW.jpg></td>";
    code += "<td width = "+ dim + "  height = "+ dim +" background = " + PATHIMG + "index/headerCentralDOWN.jpg></td>"; 
    code += "<td width = "+ dim + "  height = "+ dim +" background = " + PATHIMG + "index/headerNE.jpg></td>";
	code += "</tr>";
	code += "<tr>";
	code += "<td width = "+ dim + "  background = " + PATHIMG + "index/headerVerticalSX.jpg></td>";
	code += "<td valign = "+valign+" align = "+ textAlign +" width = "+ (width *(percentX/100) - dim * 2) + " height = "+ (height *(percentY/100) - dim * 2 * 1.8) + ">"+ html + "</td>"; 
    code += "<td width = "+ dim + "  background = " + PATHIMG + "index/headerVerticalDX.jpg></td>";
	code += "</tr>";
	code += "<tr>";
	code += "<td width = " + dim + "  height = " + dim + " background = " + PATHIMG + "index/headerSW.jpg></td>";
    code += "<td width = " + dim + "  height = " + dim + " background = " + PATHIMG + "index/headerCentralUP.jpg></td>"; 
    code += "<td width = " + dim + "  height = " + dim + " background = " + PATHIMG + "index/headerSE.jpg></td>";
	code += "</tr>";
	code += "</table>";
	
	return code;
	
}

function presenzaValoriNulli(values){
	var presente = false;
	for (var i = 0 ; i < values.length ; i++ )
	    if (trim(values[i]) == ""){
	    	presente = true;
	    	break;
	    }
	return presente;    
}

function SimpleSwap(el,which){
        el.src=el.getAttribute(which||"origsrc");
}

function textToInputConversion(el){
    for (var i = 0 ; i < el.length; i++){
        var obj = document.getElementById(el[i]);
        var testo = obj.innerHTML;
        var input = "<input size = 40 id ='"+el[i] +"' type ='text' value ='"+testo+"'>";
        obj.id = "";
        obj.innerHTML = input;
    } 
}

function attivaBottone(id,value,func){
	var el = document.getElementById(id);
	el.innerHTML = "<input type ='button' value = '"+ value + "' onclick ='"+ func + ";'>";
}

function isEmpty(elem, helperMsg){
	if(elem.value.length == 0){
		//alert(helperMsg);
		//elem.focus(); // set the focus to this input
		return true;
	}
	return false;
}

function textToSelectConversion(id,selectOptions){
    
	var el = document.getElementById(id);
	var selected = el.innerHTML;
	var selectCode = "<select id ='"+ el.id +"'>";
	
	for (var i = 0 ; i < selectOptions.length; i++)
	    if (selectOptions[i] == selected)
	        selectCode += "<option value='"+ selectOptions[i] +"' selected>" + selectOptions[i] + "</option>";
        else
	        selectCode += "<option value='"+ selectOptions[i] +"'>" + selectOptions[i] + "</option>";
    
    
    selectCode += "</select>";
    el.id = "";
    el.innerHTML = selectCode;
}

function checkNullValues(idList){
	for (var i = 0 ; i < idList.length; i++){
		var el = document.getElementById(idList[i]);
		
		if  (document.isSelect(el)){
            //verifica su oggetto select
			alert("select");
		}
	}
}

function checkForm(idForm){
	var objForm    = document.getElementById(idForm);
	var numElment  = objForm.elements.length;
	//objForm.elements[i]
	for (var i = 0 ; i < numElment; i++)
		if (objForm.elements[i].type!= "button")
		if (isEmpty(objForm.elements[i])  || (trim(objForm.elements[i].value) == "")){
            alert("compilare tutti i campi");
            return false;			
		} 
	return true;	
}

function loadNewLocation(newLocation){
	window.location = newLocation;
}

//verifica se needle è presente nell''aray arr
function inArray(needle,arr){
	trovato = false;
	for (var i = 0 ;  (i < arr.length) && (!trovato) ; i++)
	    if (arr[i] == needle)
	        trovato = true;
	return trovato;        
}

//crea il codice di una tabella contenente i valori dell'array
function arrayInTable(values,tdID,className,tdEv){
	
	var code = "<table";
	if (className) 
	   code += " class= " + className; 
	code +=">";
	for (var i = 0 ;  i < values.length ; i++){
		code +="<tr><td";
		if(tdID) code += " id = " + tdID[i];
		if(tdEv) code += " " + tdEv;
		code +=">"+ values[i] +"</td></tr>";
	}	
	code += "</table>";
	
	return code;
}

//rimuove un elemnto da un array
Array.prototype.remove=function(s){
  for(i=0;i<this .length;i++){
    if(s==this[i]) this.splice(i, 1);
  }
}

//controlla se il parametro è un nome di file valido
function isFile( filename ){
    var arrayFile = filename.split(".");
    if (arrayFile.length < 2)
       return false;
       
   var ext = arrayFile[arrayFile.length - 1];
  
   if (ext.length <  1)
       return false;
           
   return true;    
}

function fileExtension(filename){
	var arrayFile = filename.split(".");
    if (arrayFile.length < 2)
       return false;
       
   var ext = arrayFile[arrayFile.length - 1];
   return ext;
}




function creatediv(id, html, width, height, left, top) { 
   var newdiv = document.createElement("div");
   newdiv.setAttribute("id", id);   
   if (width) {
       newdiv.style.width = width;
   }   
   if (height) {
       newdiv.style.height = height;
   }   
   if ((left || top) || (left && top)) {
       newdiv.style.position = "absolute";
       
       if (left) {
           newdiv.style.left = left;
       }
       
       if (top) {
           newdiv.style.top = top;
       }
   }   
   newdiv.style.background = "white";
   newdiv.style.border = "4px solid #000";  
   //newdiv.style.overflow = "auto";
   if (html) {
       newdiv.innerHTML = html;
   } else {
       newdiv.innerHTML = "nothing";
   }   
   document.body.appendChild(newdiv);
}

function posInArray(elem,list){
	var trovato = false;
	var i = 0;
	var pos = -1;
	
	for (i = 0; i <list.length; i++){
	   if (list[i] == elem){
	   	  trovato = true;
	      pos = i;
	   }     
	}   

	return pos;     
}


var browserWidth   = 0;
var browserHeight  = 0;
var yWithScroll = 0;       
var xWithScroll = 0;
 
function browserSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  browserWidth   = myWidth;
  browserHeight  = myHeight;
}

browserSize();

function isMail ( address) {
    if ( stringEmpty (address) )
        alert ( "Errore! There is no E-Mail address entered" );
    else if ( noAtSign ( address) )
        alert ( "Errore! il campo email deve contere il simbolo @");
    else if ( nothingBeforeAt ( address) )
        alert ( "Errore! An E-Mail address must contain at least one character before the '@' character" );
    else if ( noLeftBracket ( address) )
        alert ( "Errore! The E-Mail address contains a right square bracket ']',\nbut no corresponding left square bracket '['" );
    else if ( noRightBracket ( address) )
        alert ( "Errore! The E-Mail address contains a left square bracket '[',\nbut no corresponding right square bracket ']'" );
    else if ( noValidPeriod ( address) )
        alert ( "Errore! l' email non contiente il carattere ('.')" );
    else if ( noValidSuffix ( address) )
        alert ( "Errore! il suffisso dell'e-mail deve essere di 2 o 3 caratteri" );
    else
        return (true);

    return ( false );
}

function linkCheckValidation ( formField ) {
    if ( checkValidation ( formField ) == true ) {
        alert ( 'E-Mail Address Validates OK' );
    }

    return ( false );
}

function stringEmpty ( address ) {
    // CHECK THAT THE STRING IS NOT EMPTY
    if ( address.length < 1 ) {
        return ( true );
    } else {
        return ( false );
    }
}

function noAtSign ( address ) {
    // CHECK THAT THERE IS AN '@' CHARACTER IN THE STRING
    if ( address.indexOf ( '@', 0 ) == -1 ) {
        return ( true )
    } else {
        return ( false );
    }
}

function nothingBeforeAt ( address ) {
    // CHECK THERE IS AT LEAST ONE CHARACTER BEFORE THE '@' CHARACTER
    if ( address.indexOf ( '@', 0 ) < 1 ) {
        return ( true )
    } else {
        return ( false );
    }
}

function noLeftBracket ( address ) {
    // IF EMAIL ADDRESS IN FORM 'user@[255,255,255,0]', THEN CHECK FOR LEFT BRACKET
    if ( address.indexOf ( '[', 0 ) == -1 && address.charAt ( address.length - 1 ) == ']' ) {
        return ( true )
    } else {
        return ( false );
    }
}

function noRightBracket ( address ) {
    // IF EMAIL ADDRESS IN FORM 'user@[255,255,255,0]', THEN CHECK FOR RIGHT BRACKET
    if ( address.indexOf ( '[', 0 ) > -1 && address.charAt ( address.length - 1 ) != ']' ) {
        return ( true );
    } else {
        return ( false );
    }
}

function noValidPeriod ( address ) {
    // IF EMAIL ADDRESS IN FORM 'user@[255,255,255,0]', THEN WE ARE NOT INTERESTED
    if ( address.indexOf ( '@', 0 ) > 1 && address.charAt ( address.length - 1 ) == ']' )
        return ( false );

    // CHECK THAT THERE IS AT LEAST ONE PERIOD IN THE STRING
    if ( address.indexOf ( '.', 0 ) == -1 )
        return ( true );

    return ( false );
}

function noValidSuffix ( address ) {
    // IF EMAIL ADDRESS IN FORM 'user@[255,255,255,0]', THEN WE ARE NOT INTERESTED
    if ( address.indexOf ( '@', 0 ) > 1 && address.charAt ( address.length - 1 ) == ']' )
        return ( false );

    // CHECK THAT THERE IS A TWO OR THREE CHARACTER SUFFIX AFTER THE LAST PERIOD
    var len = address.length;
    var pos = address.lastIndexOf ( '.', len - 1 ) + 1;
    if ( ( len - pos ) < 2 || ( len - pos ) > 3 ) {
        return ( true );
    } else {
        return ( false );
    }
}

function isInt(x) { 
   var y=parseInt(x); 
   if (isNaN(y)) return false; 
   return x==y && x.toString()==y.toString(); 
} 

//verifica se la stringa è una data valida (formato: mm/dd/yyyy)

function isDate(dateStr) {

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

if (matchArray == null) {
	alert("Formato data non corretto");
	return false;
}

	month = matchArray[1]; // p@rse date into variables
	day = matchArray[3];
	year = matchArray[5];

	if (month < 1 || month > 12) { // check month range
		alert("Il mese deve essere compreso tra 1 e 12.");
		return false;
	}

	if (day < 1 || day > 31) {
		alert("I giorni devono essere compresi tra 1 e 31.");
		return false;
	}

	if ((month==4 || month==6 || month==9 || month==11) && day==31) {
		alert("Il mese inserito non ha 31 giorni!")
		return false;
	}

	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("Febbraio" + year + " non ha " + day + " giorni!");
			return false;
		}
	}
	return true; // date is valid
}

 
function changeImgSrc(img,newsrc){
	img.src = newsrc;
}

function isInt(x) { 
   var y=parseInt(x); 
   if (isNaN(y)) return false; 
   return x==y && x.toString()==y.toString(); 
 } 
