// JavaScript Document

/* ***********************************************
    loads url in iframe, transfers body content into div
    provides defaults for iframe and display div ID's 
    also supports use with multiple iframes and divs
    optional message for loading in display div 
    supports functions to be called once the div has been populated with new content 
    function in iframed document can be invoked should some operations need to be performed from there 
 ********************************************* */
function LoadPage(URL, CookieName, LoadingMessage, iFrameID, DivID) {
    // defaults for iframe, display div
    iFrameID       = iFrameID || 'buffer'; 
    DivID          = DivID || 'display'; 
    LoadingMessage = LoadingMessage || false;
    CookieName     = CookieName || false;
  
    if ( window.frames[iFrameID] ) {
        // Could use location.replace method if you do not want back button to load previous iframe url 
        // window.frames[ifrmId].location.replace(url);
        window.frames[iFrameID].location = URL;
        var isLayer = document.getElementById ? document.getElementById(DivID) : null;
        if (isLayer && LoadingMessage) { // Option to display message while retrieving data 
            isLayer.innerHTML = LoadingMessage;
            isLayer.style.display = 'block'; 
        }
        document.title = window.frames[iFrameID].document.title;
        if (CookieName) { document.cookie = CookieName + '=' + URL; } 
        return false;
    } 
    return true; // other browsers follow link
}


/* ***********************************************
 called onload of iframe 
 displays body content of iframed doc in div
 checks for and invokes optional functions in both current document and iframed document 
 ********************************************* */
function SyncPage(CookieName, iFrameID, DivID, fp) {
    // defaults for iframe, display div
    iFrameID   = iFrameID || 'buffer'; 
    DivID      = DivID || 'display'; 
    CookieName = CookieName || false;
    
    var isLayer = document.getElementById ? document.getElementById(DivID) : null;
    if ( window.frames[iFrameID] && isLayer ) {
        isLayer.innerHTML = window.frames[iFrameID].document.body.innerHTML;
        isLayer.style.display = 'block'; 

        // when using with script, may have some operations to coordinate
        // function in current doc or iframed doc (doOnIframedLoad)
        if ( typeof fp == 'function' ) {
            fp();
        }
        
        // Demonstrated in tooltip demo
        if ( typeof window.frames[iFrameID].doOnIframedLoad == 'function' ) {
            window.frames[iFrameID].doOnIframedLoad();
        }
        document.title = window.frames[iFrameID].document.title;
        if (CookieName) { document.cookie = CookieName + '=' + window.frames[iFrameID].location; } 
    }
}


/* ***********************************************
 User to write a message in a specific object
 in the HTML tag (normally <div> or <span>. 
 Maybe work with others types.
 ********************************************* */
function WriteMessage(TheObj, TheMessage)
{
  document.getElementById(TheObj).innerHTML = TheMessage
}


/* ***********************************************
 Used to never allow page to be framed
 ********************************************* */
function NeverFramed()
{
 if (self != top)
 {
   top.location.replace(document.location.href);
 }
}


/* ***********************************************
 Used to never allow page to be OUT framed
 ********************************************* */
function NeverOutFramed()
{
 if (self == top)
 {
  document.cookie = 'currentPage=' + document.location.href;
  top.location.replace('index.php');
 }
}

/* ***********************************************
 Used to read a Cookie
 ********************************************* */
function readCookie(cookieName) 
{
  cookie_array = document.cookie.split ("; ");
  for (x=0; x < cookie_array.length; x++) 
   {
     cookieParts_array = cookie_array[x].split("=");
     if (cookieParts_array[0] == cookieName) 
	{
	 return cookieParts_array[1];
        } // IF
   } // FOR
  return null;
} // function readCookie
