function updatePersonalInformation(user_id)
{
  $.ajax({
    type: 'POST',
    url: '/users/'+user_id,
    data: $('#user').serialize()+'&partial=email',
    beforeSend: function(){
      error_displayed = false;
      $('#personal_information_form').fadeOut("fast", function(){
        $('#personal_information_loader').fadeIn("fast");
      });      
    },
    error: function(){
      $('#personal_information_loader').fadeOut("fast", function(){
        $('#personal_information_form').fadeIn("slow", function(){
          requestFailed();
        });
      });      
    },
    success: function(response){
      var user = JSON.parse(response);
      $('div #settings > p.name').html(user.full_name);
      $('div #login_box > p.floatleft').html(user.logged_in_as);
      $('div #settings > p.email').html(user.email);
      $('div #settings > p.timezone').html('Time zone: <i>'+user.time_zone+'</i>');
      tb_remove();
    }
  });    
}

function updateUserImage(user_id)
{
  $.ajax({
    type: 'GET',
    url: '/users/'+user_id+'/image?partial=user_image',
    error: requestFailed,
    success: function(responseHTML){
      parent.document.getElementById('user_home_left_image').innerHTML = responseHTML;
      parent.tb_remove()
    }
  });
}

function rotateUserImage(link, user_id, authenticity_token)
{
  prepForAjax(link);
 
  $.ajax({
    type: 'POST',
    url: '/users/'+user_id+'/image',
    data: 'authenticity_token='+encodeURIComponent(authenticity_token)+'&user_image[orientation]=1&_method=put',
    async: false,
    beforeSend: function(){
      error_displayed = false;
    },
    error: requestFailed,
    success: function(){
      updateUserImage(user_id);
    }
  });
  
  ajaxComplete(link);
}

function deleteUserImage(link, user_id, authenticity_token)
{
  prepForAjax(link);
   
  $.ajax({
    type: 'POST',
    url: '/users/'+user_id+'/image',
    data: 'authenticity_token='+encodeURIComponent(authenticity_token)+'&_method=delete',
    async: false,
    beforeSend: function(){
      error_displayed = false;
    },
    error: requestFailed,
    success: function(){
      updateUserImage(user_id);
    }        
  });
  
  ajaxComplete(link);
}

function editPageTitle(link, page_id)
{
  prepForAjax(link, page_id)
  
  scroll(0,0);
  tb_show(null, "#TB_inline?height=140&width=325&inlineId=title_form_tb&modal=true", false);

  $.ajax({
    type: 'GET',
    url: '/pages/'+page_id+'/edit?partial=title',
    async: false,
    beforeSend: function(){
      error_displayed = false;
    },
    error: function(){
      requestFailed();
      tb_remove();
    },
    success: function(responseHTML){
      document.getElementById('title_form').innerHTML = responseHTML;        
      $('#title_loader').fadeOut("fast", function(){
        $('#title_form').fadeIn("slow");
        createTooltips();
      });         
    }
  });
  
  ajaxComplete(link);
}

function updatePageTitle(page_id)
{
  $.ajax({
    type: 'POST',
    url: '/pages/'+page_id,
    data: $('#edit_page_title').serialize(),
    beforeSend: function(){
      error_displayed = false;
      $('#title_form').fadeOut("fast", function(){
        $('#title_loader').fadeIn("fast");
      });      
    },
    error: function(){
      $('#title_loader').fadeOut("fast", function(){
        $('#title_form').fadeIn("slow", function(){
          requestFailed();
        });
      });      
    },
    success: function(responseHTML){
      document.getElementById('page_title').innerHTML = responseHTML;
      tb_remove();      
    }
  });       
}

function updatePageBackground(page_id, authenticity_token)
{ 
  $.ajax({
    type: 'POST',
    url: '/pages/'+page_id,
    data: "_method=put&authenticity_token="+encodeURIComponent(authenticity_token)+"&page%5Bintroduction%5D="+encodeURIComponent(tinyMCE.activeEditor.getContent()),
    beforeSend: function(){
      error_displayed = false;
      $('#background_form').fadeOut("fast", function(){
        $('#background_loader').fadeIn("fast");
      });      
    },
    error: function(){
      $('#background_loader').fadeOut("fast", function(){
        $('#background_form').fadeIn("slow", function(){
          requestFailed();
        });
      });      
    },
    success: function(responseHTML){
      document.getElementById('page_background').innerHTML = responseHTML;
      resizeImages('page_background');
      setWmodeForFlash('page_background');        
      scroll(document.getElementById('page_background').offsetLeft, document.getElementById('page_background').offsetTop);
			tb_remove();      
    } 
  });
}

function updatePatientInfo(page_id, patient_id)
{  
  $.ajax({
    type: 'POST',
    url: '/pages/'+page_id+'/patients/'+patient_id,
    data: $('#edit_patient_info').serialize(),
    beforeSend: function(){
      error_displayed = false;
      $('#patient_info_form').fadeOut("fast", function(){
        $('#patient_info_loader').fadeIn("fast");
      });      
    },
    error: function(){
      $('#patient_info_loader').fadeOut("fast", function(){
        $('#patient_info_form').fadeIn("slow", function(){
          requestFailed(); 
        });
      });      
    },
    success: function(responseHTML){
      document.getElementById('patient_info').innerHTML = responseHTML;
      
      $.ajax({
        type: 'GET',
        url: '/pages/'+page_id+'?partial=title',
        beforeSend: function(){
          error_displayed = false;
        },
        error: requestFailed,
        success: function(responseHTML){
          document.getElementById('page_title').innerHTML = responseHTML;
          tb_remove();
        }
      });
    }
  });  
}

