﻿/**
* WebClient 中外合资创世科技版权所有
* FileName.......: loginfs.js
* Project........: WebClient
* Create DateTime: $Date: 2008/11/10 11:09:00 $
*/
var TEvent ={
    e:window.event
}

var Login = {    
    version:"v2008.11.10",
    lastModifyTime:"2008/11/11 08:44:00",
    historyModifyNote:[
       {"time":"2008/11/10 11:09:00","note":"1. 初始化登录页面.\n2. 从cookie里获取,设置登录信息"},
       {"time":"2008/11/11 08:44:00","note":"1. 登录主页面.\n"},
    ],
    logininformation:new Array(),
    cmbPath:null,           // 模拟combobox控件
    
    // 初始化登录页面
    Init:function(idLoginBox)
    {
        try
        {
            // 先删除原body里所有元素

            var childsBody = $$("DIV");       
            for(var i = 0;i < childsBody.length;i++)
            {
                if(childsBody[i])
                childsBody[i].remove();
            }
            
            if(typeof idLoginBox == "undefined" || idLoginBox == "" || idLoginBox == null)
            {
                idLoginBox = "loginbox";
            }
            
            // 没有登录框对象

            if(!$(idLoginBox))
            {
                var objLoginBox = document.createElement("DIV");
                objLoginBox.setAttribute("id",idLoginBox);
                //objLoginBox.className = "loginbox";
                document.getElementsByTagName("body").item(0).appendChild(objLoginBox);
            }            
            var htmlstr = "";           

            htmlstr += "<div class=\"loginboxtitle\"><div style=\"float:right;\"><div style=\"text-align:right;margin:2px 5px 0px 0px;font-size:12px;\"><a href=\"webplugs.exe\" target=_self style=\"text-decoration:none;color:#ff0000;\" title=\"如果没有安装网络视频插件或者无法通过网页自动下载安装，请下载手动安装网络视频插件！\" >视频插件下载</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"#self\" onclick=\"WebClientStyleSheet.SetStyleSheet('blue');window.location.reload();\"><img src=\"images/blue.png\" border=0 /></a>&nbsp;&nbsp;<a href=\"#self\" onclick=\"WebClientStyleSheet.SetStyleSheet('newyear');window.location.reload();\"><img src=\"images/newyear.png\" border=0 /></a>&nbsp;&nbsp;</div><div class=\""+(WebClient.showLogo == true ? "logo" : "nologo")+"\"></div></div></div>";
            htmlstr += "<div class=\"loginbox\"><form onsubmit=\"Login.Login();return false;\" >";
            
            htmlstr += "<div><span class=\"text\">"+loginLanguage.loginTitle.path+"&nbsp;&nbsp;</span><span class=\"input\" ><span id=\"cmbPath\" name=\"cmbPath\" ></span></span><span class=\"note\" id=\"t_path_note\">"+loginLanguage.loginTitle.pathNote+"</span></div>";
            htmlstr += "<div><span class=\"text\">"+loginLanguage.loginTitle.username+"&nbsp;&nbsp;</span><span class=\"input\"><input type=\"text\" name=\"t_username\" id=\"t_username\" value=\"\" maxlength=\"32\" /></span><span class=\"note\" id=\"t_username_note\">"+loginLanguage.loginTitle.usernameNote+"</span></div>";
            htmlstr += "<div><span class=\"text\">"+loginLanguage.loginTitle.password+"&nbsp;&nbsp;</span><span class=\"input\"><input type=\"password\" name=\"t_password\" id=\"t_password\" value=\"\" maxlength=\"32\" /></span><span class=\"note\" id=\"t_password_note\"><input type=\"checkbox\" name=\"t_savepwd\" id=\"t_savepwd\" onfocus=\"this.blur()\" />"+loginLanguage.loginTitle.savePassword+"</span></div>";
            
            //htmlstr += "<div class=\"submitbox\"><span class=\"input\" style=\"width:100%;text-align:center;\"><input type=\"submit\" value=\""+loginLanguage.loginTitle.loginBtn+"\" style=\"\" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type=\"button\" value=\""+loginLanguage.loginTitle.exitBtn+"\" onclick=\"Login.Cancel();\" /></span></div>";
            
            htmlstr += "<div class=\"submitbox\"><span class=\"input\" style=\"width:100%;text-align:center;\"><input type=\"submit\" value=\""+loginLanguage.loginTitle.loginBtn+"\" onfocus=\"this.blur()\" style=\"cursor:pointer;\" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type=\"button\" value=\""+loginLanguage.loginTitle.exitBtn+"\" onclick=\"WebClient.Exit();\" onfocus=\"this.blur()\" style=\"cursor:pointer;\" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></div>";
            htmlstr += "</form></div>";
            
            $(idLoginBox).innerHTML = htmlstr;
            
            this.cmbPath = new ComBoBox("cmbPath",145,Login.OnChange_ServerPath);
            WebClient.CurBody.Set("login");            
            WebClient.LoginStatus.Set(true);
            $("t_username").focus();
            
            this.GetHistoryInfo();
            
        }
        catch(e)
        {
            if(WebClient.debug)
            {
                alert("Login.Init:"+e.message+","+e.name);
            }
            throw(e);
            return false;
        }
        return true;
    },
    
    // 初始化弹出登录框
    InitPop:function()
    {
        return true;    
    },
    
    // 登录,开始生成主页面
    Login:function()
    {
        //alert("开始登录远程服务器获取资源");
        //alert(ResourceUser.Version);
        try
        {   
            if(!this.cmbPath.oTextBox.value.split(":")[1])
            {
                this.cmbPath.oTextBox.value = this.cmbPath.oTextBox.value.split(":")[0]+":8888";
            }
            
			var strRegex = "^((https|http|ftp|rtsp|mms)?://)" 
							+ "?(([0-9a-z_!~*'().&=+$%-]+: )?[0-9a-z_!~*'().&=+$%-]+@)?" //ftp的user@ 
							+ "(([0-9]{1,3}\.){3}[0-9]{1,3}" // IP形式的URL- 199.194.52.184 
							+ "|" // 允许IP和DOMAIN（域名）
							+ "([0-9a-z_!~*'()-]+\.)*" // 域名- www. 
							+ "([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\." // 二级域名 
							+ "[a-z]{2,6})" // first level domain- .com or .museum 
							+ "(:[0-9]{1,4})?" // 端口- :80 
							+ "((/?)|" // a slash isn't required if there is no file name 
							+ "(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$"; 
			var re=new RegExp(strRegex);
			
            if(this.cmbPath.oTextBox.value == "" || !re.test(this.cmbPath.oTextBox.value))
            {
                alert("请输入正确的路径!");
                this.cmbPath.oTextBox.focus();
                return;
            }            
            
            if($F("t_username") == "")
            {
                alert("请输入您的登录帐号");
                $("t_username").focus();
                return;
            }
            
            this.SetLoginInfo();
           
            if(typeof ResourceUser == "object")
            {
                ResourceUser.Init({path:this.cmbPath.oTextBox.value,username:$F("t_username"),password:$F("t_password"),savepwd:($("t_savepwd").checked? true:false)});
            }
        }
        catch(e)
        {
            if (WebClient.debug)
            {
                alert(e.message);
            }
            throw (e);
            return false;
        }
        return false;
    },
    OnChange_ServerPath:function()
    {
        if(Login.cmbPath)
        {
            var selectedIndex = Login.cmbPath.getSelectedIndex();            
            if(Login.logininformation[selectedIndex])
            {
                if(Login.logininformation[selectedIndex].username)
                {
                    $("t_username").value = Login.logininformation[selectedIndex].username;
                }
                
                if (Login.logininformation[selectedIndex].savepwd)
                {
                    $("t_savepwd").checked = true;
                    $("t_password").value = Login.logininformation[selectedIndex].password;
                }
                else
                {
                    $("t_savepwd").checked = false;
                    $("t_password").value = "";   
                }
            }
        }
    },
    // 获取用户历史登录记录
    GetHistoryInfo:function()
    {
        try
        {
            if(Cookie.CookieEnable())
            {                
                var paths = new Array();
                var values = new Array();
                var selectedIndex = 0;
                if(Cookie.GetCookie("logininformation") != null)
                {
                    this.logininformation = eval(Cookie.GetCookie("logininformation"));
                    if(Object.isArray(this.logininformation))
                    {
                        for(var i = 0;i < this.logininformation.length;i++)
                        {
                            if(this.logininformation[i].path)
                            {
                                paths.push(this.logininformation[i].path);
                                values.push(i);
                            }
                            
                            if(this.logininformation[i].selected == true)
                            {
                                selectedIndex = i;
                            }
                        }
                    }
                }            
                
                this.cmbPath.comBobox_initOptions(paths,values);
                this.cmbPath.setSelectedIndex(selectedIndex);
                
                if(this.logininformation[selectedIndex])
                {
                    if(this.logininformation[selectedIndex].username)
                    {
                        $("t_username").value = this.logininformation[selectedIndex].username;
                    }
                    
                    if (this.logininformation[selectedIndex].savepwd)
                    {
                        $("t_savepwd").checked = true;
                        $("t_password").value = this.logininformation[selectedIndex].password;
                    }                
                }
                
                //alert(this.logininformation[this.cmbPath.getOptionValue(0)].username);
                
//                if (Cookie.GetCookie("username") != null)
//                {
//                    $("t_username").value = Cookie.GetCookie("username");
//                }
//                
//                if (Cookie.GetCookie("savepwd") == "true" )
//                {
//                    $("t_savepwd").checked = true;
//                    $("t_password").value = (Cookie.GetCookie("password") != null ? Cookie.GetCookie("password") : "");
//                }                
            }
        }
        catch(e)
        {
            if(WebClient.debug)
            {
                alert("GetHistoryInfo:"+e.message+","+e.name);
            }
            throw(e);
            return false;
        }
        return true;
    },

    // 保存登录信息到cookie里

    SetLoginInfo:function()
    {
        try
        {
            if (Cookie.CookieEnable())
            {
//                var logininformation = new Array();
//                logininformation.push({path:"192.168.1.76:8888",username:"admin",password:""});
//                logininformation.push({path:"192.168.1.56:8888",username:"admin",password:"jishuzhichi"});
//                
//                //var tt = Object.toJSON(logininformation);       // 保存的文本
//                
//                Cookie.SetCookie("logininformation",Object.toJSON(logininformation));
                
                Cookie.SetCookie("username",$F("t_username"));
                if ($("t_savepwd").checked)
                {
                    Cookie.SetCookie("savepwd","true");
                    Cookie.SetCookie("password",$F("t_password"));                    
                }
                else
                {
                    //$("t_savepwd").checked = false;
                    Cookie.SetCookie("savepwd","false");
                    Cookie.SetCookie("password","");
                }
            }
        }
        catch(e)
        {
            if (WebClient.debug)
            {
                alert(e.message);
            }
            throw(e);
            return false;
        }
        return true;
    }
}