var interi = /\d/;
var alfanumerici = /[\w\&\-]/;
var browser;
var email = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;

function TestBrowser(){
	if (navigator.appName=='Microsoft Internet Explorer')
		browser = "IE";
	else
		browser = "NS";
}

function checkPiu(objEvent, obj) {
	var iKeyCode, strKey;
	if ( browser != "IE" )
		iKeyCode = objEvent.which;
	else
		iKeyCode = objEvent.keyCode;

	strKey = String.fromCharCode(iKeyCode);
	if ( ( iKeyCode != 8 ) && ( objEvent.keyCode != 9 ) ){
		if ( strKey != "" ) {
			if ( strKey == "+" ){
				  if ( obj.value != "" )
					return false;
			} else {	
				if ( !interi.test(strKey) )
					return false;
			}
		}
	}
	return true;
}

String.prototype.trim = function() {
  // skip leading and trailing whitespace
  // and return everything in between
  var x=this;
  x=x.replace(/^\s*(.*)/, "$1");
  x=x.replace(/(.*?)\s*$/, "$1");
  return x;
}

function checkNotNull(form){
	campiObbligatori = form.NotNullFieldsNODB.value.split(",");
	for (i=0; i< campiObbligatori.length; i++){
		campo = form[campiObbligatori[i].trim()].value;
		if ( campo != null )
			campo = campo.trim();
		if ( campo == "" ){
			alert("Attenzione.\nControllare che tutti i campi obbligatori siano stati inseriti.");
			form[campiObbligatori[i]].focus();
			return;
		}	
	}
	form.submit();
}

function checkPassword(form){
	//if ((form.s_password.value!=form.confirmPasswordNODB.value) || (form.s_password.value=='' || form.confirmPasswordNODB.value=='')){
	if ( (form.s_password.value.length < 6) || (form.s_password.value != form.confirmPasswordNODB.value)){
		alert('Controllare Password');
		return false;
	}
	else return true;
}

function checkEmailConfirm(e1, e2){
	if ((e1.value != e2.value)||(e1.value == '')) {
		alert("Indirizzo Email non valido.");
		e1.value='';e2.value='';
		return false;
	} 
	if (!checkEmail(e1)) {
		e1.value='';e2.value='';
		return false;
	}
	return true;
}

function checkEmail(emailField){
		if ( !email.test(emailField.value) && ( emailField.value != "" ) ) {
			alert("Email non valida.");
			emailField.value = "";
			return false;
		} else
			return true;
}

function noSpazi(objEvent) {
	var iKeyCode, strKey;

	if ( browser != "IE" ){
		iKeyCode = objEvent.which;
		strKey = String.fromCharCode(iKeyCode);
		if ( !alfanumerici.test(strKey) && ( iKeyCode != 8 ) && ( objEvent.keyCode != 9 ) || ( objEvent.keyCode == 32 ) )
			return false;
	} else {
		iKeyCode = objEvent.keyCode;
		strKey = String.fromCharCode(iKeyCode);
		if ( ( !alfanumerici.test(strKey) ) || ( objEvent.keyCode == 32 ) )
			return false;
	}	
}

function soloAlfaNumerici(objEvent) {
var iKeyCode, strKey;

	if ( browser != "IE" ){
		iKeyCode = objEvent.which;
		strKey = String.fromCharCode(iKeyCode);
		if (!alfanumerici.test(strKey) && ( iKeyCode != 8 ) && ( objEvent.keyCode != 9 ) )
			return false;
	} else {
		iKeyCode = objEvent.keyCode;
		strKey = String.fromCharCode(iKeyCode);
		if (!alfanumerici.test(strKey))
			return false;
	}	
}



function soloNumeriInteri(objEvent) {
var iKeyCode, strKey;

	if ( browser != "IE" ){
		iKeyCode = objEvent.which;
		strKey = String.fromCharCode(iKeyCode);
		if (!interi.test(strKey) && ( iKeyCode != 8 ) && ( objEvent.keyCode != 9 ) )
			return false;
	} else {
		iKeyCode = objEvent.keyCode;
		strKey = String.fromCharCode(iKeyCode);
		if (!interi.test(strKey))
			return false;
	}	
}

