



/* === 04/02/2012 === */
function XFormErrors(form) {
	var errors =false;
	var errormsg = "";
	var ffocus = true;
	form.getFields().each(function(field){
		errors =field.getErrors();
		if (errors.length>0) {
			var fname = "";
			if (field.title) {
				fname=field.title;
				fname = fname.charAt(0).toUpperCase() + fname.slice(1);
			}
			for(e=0;e<errors.length;e++) {
				if (ffocus) {
					if (field.type=="tinymcefield") {
						var formDiv = "ext."+field.id.substring(4);
						var formName = field.id.substring(4,field.id.indexOf('@'));
						XTmceShow(formDiv);
						Ext.getCmp("XForm_"+formName).body.scrollTo("top",0,false);
						Ext.getCmp("XForm_"+formName).body.scrollTo("top",Ext.fly(formDiv+"_parent").getY(),false);
					}
					else Ext.get(field.inputId).dom.focus();
					
					ffocus=false;
				}
				if (fname!="") errormsg += '<br>• '+fname+' : '+errors[e].charAt(0).toLowerCase()+errors[e].slice(1);
				else errormsg += '<br>• '+errors[e];
			}
		}
	});
	if (errormsg!="") XError('<span style="color:#f00;"><b>Formulaire invalide</b></span><br>'+errormsg,'bl',400);
}


function XTmceShow(id) {
	tinymce.get(id).show();
	if (window.getSelection) {
	  if (window.getSelection().empty) {  // Chrome
		window.getSelection().empty();
	  } else if (window.getSelection().removeAllRanges) {  // Firefox
		window.getSelection().removeAllRanges();
	  }
	} else if (document.selection) {  // IE?
	  document.selection.empty();
	}
}
function XTmceBrowser() {
	
}



/* === 17/01/2012 === */
function XLogs(file,el) {
	Ext.Ajax.request({
		url: '/_config/__logs.php',
		params: {
			file:file
		},
		success: function(response){
			Ext.get(el).update(response.responseText);
		}
		,failure : function(response){
			
		}
	});
}


/* === 17/01/2012 === */
function XAlert(title,msg,icon) {
	
	if (icon=="error") icon = Ext.MessageBox.ERROR;
	else if (icon=="info") icon = Ext.MessageBox.INFO;
	else if (icon=="question") icon = Ext.MessageBox.QUESTION;
	else if (icon=="warning") icon = Ext.MessageBox.WARNING;
	else icon = Ext.MessageBox.WARNING;

	
	Ext.MessageBox.show({
	   title:title
	   ,msg:msg
	   ,buttons: Ext.MessageBox.OK
	   ,icon: icon
   });
}

/* === 17/01/2012 === */
function XGridDelete(grid,url) {
	
	if (url==undefined) url: '_delete.php';
	var selection = grid.getSelectionModel().getSelection();
	
	var store = grid.getStore();
	if (selection.length>0) {
		
		var confirmMsg = 'Effacer cette fiche ?';
		if (selection.length>1) confirmMsg = 'Effacer '+selection.length+' fiches ?';
		
		Ext.Msg.confirm('Confirmation', confirmMsg, function(btn){
		  if (btn == 'yes'){
			  
			var ids = "";
			for (var index in selection) {
				  selection[index].data.id
			} 
			
			Ext.Ajax.request({
				url: '_delete.php',
				params: {
					ids:ids
				},
				success: function(response){
					store.remove(selection);
				}
				,failure : function(response){
					
				}
			});
			
		  }
		});
	}
}

/* === 16/01/2012 === */
function XError(html,corner,width){
	if (!width) width=300;
	if (!corner) corner = 'tr';
	Ext.create('widget.uxNotification', {
		corner: corner
		,title:"Erreur"
		,iconCls: 'ux-notification-icon-error'
		,html: html
		,cls: 'ux-notification-light'
		,paddingX: 0
		,paddingY: 0
		,slideInDelay: 300
		,slideDownDelay: 300
		,autoDestroyDelay: 5000
		,slideInAnimation: 'easeIn'
		,slideDownAnimation: 'easeOut'
		,stickOnClick: true
		,stickWhileHover: true
	}).show();
}

