// invio ad una URL
function goToUrl(str)
{
	location.replace(str);	
}
// TROVA UN OGGETTO A PARTIRE DALL'ID
// n: ID DA TROVARE
// d: DOCUMENTO OZIONALE (window.document da cui far partire la ricerca)
function findObj(n, d) 
{ //v4.01
  var p,i,x;  
  if(!d) d=document; 
  if((p=n.indexOf("?"))>0&&parent.frames.length) 
  {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);
  }
  if(!(x=d[n])&&d.all) 
  	x=d.all[n]; 
  
  for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  	
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=findObj(n,d.layers[i].document);
  if(!x && d.getElementById) 
  {
  	x=d.getElementById(n); 
  }
  return x;
}


function removeConfirm()
{
   var str1="Confermi la rimozione dei dati ?";
   var str2="Attenzione il processo è irreversibile ... proseguire ?";
   if(confirm(str1) && confirm(str2)) return true;
   return false;
}

function singleConfirm(mode)
{
   var str="";
   //alert("mode: "+mode);
   if (mode.slice(0,3) == "UPD") str="Confermi la modifica dei dati ?";
   else str="Confermi l'inserimento dei dati ?";
   if (confirm(str)) return true;
   return false;
}

function checkEmpty( obj, label ) {
  var mt=obj.value;
  if (mt.length<1) {
    alert("Il campo "+label+" non puo' essere vuoto.");
    obj.focus();
    return false;
  }
  else { 
    return true; 
  }
}

function checkEmptyOr( obj1, label1 ,obj2 , label2 ) {
  var mt1=obj1.value;
  var mt2=obj2.value;
  if (mt1.length<1 && mt2.length<1 ) {
    alert("Almeno uno tra i campi "+label1+" e "+label2+" devono essere riempiti");
    obj2.focus();
    return false;
  }
  else { 
    return true; 
  }
}



function isEmpty( obj ) {
  var mt=obj.value;
  if (mt.length<1)
    return true;
  else 
    return false; 
}


function checkMail( obj, label ) {
  var mt=obj.value;
  sw = true;
  var parts = mt.split('@');
  if (
      (parts.length != 2) ||
      (mt.search(' ')!=-1) ||
      (mt.search('/')!=-1) ||
      (mt.search('&')!=-1) ||
      (mt.search('\\?')!=-1) ||
      (mt.search(':')!=-1) 
     ) {
    alert("Il campo '"+label+"' non risulta una email valida."); 
    obj.focus();
    sw = false;
  }
  return sw;
}

function checkIntegerNum( mt ) {
   
    for( i=0; i<mt.length; i++ ) {
      ch = mt.charAt(i);
      if ( ! ((ch >= "0") && (ch <= "9")) ) return false;
    }
    return true;
}

function checkInteger(obj,label){
   var mt=obj.value;
   if (!checkIntegerNum(mt)){
      alert("Il campo "+label+" deve essere un intero.");
      obj.focus();
      return false;
   }
   return true;
}

function checkPositiveInteger(obj,label)
{
	var mt=obj.value;
	if 	(checkIntegerNum(mt) && parseInt(mt,10)>0)
		return true;
	
	alert("Il campo "+label+" deve essere un intero positivo.");
	obj.focus();
	return false;
}


function checkDate( obj, label ) {
  var mt=obj.value;
  sw = true;
  if (!isDate(mt,'dd/MM/yyyy')) {
    alert("Per il campo '"+label+"' la data deve essere \nvalida e nel formato 'GG/MM/AAAA'.");
    obj.focus();
    sw = false;
  }
  return sw;
}


function checkMinSize( obj, label, size ) 
{
  var mt=obj.value;
  sw = true;
  if (mt.length < size) {
    alert("Il campo '"+label+"' deve contenere almeno "+size+" caratteri."); 
    obj.focus();
    sw = false;
  }
  return sw;
}

function checkExactSize( obj, label, size ) {
  var mt=obj.value;
  sw = true;
  if (mt.length != size) {
    alert("Il campo '"+label+"' contiene "+mt.length+" caratteri,\nne deve contenere "+size+"."); 
    obj.focus();
    sw = false;
  }
  return sw;
}

function checkMaxSize( obj, label, size ) {
  var mt=obj.value;
  sw = true;
  if (mt.length > size) {
    alert("Il campo '"+label+"' contiene "+mt.length+" caratteri,\nne puo'contenere massimo "+size+"."); 
    obj.focus();
    sw = false;
  }
  return sw;
}

// controlla un orario nel formato hh.mm
function checkHour(obj, label)
{
	var sep='.';
	var st=obj.value;
	if (st.indexOf(sep) == -1)
	{
		alert("Il campo '"+label+"' deve essere un orario nel formato HH.MM.");
		obj.focus();
		return false;
	}
	var arr_HH_MM=st.split(sep);
	
	if (		arr_HH_MM.length != 2 || 
			arr_HH_MM[0].length != 2 || 
			arr_HH_MM[1].length != 2 ||
			
			! checkIntegerNum(arr_HH_MM[0]) ||
			parseInt(arr_HH_MM[0]) > 23 ||
			parseInt(arr_HH_MM[0]) < 0   ||
			
			! checkIntegerNum(arr_HH_MM[1]) ||
			parseInt(arr_HH_MM[1]) > 59 ||
			parseInt(arr_HH_MM[1]) < 0
		
		)
	{
		alert("Il campo '"+label+"' deve essere un orario nel formato HH.MM.");
		obj.focus();
		return false;
	}	
		
	return true;
}

// controlla se è un float nel formato [+-]xx[.yyy]
function checkFloatNum( mt ) {
   var sep='.';
   
   if( mt.split(sep).length > 2) return false;
   
   // estraggo il segno che deve sempre essere in testa
   if (mt.charAt(0) == '+' || mt.charAt(0) == '-')
   	mt=mt.slice(1);
   	
    for( i=0; i<mt.length; i++ ) {
      ch = mt.charAt(i);
      if ( ! ((ch >= "0") && (ch <= "9")) &&  ch != '.') return false;
    }
    return true;
}

// controlla se un campo è float
function checkFloat(obj,label)
{
	var mt=obj.value;
	if ( !checkFloatNum( mt ) ) {
		alert("Il campo '"+label+"' deve essere un float nel formato [+-]xxx[.yyy].");
		return false;
	}
	return true;
	
}
function openWin(action,win,w,h) {
	var wf = "";	
	wf = wf + "width=" + w;
	wf = wf + ",height=" + h;
	wf = wf + ",resizable=" + "yes";
	wf = wf + ",scrollbars=" + "yes";
	wf = wf + ",menubar=" + "no";
	wf = wf + ",toolbar=" + "no";
	wf = wf + ",directories=" + "no";
	wf = wf + ",location=" + "no";
	wf = wf + ",status=" + "no";
	window.open(action,win,wf);
}

