
/*****************************************************************************
'*
'*	Name:        default.js
'*	Description: Functions and SubRoutines to use with the templates
'*
'*	VSS Header: $$Header: /MRWA Applications/IIS_NR/Implementation/CMS/NR/mrwa/scripts/default.js 3     3/04/08 11:12a C2363 $
'*
'*****************************************************************************
'*	          (c) Copyright Main Roads Western Australia                     *
'*****************************************************************************
'*
'*	Modification History:
'*	•When			•Who			•Bug#			•What
'*	--------------------------------------------------------------------------
'*	•05-Aug-2002	•Wilson Wampers •				•Initial Version		 |
'*	•06-Jan-2003	•Wilson Wampers •				•Corrected bookmark bug  |
'*	•03-Apr-2008	•Imran Ahmed	•				•Add Internet browser detection in function emailForm	|
'*****************************************************************************/

//***************************************************************************
// Name:            openInExtWin
// Parameters:      NONE
// Return Value:    NONE
// Pre-Conditions:  NONE
// Post-Conditions: NONE
// Description:     NONE
//***************************************************************************

function openInExtWin( psURL )
{
	window.open( psURL );
}

//***************************************************************************
// Name:            continue_onLoad
// Parameters:      NONE
// Return Value:    NONE
// Pre-Conditions:  NONE
// Post-Conditions: NONE
// Description:     NONE
//***************************************************************************

function continue_onLoad()
{
    // Function called elsewhere; this is just to answer the referring call.
}

//***************************************************************************
// Name:            endEvent
// Parameters:      NONE
// Return Value:    NONE
// Pre-Conditions:  NONE
// Post-Conditions: NONE
// Description:     NONE
//***************************************************************************

function endEvent()
{
    event.returnValue = false;
    event.cancelBubble = true;
    return( false );
}

//***************************************************************************
// Name:            navigate
// Parameters:      NONE
// Return Value:    NONE
// Pre-Conditions:  NONE
// Post-Conditions: NONE
// Description:     Function navigates to the corresponding 
//                  postings or channels page by GUID
//***************************************************************************

function navigate( psGUID ) 
{ 
	try
	{
		top.location.href = "/NR/mrwa/run/start.asp?G=" + psGUID;
	}
	catch( e )
	{
		messageBox( MB_Exclamation, MB_Ok, "Cycling Link:", "This link only functions when the page is viewed in it's proper frameset!", 3 );
	} 
}

//***************************************************************************
// Name:            arNavigate
// Parameters:      NONE
// Return Value:    NONE
// Pre-Conditions:  NONE
// Post-Conditions: NONE
// Description:     Function navigates to the corresponding 
//                  postings or channels page by GUID
//***************************************************************************

function arNavigate( psGUID ) 
{ 
	top.location.href = "/NR/mrwa/run/start.asp?G=" + psGUID;
}

//***************************************************************************
// Name:            getTDElement
// Parameters:      NONE
// Return Value:    NONE
// Pre-Conditions:  NONE
// Post-Conditions: NONE
// Description:     Itterates upwards until an TD element is found
//***************************************************************************

function  getTDElement()
{
	var oTD = window.event.srcElement;
	while( oTD.tagName != "TD" )
	{
		oTD = oTD.parentElement;
	}
	return( oTD );
}

//***************************************************************************
// Name:            pad
// Parameters:      NONE
// Return Value:    NONE
// Pre-Conditions:  NONE
// Post-Conditions: NONE
// Description:     NONE
//***************************************************************************
	
function pad( pnNum  )
{
	pnNum = "" + pnNum;
	while( pnNum.length < 2 )
	{
		pnNum = "0" + pnNum;
	}
	return( pnNum );
}

//***************************************************************************
// Name:            p3d
// Parameters:      NONE
// Return Value:    NONE
// Pre-Conditions:  NONE
// Post-Conditions: NONE
// Description:     NONE
//***************************************************************************

function p3d(psStr)
{	
	while( psStr.length < 3 ) psStr = "0"+psStr;
	return( psStr );
}

//***************************************************************************
// Name:            hoveR
// Parameters:      NONE
// Return Value:    NONE
// Pre-Conditions:  NONE
// Post-Conditions: NONE
// Description:     NONE
//***************************************************************************

