/*

 *  GenFunx.js : generic function

 *

 *

 *   (c) 2001-2010 AnnunciMatrimoniali.net

 *

 *

 */



// se develompent==true fa' vedere la finestra di esecuzione EXC.php

var development=false;

var squote=unescape("%27");

var dquote=unescape("%22");

var ag_dateformat="dd.mm.yyyy";

var ag_SQLdateformat="yyyy-mm-dd";

var ag_NumberPoint=".";

var ag_NumberSepar=",";

var ag_DateSeparator=".";



// torna true le la stringa s è vuota

function strempty( s ) {

    var i;

    var retval;

    retval=true;



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

      if( s.charAt(i) != ' ' ) {

        retval=false;

        break;

      }

    }

    return( retval );

}







function showProps( ob, name )

  {

      var prop, i;

      nf =  window.open("verifica.htm", "NomedellaFinestra", "status=yes, menubar=yes, toolbar=yes, scrollbars=yes, resizable=yes, width=400, height=500");



      for( prop in ob ) {

          nf.document.writeln( name+"[\""+prop+"\"]"+" = "+ob[prop]+"<BR>");

          //document.writeln( name+"[\""+prop+"\"]"+" = "+ob[prop]+"<BR>");

      }

  }



/// ltrim ritorna la stringa s senza i blank a sinistra



function ltrim( s ) {

  while ( s.substr(0,1) == " " && s.length > 0 )

  {

    s = s.substr(1);

  }

  return s;

}



/// rtrim ritorna la stringa s senza i blank a destra



function rtrim( s ) {

  while ( s.substr(s.length-1,1) == " " && s.length > 0 )

  {

    s = s.substr(0, s.length-1 );

  }

  return s;

}



function trim( s ) {

        return ltrim( rtrim( s ));

}





// Replicate( npre, car ) : torna una stringa ripetendo ncar per nrep volte

function replicate( nrep, car )

{

  var s;

  s="";

  while( nrep-- > 0 ) s+=car;

  return s;

}





// Strzero( n, l ) : fa diventare il numero n una stringa e aggiunge zeri davanti

//                    per un totale stringa lungo l

function strzero( n, l )

{

  var s;

  s = String( n );

  //alert( "strzero s=" + s );

  s = replicate( l-s.length, '0' )+s;



  return s;

}



function togliezeridavanti( s )

{



  while ( s.substr(0,1) == "0" && s.length > 1 )

  {

    s = s.substr(1);

  }

  return s;



}



function iif( test, rettrue, retfalse ) {

        if( test )

                return rettrue;

        else

                return retfalse;

}



// validdata( c ) : c e' un campo do un form che contiene una data

//               controlla se la stringa di c.value e' una data valida

//               se si la ritorna cambiando il formato secondo la variabile separatore

//               altrimenti da un messaggio di errore e torna false



function validdata( c )

{

        var d=c.value;

        var flag=true;

        var errore=false;

        var gg, mm, aaaa;

        var sep1, sep2, posmese, posanno;

        var separatore='-';



        /// gg.mm.aaaa

        /// ggmm.aaaa

        /// ggmmaaaa

        /// 0123456789





        // controlla che ci siano i separatori



    //    alert( "test1 replicate(3, '0') " + replicate(3, '0') );

    //    alert( "test2 strzero( 4, 2 )  " + strzero( 4, 2 )  );





        sep1= d.substr(2,1);

        if( "0123456789".indexOf(sep1) >= 0  )

            posmese=2;

        else

          if( ".-/".indexOf( sep1 ) >= 0 )

            posmese=3;

          else

            errore=true;



        if ( ! errore )

        {

          // controlla che ci siano i separatori

          sep2= d.substr(5-(3-posmese),1);

          if( "0123456789".indexOf(sep2) >= 0  )

              posanno=(5-(3-posmese));

          else

            if( ".-/".indexOf( sep2 ) >= 0 )

              posanno=(5-(3-posmese))+1;

            else

              errore=true;

        }





        if( ! errore ) {

          //alert( "d.substr(posmese,2) = "+d.substr(posmese,2)+"  posmese = "+posmese+" mm="+mm);

          gg=togliezeridavanti( d.substr(0,2) ) ;

          mm=togliezeridavanti( d.substr(posmese,2) );

          aaaa=togliezeridavanti( d.substr(posanno,4) );



          //alert( "Prima  gg = "+gg+" mm = "+mm+" aaaa = "+aaaa );



          gg = parseInt( gg ) ;

          mm = parseInt( mm ) - 1;

          aaaa = parseInt( aaaa ) ;



          //alert( "Dopo  gg = "+gg+" mm = "+mm+" aaaa = "+aaaa );



          if( aaaa <= 99 ) aaaa+=2000;



          if( aaaa < 1980  || aaaa > 2200 )

                errore = true;



          if( !( gg>=1 && gg<=31 && mm >=0 && mm <= 11 ) )

            errore = true;



          if( ! errore ) {

            var testdata = new Date(aaaa, mm, gg );

            if ( isNaN(testdata) )

              errore=true;

          }

        }



        // se immettono es. 31.02.2002 il giorno cambia e va a finire al rispettivo di marzo

        //if( testdata.getDate() == undefined )

        //  errore=true;



        if( ! errore ) {

          if( gg != testdata.getDate() ) {

            //alert( "gg = "+gg +"  testdata.getDate() = "+testdata.getDate() );

            errore=true;

          }

        }



        if( errore ) {

          if( trim(d).length == 0 )

                alert( "La data non e' stata inserita !");

          else

                alert( d+" non e' una data valida!");

          //alert( testdata );

          c.value="";

        } else {

          // ricompone la stringa della data correttamente

          c.value = strzero( testdata.getDate(), 2 )+separatore+strzero( testdata.getMonth()+1, 2 ) +separatore+

                    String( testdata.getFullYear() );

        }



        //if( errore )

        //  alert( "ERRORE Giorno = "+String( gg )+" Mese = "+String( mm )+" Anno = "+String( aaaa )+

        //  "\n posmese="+String( posmese )+" posanno="+String( posanno ) );

        //else

        //  alert( "Giorno = "+String( gg )+" Mese = "+String( mm )+" Anno = "+String( aaaa) +

        //  "\n posmese="+String( posmese )+" posanno="+String( posanno ) );







        return ! errore;

}


function askngo( ask, gourl ) {

   if( confirm(ask) ) document.location.replace( gourl );

}



function goUrl( url ) {

        document.location.replace( url );

}


