function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_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=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

//------- BROWSER FUNCTION ----------------------------------------------

function checkBrowser(){

  if( document.layers ){
    alert("Browser non Supportato!");
    return false;
  }
  return true;
}

//------- STRING FUNCTION ----------------------------------------------

String.prototype.ltrim = function () { return this.replace(/^\s*/, "");}
String.prototype.rtrim = function () { return this.replace(/\s*$/, "");}
String.prototype.trim  = function () { return this.ltrim().rtrim(); }

function prepareSql(s){

  c = new Array();
  c[0] = "\'";
  c[1] = "\"";

  str = "";

  for (i=0; i<c.length; i++) {
    index = -1;
    fromIndex = 0;

    while( (index = s.indexOf( c[i], fromIndex ) ) > -1 ){

      s = s.substring(0, index) + "\\" + s.substring(index);

      fromIndex = index + 2;
    }
  }

  return s;
}

//------- PWD FUNCTIONS ------------------------------------------------

function isValidPassword( valore ){

  flag = true;

  if( valore.length<6 || valore.length >8 ){

    return false;
  }

  for(i=0; i<valore.length; i++){

    if(
        !((valore.charCodeAt(i)>=97 && valore.charCodeAt(i)<=122) ||
          (valore.charCodeAt(i)>=65 && valore.charCodeAt(i)<=90) ||
          (valore.charCodeAt(i)>=48 && valore.charCodeAt(i)<=57) ||
          valore.charCodeAt(i)==95)
      ){
      flag = false;
    }
  }
  return flag;
}

function isValidUsername( valore ){

  flag = true;

  for(i=0; i<valore.length; i++){

    if(
        !((valore.charCodeAt(i)>=97 && valore.charCodeAt(i)<=122) ||
          (valore.charCodeAt(i)>=65 && valore.charCodeAt(i)<=90) ||
          (valore.charCodeAt(i)>=48 && valore.charCodeAt(i)<=57) ||
          valore.charCodeAt(i)==95)
      ){
      flag = false;
    }
  }
  return flag;
}

//------------MAIL VALIDATION-----------

function emailvalidation(value){
	
/*
  apos   = value.indexOf("@");
  dotpos = value.lastIndexOf(".");
  lastpos= value.length-1;

  if (apos<1 || dotpos-apos<2 || lastpos-dotpos>3 || lastpos-dotpos<2){
   alert("Mail non valida");
   return false;
  }

  return true;
*/
  	

  	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	if (filter.test(value)){
		testresults=true;
	}else{
		//alert("Please input a valid email address!");
		testresults=false;
	}
	
	//alert(testresults);
	return (testresults);
}

//------- NUMBER FUNCTION ----------------------------------------------

function isNumber( val ){

  if( isNaN( val ) || val.trim() == "" ){
    return false;
  }
  return true;
}

