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

var normalOption = window.getElementByName("Normal_Setting");
var transOption = window.getElementByName("Trans_Setting")
var netOption = window.getElementByName("Net_Setting");
var otherOption = window.getElementByName("Other_Setting");
var pageTab = window.getElementByName("switch");
var AutoLogin = window.getElementByName("Auto_Login_Setting");
var PopupOption = window.getElementByName("tip_Setting");
//var AtuoSkin = window.getElementByName("auto_skin_Setting");
//var AtuoDeleteSyncFile = window.getElementByName("auto_delete_syncfile");
var NewMailTips = window.getElementByName("new_mail_tips");

//var btnViewPath = window.getElementByName("view_path");
//var syncPath = window.getElementByName("Sync_path_setting");
//var unSyncPathSet = window.getElementByName("cancel_sync_path_setting")
var btnViewDownPath = window.getElementByName("view_down_path");
var downPath = window.getElementByName("path_down_setting");

var proxyCombo = window.getElementByName("Proxy_Set");
var serverAddEdit = window.getElementByName("ServerAdd");
var portEdit = window.getElementByName("Proxy_Port");
var usernameEdit = window.getElementByName("Proxy_UserName");
var pwdEdit = window.getElementByName("Proxy_PWD");

var lblServer = window.getElementByName("lbl_proxy_server");
var lblPort = window.getElementByName("lbl_proxy_port");
var lblUserName = window.getElementByName("lbl_proxy_username");
var lblPwd = window.getElementByName("lbl_proxy_pwd");

var pNo_Limit = window.getElementByName("No_Limit");
var pLimit = window.getElementByName("Limit");
var UploadSpeed = window.getElementByName("UploadSpeed");
var DownloadSpeed = window.getElementByName("DownloadSpeed");
var UploadNum = window.getElementByName("upload_num");
var DownloadNum = window.getElementByName("download_num");
var pCalcMD5 = window.getElementByName("MD5");
var downloadspeedlbl = window.getElementByName("DownloadSpeedlbl");
var uploadspeedlbl = window.getElementByName("UploadSpeedlbl");


var kblbl1 = window.getElementByName("KbLabel1"); 
var kblbl2 = window.getElementByName("KbLabel2");

//var sync_label = window.getElementByName("sync_label");
//var sync_label1 = window.getElementByName("sync_label1");
var caution_text = window.getElementByName("caution_text");
var btn_ok = window.getElementByName("BTN_OK");

var setMana = new ISettingManager();

function WndNotify(a, b) {
    if (b == "windowinit") {        
     pageTab.selectItem(0);
     DoInit();
     pageTab.selectItem(1);
     DoInit();
     pageTab.selectItem(2);
     
     DoInit();
     pageTab.selectItem(0);
    }
    else if ("selectchanged" == b) 
    {
        if (normalOption.getName() == a) {
          var bSelected = normalOption.isSelected();
          if (bSelected == true) {
            pageTab.selectItem(0);
//            DoInit();
          }
        }    
        else if(transOption.getName() == a)
        {
            var bSelected = transOption.isSelected();
            if(bSelected == true)
            {
                pageTab.selectItem(1);
            }
        }
        else if (netOption.getName() == a) {
            var bSelected = netOption.isSelected();
            if (bSelected == true) {
                pageTab.selectItem(2);
                //            DoInit();
            }
        }
        else if (otherOption.getName() == a) {
            var bSelected = otherOption.isSelected();
            if (bSelected == true) {
                pageTab.selectItem(3);
            }
        }
        else if (pNo_Limit.getName() == a) {
            var bSelected = pNo_Limit.isSelected();
            if (bSelected == true) {
                downloadspeedlbl.setEnabled(false);
                uploadspeedlbl.setEnabled(false);
                kblbl1.setEnabled(false);
                kblbl2.setEnabled(false);
                DownloadSpeed.setText("128");
                UploadSpeed.setText("");
            }
        }
        else if (pLimit.getName() == a) {
            var bSelected = pLimit.isSelected();
            if (bSelected == true) {
                downloadspeedlbl.setEnabled(true);
                uploadspeedlbl.setEnabled(true); 
                kblbl1.setEnabled(true);
                kblbl2.setEnabled(true);

                if (DownloadSpeed.getText() == "") {
                    DownloadSpeed.setText("128");
                }

                if (UploadSpeed.getText() == "") {
                    UploadSpeed.setText("128");
                }

            }
        }

    }
    else if (b == "itemselect") {
        if (a == proxyCombo.getName()) {
            var strText = proxyCombo.getText(); 
           if (strText == "不使用代理" || strText == "IE代理") {
                serverAddEdit.setEnabled(false);
                portEdit.setEnabled(false);
                usernameEdit.setEnabled(false);
                pwdEdit.setEnabled(false);

                lblServer.setEnabled(false);
                lblPort.setEnabled(false);
                lblUserName.setEnabled(false);
                lblPwd.setEnabled(false);

            }
            else
            {
                serverAddEdit.setEnabled(true);
                portEdit.setEnabled(true);
                usernameEdit.setEnabled(true);
                pwdEdit.setEnabled(true); 
                lblServer.setEnabled(true);
                lblPort.setEnabled(true);
                lblUserName.setEnabled(true);
                lblPwd.setEnabled(true); 
            }
        }
   }
}

