/* ================================================================================
 		Desc: 登陆脚本
  
 		Called by:   
               
 		Auth: Gianteasy
 		Date: 2004-08-05
 ================================================================================
 		Change History
 ================================================================================
 		Date:		Author:				Description:
 		--------	--------			-------------------
    
 ================================================================================
 Copyright (C) 2004-2005 Gianteasy Corporation
 ================================================================================
*/


var g_cCookieExpires=31 *24; //30天

var URM=IncSrv("UserManageGE","LoginAjax");

function popsizewin(openUrl,tleft,ttop,twidth,theight,tscrollbar,tresizable)
{
    try
    {
        var win = window.open(openUrl,"","left=" + tleft +",top="+ ttop + ",width=" + twidth + ",height="+ theight + ",status=0,toolbar=0,menubar=0,location=0,scrollbars="+ tscrollbar+",resizable="+tresizable,false);
    }
    catch(e)
    {
        var win = window.open(openUrl,"index","left=" + tleft +",top="+ ttop + ",width=" + twidth + ",height="+ theight + ",status=0,toolbar=0,menubar=0,location=0,scrollbars="+ tscrollbar+",resizable="+tresizable,false);
    }
};


function fnLoadCfg(strAccountID) 
{
    var strKey="__GE_Login_"+strAccountID;
    var strResult="";
    strResult=GECookie.GetCookie(strKey);
    return strResult;
}

function fnSaveCfg(strAccountID,strVal)
{
    var strKey="__GE_Login_"+strAccountID;
    if(strVal.length>0)
        GECookie.SetCookie(strKey,strVal,g_cCookieExpires);
    else
        GECookie.DelCookie(strKey);
}
 
function fnSaveCookie(strAccountID,strUserName,strPassWD,bCookie)
{
    if(bCookie)
    {
        var strInfo="";
        var has=new GEHashtable();
        has.add("UID",strUserName);
        has.add("PWD",strPassWD);

        strInfo=has.toString();
        fnSaveCfg(strAccountID,strInfo);
    }
    else//清除记忆
    {
        var strInfo="";
        fnSaveCfg(strAccountID,strInfo);
    }
};

function fnShowCookie(strAccountID)
{
    var strTemp=fnLoadCfg(strAccountID);
    if(strTemp.length>0)
    {
        $('chkSaveInfo').checked=true;   
        var has=GEConvert.StringToHashtable(strTemp);
        var strUserName=has.getValue("UID","");
        var strPassWD=has.getValue("PWD","");
        RIA.UserCode.setValue(strUserName);
        RIA.PWD.setValue(strPassWD);
    }
    else
    {
        $('chkSaveInfo').checked=false;   
        RIA.UserCode.setValue("");
        RIA.PWD.setValue("");
    }
};


function fnClose()
{
   window.open("","_self");
   top.opener=null;
   top.close();
   //window.opener=null;
   //window.close();
};

function getAccountID()
{
    var strAccountID=g_strDefaultAccountID;
    if(RIA.AccountName!=null)
        strAccountID=RIA.AccountName.getValue();
    return strAccountID;
};


function fnLogin()
{
    if(GEFormUtil.validForm('loginTbl'))
    {
        var strUserName=RIA.UserCode.getValue();
        var strPassWD=RIA.PWD.getValue();
        var strAccountID=getAccountID();
          
	    var oRt=URM.Login(strAccountID,strUserName,strPassWD).value;
	    if(oRt!=null)
	    {   
	        if(oRt.bResult)
	        {
	            //保存Cookie
	            fnSaveCookie(strAccountID,strUserName,strPassWD,$('chkSaveInfo').checked);
                if(window.opener!=null)
                {
                   window.top.location.href=g_ApplicationPath+'default.aspx';
                }
                else
                {
                    var aw = window.screen.availWidth; 
                    var ah = window.screen.availHeight;
                    popsizewin(g_ApplicationPath+'default.aspx' ,0,0,aw-10,ah-20,1,1); 
                    fnClose();
                }
	        }
	        else
	        {
		        ShowMessage(oRt.strError);
	        }
	    }
	}
};

function getDutysNo()
{
    if(RIA.DutyNo != null)
        g_strDutysID = RIA.DutyNo.getValue();
    
    return g_strDutysID;
}

function fnValid(strDutysNo)
{
    var bFlag = true;
    
    if($("TrDutyName").style.display == "inline" && $("TrSellName").style.display == "inline")
    { 
        if(strDutysNo == null || strDutysNo == "")
        {
            bFlag = false;
        }
    }
    return bFlag;
}

function fnLoginDutys()
{
    if(GEFormUtil.validForm('loginTbl'))
    {
        var strUserName=RIA.UserCode.getValue();
        var strPassWD=RIA.PWD.getValue();
        var strAccountID=getAccountID();
        var strDutysNo = getDutysNo();  
        
        //验证
        if(!fnValid(strDutysNo))
        {
            ShowMessage("班次信息不能为空，请选择班次！");  
            return;
        }
         
	    var oRt=URM.DutyLogin(strAccountID,strUserName,strPassWD,strDutysNo).value;
	    if(oRt!=null)
	    {   
	        if(oRt.bResult)
	        {
	            //保存Cookie
	            fnSaveCookie(strAccountID,strUserName,strPassWD,$('chkSaveInfo').checked);
                if(window.opener!=null)
                {
                   window.top.location.href=g_ApplicationPath+'default.aspx';
                }
                else
                {
                    var aw = window.screen.availWidth; 
                    var ah = window.screen.availHeight;
                    popsizewin(g_ApplicationPath+'default.aspx' ,0,0,aw-10,ah-20,1,1); 
                    fnClose();
                }
	        }
	        else
	        {
		        ShowMessage(oRt.strError);
	        }
	    }
	}
};

function SetAppName()
{
    document.getElementById("AppName").innerHTML=g_strTitle;
    setTitle(g_strTitle);
};