function hoveR()
{
    window.event.srcElement.setAttribute( "src", window.event.srcElement.getAttribute("src").replace( '.gif', 'sel.gif' ) );
}

//***************************************************************************
// Name:            mrwaLink
// Parameters:      NONE
// Return Value:    NONE
// Pre-Conditions:  NONE
// Post-Conditions: NONE
// Description:     Interceptor function for links created by CMS users
//***************************************************************************

function mrwaLink( psLink, psExt )
{
	try
	{
		if( psLink != "" )
		{
			if( psLink.toLowerCase() == "#top" ) 
			{
					psLink      = "#_toc";
				var bookmarkObj = eval( psLink.substr(1) );
				    bookmarkObj.scrollIntoView( true );
				    return( true );
			}
			if( psLink.indexOf("#") != -1 )
			{
				// var aLink			= psLink.split("#");
				// window.location.href = "#" + aLink[1];
				    window.location.href = psLink;
			}
			else if( psLink.toLowerCase().indexOf("@") != -1 )
			{
				if( psExt )
				{
					if( psExt == "RTEMS" )
					{
						emailFormRTEMS( psLink.substr( (psLink.indexOf("://") + 1 ) ) );
					}
					else
					{
						var bNumber = true;
						try 
						{ 
							if( valNumber.innerText )
							{
								emailFormRTEMS( psLink.substr( (psLink.indexOf("://") + 1 ) ) ); 
							}	
								
						} 
						catch( e )
						{ 
							bNumber = false; 
						}
						if( !bNumber )
						{
							emailForm( psLink.substr( (psLink.indexOf("://") + 1 ) ) );
						}
					}
				}
				else
				{   
					var bNumber = true;
					try 
					{ 
						if( valNumber.innerText )
						{
							emailFormRTEMS( psLink.substr( (psLink.indexOf("://") + 1 ) ) ); 
						}	
							
					} 
					catch( e )
					{ 
						bNumber = false; 
					}
					if( !bNumber )
					{
						emailForm( psLink.substr( (psLink.indexOf("://") + 1 ) ) );
					}
				}
			}
			else if( psExt )
			{
				if( psExt.toLowerCase().substr(0,3) == "asp" || psExt.toLowerCase().substr(0,3) == "htm" )
				{
					while( psLink.indexOf("~") != -1 )
					{
						psLink = psLink.replace("~","/");
					}
					window.location.href = ( psLink + "." + psExt );
				}
				else if( psExt == "_blank" )
				{
				    window.open( psLink );
				}
				else if( psLink.toUpperCase().indexOf("HTTP:") != -1 )
				{
					window.open( psLink );
				}
				else
				{
					window.location.href = psLink;
				}
			}
			else 
			{
				window.location.href = psLink;
			}
		}
	}
	catch( e ) 
	{
	    errorMsg( e, "function mrwaLink()" );
	}
}

//*****************************************************************************
// Name:            checkSpider
// Parameters:      none
// Return Value:    None
// Pre-Conditions:  None
// Post-Conditions: None
// Description:     None
//*****************************************************************************

function checkSpider( psHttpHost )
{
	if( top.location.href.indexOf("http://") == -1 )
	{
		top.window.location.href = "http://"+psHttpHost+"/NR/mrwa/run/start.asp?G=" + document.body.strGUID;
	}
}

//*****************************************************************************
// Name:            helpEvent
// Parameters:      none
// Return Value:    None
// Pre-Conditions:  None
// Post-Conditions: None
// Description:     None
//*****************************************************************************

function helpEvent( )
{
	var sArg = "";
	if( window.event.type == "help" )
	{
		var Element =  window.event.srcElement;
		while ( Element.tagName != "BODY" ) 
		{
			if( Element.Hstr )
			{
				sArg = Element.Hstr;
				break;
			}
			Element = Element.parentNode;	
		}
		if( Element.Hstr )
		{
			sArg = Element.Hstr;
		}
		window.event.cancelBubble = true;
		window.event.returnValue  = false;
		messageBox( MB_Exclamation, MB_Ok, "Helper Message", "<b>The helper functions are still</b><br/><br/>" + gsUC );
	}	
}

