// ================================================
// Funzioni JavaScript
// Copyright 2001 ConsulTes Snc (info@consultes.it)
// ================================================

////////////////////////////
// TORNA A PAGINA PRECEDENTE
////////////////////////////
function ctIndietro() {
	history.back();
}

////////////////////////////
// TORNA A PAGINA PRECEDENTE
////////////////////////////
function ctIndietro2() {
	history.go(-2);
}

///////////////////////////
// APERTURA FINESTRA POP-UP
///////////////////////////
function ctWinOpen(url, titolo, proprieta) {
	finestra = window.open(url,titolo,proprieta);
}

///////////////////////////
// CHIUSURA FINESTRA POP-UP
///////////////////////////
function ctWinClose() {
	window.close();		
}

//////////////////////////////
// AGGIUNGE PAGINA A PREFERITI
//////////////////////////////
function ctPreferiti(url, testo) {
	window.external.AddFavorite(url, testo);
}

////////////////
// STAMPA PAGINA
////////////////
function ctStampa() {
	bV = parseInt(navigator.appVersion);
	if (bV >= 4) window.print();
}

//////////////////
// CONTROLLO TESTO
//////////////////
function ctChkTesto(testo, messaggio) {
	if(testo == "") {
		return messaggio + "\n";
	} else {
		return "";
	}
}

///////////////////
// CONTROLLO NUMERO
///////////////////
function ctChkNumero(stringa, minimo, massimo, messaggio) {
	if(stringa != "") {
		var numero = parseFloat(stringa);
		var ok = true;
		if (isNaN(numero)) ok = false;
		if(ok && ((minimo != 0 || massimo != 0) && (numero < minimo || numero > massimo))) ok = false;
		if(!ok) {
			return messaggio + "\n";
		} else {
			return "";
		}
	} else {
		return "";
	}
}

/////////////////////////////
// CONTROLLO INDIRIZZO E-MAIL
/////////////////////////////
function ctChkEmail(email, messaggio) {
	if(email != "") {
		var regola = /^[A-Za-z0-9._-]+@([A-Za-z0-9][A-Za-z0-9_-]+.)+[a-z]+$/;
		var ok = regola.test(email);
		if(!ok) {
			return messaggio + "\n";
		} else {
			return "";
		}
	} else {
		return "";
	}
}

/////////////////////
// CONTROLLO PASSWORD
/////////////////////
function ctChkPwd(pwd1, pwd2, messaggio) {
	if(pwd1 != "" && pwd1 != pwd2) {
		return messaggio + "\n";
	} else {
		return "";
	}
}

////////////////////////////
// CONTROLLO SELEZIONE COMBO
////////////////////////////
function ctChkSelect(selezione, messaggio) {
	if(selezione == 0) {
		return messaggio + "\n";
	} else {
		return "";
	}
}

/////////////////////////////////////
// CONTROLLO SELEZIONE LISTA MULTIPLA
/////////////////////////////////////
function ctChkSelectM(selezione, messaggio) {
	if(selezione < 0) {
		return messaggio + "\n";
	} else {
		return "";
	}
}

/////////////////
// CONTROLLO DATA
/////////////////
function ctChkData(data, minimo, messaggio, schema) {
	if(data != "") {
		var d = data.replace(/\D/g,"/");			// normalizzazione con le barre
		var a = d.split("/");						// separazione gg mm aa
		if (schema) {
			var s = schema.split(",");				// separazione a m g
			var sy = s[0]-1;
			var sm = s[1]-1;
			var sd = s[2]-1;
		} else {
			var sy = 2;
			var sm = 1;
			var sd = 0;
		}
		for (i=0;i<=2;i++) {a[i] = parseFloat(a[i]);}
		var m = new Array(0,31,28,31,30,31,30,31,31,30,31,30,31)
		m[2] += (a[sy]%4==0);						// rettifica mese per anno bisestile
		ok = false;
		if (a[sm] >= 0 && a[sm] <= 12) {			// verifica mese
			if (a[sd] >= 1 && a[sd] <= m[a[sm]]) {	// verifica giorno
				ok = true;
			}
		}
		if(!ok) {
			return messaggio + "\n";
		} else {
			return "";
		}
	} else {
		return "";
	}
}

