/*-------------------------------------------------------------------------------------------------------
                                  FUNZIONI DI UTILITA'
-------------------------------------------------------------------------------------------------------*/

//--------------------------------------------------------------------------------------------------
// Abbinata all'evento onKeyPress di un campo text, consente di inserire solo valori numerici
//--------------------------------------------------------------------------------------------------
function geu_soloNumeri(Tasto,Campo) {
	//estrae il codice ascii
	var CodiceSuc = (Tasto.which) ? Tasto.which : Tasto.keyCode;
	if ((CodiceSuc == 44  || CodiceSuc == 46) &&
		(Campo.value.indexOf(',') != -1 || Campo.value == "")) { //Non consente di inserire pių di una virgola
		Tasto.cancelBubble=true;
		Tasto.returnValue = false;
		return false;
	} else {
		if ((CodiceSuc < 47 || CodiceSuc > 57) && 			//controllo numericita'
			CodiceSuc != 9 &&
			CodiceSuc != 8 &&
			CodiceSuc != 44) {
			// Se e' stato digitato il punto lo sostituisce con una virgola
			if (CodiceSuc == 46) {
				Campo.value = Campo.value+",";
			}
			Tasto.cancelBubble=true;
			Tasto.returnValue = false;
			return false;
		} else {
			return CodiceSuc;
		}
	}
}

//--------------------------------------------------------------------------------------------------
// Come soloNumeri, ma consente di inserire solo numeri interi
//--------------------------------------------------------------------------------------------------
function geu_soloNumeriInteri(Ievt) {
	var charCode = (Ievt.which) ? Ievt.which : Ievt.keyCode
	if ((charCode > 31 && (charCode < 48 || charCode > 57))&& ((!charCode ==37) ||(!charCode ==39)))
	    return false;

    return true;
}
//--------------------------------------------------------------------------------------------------
// Consente di inserire solo caratteri alfanumerici, convertendo tutto in MAINUSCOLO
//--------------------------------------------------------------------------------------------------
function geu_soloAlfaNumeric(Ievt) {
	var charCode = (Ievt.which) ? Ievt.which : Ievt.keyCode

    //Se sono numeri
    if (((charCode<=31) || ((charCode >= 48) && (charCode <= 57)))|| (charCode ==37) ||(charCode ==39) ||(charCode ==46) )
	    return true;
    else
        //Sono lettere MAIUSCOLE
        if (((charCode >= 65) && (charCode <= 90))||((charCode >= 97) && (charCode <= 122)))
            return true;
        else
            return false;

}
//--------------------------------------------------------------------------------------------------
// Formatta un numero reale con i separatori dei decimali e con la virgola
//--------------------------------------------------------------------------------------------------
function gev_formatNumber(mystring){
	if (!isNaN(mystring)) {
		var myNewString
		mystring = mystring.toString();
		// se c'e' il punto decimale separa la parte intera
		decimale=",00";
	 	p = mystring.indexOf(".");
		if (p>0) {
			decimale=","+mystring.substr(p+1);
			mystring=mystring.substr(0,p);
		 }
		// tratto la parte intera
	 	if (mystring.length>3) {
	  		for(i=3;i < mystring.length; i = i + 3) {
	  			p = mystring.length
				myNewString = mystring.substring(0,p - i) + "." + mystring.substring(p - i, p)
				mystring = myNewString ;
	   			i++;
	  		}
	 	} else {
			myNewString = mystring;
		}
		return myNewString+decimale;
	} else {
		return "0,00";
	}
}

//--------------------------------------------------------------------------------------------------
// Elimina gli spazi superflui in una stringa
//--------------------------------------------------------------------------------------------------
function geu_trim(IString) {
   return IString.replace(/^\s*|\s*$/g,"");
}