function updatePatientImage(page_id, patient_id, wizard)
{
  if(wizard){
    var patient_image_url = '/pages/'+page_id+'/patients/'+patient_id+'/image?wizard=true'  
  }
  else{
    var patient_image_url = '/pages/'+page_id+'/patients/'+patient_id+'/image'
  }

  $.ajax({
    type: 'GET',
    url: patient_image_url,
    error: requestFailed,
    success: function(responseHTML)
    {
      parent.document.getElementById('profile_image_wrapper').innerHTML = responseHTML;
      parent.createTooltips();
      parent.tb_remove();
    }
  });
}

function rotatePatientImage(link, page_id, patient_id, authenticity_token, wizard)
{  
  prepForAjax(link);

  $.ajax({
    type: 'POST',
    url: '/pages/'+page_id+'/patients/'+patient_id+'/image',
    data: 'authenticity_token='+encodeURIComponent(authenticity_token)+'&patient_image[orientation]=1&_method=put',
    async: false,
    error: requestFailed,
    success: function(){
      updatePatientImage(page_id, patient_id, wizard);
    }
  });
  
  ajaxComplete(link);  
}

function deletePatientImage(link, page_id, patient_id, authenticity_token, wizard)
{
  prepForAjax(link);
  
  $.ajax({
    type: 'POST',
    url: '/pages/'+page_id+'/patients/'+patient_id+'/image',
    data: 'authenticity_token='+encodeURIComponent(authenticity_token)+'&_method=delete',
    async: false,
    error: requestFailed,
    success: function(){
      updatePatientImage(page_id, patient_id, wizard);
    }    
  });
  
  ajaxComplete(link);
}

function updateContactInfo(page_id, patient_id)
{
  $.ajax({
    type: 'POST',
    url: '/pages/'+page_id+'/patients/'+patient_id,
    data: $('#edit_contact_info').serialize(),
    beforeSend: function(){
      error_displayed = false;
      $('#contact_info_form').fadeOut("fast", function(){
        $('#contact_info_loader').fadeIn("fast");
      });      
    },
    error: function(){
      $('#contact_info_loader').fadeOut("fast", function(){
        $('#contact_info_form').fadeIn("slow", function(){
          requestFailed(); 
        });
      });      
    },
    success: function(responseHTML){
      document.getElementById('contact_info').innerHTML = responseHTML;
      tb_remove();
    }            
  });
}

function getPageLinksIndex(link, page_id, pagination_page, wizard)
{
  prepForAjax(link);
  
  if(pagination_page == null)
  {
    pagination_page = 1;
    $('#links').fadeOut("fast", function(){
      $('#links_loader').fadeIn("fast", ajaxCall);
    });
  }
  else
  {
    ajaxCall();
  }
  
  function ajaxCall()
  {
    if(!wizard || wizard == null)
    {
      var theurl = '/pages/'+page_id+'/links?page='+pagination_page
    }
    else
    {
      var theurl = '/pages/'+page_id+'/links?page='+pagination_page+'&wizard=true';
    }
    
    $.ajax({
      type: 'GET',
      url: theurl,
      async: false,
      beforeSend: function(){
        error_displayed = false;
      },
      error: function(){
        $('#links_loader').fadeOut("fast", function(){
          $('#links').fadeIn("slow", function(){
            requestFailed(); 
          });
        }); 
      },
      success: function(responseHTML){
        document.getElementById('links').innerHTML = responseHTML;
        $('#links_loader').fadeOut("fast", function(){
          $('#links').fadeIn("slow");
          createTooltips();
        });       
      }       
    });    
  }
    
  ajaxComplete(link);    
}

function createPageLink(page_id, wizard)
{
  if(!wizard || wizard == null)
  {
    var theurl = '/pages/'+page_id+'/links';
  }
  else
  {
    var theurl = '/pages/'+page_id+'/links?wizard=true';
  } 
          
  $.ajax({
    type: 'POST',
    url: theurl,
    data: $('#new_link').serialize(),
    beforeSend: function(){
      error_displayed = false;      
      $('#links').fadeOut("fast", function(){
        $('#links_loader').fadeIn("fast");
      });
    },
    error: function(){
      $('#links_loader').fadeOut("fast", function(){
        $('#links').fadeIn("slow", function(){
          requestFailed(); 
        });
      });      
    },
    success: function(responseHTML){
      document.getElementById('links').innerHTML = responseHTML;
      if (document.getElementById('quotes_and_links_li') && document.getElementById('quotes_and_links_li').getAttribute('class').indexOf('complete') == -1) {
        document.getElementById('quotes_and_links_li').setAttribute('class', document.getElementById('quotes_and_links_li').getAttribute('class')+' complete');       
      }
      $('#links_loader').fadeOut("fast", function(){
        $('#links').fadeIn("slow", function(){
          document.getElementById('links_loader').style.display = 'none';
          createTooltips();
        });
      });           
    }
  });    
}

