/*
  global.js
  DirecTV global js functions
  Authors: chad.lindstrom@blastradius.com, chris.nott@blastradius.com
*/

/* BEGIN: Utils Section */
function _gel(a){
    return document.getElementById ? document.getElementById(a) : null;
}
function _aevt(elm,event,func){
   if(document.attachEvent){
      elm.attachEvent("on"+event, func);
   }else if(document.addEventListener){
      elm.addEventListener(event, func, true);
   }else{
      eval(elm+".on"+event+"="+func);
   }
}
function _cel(t,id){
   var elm = document.createElement(t);
   if (id != null && id != "") {
      elm.id = id;
   }
   return elm;
}
function classAdd(e,c){
   if(e.className.length == 0){ e.className = c; }
   else { e.className = " " + c; }
}
function classRemove(e,c){
   if(e.className.length > 0){ e.className = e.className.replace(/\s*c/,"");  }
}
function getQueryVariable(variable) {
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable) {
      return pair[1];
    }
  } 
  throw new Error('Query Variable ' + variable + ' not found');
}
function resetForm(t){
    while( t.parentNode.nodeName.toLowerCase() != "form" ) {
        t = t.parentNode;
        if( t.nodeName.toLowerCase() == "body") {
            throw new Error("Couldn't locate form to reset");   
        }
    }
    t.parentNode.reset();
    return false;
}
// from www.dustindiaz.com
function getElementsByClass(searchClass,node,tag) {
   var classElements = new Array();
    if ( node == null )
      node = document;
   if ( tag == null )
      tag = '*';
   var els = node.getElementsByTagName(tag);
   var elsLen = els.length;
   var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
   for (i = 0, j = 0; i < elsLen; i++) {
      if ( pattern.test(els[i].className) ) {
         classElements[j] = els[i];
         j++;
      }
   }
   return classElements;
}
function findPosX(obj) {
    var curleft = 0;
    if (obj.offsetParent) {
        while (obj.offsetParent) {
            curleft += obj.offsetLeft;
            obj = obj.offsetParent;
        }
    }
    else if (obj.x)
        curleft += obj.x;
    return curleft;
}
function findPosY(obj) {
    var curtop = 0;
    if (obj.offsetParent) {
        while (obj.offsetParent) {
            curtop += obj.offsetTop;
            obj = obj.offsetParent;
        }
    }
    else if (obj.y)
        curtop += obj.y;
    return curtop;
}
/* END: Utils Section */

/* BEGIN: Usability functions */
function checkMaxLength(inputObj,maxChars,outputObj,doChop,display){
   var _obj = document.getElementById(outputObj);
   if(inputObj){
   
      if( inputObj.value.length >= maxChars ){
         if( doChop ){
            inputObj.value = inputObj.value.substr(0,maxChars);
         }
         if(_obj && _obj.innerHTML){
            _obj.innerHTML = (display) ? display : "No";
         }
      } else {
         var _tmpMaxChars = maxChars - inputObj.value.length;
         if(_obj && _obj.innerHTML){
            _obj.innerHTML = _tmpMaxChars;
         }
      }
   }
}
/* END: Usability functions */


/* BEGIN: Accordion Nav */
var aN_i           = null;     //item
var aN_a           = null;     //accordion
var aN_iBtnClass   = "hdrBtn"; //button class
var aN_aClass      = "accordion"; //button class
var aN_btn         = null;
var aN_btn_sel_src          = "/images/common/nav/bullet_minus.png";
var aN_btn_sel_over_src    = "/images/common/nav/bullet_minus_circle.png";
var aN_btn_unsel_src       = "/images/common/nav/bullet_plus.png";
var aN_btn_unsel_over_src    = "/images/common/nav/bullet_plus_circle.png";


