﻿var m_AjaxRefreshWindowAfterClose = false;
//HTTP functions
function openUrl(url) {
    var xmlHTTP = null;
    var retVal = null;
    try {
        if (window.XMLHttpRequest) {
            xmlHTTP = new XMLHttpRequest();
        } else if (window.ActiveXObject) {
            xmlHTTP = new ActiveXObject("Microsoft.XMLHTTP");
        } else {
            window.alert('Sorry your browser is not compatible with this functionality');
            return;
        }
        xmlHTTP.open('GET', url, false);
        xmlHTTP.send(null);
        retVal = xmlHTTP.responseText;

        //m_Error = false;
        //m_ErrorMessage = '';
    } catch (e) {
        //m_Error = true;
        //m_ErrorMessage = 'Service Not Available';
    }
    return retVal;
}
function openAsyncUrl(url, callBack) {
    var xmlHTTP = null;
    var retVal = null;
    try {
        if (window.XMLHttpRequest) {
            xmlHTTP = new XMLHttpRequest();
        } else if (window.ActiveXObject) {
            xmlHTTP = new ActiveXObject("Microsoft.XMLHTTP");
        } else {
            window.alert('Sorry your browser is not compatible with this functionality');
            return;
        }
        xmlHTTP.open('GET', url, true);
        xmlHTTP.onreadystatechange = function() {
            if (xmlHTTP.readyState == 4) {
                callBack(xmlHTTP.responseText);
            }
        }
        xmlHTTP.send(null);
        //m_Error = false;
        //m_ErrorMessage = '';
    } catch (e) {
        //m_Error = true;
        //m_ErrorMessage = 'Service Not Available';
    }
}
function openAsyncUrl2(url) {
    var xmlHTTP = null;
    var retVal = null;
    try {
        if (window.XMLHttpRequest) {
            xmlHTTP = new XMLHttpRequest();
        } else if (window.ActiveXObject) {
            xmlHTTP = new ActiveXObject("Microsoft.XMLHTTP");
        } else {
            window.alert('Sorry your browser is not compatible with this functionality');
            return;
        }
        xmlHTTP.open('GET', url, true);
        xmlHTTP.send(null);
        //m_Error = false;
        //m_ErrorMessage = '';
    } catch (e) {
        //m_Error = true;
        //m_ErrorMessage = 'Service Not Available';
    }
}
function postUrl(url, data) {
    var xmlHTTP = null;
    var retVal = null;
    try {
        if (window.XMLHttpRequest) {
            xmlHTTP = new XMLHttpRequest();
        } else if (window.ActiveXObject) {
            xmlHTTP = new ActiveXObject("Microsoft.XMLHTTP");
        } else {
            window.alert('Sorry your browser is not compatible with this functionality');
            return;
        }
        xmlHTTP.open('POST', url, false);
        xmlHTTP.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        xmlHTTP.send(data);
        retVal = xmlHTTP.responseText;
        //m_Error = false;
        //m_ErrorMessage = '';
    } catch (e) {
        //m_Error = true;
        //m_ErrorMessage = 'Service Not Available';
    }
    return retVal;
}
function postUrlAsync(url, data) {
    var xmlHTTP = null;
    var retVal = null;
    try {
        if (window.XMLHttpRequest) {
            xmlHTTP = new XMLHttpRequest();
        } else if (window.ActiveXObject) {
            xmlHTTP = new ActiveXObject("Microsoft.XMLHTTP");
        } else {
            window.alert('Sorry your browser is not compatible with this functionality');
            return;
        }
        xmlHTTP.open('POST', url, true);
        xmlHTTP.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        xmlHTTP.send(data);
        xmlHTTP.onreadystatechange = function() {
            if (xmlHTTP.readyState == 4) {
                //callBack(xmlHTTP.responseText);
            }
        }
        //m_Error = false;
        //m_ErrorMessage = '';
    } catch (e) {
        //m_Error = true;
        //m_ErrorMessage = 'Service Not Available';
    }
    return retVal;
}