function updatePageLink(page_id, link_id, wizard)
{
  if(!wizard || wizard == null)
  {
    var theurl = '/pages/'+page_id+'/links/'+link_id;
  }
  else
  {
    var theurl = '/pages/'+page_id+'/links/'+link_id+'?wizard=true';
  }  
  
  $.ajax({
    type: 'POST',
    url: theurl,
    data: $('#edit_link_'+link_id).serialize(),
    beforeSend: function(){
      error_displayed = false;
      $('#links').fadeOut("fast", function(){
        $('#links_loader').fadeIn("fast");
      });
    },
    error: function(){
      $('#links_loader').fadeOut("fast", function(){
        $('#links').fadeIn("slow", function(){
          requestFailed(); 
        });
      });      
    },
    success: function(responseHTML){
      document.getElementById('links').innerHTML = responseHTML;
      $('#links_loader').fadeOut("fast", function(){
        $('#links').fadeIn("slow", function()
        {
          document.getElementById('links_loader').style.display = 'none'
          createTooltips();
        });
      });        
    }            
  });
}

function deletePageLink(link, page_id, link_id, authenticity_token, pagination_page, wizard)
{
  prepForAjax(link);

  if(!wizard || wizard == null)
  {
    var theurl = '/pages/'+page_id+'/links/'+link_id;
  }
  else
  {
    var theurl = '/pages/'+page_id+'/links/'+link_id+'?wizard=true';
  }
  
  $.ajax({
    type: 'POST',
    url: theurl,
    data: 'authenticity_token='+encodeURIComponent(authenticity_token)+'&_method=delete&page='+pagination_page,
    async: false,
    error: requestFailed,
    success: function(responseHTML){
      document.getElementById('links').innerHTML = responseHTML;
      createTooltips();
    }
  });
  
  ajaxComplete(link); 
}

function updateLinkList(link, page_id)
{ 
  prepForAjax(link);

  $.ajax({
    type: 'GET',
    url: '/pages/'+page_id+'/links?list=true',
    async: false,
    beforeSend: function(){
      error_displayed = false;
      $('#links').fadeOut("fast", function(){
        $('#links_loader').fadeIn("fast");
      });
    },
    success: function(responseHTML){
      document.getElementById('link_list').innerHTML = responseHTML;
    }        
  });
  
  tb_remove();
  
  ajaxComplete(link);  
}

function getPageQuotesIndex(link, page_id, pagination_page, wizard)
{
  prepForAjax(link);
  
  if (pagination_page == null) {
    pagination_page = 1;
    $('#quotes').fadeOut("fast", function(){
      $('#quotes_loader').fadeIn("fast", ajaxCall);
    });
  }
  else
  {
    ajaxCall();
  }
  
  function ajaxCall()
  {
    if(!wizard || wizard == null)
    {
      var theurl = '/pages/'+page_id+'/quotes?page='+pagination_page
    }
    else
    {
      var theurl = '/pages/'+page_id+'/quotes?page='+pagination_page+'&wizard=true';
    }
    
    $.ajax({
      type: 'GET',
      url: theurl,
      async: false,
      beforeSend: function(){
        error_displayed = false;
      },
      error: function(){
        $('#quotes_loader').fadeOut("fast", function(){
          $('#quotes').fadeIn("slow", function(){
            requestFailed(); 
          });
        }); 
      },
      success: function(responseHTML){
        document.getElementById('quotes').innerHTML = responseHTML;
        $('#quotes_loader').fadeOut("fast", function(){
          $('#quotes').fadeIn("slow");
          createTooltips();
        });       
      }            
    });    
  }
  
  ajaxComplete(link);  
}

function createPageQuote(page_id, wizard)
{
  if(!wizard || wizard == null)
  {
    var theurl = '/pages/'+page_id+'/quotes';
  }
  else
  {
    var theurl = '/pages/'+page_id+'/quotes?wizard=true';
  }
  
  $.ajax({
    type: 'POST',
    url: theurl,
    data: $('#new_quote').serialize(),
    beforeSend: function(){
      error_displayed = false;     
      $('#quotes').fadeOut("fast", function(){
        $('#quotes_loader').fadeIn("fast");
      });
    },
    error: function(){
      $('#quotes_loader').fadeOut("fast", function(){
        $('#quotes').fadeIn("slow", function(){
          requestFailed(); 
        });
      });      
    },
    success: function(responseHTML){
      document.getElementById('quotes').innerHTML = responseHTML;
      if (document.getElementById('quotes_and_links_li') && document.getElementById('quotes_and_links_li').getAttribute('class').indexOf('complete') == -1) {
        document.getElementById('quotes_and_links_li').setAttribute('class', document.getElementById('quotes_and_links_li').getAttribute('class')+' complete');       
      }
      $('#quotes_loader').fadeOut("fast", function(){
        $('#quotes').fadeIn("slow", function(){
          document.getElementById('quotes_loader').style.display = 'none';
          createTooltips();          
        });
      });           
    }        
  });
}

function updatePageQuote(page_id, quote_id, wizard)
{
  if(!wizard || wizard == null)
  {
    var theurl = '/pages/'+page_id+'/quotes/'+quote_id;
  }
  else
  {
    var theurl = '/pages/'+page_id+'/quotes/'+quote_id+'?wizard=true';
  }   

  $.ajax({
    type: 'POST',
    url: theurl,
    data: $('#edit_quote_'+quote_id).serialize(),
    beforeSend: function(){
      error_displayed = false;
      $('#quotes').fadeOut("fast", function(){
        $('#quotes_loader').fadeIn("fast");
      });
    },
    error: function(){
      $('#quotes_loader').fadeOut("fast", function(){
        $('#quotes').fadeIn("slow", function(){
          requestFailed(); 
        });
      });      
    },
    success: function(responseHTML){
      document.getElementById('quotes').innerHTML = responseHTML;
      $('#quotes_loader').fadeOut("fast", function(){
        $('#quotes').fadeIn("slow", function(){
          document.getElementById('quotes_loader').style.display = 'none';
          createTooltips();
        });
      });        
    }            
  });
}

