// JavaScript Document

function Keditorshow(form,field,innerhtml,width){
	
	webeditor_div = Kgetbyid(form+"_webeditor");
	container =  Ext.getCmp(form+"_container");
	Xform = Ext.getCmp("X"+form);
	field = Xform.get(field);
	field_value = innerhtml; //field.getValue();
	
	
	bodys = Kgetelementsbyclassname("x-panel-body x-panel-body-noheader x-panel-body-noborder","div");
	if (bodys.length>0){
		bodys[0].style.overflow="hidden";
		bodys[0].oldscroll = bodys[0].scrollTop;
		bodys[0].scrollTop = 0;
	}
	webeditor_div.style.height = container.getHeight()-106;
	webeditor_div.style.top = 0;
	
	
	if (width>0) {
		webeditor_div.style.width = width;
		webeditor_div.style.left = (container.getWidth()-width)/2;
	}
	webeditor_div.className = "webeditor-show";
	field_value=field_value.replace(/<script([^>]*)>.*?<\/script>/gi, '');
	WebEditorSetContent(field_value,"webeditor");
	if (field_value!=WebEditorGetContent("webeditor")) WebEditorSetContent(field_value,"webeditor");
	//WebEditorStylesheet("/k3/css/editor.css.php");

	Kgetbyid(form).editorfield=field.id;	

	
}
function Keditorhide(form){

	if(form) Keditorsave(form);
	else {
		for(i=0; i<document.forms.length; i++){
			Keditorsave(document.forms[i].id);
		}
	}

}
function Keditorsave(form){
	
	if (field = Kgetbyid(form).editorfield){
		
		preview = Kgetbyid(form+"_"+field+"_preview");
		Xform = Ext.getCmp("X"+form);
		field = Xform.get(field);
		newval = WebEditorGetContent("webeditor");
		field.setValue(newval);
		preview.innerHTML = newval;
		bodys = Kgetelementsbyclassname("x-panel-body x-panel-body-noheader x-panel-body-noborder","div");
		if (bodys.length>0){
			bodys[0].style.overflow="auto";
			bodys[0].scrollTop = bodys[0].oldscroll;
		}
		Kgetbyid(form+"_webeditor").className = "webeditor-hide";
		if (field.isValid()) preview.className = "webeditor-preview";
		else  preview.className = "webeditor-preview-invalid";
	}
}
function Keditorclose(form){

	Ext.MessageBox.buttonText.yes = "Oui";
	Ext.MessageBox.buttonText.no = "Non";
	Ext.Msg.show({
	   title:"Confirmation"
	   ,msg: "Confirmez vous la fermeture de l'éditeur ?<br>La saisie en cours ne sera pas sauvegardée."
	   ,buttons: Ext.Msg.YESNO
	   ,icon: Ext.MessageBox.QUESTION
	   ,fn: function(btn){
			if(btn=='yes') {
				bodys = Kgetelementsbyclassname("x-panel-body x-panel-body-noheader x-panel-body-noborder","div");
				bodys[0].style.overflow="auto";
				bodys[0].scrollTop = bodys[0].oldscroll;
				Kgetbyid(form+"_webeditor").className = "webeditor-hide";
			}
		}
	});	
	


}

function webeditor_custom_insertvideo() {
	
	Ext.MessageBox.show({
		title: 'Code',
		msg: 'Entrer le code :',
		width:300,
		buttons: Ext.MessageBox.OKCANCEL,
		multiline: true,
		fn: function(btn,text){
			if(btn='ok') {
				WebEditorPasteContent(text);
			}
		}
	});
   
}

function webeditor_custom_cfhgspecialcharacter() {
	
	XWinframe("/k3/webeditor/specialcharacter.html?editor=webeditor",500,500)
   
}


function webeditor_custom_mailto() {
	var text = '';
	var email = '';
	var subject = '';

	var text = contenteditable_selection_text();
	var element = contenteditable_selection_container('a');
	if (element) {
		contenteditable_selection_node(element);
		text = element.innerHTML;
		var href = contenteditable_getAttribute(element, "href") || '';
		if (href.match(new RegExp("^mailto:([^?]*)(.*)$", "gi"))) email = href.replace(/^mailto:([^?]*)(.*)$/gi, "$1") || '';
		if (href.match(new RegExp("^(.*)\\?subject=(.*)", "gi"))) subject = href.replace(/^(.*)\?subject=(.*)/gi, "$2") || '';
		if (typeof(decodeURI) == "function") {
			subject = decodeURI(subject);
		}
	} else if (contenteditable_selection_contains('a')) {
		return;
	}	

	var mailwin = new Ext.Window({
			stateful:false
			,layout:"anchor"
			,renderTo:Ext.getBody()
			,header:true
			,modal:true
			,title:"Email"
			,border:false
			,constrain:true
			,width:380
			,height:150
			,bodyBorder:false
			,plain:true
			,autoScroll:true
			,bodyStyle:"padding:8px;"
			,items:new Ext.FormPanel({
				border:false
				,id:"webeditor_mailform"
				,url:""
				,labelAlign: 'right'
       			,labelWidth: 40
				,defaults:{invalidClass:"xfield-invalid",validationDelay:100}
				,items:[
					{xtype:"textfield",id:"webeditor_email",fieldLabel:"Email ",vtype:"email",vtypeText:"Adresse email non valide",allowBlank:false,width:300,value:email}
					,{xtype:"textfield",id:"webeditor_sujet",fieldLabel:"Sujet ",width:300,value:subject}
				]
			})
			,bbar:[
				{
					text: 'OK'
					,handler:function(){
						if(Ext.getCmp("webeditor_mailform").getForm().isValid()){
							webeditor.insertMailto(Ext.get("webeditor_email").getValue(),Ext.get("webeditor_sujet").getValue(),"","");
							mailwin.close();
						}
						else Ext.mbox.msg("Erreur", "Formulaire invalide.");
					}
				}
				,'-'
				,{
					text: 'Annuler'
					,handler:function(){
						mailwin.close();
					}
				}
			]

	});
	mailwin.show();
   	Ext.getCmp("webeditor_mailform").getForm().isValid();
}

function webeditor_custom_insertmedia() {
	
	XWinframe("/k3/functions/webeditor_insertmedia.php?id="+ext_ame_id,500,500,'webeditor_insertmedia');
	
}


function webeditor_seturl(url){
	Ext.getCmp("webeditor_media_url").setValue(url);
}