function EKUtilsObject() {

  this.Debug = Debug;
  this.ClearDebugDiv = ClearDebugDiv;
  this.zeropad = PadDigits;
  this.str_repeat = str_repeat;
  this.pw = pw;
  this.date_popup = date_popup;
  this.dateGetObject = dateGetObject;
  this.handle_date_popup_select = handle_date_popup_select;
  this.mouseX = mouseX;
  this.mouseY = mouseY;
  this.popUp = popUp;
  this.screenDim = screenDimensions; // returns {X,Y} screen width/height
  this.screenCenter = screenCenter;  // returns {X,Y} center point
  this.screenVisibleCoords = screenVisibleCoords; // returns {tX,tY,bX,bY} top X/Y and bottom X/Y of visible coordinates

  function screenDimensions() {
    var w = 0;
    var h = 0;
    //IE
    if(!window.innerWidth)
    {
      //strict mode
      if(!(document.documentElement.clientWidth == 0))
      {
        w = document.documentElement.clientWidth;
        h = document.documentElement.clientHeight;
      }
      //quirks mode
      else
      {
        w = document.body.clientWidth;
        h = document.body.clientHeight;
      }
    }
    //w3c
    else
    {
      w = window.innerWidth;
      h = window.innerHeight;
    }
    return {width:w,height:h};
  }

  function screenCenter() {
    var hWnd = (arguments[0] != null) ? arguments[0] : {width:0,height:0};
    var _x = 0;
    var _y = 0;
    var offsetX = 0;
    var offsetY = 0;
    //IE
    if(!window.pageYOffset)
    {
      //strict mode
      if(!(document.documentElement.scrollTop == 0))
      {
        offsetY = document.documentElement.scrollTop;
        offsetX = document.documentElement.scrollLeft;
      }
      //quirks mode
      else
      {
        offsetY = document.body.scrollTop;
        offsetX = document.body.scrollLeft;
      }
    }
    //w3c
    else
    {
      offsetX = window.pageXOffset;
      offsetY = window.pageYOffset;
    }
    _x = parseInt( ((this.screenDim().width-hWnd.width)/2)+offsetX );
    _y = parseInt( ((this.screenDim().height-hWnd.height)/2)+offsetY );
    return{X:_x,Y:_y};
  }

  function screenVisibleCoords() {
    var scnW = this.screenDim().width;
    var scnH = this.screenDim().height;
    var ctrX = this.screenCenter().X;
    var ctrY = this.screenCenter().Y;
    var topX = parseInt( ctrX - ( scnW / 2 ) );
    var topY = parseInt( ctrY - ( scnH / 2 ) );
    var btmX = parseInt( ctrX + ( scnW / 2 ) );
    var btmY = parseInt( ctrY + ( scnH / 2 ) );
    return { tX:topX,tY:topY,bX:btmX,bY:btmY };
  }

  function PadDigits(n, totalDigits)
  {
    n = n.toString();
    var pd = '';
    if (totalDigits > n.length)
    {
      for (i=0; i < (totalDigits-n.length); i++)
      {
        pd += '0';
      }
    }
    return pd + n.toString();
  }


  function str_repeat ( input, multiplier ) {
    // Returns the input string repeat mult times
    //
    // version: 909.322
    // discuss at: http://phpjs.org/functions/str_repeat    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // *     example 1: str_repeat('-=', 10);
    // *     returns 1: '-=-=-=-=-=-=-=-=-=-='
    return new Array(multiplier+1).join(input).toString();
  }

  function Debug(output_div_id,var_name,var_value) {
    var dv = document.getElementById(output_div_id);
    if ( dv ) { dv.innerHTML += "<pre>" + var_name + ":'" + var_value + "'</pre>\n"; }
  }

  function ClearDebugDiv(output_div_id) {
    var dv = document.getElementById(output_div_id);
    if ( dv ) { dv.innerHTML = ""; }
  }

  // permission is granted to use this javascript provided that the below code is not altered
  function pw() {
    return window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth
  }
  function mouseX(evt) {
    return evt.clientX ? evt.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft) : evt.pageX;
  }
  function mouseY(evt) {
    return evt.pageY ? evt.pageY : evt.clientY + (document.documentElement.scrollTop || document.body.scrollTop)
  }
  function popUp(evt,oi) {
    if (document.getElementById) {
      var wp = pw();
      dm = document.getElementById(oi);
      ds = dm.style;
      st = ds.visibility;
      if (dm.offsetWidth) { ew = dm.offsetWidth; }
      else if (dm.clip.width) { ew = dm.clip.width; }
      if (st == "visible" || st == "show") { ds.visibility = "hidden"; }
      else {
        tv = mouseY(evt) - 220;
        lv = mouseX(evt) - (ew/4);
        if (lv < 2) { lv = 2; } else if (lv + ew > wp) { lv -= ew/2; }
        lv += 'px';tv += 'px';
        ds.left = lv;
        ds.top = tv;
        ds.visibility = "visible";
      }
    }
  }

  function dateGetObject(this_date) {
    var tObj = new Date(); // Get current date to determine timezone adder
    var dValid = ( parseInt(Date.parse(this_date)) ? true : false ); // Check if default date is valid
    var dObj;
    if ( dValid ) {
      dObj = new Date( Date.parse(this_date) + parseFloat(tObj.getTimezoneOffset()*60000) );
    }
    else {
      dObj = new Date();
    }
    //alert('DATES: T:' + tObj + '\nDATE:' + Date.parse(this_date) + '\nT OFFSET:' + tObj.getTimezoneOffset() + '\nD' + dObj);

    return dObj;
  }

  if ( typeof(GLOBAL_DATE_FORMAT) == "undefined" ) {
    var GLOBAL_DATE_FORMAT = new Array("y","m","d"); // Use only lowercase 'y','m', or 'd' in any order. -ELK
  }
  var GLOBAL_TEXT_ELEMENT;
  function date_popup(e,e_text_input,e_default_date) {
    // e_text_input is an element
    // e_default_date is a string
    if ( e_text_input.disabled ) { return false; }
    var scnVisCrds = EKUtil.screenVisibleCoords();
    var clkX = EKUtil.mouseX(e)
    var clkY = EKUtil.mouseY(e);
    var imgWidth = 310; // calendar_bg_1.png
    var imgHeight = 240; // calendar_bg_1.png
    var popupLeft = parseInt( clkX + 30 );
    var popupTop = parseInt( clkY - 20 );
    //var popupLeft = parseInt( scnVisCrds.bX - imgWidth );
    //var popupTop = parseInt( scnVisCrds.bY - imgHeight );
    if ( ( popupLeft + imgWidth ) > scnVisCrds.bX ) { popupLeft = parseInt( scnVisCrds.bX - imgWidth ); }
    if ( ( popupTop + imgHeight ) > scnVisCrds.bY ) { popupTop = parseInt( scnVisCrds.bY - imgHeight ); }
    if ( popupLeft < 0 ) { popupLeft = 0; }
    if ( popupTop < 0 ) { popupTop = 0; }
     GLOBAL_TEXT_ELEMENT = e_text_input;
      var cal_container_id = "calendar_popup"; // e_text_input.id + "_cal";
      var caldiv = document.getElementById(cal_container_id);
      if ( caldiv ) { document.body.removeChild(caldiv); } // Destroy any existing calendar
      caldiv = document.createElement( "div" );
      caldiv.id = cal_container_id;
      caldiv.style.display = "block";
      caldiv.style.top = popupTop + "px"; // MUST use "px" for FireFox to work. -ELK 20100607
      caldiv.style.left = popupLeft + "px"; // MUST use "px" for FireFox to work. -ELK 20100607
      document.body.appendChild(caldiv);
      caldiv = document.getElementById(cal_container_id);
      caldiv.style.top = popupTop;
      caldiv.style.left = popupLeft;
      var date_parts = e_text_input.value.split('-');
      var sel_date = "";
      /*
      var date_obj;
      if ( date_parts[2] != undefined ) {
        date_obj = new Date(date_parts[2],(date_parts[0]-1),date_parts[1]);
      }
      else {
        date_obj = new Date();
      }
      */
      var date_obj = this.dateGetObject(e_text_input.value); // e_default_date
      var calgrp = new YAHOO.widget.CalendarGroup( "cal1" , cal_container_id ,
        {
        pages:2,
        title:"<p class=\"date_popup_title\"><b>Please select a date</b><br/>(Click the month to enter a new month/year)</p>",
        close:true,
        iframe:true,
        navigator:true,
        pagedate:date_obj
        }
        );
      calgrp.render();
      calgrp.selectEvent.subscribe(EKUtil.handle_date_popup_select, calgrp, true);
      // Listener to show the two page Calendar when the button is clicked
      //YAHOO.util.Event.addListener("show2up", "click", calgrp.show, calgrp, true);
      return true;
  }
  function handle_date_popup_select(type,args,obj) {
    //alert("HANDLING CLICK!");
    var dlm = "/";
    if ( GLOBAL_DATE_FORMAT[0] == "y" ) { dlm = "-"; }
    var dates = args[0];
    var date = dates[0];
    var year = date[0], month = date[1], day = date[2];
    var m = EKUtil.zeropad(month,2);
    var d = EKUtil.zeropad(day,2);
    var y = EKUtil.zeropad(year,4);
    if ( ! GLOBAL_TEXT_ELEMENT.disabled ) {
      // Evaluates 'y', 'm', 'd' variables as values.
      GLOBAL_TEXT_ELEMENT.value = eval(GLOBAL_DATE_FORMAT[0]) + dlm + eval(GLOBAL_DATE_FORMAT[1]) + dlm + eval(GLOBAL_DATE_FORMAT[2]);
    }
    document.getElementById("calendar_popup").style.display = "none";
  }
  //YAHOO.example.calendar.cal1.selectEvent.subscribe(handleSelect, YAHOO.example.calendar.cal1, true);


}

var EKUtil = new EKUtilsObject();

// Shortcut function for document.getElementById()
function $$(e) {
  return document.getElementById(e);
}