function deletePageQuote(link, page_id, quote_id, authenticity_token, pagination_page, wizard)
{
  prepForAjax(link);

  if(!wizard || wizard == null)
  {
    var theurl = '/pages/'+page_id+'/quotes/'+quote_id;
  }
  else
  {
    var theurl = '/pages/'+page_id+'/quotes/'+quote_id+'?wizard=true';
  }  
  
  $.ajax({
    type: 'POST',
    url: theurl,
    data: 'authenticity_token='+encodeURIComponent(authenticity_token)+'&_method=delete&page='+pagination_page,
    async: false,
    error: requestFailed,
    success: function(responseHTML){
      document.getElementById('quotes').innerHTML = responseHTML;
      createTooltips();
    }    
  });
  
  ajaxComplete(link);
}

function updateQuoteCarousel(link, page_id)
{
  prepForAjax(link);

  $.ajax({
    type: 'GET',
    url: '/pages/'+page_id+'/quotes?carousel=true',
    async: false,
    beforeSend: function(){
      error_displayed = false;
      $('#quotes').fadeOut("fast", function(){
        $('#quotes_loader').fadeIn("fast");
      });
    },
    success: function(responseHTML){
      document.location = document.location;
    }     
  });
}

function getPageMessagesIndex(link, page_id, pagination_page)
{
  prepForAjax(link);
  
  if(pagination_page == null)
  {
    pagination_page = 1;
  } 

  $.ajax({
    type: 'GET',
    url: '/pages/'+page_id+'/messages?page='+pagination_page,
    async: false,
    beforeSend: function(){
      error_displayed = false;
    },    
    error: requestFailed,
    success: function(responseHTML){
      document.getElementById('messages').innerHTML = responseHTML;
      resizeImages('messages');
      setWmodeForFlash('messages');
    }
  });
    
  ajaxComplete(link);    
}

function createPageMessage(page_id, authenticity_token)
{
  $.ajax({
    type: 'POST',
    url: '/pages/'+page_id+'/messages',
    data: "authenticity_token="+encodeURIComponent(authenticity_token)+"&message%5Bbody%5D="+encodeURIComponent(tinyMCE.activeEditor.getContent()),
    beforeSend: function(){
      error_displayed = false;
      $('#edit_messages').fadeOut("fast", function(){
        $('#messages_loader').fadeIn("fast");
      });      
    },
    error: function(){
      $('#messages_loader').fadeOut("fast", function(){
        $('#edit_messages').fadeIn("slow", function(){
          requestFailed();
        });
      });      
    },
    success: function(responseHTML){
      document.getElementById('messages').innerHTML = responseHTML;
      resizeImages('messages');
      setWmodeForFlash('messages');
      createTooltips();        
      scroll(document.getElementById('messages').offsetLeft, document.getElementById('messages').offsetTop);
      tb_remove();      
    }        
  });   
}

function deletePageMessage(link, page_id, message_id, authenticity_token, pagination_page)
{
  prepForAjax(link);
  
  $.ajax({
    type: 'POST',
    url: '/pages/'+page_id+'/messages/'+message_id,
    data: 'authenticity_token='+encodeURIComponent(authenticity_token)+'&_method=delete&page='+pagination_page,
    async: false,
    beforeSend: function(){
      error_displayed = false;    
    },    
    error: requestFailed,
    success: function(responseHTML){
      document.getElementById('messages').innerHTML = responseHTML;
      resizeImages('messages');
      setWmodeForFlash('messages');
      createTooltips();
    }
  });
  
  ajaxComplete(link); 
}

function getPageStatusUpdatesIndex(link, page_id, pagination_page)
{
  prepForAjax(link);
  
  if(pagination_page == null)
  {
    pagination_page = 1;
  }   
  
  $.ajax({
    type: 'GET',
    url: '/pages/'+page_id+'/status_updates?page='+pagination_page,
    async: false,
    beforeSend: function(){
      error_displayed = false;
    },
    error: requestFailed,
    success: function(responseHTML){
      document.getElementById('status_updates').innerHTML = responseHTML;
      resizeImages('status_updates');
      setWmodeForFlash('status_updates');
    }        
  });
    
  ajaxComplete(link);  
}

function createPageStatusUpdate(page_id, authenticity_token)
{    
  $.ajax({
    type: 'POST',
    url: '/pages/'+page_id+'/status_updates',
    data: "authenticity_token="+encodeURIComponent(authenticity_token)+"&status_update%5Bsubject%5D="+encodeURIComponent(document.getElementById('status_update_subject').value)+"&status_update%5Bbody%5D="+encodeURIComponent(tinyMCE.activeEditor.getContent()),
    beforeSend: function(){
      error_displayed = false;
      $('#edit_status_updates').fadeOut("fast", function(){
        $('#status_updates_loader').fadeIn("fast");
      });      
    },
    error: function(){
      $('#status_updates_loader').fadeOut("fast", function(){
        $('#edit_status_updates').fadeIn("slow", function(){
          requestFailed();
        });
      });      
    },
    success: function(responseHTML){
      document.getElementById('status_updates').innerHTML = responseHTML;
      resizeImages('status_updates');
      setWmodeForFlash('status_updates');        
      createTooltips();
      scroll(document.getElementById('status_updates').offsetLeft, document.getElementById('status_updates').offsetTop);
      tb_remove();      
    }        
  });
}

