// loadXmlData.js
// take TestData and Type from testas.html
// Use TestData to get test xml data.
// Use Type to choose test template data object
// Load/join test data to test template data object

console.log('> iliustraData.js is iniciated;');
var content_type = 'test';

var doId = "Test player";
var params = {};

var open = {};
open.id = 'ss_0_0';

//for website iframe test player
//var iFrame = new Test_iFrame_Params("main.xml");
//var iFrame2 = new Test_iFrame_Params("main.xml");

var iFrame = new Test_iFrame_Params("http://doplayers.incore.lt/tests/final/2/2.xml");
//var iFrame = new Test_iFrame_Params("2.xml");
//var iFrame2 = new Test_iFrame_Params("2.xml");
var iFrame2 = new Test_iFrame_Params("http://doplayers.incore.lt/tests/final/1/1.xml");

function Test_iFrame_Params(xmlPath) {
    console.log('> JSF Test_iFrame_Params()');
    var prmstr = window.location.search.substr(1);
    var prmarr = prmstr.split("&");
    //var params = {};
    var data = '';    

    if (prmarr.length > 1) {
        for (var i = 0; i < prmarr.length; i++) {
            var tmparr = prmarr[i].split("=");
            data = tmparr[1].replace(/%2F/g, "/");
            data = data.replace(/%3A/g, ":");
            data = data.replace(/%3F/g, "?");
            data = data.replace(/%3D/g, "=");
            data = data.replace(/%26/g, "&");
            params[tmparr[0]] = data;
        }
    }

    if (params['TestData'] != undefined) {
        this.testDataLink = params['TestData'];
        this.testType = params['Type'];
    } else {
        this.testDataLink = xmlPath;
    }

    //this.testDataLink = xmlPath; //params['TestData'];
    //this.testType = "1b"; //params['Type'];

    console.log("test data xml link: " + this.testDataLink); // šis parametras - duomenų xml
    //console.log("test type xml link: " + this.testType); // testo tipas

    // get test xml data as text to xml
    var xml = loadXMLDoc(this.testDataLink);

    // parse xml text to dom variable
    var dom = parseXml(xml);

    // convert dom to json
    var json = xml2json(dom, '\t');
    //console.log("test data json: " + json); 
    //console.log("myjson:", json)
    // convert xml test data json to js object
    this.testData = JSON.parse(json);

    console.log('testData object:');
    console.log(this.testData);
}

// convert xml test data json to js object
var testObj = iFrame.testData;
//var testType = iFrame.testData.data.type 
//var testType = iFrame.testData.tests.data[0].type; 
//console.log('Test type in xml :' + testType);

if (params['TestData'] != undefined) { //check if iliustra
    
    var doContent = {
        "section0": {
            "slide0": {
            }
        },
    }
    
    var tObj = getXmlData_Iliustra(iFrame.testData, iFrame.testType);
    // doObj = tObj;
    doContent = tObj;
    //console.log("iliustraOutput:", doObj);
} else {

    var doContent = {
        "section0": {
            "slide0": {
                "template": "1",
                "script": {
                    "name": "templateT0script",
                    "iniciated": false,
                    "description": "Pirmas testo kadras"
                },
                "background": ""
                        //"background":"<img src=\"content/1-1.jpg\" alt=\"\" ></img>"
            },
        }
    };

    //TEST1-----------------------------------------------------//
    var fullObj = {};
    fullObj = getXmlData(fullObj, iFrame.testData.tests.data);
    doContent["section1"] = fullObj; //for DO object
    doObj = doContent; //for player
    //console.log("iliustraOutput2:", doObj);
    //TEST1 END-------------------------------------------------//

    //TEST2------------------------------------------------------//
    var fullObj2 = {};
    fullObj2 = getXmlData(fullObj2, iFrame2.testData.tests.data);
    doContent["section2"] = fullObj2; //for DO object
    doObj = doContent; //for player
    //TEST2 END-------------------------------------------------//

    console.log('testing');
}

