﻿
var rootUrl = '/Saude/';
var offline = false;
var isAuthenticatedCache = false;

$(function() {
    isAuthenticated();
})

function ajaxProxy(webservice, method, jsonstring, callback) {
    $.ajax({
        type: "POST",
        url: rootUrl + "WebServices/" + webservice + ".asmx/" + method,
        data: jsonstring,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(response) {
            callback((typeof response.d) == 'string' ? eval('(' + response.d + ')') : response.d);
        },
        error: function(xhr, textStatus, errorThrown) {
            callback({ 'Error': 'Error na requisição: ' + xhr.statusText });
        }
    });
}

function formReset(form) {
    $(form + " input[type='text']").each(function() { $(this).val(""); });
    $(form + " textarea").each(function() { $(this).html(""); });
}

function isAuthenticated() {
    if (isAuthenticatedCache)
        return true;

    ajaxProxy('WsForum', 'IsAuthenticated', '{}',
        function(result) {
            if (result.Error) {
                
            }
            else {
                isAuthenticatedCache = result.IsAuthenticated                
                if (isAuthenticatedCache) {                    
                    $('a#linkLogin').attr('href', '/Br/Logout.aspx');
                    $('a#linkLogin').html('Logout');
                }                
                return isAuthenticatedCache;
            }
        }
    );
}

//// Replace the normal jQuery getScript function with one that supports
//// debugging and which references the script files as external resources
//// rather than inline.    
$.extend({
   getScript: function(url, callback) {
      var head = document.getElementsByTagName("head")[0];
      var script = document.createElement("script");
      script.src = url;

      // Handle Script loading
      {
         var done = false;

         // Attach handlers for all browsers
         script.onload = script.onreadystatechange = function(){
            if ( !done && (!this.readyState ||
                  this.readyState == "loaded" || this.readyState == "complete") ) {
               done = true;
               if (callback)
                  callback();

               // Handle memory leak in IE
               script.onload = script.onreadystatechange = null;
            }
         };
      }

      head.appendChild(script);

      // We handle everything using the script element injection
      return undefined;
   }
});

// remove HTML tags and blank spaces
jQuery.extend({
   clearText: function(text) {
      var result = text.replace(/<[^>]*>/g, '');
      result = $.trim(result);
      result = result.replace(/^[\s(&nbsp;)]+/g, '');
      
      return result;
   }
});

// replace invalid character to json
jQuery.extend({
   jsonText: function(text) {    
      return text.replace("\'", "").replace("\\", "\\\\");
   }
});

// truncate
jQuery.extend({
   truncateText: function(text, len) {    
      return text.substring(0, (text.length > len ? len : text.length));
   }
});