function updatePageStatusUpdate(page_id, status_update_id, authenticity_token)
{
  $.ajax({
    type: 'POST',
    url: '/pages/'+page_id+'/status_updates/'+status_update_id,
    data: "_method=put&authenticity_token="+encodeURIComponent(authenticity_token)+"&status_update%5Bsubject%5D="+encodeURIComponent(document.getElementById('status_update_subject').value)+"&status_update%5Bbody%5D="+encodeURIComponent(tinyMCE.activeEditor.getContent())+"&page="+document.status_update.pagination_page.value,
    beforeSend: function(){
      error_displayed = false;
      $('#edit_status_updates').fadeOut("fast", function(){
        $('#status_updates_loader').fadeIn("fast");
      });      
    },
    error: function(){
      $('#status_updates_loader').fadeOut("fast", function(){
        $('#edit_status_updates').fadeIn("slow", function(){
          requestFailed();
        });
      });      
    },
    success: function(responseHTML){
      document.getElementById('status_updates').innerHTML = responseHTML;
      resizeImages('status_updates');
      setWmodeForFlash('status_updates');
      createTooltips();
      scroll(document.getElementById('status_updates').offsetLeft, document.getElementById('status_updates').offsetTop);
      tb_remove();      
    }         
  });
}

function deletePageStatusUpdate(link, page_id, status_update_id, authenticity_token, pagination_page)
{
  prepForAjax(link);
  
  $.ajax({
    type: 'POST',
    url: '/pages/'+page_id+'/status_updates/'+status_update_id,
    data: 'authenticity_token='+encodeURIComponent(authenticity_token)+'&_method=delete&page='+pagination_page,
    async: false,
    beforeSend: function(){
      error_displayed = false;    
    },
    error: requestFailed,
    success: function(responseHTML){
      document.getElementById('status_updates').innerHTML = responseHTML;
      resizeImages('status_updates');
      setWmodeForFlash('status_updates');
      createTooltips();
    }        
  });
  
  ajaxComplete(link);
}

function getPageImageGallery(link, page_id, iframe)
{
  if(iframe == null || !iframe)
  {
    $('#edit_page_images').fadeOut("fast", function(){
      $('#images_loader').fadeIn("fast", function(){
        document.location = document.location;
      });
    });        
  }
  else
  {
    parent.$('#edit_page_images').fadeOut("fast", function(){
      parent.$('#images_loader').fadeIn("fast", function(){
        parent.document.location = parent.document.location;
      });
    });        
  }  
/*   
  prepForAjax(link);
 
  $.ajax({
    type: 'GET',
    url: '/pages/'+page_id+'/images?partial=gallery',
    async: false,
    beforeSend: function(){
      error_displayed = false;

    },
    success: function(responseHTML){
      if(iframe == null || !iframe)
      {
        document.getElementById('profile_gallery_container').innerHTML = responseHTML;
        $('#photo_carousel').jcarousel({
          itemLoadCallback: {
            onBeforeAnimation: function(carousel, state){
              getNextImages(carousel, state, page_id);
            }
          }
        });
        $('#photo_slideshow').cycle({timeout: 10000, speed: 3000, pause: 1, sync: 1});            
      }
    }    
  });
  
  if(iframe == null || !iframe)
  {
    tb_remove();
  }
  else
  {
    parent.tb_remove();
  }
    
  ajaxComplete(link);
*/  
}

function getPageMembershipsIndex(link, page_id, pagination_page, wizard)
{
  prepForAjax(link); 
  
  if(pagination_page == null)
  {
    pagination_page = 1;
    $('#memberships').fadeOut("fast", function(){
      $('#memberships_loader').fadeIn("fast", ajaxCall);
    });    
  }
  else
  {
    ajaxCall();
  }
  
  function ajaxCall()
  {
    if(wizard == null || wizard == false)
    {
      var theurl = '/pages/'+page_id+'/memberships?page='+pagination_page;
    }
    else
    {
      var theurl = '/pages/'+page_id+'/memberships?page='+pagination_page+'&wizard=true';
    }      

    $.ajax({
      type: 'GET',
      url: theurl,
      async: false,
      beforeSend: function(){
        error_displayed = false;
      },
      error: function(){
        $('#memberships_loader').fadeOut("fast", function(){
          $('#memberships').fadeIn("slow", function(){
            requestFailed(); 
          });
        }); 
      },
      success: function(responseHTML){
        document.getElementById('memberships').innerHTML = responseHTML;
        $('#memberships_loader').fadeOut("fast", function(){
          $('#memberships').fadeIn("slow");
          createTooltips();
        });       
      }             
    });   
  }
    
  ajaxComplete(link);      
}

