
if (typeof dd == "undefined") {
    /**
     * The YAHOO global namespace object
     * @class YAHOO
     * @static
     */
    var dd = {};
}

dd.ajax = {	

		/*
		* @param request (POST | GET | UPLOAD )
		* @param arguments {cache, form, url}
		*/
		sendRequest:function(request,args,oCallBack)
		{	
			var ajaxSuccess = function(oResponse) {									
                // Error if no response
                if(!oResponse) {
                    
                }     // Forward to handler
                else {                	                	                
                    this.handleResponse(oResponse,args,oCallBack);
                }
            };
						
			var _callback = {
		    	upload: ajaxSuccess,
		        success: ajaxSuccess,
		        failure: this.handleFailure,
		        scope: this
		    }


		    try{		
				 // REQUEST
				 switch(request){			 	
				 	case "GET":		
				 		var cache = (args.cache == "true"?true:false);				 		
				 		if(cache){				 			
				 			var oCachedResponse =this.DataSource.getCachedResponse(args.url,oCallBack);	
				 			if(oCachedResponse){				 				
				 				this.handleResponse('',args,oCallBack,oCachedResponse)		
				 				return;		 				
				 			}			 			
				 		}						 		
				 		var o = this.Connect.asyncRequest("GET", args.url, _callback);	
				 		break;
				 		
				 	case "POST":
				 		var formid =args.formid; 
				 	  	this.Connect.setForm(formid);
				 		this.Connect.asyncRequest("POST", args.url, _callback, null);				 		
				 		break;
				 		
				 	case "UPLOAD":					 	
				 		var formid =args.formid; 
				 	  	this.Connect.setForm(formid, true);
						if (this.Connect._formNode.encoding){
							this.Connect._formNode.encoding = 'multipart/form-data';
						} else {
							this.Connect._formNode.enctype = 'multipart/form-data';
						}
				 		this.Connect.asyncRequest("POST", args.url, _callback);				 		
				 		break;
				 }
		    }catch(err){
		    	this.handleFailure(err);
		    }
			 
		},		
				
		handleResponse:function(oResponse,args,oCallBack,oCachedResponse){
			
			if(typeof oResponse == undefined && typeof oCachedResponse == undefined){
				this.handleFailure('No response');
			}
			
			try{								
				// # no CallBack object
					if(!oCallBack){
						return;
					}else{		
				// # CallBack Object			
						if(typeof oCallBack == "object"){
							fCallBack = "";		
							//oCallBack = "handleResponse";	
							//alert(oCallBack)	
						}else if (typeof oCallBack == "function"){
							
						}else{
							var o = dd.namespacef(oCallBack);
							oCallBack = o[0];						
							fCallBack = o[1];												
						}
					}
			
					if(oCachedResponse){
						//return (oReturn?oReturn(oCachedResponse):return oCachedResponse);
						oCallBack(oCachedResponse);
						return;
					}			
					
					if(args.dataType == "TEXT"){
						oParsedResponse = oResponse.responseText;						
					}else{
						oParsedResponse = oResponse.responseText.parseJSON();
					}							
					
					if(oParsedResponse.status == 'ok' || args.dataType == "TEXT"){								
						
						if(oParsedResponse.message && fCallBack == 'showConfirm'){	
							oCallBack[fCallBack](oParsedResponse.message);							
						}else{
							/*if(fCallBack) oCallBack[fCallBack](oParsedResponse);												
							else oCallBack.handleResponse(oParsedResponse);	*/
							oCallBack(oParsedResponse);	
						}							
						var cache = (args.cache == "true"?true:false);
						var recache = (args.recache == "true"?true:false);
						if(cache || recache){
							this.addToCache(args.url, oParsedResponse);
						}
						return;
					}else{
						this.handleFailure(oParsedResponse.message);
					}
			}
			catch(err){				
				this.handleFailure(err);
			}
		},
	
		//handleFailure:function(oRequest, oParsedResponse)
		handleFailure:function(msg)
		{			
			//initList(oParsedResponse)
			//alert(msg + '::'+msg.fileName+'::'+msg.lineNumber );
			//return;
			msg += (msg.fileName?' '+msg.fileName:'');
			msg += (msg.lineNumber? ' '+msg.lineNumber:'' );
			
			alert(msg)
			//showConfirm(msg ,false);			
			//alert(msg)
			return;
		},
		
		showConfirm:function(message)
		{
			showConfirm(message,true);			
		},
		
		addToCache:function(request,parsedResponse){				
			this.DataSource.addToCache(request, parsedResponse);			
		},
		
		clearCacheElement:function(oRequest,partialRequest)
		{
			var aCache = this.DataSource._aCache;
    		var nCacheLength = (aCache) ? aCache.length : 0;
    		partialRequest = (partialRequest?partialRequest:0);
    		
			 // Loop through each cached element
	        for(var i = nCacheLength-1; i >= 0; i--) {
	            var oCacheElem = aCache[i];	
	            // Defer cache hit logic to a public overridable method
	            if(partialRequest){
	            	var s =  oCacheElem.request.search(oRequest);
	            	if(s>0){
	            		aCache.splice(i,1);
	            	}
	            }else{
		            if(this.DataSource.isCacheHit(oRequest,oCacheElem.request)) {   
		                // clear               
		                aCache.splice(i,1);
		                break;
		            }
	            }
	        }
		},
		
		flushCache:function()
		{
			this.DataSource.flushCache();
		}
		
		
	
};

 /**
 * Alias to YUI Connection Manager. Allows implementers to specify their own
 * subclasses of the YUI Connection Manager utility.
 *
 * @property connMgr
 * @type Object
 * @default YAHOO.util.Connect
 */
	dd.ajax.Connect = YAHOO.util.Connect || null;
/**
 * Alias to YUI DataSoure. 
 * 
 *
 * @property DataSource
 * @type Object
 * @default YAHOO.util.DataSource
 */
   dd.ajax.DataSource = new YAHOO.util.DataSource('wiredrive',{'maxCacheEntries':20}) || null;