/* activate accordion */
function actAcc(t){
   _swapBtn( _getBtn(t), 2 );
}
/* deactivate accordion */
function deactAcc(t){
   _swapBtn( _getBtn(t), 3 );
}
/* open accordion */
function toggleAcc(t){
   if( aN_a != null ){
      var _tmpThis = _getAcc(t);
      var _tmpActive = aN_a;

      aN_btn = _swapBtn( aN_btn, 0);
      aN_a = _hideAcc( aN_a );

      if( _tmpThis === _tmpActive ){
         return;
      }
   }

   aN_btn = _swapBtn( _getBtn(t), 1);

   aN_a = _getAcc(t);
   _showAcc( aN_a );
}
function _hideAcc(t){
   if( t != null ){
      t.style.display = "none";
   }
   return null;
}
function _showAcc(t){
   if( t != null ){
      t.style.display = "block";
   }
}
/* flag values: 0=> swap it click, 1=> revert it, 2=> swap it over, 3=> swap it out */
function _swapBtn(b,flag){
   if( b == null ){
      return null;
   }
   if(flag == 1){
      b.src = aN_btn_sel_src;
   } else if(flag == 2){
      //do mouse-over swapping
      if(b.src.indexOf(aN_btn_sel_src) > 0){
         b.src = aN_btn_sel_over_src;
      } else if(b.src.indexOf(aN_btn_unsel_src) > 0){
         b.src = aN_btn_unsel_over_src;
      }
   } else if(flag == 3){
      //do mouse-out swapping
      if(b.src.indexOf(aN_btn_sel_over_src) > 0){
         b.src = aN_btn_sel_src;
      } else if(b.src.indexOf(aN_btn_unsel_over_src) > 0){
         b.src = aN_btn_unsel_src;
      }
   } else{
      b.src = aN_btn_unsel_src;
   }
   return b;
}
function _getBtn(t){
   return _getObj(t.childNodes,aN_iBtnClass);
}
function _getAcc(t){
   return _getObj(t.parentNode.childNodes,aN_aClass);
}
/* END: Accordion Nav */

/* BEGIN: Main Nav */
var gN_f      = null;   //flyout
var gN_i      = null;   //nav item
var gN_t      = null;   //timer
var gN_iClass = "flyoutActive";
var gN_nClass = "flyout";

function showFlyout(t){
   if( gN_t != null ){  clearTimeout( gN_t );  }

   var tmp_i = t;  var tmp_f = _getFlyout(t);

   if( gN_i != null || gN_f != null ){
      if( gN_i === tmp_i || gN_f === tmp_f ){ return; /* we're already showing what we need */  }
      else { _hideFlyout(); }
   }

   gN_i = tmp_i;  gN_f = tmp_f;

   if( gN_f != null ){ gN_f.style.display = "block"; }
   //else { throw new Error("'gN_f' is null when it should not be."); }

   holdFlyout();
}
function _getFlyout(t){
   return _getObj(t.parentNode.childNodes,gN_nClass);
}
function holdFlyout(){
   if( gN_i != null ){ classAdd(gN_i,gN_iClass); }
   if( gN_t != null ){ clearTimeout( gN_t );  }
}
function hideFlyout(){
   if( gN_f != null ){ gN_t = setTimeout( "_hideFlyout();", 500 );  }
}
/* helper function, performs the dirtyness */
function _hideFlyout(){
   if( gN_i != null ){ classRemove(gN_i,gN_iClass); gN_i = null; }
   if( gN_f != null ){ gN_f.style.display = "none"; gN_f = null; }
}
/* END: Main Nav */

function _getObj(items,c){
   var _s = items.length;
   if( _s > 0 ){
      for( var i = 0; i < _s; i++ ){
         if( items[i].className == c ){
            return items[i];
         }
      }
   }
   //debugObj.trace("didn't find");
   return null;
}

/*BEGIN: force footer */
function getWindowHeight() {
   var windowHeight=0;
   if (typeof(window.innerHeight)=='number') {
      windowHeight=window.innerHeight;
   }else {
      if (document.documentElement&&document.documentElement.clientHeight) {
         windowHeight=document.documentElement.clientHeight;
      } else {
         if (document.body&&document.body.clientHeight) {
            windowHeight=document.body.clientHeight;
         }
      }
   }
   return windowHeight;
}
               