//*****************************************************************************
// Name:            contextHandler
// Parameters:      none
// Return Value:    None
// Pre-Conditions:  None
// Post-Conditions: None
// Description:     None
//*****************************************************************************

function contextHandler()
{
	if( window.event.srcElement.tagName != "INPUT" )
	{
		window.event.cancelBubble = true;
		window.event.returnValue  = false;
		return( false );
	}
}

//***************************************************************************
// Name:            mailForm
// Parameters:      NONE
// Return Value:    NONE
// Pre-Conditions:  NONE
// Post-Conditions: NONE
// Description:     Opens a modal window to the user for emailing a form
//***************************************************************************

function emailForm( psLink )
{
    var sLink = psLink;
	if( sLink.toLowerCase().indexOf("mailto:") != -1 )
	{
		sLink = sLink.substr( ( sLink.indexOf( "mailto:" ) + 1 ) );
	} 
		    
	var Explorer = navigator.appVersion.indexOf("MSIE");
	if (Explorer > 0)
	{
	var XMLFrag  = new String();
		XMLFrag += "<request>";
		XMLFrag += "\n <email value=\"" + sLink + "\"/>"; 
		XMLFrag += "\n</request>";
		
		var sDialogAttr = "dialogHeight:545px;dialogWidth:478px;center:1;status:0;help:0;resizable:1;";
		var sAnswer  = window.showModalDialog( "/NR/mrwa/forms/emailForm.asp", XMLFrag, sDialogAttr );
			if( sAnswer != null )
			{
			replyForm( sAnswer );
			}	
		
	}
	else
	{		
		location.href="mailto:" + sLink + "?subject=Enquiries"
	
	}
}

//***************************************************************************
// Name:            mailForm
// Parameters:      NONE
// Return Value:    NONE
// Pre-Conditions:  NONE
// Post-Conditions: NONE
// Description:     Opens a modal window to the user for emailing a form
//***************************************************************************

function emailFormRTEMS( psLink )
{
    var sValNumber, sValVersion, sValDate;
    var sLink = psLink;
    
    try { sValNumber  = valNumber.innerText }  catch(e) { sValNumber  = "" };
    try { sValVersion = valVersion.innerText } catch(e) { sValVersion = "" };
    try { sValDate    = valDate.innerText }    catch(e) { sValDate    = "" }; 
	
	if( sLink.toLowerCase().indexOf("mailto:") != -1 )
	{
		sLink = sLink.substr( ( sLink.indexOf( "mailto:" ) + 1 ) );
	} 
	
	var Explorer = navigator.appVersion.indexOf("MSIE");
	if (Explorer > 0)
	{
		var XMLFrag  = new String();
			XMLFrag += "<request>";
			XMLFrag += "\n <number value=\""  + sValNumber  + "\"/>";
			XMLFrag += "\n <version value=\"" + sValVersion + "\"/>";
			XMLFrag += "\n <date value=\""    + sValDate    + "\"/>";
			XMLFrag += "\n <email value=\""	  +	sLink      + "\"/>"; 
			XMLFrag += "\n</request>";
		var sDialogAttr = "dialogHeight:525px;dialogWidth:478px;center:1;status:0;help:0;resizable:1;";
		var sAnswer  = window.showModalDialog( "/NR/mrwa/forms/emailFormRTEMS.asp", XMLFrag, sDialogAttr );
			if( sAnswer != null )
			{
				replyForm( sAnswer );
			}
	}
	else
	{				
		location.href="mailto:" + sLink + "?subject=Enquiries"
	}
}

//***************************************************************************
// Name:            replyForm
// Parameters:      NONE
// Return Value:    NONE
// Pre-Conditions:  NONE
// Post-Conditions: NONE
// Description:     Displays a modalwindow with a reply thank you message
//***************************************************************************

function replyForm( sAnswer )
{
	messageBox( MB_Information, MB_Ok, "MailForm", "Your email will be processed swiftly!<br/><br/>Thanks for visiting the Main Roads Australia Web Site" );
}

//***************************************************************************
// Name:            re_positionMenu
// Parameters:      NONE
// Return Value:    NONE
// Pre-Conditions:  NONE
// Post-Conditions: NONE
// Description:     Repositions the menu to the right
//***************************************************************************

