
// Invoke ResizeParent if height or width is found in query string
// Used by ShowcaseFrameResize.htm from within HPSF
try {
    var height = getQueryVariable('height');
    var width = getQueryVariable('width');
    if (height != 'no' ||
        width != 'no') {
        ResizeParent(width, height);
        document.write = function() { };
    }
} catch (exception) { }

// Called in head of hpsf
function BeginiFrameResize(resizeUrl) {
    var url = unescape(resizeUrl);
    Event.observe(window, 'load', function() {
        url += (url.indexOf('?') != -1 ? '&' : '?');
        url += 'height=' + getCurrentHeight();
        url += '&width=' + getCurrentWidth();
        $$('iframe').each(function(item) {
            item.src = url;
        });
    });
}

// Called in body of hpsf
function EndiFrameResize(resizeUrl) {
    var url = resizeUrl;
    url += (url.indexOf('?') != -1 ? '&' : '?');
    url += 'height=' + getCurrentHeight();
    url += '&width=' + getCurrentWidth();
    createHPSFiFrame('hpsfHiddeniFrame', url, '', '0px', '0px', '0', 'no');
}

// Called from proxy page, ShowcaseFrameResize
function ResizeParent(width, height) {
    try {
        if ((!!width || !!height) &&
            parent.parent != parent) {
            parent.parent.parent.ShowcaseFrameResize(width, height);
        }
    } catch (exception) { }
}

// Called in reseller web page
function ShowcaseFrameResize(width, height) {
    try {
        var iframe = document.getElementsByTagName('iframe')[0];

        if (iframe) {
            if (!!width) {
                iframe.style.width = width + 'px';
            }
            if (!!height) {
                iframe.style.height = height + 'px';
            }
        }
    } catch (exception) {
    }
}

function createHPSFiFrame(id, url, HPSFProxyPageUrl, height, width, border, scrolling) {
    var h = getQueryVariable('height');
    var w = getQueryVariable('width');
    if (h == 'no' &&
        w == 'no') {
        var resizeUrl;
        if (!!url &&
            !!HPSFProxyPageUrl) {
            resizeUrl = HPSFProxyPageUrl;
        }
        else {
            var resizeUrl = getQueryVariable('resizeUrl');
            if (!!resizeUrl &&
            resizeUrl == 'no') {
                resizeUrl = document.location.toString();
            }
        }
        if (!!resizeUrl) {
            url += (url.indexOf('?') != -1 ? '&' : '?');
            url += 'resizeUrl=';
            url += escape(resizeUrl);
        }

        var style = '';
        if (!!width) {
            style += 'width: ' + (isNaN(width) ? width : width + 'px') + '; ';
        }
        if (!!height) {
            style += 'height: ' + (isNaN(height) ? height : height + 'px') + '; ';
        }
        document.write('<iframe' +
            (!!id ? ' id = "' + id + '"' : '') +
            (!!id ? ' name = "' + id + '"' : '') +
            ' src = "' + url + '"' +
            ' style = "' + style + '"' +
            ' frameborder = "' + (!!border ? border : '0') + '"' +
            ' scrolling = "' + (!!scrolling ? scrolling : 'no') + '"' +
            '></iframe>');
    }
}

function getCurrentHeight() {
    return $$('body')[0].getHeight();
    }

function getCurrentWidth() {
    return $$('body')[0].getWidth();
}

function getQueryVariable(variable) {
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i = 0; i < vars.length; i++) {
        var pair = vars[i].split("=");
        if (pair[0] == variable) {
            return pair[1];
        }
    }
    return "no";
}

function SetTargetOfForm(target) {
    document.forms[0].target = target;
}

Event.observe(window, 'load', function() {
    if (!!parent.SetHpsfHeight) {
        parent.SetHpsfHeight(getCurrentHeight());
    }
});
