﻿//
// septnetFramework.URIResource JS (chenjiahai@yahoo.cn)
// ver: 4.7.3712.3
//      

RV = function()
{
    // 初始化
    
    this.success= true;

    this.status = "+OK";
    this.content= "";
    this.error  = "";    
    
    this.Parse = function(r)
    {    
        if(r.length == 0)
            return this;
            
        // 解析
        
        if(r.substring(0,3) != "+OK" && r.substring(0,4) != "-ERR")
        {
            alert(r);
            
            this.status = "-ERR";
            this.success= false;
            this.error = "服务器返回的消息格式不是 +OK | -ERR, 请检查URIResource对象相关函数。";

            return this;
        }

        var vs = splitr(r);
        
        this.status = vs[0];
        
        if(vs[0] == "+OK")
        {
             this.success = true;
             this.error = "";
             this.content=vs[1];
        }     
        else
        {
             this.success = false;
             this.error = vs[1];
             this.content="";
        }     
        
        return this;
    }
    
    return this;
}

// Http事件

function Http()
{

}

Http.start= function(){}
Http.end  = function(){}

// verify septnetApplet Runtime Library

function septnetAppletRuntimeLibraryHelper(tag)
{
    if(parent.document.getElementById("viModalDialog"))
    {
        parent.hidewaiter();
        
        if(tag == 1)
            openUrl('~/View/dlgRuntimeLibrarymInstall.aspx');   // 在当前对话框中打开下载页面
        else
            openUrl('~/View/dlgRunAsAdministrator.aspx');
    }    
    else
    {
        hidewaiter();
        
        if(tag == 1)
            modalDialog('~/View/dlgRuntimeLibrarymInstall.aspx',700,200);
        else
            modalDialog('~/View/dlgRunAsAdministrator.aspx',700,200);
    }
}

function septnetAppletRuntimeLibrary()
{
    try
    {
        ieexts = new ActiveXObject("7net.septnetApplet");
        
        if(ieexts.Version != "4.0.3712.3")
        {
            ieexts = null;
            alert("7netApplet Runtime Library 未更新 !\r\n\r\n*请从控制面板\"添加/删除程序\"中卸载该软件并重新下载安装程序安装  ");
            return false;  
             
        }
        else
        {
            if(ieexts.IsAdministrator() == "1")
                return true;
            else
            {
                ieexts = null;
                alert("当前操作系统必须\"以管理员身份运行\"浏览器才能加载7netApplet !");
                
                septnetAppletRuntimeLibraryHelper(2);
                return false;
            }    
        }    
    }
    catch(e)
    {
        ieexts = null;
        alert('7netApplet Runtime Library 未安装 !');
        
        septnetAppletRuntimeLibraryHelper(1);
        return false;    
    }     
}

// 去掉字串开始的小写字母

function rvprefix(v)
{
    if(v == v.toLowerCase())
        return v;
        
    var arr = v.split("");
    
    var idx = 0;
    for(idx = 0; idx < arr.length; idx++)
    {
        if(arr[idx] == arr[idx].toUpperCase())
            break;
    }
    
    return v.substring(idx, v.length);
}

// 资源操作