function FormatNumber(Number,Decimals,Separator)
{
 // **********************************************************
 // Placed in the public domain by Affordable Production Tools
 // March 21, 1998
 // Web site: http://www.aptools.com/
 //
 // November 24, 1998 -- Error which allowed a null value
 // to remain null fixed. Now forces value to 0.
 //
 // October 28, 2001 -- Modified to provide leading 0 for fractional number
 // less than 1.
 //
 // This function accepts a number to format and number
 // specifying the number of decimal places to format to. May
 // optionally use a separator other than '.' if specified.
 //
 // If no decimals are specified, the function defaults to
 // two decimal places. If no number is passed, the function
 // defaults to 0. Decimal separator defaults to '.' .
 //
 // If the number passed is too large to format as a decimal
 // number (e.g.: 1.23e+25), or if the conversion process
 // results in such a number, the original number is returned
 // unchanged.
 // **********************************************************
 Number += ""          // Force argument to string.
 Decimals += ""        // Force argument to string.
 Separator += ""       // Force argument to string.
 if((Separator == "") || (Separator.length > 1))
  Separator = "."
 if(Number.length == 0)
  Number = "0"
 var OriginalNumber = Number  // Save for number too large.
 var Sign = 1
 var Pad = ""
 var Count = 0
 // If no number passed, force number to 0.
 if(parseFloat(Number)){
  Number = parseFloat(Number)} else {
  Number = 0}
 // If no decimals passed, default decimals to 2.
 if((parseInt(Decimals,10)) || (parseInt(Decimals,10) == 0)){
  Decimals = parseInt(Decimals,10)} else {
  Decimals = 2}
 if(Number < 0)
 {
  Sign = -1         // Remember sign of Number.
  Number *= Sign    // Force absolute value of Number.
 }
 if(Decimals < 0)
  Decimals *= -1    // Force absolute value of Decimals.
 // Next, convert number to rounded integer and force to string value.
 // (Number contains 1 extra digit used to force rounding)
 Number = "" + Math.floor(Number * Math.pow(10,Decimals + 1) + 5)
 if((Number.substring(1,2) == '.')||((Number + '')=='NaN'))
  return(OriginalNumber) // Number too large to format as specified.
 // If length of Number is less than number of decimals requested +1,
 // pad with zeros to requested length.
 if(Number.length < Decimals +1) // Construct pad string.
 {
  for(Count = Number.length; Count <= Decimals; Count++)
   Pad += "0"
 }
 Number = Pad + Number // Pad number as needed.
 if(Decimals == 0){
  // Drop extra digit -- Decimal portion is formatted.
  Number = Number.substring(0, Number.length -1)} else {
  // Or, format number with decimal point and drop extra decimal digit.
 Number = Number.substring(0,Number.length - Decimals -1) +
          Separator +
          Number.substring(Number.length - Decimals -1,
          Number.length -1)}
 if((Number == "") || (parseFloat(Number) < 1))
  Number="0"+Number // Force leading 0 for |Number| less than 1.
 if(Sign == -1)
  Number = "-" + Number  // Set sign of number.
 return(Number)
}

//------- DATE FUNCTION ----------------------------------------------

var today = new Date();

function isDate(aa, mm, gg){

 if( aa.length == 4 ){

   d = new Date(aa, mm, gg);
   return ((d.getDate()==gg) && (d.getMonth()==mm));
 }
 return false;
}


  /** Carica il select dei Giorni */
function loadGG(obj){

  for(i=0; i<31; i++){
    obj.options[i] = new  Option( i+1 , i+1);
  }
}

  /** Carica il select dei Mesi */
function loadMM(obj){

  for(i=0; i<12; i++){

    obj.options[i] = new  Option( i+1 , i+1);
  }
}

  /** Carica il select degli Anni */
function loadAA(obj){

  var anno = today.getYear() - 2;
  if( anno < 1000 ){ anno = anno + 1900; } //x Netscape

  for(var i=0; i<5; i++){

    obj.options[i] = new  Option( anno + i , anno + i);
  }
}

  /** Carica il select degli Anni con un Range Elevato*/
function loadAALarge(obj){

  var anno = today.getYear()+3;
	var j=0;
  if( anno < 1000 ){ anno = anno + 1900; } //x Netscape

  for(var i=2003; i<anno; i++){

    obj.options[j] = new  Option( i , i);
		j++
  }
}

  /** Imposta la data che gli viene passata */
function setDate(dateObj, ggObj, mmObj, aaObj){

  for(var i=0; i<ggObj.options.length; i++){

    if( ggObj.options[i].value == (""+(dateObj.getDate())) ){

      ggObj.options[i].selected = true;
    }
  }

  for(var i=0; i<mmObj.options.length; i++){

    if( mmObj.options[i].value == (""+(dateObj.getMonth()+1)) ){
      mmObj.options[i].selected = true;
    }
  }

  for(var i=0; i<aaObj.options.length; i++){

    var anno = dateObj.getYear();
    if( anno < 1000 ){ anno = anno + 1900; } //x Netscape

    //if( aaObj.options[i].value == (dateObj.getYear()+1900) ){
    if( aaObj.options[i].value == (""+anno) ){
      aaObj.options[i].selected = true;
    }
  }
}

//--------------------------------------------------------------

