﻿//站点根目录
//定义全局基本路径,变更时需要修改
//以/线结尾,如/tmd/
var sysPath = "";

function GetRootPath()
{
    var strFullPath = window.document.location.href;
    var strPath = window.document.location.pathname;

    var pos = strFullPath.indexOf(strPath);
    var prePath = strFullPath.substring(0, pos);

    var postPath = strPath.substring(0, strPath.substr(1).indexOf('/') + 1);
    return (prePath + postPath);
}

if (sysPath == "")
{
    sysPath = GetRootPath();
}

//以下为快捷键操作部分
$().ready(function()
{
    //生成所有快捷键
    setInputAccessKey();
});

//生成快捷键信息
function setInputAccessKey()
{
    $("*[AccessKey]").each(function()
    {
        if(this.accessKey.indexOf("快捷") != -1)
        {
            return;
        }
        
        var sTitle = this.title;
        if(sTitle + "a" != "a")
        {
            sTitle += "\n";
        }
        
        if(this.accessKey == "s")
        {
            this.title = sTitle + "快捷键:ALT + S ";
        }
        else
        {
            this.title = sTitle + "快捷键:ALT + " + this.accessKey.toUpperCase();
        }
    });
}

//判断文本是否为空
function isNull(str)
{
	///	<summary>
    ///		判断文本是否为空
	///	</summary>
	///	<param name="str" type="String">
    ///		请求判断的值
	///	</param>
	///	<returns type="bool">返回成功表示值为空，反之为失败</returns>
	
    if(str == undefined || str == "undefined" || str == null || str == "")
    {
        return(true);
    }
    else
    {
        return(false);
    }
}

function addTempUrl(theUrl)
{
	///	<summary>
    ///	为请求的URL添加临时变量,强制更新页面缓存
	///	</summary>
	///	<param name="theUrl" type="String">
    ///	请求添加的URL
	///	</param>
	///	<returns type="bool">返回带临时变量的URL</returns>

    if(theUrl.substring(theUrl.length-1) == "&")
	{
	    theUrl += "temp=" + new Date().toString();
	}
	else
	{
	    if(theUrl.indexOf("?") == -1)
	    {
	        theUrl += "?temp=" + new Date().toString();
	    }
	    else
	    {
	        theUrl += "&temp=" + new Date().toString();
	    }  
	}
	
	return theUrl;
}

function getOpenShowModalError(e)
{
	///	<summary>
    ///	为弹出模态窗口集中返回错误信息(指浏览器不支持时)
	///	</summary>
	///	<param name="e" type="String">
    ///	弹窗时出现的错误对象
	///	</param>
	///	<returns type="bool">返回完整的错误文本信息</returns>
	
    var msg = "无法弹出窗口！错误信息为:\n\n";
    msg += e.message + "\n\n";
    msg += "原因可能是：\n1、弹出窗口被拦截\n2、浏览器版本太低（低于5.5）。\n\n";
    msg += "解决办法：\n1、请将本站点设置为信任站点！\n2、升级浏览器版本为5.5或更高。"
    msg += "\n\n信任站点设置办法：";
    msg += "\n点击浏览器的“工具”－“Internet选项”－“安全”－“受信任的站点”－“站点”";
    msg += "\n将本站点添加到可信任的站点中即可。";
    
    return msg;
}

function openShowModalSource(win,theUrl,width,height)
{
	///	<summary>
    ///	弹出窗口的源代码,不被直接调用
	///	</summary>
	///	<param name="win" type="object">
    ///	弹出窗口对应的页面对象,可能是本页面也可能是父页面
	///	</param>
	///	<param name="theUrl" type="String">
    ///	弹出页面的地址
	///	</param>
	///	<param name="width" type="String">
    ///	页面宽度
	///	</param>
	///	<param name="height" type="String">
    ///	页面高度
	///	</param>
	///	<returns type="object">返回模态窗口返回的值,object</returns>
	
	var query = "center: Yes; help: Yes; resizable: Yes; status: no;scroll:Yes; dialogHeight :" + height + "px; dialogWidth:" + width + "px;";
	theUrl = addTempUrl(theUrl);
	try
	{
		//返回值,aa,弹出时传递当前窗口对象
	    var obj = win.showModalDialog(theUrl,win,query);
	    
		return(obj);
	}
	catch(e)
	{
	    var msg = getOpenShowModalError(e);
		alert(msg);
		return(null);
	}
}

