/* Javascript File ajax.js */

/* ------------------------------------------------------ AJAX Component Methods ------------------------------------------------
    1.	ajaxComponent
    2.	setURL, getURL
    3.	setMethodName, getMethodName
    4.	setRequestParameter, getRequestParameter
    5.	setDescription, getDescription
    6.	setVars, getVars
    7.	setReturnFunction, getReturnFunction
    8.	send
    -------------------------------------------------------------------------------------------------------------------------- */
    //  Global array for reusable objects.
    var ajaxRequests = new Array();
    
    function getAjaxObject(freed){
        this.ajax = false; 
        this.freed = freed;         

        if (window.XMLHttpRequest) {              
            this.ajax = new XMLHttpRequest();  
        } 
        else {                                  
            this.ajax = new ActiveXObject("Microsoft.XMLHTTP");
        }   

    }
// ------------------------------------------------------------------------------------------------------------------------------
    function ajaxComponent(callBackFunction){
        this.method 	 = 'GET';
        this.url 	 = 'index.php';
        this.params	 = '';
        this.description = '';
        this.body = '';
        
        this.callBackArrayNames	 = new Array();
        this.callBackArrayValues = new Array();
        this.callback = callBackFunction || function(){};

        this.setURL = function(url){
            this.url = url;	
        }
        this.getURL = function(){
            return this.url;	
        }

        this.setMethodType = function(methodType){
            methodType = methodType.toUpperCase();
            if(methodType != 'GET' && methodType != 'POST'){
                this.method = 'GET';	
            }
            else{
                this.method = methodType;	
            }	
        }

        this.getMethodType = function(){
            return this.method;	
        }

        this.setRequestParameter = function(parameterValue){
            this.params = parameterValue;
            var lengthParams = this.params.length;

            if(lengthParams > 255){
                this.method = 'POST';
            }

            if(this.method == 'GET'){

            }
            else{
                // POST parameters goes here.
                this.body = parameterValue;
            }
        }

        this.getRequestParameter = function(){
            return this.params;
        }

        this.getRequestParameterLength = function(){
            return this.params.length;
        }

        this.setDescription = function(description){
            this.description = description;
        }

        this.getDescription = function(){
            return this.description;
        }

        this.setCallBackVars = function(varName, varValue){
            this.callBackArrayNames[this.callBackArrayNames.length]   = varName;
            this.callBackArrayValues[this.callBackArrayValues.length] = varValue;
        }

        this.getCallBackVars = function(varName){
            for(var x=0; x < this.callBackArrayNames.length; x++){
                if(this.callBackArrayNames[x] == varName){
                    return this.callBackArrayValues[x];
                    break;
                }
            }
            return null;
        }

        this.setCallBackFunction = function(functionName){                   
            this.callBackFunction = functionName;
        }

        this.getCallBackFunction = function(){
            callBack();
        }

        this.send = function(){
	        
            var pos = -1;
            for (var i=0; i < ajaxRequests.length; i++){
                if(ajaxRequests[i].freed == 1){ 
                    pos = i; 
                    break; 
                }
            }
            if (pos == -1){ 
                pos = ajaxRequests.length; 
                ajaxRequests[pos] = new getAjaxObject(1); 
            }
            
            // transporter = getAjaxObject();
            // transporter = ajaxRequests[pos];
            ajaxRequests[pos].freed = 0;
            
	        if (this.getMethodType() == 'GET'){
	           ajaxRequests[pos].ajax.open('GET',this.getURL() + '?' + this.getRequestParameter(),true);
			}    
	            
            ajaxRequests[pos].ajax.onreadystatechange = function(){
                if(ajaxRequests[pos].ajax.readyState == 4){
	                if (ajaxRequests[pos].ajax.status == 200) {
	                    callback(ajaxRequests[pos].ajax.responseText,ajaxRequests[pos].ajax.status,ajaxRequests[pos].ajax.responseXML);
	                    ajaxRequests[pos].freed = 1;
                	}
                	else {
	                	alert('error?: '+ajaxRequests[pos].ajax.status);
                	}
                }
            }

            if(this.getMethodType() == 'GET'){
                if (window.XMLHttpRequest){
	                ajaxRequests[pos].ajax.send(null);	                
                }
                else {
 	                ajaxRequests[pos].ajax.send();
          	 	}
            }
            else{
				this.body = this.getRequestParameter();
				
                ajaxRequests[pos].ajax.open('POST',this.getURL(),true);
				ajaxRequests[pos].ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        		ajaxRequests[pos].ajax.setRequestHeader("Content-Length", this.body.length);
				ajaxRequests[pos].ajax.send(this.body);
            }

        }
        return this;
    } 
		
