// Eportal WYSIWYG
// Last Update: Jan, 9th 2004

        var INCL_FOLDER_URL="/GEA-SOLUTIONS/GEA-ECM/WYSIWYG/js"
        var INCL_PATH_EPORTAL="/GEA-SOLUTIONS/GEA-ECM/"
        var FORMS_FOLDER_URL="/GEA-SOLUTIONS/GEA-ECM/WYSIWYG/"
        var encodeUTF8

function init(){
    // Read config File
    var xmlDom = new ActiveXObject("MSXML2.DOMDocument.3.0")
    xmlDom.async=false
    xmlDom.load("/GEA-SOLUTIONS/GEA-ECM/WYSIWYG/wysiwyg.xml");
    var root = xmlDom.documentElement
    for(var i=0;i<root.childNodes.length;i++){
        eval('var '+root.childNodes.item(i).nodeName+'= "'+root.childNodes.item(i).text+'";'); 
    }
    if( xmlDom.load(location.href.substr(0,location.href.lastIndexOf("/")+1)+"wysiwyg.xml")) {
        root = xmlDom.documentElement
        for(var i=0;i<root.childNodes.length;i++){
            eval(root.childNodes.item(i).nodeName+'= "'+root.childNodes.item(i).text+'";'); 
        }
    }

    // Look for editable objects
    var d = document.all
    var intElementCount = d.length
    for(var i=0;i<intElementCount;i++){
        var element = d[i]
        if(element.eportal=="edit") {
            var strType = element.typeEportal.toLowerCase()
            switch(strType) {
                case "image":
                    element.onmousedown=getImage //onmousedown para detectar botón del ratón
                    if(element.zoom && element.zoom.toLowerCase()=="true"){ //Evaluación en cortocircuito
                        element.onmouseover=doZoom
                        element.onmouseout=undoZoom
                    }
                    element.oncontextmenu=new Function("return false")
                break;
                case "text":
                    //element.onmousedown=getText
                    //element.onmousedown=contextMenuTextBox
                    element.oncontextmenu=contextMenuTextBox
                    element.contentEditable=true
                    element.onmouseover=new Function("this.previousbgcolor=this.style.backgroundColor;this.style.backgroundColor='"+textBgColorNode+"'")
                    element.onmouseout=new Function("this.style.backgroundColor=this.previousbgcolor")
                    element.onkeydown=new Function("this.setAttribute('updated','true',0);")
                    if (element.allowInnerHTML && element.allowInnerHTML=="false"){
                        element.onblur = setTexto
                        element.onbeforepaste = new Function("event.returnValue = false;")
                        element.onpaste = fnOnPaste
                    }
                break;
            }
            element.style.cursor="hand"
         }
     }
     window.defaultStatus="ePortal WYSIWYG ready."
}

var textBoxMenu = null, textBoxX = -1, textBoxY = -1, textBoxAbsX = -1, textBoxAbsY = -1, selRngOnTextBox = null;