function setFooter(footerId,contentId,spacing) {
   if (document.getElementById) {
      var windowHeight = getWindowHeight();
     
      if (windowHeight>0) {
         var _tmpObjC = document.getElementById(contentId);
         var _tmpObjF = document.getElementById(footerId);
               
         if(_tmpObjF && _tmpObjC){
            var contentHeight = _tmpObjC.offsetHeight;
            var footerHeight = _tmpObjF.offsetHeight;
         var total = eval(contentHeight+footerHeight+spacing);
         if ( windowHeight > total ) {
            _tmpObjF.style.position = 'absolute';
            _tmpObjF.style.bottom = '0px';   //work IE and FF
            } else {
            _tmpObjF.style.position = 'absolute';
            _tmpObjF.style.top = (total - 20) + 'px';      //work FF
            /*
            if(navigator.userAgent.toLowerCase().indexOf('msie') > 0){
               _tmpObjF.style.bottom = '-28px';
            }*/
         }
         }
      }
   }
}
// returns date string in format: 'Thurs. Nov. 20, 2005'
function getDTVDateString(){
   var _wkDayArr = new Array("Mon","Tues","Wed","Thurs","Fri","Sat","Sun");
   var _mosArr = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sept","Oct","Nov","Dec");
   var _dateObj = new Date();
   return _wkDayArr[_dateObj.getDay()]  + ". " + _mosArr[_dateObj.getMonth()] + ". " + _dateObj.getDate() + ", " + _dateObj.getFullYear();
}

window.onload = function() {

  setFooter('containerFooter','contentMain',100);

   var _tmpObj = null;

   _tmpObj = document.getElementById('myDTVdateStr');
   if( _tmpObj != null ){
      _tmpObj.innerHTML = getDTVDateString();
      _tmpObj = null;
   }
   _tmpObj = document.getElementById('openAccordion');
   if( _tmpObj != null ){
      toggleAcc(_tmpObj);
      _tmpObj = null;
   }
   
   //members only stuff
   var links = document.getElementsByTagName('a');
   for (var linkIndex = 0; linkIndex < links.length; linkIndex++) {
      var _linkElm = links[linkIndex];
      if (_linkElm.className.indexOf('membersOnly') > -1) {
         _linkElm.onmouseover = showMembersOnly;
         _linkElm.onmouseout = hideMembersOnly;
      }
   }

    //code added by Kapil - 2/22/06 for adding premium packages if TOTAL CHOICE PREMIER base pkg is selected as the default pkg.
    var locationVar = window.document.location.pathname;
    if(locationVar.indexOf("buildYourSystem2.jsp") != -1)
    {
        if(_gel('selectedBaseId') != null)
        {
            if(_gel('selectedBaseId').value == "S")
            {
                _gel('spanishRadio').checked = "true";
            }
            else
            {
                _gel('englishRadio').checked = "true";
            }
        }
        if(_gel('englishRadio').checked)
        {
            langOnClick(true);
        }
        else
        {
            langOnClick(false);
        }       
    }
    else if(locationVar.indexOf("buildYourSystem4.jsp") != -1)
    {
        updateNumTVs(null);
    }
}

window.onresize = function() {
  setFooter('containerFooter','contentMain',100);
}

/*END: force footer */

/* BEGIN: Members Only links */
 /*note: HTML for element is in the footer */
function showMembersOnly(e) {
   if (!e) e = event;
   var linkElement = (e.target != null) ? e.target : e.srcElement;
   var _tmpObj = document.getElementById('membersOnly');
   _tmpObj.style.top = getOffsetTop(linkElement, true) - 25 + 'px';
   _tmpObj.style.left = getOffsetLeft(linkElement, true) + 10 + 'px';
   _tmpObj.style.visibility = 'visible';
}

function hideMembersOnly(e) {
   var _tmpObj = document.getElementById('membersOnly');
   _tmpObj.style.visibility = 'hidden';
}

function getOffsetTop(element, deep) {
   return getOffsetProperty(element, 'Top', deep);
}

function getOffsetLeft(element, deep) {
   return getOffsetProperty(element, 'Left', deep);
}

function getOffsetProperty(element, property, deep) {
   var offsetValue = 0;
   offsetProperty = 'offset' + property;

   do {
      offsetValue += element[offsetProperty];
      element = element.offsetParent;
   } while (deep == true && element != document.body && element != null);
   return offsetValue;
}
/*END: Members Only links */

