

var GAuthInfo = null;
var GAuthDialog = null;
var GUserProfile = null;

function InitAccountDialog() {

   	var user_login, user_password;

	var doc = qx.ui.core.ClientDocument.getInstance();

	var left = parseInt((window.document.body.clientWidth-360)/2); //qx.html.Window.getInnerWidth(window);
	var top = parseInt((window.document.body.clientHeight-(200+50))/2); //qx.html.Window.getInnerHeigth(window);

	var auth_dialog = new qx.ui.window.Window(Dialog_Auth_Caption);
        with(auth_dialog)
	{
      		setResizeable(false);
	    	setMoveable(true);
        	setLeft(left);
			setTop(top);
			setWidth(360);
			setHeight(200);
			setModal(true);
	 		setShowMaximize();
	 		setShowMinimize();
	 		
	 		addEventListener("beforeAppear", function() {
	 		      var login = qx.io.local.CookieTransport.get("login");
	 		      if(login != "") login_field.setValue(login);
	 		      var password = qx.io.local.CookieTransport.get("password");
	 		      if(password != "") password_field.setValue(password);
                        });
	}
	doc.add(auth_dialog);

	//var fs = new qx.ui.groupbox.GroupBox(Group_Auth_Fields);
	var fs = new qx.ui.layout.CanvasLayout();
    with(fs)
    {
		setLeft(10);
		setTop(10);
		setRight(10);
		setBottom(10);
		setPadding(10);
		setBorder(new qx.renderer.border.Border(1, "solid", "#B0B0B0"));
	}
	auth_dialog.add(fs);

	var icon1 = new qx.ui.basic.Image(map_resource_map + "www.png"); //"icon/48/status/dialog-password.png");
    with(icon1) {
		setTop(0);
    	setLeft(0);
		setToolTip(new qx.ui.popup.ToolTip(";-)"));
	}
   	fs.add(icon1);

    login_field = new qx.ui.form.TextField(Atom_Auth_Login);
    with(login_field)
    {
       	setTop(20);
       	setRight(20);
       	setWidth(150);
		setHeight(20);
		setBorder(new qx.renderer.border.Border(1, "solid", "#B0B0B0"));
        addEventListener("focusin", function(e) {
                    if(this.getValue() == Atom_Auth_Login)
                              this.setValue("");
        });
	}
	fs.add(login_field);

	password_field = new qx.ui.form.PasswordField(Atom_Auth_Password);
    with(password_field)
    {
       	setTop(50);
       	setRight(20);
       	setWidth(150);
		setHeight(20);
		setBorder(new qx.renderer.border.Border(1, "solid", "#B0B0B0"));
        addEventListener("focusin", function(e) {
                    if(this.getValue() == Atom_Auth_Password)
                              this.setValue("");
        });
	}
	fs.add(password_field);

/*	var  a1 = new qx.ui.basic.Atom(Atom_Auth_Login);
    with(a1)
    {
       	setTop(20);
       	setRight(135);
       	//setWidth("35%");
       	setHorizontalChildrenAlign("right");
    }
	fs.add(a1);

	var  a2 = new qx.ui.basic.Atom(Atom_Auth_Password);
    with(a2)
    {
       	setTop(50);
       	setRight(135);
       	//setWidth("35%");
       	setHorizontalChildrenAlign("right");
    }
	fs.add(a2);
*/

	var btn1 = new qx.ui.form.Button(Label_Ok, "icon/16/actions/dialog-ok.png");
    with(btn1)
    {
		setHorizontalChildrenAlign("center");
    	setWidth(150);
		setHeight(28);
		setTop(85);
    	setRight(20);
    	addEventListener("execute", buttonExecute);
    }
	fs.add(btn1);

	function buttonExecute() {
               var ret;
               this.setEnabled(false)

               qx.io.local.CookieTransport.set("login", login_field.getValue());
               qx.io.local.CookieTransport.set("password", password_field.getValue());

               var params = new SOAPClientParameters();
               params.add("elmtLogin", login_field.getValue());
               params.add("elmtPassword", password_field.getValue());

               var user_auth = SheduleManager.add_task("terAuth", "mtdLogin", TF_RUN_ONCE);
               user_auth.setParams(params, false, User_Auth_Callback);

               function User_Auth_Callback(r) {
                        if(r.cplxResult.elmtResult == 0) {
                               GAuthInfo = r.cplxResult;
                               auth_dialog.close();

                               Switcher.switchSector(GAuthInfo.elmtAccessLevel);

                               ResetWidgets(RESET_MARKERS);

                               if(Switcher.currentSector > 1) {
                                      ResetWidgets(RESET_NULL);
                               }

                               if(Switcher.currentSector != Switcher.GUEST_SECTOR) {
                                       var soap_data = [];
                                       soap_data.elmtAccuid = GAuthInfo.elmtAccuid;
                                       GUserProfile = GetUserProfile(soap_data);
                                       Profile.fav_str = GUserProfile.elmtFavorite;
                                       Profile.map_servers = GUserProfile.elmtMapServer;
                                       GDebugPort.debug("Favorites: " + Profile.fav_str);
                                       GDebugPort.debug("Map servers: " + Profile.map_servers);
                               }

                               Favorites.reset();

                               if(Switcher.currentSector == Switcher.ADMIN_SECTOR) {

                               }

                        } else  alert(Login_Error); //alert(Login_Error + "\n\nERROR: mtdLogin | elmtResult = " + r.cplxResult.elmtResult);
               }

	       this.setEnabled(true);
        }

	return auth_dialog;
}