function DoInit() {
    var nSel = pageTab.getCurSel();
    if (nSel == 0) {        
    var bLogined = setMana.isLogined();
    //syncPath.setEnabled(bLogined);
    //unSyncPathSet.setVisible(bLogined);      
       //btnViewPath.setEnabled(bLogined);//www 2016.04.11
    var NormalMap = setMana.getElementByName("NormalSet");
    if (NormalMap == 0) {
        return;
    }

         
    
    //var bVDisk = NormalMap.getValue("bVDisk");
    //if (bVDisk == "1") {
    //    btnViewPath.setVisible(false);        
    //    syncPath.setEnabled(false);
    //    unSyncPathSet.setVisible(false);  
    //    sync_label.setVisible(false);
    //    sync_label1.setVisible(false);
    //    AtuoDeleteSyncFile.setVisible(false);
    //}//www 2016.04.11
     var strAutoLogin = NormalMap.getValue("bAutoLogin");
    if (strAutoLogin == "1") {
        AutoLogin.selected(true);
    }
    else
        AutoLogin.selected(false);

    var strPopup = NormalMap.getValue("bPopup");
    if (strPopup == "1") {
        PopupOption.selected(true);
    }
    else
        PopupOption.selected(false);
    
    //var strAutoSkin = NormalMap.getValue("bAutoSkin");
    //if (strAutoSkin == "1") {
    //    AtuoSkin.selected(true);
    //}
    //else
       //    AtuoSkin.selected(false);//www 2016.04.11
    
    //var strAutoDeleteSyncFile = NormalMap.getValue("bAutoDeleteSyncFile");
    //if (strAutoDeleteSyncFile == "1") {
    //    AtuoDeleteSyncFile.selected(true);
    //}
    //else
       //    AtuoDeleteSyncFile.selected(false);//www 2016.04.11
        
      //var strNewMailTips = NormalMap.getValue("bNewMailTips");
      //if (strNewMailTips == "1") {
      //  NewMailTips.selected(true);
      //}
      //else
      //  NewMailTips.selected(false);
    //if (bLogined) {
    //    var strSyncPath = NormalMap.getValue("SyncPath");
    //    if (strSyncPath == "") {
    //        //syncPath.setText("");
    //        syncPath.setEnabled(false);
    //        //unSyncPathSet.setVisible(false);  
    //        AtuoDeleteSyncFile.setEnabled(false);
    //        AtuoDeleteSyncFile.setToolTip("还没有设置同步盘");
    //    }
    //    else
    //        syncPath.setText(strSyncPath);
    //}
    //else
    //{
    //    syncPath.setText("");        
       //}//www 2016.04.11
     //var strDownPath = NormalMap.getValue("DownPath");
     //if (strDownPath == "") {
     //    //syncPath.setText("");
     //     downPath.setEnabled(false);
     // }
     // else
       //    downPath.setText(strDownPath);//www 2016.04.11        
   }
   else if (nSel == 1) {       
       var PTransMap = setMana.getElementByName("TransSet");
       if (0 == PTransMap) {
           return;
       }
              
       var bCalcMD5 = PTransMap.getValue("bCalcMD5");
       var sUploadNum = PTransMap.getValue("UploadNum");
       var sDownloadNum = PTransMap.getValue("DownloadNum");
       if (bCalcMD5 == "1")
           pCalcMD5.selected(true);
       else
           pCalcMD5.selected(false);

       if (sUploadNum == "1")
           UploadNum.selectItem(0);
       else if (sUploadNum == "2")
           UploadNum.selectItem(1);
       else if (sUploadNum == "3")
           UploadNum.selectItem(2);
       else if (sUploadNum == "4")
           UploadNum.selectItem(3);
       else if (sUploadNum == "5")
           UploadNum.selectItem(4);

       if (sDownloadNum == "1")
           DownloadNum.selectItem(0);
       else if (sDownloadNum == "2")
           DownloadNum.selectItem(1);
       else if (sDownloadNum == "3")
           DownloadNum.selectItem(2);
       else if (sDownloadNum == "4")
           DownloadNum.selectItem(3);
       else if (sDownloadNum == "5")
           DownloadNum.selectItem(4);             

       var bUploadLimit = PTransMap.getValue("bUploadLimit");       
       var bDownloadLimit = PTransMap.getValue("bDownloadLimit");
       if ((bDownloadLimit == "1") && (bUploadLimit == "1")) {
           pLimit.selected(true); 
           DownloadSpeed.setEnabled(true);
           UploadSpeed.setEnabled(true);

           downloadspeedlbl.setEnabled(true);
           uploadspeedlbl.setEnabled(true);


           kblbl1.setEnabled(true);
           kblbl2.setEnabled(true);
       }
       else {
           pNo_Limit.selected(true);
           DownloadSpeed.setEnabled(false);
           UploadSpeed.setEnabled(false);

           downloadspeedlbl.setEnabled(false);
           uploadspeedlbl.setEnabled(false);


           kblbl1.setEnabled(false);
           kblbl2.setEnabled(false);
       }       
       var sUploadSpeed = PTransMap.getValue("UploadSpeed");
       UploadSpeed.setText(sUploadSpeed);

       var sDownloadSpeed = PTransMap.getValue("DownloadSpeed");
       DownloadSpeed.setText(sDownloadSpeed);       
       var strDownPath = PTransMap.getValue("DownPath");
       if (strDownPath == "") {
           //syncPath.setText("");
           downPath.setEnabled(false);
       }
       else
       {
           downPath.setText(strDownPath);//www 2016.04.11
       }
       
   }
   else if (nSel == 2) {
       
       proxyCombo.selectItem(0);
       var ProxyMap = setMana.getElementByName("ProxySet");
       if (ProxyMap == 0) {
           return;
       }
       var sPort = ProxyMap.getValue("ProxyPort");
       if ("0" != sPort)
       {
           portEdit.setText(sPort);
       }

       var sHost = ProxyMap.getValue("ProxyHost");
       serverAddEdit.setText(sHost);

       var sUserName = ProxyMap.getValue("ProxyUserName");
       usernameEdit.setText(sUserName);

       var sPwd = ProxyMap.getValue("ProxyPassword");
       pwdEdit.setText(sPwd);

       var sproxyType = ProxyMap.getValue("ProxyType");
       var nType = parseInt(sproxyType, 10);
       if (nType == 4) {
           nType = nType - 1;
       }
       else if (nType == 8)
       {
           nType = nType - 4;
       }
       proxyCombo.selectItem(nType);
       if (nType == 0) {
           return;
       }
   }
}