/* === 15/01/2012 === */
function XPrint(elid){
	
	var mask = Ext.getBody().createChild({
		tag: 'div',
		cls: 'xprint-mask',
		html: '<div style="display:block; position:absolute; left:50%; top:50%; width:100px; margin-left:-50px; height:30px; margin-top:-15px; ">impression ...</div>'
	}); 

	var elbody = Ext.get(elid);
	if (Ext.get(elid+"-body")) elbody = Ext.get(el+"-body");
	var elparent = elbody.parent();
	
	Ext.getBody().addCls("xprint-body");
	elbody.addCls("xprint");
	elbody.appendTo(Ext.getBody());
	
	window.focus();
	window.print();
	
	Ext.Function.defer(function(){
		Ext.getBody().removeCls("xprint-body");
		elbody.removeCls("xprint");
		elbody.appendTo(elparent);
		mask.remove();
	}, 500);
	
}


function Xmsg(t,s,d,cntdiv){
	
	if (d=="undefined") d = 1000;
    var msgCt = Ext.get("msg-div");
	if(!msgCt){
		msgCt = Ext.core.DomHelper.insertFirst(document.body, {id:'msg-div'}, true);
	}
	var s = Ext.String.format.apply(String, Array.prototype.slice.call(arguments, 1));
	
	var content = '<div class="msg"><h3>' + t + '</h3><p>' + s + '</p><div id="msg"></div>';
	if (cntdiv) content += Ext.get(cntdiv).dom.innerHTML;
	content += '</div>';
	
	var m = Ext.core.DomHelper.append(msgCt, content, true);
	
	m.hide();
	m.slideIn('t').ghost("t", { delay: d, remove: true});

 
}



var ckeditors = new Array();

function KX_ckeditor(el,table,id){
	
	
	var formid = el.substring(4,el.indexOf('@'));
	var fieldid = el.substring(4);
	
	var textarea = new Ext.form.TextArea({
		id : "ext_"+fieldid
		,inputId: fieldid
		,hidden:true
		,value : Ext.get(el).dom.innerHTML
	 });
	Ext.getCmp("XForm_"+formid).add(textarea);
	
	ckeditors.push(el);
	
	var editor = CKEDITOR.replace(el,
		{ 
		customConfig : '/_config/ckeditor_config.js' 
		,filebrowserBrowseUrl : "/k4/ajax/__ckeditor_files_browser.php?table=pages&id=5ae4c25ff08d3ad7736afd4086a448aaae513e2e"
		,filebrowserImageBrowseUrl : "/k4/ajax/__ckeditor_files_browser.php?table=pages&id=5ae4c25ff08d3ad7736afd4086a448aaae513e2e&type=img"
		,filebrowserUploadUrl : "/k3/functions/file_upload.php?table=pages&id=5ae4c25ff08d3ad7736afd4086a448aaae513e2e"
		,filebrowserImageUploadUrl : "/k3/functions/file_upload.php?type=img&table=pages&id=5ae4c25ff08d3ad7736afd4086a448aaae513e2e"
		}
	);		
}






function Xcheckbox(box){
	
	var checkbox = Ext.get(box.id);
	var counter = Ext.getCmp("ext_"+box.id.substring(4,box.id.lastIndexOf("°"))+".count");
	
	var myclass = checkbox.getAttribute("className");
	
	var subclass = myclass;
	if (myclass.indexOf(" ")>0) subclass = myclass.substring(0,myclass.indexOf(" "))
	
	if (myclass == subclass) {
		checkbox.addCls(subclass+"-checked");
		counter.setValue(counter.getValue()+1);
		Ext.getCmp("ext_"+box.id.substring(4)).checked=true;
	}
	else {
		checkbox.removeCls(subclass+"-checked");
		counter.setValue(counter.getValue()-1);
		Ext.getCmp("ext_"+box.id.substring(4)).checked=false;
	}
	
}
function XcheckboxCount(basename){
	
	var num=1;
	var checks=0;
	var cb = false;
	while (cb = Ext.getCmp(basename+"°"+num)) {
		if (cb.checked) checks++;
		num++;
	}
	Ext.getCmp(basename+".count").setValue(checks);
}