/*BEGIN: Form JS */
 //used to move cursor in phone and sin fields
function cursorHelper(t){
   if(t.className.indexOf('areaCode') != -1){
      if( t.value.length < 3 ){
         return;
      }
      var _tmpObj = _getObj(t.parentNode.childNodes,'num3');
      _tmpObj.focus();
   }
   if(t.className.indexOf('num3') != -1){
      if( t.value.length < 3 ){
         return;
      }
      var _tmpObj = _getObj(t.parentNode.childNodes,'num4');
      _tmpObj.focus();
   }
   
   if(t.className.indexOf('sin1') != -1){
      if( t.value.length < 3 ){
         return;
      }
      var _tmpObj = _getObj(t.parentNode.childNodes,'sin2');
      _tmpObj.focus();
   }
   if(t.className.indexOf('sin2') != -1){
      if( t.value.length < 2 ){
         return;
      }
      var _tmpObj = _getObj(t.parentNode.childNodes,'sin3');
      _tmpObj.focus();
   }
}
/*END: Form JS */


/******************************************************************************
   Popup Windows
******************************************************************************/

var winReference = null;
// Open a window at a given position on the screen
function openPositionedWindow(url, name, width, height, x, y, status, scrollbars, moreProperties, openerName) {

   // ie 4.5 and 5.0 mac - windows are 2 pixels too short; if a statusbar is used, the window will be an additional 15 pixels short
   var agent = navigator.userAgent.toLowerCase();
   if (agent.indexOf("mac") != -1 && agent.indexOf("msie") != -1 && (agent.indexOf("msie 4") != -1 || agent.indexOf("msie 5.0") != -1) ) {
      height += (status) ? 17 : 2;
   }

   // Adjust width if scrollbars are used (pc places scrollbars inside the content area; mac outside) 
   //width += (scrollbars != '' && scrollbars != null && agent.indexOf("mac") == -1) ? 16 : 0;

   var properties = 'width=' + width + ',height=' + height + ',screenX=' + x + ',screenY=' + y + ',left=' + x + ',top=' + y + ((status) ? ',status' : '') + ',scrollbars' + ((scrollbars) ? '' : '=no') + ((moreProperties) ? ',' + moreProperties : '');
   var reference = openWindow(url, name, properties, openerName);
   return reference;
}

// Open a window at the center of the screen
function openCenteredWindow(url, name, width, height, status, scrollbars, moreProperties, openerName) {
   var x, y = 0;
   if (screen) {
      x = (screen.availWidth - width) / 2;
      y = (screen.availHeight - height) / 2;
   }
   if (!status) status = '';
   if (!openerName) openerName = '';
   var reference = openPositionedWindow(url, name, width, height, x, y, status, scrollbars, moreProperties, openerName);
   return reference;
}

// Core utility function that actually creates the window and gives focus to it
function openWindow(url, name, properties, openerName) {

   // ie4.x pc can't give focus to windows containing documents from a different domain
   // in this case, initially load a local interstisial page to allow focussing before loading final url
   var agent = navigator.userAgent.toLowerCase();
   if (agent.indexOf("msie") != -1 && parseInt(navigator.appVersion) == 4 && agent.indexOf("msie 5") == -1 && agent.indexOf("msie5") == -1 && agent.indexOf("win") != -1 && url.indexOf('http://') == 0) {
      winReference = window.open('about:blank', name, properties);
      setTimeout('if (winReference && !winReference.closed) winReference.location.replace("' + url + '")', 300);
   }
   else {
      winReference = window.open(url, name, properties);
   }
   // ie doesn't like giving focus immediately (to new window in 4.5 on mac; to existing ones in 5 on pc)
   setTimeout('if (winReference && !winReference.closed) winReference.focus()', 200);
   if (openerName) self.name = openerName;
   return winReference;
}

