﻿var inframe_config = {
   /*******************************************
    ********************************************    
    BEGIN CONFIGURATION SETTINGS
    ********************************************
    *******************************************/

    SHARED_DOMAIN: 'arma.org',
    OFFSET_HEIGHT: 50,
    MERGE_TITLES: 1,    // 0=None, 1=Prepend iframe title, 2=Append iframe title
    MERGE_SEPARATOR: '-'

    /*******************************************
    ********************************************    
    END CONFIGURATION SETTINGS
    ********************************************
    *******************************************/
};

if (!window.addLoadEvent) {
    var addLoadEvent = function(fp) {
        if (window.addEventListener) window.addEventListener("load", fp, false);
        else if (window.attachEvent) window.attachEvent("onload", fp);
        else {
            var oldQueue = window.onload ? window.onload : function() { };
            window.onload = function() {
                oldQueue();
                fp();
            }
        }
    };
}

var inframe_local = {
    xssRelaxed: false,
    childState: 0,
    hostState: 0,
    uniqueId: Math.random()
};

var inframe_relaxXss = function() {
    try {
        if (inframe_local.xssRelaxed) return;
        document.domain = inframe_config.SHARED_DOMAIN;
        inframe_local.xssRelaxed = true;
    }
    catch (ex) { }
};

var inframe_findChildElement = function(childName) {
    var childFrame = document.getElementById(childName);
    if (childFrame) return childFrame;

    var nameMatches = document.getElementsByName(childName);
    var i = nameMatches.length;
    while (i--) {
        if (nameMatches[i].nodeName == 'IFRAME') return nameMatches[i];
    }

    return null;
};

var inframe_setup = function() {
    if (!inframe_local.xssRelaxed) inframe_relaxXss();

    if (self != top) {
        if (window.parent) {
            setTimeout('inframe_setupChild()', 350);
        }
    }
    else {
        inframe_setupHost();
    }
};

var inframe_setupChild = function() {
    if (!inframe_local.xssRelaxed) inframe_relaxXss();
    if (!window.parent) return;

    try {
        this.childState = 1;
        var rwin = window.parent;
        var inframe_remote = rwin.inframe_local;
        var remote_config = rwin.inframe_config;

        if (inframe_remote.hostState != 2) {
            setTimeout('inframe_setupChild()', 200);
        }

        var elref = rwin.inframe_findChildElement(self.name);
        if (!elref) return;

        var newHeight = (parseInt(document.body.scrollHeight) + Math.max(remote_config.OFFSET_HEIGHT, inframe_config.OFFSET_HEIGHT));
        if (elref.style) elref.style.height = newHeight;
        elref.height = newHeight;
        elref.setAttribute('height', newHeight);
        document.body.style.overflow = 'hidden';


        if (remote_config.MERGE_TITLES == 1) {
            window.parent.document.title = (self.document.title + ' ' + remote_config.MERGE_SEPARATOR + ' ' + window.parent.document.title);
        }
        else if (remote_config.MERGE_TITLES == 2) {
            window.parent.document.title += (' ' + remote_config.MERGE_SEPARATOR + ' ' + self.document.title);
        }

        inframe_local.childState = 2;
        inframe_setChildLinkTarget('_parent');
    }
    catch (ex) {
        if (ex.description == "Permission denied") {
            // parent has not relaxed...
            setTimeout('inframe_setupChild()', 200);
        }
    }
};

var inframe_setupHost = function() {
    inframe_local.hostState = 2;
};


var inframe_setChildLinkTarget = function(newTarget)
{
    if (self != top) {
        if (window.parent) {
            try
	    {
	        var rwin = window.parent;
		var hostHref = rwin.location.toString();

	        var linkList = document.getElementsByTagName('A');

	        for(var i=0; i<linkList.length; i++)
	        {
	            var lnk = linkList[i];
                if(lnk.href != null && lnk.href.toLowerCase().indexOf('dynamicpage.aspx') != -1 && hostHref.toLowerCase().indexOf('dynamicpage.aspx') != -1 && lnk.href.toLowerCase().indexOf('javascript:') == -1)
				{
					var dpPos = hostHref.toLowerCase().indexOf('dynamicpage.aspx');
					var linkBase = hostHref.substring(0, dpPos);
					var oldDpPos = lnk.href.toLowerCase().indexOf('dynamicpage.aspx');

					lnk.href = linkBase + unescape(lnk.href.substring(oldDpPos));
					if(lnk.href.toLowerCase().indexOf('url_success=') != -1)
					{
						var uPos = lnk.href.toLowerCase().indexOf('url_success=') + 12;
						var newHref = lnk.href.substring(0, uPos) + encodeURIComponent(lnk.href.substring(uPos));
						lnk.href = newHref;
					}

					lnk.target = newTarget;
				}
				else if(lnk.href && lnk.href.toLowerCase().indexOf('http') == 0 && lnk.href.toLowerCase().indexOf('javascript:') == -1 && lnk.target.toLowerCase() != "_blank")
				{
					lnk.target = newTarget;
				}
	        }
	    }
	    catch(er) { }
        }
    }
};

addLoadEvent(inframe_setup);


function calcHeight() {}

function change_parent_url(url)
{
	document.location=url;
}

function changeIframeUrl(url)
{
    document.getElementById('ewebFrame').src = url;
}
