﻿////////////////////////////////// Ajax 基本 //////////////////////////////////

var unique = 
{
    openLearning : { }
};

unique.openLearning.AjaxHandler = "../AjaxHandler.ashx";

myAjax = function(url, serviceType)
{
    if(url == null)
    {
        this.serviceUrl = unique.openLearning.AjaxHandler;
    }
    if(serviceType == null || (serviceType != myAjax.ServiceTypes.HttpHandler && serviceType != myAjax.ServiceTypes.WebService))
    {
        if(this.serviceUrl.match(/\.asmx$/ig))
        {
            this.serviceType = myAjax.ServiceTypes.WebService;
        }
        else
        {
            this.serviceType = myAjax.ServiceTypes.HttpHandler;
        }
    } else {
        this.serviceType = serviceType;
    }
    
    this.proxyUrl = "";
    this.request = new _XMLHttpRequest();
    this.request.engine = this;
};

myAjax.ServiceTypes = 
{
    HttpHandler : "HttpHandler",
    WebService : "WebService"
};

myAjax.prototype.initRequest = function(method, url, callback)
{
    if(this.request == null)
        this.request = new _XMLHttpRequest();
    this.request.open(method, url);
    this.request.onreadystatechange = callback;
};

myAjax.prototype.wrapCallback = function(onComplete, onSuccess, onFailure, onHttpError)
{
    return function(result)
    {
        if(this.isReady() && this.xhr.status == 200)
        {
            if(onSuccess == null && onFailure == null && onComplete != null)
            {
                onComplete(result);
            } else if(onSuccess != null && result.success) {
                onSuccess(result);
            } else if(onFailure != null && !result.success) {
                onFailure(result);
            }
        } else if(this.isReady() && onHttpError != null) {
            onHttpError(result);
        }
    };
};

myAjax.prototype.send = function(methodType, methodName, args, callback)
{
    if(this.serviceType == myAjax.ServiceTypes.HttpHandler)
    {
        this.initRequest("POST", this.serviceUrl, callback);
        this.request.send(methodType + "=" + methodName + "&" + args);
    } else {
        this.initRequest("POST", this.serviceUrl + "/" + methodName, callback);
        this.request.send(args);
    }
};

myAjax.prototype.doAction = function(actionName, actionData, onComplete, onSuccess, onFailure, onHttpError)
{
    var callback = this.wrapCallback(onComplete, onSuccess, onFailure, onHttpError);
    this.send("action", actionName, actionData, callback);
};

myAjax.prototype.doQuerry = function(querryName, querryData, onComplete, onSuccess, onFailure, onHttpError)
{
    var callback = this.wrapCallback(onComplete, onSuccess, onFailure, onHttpError);
    this.send("q", querryName, querryData, callback);
};

myAjax.prototype.postRequest = function(data, onComplete, onSuccess, onFailure, onHttpError)
{
    var callback = this.wrapCallback(onComplete, onSuccess, onFailure, onHttpError);
    this.initRequest("POST", this.serviceUrl, callback);
    this.request.send(data);
};

myAjax.prototype.sendRequest = function(method, data, callback)
{
    this.initRequest(method, this.serviceUrl, callback);
    this.request.send(data);
};

myAjax.prototype.getResult = function()
{
    return this.request.result;
};

window.getDataAsQueryString = function(id)
{
    var container = $(id);
    var ctrls = container.getElementsByTagName("input");
    var count = ctrls.length;
    var kvp = new KeyValuePair();
    for(var i = 0; i < count; i++)
    {
        var ctrl = ctrls[i];
        if(ctrl.type == "text" || ctrl.type == "hidden" || ctrl.type == "password")
        {
            kvp.add(ctrl.name ? ctrl.name : ctrl.id, ctrl.value);
        }
        else if(ctrl.type == "radio" || ctrl.checked)
        {
            kvp.add(ctrl.name ? ctrl.name : ctrl.id, ctrl.value);
        }
        else if(ctrl.type = "checkbox")
        {
            kvp.add(ctrl.name ? ctrl.name : ctrl.id, ctrl.checked);
        }
    }
    ctrls = container.getElementsByTagName("select");
    count = ctrls.length;
    for(var i = 0; i < count; i++)
    {
        var ctrl = ctrls[i];
        var index = ctrl.selectedIndex;
        if(index >= 0 && index < ctrl.options.length)
        {
            kvp.add(ctrl.name ? ctrl.name : ctrl.id, ctrl.options(index).value);
        }
    }
    ctrls = container.getElementsByTagName("textarea");
    count = ctrls.length;
    for(var i = 0; i < count; i++)
    {
        var ctrl = ctrls[i];
        kvp.add(ctrl.name ? ctrl.name : ctrl.id, ctrl.innerText);
    }
    return kvp.toQueryString();    
};

window.setChildEnable = function(id, enable)
{
    var container = $(id);
    var ctrls = container.getElementsByTagName("input");
    var count = ctrls.length;
    for(var i = 0; i < count; i++)
    {
        ctrls[i].disabled = !enable;
    }
    ctrls = container.getElementsByTagName("select");
    count = ctrls.length;
    for(var i = 0; i < count; i++)
    {
        ctrls[i].disabled = !enable;
    }
    ctrls = container.getElementsByTagName("textarea");
    count = ctrls.length;
    for(var i = 0; i < count; i++)
    {
        ctrls[i].disabled = !enable;
    }
};

function $(id)
{
    return document.getElementById(id);
}

function $V(id)
{
    return $(id).value;
}
function valueof(id)
{
    return $(id).value;
}
function getElement(id)
{
    return document.getElementById(id);
}

function XmlizeString(value)
{
    if (value == null) return null;
    value = value.replace("&", "&amp;");
    value = value.replace("<", "&lt;");
    value = value.replace(">", "&gt;");
    value = value.replace("\"", "&#34;");
    value = value.replace("'", "&#39;");
    return value;
}

function fullReplace(str, toFind, toReplaceWith)
{
    var temp = "";
    var offset = toReplaceWith.length;
    var position = str.indexOf(toFind);
    if(position < 0)
        return str;
    while(position >= 0 && str.length > 0)
    {
        str = str.replace(toFind, toReplaceWith);
        position += offset;
        temp += str.substr(0, position);
        str = str.substr(position);
        position = str.indexOf(toFind);
    }
    return temp;
}

function DeXmlizeString(value)
{
    if (value == null) return null;
    ret = fullReplace(value, "&lt;", "<");
    ret = fullReplace(value, "&gt;", ">");
    ret = fullReplace(value, "&#39;", "'");
    ret = fullReplace(value, "&#34;", "\"");
    ret = fullReplace(value, "&amp;", "&");
    return ret;
}

function Urlize(tagName)
{
    tagName = fullReplace(tagName, "?", "%3F");
    tagName = fullReplace(tagName, "&", "%26");
    tagName = fullReplace(tagName, "|", "%7C");
    tagName = fullReplace(tagName, "\\", "%5F");
    tagName = fullReplace(tagName, "=", "%3D");
    tagName = fullReplace(tagName, ":", "%3A");
    tagName = fullReplace(tagName, "/", "%2F");
    tagName = fullReplace(tagName, "#", "%23");
    return tagName;
}

//function myAjax(proxy)
//{
//    this.ActionUrl = "Action.aspx";
//    this.QuerryUrl = "querry.aspx";
//    this.ProxyUrl = "";
//}
// 过时的方法