////////////////
// CONTROLLO ORA
////////////////
function ctChkOra(ora, messaggio) {
	if(ora != "") {
		var d = ora.replace(/\D/g,":");				// normalizzazione con i puntini
		var a = d.split(":");						// separazione hh mm ss
		for (i=0;i<=2;i++) {a[i] = parseFloat(a[i]);}
		ok = false;
		if (a[0] >= 0 && a[0] <= 23) {				// verifica ora
		if (a[1] >= 0 && a[1] <= 59) {				// verifica minuti
		if (a[2] >= 0 && a[2] <= 59) {				// verifica secondi
			ok = true;
		}
		}
		}
		if(!ok) {
			return messaggio + "\n";
		} else {
			return "";
		}
	} else {
		return "";
	}
}

////////////////////////////
// VISUALIZZAZIONE MESSAGGIO
////////////////////////////
function ctMessaggio(messaggio) {
	if(messaggio.length > 0) {
		alert(messaggio);
		return false;
	} else {
		return true;
	}
}

////////////////////////
// CONFERMA ELIMINAZIONE
////////////////////////
function ctElimina(url, messaggio) {
	azione=confirm(messaggio);
	if (azione == true) {
		document.location = url;
	}
}

/////////////////////////
// ROLLOVER RIGHE TABELLA
/////////////////////////
function ctRollOver(id, colore) {
	if (document.layers) {
		document.layers[id].bgColor=colore;
	} else {
		if (document.all) {
			document.all[id].style.background=colore;
		}
	}
}

///////////////////////
// FOCUS CAMPI FORM
///////////////////////
function ctFocus(elemento, stato) {
	colore = (stato==1) ? '#FFFFBB' : 'white';
	elemento.style.backgroundColor=colore;
}

///////////
// CONFERMA
///////////
function ct_confirm(url, messaggio) {return ctElimina(url, messaggio)}

////////////////////////////////////////////
// SELEZIONA / DESELEZIONA LE RIGHE DEL GRID
////////////////////////////////////////////
function ctGlobal(modulo, check) {
	var elts      = document.forms[modulo].elements['globals[]'];
	var elts_cnt  = (typeof(elts.length) != 'undefined')
                  ? elts.length
                  : 0;
	if (elts_cnt) {
        for (var i = 0; i < elts_cnt; i++) {
			elts[i].checked = check;
        }
    } else {
        elts.checked        = check;
    }

    return true;
}

////////////
// CONTROLLI
////////////
function ct_check_mandatory(campo, messaggio) {return ctChkTesto(campo.value, messaggio)}
function ct_check_number(stringa, messaggio) {return ctChkNumero(stringa.value, 0, 0, messaggio)}
function ct_check_range(stringa, minimo, massimo, messaggio) {return ctChkNumero(stringa.value, minimo, massimo, messaggio)}
function ct_check_email(email, messaggio) {return ctChkEmail(email.value, messaggio)}
function ct_check_web(url, messaggio) {return ''}
function ct_check_ftp(url, messaggio) {return ''}
function ct_check_password(pwd1, pwd2, messaggio) {return ctChkPwd(pwd1.value, pwd2.value, messaggio)}
function ct_check_select(selezione, messaggio) {return ctChkSelect(selezione.selectedIndex, messaggio)}
function ct_check_date(data, minimo, messaggio, schema) {return ctChkData(data.value, minimo, messaggio, schema)}
function ct_check_time(ora, messaggio) {return ctChkOra(ora.value, messaggio)}

