// <license>BorgWorX PUBKLIC SOURCE LICENSE</license>
// <copyright>
// Portions Copyright (c) 2003-2005 SediSys Information Technologies, Ges.m.b.H. All Rights Reserved.
//
// This file contains Original Code and/or Modifications of Original Code
// as defined in and that are subject to the BorgWorX Public Source License
// Version 1.0 (the 'License'). You may not use this file except in
// compliance with the License. Please obtain a copy of the License at
// http://www.SediSys.com/BPL/ and read it before using this
// file.
//
// The Original Code and all software distributed under the License are
// distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
// EXPRESS OR IMPLIED, AND BorgWorX HEREBY DISCLAIMS ALL SUCH WARRANTIES,
// INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
// Please see the License for the specific language governing rights and
// limitations under the License.
// </copyright>
// <authors>
// Michael Schwarz <info@schwarz-interactive.de>
// </authors>
// <changes>
// Christian Hubinger <chubinger@sedisys.com>
// 4.10.2005 - fix type error with non existing data
// 5.10.2005 - remove native javascript object prototyping, causes nasty side effects with 3rdParty components
// </changes>
if( ! window.addNamespace ) {
	window.addNamespace = function( ns ) 	{
		var nsParts = ns.split(".");
		var root = window;

		for( var i = 0; i < nsParts.length; i++ ) 		{
			if( typeof root[ nsParts[ i ] ] == "undefined" ) {
				root[ nsParts[ i ] ] = new Object();
			}
			root = root[ nsParts[ i ] ];
		}
	}
}

if( ! window.XMLHttpRequest ) {
	window.XMLHttpRequest = function() 	{
		var xmlHttp;
		try { 
			xmlHttp = new ActiveXObject( "Msxml2.XMLHTTP.4.0" ); 
			return xmlHttp; 
		} catch( ex ) {}
		try { 
			xmlHttp = new ActiveXObject( "MSXML2.XMLHTTP" ); 
			return xmlHttp; 
		} catch( ex ){}
		try { 
			xmlHttp = new ActiveXObject( "Microsoft.XMLHTTP" ); 
			return xmlHttp; 
		} catch( ex ){}
		return null;
	}
}