function re_positionMenu()
{
	var wOff = document.body.offsetWidth - (document.all['_Menu'].offsetWidth+20);
	_Menu.style.posLeft = wOff;
	_Menu.style.posTop  = 0;
}

//***************************************************************************
// Name:            menuTimer
// Parameters:      NONE
// Return Value:    NONE
// Pre-Conditions:  NONE
// Post-Conditions: NONE
// Description:     Timer to enable other onload events first
//***************************************************************************

function menuTimer()
{
	setTimeout( 're_positionMenu()', 10 );
	try{ onLoad_Continue(); } catch(e) {};
}

//***************************************************************************
// Name:            printThis
// Parameters:      None
// Return Value:    None
// Pre-Conditions:  None
// Post-Conditions: None
// Description:     None
//***************************************************************************

function printThis()
{
	downloadAndPrint( (window.location.href).toString() );
}

//***************************************************************************
// Name:            downloadAndPrint
// Parameters:      None
// Return Value:    None
// Pre-Conditions:  None
// Post-Conditions: None
// Description:     None
//***************************************************************************

function downloadAndPrint( psURL )
{
	try
	{
		var oNewoDL = document.createElement("ie:download");
			oNewoDL.id = "oDL";
			oNewoDL.style.behavior = "url(#default#download)";
			document.body.appendChild( oNewoDL );
			oDL.startDownload( psURL, openWindowAndPrintFromURL );
	}
	catch( oError )
	{
		displayError( oError, "downloadAndPrint() ==> URL " + psURL );
	}
}

//***************************************************************************
// Name:            openWindowAndPrintFromURL
// Parameters:      None
// Return Value:    None
// Pre-Conditions:  None
// Post-Conditions: None
// Description:     None
//***************************************************************************

function openWindowAndPrintFromURL( sHTML )
{
	var w     = window.open( "", "printContent", "resizable=1,scrollbars=0,titlebar=0,width=40px,height=40px,top=0px,left=0px;");
    w.onblur  = "self.close";
    w.onerror = "self.close";
    w.document.open();
    w.document.write(sHTML);
    w.document.write("<scr");
    w.document.write("ipt>window.onload=_print;function _print(){top.window.moveTo(-1000,-1000);window.print();self.close();}</scr");
    w.document.write("ipt>");
    w.document.close();	
}

//***************************************************************************
// Name:            prepDynAndPrint
// Parameters:      NONE
// Return Value:    NONE
// Pre-Conditions:  NONE
// Post-Conditions: NONE
// Description:     NONE
//***************************************************************************

function prepDynAndPrint()
{   
	try
	{       
	        document.all("tPrint").style.visibility = 'hidden';
	        document.all("tDownload").style.visibility = 'hidden';
	        with( document.all("dContent").style )
	        {
	            border = "0px";
	            backgroundColor = "white";
	        }
	        // Create print table
		var oPrtTbl = document.createElement("TABLE");
		    oPrtTbl = document.body.appendChild( oPrtTbl );
			// pageContent
			for( var ni=0; ni<pContainer.rows.length; ni++ )
			{
				oTR = oPrtTbl.insertRow(-1);
				oTR = oTR.replaceNode( pContainer.rows[ni].cloneNode(true));
			}
		var w = window.open( "", "printContent", "resizable=1,scrollbars=0,titlebar=0,width=40px,height=40px,top=0px,left=0px;");
			w.document.open();
			w.document.write("<html><STYLE>");
			w.document.write("  BODY     { background-color: #ffffff; margin: 0px; padding: 0px; }");
			w.document.write("  TEXTAREA { background-color: #efeeee; font-family: 'Lucida Console'; font-size: 11px; border: 0px; }");
			w.document.write("</STYLE>");
			w.document.write("<link href=\"/NR/mrwa/css/dynTmpl/dynTmplprt.css\" rel=\"stylesheet\" type=\"text/css\" />");
			w.document.write("<body scroll=yes>");
			w.document.write( oPrtTbl.outerHTML );
			w.document.write("</body></html>");
			w.document.write("<scr");
			w.document.write("ipt>window.onload=_print;function _print(){window.focus();window.print();self.close();}</scr");
			w.document.write("ipt>");
			w.document.close();
			// Remove print code
			oPrtTbl.removeNode(true);
			window.location.reload();
	}
	catch( oError )
	{
	   displayError( oError, "prepDynAndPrint() --><br/>" + window.location.href.toString() );
	}	
}

