
// OnLoad Function called by each page
// Highlights current section in navigation bar and makes its subsections visible
var pageBranchPath = new String();
function started(){
    var displaynav      = strFilePath;

    // preload images for naviation
    var preloadImg = new Array()
    preloadImg[0] = new Image;preloadImg[0].src="/portal_images/furniture/square";
    preloadImg[1] = new Image;preloadImg[1].src="/portal_images/furniture/arrow_down";
    preloadImg[2] = new Image;preloadImg[2].src="/portal_images/furniture/arrow";
    
    if(screen.width < 1024 && screen.height < 720){resizeFlash();resizeImg();}    

    if(pageBranchPath.length>0){openBranch(pageBranchPath);}
    if(document.getElementById("sectionNewsStories") || document.getElementById("changeheight")){padOutPage();}
}


// Functions for search for a business pages
// Sets value of business category selected 
function change(pageURL){ 
    var x=document.forms.bussearch.select1;
    var y=x.options[x.selectedIndex].text;
    document.getElementById("catVal").value=document.forms.bussearch.select1.value;
    document.getElementById("busName").value=y;
    document.getElementById("bussearch").submit();
}

// Sets value of business selected
function details(pageURL){
    document.getElementById("busVal").value=document.forms.bussearch.select2.value;
    document.forms.bussearch.submit();
}
// Back button form submission
function goback(pageURL){document.forms.bussearch.submit();}
// End of Functions for search for a business pages

//Site Search functions to submit page
function submitsearchnext(){
    document.forms['mainsearch'].action = './?view=Search+results&page=next';
    document.forms.mainsearch.submit();
}
function submittextsearchnext(){
    document.forms['mainsearch'].action = './?view=Text_Search+results&page=next';
    document.forms.mainsearch.submit();
}

function submitregisteredsearchnext(){
    document.forms['mainsearch'].action = './?view=Registered+Search+results&page=next';
    document.forms.mainsearch.submit();
}

function submitsearchprev(){
    document.forms['mainsearch'].action = './?view=Search+results&page=prev'  
    document.forms.mainsearch.submit();
}

function submitcoursesearchprev(){
    document.forms['search'].action = './?page=Previous'  
    document.forms.search.submit();
}

function submitcoursesearchgoToPage(intPageNum){
    document.forms['search'].action = './?page=' + intPageNum;
    document.forms.search.submit();
}

function submitcoursesearchnext(){
    document.forms['search'].action = './?page=Next'  
    document.forms.search.submit();
}

function submittextsearchprev(){
    document.forms['mainsearch'].action = './?view=Text_Search+results&page=prev'  
    document.forms.mainsearch.submit();
}

function submitregisteredsearchprev(){
    document.forms['mainsearch'].action = './?view=Registered+Search+results&page=prev'  
    document.forms.mainsearch.submit();
}

function changesite(){
    //alert(document.getElementById("searchval").value);
    document.getElementById("srch_branchUrl").value=document.getElementById("searchval").value;
}
//End of Site Search functions to submit page


//used to Hide display items on some pages
//rows is the number of rows to be hidden
//theid is the idea of the entry
function hideonlist(theno,theid,third,rows){
    for (i = 1; i < theno+1; i++){ 
        for (j = 1; j < rows+1; j++){
             document.getElementById("row"+j+i).style.display="none";      
            }       
        }
    display(third);
}

//Used to show items on page
function showall(no,third,rows)
{
    for (i = 1; i < no+1; i++){   
        for (j = 1; j < rows+1; j++){
        document.getElementById("row"+j+i).style.display="block"; 
        }
    }
 display(third);
}



function display(title){ 
    if(document.getElementById("title").style.display == 'none'){
        document.getElementById(title).style.display="block";
    }
    else {    document.getElementById(title).style.display="none";    }
}


function Radio(branch){document.getElementById("srch_branchUrl").value=branch;}

