﻿/**
 *  文件     :  AppFunction.js
 *　描述     :  基本方法实现
 *  创建者   :  zhuyanli 2009-3-19
 */




/**
 * 初始化系统变量AppConfig的oGlobal_Var
 */ 
 
//sTitle
oGlobal_Var.sTitle = "国库集中收付(政府采购)网上申拨系统";
window.document.title = oGlobal_Var.sTitle;
//sWebUrl
oGlobal_Var.sWebUrl = fnGetWebUrl();
//sAjaxUrl
oGlobal_Var.sAjaxUrl = oGlobal_Var.sWebUrl + 'WebApp/Core/RequestServicePage.aspx';
//sExportExcelUrl
oGlobal_Var.sExportExcelUrl =  oGlobal_Var.sWebUrl + 'WebApp/Core/ExportExcel.aspx';
//nPageSize
oGlobal_Var.nPageSize= 50; 


/**
 * 系统登陆成功与否
 * 
 */
function fnAppLogined()
{   
    if ( fnGetCookie("UserID") == null || fnGetCookie("UserID") == undefined || fnGetCookie("UserID") == ""|| fnGetCookie("LoginDate") == null || fnGetCookie("LoginDate") == undefined || fnGetCookie("LoginDate") == "" )
    {
        oLogin_Var.bLogin = false;
        return false;
    }
    else
    {
        oLogin_Var.bLogin = true;
        return true;
    }
}

/**
 * 取得系统的虚拟目录路径
 * 
 */
function fnGetWebUrl()
{
    var webUrl = window.location.href;
    webUrl = webUrl.substr(0,webUrl.lastIndexOf("/")+1); 
    return webUrl;
}
 
/**
 * 方法说明: 根据名称取得Cookie值
 * 参数说明: 
 * name: Cookie名称
 */
function fnGetCookie(name)   
{
    var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
     if(arr != null) return decodeURI(arr[2]); return null;

}

/**
 * 导出当前页数据到Excel
 * 创建ActiveXObject("Excel.Application")对象,拷贝数据的方式导出到Excel
 * sTagName : 要导出内容的标识名称
 */
function fnAutomateExcel(sTagName) 
{ 
    window.clipboardData.setData("Text",document.all(sTagName).outerHTML);   
    try  
    {   
        var ExApp = new ActiveXObject("Excel.Application")   
        var ExWBk = ExApp.workbooks.add()   
        var ExWSh = ExWBk.worksheets(1)   
        ExApp.DisplayAlerts = false  
        ExApp.visible = true  
    }     
    catch(e)   
    {   
        alert("您的电脑没有安装Microsoft Excel软件！或者没有开启ActiveX插件运行")   
        return false  
    }    
    ExWBk.worksheets(1).Paste;    

}

/**
 * 打印当前页数据
 * 复制数据到新的页面.再打印新页面
 * sTagName  : 要打印内容的标识名称
 */
function fnGlobalPrintGrid(sTagName)
{
    var prtGrid = document.getElementById(sTagName);
    
    var winPrint = window.open('','','letf=0,top=0,width=1,height=1,toolbar=0,scrollbars=0,status=0,menubar=0');
    winPrint.document.write(prtGrid.innerHTML);
    winPrint.document.close();
    winPrint.focus();
    winPrint.print();
    winPrint.close();
}

/**
 * 判断上传文件是否符合指定格式文件
 * oFile: 当前选择文件的控件
 * suffix : 扩展名 ( 字符串格式,可以多个扩展名'txt/doc')
 */
function fnVerifyFileExtension(oFile,suffix)
{
    var pos = oFile.value.lastIndexOf(".");
    var lastname = oFile.value.substring(pos+1,oFile.value.length).toLowerCase();
    if ( suffix.indexOf(lastname) < 0)
    {
        Ext.MessageBox.alert('警告','上传的文件格式为:' + suffix + '!');     
        oFile.setValue(""); 
    }
}

/**
 * 上传多个文件时,判断当前上传的文件是否已经存在.
 * oFile: 当前选择文件的控件
 * oFormPanel : 当前表单
 */
function fnVerifyFileObject(oFile,oFormPanel)
{
    var bFlExist = false;
    var curCmp = null;
    
    for(var i = 0 ; i < oFormPanel.items.length; i++ )
    {
        curCmp = null;
        
        if ( oFormPanel.items.items[i].xtype == "fileuploadfield")
        {
            curCmp = oFormPanel.items.items[i];            
        }
        
        if ( oFormPanel.items.items[i].xtype == undefined )
        {
            if ( oFormPanel.items.items[i].items.items[0].items.items[0].xtype == "fileuploadfield" )
                curCmp = oFormPanel.items.items[i].items.items[0].items.items[0];            
        }
        
        if ( curCmp != null )
        {
            if ( oFile != curCmp )
            {
                if ( oFile.value == curCmp.value )
                {
                    bFlExist = true;
                    break;
                }
            }
        }            
    }
    
    if ( bFlExist )
    {
        Ext.MessageBox.alert('警告','文件已经存在!');     
        oFile.setValue("");   
    }
}



/**
 * 初始化窗口toolbar
 * win当前窗口
 * unitid 当前选中单位ID
 */
function fnInitWindowTopBar(win,unitid)
{
    fnGetWindowTopBar(win,unitid);
}

/**
 * 请求取得当前用户的工具栏的功能权限
 * 
 */
function fnGetWindowTopBar(win,unitid)
{
    if ( !oTopToolBar_Var.bIsLoaded || oTopToolBar_Var.sItems == "" )
    {
        Ext.Ajax.request({
            url:oGlobal_Var.sAjaxUrl,
            method:'GET',
            params:{
                Class:'RightWebMethod',
                Method:'RequestGetModuleByUserID',
                sUnitID:unitid
            },
            success:function(v){           
                oTopToolBar_Var.sItems = v.responseText;
                
                if ( oTopToolBar_Var.sItems != "" )
                    fnLayoutWindowTopBar(win);
                    
                oTopToolBar_Var.bIsLoaded = true;                  
            }
        });
    }
    else
        fnLayoutWindowTopBar(win);    
}
 
/**    
 * 根据权限设置工具栏按钮
 */
function fnLayoutWindowTopBar(win)
{    
    var tbar = win.getTopToolbar();
    var sp = false; //分隔符是否显示
    for ( var i = 0 ; i < tbar.items.items.length ; i++ )
    {
        if ( tbar.items.items[i].type == "button" )
        {
            if ( oTopToolBar_Var.sItems.indexOf(tbar.items.items[i].id + ",") < 0 )
            {
                sp = true;
                tbar.items.items[i].setVisible(false);
            }
            else
            {
                sp = false;
                tbar.items.items[i].setVisible(true);
            }
        }
        else
        {
            if ( sp )
            {
                if ( tbar.items.items[i].xtype == undefined )
                    tbar.items.items[i].setVisible(false);     
            }       
            else
                tbar.items.items[i].setVisible(true);
        }
    }
}


/**
 * 取得服务器日期
 *
**/
function fnAjaxGetServiceDate(){
    Ext.Ajax.request({
        url: oGlobal_Var.sAjaxUrl,
        method: 'GET',
        params:{
            Class  : 'PageBase',
            Method : 'GetServerDateTime'
        },
        success: function(v) {
            return v.responseText;                    
        },
        failure: function(v) { Ext.Msg.alert('警告','请刷新页面!'); }
    });
}