//INICIALIZACIÓN DE RICH TEXT PAR1 UBICACIÓN DEL TEXT AREA, PAR2 TEXTO A TRAER DESDE EL INICIO
initRichtext('dscinptEsp', "Descripción del Evento");
function initRichtext(id, entry){
    // The trick to keep the editor in the sample quite small
    // unless user specified own height.
    CKEDITOR.config.height = '125';

    
	var wysiwygareaAvailable = isWysiwygareaAvailable(),
		isBBCodeBuiltIn = !!CKEDITOR.plugins.get('bbcode');

	var editorElement = CKEDITOR.document.getById(id);
	editorElement.setHtml(entry);
		// Depending on the wysiwygarea plugin availability initialize classic or inline editor.
	if ( wysiwygareaAvailable ) {
	    CKEDITOR.replace(id, {
            filebrowserUploadUrl: "/ckeditor_fileupload/ajaxfile.php?type=file",
            filebrowserImageUploadUrl: "/ckeditor_fileupload/ajaxfile.php?type=image",
        });
	} else {
		editorElement.setAttribute( 'contenteditable', 'true' );
		CKEDITOR.inline(id);
		// TODO we can consider displaying some info box that
		// without wysiwygarea the classic editor may not work.
	}
    function isWysiwygareaAvailable() {
		// If in development mode, then the wysiwygarea must be available.
		// Split REV into two strings so builder does not replace it :D.
		if ( CKEDITOR.revision == ( '%RE' + 'V%' ) ) {
			return true;
		}
		return !!CKEDITOR.plugins.get('wysiwygarea');
	}
}