function Portal(val){
    if(document.getElementById(val)!=null){
        if(document.getElementById(val).style.display == 'none'){ 
            document.getElementById(val).style.display="block";
            val=val+1;
            if(document.getElementById(val)!=null){
                document.getElementById(val).innerHTML="<img src='/portal_images/furniture/arrow_down' alt='arrowdown image - showing subpages'>"; 
            }
            val=val+1; 
            if(document.getElementById(val)!=null){
                document.getElementById(val).className="mainnavonstateborder";
            }
        }
        else{
            if(document.getElementById(val)!=null){ 
                document.getElementById(val).style.display="none"; 
            }
            val=val+1;
            if(document.getElementById(val)!=null){
                document.getElementById(val).innerHTML="<img src='/portal_images/furniture/arrow' alt='arrow image - subpages'>";
                document.getElementById(val).className="";
            }
        } 
    } 
}

/*
 * The Following Function has been written to ensure
 * that the page content area is at least the same height as the menu
 * this should mean that the page footer is displayed below the menu instead
 * of underneath as has happened on some pages.
 *
 */
var winLoad;
function padOutPage(){
    // if the page has not loaded yet then wait for it to load before running
    if(window.onload == null){
        winLoad = window.onload;
        window.onload = function(){if(winLoad){winLoad()}padOutPage();}
        return false;
    }
    var contentArray = new Array();
    var menuDivID = "navigationPage";
    var pageLength = 0, arrayCounter = 0;

    menuDiv = document.getElementById(menuDivID);
    
    contentArray[arrayCounter++] = document.getElementById("changeheight");
    contentArray[arrayCounter++] = document.getElementById("sectionNewsStories");
    contentArray[arrayCounter++] = document.getElementById("maincontent");
    for(var i=0;i<contentArray.length;i++){
        if(contentArray[i]){pageLength += contentArray[i].clientHeight;
        }
    }
    
    if(menuDiv){
        //if the menu height is greater then the page content then
        if(menuDiv.clientHeight > pageLength){
            //window.status = "Padding out";
            //work out how much needs to be added to the height
            intHeigthDiff = menuDiv.clientHeight - pageLength;
            // try to add the difference to the first div in array            
            for(var i=0;i<contentArray.length;i++){if(contentArray[i]){contentArray[i].style.height = contentArray[i].clientHeight + intHeigthDiff + "px";window.status = "Padding out" ;setTimeout("window.status = '';",3000); return false}}
        }
    }
}

function dummyFunction(){}/* this is attached to <a href="javascript:dummyFunction()" onclick=""></a>*/

function showMenu(strDivID, trgElemnt){
    thisItem = document.getElementById(strDivID);
    if(thisItem){
        //alert(trgElemnt.className);
        if(thisItem.style.display == ""){thisItem.style.display = "block";trgElemnt.innerHTML = "<img src=\"/portal_images/furniture/arrow_down\" alt='sub menu' />";}
        else{thisItem.style.display = "";trgElemnt.innerHTML = "<img src='/portal_images/furniture/arrow' alt='sub menu' />";}
    }
}

function openBranch(strBranchID){
    foundMenuItem = false;
    if(document.getElementById(strBranchID) ){ //if the whole branch exists
        // ensure you fill whole container by changing class name of xxx:link
        if(document.getElementById(strBranchID+":link")){
            document.getElementById(strBranchID+":link").className += " selectedItem";
        }
        if(strBranchID != "Home" && strBranchID != "BLN" && document.getElementById(strBranchID+":link")){document.getElementById(strBranchID+":link").style.width = calcItemWidth(document.getElementById(strBranchID+":link").className);}

        // get element to change and make it visible!
        trgElemnt = document.getElementById(strBranchID);
        trgElemnt.style.display = "block";
        foundMenuItem = true;
    }
    else if(document.getElementById(strBranchID+":link")){//trgElemnt = 
        document.getElementById(strBranchID+":link").className += " selectedItem";
        document.getElementById(strBranchID+":link").style.width = calcItemWidth(document.getElementById(strBranchID+":link").className);

        document.getElementById(strBranchID+":link").outerHTML = "<div style=\"border-bottom:1pt solid white;width:151px;\">" + document.getElementById(strBranchID+":link").outerHTML + "</div>";
        document.getElementById(strBranchID+":link").style.borderBottomWidth = "0px"
        foundMenuItem = true;
    }
    openParentBranches(strBranchID)
    
    if(!foundMenuItem){findParentMenuItem(strBranchID);}
//alert(document.getElementById(strBranchID+":link").outerHTML)
}

