var buttons = window.getElementsByClassName("ButtonUI");
for (var i = 0; i < buttons.length; i++) {
    buttons[i].OnClick = "OnClick";
}

var UserCombo = window.getElementByName("UserCombo");
var UserComboSingle = window.getElementByName("UserComboSingle")
var PassWordEdit = window.getElementByName("PassWordEdit");
var AutoLogin = window.getElementByName("AtuoLogin");
var SavePwd = window.getElementByName("KeepPwd");
var errorICon = window.getElementByName("errorTips_icon");
var errorTips = window.getElementByName("errorTips");
var errorbtn = window.getElementByName("errorTips_btn");
var loginbtn = window.getElementByName("Login");

function ShowErrorTips(a, b, c) {
    if ( (c == 5) && (a == true))
    {
        errorTips.setText("b");
        errorTips.setVisible(true);
        errorbtn.setVisible(true);
        errorICon.setVisible(true);
        loginbtn.setEnabled(false);
    }
    else
    {
        errorbtn.setVisible(false);
        if (a == true) {
            errorTips.setText(b);
            errorTips.setVisible(true);
            errorICon.setVisible(true);
            loginbtn.setEnabled(false);
            
        }
        else 
        {
            errorTips.setText("");
            errorTips.setVisible(false);
            errorICon.setVisible(false);
        }
    }
}

function DeleteAllInit() {
    //   UserEdit.setText("");
   UserCombo.setVisible(false);
   UserComboSingle.setVisible(true);
   PassWordEdit.setText("");
   AutoLogin.selected(false);
   SavePwd.selected(false);
}

function GrayWnd(a) {
    if (a == true) {
    UserCombo.setKeyState(true);
    }
   else
   {
    UserCombo.setKeyState(false);
   }
}

var ILoginM = new ILoginWndManager();
var bFirst = false;

function WndNotify(a, b) {
    if (b == "itemselect") {
        if (a == UserCombo.getName()) {
//   	       ShowErrorTips(false, "", 0);
//            UserEdit.setText(UserCombo.getText());
            var strName = UserCombo.getText();
    	    DoLoginInit(strName);
        }
    }
    else if (b == "itemdelete") {
     if (a == UserCombo.getName()) {
//       ShowErrorTips(false, "", 0);
        window.setTimer("DoDeleteUser", 10, 10);
        
    }
    }
    else if (b == "windowinit") {
       AutoLogin.selected(false);
       SavePwd.selected(false);
       ShowErrorTips(false, "", 0);
        
        UserCombo.clickItem(0);
        var strName = UserCombo.getText();
    	DoLoginInit(strName);
    }
    else if (b == "textchanged") {   //用户输入新账号时
        if (a == UserCombo.getName()) {
//     ShowErrorTips(false, "", 0);
        var str = UserCombo.getText();
        if (str == "") {
           PassWordEdit.setText("");
           AutoLogin.selected(false);
           SavePwd.selected(false);
//           loginbtn.setEnabled(false);
           return;
        }
//        loginbtn.setEnabled(true);
        var UserMap = ILoginM.getElementByName(str);
        if (UserMap == 0) {
            PassWordEdit.setText("");
            AutoLogin.selected(false);
            SavePwd.selected(false);
            return;
         }           
         DoLoginInit(str);
        return;        
    }
    else if (a == PassWordEdit.getName()) {
//        ShowErrorTips(false, "", 0);
        var str = UserCombo.getText();
        if (str == "") {
            loginbtn.setEnabled(false);
        }
        else
        {
//            loginbtn.setEnabled(true);
        }
       }
    }
    else if ("selectchanged" == b) 
    {
//       ShowErrorTips(false, "", 0);
        if (AutoLogin.getName() == a) {
          var bSelected = AutoLogin.isSelected();
          if (bSelected == true) {
            SavePwd.selected(true);
          }
        }    
        else if (SavePwd.getName() == a) {
         var bSelected = SavePwd.isSelected();
         if (bSelected == false) {
            AutoLogin.selected(false);
         }
        }
    }
    else if ("return" == b) {   //回车键
        if (loginbtn.isEnabled()) {
            AutoLogin.setFocus();
            DoLogin();
        }
    }
}

function OnClick(obj) {
    if (obj.getName() == "Login") {
        var str;
        if (UserCombo.isVisible())
        {
            str = UserCombo.getText();
        }
        else
        {
            str = UserComboSingle.getText();
        }
        ILoginM.doLogin(str, PassWordEdit.getText(), AutoLogin.isSelected(), SavePwd.isSelected());
        GrayWnd(true);
    }
    else if (obj.getName() == "closebtn") {
        window.closeWnd();
    }
    else if (obj.getName() == "minbtn") {
        window.sendWndMessage(WM_SYSCOMMAND, SC_MINIMIZE, 0);
    }
    else if (obj.getName() == "maxbtn") {
        window.sendWndMessage(WM_SYSCOMMAND, SC_MAXIMIZE, 0);
    }
    else if (obj.getName() == "restorebtn") {
        window.sendWndMessage(WM_SYSCOMMAND, SC_RESTORE, 0);
    }
    else if (obj.getName() == "JoinIn") {
        var str = "http://e.189.cn/registerMobile.do?appKey=cloud";
        window.shellExecute("open", str, "", "", SW_SHOWNOACTIVATE);

    }
    else if (obj.getName() == "Retrieve") {
        var str = "http://e.189.cn/getPassword.do";
        window.shellExecute("open", str, "", "", SW_SHOWNOACTIVATE);
    }
}

