// dynamically change the height of an embedded iFrame
  // tested in FF2/IE7 (Windows), FF2/Safari3 (Mac)
  // *NOTE: resizing the iframe doesnot work across multiple domains
  // *NOTE: it also does not work when iframe content is HTTPS and portal page is HTTP
  function resizeIframe(frameElement) {
    try {
        // find the height of the internal page
        var outer_height = frameElement.contentWindow.document.body.scrollHeight;
        if (outer_height > 0) {
            //change the height of the iframe
            frameElement.height = outer_height;
        }
        //document.title += '.';
        //alert("resizeIframe() works! outer_height="+outer_height);
    } catch(e) {
        //alert(e.message);
    }
    
}

var loggedIn=false;
var http_request = false;
//Call used for making request and updating the login link.
function makeRequest() {
    var prot = window.top.window.location.protocol;
    var host = window.top.window.location.host;
    var port = '';
    if(window.top.window.location.port != 80 && window.top.window.location != 443){
        port = ':'+window.top.window.location.port;
    }
    var server = prot + '//' + host + port;
    var url = prot + '//' + host + port +'/bcferries/getRemoteUser.jsp?ignorecache='+(Math.random() * 10000);
    
    if (window.top.window.XMLHttpRequest) { // Mozilla, Safari,...
        http_request = new XMLHttpRequest();
    } else if (window.top.window.ActiveXObject) { // IE
        http_request = new ActiveXObject("Microsoft.XMLHTTP");
    }
    http_request.onreadystatechange = setLoginLink;
    http_request.open('GET', url, true);
    http_request.send(null);
}

//Call used for making request and updating the login link.
function makeAppRequest(url) {
    var prot = window.top.window.location.protocol;
    var host = window.top.window.location.host;
    var port = '';
    if(window.top.window.location.port != 80 && window.top.window.location != 443){
        port = ':'+window.top.window.location.port;
    }
    var server = prot + '//' + host + port;
    
    if (window.top.window.XMLHttpRequest) { // Mozilla, Safari,...
        http_request = new XMLHttpRequest();
    } else if (window.top.window.ActiveXObject) { // IE
        http_request = new ActiveXObject("Microsoft.XMLHTTP");
    }
    http_request.onreadystatechange = setLoginLink;
    http_request.open('GET', url, true);
    http_request.send(null);
}

    //Callback invoked on Ajax completion.
    function setLoginLink() {
        if (http_request.readyState == 4) {
            if (http_request.status == 200) {
               var username=http_request.responseText;
               var prot = window.top.window.location.protocol;
               var host = window.top.window.location.host;
               var port = '';
               if(window.top.window.location.port != 80 && window.top.window.location != 443){
                   port = ':'+window.top.window.location.port;
               }
              // var server = prot + '//' + host + port;
                var server = 'https://' + host;
                var httpServer = 'http://' + host;
               if (username) {
                   window.top.document.getElementById("loginLink").innerHTML='<span id="header_logout"><a href="https://login.bcferries.com/pls/orasso/orasso.wwsso_app_admin.ls_logout?p_done_url='+server+'/bcfuserprofile" target="_top" title="Sign Out">Sign Out</a></span>';
                loggedIn=true;
               }else {
                var signinPanel = '<span id="header_signin">' +
                '<a href="'+httpServer+'/res/SigninCentre.html"  target="_top" title="Sign In Centre">Sign In</a>' +
                '<span id="header_signin_application">' + '</span>' + '</span>';
                window.top.document.getElementById("loginLink").innerHTML=signinPanel;
                loggedIn=false;
               }
           } else {
                //alert('There was a problem with the request.');
            }
        }
        if(typeof notifyLoginCheckComplete == 'function'){
            notifyLoginCheckComplete();
        }
    }



    
    
    function Navigate(choice) {
        var spot = choice.options[choice.selectedIndex].value;
        if (spot != 'choose') {
            location.href = spot;
        }
    }


var separator = '?';

function getQueryString()
{
    var qString = window.location.search.substring(1);
    
    if (qString == null || qString == '')
        separator = '';
        
    return qString;
}

function getTargetHostName()
{
    return 'www.bcferries.com';
}

function getContextName()
{
    return 'current_conditions';
}

function getRemoteTopPageName(localPageName)
{
    var remotePageNames = new Array();
    
    remotePageNames["at-a-glance.asp"] = "terminals.html";
    remotePageNames["webcams.asp"] = "webcams.html";
    remotePageNames["at-a-glance.asp#isn"] = "service_notices.html#isn";
    remotePageNames["maps.asp"] = "vessel_positions.html";
    remotePageNames["actualDepartures.asp"] = "actualDepartures.html";
    remotePageNames["sailingDetail.asp"] = "sailing_details.html";
    
    var retVal = remotePageNames[localPageName];
    
    if (retVal == null || retVal == '')
    {
        return '';
    }
    else
    {
        return retVal;
    }
        
    
}