function calcItemWidth(strClassesName){
    // if it is IE 5
    if(navigator.appVersion.lastIndexOf("MSIE 5")>0){
        return "145px";
        //if(strClassesName.indexOf("level1")>=0){return "149px";}
        //else if(strClassesName.indexOf("level2")>=0){return "145px";}
        //else if(strClassesName.indexOf("level3")>=0){return "145px";}
        //else if(strClassesName.indexOf("level4")>=0){return "145px";}
        //else if(strClassesName.indexOf("level5")>=0){return "145px";}
    }
    else {
        if(strClassesName.indexOf("level1")>=0){return "146px";}
        else if(strClassesName.indexOf("level2")>=0){return "139px";}
        else if(strClassesName.indexOf("level3")>=0){return "136px";}
        else if(strClassesName.indexOf("level4")>=0){return "134px";}
        else if(strClassesName.indexOf("level5")>=0){return "131px";}
    }
}

function findParentMenuItem(strBranchID){
    pathArray = strBranchID.split(":");
    // if the last URL part is BLN or businessportal then ignore it
    //alert(buildBranchPath(pathArray,pathArray.length-1))
    if(buildBranchPath(pathArray,pathArray.length-1) == "businessportal" || buildBranchPath(pathArray,pathArray.length-1) == "Home" || buildBranchPath(pathArray,pathArray.length-1) == "BLN"){return;}
    if(document.getElementById(buildBranchPath(pathArray,pathArray.length-1)+":link")){
        strBranchID = buildBranchPath(pathArray,pathArray.length-1);
        document.getElementById(strBranchID+":link").className += " selectedItem";
        document.getElementById(strBranchID+":link").style.width = calcItemWidth(document.getElementById(strBranchID+":link").className);
        document.getElementById(strBranchID+":link").outerHTML = "<div style=\"border-bottom:1pt solid white;width:151px;\">" + document.getElementById(strBranchID+":link").outerHTML + "</div>";
        document.getElementById(strBranchID+":link").style.borderBottomWidth = "0px"
    }
    else if(document.getElementById(buildBranchPath(pathArray,pathArray.length-2)+":link")){
        strBranchID = buildBranchPath(pathArray,pathArray.length-2);
        document.getElementById(strBranchID+":link").className += " selectedItem";
        document.getElementById(strBranchID+":link").style.width = calcItemWidth(document.getElementById(strBranchID+":link").className);
        document.getElementById(strBranchID+":link").outerHTML = "<div style=\"border-bottom:1pt solid white;width:151px;\">" + document.getElementById(strBranchID+":link").outerHTML + "</div>";
        document.getElementById(strBranchID+":link").style.borderBottomWidth = "0px"
    }
}

function buildBranchPath(arrBranchPath, intRow){
    var outtxt = "";
    if(intRow>arrBranchPath.length){intRow = arrBranchPath.length;}
    for(var i=0;i<intRow;i++){
        outtxt += (i==0?"":":") + arrBranchPath[i];
    }
    return outtxt;
}
function openParentBranches(strBranchID){
    //window.status = strBranchID;

    //if contains space then branch and not root item
    if(strBranchID.lastIndexOf(":")>0){
        // split each section of the menu into an array for iteration through
        branchArray = strBranchID.split(":");
        // initialise the branchpath string
        strBranchPath = ""; 
        // loop through each element in array building up the branch path
        for(var i=0;i<branchArray.length;i++){
            strBranchPath += (i>0?":":"") + branchArray[i];
            trgElemnt = document.getElementById(strBranchPath)
            // does it have a submenu?
            if(document.getElementById(strBranchPath + ":link")){ 
                // if first level item and not selected then highlight
                if(i==1 && document.getElementById(strBranchPath + ":link").className.indexOf("selectedItem")<1){document.getElementById(strBranchPath + ":link").className += " level1Selected";}
                if(document.getElementById(strBranchPath + ":link").firstChild.className.indexOf("menuExpander")>-1){document.getElementById(strBranchPath + ":link").firstChild.innerHTML = "<img src=\"/portal_images/furniture/arrow_down\" />";}
            }
            if(trgElemnt){trgElemnt.style.display = "block";}
        }
    }
}