function DoLoginInit(strName) {
    //if (UserCombo.getCount() == 0 ) {
    //    UserCombo.setVisible(false);
    //}
    //else 
    //{
    //    UserCombo.setVisible(true);    
    //}
    if (strName == "") {
        loginbtn.setEnabled(false);
//        UserEdit.setText("");
//        PassWordEdit.setText("");
//        AutoLogin.selected(true);
//        SavePwd.selected(true);
        return;
    }
//  loginbtn.setEnabled(true);
    var UserMap = ILoginM.getElementByName(strName);
    if (UserMap == 0) {
//        PassWordEdit.setText("");
        return;
    }
    var strAutoLogin = UserMap.getValue("bAutoLogin");
    if (strAutoLogin == "1") {
        AutoLogin.selected(true);
    }
    else
        AutoLogin.selected(false);

    var strSavePwd = UserMap.getValue("bSavePwd");
    if (strSavePwd == "1") {
        SavePwd.selected(true);
       PassWordEdit.setText(UserMap.getValue("PassWord"));
    }
    else
    {
        SavePwd.selected(false);
        PassWordEdit.setText("");    // 不记住账号密码时，切换账号，密码变为空
    }
}

function LoginComplete() {
    window.closeWnd();
}

function DoLogin() {
    if (loginbtn.isEnabled()) {
        GrayWnd(true);
        ILoginM.doLogin(UserCombo.getText(), PassWordEdit.getText(), AutoLogin.isSelected(), SavePwd.isSelected());
    }
}

function LoginFailed(pErrorText, nErrorType)
{
    GrayWnd(false);
    //window.messageBox(pErrorText, "登录出错", MB_OK);
    ShowErrorTips(true, pErrorText, nErrorType);
}

function DoDeleteUser() {

    var strName = UserCombo.getSelText();
    var enableDelete = ILoginM.enableDeleteUser(strName);
    if (enableDelete == 0) {  //不能删除 
        var nIndex = UserCombo.getCurSel();
        
            ILoginM.deleteLoginName(strName);
            UserCombo.removeAt(nIndex);
            var strName = UserCombo.getText();
            if (strName == "") {
                DeleteAllInit();
            }
            DoLoginInit(strName);
         
        return;
    }
     
    var UserMap = ILoginM.getElementByName(strName); 
    var strState = UserMap.getValue("BindState"); 
    var nIndex = UserCombo.getCurSel();
    var nState = parseInt(strState, 10);
    if (nState == BindState_Folder) {
    	var sTip = "是否删除账号(";
   	    sTip += strName;
   	    sTip += ")的登录记录?";
   	    var nRet = window.messageBox(sTip, "用户信息", MB_YESNO);
    	if(nRet == IDYES)
    	{   
    	    ILoginM.deleteUser(strName, DeleteUserAction_All);
    	    UserCombo.removeAt(nIndex);
            var strName = UserCombo.getText();
            if (strName == "") {
            DeleteAllInit();
            }
    	    DoLoginInit(strName);
    	}
    	else if (nRet == IDNO) {
//     	    ILoginM.deleteUser(strName, DeleteUserAction_UserInfo);
//        	    UserCombo.removeAt(nIndex);
//             var strName = UserCombo.getText();
//             if (strName == "") {
//             DeleteAllInit();
//             }
//     	    DoLoginInit(strName);
    	}

     }
    else if (nState == BindState_VDisk) {
    	var sTip = "是否删除";
    	sTip += strName;
    	sTip += "用户信息及其绑定的虚拟盘? 删除虚拟盘，未上传的文件将丢失，请先进行备份";
	    var nRet = window.messageBox(sTip, "用户以及虚拟盘", MB_OKCANCEL | MB_ICONWARNING);
		if(nRet == IDOK)
		{
		    ILoginM.deleteUser(strName, DeleteUserAction_All);
		    UserCombo.removeAt(nIndex);
            var strName = UserCombo.getText();
            if (strName == "") {
                DeleteAllInit();
            }
    	    DoLoginInit(strName);
		}
    }
    else
    {
    	var sTip = "是否删除(";
    	sTip += strName;
    	sTip += ")的登录记录?";
		if(IDOK == window.messageBox(sTip, "用户信息", MB_OKCANCEL))
		{
            ILoginM.deleteUser(strName, DeleteUserAction_UserInfo);
 		    UserCombo.removeAt(nIndex);
            var strName = UserCombo.getText();
            if (strName == "") {
                DeleteAllInit();
            }
    	    DoLoginInit(strName);
		}
    }
}