function test1cTemplate(testData, testType) {   
    if (params['TestData'] != undefined) {

        tData = testData.data;		// xml test data json in js object
        console.log("1c tData = ");
        console.log(tData);
        var tObj = {
            "section0": {
                "slide0": {
                    "template": "1c",
                    "background": "",
                    'script': {
                        "name": "template1cscript",
                        'iniciated': false,
                    },
                    "question": "",
                    'photo': '',
                    "items": {
                       /* "item_0": '',
                        "item_1": '',
                        "item_2": '',
                        "item_3": ''*/
                    },
                    'answer': [],
                    'answersCount': '',
                    "CorrectCaption": "Teisingas",
                    "CorrectText": "Atsakymas teisingas, šaunu!",
                    "FaultCaption": "Teisingai!",
                    "FaultText": "Šaugu! Atsakėte teisingai.",
                    "css": ""
                }
            }
        };

        var tO = tObj.section0.slide0;

    } else {

        tData = testData;//.data;		// xml test data json in js object
        console.log("1c tData = ");
        console.log(tData);
        var tObj = {
            "template": "1c",
            "background": "",
            'script': {
                "name": "template1cscript",
                'iniciated': false,
            },
            "question": "",
            'photo': '',
            "items": {
            /*  "item_0": '',
                "item_1": '',
                "item_2": '',
                "item_3": ''*/
            },
            'answer': [],
            'answersCount': '',
            "CorrectCaption": "Teisingas",
            "CorrectText": "Atsakymas teisingas, šaunu!",
            "FaultCaption": "Teisingai!",
            "FaultText": "Šaugu! Atsakėte teisingai.",
            "css": ""

        };

        var tO = tObj;//.section0.slide0;

    }

    console.log("tData item ");
    console.log(tData.Items.Item);
    var item = tData.Items.Item;

    var answersCount = 0;
   
    for (var i = 0; i < item.length; i++) {
        tO.items['item_' + i] = item[i]['@Picture'];

        if (item[i]['@CorrectAnswer'] == 'true') {
            answersCount++;
            tO.answer.push(1);
        }
        else {
            tO.answer.push(0);
        }
    }

    tO.answersCount = answersCount;

    ordinaryData(tO, tData);

    function ordinaryData(tO, tData) {
        tO.question = tData.Question['#cdata'];
        tO.background = tData.background;
        tO.FaultCaption = tData.FaultCaption;
        tO.FaultText = tData.FaultText;
        tO.CorrectCaption = tData.CorrectCaption;
        tO.CorrectText = tData.CorrectText;
    }

    console.log("iliustraData.js made ready tObj = ");
    console.log(tObj);
    return tObj;
}

function test1bTemplate(testData, testType) {
    if (params['TestData'] != undefined) {

        tData = testData.data;		// xml test data json in js object
        console.log("1b tData = ");
        console.log(tData);
        var tObj = {
            "section0": {
                "slide0": {
                    "template": "1b",
                    "background": "",
                    'script': {
                        "name": "template1bscript",
                        'iniciated': false,
                    },
                    "question": "",
                    'photo': '',
                    "items": {
                        "item_0": '',
                        "item_1": ''
                    },
                    'answer': [],
                    'answersCount': '',
                    "CorrectCaption": "Teisingas",
                    "CorrectText": "Atsakymas teisingas, šaunu!",
                    "FaultCaption": "Teisingai!",
                    "FaultText": "Šaugu! Atsakėte teisingai.",
                    "css": ""
                }
            }
        };

        var tO = tObj.section0.slide0;

    } else {

        //tData = testData.data;		// xml test data json in js object
        tData = testData;
        console.log("1b tData = ");
        console.log(tData);
        var tObj = {
            "template": "1b",
            "background": "",
            'script': {
                "name": "template1bscript",
                'iniciated': false,
            },
            "question": "",
            'photo': '',
            "items": {
                "item_0": '',
                "item_1": ''
            },
            'answer': [],
            'answersCount': '',
            "CorrectCaption": "Teisingas",
            "CorrectText": "Atsakymas teisingas, šaunu!",
            "FaultCaption": "Teisingai!",
            "FaultText": "Šaugu! Atsakėte teisingai.",
            "css": ""

        };

        var tO = tObj;//.section0.slide0;
    }

    console.log("tData item ");
    console.log(tData.Items.Item);
    var item = tData.Items.Item;

    var l = item.length;
    tO.items_count = l;
    console.log('Items in XML = ' + l);

    var answersCount = 0;

    for (var i = 0; i < l; i++) {
        tO.items['item_' + i] = item[i]['#cdata'];

        if (item[i]['@CorrectAnswer'] == 'true') {
            answersCount++;
            tO.answer.push(1);
        }
        else {
            tO.answer.push(0);
        }
    }

    tO.answersCount = answersCount;
    tO.photo = tData.Photo;

    ordinaryData(tO, tData);

    function ordinaryData(tO, tData) {
        tO.question = tData.Question['#cdata'];
        tO.background = tData.background;
        tO.FaultCaption = tData.FaultCaption;
        tO.FaultText = tData.FaultText;
        tO.CorrectCaption = tData.CorrectCaption;
        tO.CorrectText = tData.CorrectText;
    }

    console.log("iliustraData.js made ready tObj = ");
    console.log(tObj);
    return tObj;
}