function openShowModal(theUrl, width, height)
{
    ///	<summary>
    ///	弹出常规的模态窗口对象
    ///	</summary>
    ///	<param name="theUrl" type="String">
    ///	弹出页面的地址
    ///	</param>
    ///	<param name="width" type="String">
    ///	页面宽度
    ///	</param>
    ///	<param name="height" type="String">
    ///	页面高度
    ///	</param>
    ///	<returns type="object">返回模态窗口返回的值,object</returns>

    return (openShowModalSource(window, theUrl, width, height));
}

function openTopShowModal(theUrl, width, height)
{
	///	<summary>
    ///	弹出父窗口的模态窗口对象
	///	</summary>
	///	<param name="theUrl" type="String">
    ///	弹出页面的地址
	///	</param>
	///	<param name="width" type="String">
    ///	页面宽度
	///	</param>
	///	<param name="height" type="String">
    ///	页面高度
	///	</param>
	///	<returns type="object">返回模态窗口返回的值,object</returns>
	
	return(openShowModalSource(window.top,theUrl,width,height));
}

function openTreeWindow(theUrl, inputValueName, inputTextName, lkFunc, width, height)
{
    ///	<summary>
    ///	弹出选择树窗口的页面
    ///	</summary>
    ///	<param name="theUrl" type="String">
    ///	弹出页面的地址
    ///	</param>
    ///	<param name="inputValueName" type="String">
    ///	存储值的输入框名称
    ///	</param>
    ///	<param name="inputTextName" type="String">
    ///	存储文本的输入框名称
    ///	</param>
    ///	<param name="lkFunc" type="String">
    ///	随带的执行方法
    ///	</param>
    ///	<param name="width" type="String">
    ///	页面宽度
    ///	</param>
    ///	<param name="height" type="String">
    ///	页面高度
    ///	</param>
    ///	<returns type="object">返回模态窗口返回的值,object</returns>

    theUrl += "&ptype=0&pTid=" + inputTextName + "&pVid=" + inputValueName + "&lkFunc=" + lkFunc + "&";
    return (openShowModalSource(window, theUrl, width, height));
}

function openFrameModal(theUrl, width, height, Title)
{
    ///	<summary>
    ///	打开套着iframe的模态窗口,使得该页面可刷新
    ///	</summary>
    ///	<param name="theUrl" type="String">
    ///	弹出页面的地址
    ///	</param>
    ///	<param name="width" type="String">
    ///	页面宽度
    ///	</param>
    ///	<param name="height" type="String">
    ///	页面高度
    ///	</param>
    ///	<returns type="object">返回模态窗口返回的值,object</returns>
    theUrl = addTempUrl(theUrl);
    var url = sysPath + "/frame.aspx?toTitle=" + encodeURIComponent(Title) + "&toUrl=" + encodeURIComponent(theUrl);

    return (openShowModalSource(window, url, width, height));
}

function openTopFrameModal(theUrl, width, height, Title)
{
    ///	<summary>
    ///	打开套着iframe的模态窗口(从父窗口弹出),使得该页面可刷新
    ///	</summary>
    ///	<param name="theUrl" type="String">
    ///	弹出页面的地址
    ///	</param>
    ///	<param name="width" type="String">
    ///	页面宽度
    ///	</param>
    ///	<param name="height" type="String">
    ///	页面高度
    ///	</param>
    ///	<returns type="object">返回模态窗口返回的值,object</returns>
    theUrl = addTempUrl(theUrl);
    var url = sysPath + "/frame.aspx?toTitle=" + encodeURIComponent(Title) + "&toUrl=" + encodeURIComponent(theUrl);

    return (openShowModalSource(window.top, url, width, height));
}