URI = function(url, fp) 
{ 
    this.url = url;
    
    // 错误回调 ！
    
    this.f = "";
    
    if(fp)
    {
        if(typeof(fp) == 'function')
            this.f = fp;
        
    }    
    
    // 将相对路径转化为绝对路径
    
    this.parseRelativeUrl = function(url)
    {
        if(!url)
        {
            alert("资源 URI 未设置 !");
            return "";
        }
        
        if(url.toLowerCase().indexOf("http://") == 0)
            return url;
        
        if(url.charAt(0) == "/")
             url = "~" + url;
        else if(url.charAt(0) != "~" && url.charAt(0) != "/")
             url = "~/" + url;
        
        // IE调用applet , 添加 site 参数
        
        if(url.toLowerCase().indexOf('~/applet/localhost') == 0)
        {
            // 如果signcode未设置，自动添加signcode 
            
            if(url.indexOf("signcode=") < 0)
            {
                if(url.indexOf('?') < 0)
                    url += '?signcode=1';
                else
                    url += '&signcode=1'; 
            }
            
            // 如果site未设置，自动将当前域作为下载地址
            
            if(url.indexOf("site=") < 0)
            {
                if(url.indexOf('?') < 0)
                    url += '?site=' + mapurl('~/');
                else
                    url += '&site=' + mapurl('~/'); 
            }
            
            return url;
            
        }
        else
        {
            return mapurl(url);
            
        }
    }
    
    // IE插件

    this.ieexts = null;
    
    this.verifyieexts = function()
    {
        return septnetAppletRuntimeLibrary();
 
    }
    
    // 判断路径是否符合IE调用约定
    
    this.isIEApplet = function()
    {
        return this.url.indexOf("applet/localhost") > 0;
    
    }
    
    // Http: post, put, delete
    
    // 在请求字串中添加 httpmethod 参数，参数值为 put | del

    this.addQuerystring = function(urltail)
    {
        var pos = this.url.indexOf("?");
        
        if(pos < 0)
            this.url += "?httpmethod=" + urltail;
        else
        {
            if(this.url.indexOf("httpmethod=")<0)
                this.url += "&httpmethod=" + urltail;   
            
        }
     }    
    
    // Http post 数据

    this.data   = "";

    this.httpstart = null;
    this.httpend   = null;
    
    this.Form = function(fn, fv)
    {
        if(isIEApplet())
        {
            ieexts.AddForm(fn, fv);
            
        }
        else
        {
            if(this.data.length == 0)
            {
                this.data = fn + '=' + encodeURIComponent(fv);
                
            }    
            else
            {
                this.data += '&' + fn + '=' + encodeURIComponent(fv);
                
            }    
        }
        
        return this;
        
    }
     
    this.getformdata = function(formid)
    {
        var t_data = $("#" + formid + " input, #" + formid + " select, #" + formid + " textarea").serialize();
      
        // 将data的数据项名称修改为服务器控件的ID
        
        var rs = "";
        var vs = t_data.split('&');
        
        for(idx = 0; idx < vs.length; idx++)
        {
            if(rs.length > 0)
                rs += "&";
                
            var ts = vs[idx].split('=');
            var tn = unescape(ts[0]);
            
            if(tn.lastIndexOf('$') < 0)
            {
                rs += rvprefix(tn) + "=" + ts[1];
                
            }
            else
            {
                rs += rvprefix(tn.substring(tn.lastIndexOf( '$') + 1,tn.length)) + "=" + ts[1];
                
            }    
        }
        
        //
        
        if(this.data.length == 0)
            this.data = rs;
        else
            this.data += '&' + rs;
                
    }       
    
    // 状态转换 ！ 用在Post,Put,Delete之后，可以理解为successor或操作成功后的动作 ！
    
    this.s = "";
    
    this.S = function(url)
    {
        this.s = url;
        
    }
    
    /*函数是否存在*/
    this.funExists = function(funName){ 
        try
        {  
            if(typeof eval(funName)=="undefined"){return false;} 
            if(typeof eval(funName)=="function"){return true;}
        }
        catch(e)
        {
            return false;
        } 
    }
    
    this.VerifyInput = function(e, check)
    {
        // 参数检测
        
        if(e)
        {
            if($('#' + e).length == 0)
            {
                alert("页面元素 " + e + " 不存在 !");
                return false;
            }
            
            this.getformdata(e);
        }
        
        // 验证用户输入

        var validationResult = true;
        
        if(check)
        {
            if(typeof(check) == 'function')
            {
                validationResult = check();
            
            }
            else
            {
                validationResult = false;
                alert('输入验证格式错误 ! 输入验证只能是返回boolean类型的函数,如忽略该参数将采用验证控件代码 !');
                
            }
        }
        else
        {
            if(e)
            {
                if(funExists('Page_ClientValidate'))
                    validationResult = Page_ClientValidate(); 
                
            }
                
        }
        
        return validationResult;
    }
    
    this.PostDataCallback = function(rv)
    {
        if(typeof(this.s) == "function")
        {
            this.s(rv.content);
        }
        else if(typeof(this.s) == "string")
        {
            if(this.s.length > 0)
            {
                var location = this.parseRelativeUrl(this.s);
                
                if(location.length > 0)
                    window.location.href = location;
            }    
            else
            {
                update();
                
            }    
        }    
    }
    
    this.UriPostData = function(callback, fp)
    {
        if(this.data.length == 0)
            this.data = '7net=7net';
    
        // 发送请求，执行回调或调整状态（如未指定后继，则更新当前状态）

        $.ajax({
            url: this.url,
            type: "POST",
            data: this.data,
            beforeSend: function() { if(this.httpstart) this.httpstart(); else Http.start(); },
            complete: function() { if(this.httpend) this.httpend(); else Http.end(); },
            success: function(r){
            
                var rv = RV().Parse(r);
                
                if(!rv.success)
                {
                    if(typeof(fp) == 'function')
                        fp(rv.error);
                    else
                        alert(rv.error);
                        
                }
                else
                {
                    callback(rv);  
                
                }    
            },
            error: function(r)
            {
                if(typeof(fp) == 'function')
                    fp('不能访问, 请检查网络!\r\n' + this.url);
                else
                    alert('不能访问, 请检查网络!\r\n' + this.url);
            
            }
        });
    }
    
    //
    // Get使用说明：
    //
    //      1. Get(e[,check]) 获取页面
    //      2. Get(e[,check]).S(callback) 获取数据
    //
    this.GetCallback = function()
    {
        if(typeof(this.s) == 'function')
        {
            // 添加方法后缀
        
            this.addQuerystring("get");
            
            // 发送请求
            
            UriPostData(PostDataCallback, this.f);
        }
        else
        {
            if(this.data.length > 0)
            {
                var pos = this.url.indexOf("?");

                if(pos < 0)
                    window.location.href = this.url + '?' + this.data;
                else
                    window.location.href = this.url + '&' + this.data;
            } 
            else
            {
                window.location.href = this.url;
            }   
        }    
    
    }
    
    // 调用IE插件执行特定方法
    
    this.ieextsRV = function()
    {
        if(ieexts.ReturnValue == '')
        {
            window.setTimeout("ieextsRV()", 10);
            
        }
        else
        {
            var rv = RV().Parse(ieexts.ReturnValue);
        
            if(!rv.success)
            {
                if(typeof(fp) == 'function')
                    fp(rv.error);
                else
                    alert(rv.error);
                
            }
            else
            {
                window.setTimeout(function(){ PostDataCallback(rv); }, 10);
            
            }
        }            
    }
    
    this.ieextsInvoke = function(verb)
    {
        ieexts.SetData(this.url, this.data);
        ieexts.InvokeHttpVerb(verb);
     
        window.setTimeout("ieextsRV()", 10);
    }
    
    this.Get = function(e, check)
    {
        // 检测资源URI，并将资源相对路径转化为绝对路径

        this.url = this.parseRelativeUrl(this.url);
        
        if(this.url.length == 0)
            return this;
  
        // 验证用户输入
        
        if(!VerifyInput(e, check))
            return this;
      
        // Invoke verb
        
        if(isIEApplet())
        {
            if(!verifyieexts())
                return this;
        
            ieextsInvoke("Get");
            
        }
        else
        {
            window.setTimeout('GetCallback()', 10);
        
        }
        
        return this;
    }
    
    this.Post = function(e, check)
    {
        // 检测资源URI，并将资源相对路径转化为绝对路径
        
        this.url = this.parseRelativeUrl(this.url);
        
        if(this.url.length == 0)
            return this;
        
        // 验证用户输入
        
        if(!VerifyInput(e, check))
            return this;
        
        // Invoke verb
        
        if(isIEApplet())
        {
            if(!verifyieexts())
                return this;
            
            ieextsInvoke("Post");
        
        }
        else
        {
            // 发送请求
            
            UriPostData(PostDataCallback, this.f);
        
        }
        
        return this;
    }
    
    this.Put = function(e, check)
    {
        // 检测资源URI，并将资源相对路径转化为绝对路径
        
        this.url = this.parseRelativeUrl(this.url);
        
        if(this.url.length == 0)
            return this;

        // 添加方法后缀
        
        this.addQuerystring("put");
        
        // 验证用户输入
        
        if(!VerifyInput(e, check))
            return this;
        
        // Invoke verb
        
        if(isIEApplet())
        {
            if(!verifyieexts())
                return this;
            
            ieextsInvoke("Put");
        
        }
        else
        {
            // 发送请求
            
            UriPostData(PostDataCallback, this.f);
        
        }
        
        return this;
    }
    
    this.Delete = function(e, check)
    {
        // 检测资源URI，并将资源相对路径转化为绝对路径
        
        this.url = this.parseRelativeUrl(this.url);
        
        if(this.url.length == 0)
            return this;
        
        // 添加方法后缀
        
        this.addQuerystring("del");
        
        // 验证用户输入
        
        if(!VerifyInput(e, check))
            return this;
        
        // Invoke verb
        
        if(isIEApplet())
        {
            if(!verifyieexts())
                return this;
            
            ieextsInvoke("Delete");
        
        }
        else
        {
            // 发送请求
            
            UriPostData(PostDataCallback, this.f);
        
        }
        
        return this;      
    }
    
    return this;
}