function test1aTemplate(testData, testType) {

    if (params['TestData'] != undefined) {

        tData = testData.data;		// xml test data json in js object
        console.log("1a tData = ");
        console.log(tData);
        var tObj = {
            "section0": {
                "slide0": {
                    "template": "1a",
                    "background": "",
                    'script': {
                        "name": "template1ascript",
                        'iniciated': false,
                    },
                    "question": "",
                    "items": {
                        "item_0": '',
                        "item_1": ''
                    },
                    'answer': [],
                    'answersCount': '',
                    "CorrectCaption": "Teisingas",
                    "CorrectText": "Atsakymas teisingas, šaunu!",
                    "FaultCaption": "Teisingai!",
                    "FaultText": "Šaugu! Atsakėte teisingai.",
                    "css": ""
                }
            }
        };

        var tO = tObj.section0.slide0;

    } else {

        //tData = testData.data;		// xml test data json in js object
        tData = testData;
        console.log("1a tData = ");
        console.log(tData);
        var tObj = {
            "template": "1a",
            "background": "",
            'script': {
                "name": "template1ascript",
                'iniciated': false,
            },
            "question": "",
            "items": {
                "item_0": '',
                "item_1": ''
            },
            'answer': [],
            'answersCount': '',
            "CorrectCaption": "Teisingas",
            "CorrectText": "Atsakymas teisingas, šaunu!",
            "FaultCaption": "Teisingai!",
            "FaultText": "Šaugu! Atsakėte teisingai.",
            "css": ""

        };

        var tO = tObj;//.section0.slide0;

    }

    console.log("tData item ");
    console.log(tData.Items.Item);
    var item = tData.Items.Item;

    var l = item.length;
    tO.items_count = l;
    console.log('Items in XML = ' + l);

    var answersCount = 0;

    for (var i = 0; i < l; i++) {
        tO.items['item_' + i] = item[i]['#cdata'];

        if (item[i]['@CorrectAnswer'] == 'true') {
            answersCount++;
            tO.answer.push(1);
        }
        else {
            tO.answer.push(0);
        }
    }

    tO.answersCount = answersCount;

    ordinaryData(tO, tData);

    function ordinaryData(tO, tData) {
        tO.question = tData.Question['#cdata'];
        tO.background = tData.background;
        tO.FaultCaption = tData.FaultCaption;
        tO.FaultText = tData.FaultText;
        tO.CorrectCaption = tData.CorrectCaption;
        tO.CorrectText = tData.CorrectText;
    }

    console.log("iliustraData.js made ready tObj = ");
    console.log(tObj);
    return tObj;
}