function createTempUsers(page_id, wizard)
{
  if(wizard == null || wizard == false)
  {
    var theurl = '/pages/'+page_id+'/memberships';
  }
  else
  {
    var theurl = '/pages/'+page_id+'/memberships?wizard=true';
  }   
  
  $.ajax({
    type: 'POST',
    url: theurl,
    data: $('#new_contacts').serialize(),
    beforeSend: function(){
      error_displayed = false;
      if(wizard == null || wizard == false)
      {
        $('#memberships').fadeOut("fast", function(){
          $('#memberships_loader').fadeIn("fast");
        });        
      }
      else
      {
        $('#wizard_content').fadeOut("fast", function(){
          $('#loader').fadeIn("fast");
        });            
      }
    },
    error: function(){
      if(wizard == null || wizard == false)
      {
        $('#memberships_loader').fadeOut("fast", function(){
          $('#memberships').fadeIn("slow", function(){
            requestFailed(); 
          });
        });                
      }
      else
      {
        $('#loader').fadeOut("fast", function(){
          $('#wizard_content').fadeIn("slow", function(){
            requestFailed(); 
          });
        });        
      }      
    },
    success: function(responseHTML){
      if(wizard == null || wizard == false)
      {
        document.getElementById('invite_step2').innerHTML = responseHTML;
        $('#invite_step1').hide();
        $('#invite_step2').show();
        $('#memberships_loader').fadeOut("fast", function(){
          $('#memberships').fadeIn("slow", function(){
            document.getElementById('memberships_loader').style.display = 'none';
            createTooltips();
          });
        });           
      }
      else
      {
        document.getElementById('invite_step2').innerHTML = responseHTML;
        $('#invite_step1').hide();
        $('#invite_step2').show();
        $('#loader').fadeOut("fast", function(){
          $('#wizard_content').fadeIn("slow");
          createTooltips();
        }); 
      }          
    }        
  });   
}

function createPageMemberships(page_id, wizard)
{
  if(wizard == null || wizard == false)
  {
    var theurl = '/pages/'+page_id+'/memberships';
  }
  else
  {
    var theurl = '/pages/'+page_id+'/memberships?wizard=true';
  }   
  
  $.ajax({
    type: 'POST',
    url: theurl,
    data: $('#new_memberships').serialize(),
    beforeSend: function(){
      error_displayed = false;
      if(wizard == null || wizard == false)
      {
        $('#memberships').fadeOut("fast", function(){
          $('#memberships_loader').fadeIn("fast");
        });        
      }
      else
      {
        $('#wizard_content').fadeOut("fast", function(){
          $('#loader').fadeIn("fast");
        });            
      }
    },
    error: function(){
      if(wizard == null || wizard == false)
      {
        $('#memberships_loader').fadeOut("fast", function(){
          $('#memberships').fadeIn("slow", function(){
            requestFailed(); 
          });
        });                
      }
      else
      {
        $('#loader').fadeOut("fast", function(){
          $('#wizard_content').fadeIn("slow", function(){
            requestFailed(); 
          });
        });        
      }      
    },
    success: function(responseHTML){
      if(wizard == null || wizard == false)
      {
        document.getElementById('memberships').innerHTML = responseHTML;
        $('#memberships_loader').fadeOut("fast", function(){
          $('#memberships').fadeIn("slow", function(){
            document.getElementById('memberships_loader').style.display = 'none';
            createTooltips();
          });
        });           
      }
      else
      {
        document.getElementById('wizard_content').innerHTML = responseHTML;
        
        updateNavBar(); 
        
        document.getElementById('invitations_li').setAttribute('class', document.getElementById('invitations_li').getAttribute('class')+' complete');       

        $('#loader').fadeOut("fast", function(){
          $('#wizard_content').fadeIn("slow");
        }); 
      }          
    }        
  });   
}

function updatePageMembership(page_id, membership_id, wizard)
{
  if(wizard == null || wizard == false)
  {
    var theurl = '/pages/'+page_id+'/memberships/'+membership_id;
  }
  else
  {
    var theurl = '/pages/'+page_id+'/memberships/'+membership_id+'?wizard=true';
  }   
  
  $.ajax({
    type: 'POST',
    url: theurl,
    data: $('#edit_membership_'+membership_id).serialize(),
    beforeSend: function(){
      error_displayed = false;     
      $('#memberships').fadeOut("fast", function(){
        $('#memberships_loader').fadeIn("fast");
      });
    },
    error: function(){
      $('#memberships_loader').fadeOut("fast", function(){
        $('#memberships').fadeIn("slow", function(){
          requestFailed(); 
        });
      });      
    },
    success: function(responseHTML){
      document.getElementById('memberships').innerHTML = responseHTML;
      $('#memberships_loader').fadeOut("fast", function(){
        $('#memberships').fadeIn("slow", function(){
          document.getElementById('memberships_loader').style.display = 'none';
          createTooltips();
        });
      });           
    }         
  });
}

function deletePageMembership(link, page_id, membership_id, authenticity_token, pagination_page, wizard)
{
  prepForAjax(link);

  if(!wizard || wizard == null)
  {
    var theurl = '/pages/'+page_id+'/memberships/'+membership_id;
  }
  else
  {
    var theurl = '/pages/'+page_id+'/memberships/'+membership_id+'?wizard=true';
  }
  
  $.ajax({
    type: 'POST',
    url: theurl,
    data: 'authenticity_token='+encodeURIComponent(authenticity_token)+'&_method=delete&page='+pagination_page,
    async: false,
    error: requestFailed,
    success: function(responseHTML){
      document.getElementById('memberships').innerHTML = responseHTML;
      createTooltips();
    }
  });
  
  ajaxComplete(link); 
}