function XmlPost(webFileUrl, sData, callBackFunc)
{
    ///	<summary>
    ///		以同步(或异步)的方式无刷新求取数据
    ///	</summary>
    ///	<param name="webFileUrl" type="String">
    ///		请求的页面地址,可带参数
    ///	</param>
    ///	<param name="sData" type="String">
    ///		页面参数，类URL传递参数方式，此参数将与页面地址进行合并，可空
    ///	</param>
    ///	<param name="callBackFunc" type="Function">
    ///		表示采用异步形式取值，为空则同步方式，可空,写时可直接写：function(){语法}
    ///	</param>
    ///	<returns type="String">返回请求的值</returns>	

    webFileUrl = addTempUrl(webFileUrl);
    var html = "";
    try
    {
        //同步方式 加了data 和 type
        if (callBackFunc)
        {
            $.ajax({
                url: webFileUrl,
                data: sData,
                type: 'Post',
                async: true,
                cache: false,
                success: callBackFunc
            });

        }
        else
        {

            html = $.ajax({
                url: webFileUrl,
                data: sData,
                type: 'Post',
                async: false
            }).responseText;


        }
    }
    catch (e)
    {
        alert(e)
        html = "脚本错误,未取到正确数据!";
    }

    return (html);
}

function CallClientClick(butName)
{
    ///<summary>
    /// 使用AJAX调用按钮点击
    ///</summary>
    ///<param name="butName">需要被点击的客户端按钮名</param>
    ///<returns type="" />
    
    if(isNull(butName) == true)butName = "btnF";
    try
    {
        AJAXCbo.DoAjaxCall(butName,"","");
    }
    catch(e)
    {
        //非AJAX方式,直接点击它
        try
        {
            document.getElementById(butName).click();
        }
        catch(e)
        {
            alert("提交按钮出现错误");
        }
    }
}


function GetReturnValue(strReturnValue,type)
{
	///	<summary>
    ///		解析弹出窗口中返回的值
    ///		3个值,使用|线分隔
    ///		state:状态值,1/0/-1,成功/失败/未知错误(也可自行定义返回状态值)
    ///		value:返回的主键
    ///		error:其他信息,此处可自行定义
    ///     var rtValue = new GetReturnValue(a);
    ///     返回值:rtValue.state,rtValue.value,rtValue.error
	///	</summary>
	///	<param name="strReturnValue" type="String">
    ///		被解析的返回值，必须满足|||（连续三竖线）分隔的格式，例：状态值|||返回值|||错误信息
	///	</param>
	///	<param name="type" type="String">
    ///		类别,text/json,默认为text,需要json处理时,应显示附加参数json(小写)
	///	</param>
	///	<returns type="GetReturnValue">返回对象</returns>

    this.state = "-1" ;
    this.value = "" ;
    this.error = "未接收到正确的返回值,可能是页面出现错误" ;

    if (isNull(strReturnValue) == true)
    {
        return;
    }
    
    var ary = strReturnValue.split("|||");
    if(ary.length != 3)
    {
        return;
    }
    
    try
    {
        this.state = ary[0];
        this.value = type == "json" ? eval("(" + ary[1] + ")") : ary[1];
        this.error = ary[2];
    }
    catch(e)
    {
        alert("解析返回值时发生格式错误,请检查代码!");
    }
}