function contextMenuTextBox() {
    var ev = event;
    var sdoc = document;
    var extraX = 0, extraY = 0;
    if (ev == null) {
        ev = idContent.event;
        sdoc = idContent.document;
        extraX = idContentContainer.offsetLeft;
        extraY = idContentContainer.offsetTop;
    }
    textBoxMenu = ev.srcElement;
    textBoxX = ev.x;
    textBoxY = ev.y;
    //textBoxAbsX = ev.clientX + sdoc.body.parentElement.scrollLeft;
    //textBoxAbsY = ev.clientY + sdoc.body.parentElement.scrollTop;
    textBoxAbsX = ev.clientX;
    textBoxAbsY = ev.clientY;
    textBoxX = ev.clientX + sdoc.body.parentElement.scrollLeft;
    textBoxY = ev.clientY + sdoc.body.parentElement.scrollTop;
    selRngOnTextBox = sdoc.selection.createRange();

    var obj = textBoxMenu;
    bIsImage = false;
    while ( (obj.getAttribute("typeEportal") == null) && (obj.tagName != "BODY") ) {
        if (obj.tagName == "IMG") {
            bIsImage = true;
            break;
        }
        obj = obj.parentNode;
    }
    var doc = obj;
    while (doc.tagName != undefined) doc = doc.parentNode;

    obj = textBoxMenu;
    var bIsLink = false;
    while (obj!= null) {
        if (obj.tagName == "A") {
            bIsLink = true;
            break;
        }
        obj = obj.parentNode;
    }
    if ( ( (selRngOnTextBox != null) && ( ( (selRngOnTextBox.text == null) && (selRngOnTextBox.length == 1) ) || (selRngOnTextBox.text.length > 0) ) ) || bIsLink) {
        wysiwygActionsInsFileRow.style.display = "none";
        wysiwygActionsLinkFileRow.style.display = "";
    } else {
        wysiwygActionsInsFileRow.style.display = "";
        wysiwygActionsLinkFileRow.style.display = "none";
    }

    if (bIsImage) {
        wysiwygActionsInsImageRow.style.display = "none";
        wysiwygActionsEditImageRow.style.display = "";
    } else {
        wysiwygActionsInsImageRow.style.display = "";
        wysiwygActionsEditImageRow.style.display = "none";
    }

    if (event != null) {
        wysiwygActionsEditHTMLRow.style.display = "";
    } else {
        wysiwygActionsEditHTMLRow.style.display = "none";
    }

    if ( (event != null) && editTpl) {
        wysiwygActionsChangeTemplateRow.style.display = "";
    } else {
        wysiwygActionsChangeTemplateRow.style.display = "none";
    }

    ev.cancelBubble = true;
//    if (event.button == 2) {
/*
        var offset_x = 0, offset_y = 0;
        for (var elem = textBoxMenu; elem.tagName != "BODY"; elem = elem.offsetParent) {
            offset_x += elem.offsetLeft;
            offset_y += elem.offsetTop;
            if (elem.currentStyle.overflowX == "auto") offset_x -= elem.scrollLeft;
            if (elem.currentStyle.overflowY == "auto") offset_y -= elem.scrollTop;
        }
        //moveLayer ('wysiwygActions', offset_x + event.offsetX - 10, offset_y + event.offsetY - 10);
        */
        var html = document.body.parentElement;
        if ( (html.scrollTop == 0 && document.body.scrollTop > 0) || (html.scrollLeft == 0 && document.body.scrollLeft > 0) )
            html = document.body;
        moveLayer ('wysiwygActions', ev.clientX + extraX + html.scrollLeft - 10, ev.clientY + extraY + html.scrollTop - 10);
        showLayer ('wysiwygActions',1);
//    }
    return false;
}

function getImage(){
    var obj = event.srcElement
    event.cancelBubble=true
    if(event.button==2){
        while (obj.getAttribute("typeEportal")==null){
            obj = obj.parentNode
        }
        if(obj.repository && obj.repository.toLowerCase()=="true"){
            var objResultado = showCenterWindow(IMAGE_PATH_EPORTAL,790,540,Array (obj, (obj.urlField != undefined) ) )
        } 
        /* else{
            var objResultado = showCenterWindow(FORMS_FOLDER_URL + "formImagen.asp",500,240,obj)
        }
        */
    }
    return false
}