// ------------------------------------------------------------------------------------------------------------------------------

    function paramsToJSON(s) {
        var json = '';
        var t = s.split('&'); 
        var o = false;    
        var last = false;

        for(var i=0; i<t.length;i++){   
       
           if (i == (t.length-1) ) { 
                    last = true; 
            }

            var x = t[i].split('=');             
            for(j=0;j<x.length;j++) {     
                o = (o) ? false:true;
                 if(o) { 
                        json += '"' + x[j] +'":'; 
                }
                else if(last) {
                        json += '"'+x[j] +'"'; 
                } 
                else { 
                        json += '"'+x[j] +'",'; 
                }           
           } 
        } 
        json += '';
        return json;
    }
         
// ------------------------------------------------------------------------------------------------------------------------------
    function multipleAjaxComponent(){
        this.activeAjaxObjects = 0;
        this.jsonArray = new Array();
        this.ajaxObj = 1;
        this.paramsLength = 0;
        this.jsonMethodType = 'GET';
        this.jsonMultiURL = 'index.php';
        
        this.append = function(ajaxObject){
            
            var currentURL = ajaxObject.getURL();
            var currentParam = paramsToJSON(ajaxObject.getRequestParameter());
            var currentDesc = ajaxObject.getDescription();
            
            var jsonValue = '"ajaxObj_' + ajaxObj++ + '"';
                jsonValue += ':{ "url" : "' + currentURL + '",';
                jsonValue += '"params" : {' + currentParam + ' },';
                jsonValue += '"description" : " ' + currentDesc + '" }';
            
            this.paramsLength = this.paramsLength + jsonValue.length;
            this.jsonArray[this.activeAjaxObjects] = jsonValue;
            this.activeAjaxObjects = this.activeAjaxObjects + 1;
        }
        
        this.getAjaxCollection = function(){
             return this.jsonArray;
        }
        
        this.getActiveAjaxObjects = function(){
            return this.activeAjaxObjects;
        }
        
        this.setJSONMultiURL = function(multiURL){
            this.jsonMultiURL = multiURL;
        }
        
        this.getJSONMultiURL = function(){
            return this.jsonMultiURL;
        }
        
        this.setJSONMethodType = function(){
            this.jsonMethodType = 'POST';
        }
        
        this.getJSONMethodType = function(){
            return this.jsonMethodType;
        }
        
        this.sendMultipleRequest = function(){
            var pos = -1;
            for (var i=0; i < ajaxRequests.length; i++){
                if(ajaxRequests[i].freed == 1){ 
                    pos = i; 
                    break; 
                }
            }
            if (pos == -1){ 
                pos = ajaxRequests.length; 
                ajaxRequests[pos] = new getAjaxObject(1); 
            }
            
            ajaxRequests[pos].freed = 0;
            ajaxRequests[pos].ajax.open('POST',this.getJSONMultiURL(),true);
            
            ajaxRequests[pos].ajax.onreadystatechange = function() {
                if(ajaxRequests[pos].ajax.readyState == 4 && ajaxRequests[pos].ajax.status == 200) {
                    callback(ajaxRequests[pos].ajax.responseText,ajaxRequests[pos].ajax.status,ajaxRequests[pos].ajax.responseXML);
                    ajaxRequests[pos].freed = 1;
                }
            }
            
            //  This code block not necesary. Because Multiple Ajax request is always POST.
            this.setJSONMethodType();
            if(this.getJSONMethodType() == 'POST'){
				ajaxRequests[pos].ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        		ajaxRequests[pos].ajax.setRequestHeader("Content-Length", this.body.length);
                ajaxRequests[pos].ajax.send('&json={' + this.getAjaxCollection() + '}');
            }
        }
              
        return this;
    }
//  ----------------------------------------------------------------------------