function largePopup(t){
	openCenteredWindow(t.href, 'largePopup', 382, 444, true, false, '', 'opener');
	return false;
}
function mediumPopup(t){
	openCenteredWindow(t.href, 'mediumPopup', 382, 358, true, false, '', 'opener');
	return false;
}
function formPopup(t){
	openCenteredWindow(t.href, 'formPopup', 395, 358, true, true, '', 'opener');
	return false;
}
function smallPopup(t){
	openCenteredWindow(t.href, 'smallPopup', 382, 232, true, false, '', 'opener');
	return false;
}
function MoreInfoPopup(t){
	openCenteredWindow(t.href, 'moreInfo', 382, 232, true, false, '', 'opener');
	return false;
}
function VideoPreviewPopup(t){
   openCenteredWindow(t.href, 'details', 680, 462, true, false, '', 'opener');
   return false;
}
function ShowDetailsPopup(t){
	openCenteredWindow(t.href, 'details', 680, 462, true, false, '', 'opener');
	return false;
}
function ShowAddWatchListPopup(t){
	openCenteredWindow(this.href, 'addToWatchlist', 382, 244, true, false, '', 'opener');
	return false;
}

/******************************************************************************
   User alert bar (on home pages only)
******************************************************************************/

var alertBarMinHeight = 17; // line height of ticker
var alertBarPerLineSeconds = 6; // how many seconds the message stays on screen
var alertBarScrollMilliseconds = 100; // scroll speed
var alertBar;
var alertBarScrollInterval;
var alertBarPixelsToScroll;

function initAlertBar() {
   alertBar = document.getElementById('alertBar');
   alertBar.style.top = '0px';
   if (alertBar.offsetHeight > alertBarMinHeight) {
      setInterval('scrollAlertBar()', alertBarPerLineSeconds * 1000);
   }
}

function scrollAlertBar() {
   
   // complete the last scroll cycle
   clearInterval(alertBarScrollInterval);
   
   if( alertBarPixelsToScroll ){
      alertBar.style.top = parseInt(alertBar.style.top) - alertBarPixelsToScroll + 'px';
   }
   
   // start next scroll cycle
   alertBarPixelsToScroll = alertBarMinHeight;
   alertBarScrollInterval = setInterval('moveAlertBarUpOnePixel()', alertBarScrollMilliseconds);
   
}

function moveAlertBarUpOnePixel() {
   alertBar.style.top = parseInt(alertBar.style.top) - 1 + 'px';
   alertBarPixelsToScroll--;
   
   // scroll past end?  loop back to beginning
   if (-alertBar.offsetTop >= alertBar.offsetHeight) {
      alertBar.style.top = alertBarMinHeight + 'px';
      alertBarPixelsToScroll = alertBarMinHeight;
   }
   
   // moved to the next line?  stop moving
   if (alertBarPixelsToScroll <= 0) {  
      clearInterval(alertBarScrollInterval);
   }
}


/******************************************************************************
   Answer toggle
******************************************************************************/
   function toggleAnswer(id) {
      var answerElement = document.getElementById('answer_' + id);
      if (answerElement.style.display == 'block') {
         hideAnswer(id);
      }
      else {
         showAnswer(id);
      }
   }
   
   function showAllAnswers() {
      var sectionIndex = 1;
      while (document.getElementById('answer_' + sectionIndex + '_1')) {
         var questionIndex = 1;
         while (document.getElementById('answer_' + sectionIndex + '_' + questionIndex)) {
            showAnswer(sectionIndex + '_' + questionIndex);
            questionIndex++;
         }
         sectionIndex++;
      }
   }
   
   function hideAllAnswers() {
      var sectionIndex = 1;
      while (document.getElementById('answer_' + sectionIndex + '_1')) {
         var questionIndex = 1;
         while (document.getElementById('answer_' + sectionIndex + '_' + questionIndex)) {
            hideAnswer(sectionIndex + '_' + questionIndex);
            questionIndex++;
         }
         sectionIndex++;
      }
   }
   
   function showAnswer(id) {
      var answerElement = document.getElementById('answer_' + id);
      answerElement.style.display = 'block';
      var imageElement = document.getElementById('arrow_' + id);
      imageElement.src = '/images/common/btns/btn_expanded.png'
      imageElement.height = '47';   //perhaps this should be offsetheight?
      var questionElement = document.getElementById('question_' + id);
      questionElement.style.fontWeight = 'bold';
   }
   
   function hideAnswer(id) {
      var answerElement = document.getElementById('answer_' + id);
      answerElement.style.display = 'none';
      var imageElement = document.getElementById('arrow_' + id);
      imageElement.src = '/images/common/btns/btn_collapsed.png'
      imageElement.height = '14';
      var questionElement = document.getElementById('question_' + id);
   }