function getLocalPageName()
{
    var sPath = window.location.pathname;

    var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);    
    
    if (sPath.lastIndexOf('isn') > 0)
    {
        sPage = sPage + '#isn';
    }
    
    return sPage;
}

function redirectToMarqui()
{
    var localPageName = getLocalPageName();
    
      
    var targetURL = 'https://' + getTargetHostName() + '/' + getContextName() + '/' + getRemoteTopPageName(localPageName) + separator + getQueryString();
    
    
    //window.location = targetURL;
    
    return targetURL;
}


// New Re-size iFrame

function doIframe(){
    o = document.getElementsByTagName('iframe');
    for(i=0;i<o.length;i++){
        if (/\bautoHeight\b/.test(o[i].className)){
            setHeight(o[i]);
            addEvent(o[i],'load', doIframe);
        }
    }
}

function setHeight(e){
    if(e.contentDocument){
        e.height = e.contentDocument.body.offsetHeight + 35;
    } else {
        e.height = e.contentWindow.document.body.scrollHeight;
    }
}

function addEvent(obj, evType, fn){
    if(obj.addEventListener)
    {
    obj.addEventListener(evType, fn,false);
    return true;
    } else if (obj.attachEvent){
    var r = obj.attachEvent("on"+evType, fn);
    return r;
    } else {
    return false;
    }
}

if (document.getElementById && document.createTextNode){
 addEvent(window,'load', doIframe); 
}

function writeIframeString(url)
    {
        var target = url;
        var extTarget = '';
        
        var qs = getQueryString();
        var hh = getHashString();
        
        if (qs != '')
        {
            target = target + '?' +  getQueryString();   
        }
        
        if (hh !=  '')
        {
            target = target + getHashString(); 
        }
        
        document.write('<iframe id="ifr" allowTransparency="true" frameborder="0" scrolling="auto"');
        document.write('src="' + target);
        document.write('"></iframe>');
 
    }
 
function getHashString()
{
    
    var hString = window.location.hash;
    
    if (hString == null || hString == '')
    {
        return '';
    }
    else
    {
        return hString;
    }
}

function popitup(v) { return true;}

function swapContent(image)
{
    var element = document.getElementById("image_rotator");
    element.style.backgroundImage = "url("+image+")";
    }

