﻿// Declare Url to which all Ajax-Requests should go
var AJAX_URL;

// And its Setter Method...
function setAjaxUrl(ajaxUrl) {
    AJAX_URL = ajaxUrl;
}

// yahoo ajax-handler functions-vars
var handleSuccess = function(o)
{
    if(o.responseText !== undefined)
    {
        try {
            var myJSONObject = eval('(' +  o.responseText + ')');
            try {
                if(myJSONObject.ajaxInvoke !== undefined) {
                    var sMethodCall = myJSONObject.ajaxInvoke + "_callback(";
                    if(myJSONObject.Control !== undefined) {
                        sMethodCall += 'myJSONObject';
                    }
                    sMethodCall += ");"
                    try {
                        eval(sMethodCall);
                    } catch(e) { /* Callback-Function not found */ }
                }
                if(myJSONObject.ajaxRender !== undefined) {
                    if(myJSONObject.Control !== undefined) {
                        var sMethodCall = myJSONObject.ajaxRender + "_callback(myJSONObject.Control.HTML);"
                        try {
                            eval(sMethodCall);
                        } catch(e) {
                            // no callback-function for this control -> simply replace it
                            replaceControl(myJSONObject.Control.ClientID,myJSONObject.Control.HTML);
                        }
                    } else {
                        alert("Could not render control!");
                    }
                }
            } catch(e) { 
                alert("Error in callback-handling!\n\n" + e);
            }
        } catch(e) {
          handleFailureResponse(o.responseText);
        }
    }
}
var handleFailure = function(o)
{    
  if(o.status != 0) {
    //alert("Allgemeiner Kommunikationsfehler.\n(" + o.status + ": " + o.statusText + ")");
    //document.getElementById("PageContent").innerHTML = o.responseText;
    handleFailureResponse(o.responseText);
  }
};
var callback =
{
  success:handleSuccess,
  failure:handleFailure,
  argument:['foo','bar']
};
function handleFailureResponse(responseText) {
  try {
    var CtrlID = /^.*"ClientID":"([^"]*)".*$/;
    var CtrlHtml = /^.*"HTML":"(.*[^\\])".*/;
    var ResponseHtml = CtrlHtml.exec(responseText)[1].replace(/\\"/g,'"').replace(/\\r|\\n|\\t/g,'');
    try {
      var ResponseCtrl = document.getElementById(CtrlID.exec(responseText)[1]);
      if (CtrlID.test(responseText) && ResponseCtrl) {
        ResponseCtrl.innerHTML = ResponseHtml;
      } else {
        throw new Error("Invalid Control-ID");
      }
    } catch(e2) {
      document.getElementById("PageContent").innerHTML = ResponseHtml;
    }
  } catch(e) {
    document.getElementById("PageContent").innerHTML = responseText;
  }
}

 // function to make ajax-request             	     
function AjaxRequest(sUrl, invokeMethod, renderControl, submitForm, sParameters)
{
    var postdata = "ajaxInvoke=" + invokeMethod + "&ajaxRender=" + renderControl;
    if(sParameters.length > 0)
        postdata += "&" + sParameters;
        
    if(submitForm) {
        YAHOO.util.Connect.setForm(document.getElementById("aspnetForm"))
        var request = YAHOO.util.Connect.asyncRequest('POST', sUrl + ( sUrl.indexOf("?") >= 0 ? "&" : "?" ) + postdata, callback);                                 
    } else {
        var request = YAHOO.util.Connect.asyncRequest('POST', sUrl, callback, postdata);
    }                                
}

// replace the content of a Control with given content (outerHTML replacement for ff etc)
function replaceControl(controlID,newContent) {
    var ctrl = document.getElementById(controlID);
    if(ctrl != undefined && ctrl != null) {
        var newInnerHTML = newContent.replace(/^<[^<]*>|[\r\n\t]|<[^<]*>$/g,"");
        ctrl.innerHTML = newInnerHTML;
        return true;
    } else {
        // control doesn't exist, so do nothing... (we could add it to dynamic content, but we don't...)
        return false;
    }
}

// disable .net postback submit
function disableSubmit() {
    document.getElementById("aspnetForm").onsubmit = function(){ return false; };
}

// Simply invoke the given Method on the Server using Ajax
function ajaxInvoke(methodName) {
    AjaxRequest(AJAX_URL,methodName,"",false,"");
    return false;
}

// Render and update single Server-Control
function get_control(control_id, sUrl) {
    AjaxRequest((sUrl == undefined ? AJAX_URL : sUrl),"",control_id,false,"");
}

// Load whole PageContent
function init_page(initUrl) {
    //setAjaxUrl(initUrl.replace(/\?.*/,""));
    setAjaxUrl(initUrl);
    AjaxRequest(initUrl,"InitPage","",false,"");
}
function InitPage_callback(AjaxResponse) {
    if(document.getElementById("PageContent") != undefined) {
        if(document.getElementById("PageContent").nodeName.match(/div/i)) {
            replaceControl("PageContent",AjaxResponse.Control.HTML);
        } else {
            document.getElementById("PageContent").innerHTML = AjaxResponse.Control.HTML;
        }
    } else {
        document.getElementsByTagName("body")[0].innerHTML = AjaxResponse.Control.HTML;
    }
    disableSubmit();
}

