// Frame.js -- Must be added in iframe window

window.setInterval(publishHeight, 300);

var valid_hostFrameId  = "tools_fwf";
var frameId_Cookie = "__frameid";
var hostUrl_Cookie = "__hostUrl";
//var cookieExpOffset = 1*24*60*60*1000; //(days*24*60*60*1000)


function debugFrame() {
/*
    document.write("<br/>");

    document.write("window.location.href: " + window.location.href + "<br/>");

    var qs = parseQueryString(window.location.href);
    var frameId = getFrameId(qs);
    document.write("frameId2: " + frameId + "<br/>");

    document.write("getBodyHeight(): " + getBodyHeight() + "<br/>");
    document.write("getViewPortHeight(): " + getViewPortHeight() + "<br/>");
*/
}


function publishHeight()
{
    var qs = parseQueryString(window.location.href);

    var frameId = getFrameId(qs);

    if (frameId == null) return;

    var actualHeight = getBodyHeight();
    var currentHeight = getViewPortHeight();

    var hostUrl = getHostUrl(qs);

    hostUrl += "#";
    hostUrl += 'frameId=' + frameId;
    hostUrl += '&';
    hostUrl += 'height=' + actualHeight.toString();

    window.top.location = hostUrl;
}

function getFrameId(qs)
{
    var frameId = qs["frameId"];

    if (frameId != null && frameId.indexOf(valid_hostFrameId) == 0) {
        
//	var date = new Date();
//	date.setTime(date.getTime()+cookieExpOffset);
//	setCookie(frameId_Cookie, frameId, date, "/", document.domain);
	setCookie(frameId_Cookie, frameId, "", "/", document.domain);
    }

    else {
	frameId = getCookie(frameId_Cookie);
    }

    return (frameId == valid_hostFrameId) ? frameId : null;
}

function getHostUrl(qs)
{
    var hostUrl = qs["host"];

    if (hostUrl != null && hostUrl.indexOf('http') == 0) {
//	var date = new Date();
//	date.setTime(date.getTime()+cookieExpOffset);
//	setCookie(hostUrl_Cookie, hostUrl, date, "/", document.domain);
	setCookie(hostUrl_Cookie, hostUrl, "", "/", document.domain);
    }
    else {
	hostUrl = getCookie(hostUrl_Cookie);
    }

    return hostUrl;
}

function getBodyHeight()
{
    var height;
    var scrollHeight;
    var offsetHeight;

    if (document.height)
    {
        height = document.height;
    }
    else if (document.body)
    {
        if (document.body.scrollHeight)
        {
            height = scrollHeight = document.body.scrollHeight;
        }
        if (document.body.offsetHeight)
        {
            height = offsetHeight = document.body.offsetHeight;
        }

        if (scrollHeight && offsetHeight)
        {
            height = Math.max(scrollHeight, offsetHeight);
        }
    }

    return (isIE()) ? height+15 : height;
}

function getViewPortHeight()
{
    var height = 0;

    if (window.innerHeight)
    {
        height = window.innerHeight - 18;
    }
    else if ((document.documentElement) && (document.documentElement.clientHeight))
    {
        height = document.documentElement.clientHeight;
    }
    else if ((document.body) && (document.body.clientHeight))
    {
        height = document.body.clientHeight;
    }

    return height;
}

function parseQueryString(url)
{
    url = new String(url);
    var queryStringValues = new Object();
    var querystring = url.substring((url.indexOf('?') + 1), url.length);

    querystring = querystring.substring(querystring.indexOf('frameId=')); 

    var querystringSplit = querystring.split('&');

    for (i = 0; i < querystringSplit.length; i++)
    {
        var pair = querystringSplit[i].split('=');

	var name = pair[0];
        var value = pair[1];

        queryStringValues[name] = value;
    }

    return queryStringValues;
}

function getCurrentDocumentURL()
{
    return document.URL;	
}

function isIE() {
	if (navigator.appName.indexOf("Microsoft") == 0) {
		return true;
	}
	return false;
}

/**
 * Sets a Cookie with the given name and value.
 *
 * name       Name of the cookie
 * value      Value of the cookie
 * [expires]  Expiration date of the cookie (default: end of current session)
 * [path]     Path where the cookie is valid (default: path of calling document)
 * [domain]   Domain where the cookie is valid
 *              (default: domain of calling document)
 * [secure]   Boolean value indicating if the cookie transmission requires a
 *              secure transmission
 */
function setCookie(name, value, expires, path, domain, secure) {
    document.cookie= name + "=" + value +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

/**
 * Gets the value of the specified cookie.
 *
 * name  Name of the desired cookie.
 *
 * Returns a string containing value of specified cookie,
 *   or null if cookie does not exist.
 */
function getCookie(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    } else {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1) {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

/**
 * Deletes the specified cookie.
 *
 * name      name of the cookie
 * [path]    path of the cookie (must be same as path used to create cookie)
 * [domain]  domain of the cookie (must be same as domain used to create cookie)
 */
function deleteCookie(name, path, domain) {
    if (getCookie(name)) {
        document.cookie = name + "=" +
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}