function prepareParameters(formName) {
    var f = document.getElementById(formName);
    if (f == null) return '';
    var data = 'sender=sendDataRequest';
    var name = '';
    var value = '';
    var inp = null;

    //INPUT collector    
    var coll = f.getElementsByTagName('INPUT');
    for (var i = 0; i < coll.length; i++) {
        inp = coll[i];
        name = inp.name;
        //Added by Robert Vastag 2008.01.05.
        value = '';
        if (name != '') {
            switch (inp.type) {
                case 'text':
                case 'hidden':
                case 'password':
                case 'image':
                    value = parseContent(inp.value);
                    break;
                case 'checkbox':
                case 'radiobutton':
                case 'radio':
                    //Modified by Robert Vastag 2008.01.05.
                    if (inp.checked) value = parseContent(inp.value); else continue;
                    break;
            }
            data += '&' + name + '=' + value;
        }
    }

    coll = f.getElementsByTagName('TEXTAREA');

    for (var i = 0; i < coll.length; i++) {
        inp = coll[i];
        name = inp.name;

        if (name != '') {
            value = inp.value.replace(/\n/g,'@@BR@@');
            data += '&' + name + '=' + parseContent(value);
        }
    }

    coll = f.getElementsByTagName('SELECT');
    for (var i = 0; i < coll.length; i++) {
        inp = coll[i];
        if (coll[i].getAttribute("soda_name") != null) {
            name = coll[i].getAttribute("soda_name");
        } else {
            name = inp.name;
        }
        if (name != '') {
            for (var j = 0; j < inp.options.length; j++) {
                if (inp.options[j].selected) {
                    value = inp.options[j].value;
                    data += '&' + name + '=' + parseContent(value);
                    if (!inp.multiple) break;
                }
            }
        }
    }
    return data;
}
function sendDataRequest(formName, url, async) {
    var data = prepareParameters(formName);
    if (typeof (async) == 'undefined') {
        return postUrl(url, data);
    } else {
        if (async) return postUrlAsync(url, data);
        else return postUrl(url, data);
    }
}
function parseContent(v) {
    var d = document.createElement('DIV');
    d.innerHTML = v;
    if (d.innerText === undefined) return encodeURIComponent(d.textContent);
    return encodeURIComponent(d.innerText);
}
//Window Management
function addStyle(selector, declaration) {
    // test for IE
    var ua = navigator.userAgent.toLowerCase();
    var isIE = (/msie/.test(ua)) && !(/opera/.test(ua)) && (/win/.test(ua));

    // create the style node for all browsers
    var style_node = document.createElement("style");
    style_node.setAttribute("type", "text/css");
    style_node.setAttribute("media", "screen");

    // append a rule for good browsers
    if (!isIE) style_node.appendChild(document.createTextNode(selector + " {" + declaration + "}"));

    // append the style node
    document.getElementsByTagName("head")[0].appendChild(style_node);

    // use alternative methods for IE
    if (isIE && document.styleSheets && document.styleSheets.length > 0) {
        var last_style_node = document.styleSheets[document.styleSheets.length - 1];
        if (typeof (last_style_node.addRule) == "object") last_style_node.addRule(selector, declaration);
    }
};
var m_SodaLayerCreated = 0;
var m_SodaOpenMode = 0;
var m_SodaSourceContentId = '';
function sodaAjaxWindow() {
    this.bgColor = 'white';
    this.headerBgColor = '#a1d8fb';
    this.width = 400;
    this.height = 200;
    this.caption = 'Ajax Window';
    this.closeText = 'Close';
}
function hideSelect() {
    var doc = document;
    if (window.parent != null) doc = window.parent.document;
    var coll = null;
    if ((BrowserDetect.browser.toLowerCase() == 'explorer') && (BrowserDetect.version < 7)) {
        coll = doc.getElementsByTagName('SELECT');
        for (var i = 0; i < coll.length; i++) {
            if (coll[i].getAttribute("soda_ignore") == "true") {

            } else {
                coll[i].style.display = 'none';
            }
        }
    }
    if (BrowserDetect.browser.toLowerCase() == 'safari') {
        coll = doc.getElementById('SilverlightPlugInHost');
        if (coll != null) {
            coll.style.left = '-1000px';
        }
    }
}
function showSelect() {
    var coll = null;
    var doc = document;
    if (window.parent != null) doc = window.parent.document;
    if ((BrowserDetect.browser.toLowerCase() == 'explorer') && (BrowserDetect.version < 7)) {
        coll = doc.getElementsByTagName('SELECT');
        for (var i = 0; i < coll.length; i++) {
            coll[i].style.display = 'inline';
        }
    }
    if (BrowserDetect.browser.toLowerCase() == 'safari') {
        coll = doc.getElementById('SilverlightPlugInHost');
        if (coll != null) {
            coll.style.left = '0px';
        }
    }
}
function initWindow(win) {

    //create a gray background
    var cName = 'soda_grey_layer';
    var pageWidth = '0px';
    var pageHeight = '0px';
    var style = '';
    if (document.body && (document.body.scrollWidth || document.body.scrollHeight)) {
        pageWidth = document.body.scrollWidth + 'px';
        pageHeight = document.body.scrollHeight + 'px';
    }
    else if (document.body.offsetWidth) {
        pageWidth = document.body.offsetWidth + 'px';
        pageHeight = document.body.offsetHeight + 'px';
    }

    pageHeight = '100%';
    pageWidth = '100%';
    if (m_SodaLayerCreated == 0) {
        //Background
        style = 'display:block;background-color: black; filter: alpha(Opacity=30);-khtml-opacity: .3;-moz-opacity: .3;opacity: 0.3;position: fixed; top: 0px; left: 0px; z-index: 1000; width:' + pageWidth + '; height:' + pageHeight + ';';
        addStyle('#soda_layer_placeHolder', style);
        //IE 6 hack
        //style = 'position: absolute;height: expression(document.body.scrollHeight > document.body.offsetHeight ? document.body.scrollHeight : document.body.offsetHeight + \'px\');';
        //addStyle('* html #soda_layer_placeHolder',style);

        var positionX = (document.body.offsetWidth / 2) - (win.width / 2);
        var positionY = 0 - parseInt(this.offsetHeight / 2) + (document.documentElement && document.documentElement.scrollTop || document.body.scrollTop) + 'px';

        //Window Style
        style = 'border: 0px solid #a1d8fb; display:block;background-color:' + win.bgColor + ';height:' + win.height + 'px;width:' + win.width + 'px;z-index:1001;position:fixed;left:50%;top:50%';
        addStyle('#soda_main_window', style);

        //style = 'position: absolute; margin-top: expression(0 - parseInt(this.offsetHeight / 2) + (TBWindowMargin = document.documentElement && document.documentElement.scrollTop || document.body.scrollTop) + 'px');';
        //addStyle('* html #soda_main_window',style);

        //Header Style     
        style = 'font-family: Verdana, Arial, Helvetica, sans-serif;font-weight:bold;background-color:' + win.headerBgColor + ';text-align:left;font-size: 13px;padding-left: 5px;color:black;';
        addStyle('#soda_window_header', style);

        //Header Control Style     
        style = 'text-align:right;color:black;font-size:13px;padding-right:5px;font-weight:normal;text-decoration:underline';
        addStyle('#soda_window_control', style);
        style = 'text-align:right;color:black;font-size:13px;padding-right:5px;font-weight:normal;text-decoration:none';
        addStyle('#soda_window_control:hover', style);

        //addStyle('#soda_window_control a',style);       
        style = 'padding: 5px;font-size:14px;text-align:left';
        addStyle('#soda_window_content', style);

        m_SodaLayerCreated = 1;
    }

    var placeHolder = document.createElement('DIV');
    placeHolder.id = 'soda_layer_placeHolder';
    //placeHolder.className = cName;
    document.body.appendChild(placeHolder);
    
    var windowHolder = document.createElement('DIV');
    windowHolder.id = 'soda_main_window';
    windowHolder.style.width = win.width + 'px';
    windowHolder.style.height = win.height + 'px';

    windowHolder.style.marginLeft = '-' + parseInt(win.width / 2) + 'px';
    windowHolder.style.marginTop = '-' + parseInt(win.height / 2) + 'px';
    document.body.appendChild(windowHolder);

    if (window.addEventListener) { // Mozilla, Netscape, Firefox
        window.addEventListener('resize', resizeWindow, false);
    } else { // IE
        window.attachEvent('onresize', resizeWindow);
    }


    //Header
    var headerHolder = document.createElement('DIV');
    headerHolder.id = 'soda_window_header';
    headerHolder.innerHTML = '<table width="100%"><tr><td>' + win.caption + '</td><td align=right><a href="javascript: closeAjaxWindow()" id="soda_window_control">' + win.closeText + '</a></td></tr></table>';
    //windowHolder.appendChild(headerHolder); 

    //Windows Control
    headerHolder = document.createElement('DIV');
    headerHolder.id = 'soda_window_content';

    windowHolder.appendChild(headerHolder);
    hideSelect();
    return headerHolder;
}
function initNotificationWindow(win) {

    //create a gray background
    var cName = 'soda_grey_layer';
    var pageWidth = '0px';
    var pageHeight = '0px';
    var style = '';
    if (document.body && (document.body.scrollWidth || document.body.scrollHeight)) {
        pageWidth = document.body.scrollWidth + 'px';
        pageHeight = document.body.scrollHeight + 'px';
    }
    else if (document.body.offsetWidth) {
        pageWidth = document.body.offsetWidth + 'px';
        pageHeight = document.body.offsetHeight + 'px';
    }

    pageHeight = '100%';
    pageWidth = '100%';
    if (m_SodaLayerCreated == 0) {
        //Background
        style = 'display:block;position: fixed; top: 0px; left: 0px; z-index: 1000; width:' + pageWidth + '; height:' + pageHeight + ';';
        addStyle('#soda_layer_placeHolder', style);
        //IE 6 hack
        //style = 'position: absolute;height: expression(document.body.scrollHeight > document.body.offsetHeight ? document.body.scrollHeight : document.body.offsetHeight + \'px\');';
        //addStyle('* html #soda_layer_placeHolder',style);

        var positionX = (document.body.offsetWidth / 2) - (win.width / 2);
        var positionY = 0 - parseInt(this.offsetHeight / 2) + (document.documentElement && document.documentElement.scrollTop || document.body.scrollTop) + 'px';

        //Window Style
        style = 'border: 2px solid #d4d4d4; display:block;background-color:' + win.bgColor + ';height:' + win.height + 'px;width:' + win.width + 'px;z-index:1001;position:fixed;left:50%;top:50%';
        addStyle('#soda_main_window', style);

        //style = 'position: absolute; margin-top: expression(0 - parseInt(this.offsetHeight / 2) + (TBWindowMargin = document.documentElement && document.documentElement.scrollTop || document.body.scrollTop) + 'px');';
        //addStyle('* html #soda_main_window',style);

        //Header Style     
        style = 'font-family: Verdana, Arial, Helvetica, sans-serif;background-color:' + win.headerBgColor + ';text-align:left;font-size: 16px;padding-left: 5px;color: white;';
        addStyle('#soda_window_header', style);
        style = 'text-align:left;font-size: 15px;color: white;font-weight:bold;';
        addStyle('#soda_window_header_caption', style);

        //Header Control Style     
        style = 'text-align:right;color:white;font-size:12px;padding-right:5px;font-weight:normal;text-decoration:none';
        addStyle('#soda_window_control', style);
        style = 'text-align:right;color:white;font-size:12px;padding-right:5px;font-weight:normal;text-decoration:underline';
        addStyle('#soda_window_control:hover', style);

        //addStyle('#soda_window_control a',style);       
        style = 'padding: 5px;font-size:14px;text-align:left';
        addStyle('#soda_window_content', style);

        m_SodaLayerCreated = 1;
    }

    var placeHolder = document.createElement('DIV');
    placeHolder.id = 'soda_layer_placeHolder';
    //placeHolder.className = cName;
    document.body.appendChild(placeHolder);

    var windowHolder = document.createElement('DIV');
    windowHolder.id = 'soda_main_window';
    windowHolder.style.width = win.width + 'px';
    windowHolder.style.height = win.height + 'px';

    windowHolder.style.marginLeft = '-' + parseInt(win.width / 2) + 'px';
    windowHolder.style.marginTop = '-' + parseInt(win.height / 2) + 'px';
    document.body.appendChild(windowHolder);

    if (window.addEventListener) { // Mozilla, Netscape, Firefox
        window.addEventListener('resize', resizeWindow, false);
    } else { // IE
        window.attachEvent('onresize', resizeWindow);
    }


    //Header
    var headerHolder = document.createElement('DIV');
    headerHolder.id = 'soda_window_header';
    headerHolder.innerHTML = '<table width="100%"><tr><td id="soda_window_header_caption">' + win.caption + '</td><td align=right><a href="javascript: closeAjaxWindow()" id="soda_window_control">' + win.closeText + '</a></td></tr></table>';
    windowHolder.appendChild(headerHolder);

    //Windows Control
    headerHolder = document.createElement('DIV');
    headerHolder.id = 'soda_window_content';

    windowHolder.appendChild(headerHolder);
    hideSelect();
    return headerHolder;
}
function createNotification2(s, win) {
    createWindowFromUrl('../services/gettemplate.aspx?id=' + s, win);
}
function createNotification(s, win) {
    m_SodaOpenMode = 4;
    var contentHolder = initWindow(win);
    if (contentHolder != null) {
        contentHolder.innerHTML = s;
    }
}
function createWindowFromElement(id, win) {
    m_SodaOpenMode = 1;
    var contentHolder = initWindow(win);

    var d = document.getElementById(id);

    if (contentHolder != null) {
        if (d != null) {
            contentHolder.innerHTML = d.innerHTML;
            m_SodaSourceContentId = id;
            d.innerHTML = '';
        }
    }
}
function createWindowFromUrl(url, win) {
    m_SodaOpenMode = 2;

    var contentHolder = initWindow(win);

    if (contentHolder != null) {
        contentHolder.innerHTML = createLoadingAnimation(parseInt(win.height - 20));
        var callback = function(v) {
            contentHolder.innerHTML = v;
        }

        openAsyncUrl(url, callback);
    }
}
function createWindowInFrame(url, win) {
    m_SodaOpenMode = 3;
    var contentHolder = initWindow(win);
    if (contentHolder != null) {
        contentHolder.innerHTML = '<iframe frameborder="0" src="' + url + '" width="' + parseInt(win.width - 10) + '" height="' + parseInt(win.height - 10) + '"></iframe>';
    }
}
function createLoadingAnimation(height) {
    //return '<div style="height:' + height + 'px;vertical-align:middle;text-align:center;border:1px solid red;"><img src="../media/images/ajax-loader.gif"></div>';
    return '<div style="height:' + height + 'px;width:100%;position:relative;vertical-align:middle;text-align:center;"><img src="../media/images/ajax-loader.gif" style="position:absolute; top:43%;margin-left:-70px;"></div>';
}
function setDropdownItem(id, v) {
    var ddl = document.getElementById(id);
    if (ddl != null) {
        for (var i = 0; i < ddl.options.length; i++) {
            if (ddl.options[i].value.toLowerCase() == v.toString().toLowerCase()) {
                ddl.selectedIndex = i;
                break;
            }
        }
    }
}
function mergeParameters(id, parameters) {
    var d = document.getElementById(id);
    // Parse a given querystring
    var qs = new Querystring(parameters);
    if (d != null) {
        var coll = d.getElementsByTagName('INPUT');
        if (coll != null) {
            for (var i = 0; i < coll.length; i++) {
                if (qs.contains(coll[i].name)){
                    coll[i].value = qs.get(coll[i].name);
                }
            }
        }
        coll = d.getElementsByTagName('TEXTAREA');
        if (coll != null) {
            for (var i = 0; i < coll.length; i++) {
                if (qs.contains(coll[i].name)) {
                    coll[i].value = qs.get(coll[i].name);
                }
            }
        }
        coll = d.getElementsByTagName('SELECT');
        if (coll != null) {
            for (var i = 0; i < coll.length; i++) {
                if (qs.contains(coll[i].name)) {
                    setDropdownItem(coll[i].id,qs.get(coll[i].name));
                }
            }
        }
    }
}
function closeAjaxWindow2() {
    var doc = document;
    if (window.parent != null) doc = window.parent.document;
    var d = doc.getElementById('soda_main_window');
    if (d != null) { doc.body.removeChild(d); }
    d = doc.getElementById('soda_layer_placeHolder');
    if (d != null) { doc.body.removeChild(d); }
    showSelect();
}
function closeAjaxWindow3() {
    var doc = document;
    //if (window.parent != null) doc = window.parent.document;
    var d = doc.getElementById('soda_main_window');
    if (d != null) { doc.body.removeChild(d); }
    d = doc.getElementById('soda_layer_placeHolder');
    if (d != null) { doc.body.removeChild(d); }
    showSelect();
}
function closeAjaxWindow() {
    var d = null;
    var s = null;
    d = document.getElementById('soda_window_content');
    if (m_SodaOpenMode == 1) {
        s = document.getElementById(m_SodaSourceContentId);
        if ((d != null) && (s != null)) {
            var content = d.innerHTML;
            d.innerHTML = '';
            s.innerHTML = content;
        }
    }
    //Animation
    d.innerHTML = ''; //<div align="center">Closing...</div>';
    closeAjaxWindowPre();
}
function closeAjaxWindowPre() {
    var d = document.getElementById('soda_main_window');
    var i = parseInt(d.style.height.replace('px', ''))
    i -= 80;
    if (i <= 0) {
        d.style.height = '1px';
        closeAjaxWindowPre2();
        //closeAjaxWindowPost();
    }
    else {
        d.style.height = i + 'px';
        resizeWindow();
        setTimeout("closeAjaxWindowPre()", 5);
    }
}
function closeAjaxWindowPre2() {
    var d = document.getElementById('soda_main_window');
    var i = parseInt(d.style.width.replace('px', ''))
    i -= 50;
    if (i <= 0) {
        d.style.width = '0px';
        d.style.height = '0px';
        closeAjaxWindowPre3();
        //closeAjaxWindowPost();
    }
    else {
        d.style.width = i + 'px';
        resizeWindow();
        setTimeout("closeAjaxWindowPre2()", 5);
    }
}
var m_Opacity = 75;
function closeAjaxWindowPre3() {
    var d = document.getElementById('soda_layer_placeHolder');
    if (d != null) {
        m_Opacity -= 20;
        var opacity = m_Opacity;
        d.style.opacity = (opacity / 100);
        d.style.MozOpacity = (opacity / 100);
        d.style.KhtmlOpacity = (opacity / 100);
        d.style.filter = "alpha(opacity=" + opacity + ")";
        if (m_Opacity <= 0) {
            m_Opacity = 75;
            closeAjaxWindowPost();
        }
        else {
            setTimeout("closeAjaxWindowPre3()", 2);
        }
    }
}
function closeAjaxWindowSimple() {
    var d = null;
    var s = null;
    d = document.getElementById('soda_window_content');
    if (m_SodaOpenMode == 1) {
        s = document.getElementById(m_SodaSourceContentId);
        if ((d != null) && (s != null)) {
            var content = d.innerHTML;
            d.innerHTML = '';
            s.innerHTML = content;
        }
    }
    closeAjaxWindowPost();
}
function closeAjaxWindowPost() {
    var d = null;
    d = document.getElementById('soda_main_window');
    if (d != null) { document.body.removeChild(d); }
    d = document.getElementById('soda_layer_placeHolder');
    if (d != null) { document.body.removeChild(d); }
    showSelect();
    if (m_AjaxRefreshWindowAfterClose) {
        window.location = window.location;
    }

}
function resizeWindow() {
    var d = document.getElementById('soda_layer_placeHolder');
    var pageWidth = '0px';
    var pageHeight = '0px';
    if (document.body && (document.body.scrollWidth || document.body.scrollHeight)) {
        pageWidth = document.body.scrollWidth + 'px';
        pageHeight = document.body.scrollHeight + 'px';
    }
    else if (document.body.offsetWidth) {
        pageWidth = document.body.offsetWidth + 'px';
        pageHeight = document.body.offsetHeight + 'px';
    }

    if (d != null) {
        //d.style.width = pageWidth;
        //d.style.height = pageHeight;
    }
    d = document.getElementById('soda_main_window');
    if (d != null) {
        d.style.marginLeft = '-' + parseInt(d.style.width.replace('px', '') / 2) + 'px';
        d.style.marginTop = '-' + parseInt(d.style.height.replace('px', '') / 2) + 'px';
    }
}