function test11aTemplate(testData, testType) {

    if (params['TestData'] != undefined) {
        tData = testObj.data;		// xml test data json in js object
        console.log("11a tData = ");
        console.log(tData);
        var tObj = {
            "section0": {
                "slide0": {
                    "template": "11a",
                    "background": "",
                    'script': {
                        "name": "template11ascript",
                        'iniciated': false,
                    },
                    "question": "",
                    'items_count': '',
                    "items": {
                        "item_0": "",
                        "item_1": "",
                        "item_2": "",
                        "item_3": "",
                        "item_4": "",
                        "item_5": ""
                    },
                    'answer': [],
                    "CorrectCaption": "Teisingas",
                    "CorrectText": "Atsakymas teisingas, šaunu!",
                    "FaultCaption": "Teisingai!",
                    "FaultText": "Šaugu! Atsakėte teisingai.",
                    "css": ""
                }
            }
        };

        var tO = tObj.section0.slide0;

    } else {

        //tData = testObj.data;		// xml test data json in js object
        tData = testData;
        console.log("11a tData = ");
        console.log(tData);
        var tObj = {
            "template": "11a",
            "background": "",
            'script': {
                "name": "template11ascript",
                'iniciated': false,
            },
            "question": "",
            'items_count': '',
            "items": {
                "item_0": "",
                "item_1": "",
                "item_2": "",
                "item_3": "",
                "item_4": "",
                "item_5": ""
            },
            'answer': [],
            "CorrectCaption": "Teisingas",
            "CorrectText": "Atsakymas teisingas, šaunu!",
            "FaultCaption": "Teisingai!",
            "FaultText": "Šaugu! Atsakėte teisingai.",
            "css": ""

        };

        var tO = tObj;//.section0.slide0;
    }
    var Items = tData.Items.Item;
    var l = Items.length;
    tO.items_count = l;
    console.log('Items in XML = ' + l);
    for (var i = 0; i < l; i++) {
        tO.items['item_' + i] = Items[i]['#cdata'];
        tO.answer.push(i);
    }


    ordinaryData(tO, tData);

    function ordinaryData(tO, tData) {
        tO.question = tData.Question['#cdata'];
        tO.background = tData.Background;
        tO.FaultCaption = tData.FaultCaption;
        tO.FaultText = tData.FaultText;
        tO.CorrectCaption = tData.CorrectCaption;
        tO.CorrectText = tData.CorrectText;
    }

    console.log("iliustraData.js made ready tObj = ");
    console.log(tObj);
    return tObj;
}

function test2aTemplate(testData, testType) {	// construct test 2a-T2 template object from iliustra xml test data
    if (params['TestData'] != undefined) {

        var tData = testData.data;		// xml test data json in js object
        console.log("2a tData = ");
        console.log(tData);

        var tObj = {
            "section0": {
                "slide0": {
                    "template": "2a",
                    "background": "",
                    'script': {
                        "name": "template2ascript",
                        'iniciated': false,
                    },
                    "question": "",
                    "f_items": {}, // 2 types: image; text;
                    "m_items": {}, // 2 types: image; text;
                    "f_background_image": "img/test_f5.png",
                    "m_background_image": "img/test_m5.png",
                    "f_background_image_white": "img/test_f5_w.png",
                    "m_background_image_white": "img/test_m5_w.png",
                    "CorrectCaption": "Teisingas",
                    "CorrectText": "Atsakymas teisingas, šaunu!",
                    "FaultCaption": "",
                    "FaultText": "",
                    "css": ""
                }
            }
        };

        var tO = tObj.section0.slide0;

    } else {

        var tData = testData;//.data;		// xml test data json in js object
        console.log("2a tData = ");
        console.log(tData);

        var tObj = {
            "template": "2a",
            "background": "",
            'script': {
                "name": "template2ascript",
                'iniciated': false,
            },
            "question": "",
            "f_items": {}, // 2 types: image; text;
            "m_items": {}, // 2 types: image; text;
            "f_background_image": "img/test_f5.png",
            "m_background_image": "img/test_m5.png",
            "f_background_image_white": "img/test_f5_w.png",
            "m_background_image_white": "img/test_m5_w.png",
            "CorrectCaption": "Teisingas",
            "CorrectText": "Atsakymas teisingas, šaunu!",
            "FaultCaption": "",
            "FaultText": "",
            "css": ""

        };

        var tO = tObj;//.section0.slide0;

    }
    console.log(tO)

    var i = 0;

    tData.Items.Item.forEach(function(entry) {
        tO.f_items['f_item_' + i] = entry['@Picture'];
        tO.m_items['m_item_' + i] = entry['#cdata'];
        i++;
    });

    // should be used for all tests templates

    ordinaryData(tO, tData);

    function ordinaryData(tO, tData) {
        tO.background = tData.background;
        tO.question = tData.Question['#cdata'];
        tO.FaultCaption = tData.FaultCaption;
        tO.FaultText = tData.FaultText;
        tO.CorrectCaption = tData.CorrectCaption;
        tO.CorrectText = tData.CorrectText;
    }


    console.log("iliustraData.js made ready tObj = ");
    console.log("2a_obj:", tObj);

    return tObj;
}