function contextMenuGetText() {
    showLayer ("wysiwygActions", 0);
    obj = textBoxMenu;
    while (obj.getAttribute("typeEportal")==null){
        obj = obj.parentNode
    }
    document.editBlock = obj.innerHTML;
    document.editBlockStyle = "";
    for (stl in obj.currentStyle) {
        if ( ( (stl.length < 8) || (stl.toLowerCase().substr (0, 8) != "overflow") ) && 
             ( (stl.length < 7) || (stl.toLowerCase().substr (0, 7) != "display") ) && 
             (obj.currentStyle[stl] != '') ){
            _styledummy.style[stl] = obj.currentStyle[stl];
        }
    }
    document.editBlockStyle = _styledummy.style.cssText;
    textBoxMenu = obj;

    var izq = parseInt((screen.width-790)/2);
    var alt =parseInt((screen.height-590)/2);

    var edit = HTML_PATH_EPORTAL;
    if (edit.indexOf ('?') == -1) edit += '?';
    if (edit.indexOf ('?') != (edit.length - 1) ) edit+= '&';
    edit += 'style=';
    var ss = "";
    while (obj.tagName != null) obj = obj.parentNode;
    var iPos = obj.URL.lastIndexOf ('/');
    var baseUrl = obj.URL.substring (0, iPos + 1);
    for (var i = 0; i < obj.styleSheets.length; i++) {
        var sshref = obj.styleSheets[i].href;
        if (sshref.length > 0) {
            if ( (sshref.indexOf ("://") == -1) && (sshref.charAt (0) != '/') ) sshref = baseUrl + sshref;
            if (ss.length > 0) ss += ",";
            ss += escape (sshref);
        }
    }
    edit += ss;

    var w = open (edit, "_blank", "location=no,width=790,height=590,menubar=no,resizable=yes,status=yes,scrollbars=yes,screenX=" + izq + ",screenY=" + alt);
    return true;
}

function contextMenuGetTemplate() {
    showLayer ("wysiwygActions", 0);
    obj = textBoxMenu;
    while (obj.getAttribute("typeEportal")==null){
        obj = obj.parentNode
    }

    var izq = parseInt((screen.width-790)/2);
    var alt =parseInt((screen.height-590)/2);

    var seltpl = SELTPL_PATH_EPORTAL + "?iniframe=1";
    var w = open (seltpl, "_blank", "location=no,width=790,height=590,menubar=no,resizable=yes,status=yes,scrollbars=yes,screenX=" + izq + ",screenY=" + alt);
    return true;
}

function getTextSave() {
    textBoxMenu.innerHTML = document.editBlock;
    textBoxMenu.setAttribute('updated','true',0);
}

var _img_obj, _img_haslink, _img_window = null, bIsImage, bHasLink, _updatedSelection = null;

function updateImageSource (obj) {
    bIsImage = false;
    while ( (obj.getAttribute("typeEportal") == null) && (obj.tagName != "BODY") ) {
        if (obj.tagName == "IMG") {
            bIsImage = true;
            break;
        }
        obj = obj.parentNode
    }
    bHasLink = false;
    for (var pobj = textBoxMenu; pobj != null; pobj = pobj.parentNode) {
        if (pobj.tagName == "A") {
            bHasLink = true;
            break;
        }
    }

    _img_haslink = bHasLink;
    _img_obj = obj;
    return obj;
}

function contextMenuGetImage() {
    showLayer ("wysiwygActions", 0);

    var obj = textBoxMenu;
    _updatedSelection = null;
    openImageSelector (obj);
}

function openImageSelector(obj) {
    updateImageSource (obj);
    var mode = (bIsImage)? "editim": "text";
    var imgUrl = IMAGE_PATH_EPORTAL;
    if ( (imgUrl.indexOf ('?') > -1) && (imgUrl.charAt (imgUrl.length - 1) != '?') ) imgUrl += "&";
    if (imgUrl.indexOf ('?') == -1) imgUrl += "?";
    imgUrl += "mode=" + mode;

    if (_floating_images_app) {
        var imgCode = showCenterWindow(imgUrl, 790, 540, Array (obj, bHasLink) );
        moveLayer ("wysiwygActions", textBoxX, textBoxY + 200);
        if (!bIsImage) pasteImage (imgCode, obj);
    } else {
        _img_obj = obj;
        _img_haslink = bHasLink;
        if ( (_img_window == null) || (_img_window.closed) ) {
            var x = parseInt((screen.width-790)/2);
            var y = parseInt((screen.height-540)/2);
            _img_window = open (imgUrl, '_wb_imgselectwindow', 'width=790,height=540,directories=no,menu=no,resizable=yes,screenX=' + x + ',screenY=' + y + ',left=' + x + ',top=' + y);
        } else {
            _img_window.mode = mode;
            _img_window.imageInit();
        }
        //alert ("Antes focus");
        //_img_window.focus();
    }
}

