function build_tips(tips)
{
  //if(document.compatMode == 'CSS1Compat') return ;
	for(var i=0;i<tips.length;i++)
		new Ext.ToolTip({
			target: tips[i].tgt,
			title: tips[i].title,
			width:200,
			anchor:'anchor',
			anchorToTarget:false,
			anchorPosition:'top',
			html: tips[i].msg
		});
}
Array.prototype.exists = function(search){
  for (var i=0; i<this.length; i++)
    if (this[i] == search) return true;
		
  return false;
}
Ext.form.Field.prototype.msgTarget = 'side';
function retour(){
		var controlBar=document.getElementById('controles2');
		var retour=document.getElementById('retour');
		controlBar.removeChild(retour);
		history.back();
}
function LoginPopup()
{
			var win;
			var form1=new Ext.FormPanel({
        labelWidth: 75, // label settings here cascade unless overridden
        bodyStyle:'padding:5px 5px 0',
        width: 350,
        defaults: {width: 230},
        defaultType: 'textfield',

        items: [{
                fieldLabel: "Nom d'utilisateur",
                name: 'login',
                allowBlank:false
            },{
                fieldLabel: 'Mot de passe',
                name: 'password',
				inputType:'password',
                allowBlank:false
            }],
        buttons: [{
            text: 'Ok',
            handler: 
                function(){
                form1.form.submit({
                    method: 'POST',
                    waitMsg: 'wait...', 
					url:'ajax.php',
					params:{action: "login"},
					standardSubmit:false,
                    success: function(f,a) {
                        win.hide();
						form1.form.reset();
                        Ext.MessageBox.alert('Connexion','Bonjour '+a.result.user_nom); 
						/*window.resizeTo(screen.width,screen.height);
						window.moveTo(0,0);*/
						window.location.reload();
                        }, 
                    failure: function() { 
						form1.form.reset();
                        Ext.MessageBox.alert('Connexion','Echec de connexion'); 
                        }
                })
                }
        },{
            text: 'Cancel',
            handler  : function(){
						form1.form.reset();
                        win.hide();
                  }
        }]

    });
		win = new Ext.Window({
				width:370,
				autoHeight:true,
				resizable:false,
				plain: true,
				animate:true,
				title: 'Se connecter',
				closeAction:'hide',
				modal:true,
			items : form1,
			y:200
    });
		win.show();
		return win;
} 
var tb=null;
var video="connexion";

var helpMenuConfig=
	{
		xtype:'button',
		text:"Aide",
		cls:"menu_tb",
		menu:{
		width:'250px',
		items :
		[
			{
				text:"Options du menu",
				handler:function(){
					video_help(video);
				}
			},{
				text:"Réaliser une vente avec Evimmo3d",
				handler:function(){
					video_help("vente1");
				}			
			}
		]}
	};