//--------------------------------------------------------------------------------------------------
// Trasforma il contenuto dei campi di un form in una stringa, utilizzando la convensione di tipo
// http GET (?campo1=valore1&campo2=valore2&campo3...).
// La funzione č disponibile in tre versioni:
// 		geu_formToStringByName			Accetta in input il nome del form
// 		geu_formToStringById			Accetta in input l'id del form
// 		geu_formToStringByObject		Accetta in input il riferimento all'oggetto form
//--------------------------------------------------------------------------------------------------
function geu_formToStringByName(IFormName){
    return geu_formToStringByObject(document.forms[IFormName]);
}

function geu_formToStringByID(IFormId){
   return geu_formToStringByObject(document.getElementById(IFormId));
}

function geu_formToStringByObject(IForm){
    var WQueryString='';
    var WConcat='';

    if (IForm!=null){
      WQueryString+='?';
      //Scorre tutti gli elementi del form
      for (i = 0; i < IForm.elements.length; i++) {
          WValue="";
          WFieldName = IForm.elements[i].name;
          if ((IForm.elements[i].type == 'radio')||(IForm.elements[i].type == 'checkbox')){
            if (IForm.elements[i].checked) {
				WValue = IForm.elements[i].value
			}
		  } else {
			if (IForm.elements[i].value !== "") {
				WValue = IForm.elements[i].value;
            } else {
            	WValue = " ";
			}
		  }

          if (i!=0)
              WConcat='&';

          if (WValue!=="") {
            WQueryString+= WConcat + encodeURIComponent(WFieldName) + "=" + encodeURIComponent(WValue);
		  }

      }
    }
    return (WQueryString);
}

// Apre una finestra di help
function geu_openHelpWindow(Type, HelpId, HelpKey) {
	window.open("r_help.php?GType=" + Type + "&GHelpId=" + HelpId + "&GHelpKey=" + HelpKey, '' , 'left=100,top=100,height=250,width=300,toolbar=0,location=0,directories=0,resizable=no,menuBar=0,resizable=1,status=0');
}

// Apre una finestra di Popup
function geu_openPopup(IUrl, IWidth, IHeight, ITop, ILeft, IName) {
  var int_windowLeft = (ILeft) ? ILeft : (screen.width - IWidth) / 2;
  var int_windowTop = (ILeft) ? ILeft : (screen.height - IHeight) / 2;
  var obj_window= window.open(IUrl, IName , 'left='+ int_windowLeft + ',top='+ int_windowTop + ',height='+ IHeight + ',width=' + IWidth + 'toolbar=0,location=0,directories=0,resizable=1,menuBar=0,status=0,scrollbars=1');
  if (parseInt(navigator.appVersion) >= 4)
    obj_window.window.focus();
}

//Testa la correttezza formale della password( Lunghezza, caratteri accettati)
function geu_testPadsword(IPass){
        IPass= IPass.replace(/\s+$|^\s+/g,"");
        var WReg= new RegExp("^[a-zA-Z0-9\-\_\:\;\.\,\=\@\%\&]{6,20}$");
        return WReg.test(IPass);
}
//-------------------------------------------------------------------------------------------------------

//------------------------------------------------------------------------------
function geu_navigateTo(sURL,target) {
  if(!sURL)
      return false;
  if ((target == null) || (target=="")) {	target = '_self'  }
  if (target == '_self') {
    window.location.replace(sURL);
    return false;
  }
  if (target == '_top') {
    top.window.location.replace(sURL);
    return false
  }
  if (target =='_parent') {
    parent.window.location.replace(sURL);
    return false;
  }
  if (target == '_blank' || parent.frames.length < 1) {
    window.open(sURL, target);
    return false;
  } else {
    if (parent.frames[target])
        parent.frames[target].location.replace(sURL);
    else
        window.open(sURL, target);
    return false;
  }
}
//------------------------------------------------------------------------------
//funzione che segue il Logout
function gen_logout(){
    ajaxReq('r_logount_ajax.php', '', null)
}

