<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[2334] 2013/sayaksarkar/trunk: User Authentication module implemented.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://gsoc.trac.wordpress.org/changeset/2334">2334</a></dd>
<dt>Author</dt> <dd>sayaksarkar</dd>
<dt>Date</dt> <dd>2013-09-21 00:00:58 +0000 (Sat, 21 Sep 2013)</dd>
</dl>

<h3>Log Message</h3>
<pre>User Authentication module implemented.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#2013sayaksarkartrunkindexhtml">2013/sayaksarkar/trunk/index.html</a></li>
<li><a href="#2013sayaksarkartrunkmanifestwebapp">2013/sayaksarkar/trunk/manifest.webapp</a></li>
<li><a href="#2013sayaksarkartrunksourceaddexternaljs">2013/sayaksarkar/trunk/source/addexternal.js</a></li>
<li><a href="#2013sayaksarkartrunksourceappcss">2013/sayaksarkar/trunk/source/app.css</a></li>
<li><a href="#2013sayaksarkartrunksourcepackagejs">2013/sayaksarkar/trunk/source/package.js</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#2013sayaksarkartrunksourcexmlrpcservicejs">2013/sayaksarkar/trunk/source/xmlrpcservice.js</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="2013sayaksarkartrunkindexhtml"></a>
<div class="modfile"><h4>Modified: 2013/sayaksarkar/trunk/index.html (2333 => 2334)</h4>
<pre class="diff"><span>
<span class="info">--- 2013/sayaksarkar/trunk/index.html        2013-09-17 21:45:58 UTC (rev 2333)
+++ 2013/sayaksarkar/trunk/index.html   2013-09-21 00:00:58 UTC (rev 2334)
</span><span class="lines">@@ -4,20 +4,15 @@
</span><span class="cx">          <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
</span><span class="cx">          <title>WordPress</title>
</span><span class="cx">          <link rel="shortcut icon" href="assets/favicon.png"/>
</span><del>-               <!-- -->
</del><span class="cx">           <meta http-equiv="Content-Type" content="text/html; charset=utf8"/>
</span><del>-               <meta name="apple-mobile-web-app-capable" content="yes"/>
</del><span class="cx">           <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
</span><del>-               <!-- Less.js (uncomment for client-side rendering of less stylesheets; leave commented to use only CSS) -->
</del><span class="cx">           <!-- <script src="enyo/tools/minifier/node_modules/less/dist/less-1.3.0e.min.js"></script> -->
</span><del>-               <!-- enyo (debug) -->
</del><span class="cx">           <script src="enyo/enyo.js"></script>
</span><del>-               <!-- application (debug) -->
</del><span class="cx">           <script src="source/package.js" type="text/javascript"></script>
</span><span class="cx">  </head>
</span><span class="cx">  <body class="enyo-unselectable">
</span><span class="cx">          <script>
</span><del>-                       new wp.Login().renderInto(document.body);
</del><ins>+                        new wp.AddExternal().renderInto(document.body);
</ins><span class="cx">           </script>
</span><span class="cx">  </body>
</span><span class="cx"> </html>
</span></span></pre></div>
<a id="2013sayaksarkartrunkmanifestwebapp"></a>
<div class="modfile"><h4>Modified: 2013/sayaksarkar/trunk/manifest.webapp (2333 => 2334)</h4>
<pre class="diff"><span>
<span class="info">--- 2013/sayaksarkar/trunk/manifest.webapp   2013-09-17 21:45:58 UTC (rev 2333)
+++ 2013/sayaksarkar/trunk/manifest.webapp      2013-09-21 00:00:58 UTC (rev 2334)
</span><span class="lines">@@ -4,9 +4,9 @@
</span><span class="cx">     "launch_path": "/index.html",
</span><span class="cx">     "description": "WordPress for Firefox OS.",
</span><span class="cx">     "icons": {
</span><del>-          "30": "/images/icon30.png",
</del><ins>+             "30": "/images/icon30.png",
</ins><span class="cx">        "48": "/images/icon48.png",
</span><del>-          "60": "/images/icon60.png",
</del><ins>+             "60": "/images/icon60.png",
</ins><span class="cx">        "64": "/images/icon64.png",
</span><span class="cx">        "128": "/images/icon128.png"
</span><span class="cx">     },
</span><span class="lines">@@ -14,5 +14,13 @@
</span><span class="cx">         "name": "Sayak Sarkar",
</span><span class="cx">         "url": "http://sayak.in"
</span><span class="cx">     },
</span><ins>+    "installs_allowed_from": ["*"],
+    "permissions": {
+      "systemXHR": {
+        "description":"Required for remote syncing via XMLRPC."
+      }
+    },
+    "type": "privileged",
+    "orientation": "portrait-primary",
</ins><span class="cx">     "default_locale": "en"
</span><span class="cx"> }
</span></span></pre></div>
<a id="2013sayaksarkartrunksourceaddexternaljs"></a>
<div class="modfile"><h4>Modified: 2013/sayaksarkar/trunk/source/addexternal.js (2333 => 2334)</h4>
<pre class="diff"><span>
<span class="info">--- 2013/sayaksarkar/trunk/source/addexternal.js     2013-09-17 21:45:58 UTC (rev 2333)
+++ 2013/sayaksarkar/trunk/source/addexternal.js        2013-09-21 00:00:58 UTC (rev 2334)
</span><span class="lines">@@ -5,36 +5,64 @@
</span><span class="cx">  classes: "enyo-fit",
</span><span class="cx">  components: [
</span><span class="cx"> 
</span><ins>+               
</ins><span class="cx">           //Header Toolbar Definition
</span><span class="cx">          {kind: "onyx.Toolbar", layoutKind:"FittableColumnsLayout", classes: "toolbar", components: [
</span><del>-                       {kind: "onyx.Icon", src: "images/toolbar/wp.png", style: "margin-left:20px; height: 38px; width: 38px;"},
</del><ins>+                        {kind: "onyx.Icon", src: "images/toolbar/wp.png", style: "margin-left:10px; height: 38px; width: 38px;"},
</ins><span class="cx">                   {content: "WordPress", fit: true}
</span><span class="cx">          ]},
</span><ins>+               
</ins><span class="cx"> 
</span><del>-               {kind: "enyo.Image", classes: "banner", src: "images/wplogo.png"},
-
-               {content: "ACCOUNT DETAILS", style: "color: grey; margin-left: 20px; margin-top: -5px; margin-bottom: -10px; font-size: 12px; font-weight: bold;"},
</del><ins>+                {kind: "enyo.Image", classes: "logoBanner", src: "images/icon128.png", style: "margin-top: 10px;"},
+               
+               {content: "ACCOUNT DETAILS", style: "color: grey; margin-left: 20px; margin-bottom: -10px; font-size: 12px; font-weight: bold;"},
</ins><span class="cx">           {kind: "onyx.Groupbox", style: "margin-bottom: 10px;", components: [
</span><del>-                       {kind: "Input", name: "blogname", classes: "addExternalInputBox", placeholder: " Blog URL", type: "url", selectOnFocus: true},
-                       {kind: "Input", name: "user_name", classes: "addExternalInputBox", placeholder: " Username", type: "text", selectOnFocus: true},
-                       {kind: "Input", name: "pass1", classes: "addExternalInputBox", placeholder: " Password", type: "password", selectOnFocus: true}
</del><ins>+                        {kind: "Input", name: "blogurl", classes: "addExternalInputBox", placeholder: " Blog URL", type: "url", selectOnFocus: true},
+                       {kind: "Input", name: "username", classes: "addExternalInputBox", placeholder: " Username", type: "text", selectOnFocus: true},
+                       {kind: "Input", name: "password", classes: "addExternalInputBox", placeholder: " Password", type: "password", selectOnFocus: true}
</ins><span class="cx">           ]},
</span><span class="cx"> 
</span><ins>+               /*
</ins><span class="cx">           {kind: "onyx.Button", style: "margin-left: 20px;", content: "Optional Settings", ontap: "optionalSettings"},
</span><span class="cx">          {tag: "br"},
</span><del>-               {kind: "onyx.Button", style: "margin-top: 5px; margin-left: 210px;", content: "Sign In", ontap: "signIn"},
</del><ins>+                */
+               {id: "SignInButton", kind: "onyx.Button", style: "margin-top: 2px; margin-left: 100px;", content: "Sign In", ontap: "signIn"},
+               {tag: "br"},
+               {id: "status", style: "margin-top: -30px; text-align: center; font-size: 16px;", content: ""},
+               {id: "spinner", kind: "onyx.Spinner", classes: "onyx-light", style: "margin-left: 125px; visibility: hidden;"}
</ins><span class="cx"> 
</span><ins>+               /*
</ins><span class="cx">           {content: "Don't yet have a blog?", style: "margin-top: 10px; text-align: center;"},
</span><span class="cx">          {kind: "onyx.Button", style: "margin-left: 68px; height: 55px; width: 180px;", allowHtml: true, content: "Get a free blog at <br/> WordPress.com", ontap: "signUp"}
</span><ins>+               */
</ins><span class="cx">   ],
</span><ins>+
</ins><span class="cx">   signUp: function () {
</span><span class="cx">          new wp.Signup().renderInto(document.body);
</span><span class="cx">          //window.open("https://en.wordpress.com/signup/?ref=wp-android");
</span><span class="cx">  },
</span><ins>+
</ins><span class="cx">   signIn: function () {
</span><del>-               new wp.Posts().renderInto(document.body);
</del><ins>+                var url = fixURL(this.$.blogurl.getValue()) + "/xmlrpc.php";
+               var username = this.$.username.getValue();
+               var password = this.$.password.getValue();      
+               var params = [username, password];
+               var xmlrpc_data =  XMLRPCBuilder.marshal("wp.getUsersBlogs", params);
+               makeRequest(url, xmlrpc_data);
+               //new wp.Posts().renderInto(document.body);
</ins><span class="cx">   },
</span><ins>+
</ins><span class="cx">   optionalSettings: function() {
</span><span class="cx">          new wp.OptionalSettings().renderInto(document.body);
</span><span class="cx">  }
</span><del>-})
</del><span class="cx">\ No newline at end of file
</span><ins>+});
+
+function fixURL(url) {
+    if(url.substr(-1) == '/') {
+        return url.substr(0, url.length - 1);
+    }
+    if(url.match('^http://')){
+       url = url.replace('http://','https://')
+       }
+    return url;
+}
</ins><span class="cx">\ No newline at end of file
</span></span></pre></div>
<a id="2013sayaksarkartrunksourceappcss"></a>
<div class="modfile"><h4>Modified: 2013/sayaksarkar/trunk/source/app.css (2333 => 2334)</h4>
<pre class="diff"><span>
<span class="info">--- 2013/sayaksarkar/trunk/source/app.css    2013-09-17 21:45:58 UTC (rev 2333)
+++ 2013/sayaksarkar/trunk/source/app.css       2013-09-21 00:00:58 UTC (rev 2334)
</span><span class="lines">@@ -101,6 +101,13 @@
</span><span class="cx">  width: 300px;
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+.logoBanner{
+       padding-left: 96px;
+       margin-top: 12px;
+       margin-bottom: 12px;
+       /*width: 128px;*/
+}
+
</ins><span class="cx"> .frame {
</span><span class="cx">  height: 100%;
</span><span class="cx">  width: 100%;
</span></span></pre></div>
<a id="2013sayaksarkartrunksourcepackagejs"></a>
<div class="modfile"><h4>Modified: 2013/sayaksarkar/trunk/source/package.js (2333 => 2334)</h4>
<pre class="diff"><span>
<span class="info">--- 2013/sayaksarkar/trunk/source/package.js 2013-09-17 21:45:58 UTC (rev 2333)
+++ 2013/sayaksarkar/trunk/source/package.js    2013-09-21 00:00:58 UTC (rev 2334)
</span><span class="lines">@@ -16,5 +16,6 @@
</span><span class="cx">  "pagesettings.js",
</span><span class="cx">  "comments.js",
</span><span class="cx">  "reader.js",
</span><del>-       "menu.js"
</del><ins>+        "menu.js",
+       "xmlrpcservice.js"
</ins><span class="cx"> );
</span></span></pre></div>
<a id="2013sayaksarkartrunksourcexmlrpcservicejs"></a>
<div class="addfile"><h4>Added: 2013/sayaksarkar/trunk/source/xmlrpcservice.js (0 => 2334)</h4>
<pre class="diff"><span>
<span class="info">--- 2013/sayaksarkar/trunk/source/xmlrpcservice.js                           (rev 0)
+++ 2013/sayaksarkar/trunk/source/xmlrpcservice.js      2013-09-21 00:00:58 UTC (rev 2334)
</span><span class="lines">@@ -0,0 +1,424 @@
</span><ins>+  function makeRequest(url, data) {
+    var xhr = new XMLHttpRequest({mozSystem:true});
+    xhr.open('POST', url);
+    
+    xhr.onreadystatechange = function() {
+      console.log("Readystate: ", xhr.readyState);
+      if (xhr.readyState == 1) {
+        document.getElementById("SignInButton").style.visibility="hidden";
+        document.getElementById("status").style.color="#000000";
+        var reqStatus = "Signing in...";
+        document.getElementById("spinner").style.visibility="visible";
+      }
+       else if (xhr.readyState == 2) {
+        var reqStatus = "Status available..";
+      }
+      else if (xhr.readyState == 3) {
+        var reqStatus = "Downloading stuff....";
+      } 
+      else if(xhr.readyState == 4) {
+        var reqStatus = "<br/><br/>Invalid Username/Password.";
+        document.getElementById("SignInButton").style.visibility="visible";
+        document.getElementById("status").style.color="red";
+        document.getElementById("spinner").style.visibility="hidden";
+      }
+      document.getElementById("status").innerHTML = reqStatus;
+    }
+    
+    xhr.onload = function() {
+      handleSuccess(xhr);
+    };
+    
+    xhr.onerror = function() {
+      handleError(xhr);
+    };
+    
+    xhr.send(data);    
+    return xhr;
+  }
+
+  function handleSuccess(xhr) {
+    
+    //var respText = xhr.responseText;      
+     
+    var parser = new XMLRPCParser(xhr.response);
+    var json = parser.toObject();
+    if (parser.fault) {
+      /*alert("Parser Fault");
+      console.log(xhr.response);*/
+      return;
+    }
+    else {
+      new wp.Posts().renderInto(document.body);
+    }
+    console.log(json);
+  }
+
+  function handleError(xhr) {
+    alert("Error: " + xhr.statusText);
+  }
+
+
+XMLRPCBuilder = function(methodName, methodParams){
+  this.methodName = methodName;
+  this.methodParams = methodParams;
+}
+
+XMLRPCBuilder.marshal = function(methodName, methodParams){
+  return (new XMLRPCBuilder(methodName, methodParams)).toString();
+}
+
+XMLRPCBuilder.prototype.toString = function(){
+  return "<?xml version=\"1.0\"?>\n" +
+    "<methodCall>\n<methodName>" + this.methodName + "</methodName>\n" +
+    "<params>\n" + this.buildParams(this.methodParams) + "</params>\n</methodCall>";
+}
+
+XMLRPCBuilder.prototype.buildParams = function(params){
+  xml = "";
+  for (var i=0; i < params.length; i++) {
+    xml += "<param><value>" + this.encode(params[i]) +  "</value></param>\n";
+  };
+  return xml;
+}
+
+XMLRPCBuilder.prototype.encode = function(param){
+  var x = XMLRPCBuilder;
+  if (param instanceof Array) {
+    // collect each one and buildObject on it
+    var array = "<array>\n<data>\n";
+      for (var i=0; i < param.length; i++) {
+        array += "<value>" + this.encode(param[i]) + "</value>\n";
+      };
+    array += "</data>\n</array>\n";
+    return array;
+  } else if (param instanceof Function) {
+    return this.encode(param());
+  } else if (typeof(param) == "string") {
+    return "<string><![CDATA[" + param + "]]></string>";
+  } else if (typeof(param) == "number" || param instanceof Number) {
+    if (Math.round(param) == param) {
+      return "<i4>" + param + "</i4>";
+    }else{
+      return "<double>" + param + "</double>";
+    }
+  } else if (param instanceof Date || param.__proto__ instanceof Date || param.__proto__.constructor.name == 'Date') {
+    return "<dateTime.iso8601>" + XMLRPCBuilder.dateToIso8601(param) + "</dateTime.iso8601>";
+  // } else if (x.isObject(param)) {
+  } else {
+    var struct = "<struct>";
+    for (key in param) {
+      struct += "<member>";
+      struct += "<name>";
+      struct += key;
+      struct += "</name>";
+      struct += "<value>";
+      struct += this.encode(param[key]);
+      struct += "</value>";
+      struct += "</member>";
+    };
+    return struct + "</struct>";
+  }
+  throw("Don't know how to encode " + param.constructor + ": " + param.toString());
+}
+
+XMLRPCBuilder.dateToIso8601 = function(date){
+  year = date.getUTCFullYear();
+  month = date.getUTCMonth() + 1;
+  if (month < 10) month = "0" + month;     
+  day = date.getUTCDate();
+  if (day < 10) day = "0" + day;     
+  var minutes = date.getUTCMinutes();
+  if (minutes < 10) minutes = "0" + minutes; 
+  var seconds = date.getUTCSeconds();
+  if (seconds < 10) seconds = "0" + seconds;  
+  var hrs = date.getUTCHours()
+  if (hrs < 10) hrs = "0" + hrs;
+  time = hrs + ':' + minutes + ':' + seconds;
+  return " " + year + month + day + "T" + time;
+}
+
+XMLRPCBuilder.isNumber = function(param){
+  return (typeof(param) == 'number' || param instanceof Number);
+}
+
+XMLRPCBuilder.isDate = function(param) {
+  if (param == null) return false;
+  //console.log(">>> XMLRPCBuilder.isDate testing the paramter datatype: ", param,  typeof(param));  
+
+  //console.log("hey: ", param.__proto__.constructor.name ); //this does the trick
+  return (param instanceof Date || param.__proto__ instanceof Date || param.__proto__.constructor.name == 'Date');
+}
+
+XMLRPCBuilder.isString = function(param){
+  return typeof param == 'string';
+}
+
+XMLRPCBuilder.isInt = function(param){
+  return Math.round(param) == param;
+}
+
+XMLRPCBuilder.isObject = function(param){
+  return (param instanceof Object || param.constructor == Object);
+}
+
+XMLRPCParser = function(string){
+  
+  //TODO: clean the document with tidy. I've not found any js implementation of HTML tidy. this is bad.
+  //NOTE: the DOMParser object is an HTML Dom parser, not XML, so it does not require "valid" XML so we may not need HTML tidy.
+    
+  //remove the BOM bytes, white spaces
+  var cleaned = XMLRPCParser.protectStrings(XMLRPCParser.cleanBOM(XMLRPCParser.cleanDocument(string)));
+  this.document = doc = new DOMParser().parseFromString(cleaned, 'text/xml');
+  //console.log("Parsed DOM", doc);
+}
+
+XMLRPCParser.parse = function(string){
+  return (new XMLRPCParser(string)).toObject();
+}
+
+// prevents empty text nodes. and garbage between non-content XML-RPC nodes
+XMLRPCParser.cleanDocument = function(string){
+  var open, pairs = [
+    ['methodResponse', 'params'],
+    ['params', 'param'],
+    ['param', 'value'],
+    ['array', 'data'],
+    ['struct', 'member']
+  ], open;
+  var values = ['array','string', 'i4', 'int', 'dateTime\\.iso8601', 'double', 'struct'];
+  pairs.push(['value', values.join("|")]);
+  var pair, reg;
+  for (var i=0; i < pairs.length; i++) {
+    pair = pairs[i];
+    open = new RegExp("<" + pair[0] + ">.*?<(" + pair[1] + ")>",'gmi');
+    string = string.replace(open,"<"+pair[0]+"><$1>");
+  };
+  
+  return string.replace(/>\n[\s]+\n</g, '><');
+}
+
+// Removes all characters before the opening XLM declaration
+XMLRPCParser.cleanBOM = function(string){
+  var leading_removed = string.substring(string.indexOf("<"));
+  return leading_removed.replace(/^[^<]{0,}<\?xml([^>]+)>[^<]+<methodResponse/, "<?xml version=\"1.0\" ?>\n<methodResponse");
+}
+
+// wrap the strings in <![CDATA[ ]]>
+// We are treating all string values as potentially malformed XML. The parser will not attempt to process any of the
+// potentially dangerous copy
+XMLRPCParser.protectStrings = function(string){
+  var cleaned = string.replace(/<string>([\s]+<\!\[CDATA\[)?/gi, "<string><![CDATA[").replace(/<\/string>/gi, "]]></string>");
+  return  cleaned;
+  
+}
+
+// TODO: Remove XPath and start using querySelector since we're dealing with a DOM object
+XMLRPCParser.prototype.toObject = function(){
+  // the the method response value
+  var fault = this.queryDocument('/methodResponse/fault/value/*')[0];
+  if(fault){
+    this.fault = true;
+    return this.parse(fault);
+  }
+
+  //checks for DOM fault
+/*
+ * <methodresponse>​
+<parsererror style=​"display:​ block;​ white-space:​ pre;​ border:​ 2px solid #c77;​ padding:​ 0 1em 0 1em;​ margin:​ 1em;​ background-color:​ #fdd;​ color:​ black">​
+<h3>​This page contains the following errors:​</h3>​
+<div style=​"font-family:​monospace;​font-size:​12px">​
+"error on line 218 at column 59: Input is not proper UTF-8, indicate encoding !
+Bytes: 0x1B 0x5B 0x20 0xC2
+"
+</div>​
+<h3>​Below is a rendering of the page up to the first error.​</h3>​
+</parsererror>​
+<params>​…​</params>​*/  
+  fault = this.document.getElementsByTagName("parsererror")[0];
+  if(fault){
+    this.fault = true;
+    return {"faultCode":32700,"faultString":"Parse Error, not well formed"};
+  }
+  
+  var value = this.queryDocument('/methodResponse/params/param/value/*')[0];
+  return this.parse(value);
+}
+
+// TODO: Remove XPath and start using querySelector since we're dealing with a DOM object
+XMLRPCParser.prototype.queryDocument = function(xpath, node){
+  var results = [];
+  var query = this.document.evaluate(xpath, node || this.document, null,
+                XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
+  for (var i = 0, length = query.snapshotLength; i < length; i++)
+    results.push(query.snapshotItem(i));
+  return results;
+}
+
+// TODO: Remove XPath and start using querySelector since we're dealing with a DOM object
+XMLRPCParser.prototype.parse = function(node){
+
+  switch(node.nodeName){
+    
+    case 'array':
+      var children = this.queryDocument('./data/value/*', node);
+      var array = [];
+      for (var i=0; i < children.length; i++) {
+        children[i]
+        array.push(this.parse(children[i]));
+      };
+      return array;
+      break;
+    
+    case 'struct':
+      var struct = {};
+      var children = this.queryDocument('./member', node);
+      for (var i=0; i < children.length; i++) {
+        var name = this.queryDocument('./name', children[i])[0];
+        var value = this.queryDocument('./value/*', children[i])[0];
+        struct[name.firstChild.nodeValue] = this.parse(value);
+      };
+      return struct;
+      break;
+      
+    case 'string':
+      var s = node.firstChild ? node.firstChild.nodeValue : '';
+      s = s.replace(/&amp;([a-z]+|\#[\d]+);/ig, '&$1;').replace(/&gt;/gi,'>').replace(/&lt;/gi,'<').replace(/&quot;/gi,'"');
+      return s.split('&#039;').join("'");
+      break;
+      
+    case 'boolean':
+      return node.firstChild ? node.firstChild.nodeValue == "1" : false ;
+      break;
+      
+    case 'i4':
+    case 'int':
+    case 'double':
+      return node.firstChild ? parseInt(node.firstChild.nodeValue) : null;
+      break;
+      
+    case 'dateTime.iso8601':
+      return node.firstChild ? Date.fromIso8601(node.firstChild.nodeValue) : null;
+      break;
+      
+    default:
+      throw("Don't know how to parse node: " + node.nodeName);
+      break;
+    
+    
+  }
+  
+}
+
+
+/** 
+ * Date
+ */

+ /**
+* <p>Convert a GMT date to ISO8601.</p>
+* @return
+*              <code>String</code> with an ISO8601 date.
+*/
+// Date.prototype.toIso8601 = function() {
+//   console.log("Encoding date", this, this.getUTCFullYear);
+//   year = this.getUTCFullYear();
+//   month = this.getUTCMonth() + 1;
+//   if (month < 10) month = "0" + month;     
+//   day = this.getUTCDate();
+//   if (day < 10) day = "0" + day;     
+//   time = this.getUTCHours() + ':' + this.getUTCMinutes() + ':' + this.getUTCSeconds();
+//   return year + month + day + "T" + time;
+// };
+
+/**
+* <p>Convert ISO8601 date to GMT.</p>
+* @param value
+*              ISO8601 date.
+* @return
+*              GMT date.
+*/
+Date.fromIso8601 = function(value) {
+  year = value.substr(0,4); 
+  month = value.substr(4,2);
+  day = value.substr(6,2); 
+  hour = value.substr(9,2); 
+  minute = value.substr(12,2); 
+  sec = value.substr(15,2);
+       var d = new Date(Date.UTC(year, month - 1, day, hour, minute, sec, 0));
+  return d;
+};
+
+/** 
+ * Base64
+ */
+function Base64(value) {       
+  Base64.prototype.bytes = value;
+};
+
+/** <p>Base64 characters map.</p> */
+Base64.CHAR_MAP = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
+
+/**
+* <p>Encode the object bytes using base64 algorithm.</p>
+* @return
+*              Encoded string.
+*/
+Base64.prototype.encode = function() {
+  if(typeof btoa == "function")
+    this.bytes = btoa(this.bytes);
+  else {
+    var _byte = new Array(), _char = new Array(), _result = new Array();
+    var j = 0;
+       for (var i = 0; i < this.bytes.length; i += 3) {
+      _byte[0] = this.bytes.charCodeAt(i);
+         _byte[1] = this.bytes.charCodeAt(i + 1);
+         _byte[2] = this.bytes.charCodeAt(i + 2);
+         _char[0] = _byte[0] >> 2;
+         _char[1] = ((_byte[0] & 3) << 4) | (_byte[1] >> 4);
+         _char[2] = ((_byte[1] & 15) << 2) | (_byte[2] >> 6);
+      _char[3] = _byte[2] & 63;            
+         if(isNaN(_byte[1]))
+           _char[2] = _char[3] = 64;
+         else 
+         if(isNaN(_byte[2]))
+           _char[3] = 64;
+         _result[j++] = Base64.CHAR_MAP.charAt(_char[0]) + Base64.CHAR_MAP.charAt(_char[1]) 
+                                  + Base64.CHAR_MAP.charAt(_char[2]) + Base64.CHAR_MAP.charAt(_char[3]);
+       }        
+    this.bytes = _result.join("");
+  }
+  return this.bytes;
+};
+
+/**
+* <p>Decode the object bytes using base64 algorithm.</p>
+* @return
+*              Decoded string.
+*/
+Base64.prototype.decode = function() {
+  if(typeof atob == "function")      
+    this.bytes = atob(this.bytes);
+  else {
+       var _byte = new Array(), _char = new Array(), _result = new Array();
+       var j = 0;
+       while ((this.bytes.length % 4) != 0)
+         this.bytes += "=";
+    for (var i = 0; i < this.bytes.length; i += 4) {
+         _char[0] = Base64.CHAR_MAP.indexOf(this.bytes.charAt(i));
+         _char[1] = Base64.CHAR_MAP.indexOf(this.bytes.charAt(i + 1));
+         _char[2] = Base64.CHAR_MAP.indexOf(this.bytes.charAt(i + 2));
+         _char[3] = Base64.CHAR_MAP.indexOf(this.bytes.charAt(i + 3));
+         _byte[0] = (_char[0] << 2) | (_char[1] >> 4);
+         _byte[1] = ((_char[1] & 15) << 4) | (_char[2] >> 2);
+         _byte[2] = ((_char[2] & 3) << 6) | _char[3];
+         _result[j++] = String.fromCharCode(_byte[0]);
+         if(_char[2] != 64) 
+           _result[j++] = String.fromCharCode(_byte[1]);
+         if(_char[3] != 64) 
+           _result[j++] = String.fromCharCode(_byte[2]);       
+       }
+       this.bytes = _result.join("");
+  }
+  return this.bytes;
+};
</ins><span class="cx">\ No newline at end of file
</span></span></pre>
</div>
</div>

</body>
</html>