// JavaScript objects wrappers
JSONObject = function( obj ) {
	this.obj = obj;
	
	this.toJSON = function() {
		if ( this.obj == null ) {
			return 'null';
		}
	//	debugger
		switch( this.obj.constructor ) {
			case Object:
				return ObjectToJSON( this.obj );
				break;
			
			case Array:
				return ArrayToJSON( this.obj );
				break;
				
			case String:
				return StringToJSON( this.obj );
				break;
			
			case Date:
				return DateToJSON( this.obj );
				break;
			
			case Number:
			case Boolean:
				return String( this.obj );
				break;
		}
	}
	
	function ObjectToJSON( obj ) {
		var v=[];
		for( attr in obj ) 	{
			if( typeof obj[ attr ] != "function" ) {
				var attribute = new JSONObject( obj[attr] );
				v.push('"' + attr + '": ' + ( obj[attr] != null ? attribute.toJSON() : 'null' ) );
			}
		}
		if( v.length > 0 ) {
			return "{" + v.join(", ") + "}";
		} else {
			return "{}";
		}
	
	}
	
	function StringToJSON( obj ) {
		var s = obj; // .encodeURI();
		s = '"' + s.replace(/(["\\])/g, '\\$1') + '"';
		s = s.replace(/\n/g,"\\n");
		s = s.replace(/\r/g,"\\r");
		s = s.replace(/\t/g,"\\t");
		return s;	
	}
	
	function DateToJSON( obj ) {
		var o = new Object();
		o.__type = "System.DateTime";
		o.Year = this.getUTCFullYear();
		o.Month = this.getUTCMonth() + 1;
		o.Day = this.getUTCDate();
		o.Hour = this.getUTCHours();
		o.Minute = this.getUTCMinutes();
		o.Second = this.getUTCSeconds();
		o.Millisecond = this.getUTCMilliseconds();
		o.TimezoneOffset = this.getTimezoneOffset();
		return new JSONObject( o ).toJSON();
	}
	
	function ArrayToJSON( obj ) {
		var v = [];

		for( var i = 0; i < obj.length; i++ ) {
			var elem = new JSONObject( obj[ i ] );
			v[v.length] = obj[ i ] != null ? elem.toJSON() : 'null';
		}
		return "[" + v.join(", ") + "]";
	}
}


JSONArray = function() {
	this.inherits = Array;
	this.inherits();
	
	this.push = function( o ) 	{
		this[ this.length ] = o;
	}
	
	this.getLast = function() 	{
		if( this.length == 0 ) {
			return null;
		}
		return this[ this.length -1 ];
	}
	
	this.getFirst = function() {
		if( this.length == 0 ) {
			return null;
		}
		return this[ 0 ];
	}
}


// .NET wrapper objects
addNamespace( "System.Data" );

System.Data.DataSet = function( name ) {
	this.__type = "System.Data.DataSet, System.Data, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";
	this.Name = name != null ? name : "";
	this.Tables = [];
}

System.Data.DataTable = function( name ) {
	this.__type = "System.Data.DataTable, System.Data, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";
	this.Name = name != null ? name : "";
	this.Columns = [];
	this.Rows = [];
	
	this.addColumn = function( name, type ) {
		var c = new Object();
		c.Name = name;
		c.__type = type;
		this.Columns.push( c );
	}
}

// Browser related members
addNamespace("MS.Browser" );

MS.Browser.isIE = (window.navigator.appName.toLowerCase().indexOf('explorer') != -1 || window.navigator.appName.toLowerCase().indexOf('msie') != -1 );

// Debug
addNamespace( "MS.Debug" );

MS.Debug.enabled = false;
MS.Debug.trace = function(s) {}

// Ajax.NET Professional related members
addNamespace( "AjaxPro" );

AjaxPro.noOperation = function() {}
AjaxPro.cryptProvider = null;
AjaxPro.token = "";
AjaxPro.invoke = function( m, args, c, url ) {
	var o = new Object();
	o.method = m;
	o.args = args;
	o.callback = c[ 1 ].length > c[ 0 ] ? c[ 1 ][ c[ 0 ] ] : AjaxPro.noOperation;
	o.context = c[ 1 ].length > c[ 0 ] + 1 ? c[ 1 ][ c[ 0 ]+1 ] : null;
	o.session = c[ 2 ];
		
	var xml = new XMLHttpRequest();
	var data = "";
	var async = typeof o.callback == "function" && o.callback != AjaxPro.noOperation;
	
	for( attr in o.args )	{
		if( typeof o.args[ attr ] != "function") {
			var attribute = new JSONObject( o.args[attr] );
			data += attr + "=" + (o.args[attr] != null ? attribute.toJSON() : 'null') + "\r\n";
		}
	}

	if( MS.Debug.enabled ) {
		MS.Debug.trace( "JSON string: " + data );
	}
	
	if( async ) {
		xml.onreadystatechange = doCallback;
	}
	var dataLength = 0;
	if( data.length != 0 ) {
		dataLength = data.length;
	} else {
		data = new Date().getTime();
		dataLength = String( new Date().getTime() ).length;
	}
	
	xml.open( "POST", url, async );
	xml.setRequestHeader( "Content-type", "application/x-www-form-urlencoded" );
	xml.setRequestHeader( "Content-length", dataLength );
	xml.setRequestHeader( "Ajax-method", o.method );
	xml.setRequestHeader( "Ajax-session", o.session );
	xml.setRequestHeader( "Ajax-token", AjaxPro.token );
	
	if( MS.Browser.isIE ) {
		xml.setRequestHeader( "Accept-Encoding", "gzip, deflate" );
	} else {
		xml.setRequestHeader( "Connection", "close" );		// Mozilla Bug #246651
	}
	
	if( AjaxPro.cryptProvider != null ) {
		data = AjaxPro.cryptProvider.encrypt( data );
	}
	if( MS.Debug.enabled ) {
		MS.Debug.trace( "XMLHttpRequest: " + url + "\r\n  Method: " + o.method + "\r\n  Data: " + data );
	}
	xml.send( data );
	
	if( ! async ) {
		return createResponse();
	}
	return true;	
		
	function doCallback() {
		if( xml.readyState != 4 ) {
			return;
		}

		if( xml.status == 200 ) {
			o.callback( createResponse() );
		}
	}
	
	function createResponse() {
		var r = new Object();
		r.url = url;
		r.error = false;
		r.request = o;
		r.value = null;
		r.responseText = xml.responseText;

		if( MS.Debug.enabled ) {
			MS.Debug.trace( "XMLHttpResponse: " + url + "\r\n  Method: " + o.method + "\r\n  Response: " + r.responseText );
		}
		if( AjaxPro.cryptProvider != null ) {
			r.responseText = AjaxPro.cryptProvider.decrypt( r.responseText );
		}
		if( MS.Debug.enabled ) {
			MS.Debug.trace( "JSON string: " + r.responseText );
		}
		
		// alert( r.responseText );
		eval( "r.value = " + r.responseText + ";" );
		return r;
	}
}