/*
function updateImageTarget() {
    _updatedSelection = idContent.document.selection.createRange();
    var xx = _updatedSelection.parentElement();
    while (xx.location == undefined) xx = xx.parentElement();
    alert (xx.name);
}
*/
function pasteImage (imgCode, obj) {
    var rng;
    if (_updatedSelection != null) {
        rng = _updatedSelection;
    } else {
        var bod = obj;
        while (bod.tagName != "BODY") bod = bod.parentNode;
        rng = bod.createTextRange();
        rng.moveToPoint (textBoxAbsX, textBoxAbsY);
    }
    if (imgCode != null) {
        if (isHTMLMode) rng.text = imgCode.replace (location.protocol + "//" + location.host, "").replace (location.protocol + "//" + location.hostname, "");
        else {
            rng.pasteHTML (imgCode);
			var tagsWithUrl = new Array ("A", "IMG");
			var urlAttribs = new Array ("href", "src");
			for (var j = 0; j < tagsWithUrl.length; j++) {
    		    var links = obj.getElementsByTagName (tagsWithUrl[j]);
    		    for (var i = 0; i < links.length; i++) links[i][urlAttribs[j]] = links[i][urlAttribs[j]].replace (location.protocol + "//" + location.host, "").replace (location.protocol + "//" + location.hostname, "");
    		}
    	}
        var parentobj = obj;
        while ( (parentobj != null) && (parentobj.typeEportal != "text") ) parentobj = parentobj.parentNode;
        if (parentobj != null) parentobj.setAttribute ('updated', 'true', 0);
    }
}

function contextMenuGetFile() {
    showLayer ("wysiwygActions", 0);
    obj = textBoxMenu;

    var bHasLink = false;
    for (var pobj = textBoxMenu; pobj != null; pobj = pobj.parentNode) {
        if (pobj.tagName == "A") {
            bHasLink = true;
            break;
        }
    }
    var linkCode = showCenterWindow(FILE_PATH_EPORTAL, 790, 540, Array (obj, bHasLink, ((selRngOnTextBox.htmlText != undefined)&&(selRngOnTextBox.htmlText.length > 0))? selRngOnTextBox: null) );
    var doc;
    for (doc = textBoxMenu; doc.tagName != undefined; doc = doc.parentNode) ;
    

    moveLayer ("wysiwygActions", textBoxX, textBoxY + 200);

    if (linkCode != null) {
        var d = document.createElement ("div");
        if ( (selRngOnTextBox.htmlText != undefined) && (selRngOnTextBox.htmlText.length > 0) ) {
            if (isHTMLMode) selRngOnTextBox.text = linkCode.replace (location.protocol + "//" + location.host, "").replace (location.protocol + "//" + location.hostname, "");
            else selRngOnTextBox.pasteHTML (linkCode);
        } else if (selRngOnTextBox.htmlText == undefined) {
            d.innerHTML = linkCode;
            var link = doc.createElement ("a");
            link.href = d.children[0].href;
            selRngOnTextBox(0).applyElement (link, "outside");
            
        } else if (bHasLink) {
            d.innerHTML = linkCode;
            var lnk = d.children[0];
            pobj.href = lnk.href;
            pobj.innerHTML = lnk.innerHTML;
        } else {
            if ((selRngOnTextBox.text == undefined) || (selRngOnTextBox.text.length == 0) ){
                var bod = obj;
                while (bod.tagName != "BODY") bod = bod.parentNode;
                rng = bod.createTextRange();
                rng.moveToPoint (textBoxAbsX, textBoxAbsY);
                if (linkCode != null) {
                    if (isHTMLMode) rng.text = linkCode.replace (location.protocol + "//" + location.host, "").replace (location.protocol + "//" + location.hostname, "");
                    else {
                        rng.pasteHTML (linkCode);
            			var tagsWithUrl = new Array ("A", "IMG");
            			var urlAttribs = new Array ("href", "src");
            			for (var j = 0; j < tagsWithUrl.length; j++) {
                		    var links = obj.getElementsByTagName (tagsWithUrl[j]);
                		    for (var i = 0; i < links.length; i++) links[i][urlAttribs[j]] = links[i][urlAttribs[j]].replace (location.protocol + "//" + location.host, "").replace (location.protocol + "//" + location.hostname, "");
                		}
                	}
                }
            }
        }
        var parentobj = textBoxMenu;
        while ( (parentobj != null) && (parentobj.typeEportal != "text") ) parentobj = parentobj.parentNode;
        if (parentobj != null) parentobj.setAttribute ('updated', 'true', 0);
    }
}