/*
The following function has been designed to resize flash images when the users screen size is smaller than 1024 * 768
Normally the images should be 238px*238px but at 800*600 the images should be reduced to 167px*167px

Resizes flash objects who have resize set as their class
*/
function resizeFlash(){
    if(document.getElementsByTagName("object").length>0){
        // Flash objects found
        // loop for each object and correct size
        objFlash = document.getElementsByTagName("object")
        for(var i=0;i<objFlash.length;i++){
            // if the flash images are to be resized then their class will be set as such
            if(objFlash[i].className=="resize"){
                objFlash[i].width = "167";
                objFlash[i].height = "167";
            }
            // this resizes features on sub section pages that are
            else if(objFlash[i].className=="resizeSideImg"){
                objFlash[i].width = "130";
                objFlash[i].height = "130";
            }
        }
    }return;
}

/*
The following function has been designed to resize flash images when the users screen size is smaller than 1024 * 768

Resizes images who have resize set as their class
*/
function resizeImg(){


    self.status = "changing image size";
    if(document.getElementsByTagName("img").length>0){
        // Flash objects found
        // loop for each object and correct size
        objImg = document.getElementsByTagName("img");
        for(var i=0;i<objImg.length;i++){
            // if the images are to be resized then their class will be set as such
            //if(objImg[i].className=="resize"){
             //   objImg[i].width = "167";
            //    objImg[i].height = "167";
            //}
            if((screen.width  < 1024)&&(objImg[i].className=="resizeTxt")){

                objImg[i].width = "165";
                objImg[i].height = "165";
            }
            if((screen.width  >= 1024)&&(objImg[i].className=="resizeTxt")){

                objImg[i].width = "228";
                objImg[i].height = "228";
            }

            if((screen.width  < 1024)&&(objImg[i].className=="resizeTxtWrap")){

                objImg[i].width = "80";
                objImg[i].height = "80";
            }
            if((screen.width  >= 1024 )&&(objImg[i].className=="resizeTxtWrap")){

                objImg[i].width = "160";
                objImg[i].height = "160";
            }
        }
    }
    self.status = "";
    return;
}

function displayProps(obj, objType){
    txt = "<table>";
    for (n in obj){
        if(objType){
            if(obj[n] == objType){txt += "<tr><td>" + (n.hasOwnProperty("Name")? " obj " : "") + n + "</td><td>" + obj[n] + "</td></tr>\n";}
        }
        else{txt += "<tr><td>" + (n.hasOwnProperty("Name")? " obj " : "") + n + "</td><td>" + obj[n] + "</td></tr>\n";}
    }
    txt += "</table>";
    winPref = "toolbar=no, status=no, scrollbars=yes, resizeable=yes, menubar=no, location=no, height=300,width=300"
    propWin = window.open(null, "_blank", winPref);
    propWin.document.write(txt);

}

function showImage(imgID){

    img2Open = document.getElementById(imgID);
    if(img2Open){
	
        tempImg = new Image;
        tempImg.onload = function (){
            winPref = "toolbar=no, status=no, scrollbars=no, resizeable=yes, menubar=no, location=no, height="+ (tempImg.height+30) + ",width="+ (tempImg.width)
            imgWin = window.open(null, "_blank", winPref)
            imgWin.document.write("<html><head></head><body style='text-align:center;padding:0;margin:0;'><img src='" + this.src+ "' /><br /><input type='button' value='close' style='font-weight:0.8em;text-decoration:none;width:100%;text-align:center' onclick='javascript:window.close();' /></body></html>");
            //imgWin.document.focus();
        }
        tempImg.src = img2Open.src;
        self.status = "height: " + tempImg.height + "; width: "+tempImg.width;
        
        return false;
    }
}


//for photolibrary text version
function showImageTxt(img){

 tempImg = new Image;
        tempImg.onload = function (){
        winPref = "toolbar=no, status=no, scrollbars=no, resizeable=yes, menubar=no, location=no, height="+ (tempImg.height+30) + ",width="+ (tempImg.width)
        imgWin = window.open(null, "_blank", winPref)
        imgWin.document.write("<html><head></head><body style='text-align:center;padding:0;margin:0;'><img src='" + this.src+ "' /><br /><input type='button' value='close' style='font-weight:0.8em;text-decoration:none;width:100%;text-align:center' onclick='javascript:window.close();' /></body></html>");

	}
        tempImg.src = img;
        self.status = "height: " + tempImg.height + "; width: "+tempImg.width;
        
        return false;
}