function DoClickOK() {
//    if (pageTab.getCurSel() == 1) {
        var nType = proxyCombo.getCurSel();
        var sPort = portEdit.getText();
        var nPort = parseInt(sPort, 10);
        var sHost = serverAddEdit.getText();
        var sName = usernameEdit.getText();
        var sPwd =  pwdEdit.getText();
        setMana.doApplyProxySet(nType, nPort, sHost, sName, sPwd);
//    }    

       var bAuto = AutoLogin.isSelected();
       var bPopup = PopupOption.isSelected();
       //var bAutoSkin = AtuoSkin.isSelected();
       var bNewMailTips = NewMailTips.isSelected();
       //var bAutoDeleteSyncFile = AtuoDeleteSyncFile.isSelected();

       setMana.doApplyNormalSet(bAuto, bPopup, false, false, bNewMailTips, "", downPath.getText()); 
       
       var bUploadLimit;
       var bDownloadLimit;
       var bCalcMD5;
       if(pNo_Limit.isSelected())
       {
           bUploadLimit = false;
           bDownloadLimit = false;
       }
       else
       {
           bUploadLimit = true;
           bDownloadLimit = true;
       }
       if (UploadSpeed.getText() == "")
       {
           UploadSpeed.setText("128");
       }
       if (DownloadSpeed.getText() == "")
       {
           DownloadSpeed.setText("128");
       }
       if(pCalcMD5.isSelected())
           bCalcMD5 = true;
       else
           bCalcMD5 = false;
       setMana.doApplyTransSet(bUploadLimit, bDownloadLimit, UploadSpeed.getText(), DownloadSpeed.getText(), UploadNum.getText(), DownloadNum.getText(), bCalcMD5);
}

