﻿if(!Patents) var Patents={};
if(!Patents.Service) Patents.Service={};

Patents.Service = 
{
    getPatentByID: function(patentID, siteLanguage, titleDivID, abstractDivID) 
    {
        $(document).ready(function() {
            $.ajax({
              type: "POST",
              url: "/WebServices/patentservice.asmx/GetAPatentLess",
              data: "{'patentID':'" + patentID + "','siteLanguage':'" + siteLanguage + "'}",
              cache: false,
              contentType: "application/json; charset=utf-8",
              dataType: "json",
              success: function(msg) {
                $(titleDivID).html(msg.Title);
                $(abstractDivID).html(msg.Abstract);
              },
              error:function(XMLHttpRequest, textStatus, errorThrown){
                alert( "Error Occured!" );
              }
            });
        });
    },
    
    searchPatents: function(query, siteLanguage, hpp, pNum, transform, resultTableID, lblTotalTop, lblShowingTop, lblTotalPageTop, ddlPageNumTop) 
    {
        $(document).ready(function() {
            $.ajax({
              type: "POST",
              url: "/WebServices/patentservice.asmx/searchPatents",
              data: "{'query':'" + query + "','siteLanguage':'" + siteLanguage + "','hpp':'" + hpp + "','pNum':'" + pNum + "','transform':'" + transform + "'}",
              cache: false,
              contentType: "application/json; charset=utf-8",
              dataType: "json",
              success: function(msg) {
                var totalNoHits = parseInt(msg.totalNoHits);
                var from = (pNum - 1) * hpp + 1;
                var to = (pNum * hpp > totalNoHits) ? totalNoHits : pNum * hpp;
                
                var totalPage = Math.round((totalNoHits + parseInt(hpp) - 1) / parseInt(hpp));
                var totalViewingPage = (parseInt(pNum)+100 > totalPage)?totalPage:parseInt(pNum) + 100;
                var currentViewingPage = (parseInt(pNum) - 100 <= 0) ? 1 : parseInt(pNum) - 100;
                
                $(lblTotalPageTop).html(totalPage);
                var options = '';
                for (var i = currentViewingPage; i <= totalViewingPage; i++)
                {
	                options += '<option value="' + i + '">' + i ;
                }
                $(ddlPageNumTop).html(options);
                //hide loading message
                $(resultTableID + " tr").hide();
                
                $(lblTotalTop).html(msg.totalNoHits);
                $(lblShowingTop).html(from + "-" + to);
                
                for(var i = 0; i<msg.hits.length; i++)
                {
                    var hitNum = hpp * (pNum - 1) + i ;
                    var ev = "odd";
                    if(i%2 == 0)ev = "even";
                     
                    $(resultTableID).append($("<div>").getTransform(
                       "/_XSLT/US/SearchResults.xslt", 
                        msg.hits[i].excerpt,
                        {params:{hitnumber:hitNum,sitelanguage:siteLanguage,key:msg.hits[i].key}}
                        ).children().addClass(ev)[1]);
                 }
              },
              error:function(XMLHttpRequest, textStatus, errorThrown){
                alert( "Error Occured!" );
              }
            });
        });
    },
    
    showPreviewTab: function(patentID, divID, tabDivID)
    {
        $(document).ready(function() {
            $.ajax({
              type: "POST",
              url: "/WebServices/patentservice.asmx/GetPDFLink",
              data: "{'patentID':'" + patentID + "'}",
              cache: false,
              contentType: "application/json; charset=utf-8",
              dataType: "json",
              success: function(msg) {
                $(tabDivID).css("display","block"); 
                if(msg != "")
                   $(divID).css("display","block"); 
              }
            });
        });
    },
    
    getLatestNews: function(divID,xsltPath)
    {
        //setInterval(function() {
            $.ajax({
              type: "POST",
              url: "/WebServices/newsservice.asmx/GetLatestNews",
              data: "{}",
              cache: false,
              contentType: "application/json; charset=utf-8",
              dataType: "text",
              success: function(msg) {
                Patents.Service.ApplyXSLT(msg, divID, xsltPath);
              }
            });
        //}, 3000);
    },
    
    getAssignees: function(patentID, divID,templateURL)
    {
        $.ajax({
          type: "POST",
          url: "/WebServices/assigneeservice.asmx/GetAssignees",
          data: "{'patentID':'" + patentID + "'}",
          cache: false,
          contentType: "application/json; charset=utf-8",
          dataType: "json",
          success: function(msg) {
            if(msg == "")
                Patents.Service.ApplyTemplate(msg, divID, "/_JTemplates/None.aspx");
            else
            {
               $(divID).empty();
               for(var i = 0; i<msg.length; i++)
               {
                   var name = msg[i].OrganizationName;
                   $(divID).append("<input type='radio' name='assignees' id='" + name + "' value='" + name + "'/>&nbsp;" + name + "<br/>");
               } 
            } 
          }
        });
    },
    
    getInventors: function(patentID, divID,templateURL)
    {
        $.ajax({
          type: "POST",
          url: "/WebServices/inventorservice.asmx/GetInventors",
          data: "{'patentID':'" + patentID + "'}",
          cache: false,
          contentType: "application/json; charset=utf-8",
          dataType: "json",
          success: function(msg) {
            if(msg == "")
                Patents.Service.ApplyTemplate(msg, divID, "/_JTemplates/None.aspx");
            else
            {
                $(divID).empty();
                for(var i = 0; i<msg.length; i++)
                {
                    var name = "";
                    var displayName = "";
                    
                    if(msg[i].Middlename != "" && msg[i].Middlename != undefined)
                    {
                        name = msg[i].Lastname + msg[i].Middle + msg[i].Firstname;
                        displayName = " " + msg[i].Firstname + " " + msg[i].Middlename + " " + msg[i].Lastname;
                    }else{
                        name = msg[i].Lastname + msg[i].Firstname;
                        displayName = " " + msg[i].Firstname + " " + msg[i].Lastname;
                    }
                    $(divID).append("<input type='radio' name='inventors' id='" + name + "' value='" + name + "'/> " + displayName + "<br/>");
                }
            }
          }
        });
    },
    
    getAttorneys: function(patentID, divID,templateURL)
    {
        $.ajax({
          type: "POST",
          url: "/WebServices/attorneyservice.asmx/GetAttorneys",
          data: "{'patentID':'" + patentID + "'}",
          cache: false,
          contentType: "application/json; charset=utf-8",
          dataType: "json",
          success: function(msg) {
            if(msg == "")
                Patents.Service.ApplyTemplate(msg, divID, "/_JTemplates/None.aspx");
            else
            {
               $(divID).empty();
               for(var i = 0; i<msg.length; i++)
               {
                   var name = msg[i].OrganizationName;
                   $(divID).append("<input type='radio' name='attorneys' id='" + name + "' value='" + name + "'/> " + name + "<br/>");
               } 
            }    
          }
        });
    },
    
    getLatestWebUsers: function(divID,templateURL)
    {
        $(document).ready(function() {
            //setInterval(function() {
            $.ajax({
              type: "POST",
              url: "/WebServices/webuserservice.asmx/GetLastestWebUsers",
              data: "{}",
              cache: false,
              contentType: "application/json; charset=utf-8",
              dataType: "json",
              success: function(msg) {
                Patents.Service.ApplyTemplate(msg, divID, templateURL);
              }
            });
            //}, 3000);
        });
    },
    
    getLatestIPX: function(siteLanguage,divID,templateURL)
    {
        $(document).ready(function() {
            //setInterval(function() {
                $.ajax({
                  type: "POST",
                  url: "/WebServices/patentservice.asmx/GetLatestIPX",
                  data: "{'siteLanguage':'" + siteLanguage + "'}",
                  cache: false,
                  contentType: "application/json; charset=utf-8",
                  dataType: "json",
                  success: function(msg) {
                    Patents.Service.ApplyTemplate(msg, divID, templateURL);
                  }
                });
            //}, 3000);
        });  
    },
    
    getLatestClaimed: function(siteLanguage,divID,templateURL)
    {
        $(document).ready(function() {
            //setInterval(function() {
            $.ajax({
              type: "POST",
              url: "/WebServices/patentservice.asmx/GetLatestClaimed",
              data: "{'siteLanguage':'" + siteLanguage + "'}",
              cache: false,
              contentType: "application/json; charset=utf-8",
              dataType: "json",
              success: function(msg) {
                Patents.Service.ApplyTemplate(msg, divID, templateURL);
              }
            });
           //}, 3000);
        });
    },
    
    getFeaturedPatents: function(siteLanguage, divID,templateURL)
    {
        $(document).ready(function() {
            $.ajax({
              type: "POST",
              url: "/WebServices/patentservice.asmx/GetFeaturedPatents",
              data: "{'siteLanguage':'" + siteLanguage + "'}",
              cache: false,
              contentType: "application/json; charset=utf-8",
              dataType: "json",
              success: function(msg) {
                Patents.Service.ApplyTemplate(msg, divID, templateURL);
              }
            });
        });
    },
    
    getAFeaturedPatent: function(siteLanguage,divID,templateURL)
    {
        $(document).ready(function() {
            $.ajax({
              type: "POST",
              url: "/WebServices/patentservice.asmx/GetAFeaturedPatent",
              data: "{'siteLanguage':'" + siteLanguage + "'}",
              cache: false,
              contentType: "application/json; charset=utf-8",
              dataType: "json",
              success: function(msg) {
                Patents.Service.ApplyTemplate(msg, divID, templateURL);
              }
            });
        });
    },
    
    getAFeaturedTechnology: function(siteLanguage, divID,templateURL)
    {
        $(document).ready(function() {
            $.ajax({
              type: "POST",
              url: "/WebServices/patentservice.asmx/GetAFeaturedTechnology",
              data: "{'siteLanguage':'" + siteLanguage + "'}",
              cache: false,
              contentType: "application/json; charset=utf-8",
              dataType: "json",
              success: function(msg) {
                Patents.Service.ApplyTemplate(msg, divID, templateURL);
              }
            });
        });
    },
    
    getCommunityFeed: function(divID,templateURL)
    {
        $(document).ready(function() {
            $.ajax({
              type: "POST",
              url: "/WebServices/webuserservice.asmx/GetRandomLatestWebUserImages",
              data: "{}",
              cache: false,
              contentType: "application/json; charset=utf-8",
              dataType: "json",
              success: function(msg) {
                Patents.Service.ApplyTemplate(msg, divID, templateURL);
              }
            });
        });
    },
    
    getPDFLink: function(patentID, divID,templateURL)
    {
        $(document).ready(function() {
            $.ajax({
              type: "POST",
              url: '/WebServices/patentservice.asmx/GetPDFLink',
              data: "{'patentID':'" + patentID + "'}",
              cache: false,
              contentType: "application/json; charset=utf-8",
              dataType: "json",
              success: function(msg) {
                Patents.Service.ApplyTemplate(msg, divID, templateURL);
              }
            });
        });
    },
    
    ApplyTemplate: function(msg, divID, templateURL) {
      $(divID).setTemplateURL(templateURL);
      $(divID).processTemplate(msg);
    }, 
    
    ApplyXSLT: function(msg, divID, xsltPath)
    {
      $(divID).getTransform(xsltPath, msg);       
    }
}