function getText(){
    event.cancelBubble=true
    obj=event.srcElement
    if(event.button==2){
        while (obj.getAttribute("typeEportal")==null){
            obj = obj.parentNode
        }
        showCenterWindow(FORMS_FOLDER_URL + "formText.htm",790,590,obj)
    }
    return false
}

function fnOnPaste(){
    event.returnValue = false;
    var obj = event.srcElement
    obj.innerHTML = window.clipboardData.getData("Text")
}
         
        function setTexto(){
            event.srcElement.innerHTML = event.srcElement.innerText
        }    
        
        function doZoom(){
            var obj = event.srcElement
            var objStyle = obj.style
            var oCloneNode = obj.cloneNode(false);
            oCloneNode.id=obj.id + "cloned"
               obj.insertAdjacentElement("afterEnd",oCloneNode)

            var dtop=event.y-event.offsetY+document.body.scrollTop
            var dleft=event.x-event.offsetX+document.body.scrollLeft
            objStyle.zIndex='2000';
            objStyle.position='absolute'
            objStyle.zoom='1.5'
            objStyle.top=dtop-parseInt(((parseFloat(objStyle.zoom)-1)*obj.offsetHeight)/2)
            objStyle.left=dleft-parseInt(((parseFloat(objStyle.zoom)-1)*obj.offsetWidth)/2)
        }
    
         function undoZoom(){
            var objStyle = event.srcElement.style
            document.all[event.srcElement.id+"cloned"].removeNode()
            objStyle.zIndex='0';
            objStyle.position='static';
            objStyle.zoom='';
            objStyle.top='';
            objStyle.left=''
        }
        
function showCenterWindow(archivo,ancho,alto,args){
    var izq = parseInt((screen.width-ancho)/2)
    var alt =parseInt((screen.height-alto)/2)
    return showModalDialog(archivo,args,"dialogHeight:" + alto + "px;dialogWidth:" + ancho + "px;top:" + alt + ";left:" + izq + ";status:yes;help:no;edge:sunken;scroll:yes;resizable:yes")
}