function updatePageStyle(page_id)
{
  $.ajax({
    type: 'POST',
    url: '/pages/'+page_id,
    data: $('#edit_page_style').serialize(),
    beforeSend: function(){
      error_displayed = false;
    },
    error: requestFailed,
    success: function(){
      document.getElementById('patient_page_stylesheet').href = "/stylesheets/themes/"+document.edit_page_style.page_style_sheet.value+".css";
      tb_remove();
    }
  }); 
}

function updatePageSponsor(page_id)
{ 
  $.ajax({
    type: 'POST',
    url: '/pages/'+page_id,
    data: $('#edit_page_sponsor').serialize(),
    beforeSend: function(){
      error_displayed = false;
    },
    error: requestFailed,
    success: function(){
      document.location = document.location;
    }    
  });
    
/*
  var code = "0";
  
  if(document.edit_page_sponsor.page_sponsor_code.value != '')
  {
    code = document.edit_page_sponsor.page_sponsor_code.value;
  }
  
  var sponsor = 
    $.ajax({
      type: 'GET',
      url: '/check_sponsor_code/'+encodeURIComponent(code),
      async: false,
    }).responseText;
  if(sponsor == "")
  {
    sponsor = "default";
  }
  document.edit_page_sponsor.reset();
  document.getElementById("patient_page_stylesheet").setAttribute("href", "/stylesheets/"+sponsor+".css");
  
  tb_remove();
*/
}

function showStatusUpdateUserImage(status_update_id, user_id)
{
  $.ajax({
    type: 'GET',
    url: '/users/'+user_id+'/image',
    beforeSend: function(){
      error_displayed = false;
    },
    error: requestFailed,
    success: function(responseHTML){
      parent.document.getElementById('status_update_'+status_update_id+'_user_image').innerHTML = responseHTML;
    }     
  }); 
}

function showMessageUserImage(message_id, user_id)
{
  $.ajax({
    type: 'GET',
    url: '/users/'+user_id+'/image',
    beforeSend: function(){
      error_displayed = false;
    },
    error: requestFailed,
    success: function(responseHTML){
      parent.document.getElementById('message_'+message_id+'_user_image').innerHTML = responseHTML;
    } 
  }); 
}

function createFeedback(sponsor_id)
{
  if(sponsor_id != null)
  {
    var theurl = '/sponsors/'+sponsor_id+'/feedbacks'
  }
  else
  {
    var theurl = '/feedbacks'
  }
  $.ajax({
    type: 'POST',
    url: theurl,
    data: $('#feedback').serialize(),
    beforeSend: function(){
      error_displayed = false;
      $('#new_feedback').fadeOut("fast", function(){
        $('#feedback_loader').fadeIn("fast");
      });      
    },
    error: function(){
      $('#feedback_loader').fadeOut("fast", function(){
        $('#new_feedback').fadeIn("slow", function(){
          requestFailed();
        });
      });      
    },
    success: function(responseHTML){
      document.getElementById('new_feedback').innerHTML = responseHTML;
      $('#feedback_loader').fadeOut("fast", function(){
        $('#new_feedback').fadeIn("slow");
      });      
    }
  });  
}

function createSupportTicket(page_id)
{
  if(page_id != null)
  {
    var theurl = '/pages/'+page_id+'/support_tickets'
  }
  else
  {
    var theurl = '/support_tickets'
  }
    
  $.ajax({
    type: 'POST',
    url: theurl,
    data: $('#support_ticket').serialize(),
    beforeSend: function(){
      error_displayed = false;
      $('#new_support_ticket').fadeOut("fast", function(){
        $('#support_ticket_loader').fadeIn("fast");
      });      
    },
    error: function(){
      $('#support_ticket_loader').fadeOut("fast", function(){
        $('#new_support_ticket').fadeIn("slow", function(){
          requestFailed();
        });
      });      
    },
    success: function(responseHTML){
      document.getElementById('new_support_ticket').innerHTML = responseHTML;
      $('#support_ticket_loader').fadeOut("fast", function(){
        $('#new_support_ticket').fadeIn("slow");
      });      
    }
  });    
}

function sortPageImages(link, page_id, authenticity_token, wizard)
{
  prepForAjax(link);
 
  $.ajax({
    type: 'POST',
    url: '/pages/'+page_id,
    data: "_method=put&authenticity_token="+encodeURIComponent(authenticity_token)+"&"+$('#sortable_images').sortable('serialize'),
    async: false,
    beforeSend: function(){
      error_displayed = false;
    },
    error: requestFailed,
    success: function(){
      if(wizard == null || !wizard)
      {
        document.location = '/pages/'+page_id+'/images?layout=blank';  
      }
      else
      {
        document.location = '/pages/'+page_id+'/images?layout=blank&wizard=true';
      }
    }    
  });
  
  ajaxComplete(link);  
}

function createPasswordResetCode(link, validator)
{
  prepForAjax(link);

  if($("#username").valid())
  {
    var success = false;
    var ajax_response = null;
    
    $('#signin_form').slideUp("slow", function(){
      $.ajax({
        type: 'POST',
        url: '/passwords/create',
        data: $('#login_form').serialize(),
        async: false,
        beforeSend: function(){
          error_displayed = false;      
        },
        error: function(){
          requestFailed();
        },
        success: function(response){
          if (response == 'success') {
            success = true;          
          }
          else 
          {
            ajax_response = response;          
          }       
        }
      });
      if(success)
      {
        $('#reset_link_sent').slideDown("slow");
      }
      else{
        $('#signin_form').slideDown("slow", function(){
          if(ajax_response != null)
          {
            $('#login_form').validate().showErrors({"username": ajax_response});
          }
        });  
      }      
    }); 
  }
  
  ajaxComplete(link);   
}

