    
    
  Event.observe(window, 'dom:loaded', init, false);

  sections = ['col_1','col_2','col_3'];
                                                        
  function  moduleHover()
  {
    alert('observing');
  
  }



  function init()
  {


    new Effect.Highlight('serverMsg');
    $('serverMsg').innerHTML = 'Loaded';
    
    

    
  }

  var i = 0;
  
  function setProperHeight()
  { 
    var h = 0;
    var sections    = $A($$('.section'));//document.getElementsByClassName('section');
    
    sections.each(function(section) {
        h = Math.max (h, section.childElements().inject(0, function(sum, element) { return sum + element.getHeight(); }));
    }); 
    
    var minH    = document.viewport.getHeight();
    
    if ( h < minH ) {
      sections.invoke("setStyle", {height: minH + 'px'});
    }
    else  {
      sections.invoke("setStyle", {height: h + 'px'});
    }
  }
  
  
  
  

  function viewBannerAdmin()
  {
    if ( $('wrap_text_banner_admin').style.display == 'none' )
    {
      $('wrap_admin_tab').setStyle({ 'position' : 'absolute' });
      $('wrap_text_banner_admin').show();
      $('wrap_admin_tab').setStyle({ 'borderBottom' : '0px' });
    }
    else
    {
      $('wrap_text_banner_admin').hide();
      $('wrap_admin_tab').setStyle({ 'borderBottom' : '1px solid #c2c2c2' });
      $('wrap_admin_tab').setStyle({ 'position' : 'relative' });
    }
  }


  
  Event.observe(window, 'load', function () {
    
    if ( $('wrap_existing_banners') )   {
      
      //$('wrap_admin_tab').observe('click', function () {
        $('wrap_existing_banners').innerHTML = 'Loading... <img src="/gather/ajax-loader.gif">';
        new Ajax.Request(
          '/modules/dash_xhr_banners.php', 
          {
            method: 'get', 
            onComplete: function (transport) {
              $('wrap_existing_banners').update(transport.responseText);
            }
          }
        );    
      //});


      $('wrap_admin_tab').observe('click', function () {
        
        $('wrap_existing_banners').innerHTML = 'Loading... <img src="/gather/ajax-loader.gif">';
        new Ajax.Request(
          '/modules/dash_xhr_banners.php', 
          {
            method: 'get', 
            onComplete: function (transport) {
              $('wrap_existing_banners').update(transport.responseText);
            }
          }
        );    
      });     
    }
  });
    



  
  
    function deleteBanner(this_bnr_id)
    {
      var args = 'tb_id='+this_bnr_id;
      
      new Ajax.Request(
        '/modules/dash_xhr_delete_banner.php', 
        {
          method: 'post',
          postBody: args,
          onComplete: function (transport) {
            
            if ( transport.responseText == 'success' ) {
              new Ajax.Request(
                '/modules/dash_xhr_banners.php', 
                {
                  method: 'get', 
                  onComplete: function (transport) {
                    $('wrap_existing_banners').update(transport.responseText);
                  }
                }
              );                  
            }
          }
        }
      
      );
    
    
    }
    
    function getBannerToEdit(this_bnr_id)
    {
      //alert(this_bnr_id);
      
      var args = 'tb_id='+this_bnr_id;
      
      
      //alert(args);
      new Ajax.Request(
        '/modules/dash_xhr_bannerjson.php', 
        {
          method: 'get',
          parameters: args,
          onComplete: function (transport) {
            //alert(transport.responseText);
            
            var tb  = transport.responseText.evalJSON(true);
            
            
            //alert(transport.responseText);
            
            newBanner();
            
            $('tb_id').value            = tb.tb_id;
            $('tb_text_display').value  = tb.tb_text_display;
            $('tb_text_href').value     = tb.tb_text_href;
            $('tb_date_created').value  = tb.tb_date_created;
            $('tb_date_expires').value  = tb.tb_date_expires;
            $('tb_isdefault').value     = tb.tb_isdefault;
            $('tb_billing').value       = tb.tb_billing;
            
          }
        }
      );
    
    
    
    }

    function newBanner()
    {
      $('tb_id').highlight().value            = '';
      $('tb_text_display').highlight().value  = '';
      $('tb_text_href').highlight().value     = '';
      $('tb_date_created').highlight().value  = '0000-00-00';
      $('tb_date_expires').highlight().value  = '0000-00-00';
      $('tb_isdefault').highlight().value     = 0;
      $('tb_billing').highlight().value       = 0;
    }

  
    function saveBanner()
    {
    
    
      if ( $('tb_text_display').value != '' &&  $('tb_text_href').value != '' ) 
      {
        
        if ( $('tb_isdefault').value == 1 ) 
        {
          $('anchor_text_banner').href = $('tb_text_href').value;
          $('anchor_text_banner').update($('tb_text_display').value);       
        }
      
      
        var arg_serialized = $('tb_save').serialize();

        //alert(arg_serialized);

        $('wrap_existing_banners').update('Saving... <img src="/gather/ajax-loader.gif">');

        new Ajax.Request(
          '/modules/dash_xhr_savebanner.php', 
          {
            method: 'get',
            parameters: arg_serialized,
            onComplete: function (transport) {
              if (transport.responseText == 'success') {
                new Ajax.Request(
                  '/modules/dash_xhr_banners.php', 
                  {
                    method: 'get', 
                    onComplete: function (transport) {
                      $('wrap_existing_banners').update(transport.responseText);
                    }
                  }
                );                  
              }
              else {

                $('wrap_existing_banners').update(transport.responseText);

              }

              newBanner();
            }
          }     
        );
      }
      else
      {
        alert('Please complete both the DISPLAY and URL fields.');      
      }
    }
  
  

    function createNewSection(name) {
      var name = $F('sectionName');
      if (name != '') {
        var newDiv = Builder.node('div', {id: 'group' + (sections.length + 1), className: 'section', style: 'display:none;' }, [
          Builder.node('h3', {className: 'handle'}, name)
        ]);

        sections.push(newDiv.id);
        $('page').appendChild(newDiv);
        Effect.Appear(newDiv.id);
        destroyLineItemSortables();
        createLineItemSortables();
        createGroupSortable();
      }
    }

    function createLineItemSortables() {
      for(var i = 0; i < sections.length; i++) {
        Sortable.create(sections[i],{tag:'div',dropOnEmpty: true, containment: sections,only:'lineitem'});
      }
    }

    function destroyLineItemSortables() {
      for(var i = 0; i < sections.length; i++) {
        Sortable.destroy(sections[i]);
      }
    }

    function createGroupSortable() {
      Sortable.create('page',{tag:'div',only:'section',handle:'handle'});
    }


    function sendFeedBack()
    {
    
        var user_id   = $F('user_id');
        var bug_str   = $F('bug_str');
        var trac_rate = $F('trac_rating');

        var xhrUrl  = '../modules/fdb_xhr_server.php';
        var argStr  = 'bug_str='+encodeURIComponent(bug_str)+'&rate_int='+trac_rate+'&user_id='+user_id;


        
        
        var rssXhr  = new Ajax.Updater('feedbackBox', xhrUrl, { 
                                                                method: 'get', 
                                                                parameters: argStr
                                                            });
    
    }



    /*
    Debug Functions for checking the group and item order
    */



    function validFeed(this_rss_url) 
    {
        var v = new RegExp();
        v.compile("^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$");
        if (!v.test(this_rss_url)) {
            $("rssUrlError").innerHTML = "This url is invalid! Please try again!";
            $("rssUrlError").show();
        }
        else  {
          return this_rss_url;
        }

    }

    function processFeed(this_rss_url)
    {
      $('clickConfirm').hide();
      $('mdlPanelId').innerHTML = '<center>Loading Available Modules...<br><br> <img src="../modules/ajax-loader.gif"></center>';

      if (!this_rss_url) {}
      else  {



        
        var user_id = $F('user_id');

        var xhrUrl  = '../modules/rss_xhr_server.php';
        var argStr  = 'rss_url='+encodeURIComponent(this_rss_url)+'&user_id='+user_id;

        var rssXhr  = new Ajax.Request(xhrUrl, { method: 'get', 
                                                 parameters: argStr, 
                                                 onSuccess: function (transport) {
                                                  
                                                  if ( transport.responseText == 'Invalid RSS Feed URL' ) {
                                                    alert(transport.responseText);
                                                  }
                                                  else {
                                                    $('addModules').hide();
                                                    availModules();
                                                  }
                                                 } 
                                                } 
                                              );

      }

    }


    function resetModules()
    {
      var xhrUrl  = '/modules/mod_xhr_reset.php';
      
      if ( confirm("Are you sure you want to \n reset your Dashboard? \n\n Clicking 'yes' will reset your dashboard to \n default module set up.") )
      {
        new Ajax.Request(xhrUrl, {
          onComplete: function (transport) {
            if ( transport.responseText == 'OK' ) {
              location.href = window.location;
            }
            else {
              alert(transport.responseText);
            }
            
          }
        });
      }
    }





    function availModules()
    {
        
        
        $('addModules').show();
      
        var url         = '../modules/mod_xhr_server.php';
        var target      = 'serverMsg';
        var user_id     = $F('user_id');
        var dataStr     = '';
        
        
        /*
        var sectMods    = document.getElementsByClassName('section');
        
        
        for (var i=0, len=sectMods.length; i < len; ++i) {        
          dataStr += Sortable.serialize(sectMods[i].id) + '&';
        
        }

        //$('debug').update(dataStr).show();
        
        //return;
        
        
        sectMods.each(function(section) {
            //var sectionID = section.id;
            //var order     = Sortable.serialize(section.id);
            dataStr += Sortable.serialize(section.id) + '&';
        });        
        */
        
        //dataStr     += '&user_id='+user_id+'&action=avlmdl';
        
        
        dataStr = 'action=avlmdl';
        
        new Ajax.Updater('addModules', url, { 
          method: 'get', 
          parameters: dataStr,
          onComplete: function () {
            $('serverMsg').innerHTML = 'Loaded';
            
            
            
            //setGroupOrder();
          }
        });        

        
        
  }



    function addThisModule(this_dash_id)
    {
        var user_id     = $F('user_id');      
        var url         = '../modules/mod_xhr_server.php';
        var target      = 'serverMsg';
        var dataStr     = '';

        
        dataStr += 'dash_id='+this_dash_id+'&user_id='+user_id+'&action=adddash';
        
        var addDash = new Ajax.Updater('serverMsg', url, { method: 'get', parameters: dataStr });
        
        if ( addDash )
        {
          $('icon_'+this_dash_id).src='../img/_icon_dash_add.png';
          $('amdl_'+this_dash_id).style.borderColor='#197b30';
          $('clickto_'+this_dash_id).innerHTML='Click To <br> Remove This';
          $('amdl_'+this_dash_id).onclick = function() {removeThisModule(this_dash_id);}
        }
      
    }
 
    function removeThisModule(this_dash_id)
    {
        var user_id     = $F('user_id');      
        var url         = '../modules/mod_xhr_server.php';
        var target      = 'serverMsg';
        var dataStr     = '';

        
        dataStr += 'dash_id='+this_dash_id+'&user_id='+user_id+'&action=removedash';
        
        var removedash = new Ajax.Updater('serverMsg', url, { method: 'get', parameters: dataStr });
        
        if ( removedash )
        {
          $('icon_'+this_dash_id).src='../img/_icon_dash_remove.png';
          $('amdl_'+this_dash_id).style.borderColor='#d0d0d0';
          $('clickto_'+this_dash_id).innerHTML='Click To <br> Add This';
          $('amdl_'+this_dash_id).onclick = function() {addThisModule(this_dash_id);}
          
        }
      
    } 
 
 
 
 
 
 
    function setGroupOrder() {
        
        var url         = '../modules/mod_xhr_server.php';
        var target      = 'serverMsg';
        var user_id     = $F('user_id');
        var sections    = $A($$('.section'));//document.getElementsByClassName('section');
        var dataStr     = '';

        sections.each(function(section) {
            var sectionID = section.id;
            var order = Sortable.serialize(sectionID);
            dataStr += order + '&';
        });
        
        dataStr += '&user_id='+user_id+'&action=set';
        
        setProperHeight();
        
        new Ajax.Updater(target, url, { 
          method: 'get', 
          parameters: dataStr,
          onComplete: function ()  {
            returnBg  = new Effect.Highlight(target, { restorecolor: 'transparent' });
            $(target).innerHTML = 'Saved';
          }
        });
        
        
        
        
        
        //$('debug').innerHTML      = dataStr;
        //$('debug').style.display  ='block';
        //return false;
    }

    function setServerMsgProp()
    {
        new Effect.Highlight('serverMsg');
    }





    function showModuleOptions(dash_id)
    {
      if ($('modopt_'+dash_id).style.display!='block')
      {
        Effect.BlindDown('modopt_'+dash_id, {duration:0.25});
      }

    }
    
    function hideModuleOptions(dash_id)
    {
      //Effect.BlindUp('modopt_'+dash_id, {duration:0.25});
    }


    function dashState(dash_id)
    {
      $('mod_'+dash_id).toggle();
      
      
      if ( $('state_'+dash_id).value == '-' )
      {
        
        $('state_'+dash_id).value='+';
        $('stateBtn_'+dash_id).setAttribute('title', 'Maximize');
        $('stateBtn_'+dash_id).className='option_mx_16';
        
        dashUpdateState(dash_id, 'min');
        
      }
      else
      {
        
        $('state_'+dash_id).value='-';
        $('stateBtn_'+dash_id).setAttribute('title', 'Minimize');
        $('stateBtn_'+dash_id).className='option_m_16';
        
        dashUpdateState(dash_id, '');
        
        
      }
      
      
      
      
      
      setProperHeight();
    }

    function dashUpdateState(this_dash_id, stateStr)
    {
        var user_id     = $F('user_id');
        var url         = '../modules/mod_xhr_server.php';
        var target      = 'serverMsg';    
    
        var params      = 'action=pref&state='+stateStr+'&dash_id='+this_dash_id+'&user_id='+user_id;
        
        var setState    = new Ajax.Updater(target, url, { method: 'get', parameters: params });
        
        setProperHeight();
        
        
        return true;       
        
    }
    
    function dashRemove(this_dash_id, this_dash_title)
    {

      var url         = '../modules/mod_xhr_server.php';
      var target      = 'serverMsg';
      var user_id     = $F('user_id');

      var dash_id     = 'item_'+this_dash_id;

      if ( confirm("You are about to remove '"+this_dash_title+"' from your dashboard. \n\n Are you sure you want to do this?") )
      {

        if ( $(dash_id).remove() )
        {
          if ( this_dash_id == '046' ) {
            //setCookie( name, value, expires, path, domain, secure )
            setCookie( 'no_ad_module', 'true', 365, '/' );
          }
          setGroupOrder();
        }         
      } 
      setProperHeight();
    }
























    function getGroupOrder() {
        
        var url         = 'proto_get.php';
        var target      = 'debug';
        var sections    = $A($$('.section'));//document.getElementsByClassName('section');
        
        var dataStr     = '';
        
        sections.each(function(section) {
            var sectionID = section.id;
            var order = Sortable.serialize(sectionID);
            dataStr += order + '&';
            //alerttext += sectionID + ': ' + Sortable.sequence(section) + '\n';
        });
        
        
        var myAjax = new Ajax.Updater(target, url, {    method: 'get',  parameters: dataStr});
        
        $(target).innerHTML = dataStr;
        $(target).style.display='block';
        
        
        
        return false;
    }
    
    
    function showColName(id)    {
        var colId = 'col_'+id+'_h';
        $(colId).style.display='block';
    }
    
    function hideColName(id)    {
        var colId = 'col_'+id+'_h';
        $(colId).style.display='none';
    }
    




 
  function closeModalDialog()
  {
      
      
      $('overlayBackId').remove();
      $('mobileConnect').hide();
      
  }

 


  function openModalDialog() {


      var body = document.getElementsByTagName('body')[0];
      var offs = document.viewport.getScrollOffsets();

      body.appendChild(Builder.node('div', {id:'overlayBackId', className:'overlayBack', onclick: 'closeModalDialog();'  }));

      $('overlayBackId').style.height = ( document.viewport.getHeight() + (offs.top*2)) +'px';

      var xPos  = document.viewport.getWidth()/3;
      //var yPos  = document.viewport.getHeight()/8;
      var yPos  = 50;
      $('mobileConnect').style.left   = xPos+'px';
      $('mobileConnect').style.top    = (offs.top + yPos)+'px';

      
      new Ajax.Request('../modules/mod_xhr_mobile_check.php', {
          method: 'get',
          onComplete: function (transport) {
            

            var show_div = transport.responseText;


            $('mobileConnect').show();

            $('tell_friend_frame').hide();
            $('wrap_all').show();
            $('wrap_top_copy').show();
            $('wrap_response').hide();            
            
            
            
            //alert(show_div);
            
            
            
            

            
            
            
            if ( show_div == 'wrap_condition_disconnect' ) {  
              //alert('disconnect');
              $('wrap_condition_disconnect').show();
              
              $('modal_header').update("Update your cell/mobile phone number");
              $('wrap_top_disconnect').show();
              $('wrap_top_connect').hide();
              $('wrap_condition_connect').hide();
              $('wrap_mobile_tip').show();  
              $('btn_discn').show();
              
              
              
              
              
              //$('mobile_disconnect').observe('submit', disconnectMobileId);
              
            }
            
            else  {
              //alert('connect');
            
              $('wrap_condition_connect').show(); 
              
              $('wrap_top_disconnect').hide();  
              $('wrap_condition_disconnect').hide();
              
              
              
              $('modal_header').update("Track Your Daily Meals With Text Messaging!");  
              
              $('wrap_top_connect').show();
              
              
              $('mobile_id').focus();
              //$('mobile_id').value='';
            
              $('wrap_top_connect').show();
              $('wrap_mobile_tip').show();
              
              
              
              
              
              
              
              //$('mobile_connect').observe('submit', connectToMobile);
            }
            
              
          }
        
      
      });
      
      
      
  }



  function closeTestModalDialog()
  {
      
      
      $('overlayBackId').remove();
      $('modal_personality').hide();
      
  }




  function openTestModalDialog(bg_code) 
  {


      var body = document.getElementsByTagName('body')[0];
      var offs = document.viewport.getScrollOffsets();

      body.appendChild(Builder.node('div', {id:'overlayBackId', className:'overlayBack', onclick: 'closeTestModalDialog();'  }));

      $('overlayBackId').style.height = ( document.viewport.getHeight() + (offs.top*2)) +'px';

      var xPos  = document.viewport.getWidth()/4;
      //var yPos  = document.viewport.getHeight()/8;
      var yPos  = 100;
      $('modal_personality').style.left   = xPos+'px';
      $('modal_personality').style.top    = (offs.top + yPos)+'px';

      var type_arr = new Array();
      type_arr['ep'] = 'Eating Personality';
      type_arr['xp'] = 'Exercise Personality';
      type_arr['cp'] = 'Coping Personality';
      
      
      $('test_type_str').innerHTML        = type_arr[bg_code];
      $('lower_test_type_str').innerHTML  = type_arr[bg_code].toLowerCase();
      
      
      $('modal_personality').show();
  }














  function replaceWithIFrame(iframe_src)
  {
  
    $('modal_header').update("Tell A Friend!"); 
    $('wrap_all').hide();
    
    
    $('tell_friend_frame').setAttribute('src', '/tellfriend/');
    
    $('tell_friend_frame').setStyle({ width: '400px', height: '500px', border: 'none' }).show();
    
    
    
    
  
  }

  
  function keyListener(form_id, key) 
  {
    //alert(key);
  
    if (key == 13) {
      
      if ( form_id.id == 'mobile_disconnect' ) 
        disconnectMobileId('');
      else
        connectToMobile();  
    
    }
      
  }

  function connectToMobile()
  {
    return connectMobileId($('mobile_id').value);
  }


  function connectMobileId(mobile_id)
  {
    //var argSerial   = Form.serializeElements( $('mobile_connect').getElements() );  
    
    //alert(argSerial);
    
    var argParams = 'mobile_id='+mobile_id;
    
    var xhrUrl    = '../modules/mod_xhr_mobile_service.php';
    
    //wrap_form_debug
    new Ajax.Request(xhrUrl, {
          method: 'get',
          parameters: argParams, 
          onComplete: function (transport) {
            
            $('wrap_response').update(transport.responseText).show();
            
            $('wrap_condition_disconnect').show();
            $('wrap_condition_connect').hide();
            //$('wrap_top_disconnect').show();
            $('wrap_top_connect').hide();

            $('wrap_mobile_tip').show();
            $('wrap_top_copy').show();
            
            
            $('btn_discn').hide();
            $('btn_close').show();
          
          }
    
    
    });
  }


  function disconnectMobileId(mobile_id)
  {
    var argParams = 'mobile_id='+mobile_id; 
    
    //alert(argParams);
    
    
    
    var xhrUrl    = '../modules/mod_xhr_mobile_disconnect.php';
    //wrap_form_debug
    
    new Ajax.Request(xhrUrl, {
          method: 'get',
          parameters: argParams,
          onComplete: function (transport) {
            $('wrap_response').update( transport.responseText).show();
            
            $('wrap_condition_disconnect').hide();
            $('wrap_condition_connect').show();
            $('wrap_top_disconnect').hide();
            //$('wrap_top_connect').show();
            
            $('wrap_mobile_tip').show();
            $('wrap_top_copy').show();
          }
    
    
    });
  }




