

var Utils = new function () {
  //  Debug.msg( "Start Utils" );


  this.getStyleClass = function (className) {
    for (var s = 0; s < document.styleSheets.length; s++)
      {
	if(document.styleSheets[s].rules)
	  {
	    for (var r = 0; r < document.styleSheets[s].rules.length; r++)
	      {
		if (document.styleSheets[s].rules[r].selectorText == '.' + className)
		  {
		    return document.styleSheets[s].rules[r];
		  }
	      }
	  }
	else if(document.styleSheets[s].cssRules)
	  {
	    for (var r = 0; r < document.styleSheets[s].cssRules.length; r++)
	      {
		if (document.styleSheets[s].cssRules[r].selectorText == '.' + className)
		  return document.styleSheets[s].cssRules[r];
	      }
	  }
      }
    return null;
  };




  this.str2docs = function ( cont ) {
    var docs = new Array();
    docs = cont.split( /\n---\n/ );
    return docs;
  };




  this.parseCsv = function ( cont, recFunc ) {
    var rows = cont.split( /\n/ );
    for ( var ii = 0; ii<rows.length; ++ii ) {
      var rwPrsFn = function () {
	if ( rows[ii] ) {
	  var rec = splitStr( rows[ii], "|" );
	  recFunc( rec );
	}
      };
      rwPrsFn();
    };
  };


  this.foreach = function ( list, lFn ) {
    for ( var idx in list ) {
      if ( typeof( list[idx] ) != "function" ) {
	lFn( idx, list[idx] );
      }
    }
  };


  this.cloneList = function ( list ) {
    var nl = new Array();
    for ( var idx in list ) {
      if ( typeof( list[idx] ) != "function" ) {
	nl[idx] = list[idx];
      }
    }
    return nl;
  };


  this.ringList = function ( bList ) {
    var intIdx = 0;
    var vlIdx = new Array();
    for ( var inv in bList ) {
      if ( typeof( bList[inv] ) != "function" ) {
	vlIdx[ intIdx ] = inv;
	++intIdx;
      }
    }
    intIdx = 0;

    var itr = function () {
      var key = vlIdx[ intIdx ];
      var val = bList[ key ];
      ++intIdx;
      if ( intIdx >= bList.length ) {
	intIdx = 0;
      }

      return [ key, val ];
    };

    return itr;
  };


  this.joinList = function ( dstList, newList ) {
    for ( var idx in newList ) {
      if ( typeof( newList[idx] ) != "function" ) {
	dstList.push( newList[idx] );
      }
    }
    return dstList;
  };



  this.crTextFilt = function ( filtTxtVal ) {
    var preRem = /^\s+/;
    filtTxtVal = filtTxtVal.replace( preRem, "");
    var sufRem = /\s+$/;
    filtTxtVal = filtTxtVal.replace( sufRem, "");
    var tfFn;
    if ( filtTxtVal ) {
      var filtRe = new RegExp(filtTxtVal, "i");
      tfFn = function ( txt ) {	return filtRe.test( txt ); }
    }
    else {
      tfFn = function ( txt ) {	return true; }
    }

    return tfFn;
  };



  this.getEpoch = function ( year, month, day, hr, min, sec ) {
    year = defined( year ) ? year : 0;
    month = defined( month ) ? month : 0;
    day = defined( day ) ? day : 0;
    hr = defined( hr ) ? hr : 0;
    min = defined( min ) ? min : 0;
    sec = defined( sec ) ? sec : 0;
    var dt = new Date();
    dt.setSeconds( sec );
    dt.setMinutes( min );
    dt.setHours( hr );
    dt.setDate( day );
    dt.setMonth( month );
    dt.setYear( year );

    return parseInt((dt.getTime()-dt.getMilliseconds())/1000);
  };



  var splitStr = function ( str, spl ) {
    var sArr = new Array();

    var currStr = "";
    for ( var ip = 0; ip < str.length; ++ip ) {
      var currChr = str.charAt( ip );
      if ( currChr == spl ) {
	sArr.push( currStr );
	currStr = "";
      }
      else {
	currStr = currStr + currChr;
      }
    }
    sArr.push( currStr );

    return sArr;
  };


  this.idCrtr = function () {
    var idLs = new Array();
    var idFn = function ( idRm ) {
      if ( typeof(idRm) != "undefined" ) {
	idLs[idRm] = 0;
      }
      else {
	var fcn = 0;
	while ( idLs[fcn] != 0 && typeof( idLs[fcn] ) != "undefined" ) ++fcn;
	idLs[fcn] = 1;
	return fcn;
      }
    };
    return idFn;
  };


  this.elWidth = function ( elm ) {
//     Debug.msg( "EL Width: "+elm.clientWidth );
//     Debug.msg( "EL Width: "+elm.childNodes );
//     for ( var ii in elm.childNodes ) {
//       Debug.msg( "EL Width: "+elm.childNodes[ii].clientWidth );

//     }

    var cDisp = elm.style.display;
    var cLf = elm.style.left;
    var cRt = elm.style.top;
    var cTp = elm.style.right;
    var cBt = elm.style.botton;

    elm.style.left = -5000;
    elm.style.top = -5000;


    elm.style.display = "block";

    var pNd = elm.parentNode;

    //    Debug.msg( "PNOD: "+typeof( pNd.toString ) );


    if ( pNd != null ) pNd.removeChild( elm );
    document.body.appendChild( elm );
    var eWd = elm.clientWidth;
    document.body.removeChild( elm );
    if ( pNd != null ) pNd.appendChild( elm );

    elm.style.display = cDisp;
    elm.style.left = cLf;
    elm.style.top = cRt;
    elm.style.right = cTp;
    elm.style.botton = cBt;



    return eWd;
  };


}