function test3bTemplate(testData, testType) {
    if (params['TestData'] != undefined) {
        tData = testObj.data;		// xml test data json in js object
        console.log("3b tData = ");
        console.log(tData);
        var tObj = {
            "section0": {
                "slide0": {
                    "template": "3e",
                    "background": "",
                    'script': {
                        "name": "template3escript",
                        'iniciated': false,
                    },
                    "question": "",
                    "items": {
                        "item_0": ""
                    },
                    'items_count': '',
                    'captions': {
                        'caption_1': ''
                    },
                    'captions_count': 0,
                    'answer': [], // array of items captions numbers
                    "CorrectCaption": "Teisingas",
                    "CorrectText": "Atsakymas teisingas, šaunu!",
                    "FaultCaption": "Teisingai!",
                    "FaultText": "Šaugu! Atsakėte teisingai.",
                    "css": ""
                }
            }
        };

        var tO = tObj.section0.slide0;

    } else {

        //tData = testObj;//.data;		// xml test data json in js object
        tData = testData;
        console.log("3b tData = ");
        console.log(tData);
        var tObj = {
            "template": "3e",
            "background": "",
            'script': {
                "name": "template3escript",
                'iniciated': false,
            },
            "question": "",
            "items": {
                "item_0": ""
            },
            'items_count': '',
            'captions': {
                'caption_1': ''
            },
            'captions_count': 0,
            'answer': [], // array of items captions numbers
            "CorrectCaption": "Teisingas",
            "CorrectText": "Atsakymas teisingas, šaunu!",
            "FaultCaption": "Teisingai!",
            "FaultText": "Šaugu! Atsakėte teisingai.",
            "css": ""

        };

        var tO = tObj;//.section0.slide0;

    }
    var Items = tData.Items.Item;
    var l = Items.length;
    tO.items_count = l;
    console.log('Items in XML = ' + l);

    var captions_count = 0;

    for (var i = 0; i < l; i++) {
        tO.items['item_' + i] = Items[i]['#cdata'];
        tO.answer.push(Items[i]['@CorrectAnswer']);
        if (captions_count < Items[i]['@CorrectAnswer']) {
            captions_count = Items[i]['@CorrectAnswer']
        }
    }
    tO.captions_count = captions_count;
    console.log('Found max item caption answer: ' + captions_count);
    captions_count++;


    for (var i = 1; i < captions_count; i++) {
        tO.captions['caption_' + i] = tData['Caption_' + i]['#cdata'];
    }

    ordinaryData(tO, tData);

    function ordinaryData(tO, tData) {
        tO.question = tData.Question['#cdata'];
        tO.background = tData.Background;
        tO.FaultCaption = tData.FaultCaption;
        tO.FaultText = tData.FaultText;
        tO.CorrectCaption = tData.CorrectCaption;
        tO.CorrectText = tData.CorrectText;
    }

    console.log("iliustraData.js made ready tObj = ");
    console.log(tObj);
    return tObj;
}


function test3eTemplate(testData, testType) {
    if (params['TestData'] != undefined) {

        tData = testObj.data;		// xml test data json in js object
        console.log("3e tData = ");
        console.log(tData);
        var tObj = {
            "section0": {
                "slide0": {
                    "template": "3e",
                    "background": "",
                    'script': {
                        "name": "template3escript",
                        'iniciated': false,
                    },
                    "question": "",
                    "items": {
                        "item_0": ""
                    },
                    'items_count': '',
                    'captions': {
                        'caption_1': ''
                    },
                    'captions_count': 0,
                    'answer': [], // array of items captions numbers
                    "CorrectCaption": "Teisingas",
                    "CorrectText": "Atsakymas teisingas, šaunu!",
                    "FaultCaption": "Teisingai!",
                    "FaultText": "Šaugu! Atsakėte teisingai.",
                    "css": ""
                }
            }
        };

        var tO = tObj.section0.slide0;

    } else {

        //tData = testObj.data;		// xml test data json in js object
        tData = testData;
        console.log("3e tData = ");
        console.log(tData);
        var tObj = {
            "template": "3e",
            "background": "",
            'script': {
                "name": "template3escript",
                'iniciated': false,
            },
            "question": "",
            "items": {
                "item_0": ""
            },
            'items_count': '',
            'captions': {
                'caption_1': ''
            },
            'captions_count': 0,
            'answer': [], // array of items captions numbers
            "CorrectCaption": "Teisingas",
            "CorrectText": "Atsakymas teisingas, šaunu!",
            "FaultCaption": "Teisingai!",
            "FaultText": "Šaugu! Atsakėte teisingai.",
            "css": ""

        };

        var tO = tObj;//.section0.slide0;
    }
    var Items = tData.Items.Item;
    var l = Items.length;
    tO.items_count = l;
    console.log('Items in XML = ' + l);

    var captions_count = 0;

    for (var i = 0; i < l; i++) {
        tO.items['item_' + i] = Items[i]['#cdata'];
        tO.answer.push(Items[i]['@CorrectAnswer']);
        if (captions_count < Items[i]['@CorrectAnswer']) {
            captions_count = Items[i]['@CorrectAnswer']
        }
    }
    tO.captions_count = captions_count;
    console.log('Found max item caption answer: ' + captions_count);
    captions_count++;


    for (var i = 1; i < captions_count; i++) {
        tO.captions['caption_' + i] = tData['Caption_' + i]['#cdata'];
    }

    ordinaryData(tO, tData);

    function ordinaryData(tO, tData) {
        tO.question = tData.Question['#cdata'];
        tO.background = tData.Background;
        tO.FaultCaption = tData.FaultCaption;
        tO.FaultText = tData.FaultText;
        tO.CorrectCaption = tData.CorrectCaption;
        tO.CorrectText = tData.CorrectText;
    }

    console.log("iliustraData.js made ready tObj = ");
    console.log(tObj);
    return tObj;
}