function MM_preloadImages() { //v3.0
var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

//declare needed variables for last modified and quickedit
//populated in CMS body pagestyle, used in /_includes/footer.html where applicable
var bcfWXEDIT="";
var bcfWXUPDATEDATE="";

<!--


//script to parse and layout the form
function layoutform(formID)
{
        
 //declare an array to hold labels
 labelArray = new Array();
        
 //pass the formID and declare the form
 mForm = document.forms[formID];
        
 //place the form HTML into a string
 mStr = (mForm.innerHTML);
 strLength = mStr.length;
        
 //We want to strip the 70%/30% from the HTML
        
 //Get the first part of the innerHTML
 indx = mStr.indexOf('<TR>');
 //innerHTM_1 = mStr.substr(0, mStr.indexOf('<TR>'));
 //innerHTM_2 = mStr.substr((mStr.indexOf('</TR>')+5),mStr.length);
 
 innerHTM_1 =  "<TABLE class=form width=100% align=center><TBODY>";
 innerHTM_2 = "<td " + mStr.substr(mStr.indexOf('v'),mStr.length);
 

 mForm.innerHTML = innerHTM_1 + innerHTM_2;

}

    function newImage(arg) {
        if (document.images) {
            rslt = new Image();
            rslt.src = arg;
            return rslt;
        }
    }

    function changeImages() {
        if (document.images && (preloadFlag == true)) {
            for (var i=0; i<changeImages.arguments.length; i+=2) {
                document[changeImages.arguments[i]].src = changeImages.arguments[i+1];
            }
        }
    }

    var preloadFlag = false;
    function preloadImages() {
        if (document.images) {
            // DISABLE THIS LEGACY CODE
            // nav_1_schedules_over = newImage("/images/nav-1_schedules_over.gif");
            // nav_1_fares_over = newImage("/images/nav-1_fares_over.gif");
            // nav_1_conditions_over = newImage("/images/nav-1_conditions_over.gif");
            // nav_2_int_res_over = newImage("/images/nav-2_int_res_over.gif");
            // nav_2_int_travel_over = newImage("/images/nav-2_int_travel_over.gif");
            // nav_2_int_about_over = newImage("/images/nav-2_int_about_over.gif");
            // nav_2_int_news_over = newImage("/images/nav-2_int_news_over.gif");
            // nav_2_int_business_over = newImage("/images/nav-2_int_business_over.gif");
            // nav_2_int_employment_over = newImage("/images/nav-2_int_employment_over.gif");
            // nav_2_int_contact_over = newImage("/images/nav-2_int_contact_over.gif");
            // nav_2_int_home_over = newImage("/images/nav-2_int_home_over.gif");
            // preloadFlag = true;
        }
    }

    function Navigate(choice) {
        var spot = choice.options[choice.selectedIndex].value;
        if (spot != 'choose') {
            location.href = spot;
        }
    }

    //script for promo pop-up
    function popUp() {
        for (i=0; i<popUp.arguments.length; i++) {
            if (i == 0) {
                var theUrl = popUp.arguments[i];
            } else if (i == 1) {
                var theName = popUp.arguments[i];
            } else if (i == 2) {
                var theWidth = popUp.arguments[i];
            } else if (i==3) {
                var theHeight = popUp.arguments[i];
            }
        }
        if (theWidth == null) {
            var theWidth="400";
        }
        if (theHeight == null) {
            var theHeight="450";
        }

        var theStuff = "resizable,scrollbars=yes,toolbar=no,menubar=no,status=yes,width=" + theWidth + ",height=" + theHeight;
        var newWindow = window.open(theUrl, theName, theStuff);
        newWindow.focus();
    }

    
    var xmlDoc, xmlObj, xmlItems; 
    //function loadXML(xmlFile) { 
    /* alert("loadXML being called!: " + xmlFile); xmlDoc.async="false"; xmlDoc.onreadystatechange=verify; xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); xmlDoc.load(xmlFile); xmlObj = xmlDoc.documentElement; */ 
    //load xml file 
    // code for IE if (window.ActiveXObject) { xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); xmlDoc.async=false; xmlDoc.load(xmlFile); xmlObj = xmlDoc.documentElement; 
    //xmlDoc.getElementsByTagName("newsitem").length verify("No. of news items: " + xmlObj.childNodes.length) } 
    // code for Mozilla, etc. else if (document.implementation &amp;&amp; document.implementation.createDocument) { xmlDoc = document.implementation.createDocument("","doc",null); xmlDoc.load(xmlFile); xmlObj = xmlDoc.documentElement; xmlDoc.onload=verify(xmlDoc) } 
    //display releases xmlItems = getNoItems(); buildTOC(0); } function verify(strMsg) { 
    // 0 Object is not initialized 
    // 1 Loading object is loading data 
    // 2 Loaded object has loaded data 
    // 3 Data from object can be worked with 
    // 4 Object completely initialized if (xmlDoc.readyState !=4) { alert("error loading xml file: " + strMsg); } 
    //} 
    
    
    
    function getNoItems() { 
        noItems = xmlDoc.getElementsByTagName("newsitem").length; return noItems;
    } 
    
    
function buildTOC(indx)
{
    strHTML = "";
    tblOpen  = "<table id='tblNews'>";
    tblClose = "</table>";
    tdTitle  = "<td class=newsTitle valign='top' width='120'>";
    tdBrief  = "<td class=newsBrief valign='top'>";//<h2>Title</h2>NewsContent</td>";
    indxMax  = indx+9;
    
    //alert ("first:" + indx + ", last: " + indxMax + ' items:' + xmlItems);
    
    if ( indxMax > xmlItems)
    { indxMax = xmlItems }
    
    for (var x = indx; x < indxMax; x++)
    {
        strPDF     = xmlDoc.getElementsByTagName("PDF")[x].text;
        strDate    = "<a href='http://www.bcferries.com/files/newspdfs/" + strPDF + "'>" + xmlDoc.getElementsByTagName("date")[x].text + "</a>";
        strTitle   = "<h2 class='tocTITLE'>" + "<a href='http://www.bcferries.com/files/newspdfs/" + strPDF + "'>" + xmlDoc.getElementsByTagName("title")[x].text + "</a>" + "</h2>";       strContent = xmlDoc.getElementsByTagName("desc")[x].text;
        
        tdDate    = tdTitle + strDate + "<td>";
        tdContent = tdBrief + strTitle + strContent + "</td>";
        
        tblRow   = "<tr>" + tdDate + tdContent + "</tr>";
        strHTML = strHTML + tblRow;
    }
    
    strHTML = tblOpen + strHTML + tblClose;
  
    buildNav(indx,indxMax)
    
    newsTOC.innerHTML = strHTML;

}
        
        
function buildNav(start,end)
{
      first = start + 1;
      last  = end;
      prev  = start-10; //0;
      
      if ( prev < 0)
      { prev = 0; }
      
      strList = "Listing " + first + " to " + last + " of " + xmlItems;
      strPrev = "[ <a href='javascript:buildTOC(" + prev + ")'><< Previous 10</a> ] ";
      strNext = " [ <a href='javascript:buildTOC(" + last + ")'>Next 10  >></a> ]";
      
      strNav = strPrev + strList + strNext;
      
      navigation.innerHTML = strNav;
}
        
        


function isLoggedIn() {
    return loggedIn;
}




