﻿var cmsidglobal;
function getdbcontent(cmsid) {
    cmsidglobal = cmsid;
    $.ajax({
        type: "POST",
        url: "blog_webservice.asmx/getdbcontent",
        data: "{'cmsid': '" + cmsid + " '}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            AjaxDBContentSucceeded(msg);
        },
        error: AjaxDBContentFailed
        });
    }
function AjaxDBContentSucceeded(result) {
    $('#ctl00_contentMain_pnlLeft').html('');
    $('#ctl00_contentMain_pnlBlogContainer').html('');
    if (result.d == 'False') {
        $('#ctl00_contentMain_pnlBlogTitle').html('<strong><font color=red>' + cmsidglobal + ' content not found</font></strong>');
    } else {
        $('#ctl00_contentMain_pnlBlogTitle').html(result.d);
    }
    return false;
}
function AjaxDBContentFailed(result) {
    $('#ctl00_contentMain_pnlLeft').html('');
    $('#ctl00_contentMain_pnlBlogContainer').html('');
    $('#ctl00_contentMain_pnlBlogTitle').html(result.d);
    return false;
}

/* For latest articles */
function getBlogEntry(blogid, logonlevel, userid) {
    alert(blogid + '\n' + logonlevel + '\n' + userid);
    $.ajax({
        type: "POST",
        url: "blog_webservice.asmx/getblogentry",
        data: "{'PostRecordID': '" + blogid + "', 'LogonLevel': '" & logonlevel & "', 'UserId':'" & userid & "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            AjaxBlogEntrySucceeded(msg);
        },
        error: function(xhr, ajaxOptions, thrownError) {
            alert(thrownError);
            AjaxBlogEntryFailed(thrownError);
        }
    });
}
function getBlogEntryTitles(numRecords) {
    $.ajax({
        type: "POST",
        url: "blog_webservice.asmx/getblogentrytitles",
        data: "{'minimumtoshow': '" + numRecords + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
        AjaxBlogEntryTitlesSucceeded(msg);
        },
        error: function(xhr, ajaxOptions, thrownError) {
            alert(thrownError);
            AjaxBlogEntryTitlesFailed(thrownError);
        }
    });
}
  
  function AjaxBlogEntrySucceeded(result) {
    if (result.d=='False') {
      $('#ctl00_contentMain_pnlBlog').html('<strong><font color=red>Invalid Blog Entry ID</font></strong>');
      
    } else {
      $('#ctl00_contentMain_pnlBlog').html(result.d);
    }
    return false;
  }
  function AjaxBlogEntryFailed(result) {
    $('#ctl00_contentMain_pnlBlog').html(result.d);
    return false;
  }
  function AjaxBlogEntryTitlesSucceeded(result) {
    if (result.d=='False') {
      $('#posts_ul').html('<strong><font color=red>Error retrieving blog entry titles</font></strong>');
    } else {
      $('#posts_ul').html(result.d);
    }
    return false;
  }
  function AjaxBlogEntryTitlesFailed(result) {
      $('#posts_ul').html(result.d);
    return false;
  }

  /* For Sign In */
  $(document).ready(function() {
      $("#btnsignin").click(function(event) {
          $.ajax({
              type: "POST",
              url: "login_webservice.asmx/login",
              data: "{'username': '" + $('#username').val() + " ','password': '" + $('#password').val() + " ','rememberme': '" + $('#rememberme').attr('checked') + " '}",
              contentType: "application/json; charset=utf-8",
              dataType: "json",
              success: function(msg) {
                  AjaxLoginSucceeded(msg);
              },
              error: AjaxLoginFailed
          });
      });
      $("#btnsignout").click(function(event) {
          $.ajax({
              type: "POST",
              url: "login_webservice.asmx/logout",
              data: "{}",
              contentType: "application/json; charset=utf-8",
              dataType: "json",
              success: function(msg) {
                  AjaxLogoutSucceeded(msg);
              },
              error: AjaxLogoutFailed
          });
      });
  });
  function AjaxLoginSucceeded(result) {
      if (result.d == 'False') {
//alert('login_fail1');
          $('#login_status').html('<strong><font color=red>Invalid Login</font></strong>');
          $('#userrealname').html('');
          $('#login').show();
          $('#logout').hide();
      } else {
//alert('login_success');
          $('#login_status').html('');
          $('#userrealname').html('<i>Welcome back</i><br />' + result.d + '<br /><br />');
          $('#login').hide();
          $('#logout').show();
      }
      return false;
  }
  function AjaxLoginFailed(result) {
//alert('login_fail2');
      $('#login_status').html(result.d);
      return false;
  }
  function AjaxLogoutSucceeded(result) {
      if (result.d == 'False') {
          $('#login_status').html(result.d);
          $('#login').hide();
          $('#logout').show();
      } else {
          $('#login').show();
          $('#logout').hide();
          $('#username').attr({ value: '' });
          $('#password').attr({ value: '' });
          $('#rememberme').attr({ checked: false });
      }
      return false;
  }
  function AjaxLogoutFailed(result) {
      $('#login_status').html(result.d);
      return false;
  }

  /* For Now Displaying */
  function NowDisplayed(strText) {
      $('#nowDisplayedContent').html(strText);
      return false;
  }