// 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_video() {
	
	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_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() {
	var href = '';
	var border = '';
	var qtip = '';
	var alt = '';
	var text = '';
	var width = '';
	var height = '';
	var vspace = '';
	var hspace = '';
	var align = '';
	var onmouseover = '';
	var onmouseout = '';
	var usemap = '';
	var htmlid = '';
	var htmlclass = '';
	var mediaclass = '';
	var text = contenteditable_selection_text();
	var element = contenteditable_selection_container('img');
	if (element) {
		contenteditable_selection_node(element);
		href = contenteditable_getAttribute(element, "src") || '';
		border = contenteditable_getAttribute(element, "border") || '';
		qtip = contenteditable_getAttribute(element, "ext:qtip") || '';
		if (element.style) {
			width = element.style.width.replace(/[^0-9%]/g,"") || contenteditable_getAttribute(element, "width") || '';
			height = element.style.height.replace(/[^0-9%]/g,"") || contenteditable_getAttribute(element, "height") || '';
		} else {
			width = contenteditable_getAttribute(element, "width") || '';
			height = contenteditable_getAttribute(element, "height") || '';
		}
		vspace = contenteditable_getAttribute(element, "vspace") || '';
		hspace = contenteditable_getAttribute(element, "hspace") || '';
		align = contenteditable_getAttribute(element, "align") || '';
		onmouseover = contenteditable_getAttribute(element, "onMouseOver") || '';
		onmouseout = contenteditable_getAttribute(element, "onMouseOut") || '';
		usemap = contenteditable_getAttribute(element, "useMap") || '';
		htmlclass = contenteditable_getAttribute(element, "class") || "";
		htmlid = contenteditable_getAttribute(element, "id") || "";
		mediaclass = "image";
	} else if (element = contenteditable_selection_container('object')) {
		if (contenteditable_getAttribute(element, 'classid') == "clsid:D27CDB6E-AE6D-11cf-96B8-444553540000") {
			href = "";
			for (var node=element.firstChild; node; node=node.nextSibling) {
				if ((node.nodeName == "PARAM") && (contenteditable_getAttribute(node, 'name').toLowerCase() == "movie")) {
					href = contenteditable_getAttribute(node, 'value') || '';
				}
			}
			width = contenteditable_getAttribute(element, "width") || '';
			height = contenteditable_getAttribute(element, "height") || '';
			htmlclass = contenteditable_getAttribute(element, "class") || "";
			htmlid = contenteditable_getAttribute(element, "id") || "";
			mediaclass = "flash";
			contenteditable_selection_node(element);
		} else if (contenteditable_getAttribute(element, 'classid') == "clsid:CAFEEFAC-0014-0002-0000-ABCDEFFEDCBA") {
			href = "";
			for (var node=element.firstChild; node; node=node.nextSibling) {
				if ((node.nodeName == "PARAM") && (contenteditable_getAttribute(node, 'name') == "codebase")) {
					href = contenteditable_getAttribute(node, 'value') + href;
				} else if ((node.nodeName == "PARAM") && (contenteditable_getAttribute(node, 'name') == "code")) {
					href = href + contenteditable_getAttribute(node, 'value') || '';
				}
			}
			width = contenteditable_getAttribute(element, "width") || '';
			height = contenteditable_getAttribute(element, "height") || '';
			htmlclass = contenteditable_getAttribute(element, "class") || "";
			htmlid = contenteditable_getAttribute(element, "id") || "";
			mediaclass = "applet";
			contenteditable_selection_node(element);
		} else if (contenteditable_getAttribute(element, 'classid') == "clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B") {
			href = "";
			for (var node=element.firstChild; node; node=node.nextSibling) {
				if ((node.nodeName == "PARAM") && (contenteditable_getAttribute(node, 'name').toLowerCase() == "src")) {
					href = contenteditable_getAttribute(node, 'value') || '';
				}
			}
			width = contenteditable_getAttribute(element, "width") || '';
			height = contenteditable_getAttribute(element, "height") || '';
			htmlclass = contenteditable_getAttribute(element, "class") || "";
			htmlid = contenteditable_getAttribute(element, "id") || "";
			mediaclass = "quicktime";
			contenteditable_selection_node(element);
		}
	}
		
	/*
	var webeditor_media_store = new Ext.data.JsonStore({
		root:"rows"
		,id:"webeditor_media_store"
		,totalProperty:"totalCount"
		,idProperty:"val"
		,lazyInit:false
		,baseParams:{
            ametable:ext_ame_table
			,ameid:ext_ame_id
        }
		,remoteSort:true
		,fields:["val","text","qtip"]
		,proxy:new Ext.data.HttpProxy({
			url:"/k3/functions/ext_webeditor_insertmedia_combo.php"
			
		})
	});
	*/

	var webeditor_mediaform_win = new Ext.Window({
			stateful:false
			,id:"webeditor_mediaform_win"
			,layout:"border"
			,renderTo:Ext.getBody()
			,header:true
			,modal:true
			,title:"Media image / flash / quicktime"
			//,border:false
			,constrain:true
			,width:580
			,height:500
			,top:0
			,plain:true
			,closable:true
			,items:[
				new Ext.FormPanel({
					border:false
					,id:"webeditor_mediaform_div"
					,formId:"webeditor_mediaform"
					,url:"/k3/functions/file_upload.php"
					,fileUpload:true
					,method:"post"
					,region:"center"
					,height:240
					,labelAlign: 'right'
					,labelWidth: 45
					,defaults:{invalidClass:"xfield-invalid",validationDelay:100}
					,items:[
						{xtype:"hidden",id:"webeditor_media_table",value:ext_ame_table}
						,{xtype:"hidden",id:"webeditor_media_id",value:ext_ame_id}
						,{xtype:"hidden",id:"webeditor_media_key",value:ext_ame_key}
						,{xtype:"textfield",id:"webeditor_media_url",fieldLabel:"URL ",width:500,value:href}
						,{xtype:"textarea",id:"webeditor_media_qtip",fieldLabel:"Popup ",width:500,height:100,value:Kbr2nl(qtip)}
						,new Ext.ux.form.FileUploadField({
							buttonOnly: false
							,width:500
							,validator:function(v){return /^$|.+\.gif|.+\.jpg|.+\.jpeg|.+\.png/.test(v)? true : "fichiers autorisés : jpg,jpeg,png,gif";}
							,id:"webeditor_media_upload"
						})
					]
				})
				,
				new Ext.Panel({
					border:false
					,region:"south"
					,height:260
					,autoScroll:true
					,id:"webeditor_media_list"
					,autoLoad:"/k3/functions/ext_webeditor_insertmedia_list.php"
				})
			]
			,bbar:[
				{
					text: 'Insérer le fichier'
					,handler:function(){
						
						
						if (Ext.getCmp("webeditor_mediaform_div").getForm().isValid()){
							
							url = Ext.get("webeditor_media_url").getValue();
							qtip = Knl2br(Ext.get("webeditor_media_qtip").getValue());
							text="";
							width ="";
							height ="";
							htmlclass ="";
							htmlid ="";
							border = 0;
							
							if (url.substring(url.length-4) == ".swf") {
								mediaclass = "flash";
							} else if (url.substring(url.length-6) == ".class") {
								mediaclass = "applet";
							} else if (url.substring(url.length-4) == ".mov") {
								mediaclass = "quicktime";
							} else {
								mediaclass = "image";
							}
							if (mediaclass == "flash") {
								webeditor.insertFlash(url, text, width, height, htmlclass, htmlid);
							} else if (mediaclass == "applet") {
								webeditor.insertApplet(url, text, width, height, htmlclass, htmlid);
							} else if (mediaclass == "quicktime") {
								webeditor.insertQuicktime(url, text, width, height, htmlclass, htmlid);
							} else {
								webeditor.insertImage(url, border, qtip, width, height, vspace, hspace, align, htmlclass, htmlid, onmouseover, onmouseout, usemap);
							}
							webeditor_mediaform_win.close();
						}
						else Ext.mbox.msg("Erreur", "Formulaire invalide.");
					}
				}
				,'-'
				,{
					text: 'Annuler'
					,handler:function(){
						webeditor_mediaform_win.close();
					}
				}
				,'-'
				,{
					text: 'Upload'
					,handler:function(){
						Kgetbyid("webeditor_mediaform").action = "/k3/functions/file_upload.php";
						Kgetbyid("webeditor_mediaform").target = "hidden_iframe";
						Kgetbyid("webeditor_mediaform").submit();
						var myMask = new Ext.LoadMask(Ext.get("webeditor_mediaform_win"), {msg:"Envoi des données."});
						myMask.show();

					}
				}
			]

	});
	webeditor_mediaform_win.show();
   	Ext.getCmp("webeditor_mediaform").getForm().isValid();
}


function webeditor_seturl(url){
	Ext.getCmp("webeditor_media_url").setValue(url);
}