function CallServerMethod(methodName, methodArgs, pageUrl, callBackFunc)
{
    ///	<summary>
    ///		客户端向服务器端请求执行某方法并且返回值
    ///		注意:传递的参数中如果有中文,请自行调用encodeURIComponent进行编码
    ///		    被调用的方法必须返回yd.core.ReturnValues对象,且1为成功,0为失败,-1为未知错误
    ///	</summary>
    ///	<param name="methodName" type="String">
    ///		方法名,必须为public方法(非静态)且有返回值(最好返回文本值),调用时请注意大小写
    ///	</param>
    ///	<param name="methodArgs" type="String">
    ///		传递能方法的参数值,参数名称应与方法中的参数名一致,先后次序可随便.
    ///		如:方法名为:public string GetM(int aa,string bbb)
    ///	</param>
    ///	<param name="pageUrl" type="String">
    ///		服务端方法所在的页面地址,默认为该调用脚本所在页面,此参数可不输入
    ///	</param>
    ///	<param name="callBackFunc" type="Function">
    ///		需要采取异步方式时，自行构造的一个方法,可空
    ///     可直接写function(){语法}
    ///	</param>
    ///	<returns type="ReturnValues">返回ReturnValues格式的文本值</returns>

    try
    {
        var theUrl = "";
        if (isNull(pageUrl))
        {
            var urlPath = window.location.pathname;
            theUrl = (urlPath.substring(0, 1) == "/" ? urlPath : "/" + urlPath);

        }
        else
        {
            theUrl = pageUrl;
        }

        theUrl += "?ydGetMethod=" + methodName;
        var a = XmlPost(theUrl, methodArgs, callBackFunc);

        return (a);
    }
    catch (e)
    {
        CallError(e);
        return ("");
    }
}


function ReturnValues(state,value,error)
{
	///	<summary>
    ///		客户端的输出返回值(一般应用于弹出窗口向父窗口提供返回值)
    ///		人工创建ReturnValues格式的值
	///	</summary>
	///	<param name="state" type="String">
    ///		执行状态,0/1/-1,失败/成功/未知错误,或自行构造返回状态值
	///	</param>
	///	<param name="value" type="String">
    ///		希望返回的值
	///	</param>
	///	<param name="error" type="String">
    ///		错误信息,可自行构造
	///	</param>
	///	<returns type="ReturnValues">人工创建ReturnValues格式的值</returns>

    return(state + "|||" + value + "|||" + error);
}

function myCurrentRead(sid,query,currentObj,url)
{
    ///<summary>
    /// 获取后台读取的记录,并且在当前行之下生成新行以显示
    ///</summary>
    ///<param name="sid">唯一主键,用于命名新行</param>
    ///<param name="query">传递的参数,如crw_id=xx&xxx_id=xx</param>
    ///<param name="currentObj">点击的对象</param>
    ///<param name="url">需要取值的页面,一般默认为本页面,可为空</param>
    ///<returns type="" />
    
    if(isNull(currentObj))
    {
        //获取当前点击对象(图片)
        var evt = null;

        try
        {
            evt = event ? event : (window.event ? window.event : null);
        }
        catch(e)
        {
            alert("无法查找到当前点击对象,请更换IE浏览器!");
        }   

        if(evt == null)return false;

        currentObj = evt.srcElement ? evt.srcElement : evt.target;
    }

    //需要新生成行的ID
    var newtrID = "tr_" + sid;
    var newtdID = "td_" + sid;
    var s = "<font color=#999999> 请稍候,正在读取信息...</font>";
    
    //切换显示与否
    if(currentObj.src.indexOf("_close") == -1)
    {
        //如果已打开则关闭
        currentObj.src = currentObj.src.replace("_open","_close");
        //删除行
        $("#" + newtrID).remove();
    }
    else
    {
        //如果关闭则打开
        currentObj.src = currentObj.src.replace("_close.gif","_open.gif");
        //插入新行
        var otr = $(currentObj).parent("td").parent("tr").eq(0);
        var columnCount = $(otr).children("td").length;
        
        var strNewTr = "";        
        strNewTr += "<tr id='" + newtrID + "'>";
        strNewTr += "   <td class='td-content'></td>";
        strNewTr += "   <td id='" + newtdID + "' colspan=" + (columnCount - 1) + " height=25 class=\"td-content\">";
        strNewTr += s ;
        strNewTr += "   </td>";
        strNewTr += "</tr>";
        
        //插入
        $(otr).after(strNewTr);
               
        //读取记录
        CallServerMethod("ClientReadBack",query,url,function(html)
        {
            var rtValue = new GetReturnValue(html);
            var str = "";
            
            if(rtValue.state == "1")
            {
                str = rtValue.value;
            }
            else
            {
                str = rtValue.error;
            }

            $("#" + newtdID).html(str);
        });
     }
}


