﻿/**
*
*  uno.xml.present.js 
*  by Hideki Yamamoto eu-gsm[+393453332248]
*  Version Alpha 0.0.3.0
*  This file is part of the uno project.
*  The user must agree with the terms specified in
*
*  ©Hideki Yamamoto, all rights reserved.
*
*  Licence       : Http://www.unodot.net/framework/current/uno.licence.txt
*  Documentation : Http://www.unodot.net/framework/current/uno.js.usage.txt
*  History       : Http://www.unodot.net/framework/current/uno.js.history.txt
*
**/

if(!uno.xml){alert('uno.xml.present.js requires uno.xml.js');}

////////////////////////////CROSSWBROWSER XML DATA ACCESS//////////////////////////
uno.xml.getattribute=function(node,attributename){for (var ai = 0 ; ai < node.attributes.length ; ai++){if (node.attributes[ai].name==attributename){return uno.xml.attributevalue(node.attributes[ai]);}}return false;};
uno.xml.attributevalue = function(attribute){if(attribute.value){return attribute.value;}if(attribute.nodeValue){return attribute.nodeValue;}return false};
uno.xml.nodetext = function(node){if(node.text){return node.text;}if(node.textContent){return node.textContent;}};
uno.xml.nodexml = function (node,removecdata){
    var test;
    if(removecdata){return uno.xml.tostringformat(node,'','<','>','','',' ',' ');}
    if(node.xml){test = node.xml;}
    else if(XMLSerializer){test = (new XMLSerializer()).serializeToString(node);}
    else{test = uno.xml.tostringformat(node,'','<','>','','');}
    return test;};
uno.xml.nodeinnerxml = function(node,removecdata){
    if(!node){return null;}
    var test = '';
    for (var i=0;i<node.childNodes.length;i++){
        test += uno.xml.nodexml(node.childNodes[i],removecdata);
    }return test;};
uno.xml.nodexmltohtml = function (node){
    var test;
    test = uno.xml.tostringformat(node,'','&lt;','&gt;','<br />','&nbsp;&nbsp;');
    test = test.replace(/à/g,"&agrave;").replace(/è/g,"&egrave;");
    test = test.replace(/ì/g,"&igrave;").replace(/ò/g,"&ograve;");
    test = test.replace(/ù/g,"&ugrave;");
    return test;
};
uno.xml.nodeinnerxmltohtml = function(node){
    var test = '';
    for (var i=0;i<node.childNodes.length;i++){
        test += uno.xml.tostringformat(node.childNodes[i],'','&lt;','&gt;','<br />','&nbsp;&nbsp;');
    }
    test = test.replace(/à/g,"&agrave;").replace(/è/g,"&egrave;");
    test = test.replace(/ì/g,"&igrave;").replace(/ò/g,"&ograve;");
    test = test.replace(/ù/g,"&ugrave;");
    return test;
};
uno.xml._tnodes = new Array();
uno.xml.presentok = function(req){
    try{var data = req.responseXML.documentElement;
        var ok = data.getElementsByTagName('ok')[0].childNodes[0].nodeValue;
        if (ok != 'ok'){
            var errs = data.getElementsByTagName('error');
	    for (var e = 0 ; e < errs.length ; e++){if (uno.dialog){uno.dialog.show('Error',errs[e].childNodes[0].nodeValue+'<br />Response :<br />'+req.responseText,'error');}else{alert(errs[e].childNodes[0].nodeValue);}}
        }}catch(ex){}
    try{if(UnTip){UnTip();}}catch(ex){}
    try{uno.xml.present(req);}catch(ex){if (uno.dialog){uno.dialog.show('Error',ex.message+'<br />Response :<br />'+req.responseText,'error');}else{alert(ex.message);}}};