function activatePage(link, page_id, authenticity_token)
{
  prepForAjax(link);
  
  $.ajax({
    type: 'POST',
    url: '/pages/'+page_id,
    data: "_method=put&authenticity_token="+encodeURIComponent(authenticity_token)+"&page[is_active]=true",
    async: false,
    beforeSend: function(){
      error_displayed = false;     
    },
    error: function(){
      requestFailed();  
    },
    success: function(response){
      $('#user_home_right').html(response);                    
    }
  });
    
  ajaxComplete(link);
}

function deactivatePage(page_id, authenticity_token)
{ 
  $.ajax({
    type: 'POST',
    url: '/pages/'+page_id,
    data: "_method=put&authenticity_token="+encodeURIComponent(authenticity_token)+"&page[is_active]=false&page%5Bclosing_message%5D="+encodeURIComponent(tinyMCE.activeEditor.getContent()),
    beforeSend: function(){
      error_displayed = false;
      $('#closing_message_form').fadeOut("fast", function(){
        $('#closing_message_loader').fadeIn("fast");
      });      
    },
    error: function(){
      $('#closing_message_loader').fadeOut("fast", function(){
        $('#closing_message_form').fadeIn("slow", function(){
          requestFailed();
        });
      });      
    },
    success: function(response){
      $('#user_home_right').html(response);
      tb_remove();
      createTooltips();   
    } 
  });
}

function toggleSponsor(link, page_id, new_is_active_value, authenticity_token)
{
  prepForAjax(link);
  
  $.ajax({
    type: 'POST',
    url: '/admin/sponsors/'+page_id,
    data: "_method=put&authenticity_token="+encodeURIComponent(authenticity_token)+"&sponsor[is_active]="+new_is_active_value,
    async: false,
    beforeSend: function(){
      error_displayed = false;     
    },
    error: function(){
      requestFailed();  
    },
    success: function(response){
      window.location = window.location;                    
    }
  });
    
  ajaxComplete(link);
}

function getPageImage(page_id, image_id)
{
  if(!(document.getElementById('page_image_'+image_id+'_tb').getElementsByTagName('img').length > 1))
  {
    $.ajax({
      type: 'GET',
      url: '/pages/'+page_id+'/images/'+image_id+'.json',
      async: false,
      success: function(response)
      {
        var image = JSON.parse(response)
        document.getElementById('page_image_'+image_id+'_tb').innerHTML =
        '<img src="'+image.public_filename+'">'+document.getElementById('page_image_'+image_id+'_tb').innerHTML;
      }
    });
  }
}

function getNextImages(carousel, state, page_id)
{
  if(state == 'next')
  {
    $.ajax({
      type: 'GET',
      url: '/pages/'+page_id+'/images.json?offset='+(carousel.size())+'&limit=5',
      async: false,
      success: function(response){
        var images = JSON.parse(response)
        for(i=0; i < 5; i++)
        {
          if(images[i])
          {
            carousel.add(carousel.size()+1, getCarouselItemHTML(images[i], page_id));
            carousel.size(carousel.size()+1);
          }
        }
      }
    });
  }
}

function getNextImage(curr, next, opts, page_id)
{
  // make sure we don't call addSlide before it is defined 
  if (opts.addSlide) 
  {
    $.ajax({
      type: 'GET',
      url: '/pages/'+page_id+'/images.json?offset='+document.getElementById("photo_slideshow").getElementsByTagName('li').length+'&limit=1',
      async: false,
      success: function(response){
        var image = JSON.parse(response)
        if(image[0])
        {
          opts.addSlide('<li class="tester">'+getSlideshowItemHTML(image[0], page_id)+'</li>'); 
        }
      }
    });   
  }
}

function createInvitationRequest()
{    
  $.ajax({
    type: 'POST',
    url: '/invitation_request',
    data: $('#invitation_request').serialize(),
    beforeSend: function(){
      error_displayed = false;
      $('#new_invitation_request').fadeOut("fast", function(){
        $('#invitation_request_loader').fadeIn("fast");
      });      
    },
    error: function(){
      $('#invitation_request_loader').fadeOut("fast", function(){
        $('#new_invitation_request').fadeIn("slow", function(){
          requestFailed();
        });
      });      
    },
    success: function(responseHTML){
      document.getElementById('new_invitation_request').innerHTML = responseHTML;
      $('#invitation_request_loader').fadeOut("fast", function(){
        $('#new_invitation_request').fadeIn("slow");
      });      
    }
  });    
}

function openOldPageAccess(link)
{
  prepForAjax(link);
  tb_show(null, "#TB_inline?height=250&width=350&inlineId=old_page_access_tb&modal=true", false);  

  $.ajax({
    type: 'GET',
    url: '/old_page_access/new',
    async: false,
    beforeSend: function(){
      error_displayed = false;
    },
    error: function(){
      requestFailed();
      tb_remove();
    },
    success: function(responseHTML){
      document.getElementById('old_page_access').innerHTML = responseHTML;

      $('#old_page_access_loader').fadeOut("fast", function(){
        $('#old_page_access').fadeIn("slow");
        createTooltips();
      });      
    }   
  });
  
  ajaxComplete(link);  
}