//***************************************************************************//////////////////////////////////////////////////////////////
// Messages and debug routines beyond this point only

//***************************************************************************
//  Name:           displayError
//  Parameters:     none
//  Returns:        none
//  Pre-condition:  none
//  Post-condition: none
//  Purpose:        none
//***************************************************************************

function displayError( e, sTitle )
{
	messageBox( MB_Critical, 
	            MB_Ok, 
	            "Trapped-Error: ", 
	            "In function<b style='color:maroon'> " + 
	            sTitle + " </b>\n<b>" + e.name + ":</b> " + 
	            e.description + "<br/><b style='color:blue'>[ " + 
	            (e.number & 0xFFFFFFFF).toString() + " ]</b>" 
	          );
}

//***************************************************************************
// Name:            errorMSG
// Parameters:      NONE
// Return Value:    NONE
// Pre-Conditions:  NONE
// Post-Conditions: NONE
// Description:     Displays the error messagebox
//***************************************************************************

function errorMsg( poErr, psMsg )
{
	messageBox( MB_Critical, 
	            MB_Ok, 
	            "ERROR: " + (poErr.number & 0xFFFFFFFF ), 
	            psMsg + "<br/>" + poErr.message 
	          );
}

//*****************************************************************************
// Name:            showHTML
// Parameters:      NONE
// Return Value:    NONE
// Pre-Conditions:  NONE
// Post-Conditions: NONE
// Description:     Debug function
//*****************************************************************************

function showHTML( psHTML )
{
	var w     = window.open( "", "MainContent", "resizable=1,scrollbars=0,titlebar=0,width=900px,height=800px,top=100px,left=100px");
    w.onblur  = "self.close";
    w.onerror = "self.close";
    w.document.open();
    w.document.write("<html><STYLE>");
    w.document.write("  BODY     { background-color: #ffffff; margin: 0px; padding: 0px; }");
    w.document.write("  TEXTAREA { background-color: #eeeeee; font-family: 'Lucida Console'; font-size: 11px; border: 0px; }");
    w.document.write("</STYLE>");
    w.document.write("<body scroll=no><textarea style='width: 100%; height: 100%' id=textarea1 name=textarea1>");
    w.document.write( psHTML );
    w.document.write("</textarea></body></html>");
    w.document.close();
}

//*****************************************************************************
// Name:            showHTML
// Parameters:      NONE
// Return Value:    NONE
// Pre-Conditions:  NONE
// Post-Conditions: NONE
// Description:     Debug function
//*****************************************************************************

function showOuterHTML( poHTML )
{
	var w     = window.open( "", "MainContent", "resizable=1,scrollbars=0,titlebar=0,width=900px,height=800px,top=100px,left=100px");
    w.onblur  = "self.close";
    w.onerror = "self.close";
    w.document.open();
    w.document.write("<html><STYLE>");
    w.document.write("  BODY     { background-color: #ffffff; margin: 0px; padding: 0px; }");
    w.document.write("  TEXTAREA { background-color: #eeeeee; font-family: 'Lucida Console'; font-size: 11px; border: 0px; }");
    w.document.write("</STYLE>");
    w.document.write("<body scroll=no><textarea style='width: 100%; height: 100%' id=textarea1 name=textarea1>");
    w.document.write( poHTML.outerHTML );
    w.document.write("</textarea></body></html>");
    w.document.close();
}

//***************************************************************************
// Name:            showInnerHTML
// Parameters:      NONE
// Return Value:    NONE
// Pre-Conditions:  NONE
// Post-Conditions: NONE
// Description:     Debug Window
//***************************************************************************

