var AutoSpyder = {
	Net : {
		sendForm : function( sConnUrl, sCommand, oForm, oCallBackFunction ) {
			var sParams = [];
			
			for( var i = 0; i < oForm.elements.length; i++ ) {
				//Do not submit the view state input
				switch ( oForm.elements[i].type ) {
					case 'checkbox' :
					case 'radio' :
						if ( oForm.elements[i].checked ) {
							sParams.push([oForm.elements[i].name, oForm.elements[i].value]);	
						}
						break;
						
					case 'select':
						sParams.push([oForm.elements[i].name, oForm.elements[i].options[oForm.elements[i].selectedIndex].value]);
						break;
						
					default:
						sParams.push([oForm.elements[i].name, oForm.elements[i].value]);
						break;
				}
			}
			if ( typeof oCallBackFunction == 'function' ) {
				AutoSpyder.Ajax.sendCommand(sConnUrl, sCommand, sParams, oCallBackFunction);
			} else {
				var oResponse = AutoSpyder.Ajax.sendCommand(sConnUrl, sCommand, sParams, oCallBackFunction);
				return oResponse;
			}
		}
	},
	Ajax : {
		sendCommand : function( sConnUrl, sCommand, sParams, oCallBackFunction ) {
			var oAjax = new AutoSpyder.Ajax.AjaxConnector();
			
			if ( oCallBackFunction && typeof oCallBackFunction == 'function' ) {
				oAjax.loadUrl(sConnUrl, sParams, oCallBackFunction);
			} else {
				oAjax.loadUrl(sConnUrl, sParams);
				return oAjax;
			}
		},
		
		loadAjaxForm: function (sUrl, sElementId) {
			$(sElementId).innerHTML = this.sendCommand(sUrl, null, null).getHttpResponse();
		},
		
		getHttpResponse: function (sUrl) {
			alert('inside');
			return this.sendCommand(sUrl, null, null).getHttpResponse();
		},
		
		AjaxConnector : function() {
			this._responseText = '';
			
			this.loadUrl = function( sUrlToCall, aParams, oAsyncFunctionPointer ) {
				var bAsync = ( typeof oAsyncFunctionPointer == 'function' );		
				var oXmlHttp = this.getHttpRequest();
				var sParams = '';
				var oConnector = this;
				var sMethod = 'GET';
				
				try {
					if ( aParams !== null && aParams.constructor == Array ) {
						sMethod = 'POST';
						for ( var i = 0; i < aParams.length; i++ ) {
							if ( aParams[i][0] !== undefined ) {
								if ( i > 0 ) {
									sParams += '&';
								}
								sParams += ( aParams[i][0] + '=' + escape(aParams[i][1]) );
							}
						}				
					} else {
						sParams = aParams;
					}
					
					oXmlHttp.open(sMethod, sUrlToCall, bAsync);
					oXmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
					
					if ( bAsync ) 	{	
						oXmlHttp.onreadystatechange = function( ) {
							if ( oXmlHttp.readyState == 4 ) {	
								if ( oXmlHttp.status == 200 || oXmlHttp.status == 304 ) {
									oConnector._responseText = oXmlHttp.responseText;
									oAsyncFunctionPointer(oConnector);
								} else {
									alert('XmlHttp request error: ' + oXmlHttp.statusText + ' (' + oXmlHttp.status + ')');
								}
							}
						};
					}
					
					oXmlHttp.send(sParams);
					
					if ( ! bAsync )	{
						if ( oXmlHttp.status == 200 || oXmlHttp.status == 304 ) {
							this._responseText = oXmlHttp.responseText;
						} else {
							alert('XmlHttp request error: ' + oXmlHttp.statusText + ' (' + oXmlHttp.status + ') Url: ' + sUrlToCall);
						}
					}
				} catch(e) {
					window.error('There was an error with the RPC. ' + e.message);
				}
			};
			
			this.getHttpRequest = function( ) {
				var oRequest = null;
				
				if ( window.XMLHttpRequest ) {
					oRequest = new XMLHttpRequest();
				} else if ( window.ActiveXObject ) {
					try {
						oRequest = new ActiveXObject('MsXml2.XmlHttp');
					} catch(e) {
						try {
							oRequest = ActiveXObject("Microsoft.XMLHTTP");
						} catch(e) { window.error('Ajax is not enabled for this browser'); }
					}
				} else {
					window.error('Ajax is not enabled for this browser.');
				}
				
				return oRequest;
			};
			this.getHttpResponse = function( ) {
				return this._responseText;
			};

			this.getResponse = function( ) {
				var oResponse = {};
				try { oResponse = eval('(' + this._responseText.replace(/[\t\n\r\f\v]+/g, '') + ')'); } catch(e) { }
				
				return oResponse;
			};
		}
	}
}
function defaultCallBackHandler(pi_ajxConnector) {
	$('modal').innerHTML = pi_ajxConnector.getHttpResponse();
}