//**************************************************************************
//**  System    : ikmp
//**  Purpose   : Common java script routines used by IKMP
//**
//**  Name                                Date       Purpose
//**  ----------------------------------- ---------- ------------------------ 
//**  P. Downer  Spherion Group Ltd       07-07-03   Initial Version
//**  C. Brunini SAGE Computing Services  07-07-03   Added additional functions for Oracle Portal implmentation
//**  R. Tindall SAGE Computing Services  11May2004  Alter for new layout - subtopiciframe removed
//**  R. Tindall SAGE Computing Services  28May2004  Add support for ikmp_events_portlet
//**
//**************************************************************************/

// constants
var cnHeaderButtonHeight = 145;
var cnHeaderNoButHeight= 113;
var cnHeaderSortHeight = 26;
var gPreloadFlag = false;


//**************************************************************************
//**  Description : Determines if the Client browser can run this web side.
//**		    The browser must support ie5.5 and netscape 6 and above.
//**                This is determined by checking the getElementById can be used.
//**                If the browser does not support this method then the page is redirected
//**                to a url passed into this function.
//**  Arguments   : url - The url to redirect too if no broowser support is detected
//**
//**  Name                                Date       Purpose
//**  ----------------------------------- ---------- ------------------------ 
//**  C. Brunini SAGE Computing Services  07-07-03   Initial Version
//**
//**************************************************************************/
function checkSupportedBrowser(url) {
     // must use browser ie 5 and up or netscape 6 and above
     // in other words it supports the getElementById method
     if (!document.getElementById)
     {
	document.location.href=url;
     }        
}