function myRepeatRead(url,rootObject)
{
    ///<summary>
    /// 重复读取后台数据,并在不改变当前读取页面的条件下重新填充
    ///</summary>
    ///<param name="url">求取值的地址,一般以问号或与号结尾</param>
    ///<param name="rootObject">当前需要填充的最顶级对象(不在更新之外)</param>
    ///<returns type="" />    
              
    //读取记录
    var str = XmlPost(url);
    $(rootObject).html(str);
}

function getEditorValue(editorId)
{
    ///<summary>
    /// 取得指定在线编辑器内容
    ///</summary>
    ///<param name="editorId">编辑器的名称(ID)</param>
    ///<returns type="String">返回在线编辑器的HTML内容</returns>

    var o_Text = "";
    try
    {
	    var o_Editor = document.getElementById(editorId + "_Frame").contentWindow;
	    o_Text = o_Editor.getText();
    }
    catch(e)
    {
        CallError(e);
    }

    return o_Text;
}

//统一显示错误信息
//注意:此函数仅应用于try{}catch(e){}中的统一显示,其他处错误显示应自行构造
function CallError(e)
{
    alert("脚本出现错误:\n" + e.message);
}

//获取页面高度
function getPageHeight()
{
    if ($.browser.msie)
    {
        return document.compatMode == "CSS1Compat" ? document.documentElement.clientHeight : document.body.clientHeight;
    }
    else
    {
        return self.innerHeight;
    }
}

//获取页面宽度
function getPageWidth()
{
    if ($.browser.msie)
    {
        return document.compatMode == "CSS1Compat" ? document.documentElement.clientWidth : document.body.clientWidth;
    }
    else
    {
        return self.innerWidth;
    }
}

function getStringLength(str)
{
    ///<summary>
    /// 获取文本的字节长度
    ///</summary>
    ///<param name="str">需要计算的文本</param>
    ///<returns type="Number" />

    if(!str) { return 0; }      
    //循环计数     
    var i = 0;
    var a = 0;   
    for (i=0;i<str.length;i++)     
    {         
        if (str.charCodeAt(i)>255)          
        {             
            //按照预期计数增加2             
            a+=2;         
        }         
        else         
        {             
            a++;
        }
    }

    return a;
}

//截取字符串
function substr(str, len, isLeave) 
{
    ///<summary>
    /// 截取指定字节长度的字符串
    ///</summary>
    ///<param name="str">需要截取的文本</param>
    ///<param name="len">需要截取的长度</param>
    ///<param name="isLeave">是否在文本后添加省略号...,默认为是</param>
    ///<returns type="String" />

    if(!str || !len) { return ''; }   
    if(!isLeave)isLeave = true;  
     
    //预期计数：中文2字节，英文1字节     
    var a = 0;      
    //循环计数     
    var i = 0;      
    //临时字串     
    var temp = '';      
    for (i=0;i<str.length;i++)     
    {         
        if (str.charCodeAt(i)>255)          
        {             
            //按照预期计数增加2             
            a+=2;         
        }         
        else         
        {             
            a++;
        }
          
        //如果增加计数后长度大于限定长度，就直接返回临时字符串         
        if(a > len) { return temp;} 

        //将当前内容加到临时字符串         
        temp += str.charAt(i);     
    }

    //如果全部是单字节字符，就直接返回源字符串     
    return str + (isLeave ? "..." : "");
} 