/* ****************************************************************************
   Date Utilities
 *****************************************************************************/

/*
    DateUtils.js
    Author:    Chad Lindstrom <webmaster(AT)chadlindstrom(DOT)ca>
    License:   http://creativecommons.org/licenses/by/1.0/
    
    Dependencies:
        - http://www.chadlindstrom.ca/resources/library/javascript/DomUtils.js
        
        These dependencies are satisified with:
            - http://www.directv.com/resources/js/global.js
*/

/* JSON for Date Utils */
var DateUtils = {
    IsSameMonth : function(firstD, secondD) {
        if (firstD == null || secondD == null){
        return false;
        }
        return firstD.getFullYear() == secondD.getFullYear() && firstD.getMonth() == secondD.getMonth();
    },  
    Today : function(){
        var today = new Date();
        today.setMilliseconds(0);
        today.setSeconds(0);
        today.setMinutes(0);
        today.setHours(0);
        return today;
    },
    FirstDayOffset : function(d){
        var firstDay = new Date(d.getFullYear(), d.getMonth(), 1);
        return firstDay.getDay();
    },
    WeekDayStr : function(i){
        var weekdayStr = new Array("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday");
        return weekdayStr[i];
    },
    WeekDayStrShort : function(i){
        var weekdayStr = new Array("Mon","Tue","Wed","Thurs","Fri","Sat","Sun");
        return weekdayStr[i];
    },
    WeekDayChar : function(i){
        var weekdayChar = new Array("M","T","W","T","F","S","S");
        return weekdayChar[i];
    },
    MonthDays : function(i,y){
        if(typeof(y) == "undefined"){ y = 2006; /*this.Today.getFullYear();*/ }
        var monthLen = new Array(31,((this.IsLeapYear(y)) ? 29: 28),31,30,31,30,31,31,30,31,30,31);
        return monthLen[i];
    },
    MonthStr : function(i){
        var monthStr = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
        return monthStr[i];
    },
    MonthStrShort : function(i){
        var monthStr = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sept","Oct","Nov","Dec");
        return monthStr[i];
    },
    MonthChar : function(i){
        var monthStr = new Array("J","F","M","A","M","J","J","A","S","O","N","D");
        return monthStr[i];
    },
    IsLeapYear : function(year){
        var flag = (year % 4 == 0) ? true : false;   //leap year calculation should be updated/fixed
        return flag;
    },
    /* adds n years to date d, returns resultant date */
    AddYear : function(d,n){
        d.setFullYear( d.getFullYear() + n );
        return d;
    },
    /* subtracts n years from date d, returns resultant date */
    SubYear : function(d,n){
        return this.AddYear(d,n*-1);
    },
    /* adds n months to date d, returns resultant date */
    AddMonth : function(d,n){
        d.setMonth( d.getMonth() + n );
        return d;
    },
    /* subtracts n months from date d, returns resultant date */
    SubMonth : function(d,n){
        return this.AddMonth(d,n*-1);
    },
    /* adds n days to date d, returns resultant date */
    AddDay : function(d,n){
        d.setDate( d.getDate() + n );
        return d;
    },
    /* subtracts n days from date d, returns resultant date */
    SubDay : function(d,n){
        return this.AddDay(d,n*-1);
    }
}
/* ****************************************************************************
   Form Utilities
 *****************************************************************************/
 
/*
    FormUtils.js
    Version:   v0.1
    Author:    Chad Lindstrom <webmaster(AT)chadlindstrom(DOT)ca>
    License:   http://creativecommons.org/licenses/by/1.0/
    
    Dependencies:
        - http://www.chadlindstrom.ca/resources/library/javascript/DateUtils.js
*/