// ================================================
// Funzioni JavaScript non proprietarie
// Estratto dall'ambiente phpMyAdmin
/* $Id: functions.js,v 1.30 2003/01/14 15:24:27 nijel Exp $ */
// ================================================

/**
 * This array is used to remember mark status of rows in browse mode
 */
var marked_row = new Array;

/**
 * Sets/unsets the pointer and marker in browse mode
 *
 * @param   object    the table row
 * @param   interger  the row number
 * @param   string    the action calling this script (over, out or click)
 * @param   string    the default background color
 * @param   string    the color to use for mouseover
 * @param   string    the color to use for marking a row
 *
 * @return  boolean  whether pointer is set or not
 */
function setPointer(theRow, theRowNum, theAction, theDefaultColor, thePointerColor, theMarkColor)
{
    var theCells = null;

    // 1. Pointer and mark feature are disabled or the browser can't get the
    //    row -> exits
    if ((thePointerColor == '' && theMarkColor == '')
        || typeof(theRow.style) == 'undefined') {
        return false;
    }

    // 2. Gets the current row and exits if the browser can't get it
    if (typeof(document.getElementsByTagName) != 'undefined') {
        theCells = theRow.getElementsByTagName('td');
    }
    else if (typeof(theRow.cells) != 'undefined') {
        theCells = theRow.cells;
    }
    else {
        return false;
    }

    // 3. Gets the current color...
    var rowCellsCnt  = theCells.length;
    var domDetect    = null;
    var currentColor = null;
    var newColor     = null;
    // 3.1 ... with DOM compatible browsers except Opera that does not return
    //         valid values with "getAttribute"
    if (typeof(window.opera) == 'undefined'
        && typeof(theCells[0].getAttribute) != 'undefined') {
        currentColor = theCells[0].getAttribute('bgcolor');
        domDetect    = true;
    }
    // 3.2 ... with other browsers
    else {
        currentColor = theCells[0].style.backgroundColor;
        domDetect    = false;
    } // end 3

    // 4. Defines the new color
    // 4.1 Current color is the default one
	// CONSULTES: aggiunto test per currentColor null
    if (currentColor == '' || currentColor == null
        || currentColor.toLowerCase() == theDefaultColor.toLowerCase()) {
        if (theAction == 'over' && thePointerColor != '') {
            newColor              = thePointerColor;
        }
        else if (theAction == 'click' && theMarkColor != '') {
            newColor              = theMarkColor;
            marked_row[theRowNum] = true;
        }
    }
    // 4.1.2 Current color is the pointer one
    else if (currentColor.toLowerCase() == thePointerColor.toLowerCase()
             && (typeof(marked_row[theRowNum]) == 'undefined' || !marked_row[theRowNum])) {
        if (theAction == 'out') {
            newColor              = theDefaultColor;
        }
        else if (theAction == 'click' && theMarkColor != '') {
            newColor              = theMarkColor;
            marked_row[theRowNum] = true;
        }
    }
    // 4.1.3 Current color is the marker one
    else if (currentColor.toLowerCase() == theMarkColor.toLowerCase()) {
        if (theAction == 'click') {
            newColor              = (thePointerColor != '')
                                  ? thePointerColor
                                  : theDefaultColor;
            marked_row[theRowNum] = (typeof(marked_row[theRowNum]) == 'undefined' || !marked_row[theRowNum])
                                  ? true
                                  : null;
        }
    } // end 4

    // 5. Sets the new color...
    if (newColor) {
        var c = null;
        // 5.1 ... with DOM compatible browsers except Opera
        if (domDetect) {
            for (c = 0; c < rowCellsCnt; c++) {
                theCells[c].setAttribute('bgcolor', newColor, 0);
            } // end for
        }
        // 5.2 ... with other browsers
        else {
            for (c = 0; c < rowCellsCnt; c++) {
                theCells[c].style.backgroundColor = newColor;
            }
        }
    } // end 5

    return true;
} // end of the 'setPointer()' function