function test4bTemplate(testData, testType) {
    if (params['TestData'] != undefined) {
        tData = testObj.data;		// xml test data json in js object
        console.log("4b tData = ");
        console.log(tData);
        var tObj = {
            "section0": {
                "slide0": {
                    "template": "4b",
                    "background": "",
                    'script': {
                        "name": "template4bscript",
                        'iniciated': false,
                    },
                    "question": "",
                    'items_count': '',
                    "items": {
                        "item_0": ""
                    },
                    "caption": '',
                    'answer': [],
                    "CorrectCaption": "Teisingas",
                    "CorrectText": "Atsakymas teisingas, šaunu!",
                    "FaultCaption": "Teisingai!",
                    "FaultText": "Šaugu! Atsakėte teisingai.",
                    "css": ""
                }
            }
        };




        var tO = tObj.section0.slide0;

    } else {

        //tData = testObj.data;		// xml test data json in js object
        tData = testData;
        console.log("4b tData = ");
        console.log(tData);
        var tObj = {
            "template": "4b",
            "background": "",
            'script': {
                "name": "template4bscript",
                'iniciated': false,
            },
            "question": "",
            'items_count': '',
            "items": {
                "item_0": ""
            },
            "caption": '',
            'answer': [],
            "CorrectCaption": "Teisingas",
            "CorrectText": "Atsakymas teisingas, šaunu!",
            "FaultCaption": "Teisingai!",
            "FaultText": "Šaugu! Atsakėte teisingai.",
            "css": ""

        };

        var tO = tObj;//.section0.slide0;
    }
    var Items = tData.Items.Item;
    var l = Items.length;
    tO.items_count = l;
    console.log('Items in XML = ' + l);
    for (var i = 0; i < l; i++) {
        tO.items['item_' + i] = Items[i]['#cdata'];
        tO.answer.push(Items[i]['@CorrectAnswer'] - 1);
    }

    tO.caption = tData.Caption_1['#cdata'];

    ordinaryData(tO, tData);

    function ordinaryData(tO, tData) {
        tO.question = tData.Question['#cdata'];
        tO.background = tData.Background;
        tO.FaultCaption = tData.FaultCaption;
        tO.FaultText = tData.FaultText;
        tO.CorrectCaption = tData.CorrectCaption;
        tO.CorrectText = tData.CorrectText;
    }

    console.log("iliustraData.js made ready tObj = ");
    console.log(tObj);
    return tObj;
}