//将对象不可用
//controlName:对象名
function disbButton(controlName)
{
    $("#" + controlName).attr("disabled",true);
}

//将对象可用
//controlName:对象名
function unDisbButton(controlName)
{
    $("#" + controlName).removeAttr("disabled");
}

function alterItemCss()
{
    ///<summary>
    /// 为列表添加交替行颜色变化
    ///</summary>

    $(".list-table tr:odd").addClass("tr-normal").hover(function(){$(this).addClass("tr-changed")},function(){$(this).removeClass("tr-changed")});
    $(".list-table tr:even").addClass("tr-alter").hover(function(){$(this).addClass("tr-changed")},function(){$(this).removeClass("tr-changed")});
}

//去头尾空格
String.prototype.trim = function()
{
    var r = this.replace(/(^\s*)|(\s*$)/g, "");
    r = Lremoveblank(r);
    r = Rremoveblank(r);
    return r;
}

function Lremoveblank(s)
{
    if (s.length == 1 && s.charCodeAt(0) == 160)
        return "";
    if (s.charCodeAt(0) == 160)
    {
        s = s.substr(1, s.length - 1);
        return removeblank(s);
    }
    else
    {
        return s;
    }
}

function Rremoveblank(s)
{
    if (s.length == 1 && s.charCodeAt(0) == 160)
        return "";
    if (s.charCodeAt(s.length - 1) == 160)
    {
        s = s.substr(0, s.length - 1);
        return Rremoveblank(s);
    }
    else
    {
        return s;
    }
}

//关闭窗口,当按ESC退出键时
function EscCloseOpenWindow()
{
    $().ready(function()
    {
        window.document.body.onkeypress = function()
        {
            if (event.keyCode == 27)
            {
                returnValue = "";
                self.close();
            }
        }
    });
}
///<summary>
/// 打开模态窗口
///</summary>
///<param name="theUrl">定位的页面地址,一般以?或&结尾</param>
///<param name="Width">打开窗口的宽度</param>
///<param name="Height">打开窗口的高度</param>
///<param name="Left">打开窗口左边位置</param>
///<param name="Top">打开窗口上边位置</param>
function openShowModalWithPos(theUrl, Width, Height, Left, Top)
{
    var Query = "center: Yes; help: Yes; resizable: no; status: no;scroll:Yes; dialogHeight :" + Height + "px; dialogWidth:" + Width + "px;dialogLeft: " + Left + "px;dialogTop: " + Top + "px;";
    if (theUrl.substring(theUrl.length - 1) == "&")
    {
        theUrl += "temp=" + new Date().toString();
    }
    else
    {
        if (theUrl.indexOf("?") == -1)
        {
            theUrl += "?temp=" + new Date().toString();
        }
        else
        {
            theUrl += "&temp=" + new Date().toString();
        }
    }

    try
    {
        //返回值,aa,弹出时传递当前窗口对象
        var aa = 0;
        aa = window.showModalDialog(theUrl, window, Query);

        return (aa);
    }
    catch (e)
    {
        var msg = "无法弹出窗口！\n\n";
        msg += "原因可能是：\n1、弹出窗口被拦截\n2、浏览器版本太低（低于5.5）。\n\n";
        msg += "解决办法：\n1、请将本站点设置为信任站点！\n2、升级浏览器版本为5.5或更高。"
        msg += "\n\n信任站点设置办法：";
        msg += "\n点击浏览器的“工具”－“Internet选项”－“安全”－“受信任的站点”－“站点”";
        msg += "\n将本站点添加到可信任的站点中去即可。";
        alert(msg);
        return (null);
    }
}
