/*
 * Copyright (c) 2013 Stephen Hansen (www.hansencomputers.com)
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:

 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE. 
 */


/** EasyJax Javascript class
 *
 * instantiate this class with the URL to post data to (PHP script using the EasyJax PHP class)
 * supply a callback function that will run if the script returns success.  If an error is returned,
 * it is announced using the alert() function
 */

function EasyJax (Url,runOnSuccess){
  this.Url = Url;
	this.runOnSuccess = runOnSuccess;
	this.post_obj = {};
	this.xmlHttp;
	
	this.boundary = '--__generic_boundary--';
	
  /** submit_data
   * generates the xmlHttp request, creates a callback function, formats the data to be submitted as a JSON string,
   * and posts the data to the server
   */
	this.submit_data = function(){
		if(window.XMLHttpRequest) {
			this.xmlHttp = new XMLHttpRequest(); 
		} else {
			this.xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
	
		//this was a version of ProcessRequest in v1 (input variable named "p_func").
		//Write function whose name is contained in data.runOnSuccess and place in a javascript for the client.
		//data is a JSON data package returned to the client from the server.
		this.xmlHttp.onreadystatechange = this.createCallback();

		this.xmlHttp.open( "POST", this.Url, true );
		this.xmlHttp.setRequestHeader("Content-Type","multipart/form-data; boundary=\""+this.boundary+"\"; charset=ISO-8859-1");
		var pdata = this.getPostBody();
		this.xmlHttp.send( pdata );
	}

  /** createCallback
   * returns a callback function which handels error conditions and success conditions (status == 200, not 404, or 500)
   */
	this.createCallback = function (){
		var x = this.xmlHttp;
		var ros = this.runOnSuccess;
		return function (){
			if ( x.readyState == 4 && x.status == 200 ) {
				try {
					var data = JSON.parse(x.responseText);
				} catch(err){
					alert("There was an error parsing JSON data.  Response Text shown below:\n\n"+x.response);
					return 1;
				}
				if(data.error != "" && data.error != undefined){
					alert(data.error);
					return 1;
				} else if(ros) {
					eval(ros)(data);
					return 0;
				} else {
					alert(ros);
				}
			}
		}
	}

  /** set_send_data
   * call this method to add data to be sent to the server
   */
	this.set_send_data = function(name,value){
		this.post_obj[name] = value;
	}
	
  /** getPostBody
   * creates a JSON string from the post_obj variable within this class.  only used in submit_data
   */
	this.getPostBody = function(){
		var body='--'+this.boundary+'\r\n';
		body += "Content-Disposition: form-data; name=\"" + "JSON_data" + "\"\r\n\r\n"
		body += JSON.stringify(this.post_obj) + "\r\n";
		return body;
	}
}

@@ -1467,18 +1467,17 @@
 ccess)%7B%0A
-  
+%09
 this.Url
@@ -1561,53 +1561,8 @@
 ;%0A%09%0A
-%09this.boundary = '--__generic_boundary--';%0A%09%0A
   /*
@@ -2330,143 +2330,93 @@
 e%22,%22
-multipart/form-data; boundary=%5C%22%22+this.boundary+%22%5C%22; charset=ISO-8859-1%22);%0A%09%09var pdata = this.getPostBody();%0A%09%09this.xmlHttp.send( pdata
+application/json; charset=UTF-8%22);%0A%09%09this.xmlHttp.send( JSON.stringify(this.post_obj)
  );%0A
@@ -3245,16 +3245,16 @@
 value)%7B%0A
+
 %09%09this.p
@@ -3283,350 +3283,6 @@
 %0A%09%7D%0A
-%09%0A  /** getPostBody%0A   * creates a JSON string from the post_obj variable within this class.  only used in submit_data%0A   */%0A%09this.getPostBody = function()%7B%0A%09%09var body='--'+this.boundary+'%5Cr%5Cn';%0A%09%09body += %22Content-Disposition: form-data; name=%5C%22%22 + %22JSON_data%22 + %22%5C%22%5Cr%5Cn%5Cr%5Cn%22%0A%09%09body += JSON.stringify(this.post_obj) + %22%5Cr%5Cn%22;%0A%09%09return body;%0A%09%7D%0A
 %7D%0A

Update EasyJax.js