//**************************************************************************
//**  Description : Create a image image, used for prloading images
//**  Arguments   : arg - image name and URL
//**
//**  Name                                Date       Purpose
//**  ----------------------------------- ---------- ------------------------ 
//**  P. Downer  Spherion Group Ltd       07-07-03   Initial Version
//**
//**************************************************************************/
function newImage(arg) {
	if (document.images) {
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}

//**************************************************************************
//**  Description : Perform the rollover functionality by swaping out the images
//**  Arguments   : n/A
//**
//**  Name                                Date       Purpose
//**  ----------------------------------- ---------- ------------------------ 
//**  P. Downer  Spherion Group Ltd       07-07-03   Initial Version
//**
//**************************************************************************/
function changeImages() {
	if (document.images && (gPreloadFlag == true)) {
		for (var i=0; i<changeImages.arguments.length; i+=2) {
			document[changeImages.arguments[i]].src = changeImages.arguments[i+1];
		}
	}
}

//**************************************************************************
//**  Description : Preload the ikmp Buttons and rollover images.
//**  Arguments   : n/A
//**
//**  Name                                Date       Purpose
//**  ----------------------------------- ---------- ------------------------ 
//**  P. Downer  Spherion Group Ltd       07-07-03   Initial Version
//**  R.Tindall  SAGE                     12May2004  Replace mainNavNews with mainNavPublications-over
//**
//**************************************************************************/
function preloadImages() {
	if (document.images) {
		mainNavHome_over = newImage("/ikmp/graphics/mainNavHome-over.gif");
		mainNavPublications_over = newImage("/ikmp/graphics/mainNavPublications-over.gif");
		mainNavDepartment_over = newImage("/ikmp/graphics/mainNavDepartment-over.gif");
		mainNavGlance_over = newImage("/ikmp/graphics/mainNavGlance-over.gif");
		mainNavTools_over = newImage("/ikmp/graphics/mainNavTools-over.gif");
		mainNavAdvSearch_over = newImage("/ikmp/graphics/mainNavAdvSearch-over.gif");
		gPreloadFlag = true;
	}
}

//**************************************************************************
//**  Description : If this website is loaded within a frame then break out of it and 
//**                display in a full browser window.
//**  Arguments   : N/A
//**
//**  Name                                Date       Purpose
//**  ----------------------------------- ---------- ------------------------ 
//**  C. Brunini SAGE Computing Services  07-07-03   Initial Version
//**
//**************************************************************************/
function breakoutOfFrame() {
  if (top.location != location) {
     top.location.href = document.location.href;
  }
}

//**************************************************************************
//**  Description : Function that is invoke from the onload event for the IKMP Web Site
//**                it calls the breakOutOfFrame and preloadImages funtions
//**  Arguments   : N/A
//**
//**  Name                                Date       Purpose
//**  ----------------------------------- ---------- ------------------------ 
//**  C. Brunini SAGE Computing Services  07-07-03   Initial Version
//**
//**************************************************************************/
function startup() {
   breakoutOfFrame();
   preloadImages();
}

//**************************************************************************
//**  Description : set the search table, search iframe and subtopic iframe height
//**                so no scroll bars appear on the outer most iframe.  This causes
//**                these objects to take up all avialable space in the browser.  
//**                This functions causes the iframe to size correctly between the header and footer so only the iframe scrolls 
//**		    and not the parant frame. These tables and iframes are displayed when the topic search is
//**                requested.
//**  Arguments   : N/A
//**
//**  Name                                Date       Purpose
//**  ----------------------------------- ---------- ------------------------ 
//**  C. Brunini SAGE Computing Services  07-07-03   Initial Version
//**  R. Tindall SAGE Computing Services  11May2004  Remove redundant subtopiciframe
//**  R. Tindall SAGE Computing Services  25May2004  Test for items before set
//**
//**************************************************************************/
function setTableHeight() {
  // set the search table height so no scroll bars appear on the outer most iframe.
  if (parent.top.document.getElementById("contentPanelIframe")) {
     var winheight = parent.top.document.getElementById("contentPanelIframe").getAttribute("height") - cnHeaderSortHeight;
     if (document.getElementById("searchTable")){
        document.getElementById("searchTable").setAttribute("height",winheight);
     }
     if (document.getElementById("searchPanelIframe")){
        document.getElementById("searchPanelIframe").setAttribute("height", winheight);
     }
  }
}

//**************************************************************************
//**  Description : set the content iframe height so no scroll bars appear on the outer most iframe.
//**                The adjusts the height of the content panel iframe to uses all available space in the browser.
//**	            This functions causes the iframe to size correctly between the header and footer so only the iframe scrolls 
//**		    and not the parant frame.
//**                these objects to take up all avialable space in the browser.   
//**  Arguments   : N/A
//**
//**  Name                                Date       Purpose
//**  ----------------------------------- ---------- ------------------------ 
//**  C. Brunini SAGE Computing Services  07-07-03   Initial Version
//**
//**************************************************************************/
function setIframeHeight() {
    // set table height based on screen resolution   
    // Check if the sort by is displayed and adjust the height
    if (! document.getElementById("home") ) {
         var winheight = (document.all?document.body.clientHeight:window.innerHeight)- cnHeaderNoButHeight;
    }
    else {
         var winheight = (document.all?document.body.clientHeight:window.innerHeight)- cnHeaderButtonHeight;
    }
    document.getElementById("contentPanelIframe").setAttribute("height", winheight);  
}


//**************************************************************************
//**  Description : Set the iframe size incase the user has resized the browser and clear the quick links list
//**  Arguments   : N/A
//**
//**  Name                                Date       Purpose
//**  ----------------------------------- ---------- ------------------------ 
//**  C. Brunini SAGE Computing Services  07-07-03   Initial Version
//**
//**************************************************************************/
function adjustContent() {
     setIframeHeight();
     if (document.getElementById("quickLinks")) 
     {
       //reset quick links
       document.getElementById("quickLinks").value="-1";
     }
}

//**************************************************************************
//**  Description : Load the quick link URL.  This URL will load in the content panel iframe by default 
//**                or in a separate window if requested.  
//**	            If the quick link is to be displayed in a new window use the following 
//**		    format=> window.open(http://www.sagecomputing.com.au) in the URL item property 
//**                in the quick link folder.
//**  Arguments   : URL - the url to load
//**
//**  Name                                Date       Purpose
//**  ----------------------------------- ---------- ------------------------ 
//**  C. Brunini SAGE Computing Services  07-07-03   Initial Version
//**
//**************************************************************************/
function displayQuickLink(url){
     if (url != -1)
     {
	 if (url.toUpperCase().indexOf('WINDOW.OPEN') >= 0)
         {
	    // display in a pop-up
            window.open(url.substring(url.toUpperCase().indexOf("WINDOW.OPEN")+12,url.length-1));
         }
	 else
	 {
            document.getElementById("contentPanelIframe").setAttribute("src", url);
	 }
         //reset quick links
         adjustContent();
     }
}

//**************************************************************************
//**  Description : Invoke the panoptic basic search in the content panel iframe
//**                Resize the window if necessary and clear the search box
//**  Arguments   : URL the url to pass to panoptic to perform the search
//**
//**  Name                                Date       Purpose
//**  ----------------------------------- ---------- ------------------------ 
//**  C. Brunini SAGE Computing Services  07-07-03   Initial Version
//**
//**************************************************************************/
function doBasicSearch(url) {
    adjustContent();
    document.getElementById("contentPanelIframe").setAttribute("src", url);
    document.getElementById("basicSearchQuery").value="";
}

//**************************************************************************
//**  Description : when a topic search is performed by the user, they have a choice to 
//** 		    further refine and sort the search.  This function will store the search Url 
//**                so it can be used when refining and sorting the search results further.
//**  Arguments   : searchUrl - The panoptic topic search URL
//**
//**  Name                                Date       Purpose
//**  ----------------------------------- ---------- ------------------------ 
//**  C. Brunini SAGE Computing Services  07-07-03   Initial Version
//**
//**************************************************************************/
function saveSortBy(searchUrl) {
    //store the searchUrl so it can be used when refining and sorting the search results
    if (parent.top.document.getElementById("searchUrl")) {
       parent.top.document.getElementById("searchUrl").value = searchUrl;
    }
}

//**************************************************************************
//**  Description : This function will resubmit the current topic search stored in the searchUrl
//** 		    item and adjust the publication type and sort order depending of the choice 
//**                of the user
//**  Arguments   : sortByUrl - The additional parmeters used to refine the panoptic topic search
//**
//**  Name                                Date       Purpose
//**  ----------------------------------- ---------- ------------------------ 
//**  C. Brunini SAGE Computing Services  07-07-03   Initial Version
//**
//**************************************************************************/
function doSortBy(sortByUrl) {
   // check that the search url has been saved then use this url and append the sort order and publication type
   var url =  parent.top.document.getElementById("searchUrl").value;
   if (url != '') {
      if (url.toLowerCase().indexOf("form=topic") > 0) {
         if (url.toLowerCase().indexOf("&sort=") >0) {
            // remove sort by and meta_x_and and append new values
            document.getElementById("searchPanelIframe").setAttribute("src",url.substring(0,url.toLowerCase().indexOf("&sort="))+sortByUrl);
         }
         else {
            document.getElementById("searchPanelIframe").setAttribute("src", url+sortByUrl);
         }
      }
   }
}

//**************************************************************************
//**  Description : This function clear the sort by nav bar
//**  Arguments   : sortByUrl - The additional parmeters used to refine the panoptic topic search
//**
//**  Name                                Date       Purpose
//**  ----------------------------------- ---------- ------------------------ 
//**  C. Brunini SAGE Computing Services  07-07-03   Initial Version
//**
//**************************************************************************/
function clearSortBy(searchUrl)
{
       saveSortBy(searchUrl)
       //reset elements in sort by 
       parent.document.getElementById("sortOrder").value ="date";
       parent.document.getElementById("publicationType").value ="";
}


//**************************************************************************
//**  Description : Validate and submit events form for Searching
//**
//**  Name                                Date       Purpose
//**  ----------------------------------- ---------- ------------------------ 
//**  R. Tindall SAGE Computing Services  28May2004  Initial Version
//**
//**************************************************************************/
function submitEvtSearch(pForm) {
	var vBadDate = false;
	var vBadDateNames = "";

        var vAftDay = pForm.p_aft_day.options[pForm.p_aft_day.selectedIndex].value
        var vAftMth = pForm.p_aft_mth.options[pForm.p_aft_mth.selectedIndex].value
        var vAftYr  = pForm.p_aft_yr.options[pForm.p_aft_yr.selectedIndex].value
        var vBefDay = pForm.p_bef_day.options[pForm.p_bef_day.selectedIndex].value
        var vBefMth = pForm.p_bef_mth.options[pForm.p_bef_mth.selectedIndex].value
        var vBefYr  = pForm.p_bef_yr.options[pForm.p_bef_yr.selectedIndex].value
	
	if( ( vAftDay != '' ) ||
            ( vAftMth != '' ) ||
            ( vAftYr != '' ) ){
          
	    if ( !dateCheck(vAftDay + vAftMth + vAftYr,'%dd%MON%yyyy')) {
                vBadDate = true;
		vBadDateNames += "     Event After Date\n";
            }
	}

	if( ( vBefDay != '' ) ||
            ( vBefMth != '' ) ||
            ( vBefYr != '' ) ){
          
	    if ( !dateCheck(vBefDay + vBefMth + vBefYr,'%dd%MON%yyyy')) {
                vBadDate = true;
		vBadDateNames += "     Event Before Date\n";
            }
	}

	if( vBadDate ) {
		alert( "Please correct invalid or incomplete date for:\n" + vBadDateNames );
	}
        else {	
           pForm.p_action.value = 'SEARCH';
	   pForm.submit();
        }
}

//**************************************************************************
//**  Description : Submit events form for viewing Event Details
//**
//**  Name                                Date       Purpose
//**  ----------------------------------- ---------- ------------------------ 
//**  R. Tindall SAGE Computing Services  28May2004  Initial Version
//**
//**************************************************************************/
function goEvtDetail(pForm, pEventId) {

	pForm.p_event_id.value = pEventId;
	pForm.p_action.value = 'DETAIL';

	pForm.submit();
}