function saveData(){
    var form = document.createElement("form")
    form.method="post"
    form.target="update"
    form.action=SAVE_PATH_EPORTAL

    var d = document.all
    var intElementCount = d.length
    var strNKCMA_IMAGESREL = ""
    var strNKCMA_CMATEXTS = ""
    var totImgs=0;
    var totTexts=0;
    for(var i=0;i<intElementCount;i++){
        var element = d[i]
        if(element.eportal=="edit" && element.updated){    
        var strType = element.typeEportal.toLowerCase()
        switch(strType) {
                        case "image":
                            
                            //Tabla de la imagen
                            var input=document.createElement("input")                                
                            input.name=''+totImgs+'_eportalImg_table'
                            input.type="hidden"
                            input.typeEportal="hidden"
                            input.value=element.table;
                            form.appendChild(input)
                            
                            //Campo ID en el registro
                            var input=document.createElement("input")                                
                            input.name=''+totImgs+'_eportalImg_idField'
                            input.type="hidden"
                            input.typeEportal="hidden"
                            input.value=element.idField;
                            form.appendChild(input)
                            
                            var input=document.createElement("input")                                
                            input.name=''+totImgs+'_eportalImg_idFieldValue'
                            input.type="hidden"
                            input.typeEportal="hidden"
                            input.value=element.idValue;
                            form.appendChild(input)

                            //Campo IDIMG en lel registro
                            var input=document.createElement("input")                                
                            input.name=''+totImgs+'_eportalImg_imgField'
                            input.type="hidden"
                            input.typeEportal="hidden"
                            input.value=element.imgField;
                            form.appendChild(input)
                            var input=document.createElement("input")                                
                            input.name=''+totImgs+'_eportalImg_imgFieldValue'
                            input.type="hidden"
                            input.typeEportal="hidden"
                            input.value=element.id_nkcma_cmaimages;
                            form.appendChild(input)
                            
                            //Campo TITLE DE LA IMAGE en lel registro
                            var input=document.createElement("input")                                
                            input.name=''+totImgs+'_eportalImg_titleField'
                            input.type="hidden"
                            input.typeEportal="hidden"
                            input.value=element.titleField;
                            form.appendChild(input)
                            
                            var input=document.createElement("input")                                
                            input.name=''+totImgs+'_eportalImg_titleFieldValue'
                            input.type="hidden"
                            input.typeEportal="hidden"
                            if(encodeUTF8){
                                input.value=encode_utf8(element.title);
                            } else {
                                input.value=element.title;
                            }
                            form.appendChild(input)            
                                            
                            if(element.url) {
                            //Campo TITLE DE LA IMAGE en lel registro
                            var input=document.createElement("input")                                
                            input.name=''+totImgs+'_eportalImg_urlField'
                            input.type="hidden"
                            input.typeEportal="hidden"
                            input.value=element.urlField;
                            form.appendChild(input)
                            var input=document.createElement("input")                                
                            input.name=''+totImgs+'_eportalImg_urlFieldValue'
                            input.type="hidden"
                            input.typeEportal="hidden"
                            input.value=element.url;
                            form.appendChild(input)
                            }

                            //Si se trata de una imagen del repositorio el valor que devolvemos
                            //es el id_nkcma_cmaimages que tiene que existir obligadoriamente, en 
                            //caso contrario devolvemos el source de la imagen.
                            /*
                            if(element.repository && element.repository.toLowerCase()=="true"){
                                input.value = element.id_nkcma_cmaimages
                                if(element.updated)
                                    strNKCMA_IMAGESREL+=element.id + "," //creamos una cadena con todos los NKCMA_IMAGESREL que habrá que actualizar
                            }else{
                                input.value=element.src
                            }                        
                            form.appendChild(input)

                            var input=document.createElement("input")                                
                            input.name=element.id + "_alt"
                            input.typeEportal="hidden"
                            input.value = element.title
                            form.appendChild(input)                            
                            */
                            totImgs++;
                            break;
                            


        case "text":
            //Build a new input field keeping the table value
            var input=document.createElement("input")                                
            input.name=''+totTexts+'_eportalText_table'
            input.type="hidden"
            input.typeEportal="hidden"
            input.value=element.table;
            form.appendChild(input)
                            
            //Build a new input field keeping the idField name
            var input=document.createElement("input")                                
            input.name=''+totTexts+'_eportalText_idField'
            input.type="hidden"
            input.typeEportal="hidden"
            input.value=element.idField;
            form.appendChild(input)
                            
            //Build a new input field keeping the idField value
            var input=document.createElement("input")                                
            input.name=''+totTexts+'_eportalText_idFieldValue'
            input.type="hidden"
            input.typeEportal="hidden"
            input.value=element.idValue;
            form.appendChild(input)

            //Build a new input field keeping the text field name
            var input=document.createElement("input")                                
            input.name=''+totTexts+'_eportalText_textField'
            input.type="hidden"
            input.typeEportal="hidden"
            input.value=element.textField;
            form.appendChild(input)
                        
            //Build a new input field keeping the text field value
            var input=document.createElement("input")                                
            input.name=''+totTexts+'_eportalText_textFieldValue'
            input.type="hidden"
            input.typeEportal="hidden"
            fixAbsoluteURLs (element, false);
            if(element.allowInnerHTML && element.allowInnerHTML.toLowerCase()=="true"){
                input.value=eportalCleanupHTML (element.innerHTML)
            } else {
                input.value=element.innerText //Por defecto sólo permitimos texto dentro de los campos typeEportal="text"
            }

            if(element.GTAG && element.GTAG.toLowerCase()=="true"){
                input.value="__GTAG_URL="+input.value+"_/GTAG_" //Por defecto no se pone GTAG
            }                            
            form.appendChild(input)
                        
            totTexts++;
            break;
        }

                    if(element.extra){
                        var aExtras = element.extra.split("|")
                        for(var i=0;i<aExtras.length;i++){
                            var variable
                            if(aExtras[i].indexOf("%")>0){
                                aElemento = aExtras[i].split("%")
                                variable = aElemento[1]
                            }else{
                                variable = aExtras[i]
                            }
                    
                            if(variable.indexOf(":")>=0){
                                aVariable = variable.split(":")
                                variable = aVariable[0]
                            }
                            var input=document.createElement("input")                                
                            input.name=element.id + "_" + variable
                            input.type="hidden"
                            input.typeEportal="hidden"
                            input.value=""
                            if(element[variable])
                                input.value=element.getAttribute(variable)
                            form.appendChild(input)
                        }
                    }//if(element.extra)

        }//if(element.typeEportal)            
    }//for            
             
            input=document.createElement("input")                                
            input.name="eportalImgs"
            input.type="hidden"
            input.typeEportal="hidden"
            input.value=""+totImgs
            form.appendChild(input)

            input=document.createElement("input")                                
            input.name="eportalTexts"
            input.type="hidden"
            input.typeEportal="hidden"
            input.value=""+totTexts
            form.appendChild(input)

            input=document.createElement("input")                                
            input.name="return_url"
            input.type="hidden"
            input.typeEportal="hidden"
            input.value=location.href
            form.appendChild(input)
            
            document.body.appendChild(form)
            nventana=window.open(FORMS_FOLDER_URL+'/waiting.' + _ext,'update','width=300,height=200,resizable=yes');
            form.submit()
            nventana.focus();

} 
        
        function encode_utf8(texto) {
          texto = texto.replace(/\r\n/g,"\n");
          var utftext = "";
          for(var n=0; n<texto.length; n++)
              {
              var c=texto.charCodeAt(n);
              if (c<128)
                  utftext += String.fromCharCode(c);
              else if((c>127) && (c<2048)) {
                  utftext += String.fromCharCode((c>>6)|192);
                  utftext += String.fromCharCode((c&63)|128);}
              else {
                  utftext += String.fromCharCode((c>>12)|224);
                  utftext += String.fromCharCode(((c>>6)&63)|128);
                  utftext += String.fromCharCode((c&63)|128);}
              }
              return utftext;
          }
      
        function defaultValues(){}
        var dv = new defaultValues()
        dv["textbgcolor"]="#DDDDDD"
        dv["encodeUTF8"]=false
        //window.onload=init

    function changeDate() {
        var monthDays = new Array (0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
        //var sDay = parent.document.fchange.day.value, iDay = sDay;
        var sDay = parent.document.fchange.ep_date.value.substring (0, 2), iDay = sDay;
        if (sDay.charAt (0)  == '0') iDay = iDay.substring (1);
        //var sMonth = parent.document.fchange.month.value, iMonth = sMonth;
        var sMonth = parent.document.fchange.ep_date.value.substring (3, 5), iMonth = sMonth;
        if (sMonth.charAt (0) == '0') iMonth = iMonth.substring (1);

        var iYear = parent.document.fchange.ep_date.value.substring (6);
        var iHour = parent.document.fchange.hour.value;
        var iMinute = parent.document.fchange.minute.value;

        var iLim = monthDays[iMonth];
        if ( (iMonth == 2) && ( (iYear % 4) == 0) ) iLim++;
        if (iDay > iLim) {
            alert ("Incorrect date");
            return false;
        }
        
        //var url = parent.document.fchange.urlfield.value;
        var url = String (document.location);
        if (url.indexOf ('?') == -1) url += '?';
        if (url.charAt (url.length - 1) != '?') url += "&";
        if (url.indexOf ("EPORTAL_CURRENT_DATE") != -1) url = url.replace (/(\&)?EPORTAL_CURRENT_DATE\=[0-9]{8}/, "");
        url += "EPORTAL_CURRENT_DATE=" + iYear + sMonth + sDay + iHour + iMinute;
        document.location = url;
    }

function calendar (calendarUrl) {
    var dt = parent.document.fchange.ep_date.value;
    var minY = 2003, maxY = 2007;
    var ret = showModalDialog (calendarUrl, Array (dt, minY, maxY), "dialogHeight: 220px; dialogWidth: 150px; resizable: no; scroll: no; status: no;");
    if (ret != null) parent.document.fchange.ep_date.value = ret;
}

function updateTemplate (val, txt) {
    obj = textBoxMenu;
    while (obj.getAttribute("typeEportal")==null){
        obj = obj.parentNode
    }
    if (confirm (tplMessage) ) {
        tplPlaceholder.startDownload (RDTPL_PATH_EPORTAL + "?idTpl=" + escape (val), onDownloadTemplate);
        //self.location = CHGTPL_PATH_EPORTAL + "?table=" + escape (obj.table) + "&field=" + escape (obj.textField) + "&idField=" + escape (obj.idField) + "&idValue=" + escape (obj.idValue) + "&idTpl=" + escape (val) + "&back=" + escape (self.location);
    }
}

function onDownloadTemplate (html) {
    obj = textBoxMenu;
    while (obj.getAttribute("typeEportal")==null){
        obj = obj.parentNode
    }
    obj.innerHTML = html;
    obj.setAttribute('updated','true',0)
}

function fixAbsoluteURLs (obj, isParent) {
    var dir = '';
    if (isParent) dir = parentUrlDir;
    else {
        dir = self.location.pathname;
        dir = dir.substring (0, dir.lastIndexOf ("/") + 1);
    }
    var idContentPath, idContentFullPath, idContentFile;
    if (document.all['idContent'] != undefined) {
        idContentPath = idContent.location.pathname;
        idContentFullPath = idContent.location.pathname + "?" + idContent.location.search;
        idContentFile = idContentPath.substring (idContentPath.lastIndexOf ("/") + 1);
    }

    var tagsWithUrl = new Array ("A", "IMG");
    var urlAttribs = new Array ("href", "src");
    for (var j = 0; j < tagsWithUrl.length; j++) {
        var links = obj.getElementsByTagName (tagsWithUrl[j]);
        for (var i = 0; i < links.length; i++) {
            var x = links[i][urlAttribs[j]].replace (location.protocol + "//" + location.host + FORMS_FOLDER_URL, "");
            x = x.replace (location.protocol + "//" + location.host + dir, "");
            x = x.replace (location.protocol + "//" + location.host, "");
            x = x.replace (location.protocol + "//" + location.hostname + FORMS_FOLDER_URL, "");
            x = x.replace (location.protocol + "//" + location.hostname + dir, "");
            x = x.replace (location.protocol + "//" + location.hostname, "");
            x = x.replace (location.pathname + "#", "#");
            x = x.replace (location.pathname + "?" + location.search + "#", "#");
            if (document.all['idContent'] != undefined) {
                x = x.replace (idContentPath + "#", "#");
                x = x.replace (idContentFullPath + "#", "#");
                x = x.replace (idContentFile + "#", "#");
            }
            links[i][urlAttribs[j]] = x;
        }
    }
}

function eportalCleanupHTML (html) {
    var TAG_OPEN = '<!-- BEGIN GeaWysiwygIgnore -->';
    var TAG_CLOSE = '<!-- END GeaWysiwygIgnore -->';
    do {
        var iposStart = html.indexOf (TAG_OPEN);
        var found = (iposStart > -1);
        if (found) {
            var iposEnd = html.indexOf (TAG_CLOSE);
            found = (iposEnd > -1);
        }
        if (found) {
            html = html.substring (0, iposStart) + html.substring (iposEnd + TAG_CLOSE.length);
        }
    } while (found);

    return html;
}