﻿	// Create By MR. Treebhoph Thoomsan
	// 
	// This library is free software; you can redistribute it and/or
	// modify it under the terms of the GNU Lesser General Public
	// License as published by the Free Software Foundation; either
	// version 2.1 of the License, or (at your option) any later version.
	// 
	// This library is distributed in the hope that it will be useful,
	// but WITHOUT ANY WARRANTY; without even the implied warranty of
	// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
	// Lesser General Public License for more details.
	// 
	// You should have received a copy of the GNU Lesser General Public
	// License along with this library; if not, write to the Free Software
	// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

	/*@ Class JsonObject */
	function JsonObject( ) 
	{
		var hash = new Hashtable( );
		this.typeName = "JsonObject"


		this.put = function( propName, propValue ) {

			hash.put( propName, replaceCarriageReturn( propValue, " " ) );
		}


		this.toString = function(  objType  ) {

			var result = "";
			var keys = hash.keys();

			for( var i = 0; i < keys.length; i ++ ) {
				var obj = hash.get( keys[i] );

				if( typeof(obj) == "string" )
            					result += ", \"" + keys[i] + "\":\"" + obj + "\""; 
				else {
					if( typeof( obj ) == "object" ) {
						if( obj.typeName == "JsonArray" )
							result += ", \"" + keys[i] + "\":" + obj.toString(); 
						else if( obj.typeName == "JsonObject" )
							result += ", \"" + keys[i] + "\":" + obj.toString( "JsonObject"); 
					}
					else
						result += ", \"" + keys[i] + "\":" + obj;
				}
			}			

			
			if( objType == "JsonObject" || objType == null )
				return  replaceCarriageReturn("{" + result.substr( 1 ) + "}", " ") ;

    			return replaceCarriageReturn( result.substr( 1 ), " ") ;
		}

	
		this.toObject = function( jsonText ) {
			if( jsonText == null )
				return eval( "(" + this.toString() + ")" );

			return eval( "(" + replaceCarriageReturn(jsonText, " ")  + ")" );
		}
	}



	/*@ Class JsonArray */
	function JsonArray( )
	{
		var list = new Array( );
		this.typeName = "JsonArray"
		

		this.add = function( jsonObj ) {

			list.push( jsonObj );
		}


		this.toString = function( ) {

			var jsonStr = "";

			for(  i = 0; i < list.length; i ++ )
				jsonStr += ", " + list[ i ].toString();

			return "[" + replaceCarriageReturn(jsonStr.substr( 1 ), " ") + "]";
		}
	}
	
	
	function replaceCarriageReturn(text,replaceWith)
	{
		text = escape(text); //encode all characters in text area to find carriage return character

		for(i=0; i < text.length; i++) {
		
			//loop through string, replacing carriage return encoding with HTML break tag
			if(text.indexOf("%0D%0A") > -1) {
				//Windows encodes returns as \r\n hex
				text=text.replace("%0D%0A",replaceWith)
			}
			else if(text.indexOf("%0A") > -1) {
				//Unix encodes returns as \n hex
				text=text.replace("%0A",replaceWith)
			}
			else if(text.indexOf("%0D") > -1) {
				//Macintosh encodes returns as \r hex
				text=text.replace("%0D",replaceWith)
			}
			
		}
		
		return unescape(text);

	}	