// get xml data from link
function loadXMLDoc(dname) {
    if (window.XMLHttpRequest) {
        xhttp = new XMLHttpRequest();
    }
    else {
        xhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

    xhttp.open("GET", dname, false);
    xhttp.send();

    return xhttp.responseText;
}

// parse xml data to DOM
function parseXml(xml) {
    var dom = null;
    if (window.DOMParser) {
        try {
            dom = (new DOMParser()).parseFromString(xml, "text/xml");
        }
        catch (e) {
            dom = null;
        }
    }
    else if (window.ActiveXObject) {
        try {
            dom = new ActiveXObject('Microsoft.XMLDOM');
            dom.async = false;
            if (!dom.loadXML(xml)) // parse error ..
                window.alert(dom.parseError.reason + dom.parseError.srcText);
        }
        catch (e) {
            dom = null;
        }
    }
    else
        alert("cannot parse xml string!");

    //$dom = $(dom);
    // name = $dom;//.find('data[id="' + 13 + '"]').get(0).outerHTML;

    return dom;

}

/*	convert xml to json object
 *	This work is licensed under Creative Commons GNU LGPL License.
 
 License: http://creativecommons.org/licenses/LGPL/2.1/
 Version: 0.9
 Author:  Stefan Goessner/2006
 Web:     http://goessner.net/ 
 */
function xml2json(xml, tab) {
    var X = {
        toObj: function(xml) {
            var o = {};
            if (xml.nodeType == 1) {   // element node ..
                if (xml.attributes.length)   // element with attributes  ..
                    for (var i = 0; i < xml.attributes.length; i++)
                        o["@" + xml.attributes[i].nodeName] = (xml.attributes[i].nodeValue || "").toString();
                if (xml.firstChild) { // element has child nodes ..
                    var textChild = 0, cdataChild = 0, hasElementChild = false;
                    for (var n = xml.firstChild; n; n = n.nextSibling) {
                        if (n.nodeType == 1)
                            hasElementChild = true;
                        else if (n.nodeType == 3 && n.nodeValue.match(/[^ \f\n\r\t\v]/))
                            textChild++; // non-whitespace text
                        else if (n.nodeType == 4)
                            cdataChild++; // cdata section node
                    }
                    if (hasElementChild) {
                        if (textChild < 2 && cdataChild < 2) { // structured element with evtl. a single text or/and cdata node ..
                            X.removeWhite(xml);
                            for (var n = xml.firstChild; n; n = n.nextSibling) {
                                if (n.nodeType == 3)  // text node
                                    o["#text"] = X.escape(n.nodeValue);
                                else if (n.nodeType == 4)  // cdata node
                                    o["#cdata"] = X.escape(n.nodeValue);
                                else if (o[n.nodeName]) {  // multiple occurence of element ..
                                    if (o[n.nodeName] instanceof Array)
                                        o[n.nodeName][o[n.nodeName].length] = X.toObj(n);
                                    else
                                        o[n.nodeName] = [o[n.nodeName], X.toObj(n)];
                                }
                                else  // first occurence of element..
                                    o[n.nodeName] = X.toObj(n);
                            }
                        }
                        else { // mixed content
                            if (!xml.attributes.length)
                                o = X.escape(X.innerXml(xml));
                            else
                                o["#text"] = X.escape(X.innerXml(xml));
                        }
                    }
                    else if (textChild) { // pure text
                        if (!xml.attributes.length)
                            o = X.escape(X.innerXml(xml));
                        else
                            o["#text"] = X.escape(X.innerXml(xml));
                    }
                    else if (cdataChild) { // cdata
                        if (cdataChild > 1)
                            o = X.escape(X.innerXml(xml));
                        else
                            for (var n = xml.firstChild; n; n = n.nextSibling)
                                o["#cdata"] = X.escape(n.nodeValue);
                    }
                }
                if (!xml.attributes.length && !xml.firstChild)
                    o = null;
            }
            else if (xml.nodeType == 9) { // document.node
                o = X.toObj(xml.documentElement);
            }
            else
                alert("unhandled node type: " + xml.nodeType);
            return o;
        },
        toJson: function(o, name, ind) {
            var json = name ? ("\"" + name + "\"") : "";
            if (o instanceof Array) {
                for (var i = 0, n = o.length; i < n; i++)
                    o[i] = X.toJson(o[i], "", ind + "\t");
                json += (name ? ":[" : "[") + (o.length > 1 ? ("\n" + ind + "\t" + o.join(",\n" + ind + "\t") + "\n" + ind) : o.join("")) + "]";
            }
            else if (o == null)
                json += (name && ":") + "null";
            else if (typeof (o) == "object") {
                var arr = [];
                for (var m in o)
                    arr[arr.length] = X.toJson(o[m], m, ind + "\t");
                json += (name ? ":{" : "{") + (arr.length > 1 ? ("\n" + ind + "\t" + arr.join(",\n" + ind + "\t") + "\n" + ind) : arr.join("")) + "}";
            }
            else if (typeof (o) == "string")
                json += (name && ":") + "\"" + o.toString() + "\"";
            else
                json += (name && ":") + o.toString();
            return json;
        },
        innerXml: function(node) {
            var s = ""
            if ("innerHTML" in node)
                s = node.innerHTML;
            else {
                var asXml = function(n) {
                    var s = "";
                    if (n.nodeType == 1) {
                        s += "<" + n.nodeName;
                        for (var i = 0; i < n.attributes.length; i++)
                            s += " " + n.attributes[i].nodeName + "=\"" + (n.attributes[i].nodeValue || "").toString() + "\"";
                        if (n.firstChild) {
                            s += ">";
                            for (var c = n.firstChild; c; c = c.nextSibling)
                                s += asXml(c);
                            s += "</" + n.nodeName + ">";
                        }
                        else
                            s += "/>";
                    }
                    else if (n.nodeType == 3)
                        s += n.nodeValue;
                    else if (n.nodeType == 4)
                        s += "<![CDATA[" + n.nodeValue + "]]>";
                    return s;
                };
                for (var c = node.firstChild; c; c = c.nextSibling)
                    s += asXml(c);
            }
            return s;
        },
        escape: function(txt) {
            return txt.replace(/[\\]/g, "\\\\")
                    .replace(/[\"]/g, '\\"')
                    .replace(/[\n]/g, '\\n')
                    .replace(/[\r]/g, '\\r');
        },
        removeWhite: function(e) {
            e.normalize();
            for (var n = e.firstChild; n; ) {
                if (n.nodeType == 3) {  // text node
                    if (!n.nodeValue.match(/[^ \f\n\r\t\v]/)) { // pure whitespace text node
                        var nxt = n.nextSibling;
                        e.removeChild(n);
                        n = nxt;
                    }
                    else
                        n = n.nextSibling;
                }
                else if (n.nodeType == 1) {  // element node
                    X.removeWhite(n);
                    n = n.nextSibling;
                }
                else                      // any other node
                    n = n.nextSibling;
            }
            return e;
        }
    };
    if (xml.nodeType == 9) // document node
        xml = xml.documentElement;
    var json = X.toJson(X.toObj(X.removeWhite(xml)), xml.nodeName, "\t");
    return "{\n" + tab + (tab ? json.replace(/\t/g, tab) : json.replace(/\t|\n/g, "")) + "\n}";
}

/*	convert json to xml
 *	This work is licensed under Creative Commons GNU LGPL License.
 
 License: http://creativecommons.org/licenses/LGPL/2.1/
 Version: 0.9
 Author:  Stefan Goessner/2006
 Web:     http://goessner.net/ 
 */
function json2xml(o, tab) {
    var toXml = function(v, name, ind) {
        var xml = "";
        if (v instanceof Array) {
            for (var i = 0, n = v.length; i < n; i++)
                xml += ind + toXml(v[i], name, ind + "\t") + "\n";
        }
        else if (typeof (v) == "object") {
            var hasChild = false;
            xml += ind + "<" + name;
            for (var m in v) {
                if (m.charAt(0) == "@")
                    xml += " " + m.substr(1) + "=\"" + v[m].toString() + "\"";
                else
                    hasChild = true;
            }
            xml += hasChild ? ">" : "/>";
            if (hasChild) {
                for (var m in v) {
                    if (m == "#text")
                        xml += v[m];
                    else if (m == "#cdata")
                        xml += "<![CDATA[" + v[m] + "]]>";
                    else if (m.charAt(0) != "@")
                        xml += toXml(v[m], m, ind + "\t");
                }
                xml += (xml.charAt(xml.length - 1) == "\n" ? ind : "") + "</" + name + ">";
            }
        }
        else {
            xml += ind + "<" + name + ">" + v.toString() + "</" + name + ">";
        }
        return xml;
    }, xml = "";
    for (var m in o)
        xml += toXml(o[m], m, "");
    return tab ? xml.replace(/\t/g, tab) : xml.replace(/\t|\n/g, "");
}

function getXmlData(fullObj, currData) {
    $.each(currData, function(i, item) {
        fullObj["slide" + i] = window['test' + item.type + 'Template'](item, item.type);
        //console.log("each" + i, item); 

    });

    return fullObj;
}

function getXmlData_Iliustra(testData, testType) {
    //alert('test' + testType + 'Template');
    fullObj = window['test' + testType + 'Template'](testData, testType);

    return fullObj;
}