function CheckHour(ctr) { 
		text=ctr.value;
		var hour='';
		var minute='';
		// controllo la lunghezza
				
		if (text.length==3)
		{	
			text='0'+text;
		}
						
		if (text.length==4)
		{
			hour=text.substring(0,2);
			minute=text.substring(2,4);
		}

		if (text.length==5)
		{
			hour=text.substring(0,2);
			minute=text.substring(3,5);
		}

		var inthour='';
		var intminute='';
		inthour=parseInt(hour);
		intminute=parseInt(minute);
		if ((inthour>=0) &&  (inthour<=23) && (intminute>=0)&& (intminute<=59))
		{	//alert('ok');
			ctr.value=hour+':'+minute;
			ctr.focused=true;
			return true;
		} else {
			ctr.value='';
			alert('Formato ora non valido.  Es. 08:30');
			ctr.focused=true;
			return false;
		}			
}

		

function CheckLength(ctr,lunghezza){
	text=ctr.value;
		//alert();
	if (text.length>lunghezza)
		alert('Lunghezza campo non valida, Max'+lunghezza+' caratteri');
}


// Checks validity of all fields of form name=filtro of page= report
// and 
function cerca(){
	if ( document.filtro.data_da.value == ""){
		alert("Attenzione\nInserire la data di Inizio per la ricerca.");
		return false;
	} else {
		if ( !isDate(document.filtro.data_da) ){
			alert("Attenzione.\nData di Inizio non valida.")
			return false;
		}
	}
	if ( document.filtro.data_a.value == ""){
		alert("Attenzione\nInserire la data di Fine per la ricerca.");
		return false;
	} else {
		if ( !isDate(document.filtro.data_a) ){
			alert("Attenzione.\nData di Fine non valida.")
			return false;
		}
	}
	//url = "./reportistica/report.php?idc=" + document.filtro.idc.value + "&ids=" + document.filtro.ids.value + "&data_da=" + document.filtro.data_da.value + "&data_a=" + document.filtro.data_a.value;
	//url = "./reportistica/report.php?idc=" + document.filtro.idc.value + "&ids=" + document.filtro.ids.value + "&data_da=" + document.filtro.data_da.value+" 00:00:00" + "&data_a=" + document.filtro.data_a.value+" 23:59:59";
	url = "./popupreport.php?idc=" + document.filtro.idc.value + "&ids=" + document.filtro.ids.value + "&data_da=" + document.filtro.data_da.value+" 00:00:00" + "&data_a=" + document.filtro.data_a.value+" 23:59:59";
	launchpopup(url,"800","600");
}

function launchpopup(form,width,height){
	window.open(form,"_blank","LEFT=50px,TOP=50px,WIDTH="+width+",HEIGHT="+height+",RESIZABLE=yes,SCROLLBARS=yes,menubar=no,toolbar=no,STATUS=0");
}

function submitLogin(objEvent){
var iKeyCode, strKey;
	if ( browser != "IE" )
		iKeyCode = objEvent.which;
	else
		iKeyCode = objEvent.keyCode;
	strKey = String.fromCharCode(iKeyCode);
	if (iKeyCode == 13)
		document.login.submit();
}

function duplicatiEmail(){
	var email1;
	var email2;
	var email3;

	e1 = document.form1.s_email_from.value;
	e2 = document.form1.emailfrom1NODB.value;
	e3 = document.form1.emailfrom2NODB.value;

	if ( e1 == '' ) 
		e1 = "-1";

	if ( e2 == '' ) 
		e2 = "-2";

	if ( e3 == '' ) 
		e3 = "-3";

	var email1 = ( e1 != e2 );
	var email2 = ( e2 != e3 );
	var email3 = ( e3 != e1 );

	if ( (email1) && (email2) && (email3) )
		return true;
	alert("Attenzione.\n\rControllare le email ci sono email duplicate.");
	return false;
}