function Xradio(box){
	
	var basename = box.id.substring(4,box.id.lastIndexOf("°"));
	var radio = Ext.get(box.id);
	
	if (radio.getAttribute("className") == 'xradio') {
		Ext.getCmp("ext_"+basename).setValue(radio.getAttribute("value"));
		var num=1;
		var radioimg = false;
		while (radioimg = Ext.fly("ext."+basename+"°"+num)) {
			radioimg.removeCls("xradio-checked");
			num++;
		}
		radio.addCls("xradio-checked");
	}
	
}

function Xhelp(page){
	
	var HelpWindow = new Ext.window.Window({
		id : "HelpWindow"
		,title: 'Aide'
		,maxWidth: 500
		,maxHeight: 400
		,iconCls:"icon-help"
		,bodyCls:"help-window"
		,bodyPadding : 5
		,modal:true
		,loader: {
			url: page
			,autoLoad : true
			,scripts:true
		}
		,autoScroll:true
		,autoShow:true
		,stateful:true
		,resized:0
		,listeners : {
			resize : {
					fn : function() { 
						
						this.resized++;
						if (this.resized==2) {
							var body = Ext.getBody();
							var pos  = this.getPosition();
							
							if (this.getWidth()>this.maxWidth) {
								
								this.setWidth(this.maxWidth);
								pos[0] = parseInt( (body.getWidth()/2) -(this.maxWidth/2) );
							}
							if (this.getHeight() > this.maxHeight) {
								this.setHeight(this.maxHeight);
								pos[1] = parseInt( (body.getHeight()/2) -(this.maxHeight/2));
							}
							this.setPagePosition(pos[0],pos[1],false);
							this.focus
						}
						
						
					}
			}
		}
	});
	HelpWindow.addManagedListener(Ext.getBody(),"keydown",function(e,html,obj){ e.preventDefault(); if (e.getKey()==13 || e.getKey()==27 || e.getKey()==32) HelpWindow.close(); e.stopEvent(); });
}

function XhelpText(text){
	
	var HelpWindow = new Ext.window.Window({
		id : "HelpWindow"
		,title: 'Aide'
		,layout:"fit"
		,maxHeight: 400
		,iconCls:"icon-help"
		,maxWidth: 300
		,bodyPadding : 10
		,modal:true
		,html:text
		,autoScroll:true
		,autoShow:true
		,stateful:false
		,listeners : {
			afterrender : {
					fn : function() { 
						var body = Ext.getBody();
						
						var pos  = this.getPosition();
						if (this.getWidth()>this.maxWidth) {
							this.setWidth(this.maxWidth);
							pos[0] = parseInt( (body.getWidth()/2) -(this.maxWidth/2) );
						}
						if (this.getHeight()>this.maxHeight) {
							this.setHeight(this.maxHeight);
							pos[1] = parseInt( (body.getHeight()/2) -(this.maxHeight/2) );
						}
						this.setPosition(pos[0],pos[1],false);
						this.focus();
					}
			}
		}
	});
	HelpWindow.addManagedListener(Ext.getBody(),"keydown",function(e,html,obj){ e.preventDefault(); if (e.getKey()==13 || e.getKey()==27 || e.getKey()==32) HelpWindow.close(); e.stopEvent(); });
}



function Xwin(url,width,height){
	
	var wid = SHA1(url);
	
	var HelpWindow = new Ext.window.Window({
		id : wid
		,title: ''
		,layout:"fit"
		
		,height: height
		,width: width
		,bodyPadding : 0
		,modal:true
		,html : '<iframe frameborder="0" width="100%" height="100%" allowtransparency="0" src="'+url+'"></iframe>'
		,autoScroll:false
		,autoShow:true
		,stateful:false
	});
	
}