function OnClick(obj) {
    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() == "view_path") {
//        var strPath = syncPath.getText();
//        var sResPath = window.selectDir("请您选择同步盘路径", strPath, BIF_RETURNONLYFSDIRS |BIF_USENEWUI | BIF_UAHINT);
//        var bOpen = setMana.judgeFileExent(sResPath);
//        if (bOpen == true) 
//        {
//            var bIsAutoBackupPath = setMana.judgeEcloudPathConflict();
//            if (bIsAutoBackupPath == true) 
//            {
//                var nRetID = window.messageBox("该目录已经设置为自动备份目录、子目录或者父目录，请重新选择。", "天翼云盘", MB_YESNO);                
//            }
//            else
//            {
//                var bHasfile = setMana.hasFile(sResPath);
//                if (bHasfile == true) 
//                {
//                  var nRetID = window.messageBox("您选的文件夹下已经有文件了，将它们也上传到天翼云盘服务器吗？", "天翼云盘", MB_YESNO | MB_ICONQUESTION);
//                  if (nRetID == IDYES) 
//                  {
//                        syncPath.setText(sResPath);
//                  }
//                }
//                else
//                {
//                  syncPath.setText(sResPath);
//                }
//                syncPath.setEnabled(true); 
//                //unSyncPathSet.setVisible(true);     
//                btn_ok.setEnabled(true);
//                //AtuoDeleteSyncFile.setEnabled(true);
//                //AtuoDeleteSyncFile.setToolTip("");
//                caution_text.setVisible(false);
//            }
//        }
//        else
//        {
////            caution_text.setVisible(true);
//            var nRetID = window.messageBox("受到系统限制，无法同步该目录，请重新选择。", "天翼云盘", MB_YESNO);
////            syncPath.setText(sResPath);
////            btn_ok.setEnabled(false);
//        }    //www 2016.04.11
    }
    else if (obj.getName() == "view_down_path") {
        var strPath = downPath.getText();
        var sResPath = window.selectDir("请您设置下载路径", strPath, BIF_RETURNONLYFSDIRS |BIF_USENEWUI | BIF_UAHINT);
        var bOpen = setMana.judgeFileExent(sResPath);
        if (bOpen == true) 
        {
            if (!setMana.judgeEcloudPathConflict(sResPath) && !setMana.judgeSyncPathConflict0fDownload(sResPath))
            {
                downPath.setText(sResPath);
            }
            btn_ok.setEnabled(true);
        }
        else
        {
            var nRetID = window.messageBox("受到系统限制，文件无法下载到该目录，请重新选择。", "天翼云盘", MB_YESNO);
//            downPath.setText(sResPath);
//            btn_ok.setEnabled(false);
        }    

    }
    else if (obj.getName() == "BTN_CLEARCACHE") {
        obj.setEnabled(false);
        obj.setText("正在清理");
    }
    else if (obj.getName() == "BTN_OK") {
        DoClickOK();        
        window.closeWnd();  
    }
    else if (obj.getName() == "BTN_CANCEL") {
        window.closeWnd();
    }
}