uno.xml.present = function(req){uno.xml.transform(req.responseXML.documentElement);}
uno.xml.transform = function(node){
    var transformed = false;
    var docwrite = false;
    var funcname = false;
    var targetelm = false;
    var att = false;
    for (var ai = 0 ; ai < node.attributes.length ; ai++){
        att = node.attributes[ai];
        if(att.name == 'tfunction'){
           transformed=true;uno.xml._tfunction(node,att);
        }else if(att.name == 'tsetvalue'){
           transformed=true;uno.xml._tsetvalue(node,att);
        }else if(att.name == 'tupdate'){
           transformed=true;uno.xml._tupdate(node,att);
        }else if(att.name == 'tredirect'){
           transformed=true;uno.xml._tredirect(node,att);
        }else if(att.name == 'twrite'){
           transformed=true;uno.xml._twrite(node);
        }else if(att.name == 'teval'){
           transformed=true;uno.xml._teval(node);
        }else if(att.name == 'tcookie'){
           transformed=true;uno.xml._tcookie(node,att);
    }   }   
    if (transformed == false){
        for(var i = 0; i<node.childNodes.length; i++){
            if(node.childNodes[i].nodeType == 1){uno.xml.transform(node.childNodes[i]);
}   }   }   };
////////////////////////////////PREDEFINED TFUNCTIONS/////////////////////////////////////
uno.xml._tfunction = function(node,att){
    var idx = uno.xml._tnodes.length;
    uno.xml._tnodes[idx] = node;
    var code = uno.xml.attributevalue(att)+'(uno.xml._tnodes['+idx+'])';
    eval(code);
};
uno.xml._tupdate = function(node,att){$(uno.xml.attributevalue(att)).innerHTML = uno.xml.nodeinnerxml(node,true)};
uno.xml._tredirect = function(node,att){location.href = uno.xml.attributevalue(att);};    
uno.xml._tsetvalue = function(node,att){$(node.nodeName).value = uno.xml.attributevalue(att);};
uno.xml._twrite = function(node){document.write(uno.xml.nodeinnerxml(node));};
uno.xml._teval = function(node){var code = uno.xml.nodeinnerxml(node);eval(code);};
uno.xml._tcookie = function(node,att){
    if(!uno.cookie){alert('uno.xml.present.js requires uno.cookie.js when using predefind transform function "tcookie".');}
    uno.cookie.create(uno.xml.attributevalue(att),uno.xml.nodeinnerxml(node),1);
};
uno.xml._terasecookie = function(node,att){
    if(!uno.cookie){alert('uno.xml.present.js requires uno.cookie.js when using predefind transform function "tcookie".');}
    uno.cookie.erase(uno.xml.attributevalue(att));
}
//////////////////////////////////////////////////////////////////////////////////////////
uno.xml.tostringformat = function (node,tabulation,opentag,closetag,newline,tabincrement,beforecdata,aftercdata){
    var test = '';
    if (!beforecdata){beforecdata='<![CDATA[';}
    if (!aftercdata){aftercdata=']]>';}  
    if(node.nodeName=='#cdata-section'){
        return beforecdata + uno.xml.nodetext(node) + aftercdata + newline;}
    if(node.nodeName=='#comment'){
        return '<!--' + uno.xml.nodetext(node) + '-->' + newline;}
    if(node.nodeName=='#text'){
        return uno.xml.nodetext(node);}
    else {
        if (node.nodeName != '#document'){
        test = tabulation + opentag + node.nodeName;
        if (node.attributes){
        for (var ai = 0 ; ai < node.attributes.length ; ai++){
            test += ' ' + node.attributes[ai].name + '="' + uno.xml.attributevalue(node.attributes[ai])+'"';
        }}
        test += closetag + newline;}        
        if (node.hasChildNodes){      
        for(var i = 0; i<node.childNodes.length; i++){
            if (node.childNodes[i].nodeName=='#text'){
                var data = uno.xml.nodetext(node.childNodes[i]);
                data = data.replace(/\n/g,'');
                for (var axe = 0; axe < 1000 ; axe++){
                     if (data.indexOf(' ') == 0){data = data.replace(' ','');}
                    else if (data.indexOf('\n') == 0){data = data.replace('\n','');}
                    else{axe=2000;}
                }
                if(data != ''){
                test += tabulation + uno.xml.nodetext(node.childNodes[i]) + newline;}
            }else if(node.childNodes[i].nodeName=='#cdata-section'){
                test += tabulation + '  ' + beforecdata + uno.xml.nodetext(node.childNodes[i]) + aftercdata + newline;
            }else if(node.childNodes[i].nodeType == 1){
                if(tabincrement){
                    test += uno.xml.tostringformat(node.childNodes[i],tabulation+tabincrement,opentag,closetag,newline,tabincrement,beforecdata,aftercdata);
                }else{
                    test += uno.xml.tostringformat(node.childNodes[i],tabulation,opentag,closetag,newline,tabincrement,beforecdata,aftercdata);                
        }   }   }   }
        if (node.nodeName != '#document'){
            return test + tabulation + opentag + '/' + node.nodeName + closetag + newline;}
        else {return test + newline;}
    }   
};