﻿// ---------------------------------------------------------------------
// Description:		SmartAjax
//					Allows user to perform an asynchronous request to
//                  a website
//
//                  Uses special scrip tag when browser is ie6
// 
// Autor:			Herbert Granofszky
// Date:			20.08.2007
// 
// (c) by team ModulAcht. All rights reserved
// ---------------------------------------------------------------------

// ---------------------------------------------------------------------
// Required script files:                   Common/prototype.js
// ---------------------------------------------------------------------

var SmartAjax = Class.create();
SmartAjax.prototype =
{
    // ---------------------------------------------------------------------
    // Constructor
    // --------------------------------------------------------------------- 
    initialize : function()
    {
        // ---------------------------------------------------------------------
        // Member init
        // ---------------------------------------------------------------------
        this.serviceId = null;
        this.requestNames = new Array();
	    this.requests = new Array();
	    this.url = null;
	    this.id = null;
	    this.onResponse = null;
	    this.extendedResponseArgument = null;
	    this.response = null;
	    this.error = null;
	    
	    // create id for this element
	    var id = 0;
	    while (true)
	    {
		    if (document["__dvSmartAjaxRequest_" + id] == null)
		    {
			    this.id = "__dvSmartAjaxRequest_" + id;
			    break;
		    }
		    id++;
	    }
	
	    // set this object to window
	    document[this.id] = this;
    },
    
    // ---------------------------------------------------------------------
    // Appends new request to request collection
    // 
    // argName:				(string) Name of request
    // argRequest:			(string/int) Request itself
    // ---------------------------------------------------------------------
    appendRequest : function(argName, argRequest)
    {
    	
    	// check for argRequest
	    if (argRequest != null)
	    {
		    if (typeof argRequest != "string")
		    {
		        argRequest = argRequest.toString();
		    }
		    this.requestNames.push(argName);
		    this.requests.push(argRequest);
	    }
	    else
	    {
		    // maybe there comes a request-string in
		    var tokens = argName.split(";");
		    // iterate through tokens
		    for (i=0; i<tokens.length; i++)
		    {
			    if (tokens[i].length > 0)
			    {
				    // split by '='
				    var keyval = tokens[i].split("=");
				    if (keyval.length == 1)
				    {
					    this.appendRequest(keyval[0], "");
				    }
				    else if (keyval.length == 2)
				    {
					    this.appendRequest(keyval[0], keyval[1]);
				    }
			    }
		    }
	    }
    },
    
    // ---------------------------------------------------------------------
    // Send requests to url
    // ---------------------------------------------------------------------
    sendRequest : function()
    {	
        // clear response
        this.response = "";
        
        // create request data
        var requestString = "";
        for (i=0; i<this.requests.length; i++)
        {
	        requestString += this.requestNames[i] + "=" + this.requests[i] + ";";
        }
        
        // use current page as request target when none was specified
        if (this.url == null || this.url == "")
        {
            var hash = window.location.hash;
            var href = window.location.href;
            // check for anchors in the current page href and cut them off
            if (hash != "") 
            {
                this.url = href.substring(0, href.indexOf(hash));
            } 
            else 
            {
                this.url = href;
            }
        }
        
         var requestUrl = this.url  + "?ajaxRequest=" + requestString 
                                    + "&clientObjectId=" + this.id 
                                    + "&serviceId=" + this.serviceId 
                                    + "&che=" + Math.floor(Math.random()*99999999999);
         
         // send request
        if (window.XMLHttpRequest)
        {
            // standards compliant mode -> use prototype's ajax object for request
            new Ajax.Request(requestUrl,
            {
                method: 'get',
                onSuccess: this.onSuccess.bindAsEventListener(this),
                onFailure: this.onFailure.bindAsEventListener(this)
            });
                  
        }
        else 
        {
            // ie6 quirk to avoid using the ActiveXObject('Microsoft.XMLHTTP')
            var head = document.getElementsByTagName("HEAD")[0];
            var script = document.createElement("script");
            script.type = "text/javascript";
            script.onload = script.onreadystatechange = this.onQuirkAjaxSuccess.bindAsEventListener(this);
            script.src = requestUrl;
	        
            head.appendChild(script);
        }
    },

    // ---------------------------------------------------------------------
    // Callback for successful ajax request
    // ---------------------------------------------------------------------
    onSuccess: function(transport)
    {
        eval(transport.responseText);
        if (this.onResponse != null)
        {
           this.onResponse(this); 
        }
    },
    
    // ---------------------------------------------------------------------
    // Callback for failed ajax request
    // ---------------------------------------------------------------------
    onFailure: function()
    {
        alert('Ajax request failed');
    },
    
    // ---------------------------------------------------------------------
    // Callback for successful quirk ajax request
    // ---------------------------------------------------------------------
    onQuirkAjaxSuccess: function(eventObject)
    {
        var script = Event.element(eventObject);
        if (script.readyState && script.readyState != "loaded" && script.readyState != "complete") return;
        
        script.onreadystatechange = script.onload = null;
        
        if (this.onResponse != null)
        {
            this.onResponse(this);
        }
    },
    
    onQuirkAjaxFailure: function(eventObject)
    {
    
    }
};