function update_user_menu(response)
{
	var dh = Ext.DomHelper;
	var user = Ext.decode(response.responseText);
	if(page=='projet'){
		var conn=Ext.fly('conn');
		if(user.user_id==0){
			var html="<a onclick='javascript:LoginPopup()'>Se connecter</a>";
			var connected=false;
			}
		else{
			var html=user.user_nom+' '+user.user_prenom+"&nbsp;&nbsp;&nbsp;<a onclick='javascript:deconnect()'>Déconnexion</a>";
			var connected=true;
		}	
		conn.update(html);
		if (parent.connected!=connected)
		{
			parent.connected=connected;
			frame.location.reload();
		}
	}
	else if(page=='client')
	{
		user_id=user.user_id;
		if(tb!=null)
			tb.destroy();
			
		if(user.user_id==0)
		{
			video="connexion";
			var westCmp=Ext.getCmp("westCmp");
			westCmp.setWidth(0);
			viewport.doLayout();
			var el=Ext.get('user');
			if(el!=null) el.remove();
			tb=new Ext.Toolbar({
				style:{
					background:'transparent url() no-repeat scroll 0 0',
					borderWidth:'0px',
					paddingTop:'1px'
				},
				items:[
					'->',helpMenuConfig,{
						text:'Se connecter',
						cls:'menu_tb',
						handler:LoginPopup
					}
				]
			});
		}
		else if(user.user_id!=0){
			video="menu";
			var westCmp=Ext.getCmp("westCmp");
			westCmp.setWidth(0);
			viewport.doLayout();
			Ext.DomHelper.append(document.body,{tag:'div',id:'user',html:'Espace de vente de '+user.user_prenom+' '+user.user_nom});
			tb=new Ext.Toolbar({
				style:{
					background:'transparent url() no-repeat scroll 0 0',
					borderWidth:'0px',
					paddingTop:'1px'
				},
				items:[{
						text:"Mon compte",
						cls:"menu_tb",
						handler:function(){clear_content();configCompte();}
					},'-',{
						text:"Espace de vente",
						cls:"menu_tb",
						handler:function(){clear_content();get_list_projets();}
					},
					'->',helpMenuConfig,
					{
						text:'Déconnexion',
						cls:"menu_tb",
						handler:function(){clear_content();get_list_projets();deconnect();}
					}
				]
			});
			var i=0;
			if(user.role.exists('admin')){
			tb.insert(i++,
				{
					xtype:'button',
					text:"Etat des ventes",
					cls:"menu_tb",
					handler:function(){clear_content();get_recap_projets();}
				});
			tb.insert(i++,'-');
			tb.insert(i++,{
					xtype:'button',
					text:"Forces de vente et allotement",
					cls:"menu_tb",
					menu:{
					width:'250px',
					id:'allot-menu',
					items :
					[
						{
							text:"Créer et éditer mes forces de vente",
							handler:function(){
								clear_content();
								edit_reseau(is_promoteur);
								}
						},{
							text:"Attribution de lots",
							handler:function(){
								clear_content();
								draw_lot_tree(is_promoteur);
								}
						}
					]}
				});
			if(is_promoteur)
			{
				/*Ext.getCmp('allot-menu').insert(2,{
					text:"Marges et récapitulatif",
					handler:function(){
						clear_content();
						draw_allot_grid(is_promoteur);
						}
				});*/
				tb.insert(i++,'-');
				tb.insert(i++,{
					xtype:'button',
					text:"Informations projets et lots",
					cls:"menu_tb",
					menu:{
					width:'150px',
					items :
					[	
						{
							text:"Fiche projet",
							handler:function(){
								clear_content();
								drawProjetForm(is_promoteur);
							}
						},
						{
							text:"Fiche lot",
							handler:function(){
								clear_content();
								drawLotForm();
							}
						},
						{
							text:"Gestion document",
							handler:function(){
								clear_content();
								doc_manager();
							}
						}
					]}
				});
			}
			tb.insert(i++,'-');
			tb.insert(i++,{
					text:"Saisie des ventes",
					cls:"menu_tb",
					menu:{
					width:'150px',
					items :
					[
						{
							text:"Les ventes",
							handler:function(){clear_content();get_recap_ventes();}
						},{
							text:"Les options en cours",
							disabled:true
						}/*,{
							text:"Les ventes saisies",
							handler:function(){clear_content();get_recap_ventes2();}
						}*/
					]}
				});
			tb.insert(i++,'-');
			}
		}
	tb.render("toolbar");
	get_list_projets();
	}
}
function set_connected()
{
	Ext.Ajax.request({
		url: 'ajax.php',
		params: { action: 'get_user' },
		success:update_user_menu
	});
}

function deconnect()
{
	Ext.Ajax.request({
   url: 'ajax.php',
   params: { action: 'logout' },
   success:function(){window.location.reload();}
	});
}

function video_help(video)
{
	window.open("help.php?video="+video,"video_help",config='height=530, width=660, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, directories=no, status=no')
}

