/**
*   Cross site proxy. 
*/
function cCrossSiteProxy(){
    
    this.objects = {};
    this.methods = {};
    this.scopes = {};
    
    this.oData = {};
    this.s = {};
    
    /**
    *   Load trhe data off an url.
    *   @param sName Name of the action. Stores data in its own container.
    *   @param sUrl Url where to get the data from
    **/
    this.load = function(sName, sUri){
        
        var sLocalName = sName
        
        this.s[sName] = document.createElement('script'); 
        this.s[sName].type = 'text/javascript'; 
        //var sGeoStartUrl = 'http://localhost/naupar/';
        //var sGeoStartUrl = 'http://naupar.inaanbouw.nu/';
        
        var sGeoStartUrl = sBaseUrl;
        
        //this.s[sName].src = 'http://localhost/naupar/GeoStart/xsstest/getUrl.php?sName=' + sName + '&integrated=' + bIntegrated + '&uri=' + escape(sUri);
        sLanguageString = '';
        if(oLanguage && oLanguage.language){
            sLanguageString = "&setlanguage=" + oLanguage.language
        }
        
        var sSessionId = '';
        if(sessid != ''){
            sSessionId = '&PHPSESSID=' + sessid;
        }
        
        this.s[sName].src = sGeoStartUrl + 'GeoStart/xsstest/getUrlInclude.php?sCrossSiteName=' + sName + sSessionId + '&integrated=' + bIntegrated + '&sCrossSitePath=' + sUri.replace('?', '&').replace(sGeoStartUrl, '') + sLanguageString;
        this.s[sName].onload = geoStartAttachEventHandler(this, 'getContent', sLocalName);
        this.s[sName].onreadystatechange = geoStartAttachEventHandler(this, 'getContent', sLocalName);    
        
        this.sUri = sUri;
        this.sName = sName;
        
        document.getElementsByTagName('head')[0].appendChild(this.s[sName]);    
        
                
    };
    
    /**
    *   Name of the method of GeoStart to be called.
    *   @param mCallback Method name in oGeoStart or function reference
    *   @param oScopt Scope of the callback
    */
    this.call = function(mCallback, oScope){
        if(oScope){
            this.scopes[this.sName] = oScope;
        }        
        this.methods[this.sName] = mCallback;
    }
    
    
    /**
    *   This method is called on completion of the ajax request.
    **/
    this.getContent = function(sName){
        if (this.oData[sName]){
            // GeoStart methode
            if(typeof(this.methods[sName]) == 'string') {
                oGeoStart[this.methods[sName]](this.oData[sName]);    
            // Functie  referentie
            } else {
                // Scope gedefinieerd? Scope overschrijven
                if(this.scopes[this.sName]){
                    this.methods[sName].apply(this.scopes[sName], [this.oData[sName]]);
                } else {
                    this.methods[sName](this.oData[sName]);    
                }
                
            }
                        
            // Script tag weer opruimen
            try {
                document.getElementsByTagName('head')[0].removeChild(this.s[sName]);
            } catch (e) {} 
            
            
            delete(this.s[sName]);
            delete(this.oData[sName]);
            delete(this.methods[sName]);            
        }
    };
    
    
    /**
    * PHP en javascript hebben een andere manier van encoden
    */
    this.URLEncode = function(clearString) {
      var output = '';
      var x = 0;
      clearString = clearString.toString();
      var regex = /(^[a-zA-Z0-9_.]*)/;
      while (x < clearString.length) {
        var match = regex.exec(clearString.substr(x));
        if (match != null && match.length > 1 && match[1] != '') {
            output += match[1];
          x += match[1].length;
        } else {
          if (clearString[x] == ' ')
            output += '+';
          else {
            var charCode = clearString.charCodeAt(x);
            var hexVal = charCode.toString(16);
            output += '%' + ( hexVal.length < 2 ? '0' : '' ) + hexVal.toUpperCase();
          }
          x++;
        }
      }
      return output;
    }
}

function geoStartAttachEventHandler(oObject, sMethodName, sName) {       
    return function () { 
        return oObject[sMethodName].apply(oObject, [sName]); 
    };
}

