﻿/******************************************************************************
* object: autoInherit
* author: Yellow Pencil
* notes:  
* 
***/
    
function classAutoInherit() {
    var o = this;
    
    o.settings = {
        login: '',
        session: '',
        service: 'http://208.123.216.54/CMS/services/RQLservice.asmx',
        confirmInherit: 'This container will inherit the elements connected to its associated ancestor container. Continue?',
        confirmEndow: 'This action will apply the elements connected to this container on all associated child pages. Continue?',
        confirmRemove: 'This action will remove all elements connected to this container on all associated child pages. Continue?',
        windowError: 'A required window has been blocked by your system.',
        serviceError: 'The RedDot automation service does not appear to be available at this time.  Your request cannot be completed.'
    };
    
    
/******************************************************************************
    * method: xmlHttpRequest
    * notes:  Cross-browser code to get an XMLHTTPRequest object to
    *         communicate with the RQL service.
    * 
    ***/
    
    o.xmlHttpRequest = function(z) { 
        var x = null; 
        if(window.XMLHttpRequest) { 
            x = new window.XMLHttpRequest(); 
        } else if(window.ActiveXObject) { 
            try { 
                if(eval("new ActiveXObject('Microsoft.XMLHTTP');")) { 
                    x = new ActiveXObject('Microsoft.XMLHTTP'); 
                }; 
                if(eval("new ActiveXObject('MSXML2.XMLHTTP');")) { 
                    x = new ActiveXObject('MSXML2.XMLHTTP'); 
                }; 
            } catch(e) {}; 
        }; 
        return x; 
    }; 
    
    
/******************************************************************************
    * method: openWindow
    * notes:  Opens a new window and tests for popup blockers.
    * 
    ***/
    
    o.openWindow = function(z) {
        var w = window.open('#', 'inheritControl', 'width=600,height=280,location=no');
        if(!w) {
            alert(o.settings.windowError);
        };
        return w;
    };
    
    
/******************************************************************************
    * method: buildPage
    * notes:  Builds the document in the popup window.
    * 
    ***/
    
    o.buildPage = function(z) {
        var s = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
        s += '<html xmlns="http://www.w3.org/1999/xhtml">';
        
        s += '<head><!-- PageID 3022 - published by RedDot 9 - 9.0.0.27 - 26845 -->';
        s += '    <title>Yellow Pencil Auto-Inheritance Control</title>';
        
        s += '<style type="text/css">';
        s += 'body { margin: 0px; padding: 20px; font-family: arial, sans-serif; background: #efefef; }';
        s += 'h1 { margin: 0px; font-size: 1.2em; }';
        s += 'img { margin: 0px 0px 10px 10px; }';
        s += '#control { clear: left; float: left; overflow: hidden; font-size: 0.75em; }';
        s += '#control p {  margin: 10px 0px 0px 0px; }';
        s += '#progressBox { float: left; display: inline; margin: 10px 0px 0px 0px; padding: 15px; width: 528px; overflow: hidden; line-height: 0px; background: #ccc; border-top: 1px solid #fff; border-left: 1px solid #fff; border-bottom: 1px solid #333; border-right: 1px solid #333; }';
        s += '#progressBar { padding: 2px; height: 15px; overflow: hidden; font-size: 0px; background: #efefef; border: 1px solid #666; }';
        s += '#progress { float: left; display: inline; width: 0px; height: 15px; overflow: hidden; background: #000; border: 0px; }';
        s += '#start { clear: both; float: left; display: inline; padding-top: 5px; overflow: hidden; line-height: 1em; }';
        s += '#end { float: right; display: inline; padding-top: 5px; overflow: hidden; line-height: 1em; }';
        s += '#status { float: left; display: inline; margin: 5px 5px 0px 5px; }';
        s += '#timeLeft { font-weight: bold; }';
        s += '#closeButton { display: none; float: right; margin: 5px 0px 0px 5px; width: 120px; overflow: hidden; }';
        s += '#closeButton a { display: block; padding: 5px 10px 5px 10px; overflow: hidden; color: #fff; text-align: center; text-decoration: none; border: 1px solid #666; background: #000; }';
        s += '</style>';
        
        s += '</head>';
        
        s += '<body>';
        s += '    <div id="control">';
        s += '        <h1>Auto-Inheritance</h1>';
        
        switch(z.type) {        
            case 'remove':
                s += '        <img width="64" height="64" src="' + o.images.contentRemove + '" alt="" align="right" />';
                s += '        <p>Do NOT navigate away from this page or click any buttons in this window until the operation is complete.</p><p>This action will remove all elements connected to this container on all associated child pages.</p>';
            break;
            case 'push':
                s += '        <img width="64" height="64" src="' + o.images.contentEndow + '" alt="" align="right" />';
                s += '        <p>Do NOT navigate away from this page or click any buttons in this window until the operation is complete.</p><p>This action will apply the elements connected to this container on all associated child pages.</p><p>Elements must be connected (not referenced) to be endowed properly.</p>';
            break;
            default:
                s += '        <img width="64" height="64" src="' + o.images.contentInherit + '" alt="" align="right" />';
                s += '        <p>Do NOT navigate away from this page or click any buttons in this window until the operation is complete.</p><p>This container will inherit the elements connected to its associated ancestor container.</p><p>Elements must be connected (not referenced) to be inherited properly.</p>';
            break;
        };
        
        s += '        <div id="progressBox">';
        s += '            <div id="progressBar">';
        s += '                <div id="progress">';
        s += '                    ';
        s += '                </div>';
        s += '            </div>';
        s += '            <span id="start">0%</span> <span id="end">100%</span>';
        s += '        </div>';
        
        s += '        <div id="status">';
        s += '            <span id="timeTitle">Time left:</span> <span id="timeLeft">Calculating</span>';
        s += '        </div>';
        
        s += '        <div id="closeButton">';
        s += '            <a href="#" onclick="window.close(); return false;">Close</a>';
        s += '        </div>';
        
        s += '    </div>';
        s += '</body>';
        
        s += '</html>';
        
        z.win.document.write(s);
        z.win.document.close();
    };
    
    
/******************************************************************************
    * method: update
    * notes:  Run the update command and refresh the status in the popup.
    * 
    ***/
    
    o.update = function(z) {
        var c = 0, v = 0;
        var a = z.win.document.getElementById('progressBar');
        var b = z.win.document.getElementById('progress');
        var x = o.xmlHttpRequest(z);
        
        z.startTime = new Date().getTime();
        
        if(z.type == 'remove') {
            var y = '<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ExecuteString xmlns="http://reddot.de/cms/webservices/navigation/1_1"><command>&lt;IODATA loginguid=&quot;' + o.settings.login + '&quot; dialoglanguageid=&quot;ENG&quot; sessionkey=&quot;' + o.settings.session + '&quot;&gt;&lt;LINK guid=&quot;' + z.targets[z.count] + '&quot;&gt;&lt;LINK action=&quot;unlink&quot; reddotcacheguid=&quot;&quot;/&gt;&lt;URL action=&quot;unlink&quot;/&gt;&lt;/LINK&gt;&lt;/IODATA&gt;</command></ExecuteString></soap:Body></soap:Envelope>';
        } else {
            var y = '<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ExecuteString xmlns="http://reddot.de/cms/webservices/navigation/1_1"><command>&lt;IODATA loginguid=&quot;' + o.settings.login + '&quot; user=&quot;christopher.sheldon&quot; dialoglanguageid=&quot;ENG&quot; sessionkey=&quot;' + o.settings.session + '&quot;&gt;&lt;CLIPBOARD action=&quot;ReferenceToLink&quot; guid=&quot;' + z.targets[z.count] + '&quot; type=&quot;link&quot; descent=&quot;unknown&quot; addition=&quot;&quot;&gt;&lt;ENTRY guid=&quot;' + z.source + '&quot; type=&quot;link&quot; descent=&quot;unknown&quot; /&gt;&lt;/CLIPBOARD&gt;&lt;/IODATA&gt;</command></ExecuteString></soap:Body></soap:Envelope>';
        };
        
        try { 
            x.async = false; 
        } catch(e) {}; 
        
        x.validateOnParse = false;
        
        x.onreadystatechange = function() { 
            if(x.readyState == 4) { 
                z.count = z.count + 1;
                
                if(x.status == 200 || x.status == 0) { 
                    b.style.width = Math.ceil(((a.offsetWidth - 6) * ((z.count + 1) / (z.targets.length)))) + 'px';
                
                    if(z.count < z.targets.length) {                        
                        z.thisTime = new Date().getTime() - z.startTime;
                        z.totalTime += z.thisTime;
                        z.averageTime = Math.ceil(z.totalTime / z.count);
                        z.timeLeft = new Date(z.averageTime * (z.targets.length - z.count + 1));
                        z.win.document.getElementById('timeLeft').innerHTML = (z.timeLeft.getMinutes() < 10 ? '0' : '') + z.timeLeft.getMinutes() + ':' + (z.timeLeft.getSeconds() < 10 ? '0' : '') + z.timeLeft.getSeconds();
                        
                        o.update(z); 
                    } else {
                        z.win.document.getElementById('timeLeft').innerHTML = 'Complete';
                        z.win.document.getElementById('closeButton').style.display = 'inline';
                        location.reload();
                        z.win.focus();
                    };
                } else {
                    // if error recieved log item to try again or report
                    //alert(o.settings.serviceError);
                    
                    o.update(z); 
                }; 
            }; 
        };
        
        x.open('POST', o.settings.service, true);
        
        x.setRequestHeader('SOAPAction', 'http://reddot.de/cms/webservices/navigation/1_1/ExecuteString'); 
        x.setRequestHeader('Content-Type', 'text/xml');
        
        x.send(y);
    };
    
    
/******************************************************************************
    * method: go
    * notes:  Begin inheritance commands.
    * 
    ***/
    
    o.go = function(z) {
        if(z && (z.source || z.type == 'remove') && z.targets) {
            var w = o.openWindow();
            
            o.buildPage({win: w, type: z.type});
            o.update({win: w, source: z.source,    targets: z.targets, type: z.type, count: 0, totalTime: 0});
        } else {
            // required properties missing
        };
    };
    
    
/******************************************************************************
    * method: go
    * notes:  Begin inheritance commands.
    * 
    ***/
    
    o.add = function(z) {
        var a = '';
        if(z && (z.inherit || z.endow)) {
            a += '<a class="inheritIcon" href="#" onclick="if(confirm(\'' + o.settings.confirmInherit + '\')) { autoInherit.go({source: \'' + z.inherit + '\', targets: [\'' + z.current + '\']}); }; return false;"><img src="' + o.images.iconInherit + '" alt="Inherit" /></a> ';
            a += '<a class="endowIcon" href="#" onclick="if(confirm(\'' + o.settings.confirmEndow + '\')) { autoInherit.go({type: \'push\', source: \'' + z.current + '\', targets: [\'' + z.endow.toString().replace(/,/ig, '\',\'') + '\']}); }; return false;"><img src="' + o.images.iconEndow + '" alt="Endow" /></a> ';
            a += '<a class="removeIcon" href="#" onclick="if(confirm(\'' + o.settings.confirmRemove + '\')) { autoInherit.go({type: \'remove\', targets: [\'' + z.endow.toString().replace(/,/ig, '\',\'') + '\']}); }; return false;"><img src="' + o.images.iconRemove + '" alt="Remove" /></a>';
        };
        document.write(a);
    };
    
    
    
    o.images = {
        iconInherit: '/images/eedc-master/ui/inherit32.png',
        iconEndow: '/images/eedc-master/ui/endow32.png',
        iconRemove: '/images/eedc-master/ui/remove32.png',
        contentInherit: '/images/eedc-master/ui/inherit64.png',
        contentEndow: '/images/eedc-master/ui/endow64.png',
        contentRemove: '/images/eedc-master/ui/remove64.png'
    };
};
var autoInherit = new classAutoInherit();
