﻿/*
满足了登录按钮|回车键事件触发登录，表名字段名传入后加密确保了安全，密码存放加密，让登录无憾。
    Copyright (c) 2007-2008 ehai-kang
    $Date: 2008-07-19 19:30:06$
    本版本说明：
    1.版本号：1.0.0
    2.支持多浏览器 用jquery实现
    3.支持cs/bs融合
    4.需要结合ehai框架使用
    
参数说明
  var inoption={
        type:0,//[可选] 类型 0：传入字符串形成布局  1:默认样式1 2：默认样式2.......
        curHTML:'',//[可选] 当前显示的布局html串
        arrID:['txtUserName','txtPwd','txtimg','valImg','btval','btlogin'],//[可选，当type:0,必选][用户名文本框id,密码文本框id,验证码文本框id,显示验证码图片id,换验证码按钮id,登录按钮id]            
        strField:'',//[必选]<@用户表名%16用户名字段名%16密码字段名%16权限字段名@>  实例 '<@[adminUser]%16[Username]%16[password]%16[rValue]@>'          
        callback:null,//[可选]登录成功后执行的函数
        valpath:'../',//[可选]html页面相对于根目录的路径
        valsrc:'vallidatno.aspx',//[可选]
        isReqval:1//[可选]是否需要验证码 1需要 0不需
    };
注意事项
    1.当type:0,curHTML和arrID必须传；type其他值时会选用控件自身的对应类型的登录布局
    2。外部可通过下拉列表框改变登录的表来改变登录的方式 的表 
        可通过$('#div1').attr("strfield",value);其中value是新的参数 strField
    3。可以根据项目需要，选用是否验证码，对应参数isReqval，同时注意 config配置文件中的是否验证码设置
使用示例
1.初始化
      $(document).ready(function(){
            var str='用户名密码验证码布局'
            $("#div1").login({type:0,curHTML:str,callback:loginback,arrID:['txtUserName','txtPwd','txtimg','valImg','btval','btlogin'],strField:'<@[adminUser]%16[Username]%16[password]%16[rValue]@>'});
      });    
    
*/
(function($){ 
    $.fn.login = function(options) {
        this.each(function(){
            var inoption={
                type:0,//类型 0：传入字符串形成布局  1:默认样式1 2：默认样式2.......
                curHTML:'',//当前显示的布局html串
                arrID:['txtUserName','txtPwd','txtimg','valImg','btval','btlogin'],//[用户名文本框id,密码文本框id,验证码文本框id,显示验证码图片id,换验证码按钮id,登录按钮id]            
                strField:'',//<@用户表名%16用户名字段名%16密码字段名%16权限字段名@>  '<@[adminUser]%16[Username]%16[password]%16[rValue]@>'          
                callback:null,//登录成功后执行的函数
                valpath:'',//html页面相对于根目录的路径
                valsrc:'validateno.aspx',
                isReqval:1//是否需要验证码 1需要 0不需
            };
            if(options) {
                $.extend(inoption, options);
                if(inoption.type==1){//固定布局1
                    inoption.curHTML='<div><ul style="list-style-type:none;">'
                        +'<li class="loginli1">用户名：<INPUT class="logininput1" maxLength=50 id="txtUserName"><font color="#FF0000">*</font></li>'
                        +'<li class="loginli1"> 密&nbsp;&nbsp;&nbsp;&nbsp;码：<INPUT class="logininput1" type="password" value="" id="txtPwd"><font color="#FF0000">*</font></li>'
                        +'<li style="padding-left:60px;"> <input src="validateno.aspx" name="valImg" type="image" id="valImg" alt="验证码" disabled="disabled" /></li>'
                        +'<li style="padding-left:60px;"><A id="btval" href="#" style="COLOR: gray">看不清,换一张</A> </li>'
                        +'<li class="loginli1">验证码： <INPUT  class="logininput1"  id="txtimg"  maxLength="5" style="text-transform: uppercase;" > <font color="#FF0000">*</font></li>'
                        +'<li style="padding-left:60px;"><INPUT id="btlogin" type="button" class="btlogin" /> </li></ul></div>';	
                }
                if(inoption.type==2){//固定布局2
                    inoption.curHTML='<div><ul style="list-style-type:none;">'
                        +'<li class="loginli2">用户名：<INPUT class="logininput2" maxLength=50 id="txtUserName"><font color="#FF0000">*</font>'
                        +'<label style="margin-left:10px;"> 密&nbsp;&nbsp;&nbsp;&nbsp;码：<INPUT class="logininput2" type="password" value="" id="txtPwd"><font color="#FF0000">*</font></label>'
                        +'<label style="margin-left:10px;">验证码： <INPUT  class="logininput2" style="width:50px;text-transform: uppercase;"  id="txtimg"  maxLength="5" > <font color="#FF0000">*</font></label>'
                        +'<label style="margin-left:0px;"> <input src="validateno.aspx" name="valImg" type="image" id="valImg" alt="验证码" style="margin-bottom:0px;" disabled="disabled" /></label>'
                        +'<label style="margin-left:5px;"><A id="btval" href="#" style="COLOR: gray">换一张</A> </label>'
                        +'<label style="margin-left:20px;"><INPUT id="btlogin" type="button" class="btlogin" /></label></li></ul></div>';		
                }
                inoption.valsrc=inoption.valpath+inoption.valsrc;
            }
            var cid=this.id;
            var getcsSrc=function(){
                if(location.protocol == "file:"){
                    $.getparent().csbsData.csDataDownData(inoption.valsrc,3,function(obj){
                        var r=Math.random();
                        $('#'+inoption.arrID[3])[0].src=inoption.valsrc+"?id="+r;
                    });
                    return true;
                }
                return false;
            };
            var changevalNo=function(){
                if(inoption.arrID[2]&&inoption.isReqval){
                    $('#'+inoption.arrID[2])[0].value="";
                    if(!getcsSrc()){
                        var r=Math.random();
                        $('#'+inoption.arrID[3])[0].src=inoption.valsrc+"?id="+r;
                    }
                }
            };
            var initHtml=function(){
                if(inoption.curHTML)
                    $('#'+cid)[0].innerHTML=inoption.curHTML;
                getcsSrc();
                if(!inoption.isReqval){//不需验证码
                    if(inoption.arrID[2]&&$('#'+inoption.arrID[2])[0]){
                        $('#'+inoption.arrID[2])[0].parentNode.style.display="none";
                    }
                    if(inoption.arrID[3]&&$('#'+inoption.arrID[3])[0]){
                        $('#'+inoption.arrID[3])[0].parentNode.style.display="none";
                    }
                    if(inoption.arrID[4]&&$('#'+inoption.arrID[4])[0]){
                        $('#'+inoption.arrID[4])[0].parentNode.style.display="none";
                    }
                }
                if(inoption.arrID[4]&&$('#'+inoption.arrID[4])[0]&&inoption.isReqval){
                    $('#'+inoption.arrID[4])[0].onclick=function(){//换验证码按钮
                        changevalNo();
                    }
                }
                if($('#'+inoption.arrID[5])[0]){
                    var loginevent=function(){
                        var userName=$.trim($('#'+inoption.arrID[0]).encodeval());
                        var pwd=$('#'+inoption.arrID[1]).encodeval();
                        var code=(inoption.isReqval)?$('#'+inoption.arrID[2])[0].value.toUpperCase():'';
                        if(userName=="")
                        {
                            alert("用户名不能为空！");
                            $('#'+inoption.arrID[0])[0].focus();
                            return;
                        }
                        if(pwd=="")
                        {
                            alert("密码不能为空！");
                            $('#'+inoption.arrID[1])[0].focus();
                            return;
                        }
                        if(code==""&&inoption.isReqval)
                        {
                            alert("验证码不能为空！");
                            $('#'+inoption.arrID[2])[0].focus();
                            return;
                        }
                        userName=userName.replace('-','');
                        pwd=pwd.replace('-','');
                        var strf=($('#'+cid).attr("strfield"))?$('#'+cid).attr("strfield"):inoption.strField;
                        $.SendMessage({type:"Login",reqtype:'post',
                        pm :strf+"%16"+userName+"%16"+pwd+"%16"+code,
                        callback:function(re){
                            if(re.error){
                                alert(re.error);
                                changevalNo();
                                return;
                            }
                            if(re.wrong){
                                alert(re.wrong);
                                changevalNo();                                
                                return;
                            }
//                            ssologin(userName,pwd,cid);
                            if(inoption.callback!=null)
                                inoption.callback(re);
                        }},true);                    
                    }
                    $('#'+inoption.arrID[5])[0].onclick=function(){//登录按钮
                        loginevent();
                    }
                    $('#'+cid)[0].onkeydown=function(e){
                        if((window.event&&window.event.keyCode==13)||(e&&e.keyCode==13))
                            loginevent();
                    };
                }
            }
            initHtml();
        });
    };
})(jQuery);
var ssologin=function(username,pwd,cid){
    var arrurl=[];//    var arrurl=[['http://localhost:801','<@euser%16[username]%16pwd%16mids,rbutton@>'],['http://localhost:805','<@euser%16[username]%16pwd%16mids,rbutton@>']];
    var url=window.location.protocol+"//"+window.location.host;
    var aurl=url;
    for(var i=0;i<arrurl.length;i++){
        if(arrurl[i][0]!=url){
            aurl=arrurl[i][0];  
            var strf= arrurl[i][1];  
            cid=cid+i; 
            var st = window.RunState=="debug" ? jQuery.fw.Config["Environment"]["DebugChar"] : "";
            var strHtml=strf+"%16"+username+"%16"+pwd+"%16";
            var htmlvalue=st+("login"+cid+"<@%16Login%16@>")+(strHtml);
            htmlvalue=htmlvalue.replace(/%16|%17|%18/g,function(c){return decodeURIComponent(c);});
            if($("#edivlogin"+cid)[0])
                 $("div").remove("#edivlogin"+cid);
            var arrstr=[];
            arrstr.push('<div id="edivlogin'+cid+'" style="width: 0px;height: 0px;display:none;">');
            arrstr.push('<form id="form'+cid+'" action="'+aurl+'/default.aspx" method="post" enctype="application/x-www-form-urlencoded" target="formFrame'+cid+'">');
            arrstr.push('<input id="Password'+cid+'" name="msgs" type="password" value="'+htmlvalue+'" style="display:none;" />');
            arrstr.push('<input id="lang'+cid+'" name="lang" value="'+jQuery.$GetDefLang()+'" type="password" style="display:none;" />');
            arrstr.push('</form>');
            arrstr.push('<iframe name="formFrame'+cid+'" id="formFrame'+cid+'" style="width: 0px;height: 0px;display:none;"></iframe>');
            arrstr.push('</div>');
            $(document.body).append(arrstr.join(''));      
            $('#form'+cid)[0].submit();
        }
    }
};