/* JSON for form tools */
var FormUtils = {
    InitDaySel : function(dayObj,dateObj,f,l){
        var arrObj = new Array();
        var selDay = dateObj.getDate();
        var currDay = DateUtils.Today().getDate();
        var totalDays = DateUtils.MonthDays(dateObj.getMonth(),dateObj.getFullYear());
        
        if(typeof(f) == "undefined"){ f = currDay; }
        if(typeof(l) == "undefined"){ l = totalDays; }
        
        for(var i = f; i <= l; i++){
            arrObj.push( new OptionItem(i, i, (i == selDay) ) );
        }
        this.PopulateSelect(dayObj,arrObj);
    },
    InitMonthSel : function(monthObj,dateObj,f,l){
        var arrObj = new Array();
        var selMonth = dateObj.getMonth();
        var currMonth = DateUtils.Today().getMonth();
        
        if(typeof(f) == "undefined"){ f = currMonth; }
        if(typeof(l) == "undefined"){ l = 11; }
        if( f < 0 && f > 11 ){ f = 0; }
        if( l < 0 && l > 11 ){ l = 11; }
        
        for(var i = f; i <= l; i++){
            arrObj.push( new OptionItem(i, DateUtils.MonthStrShort(i), (i == selMonth) ) );
        }
        this.PopulateSelect(monthObj,arrObj);
    },
    InitYearSel : function(yearObj,dateObj,f,l,z){
        var arrObj = new Array();
        var selYear = dateObj.getFullYear();
        var currYear = DateUtils.Today().getFullYear();
        
        if(typeof(f) == "undefined"){ f = currYear - 10; }
        if(typeof(l) == "undefined"){ l = currYear + 10; }
        
        for(var i = f; i <= l; i++){
            arrObj.push( new OptionItem(i, i, (i == selYear) ) );
        }
        this.PopulateSelect(yearObj,arrObj);
    },
    PopulateSelect : function(obj,arr){
        obj.options.length = 0;
        for(var i = 0; i < arr.length; i++){
          obj.options[i] = new Option(arr[i].label,arr[i].value);
            if( arr[i].isSelected ){
                obj.options[i].selected = "selected";
            }
        }
   },
    SelectAllOptions : function(obj){
        for( var i = 0; i < obj.options.length; i++ ){
            obj.options[i].selected = true;
        }
    },
    RemoveOption : function(obj){
        //loop backwards, to avoid indexing issues
        for(var i = obj.length - 1; i >= 0; i--){   
            if( obj.options[i].selected ){
                obj.remove( i );
            }
        }
    },
    AddOptions : function(obj,optionsArr,sortFunc,maxAdd){
        var isMax = true;
        if(typeof(maxAdd) == "undefined"){ isMax = false; maxAdd = optionsArr.length; }
        
        var _currOptions = this.GetOptions(obj);
        
        for( var i = 0; i < maxAdd; i++ ){
            if( !this.OptionsFind( obj, optionsArr[i].value ) ){
                _currOptions.push(optionsArr[i]);
            } else {
                maxAdd++;   /* we never actually added anything so we need
                                   to account for this */
            }
            
        }
        
        if(sortFunc){
            _currOptions.sort(sortFunc);
        } else {
            _currOptions.sort();
        }
        
        obj.options.length = 0;
        for(var k = 0; k < _currOptions.length; k++){
            obj.options[k] = new Option(_currOptions[k].label,_currOptions[k].value);
        }
    },
    GetSelectedOptions : function(obj){
        var selArr = new Array();
        for(var i=0; i < obj.length; i++){
            if(obj.options[i].selected == true){
                selArr.push(obj.options[i]);
            }
        }
        return selected;
    },
    GetOptions : function(obj){
        var optArr = new Array();
        for(var i=0; i < obj.length; i++){
            optArr.push(obj.options[i]);
        }
        return optArr;
    },
    OptionsFind : function(optionArr,value){
        
    },
    NumericOptionSort : function(a,b){
        return a.value-b.value;
    }
}

/* Orphaned Utils and Objects */
function OptionItem(v,l,sel){
    this.value = v;
    this.label = l;
    this.isSelected = sel;
}