/*
init();
function init(){
    //INICIAR CKEDITOR
    if (CKEDITOR.env.ie && CKEDITOR.env.version < 9)CKEDITOR.tools.enableHtml5Elements(document);
    
    eventid = getParameterByName('eventid');
    //Eliminar
    var delBtn = document.getElementById("delEvent");
    
    //EDITAR BOTONES
    var editBtn = document.getElementById("editBtnCnt");
    if(eventid !== ""){
        entryLoad();
        delBtn.parentElement.style.display = "";
        editBtn.classList.remove("disabledBtn");
        editBtn.classList.add("editBtnCnt");
    }else{
        blankForm();
        delBtn.parentElement.style.display = "none";
        editBtn.classList.remove("editBtnCnt");
        editBtn.classList.add("disabledBtn");
        
        //CREAR CAMPOS RICH TEXT
        initRichtext('dscinptEsp', "");
        initRichtext('dscinptIng', "");
        initRichtext('dscinptPor', "");
    }
    //Evento eliminar
    delBtn.addEventListener("click", function(){
        showPopup("popConfirm", "¿Está seguro que desea borrar este evento?");
    });
    document.getElementById("delContinue").addEventListener("click", function(){
         deleteEvent();
    });
    
    //EVENTO DE BACK Y RELLENADO DE TITULO
    document.getElementById("headtitle").innerHTML = "Información";
    document.getElementById("icbackbtn").addEventListener("click", function(){
        gotoPage("events"); 
    });
    
    //Eventos colores 
    document.getElementById("c1").addEventListener("input", function(e){
        this.parentElement.style.backgroundColor = e.target.value; 
    });
    document.getElementById("c2").addEventListener("input", function(e){
        this.parentElement.style.backgroundColor = e.target.value; 
    });
    
    //GUARDAR
    document.getElementById("saveFrm").addEventListener("click", function(){
        saveEvent(); 
    });
    
    //status
    /*document.getElementById("statusFrm").addEventListener("click", function(){
        if(this.classList.contains("active")){
            this.classList.remove("active");
            this.firstElementChild.classList.remove("balActive");
            this.parentElement.parentElement.setAttribute("style", "color:#AAAAAA");
        }else{
            this.classList.add("active");
            this.firstElementChild.classList.add("balActive");
            this.parentElement.parentElement.removeAttribute("style");
        } 
    })
    
    
    
    //Lenguajes box
    document.getElementById("espBtnLng").addEventListener("click", function(){
        document.getElementsByClassName("lngBtnSel")[0].classList.remove("lngBtnSel");
        this.classList.add("lngBtnSel");
        document.getElementById("nameLngCnt").style.marginLeft = "";
        document.getElementById("dscdetLngCnt").style.marginLeft = "";
    });
    document.getElementById("ingBtnLng").addEventListener("click", function(){
        document.getElementsByClassName("lngBtnSel")[0].classList.remove("lngBtnSel");
        this.classList.add("lngBtnSel");
        document.getElementById("nameLngCnt").style.marginLeft = "-100%";
        document.getElementById("dscdetLngCnt").style.marginLeft = "-100%";
    });
    document.getElementById("porBtnLng").addEventListener("click", function(){
        document.getElementsByClassName("lngBtnSel")[0].classList.remove("lngBtnSel");
        this.classList.add("lngBtnSel");
        document.getElementById("nameLngCnt").style.marginLeft = "-200%";
        document.getElementById("dscdetLngCnt").style.marginLeft = "-200%";
    });
    
    //EDITAR BOTONES
    document.getElementById("editBtnCnt").addEventListener("click", function(){
        if(this.classList.contains("disabledBtn")){
            showPopup("popMsg", "", "Es necesario guardar el evento para habilitar esta sección");
        }else{
            var parms = {}; 
            parms.eventid = eventid;
            gotoPage("editButtons","", parms);
        }
    });
}

function entryLoad(id){
    var sessionid = getParameterByName('sessionid');
    var parms = {};
    parms.sessionid = sessionid;
    parms.id = eventid;
    var url = "events/entry";
    var onsucces = function(rsp, status){
        switch(status){
            case 200:
                //CONTENIDO RELEVANTE
                console.log(rsp);
                entryForm(rsp);
            break;
            case 400:
                showPopup("popMsg", "", "Faltan campos obligatorios");
                console.log(rsp);
            break;
            case 401:
                console.log(rsp);
            break;
            case 500:
                console.log(rsp);
            break;
        }
    }
    callWS("GET", url, parms, onsucces);
}
function deleteEvent(){
    var sessionid = getParameterByName('sessionid');
    var parms = {};
    parms.sessionid = sessionid;
    parms.id = eventid;
    var url = "events/delete";
    var onsucces = function(rsp, status){
        switch(status){
            case 200:
                //CONTENIDO RELEVANTE
                console.log(rsp);
                gotoPage("events","", {});
            break;
            case 400:
                console.log(rsp);
            break;
            case 401:
                console.log(rsp);
            break;
            case 500:
                console.log(rsp);
            break;
        }
    }
    callWS("GET", url, parms, onsucces);
}
function saveEvent(){
    var parms = {};
    parms.sessionid = getParameterByName('sessionid');
    if(eventid !== ""){
        parms.id = eventid;
        parms.status = evstatus;
    }else{
        parms.id = 0;
        parms.status = 0;
    } 
    parms.defaultlanguage = "esp";
    /*
    if(document.getElementById("statusFrm").classList.contains("active")){
        parms.status = "1";
    }else{
        parms.status = "0";
    }
    //Comprobamos que tenga formato correcto
	var fecha_aux1 = document.getElementById("initdate").value.split("-");
 	var Fechaini = new Date(parseInt(fecha_aux1[0]),parseInt(fecha_aux1[1]-1),parseInt(fecha_aux1[2]));
 //Comprobamos que tenga formato correcto
	var fecha_aux = document.getElementById("enddate").value.split("-");
 	var Fechaend = new Date(parseInt(fecha_aux[0]),parseInt(fecha_aux[1]-1),parseInt(fecha_aux[2]));
 
    	
    Hoy = new Date();//Fecha actual del sistema
    if (Fechaini.getTime() < Hoy.getTime() || Fechaend.getTime()<Hoy.getTime()){
        showPopup("popMsg", "", "Las fechas no pueden ser anteriores a la fecha actual.");
        return;
    }
    if (Fechaend.getTime()<Fechaini.getTime()){
        showPopup("popMsg", "", "Las fechas de finalización no puede ser anterior a la de inicio.");
        return;
    }
    
    
    parms.datestart = document.getElementById("initdate").value;
    parms.dateend = document.getElementById("enddate").value;
    //parms.x = document.getElementById("contrysel").value;
    //parms.x = document.getElementById("citysel").value;&dsc_eng=&dsc_por=
    parms.name_esp =  document.getElementById("cngrsName").value;
    parms.name_eng =  document.getElementById("cngrsNameEng").value;
    parms.name_por =  document.getElementById("cngrsNamePor").value;
    if(parms.name_esp == "" && parms.name_eng == "" && parms.name_por == ""){
        showPopup("popMsg", "", "Es necesario el nombre del evento en al menos un idioma.");
        return;
    }
    parms.dsc_esp = document.getElementsByTagName("iFrame")[0].contentDocument.getElementsByTagName("body")[0].innerHTML;
    parms.dsc_eng = document.getElementsByTagName("iFrame")[1].contentDocument.getElementsByTagName("body")[0].innerHTML;
    parms.dsc_por = document.getElementsByTagName("iFrame")[2].contentDocument.getElementsByTagName("body")[0].innerHTML;
    parms.website = document.getElementById("shortDsc").value;
    parms.email = document.getElementById("mailCntct").value;
    parms.facebook = document.getElementById("fbFrm").value;
    parms.twitter = document.getElementById("twtrFrm").value;
    parms.instagram = document.getElementById("igFrm").value;
    parms.linkedin = document.getElementById("lnkdinFrm").value;
    parms.color1 = document.getElementById("c1").value.replace(/#/g, "");
    parms.color2 = document.getElementById("c2").value.replace(/#/g, "");
    var url = "events/update";
    var onsucces = function(rsp, status){
        switch(status){
            case 200:
                //CONTENIDO RELEVANTE
                console.log(rsp);
                gotoPage("events","", {});
            break;
            case 400:
                showPopup("popMsg", "", "Faltan campos obligatorios");
                console.log(rsp);
            break;
            case 401:
                console.log(rsp);
            break;
            case 500:
                console.log(rsp);
            break;
        }
    }
    callWS("GET", url, parms, onsucces);
}
function blankForm(){
    document.getElementById("initdate").value = "0000-00-00";
    document.getElementById("enddate").value = "0000-00-00";
    document.getElementById("cngrsName").value = "";
    document.getElementById("cngrsNameEng").value = "";
    document.getElementById("cngrsNamePor").value = "";
    document.getElementById("shortDsc").value = "";
    document.getElementById("mailCntct").value = "";
    document.getElementById("fbFrm").value = "";
    document.getElementById("twtrFrm").value = "";
    document.getElementById("igFrm").value = "";
    document.getElementById("lnkdinFrm").value = "";
    document.getElementById("c1").value = "#ffffff";
    document.getElementById("c2").value = "#ffffff";
    document.getElementById("dscinptEsp").value = "";
    document.getElementById("dscinptIng").value = "";
    document.getElementById("dscinptPor").value = "";
}
var evstatus = 0;
function entryForm(rsp){
    evstatus = rsp.entry.status.id;
    /*
    var status = document.getElementById("statusFrm");
    if(rsp.entry.status.id == 0){
        status.classList.remove("active");
        status.firstElementChild.classList.remove("balActive");
        status.parentElement.parentElement.setAttribute("style", "color:#AAAAAA");
    }else{
        status.classList.add("active");
        status.firstElementChild.classList.add("balActive");
        status.parentElement.parentElement.removeAttribute("style");
    } 
    console.log(rsp);
    document.getElementById("initdate").value = rsp.entry.datestart.date;
    document.getElementById("enddate").value = rsp.entry.dateend.date;
    document.getElementById("cngrsName").value = rsp.entry.name.esp;
    document.getElementById("cngrsNameEng").value = rsp.entry.name.eng;
    document.getElementById("cngrsNamePor").value = rsp.entry.name.por;
    document.getElementById("shortDsc").value = rsp.entry.website;
    document.getElementById("mailCntct").value = rsp.entry.email;
    document.getElementById("fbFrm").value = rsp.entry.facebook;
    document.getElementById("twtrFrm").value = rsp.entry.twitter;
    document.getElementById("igFrm").value = rsp.entry.instagram;
    document.getElementById("lnkdinFrm").value = rsp.entry.linkedin;
    document.getElementById("c1").value = "#"+rsp.entry.colors.first;
    document.getElementById("c1").parentElement.style.backgroundColor = "#"+rsp.entry.colors.first;
    document.getElementById("c2").value = "#"+rsp.entry.colors.second;
    document.getElementById("c2").parentElement.style.backgroundColor = "#"+rsp.entry.colors.second;
    //CREAR CAMPOS RICH TEXT
    initRichtext('dscinptEsp', rsp.entry.dsc.esp);
    initRichtext('dscinptIng', rsp.entry.dsc.eng);
    initRichtext('dscinptPor', rsp.entry.dsc.por);
}
/***utils***
function showPopup(id, tit, msg){
    document.getElementById("dscTxt").innerHTML = tit;
    document.getElementById("msgTxt").innerHTML = msg;
    document.getElementById("cnfrmTxt").innerHTML = tit;
    document.getElementsByClassName("popmsgCnt")[0].style.display = "block";
    var popups = document.getElementsByClassName("popupmsg");
    for(var i=0;i<popups.length;i++){
        popups[i].style.display = "";
    }
    var popup = document.getElementById(id);
    popup.style.display = "table";
    setTimeout(function(){
        document.getElementsByClassName("popmsgCnt")[0].style.opacity = "1";
    }, 400);
}

function closePopup(id){
    document.getElementsByClassName("popmsgCnt")[0].style.opacity = "";
    var popup = document.getElementById(id);
    setTimeout(function(){
        popup.style.display = "";
        document.getElementById("msgTxt").innerHTML = "";
        document.getElementById("dscTxt").innerHTML = "";
        document.getElementById("cnfrmTxt").innerHTML = "";
        document.getElementsByClassName("popmsgCnt")[0].style.display = "";
    }, 700);
}
*/