function showInnerHTML( psHTML )
{
	var w     = window.open( "", "showXML", "resizable=1,scrollbars=1,titlebar=0,width=1000px,height=800px,top=100px,left=100px");
    w.onblur  = "self.close";
    w.onerror = "self.close";
    w.document.open();
    w.document.write("<html><STYLE>");
    w.document.write("  BODY     { background-color: #ffffff; margin: 0px; padding: 0px; }");
    w.document.write("  TEXTAREA { background-color: #efeeee; font-family: 'Lucida Console'; font-size: 11px; border: 0px; }");
    w.document.write("</STYLE>");
    w.document.write("<body scroll=yes>");
    w.document.write( psHTML );
    w.document.write("</body></html>");
    w.document.close();
}

//***************************************************************************
// Name:            showXML
// Parameters:      NONE
// Return Value:    NONE
// Pre-Conditions:  NONE
// Post-Conditions: NONE
// Description:     Debug Window
//***************************************************************************

function showXML( psXML )
{
	window.showModalDialog( "/NR/mrwa/scripts/showXML.htm", 
			                psXML.xml, 
			                "dialogWidth:600px;dialogHeight:770px;center:1;status:0;help:0"
			              );
}

//***************************************************************************
// Name:            getHTML
// Parameters:      psFXML, psFXSL ( full path & filename )
// Return Value:    HTML
// Pre-Conditions:  NONE
// Post-Conditions: NONE
// Description:     Calls functions to transform XML with XSL into HTML result
//***************************************************************************

function getHTML( psFXML, psFXSL )
{ 
	var oXMLDOM       = new ActiveXObject("Microsoft.XMLDOM");
	    oXMLDOM.async = false;
	try
	{	// Load XML
	    oXMLDOM.load( psFXML );
	}
	catch( e ) 
	{
	    errorMsg( e, "getHTML <b>XML</b> Failed Because:" );
	}
	var oXSLDOM      = new ActiveXObject("Microsoft.XMLDOM");
	    oXSLDOM.async = false;
	try
	{	// Load XSL
	    oXSLDOM.load( psFXSL );
	} 
	catch( e ) 
	{
	    errorMsg( e, "getHTML <b>XSL</b> Failed Because:" );
	}
	try
	{	// Return HTML
		return( ( oXMLDOM.transformNode( oXSLDOM ) ) );
	}
	catch( e )
	{
		errorMsg( e, "getHTML <b>transform</b> Failed Because:" );
	}	// Release objects
	    oXMLDOM = null;
	    oXSLDOM = null;
}


//***************************************************************************
// Name:            showRequestHTML
// Parameters:      NONE
// Return Value:    NONE
// Pre-Conditions:  NONE
// Post-Conditions: NONE
// Description:     Debug Window
//***************************************************************************

function showRequestXML( psHTML )
{
	var w = window.open( "", "showRequestXML", "resizable=1,scrollbars=1,titlebar=0,width=600px,height=450px,top=540px,left=0px");
    w.document.open();
    w.document.write("<html><STYLE>");
    w.document.write("  BODY     { background-color: #ffffff; margin: 0px; padding: 0px; }");
    w.document.write("  TEXTAREA { background-color: #feeeee; font-family: 'Lucida Console'; font-size: 11px; border: 0px; }");
    w.document.write("</STYLE>");
    w.document.write("<body scroll=yes>");
    w.document.write( psHTML );
    w.document.write("</body></html>");
    w.document.close();
}

//***************************************************************************
// Name:            showResponseHTML
// Parameters:      NONE
// Return Value:    NONE
// Pre-Conditions:  NONE
// Post-Conditions: NONE
// Description:     Debug Window
//***************************************************************************

function showResponseXML( psHTML )
{
	var w = window.open( "", "showResponseXML", "resizable=1,scrollbars=1,titlebar=0,width=600px,height=450px,top=540px,left=610px");
    w.document.open();
    w.document.write("<html><STYLE>");
    w.document.write("  BODY     { background-color: #ffffff; margin: 0px; padding: 0px; }");
    w.document.write("  TEXTAREA { background-color: #eeeefe; font-family: 'Lucida Console'; font-size: 11px; border: 0px; }");
    w.document.write("</STYLE>");
    w.document.write("<body scroll=yes>");
    w.document.write( psHTML );
    w.document.write("</body></html>");
    w.document.close();
}

//***************************************************************************

