/*
JSFunctions.jsp, for Quidnunc.com website
------------------------------------------------------------------------------------------------------------
Ver			Date				Author				Comments
------------------------------------------------------------------------------------------------------------
d1			30 January 2001		John Downing		Created
d2			01 February 2001	John Downing		Added removeTag method
d3			01 February 2001	John Downing		Added tagToUpperCase method
d4			01 February 2001	John Downing		Amended tagToUpperCase to more general 'replace()' 
													method, and added call to remove <font> tags
d5			06 February 2001	John Downing		Added setDivToPrint method
d6			06 February 2001	John Downing		Updated removeTag to be more generic
------------------------------------------------------------------------------------------------------------
*/


/*
--------------------------------------------------
Constants
--------------------------------------------------
*/
var ksDEFAULT_DIV		 		= "holderscale";
var ksDEFAULT_MULTI_DIV			= "companyholder";
var ksTEXT_END 					= "</div>";
var ksSCRIPT_URL				= "/print.asp";
var ksWINDOW_PARAMETERS 		= "";
var ksUC_HREF_TAG_START			= "<A";
var ksUC_HREF_TAG_END			= "</A>";
var ksLC_HREF_TAG_START			= "<a";
var ksLC_HREF_TAG_END			= "</a>";
var ksUC_FONT_TAG_START			= "<FONT size=+0>";
var ksUC_FONT_TAG_END			= "</FONT>"
var ksUC_IMG_TAG				= "<IMG";
var ksLC_IMG_TAG				= "<img";


/*
-----------------------------------------------------
Variables
-----------------------------------------------------
*/

var sMULTI_DIV_TO_PRINT			= ksDEFAULT_MULTI_DIV			




/*
--------------------------------------------------
Functions
--------------------------------------------------
*/

function setDivToPrint(a_sDivId)
/* 
	-----------------------------------------------------------------------------------
	Ver			Date				Author				Comments
	-----------------------------------------------------------------------------------
	d5			09 February 2001	John Downing		Created
	-----------------------------------------------------------------------------------
	For pages where there are stacked DIVs, this sets which one gets printed (assuming
	the call to printVersion(DivId) from the HTML page uses sMULTI_DIV_TO_PRINT as the 
	parameter).
*/
{
	sMULTI_DIV_TO_PRINT = a_sDivId;
}

function isExplorer()
/* 
	-----------------------------------------------------------------------------------
	Ver			Date				Author				Comments
	-----------------------------------------------------------------------------------
	d1			30 January 2001		John Downing		Created
	-----------------------------------------------------------------------------------
	Checks which browser we're using. Probably needs replacing with a smart version sometime soon..
*/
{
	if (document.all) 
	{
		return true;
	}
	else 
	{
		return false;
	}
}


function printVersion(a_sDIV)
/*
	------------------------------------------------------------------------------------
	Ver			Date				Author				Comments
	------------------------------------------------------------------------------------
	d1			30 January 2001		John Downing		Created
	d2			01 February 2001	John Downing		Added call to removeTag() method
	d3			01 February 2001	John Downing		Added call to replace() methods
	------------------------------------------------------------------------------------
	Extracts the inner text from a DIV object, given its ID
	
	param: a_sDIV, the ID of the DIV object whose contents we want
	
	Note that we're using 'per-session' cookies here, ie ones that don't get stored on the 
	client machine.
	
	Also - the calls to the cookie functions reference cookieFunctions.js. These were 
	written by Written by:  Bill Dortch, hIdaho Design <bdortch@hidaho.com>, and are
	public domain.
*/
{
	var sHTML = "";
	var iNumCookies = 0;
	
	// get the content that we want to display on the printable page
	if (!a_sDIV)
	{
		sHTML = getText(ksDEFAULT_DIV);
	}
	else
	{
		sHTML = getText(a_sDIV);
	}
	
	if (! isExplorer())
	{
		// change lower case tags to upper case where relevant
		sHTML = replace(sHTML,ksLC_HREF_TAG_START,ksUC_HREF_TAG_START);
		sHTML = replace(sHTML,ksLC_HREF_TAG_END,ksUC_HREF_TAG_END);
		sHTML = replace(sHTML,ksLC_IMG_TAG,ksUC_IMG_TAG);
	}
	
	if (isExplorer())
	{
		// explorer replaces <font> tags with <font size=+0> internally (!?)
		sHTML = replace(sHTML,ksUC_FONT_TAG_START,"");
		sHTML = replace(sHTML,ksUC_FONT_TAG_END,"");
	}
	
	sHTML = removeTag(sHTML,ksUC_HREF_TAG_START,ksUC_HREF_TAG_END);
	sHTML = removeTag(sHTML,ksUC_IMG_TAG);
	// Delete any cookies already stored in this session
	
	DeleteCookie("title","/");
	
	iNumCookies = parseInt(GetCookie("total"));
	DeleteCookie("total","/");
	
	if(!isNaN(iNumCookies))
	{
		for(i=0;i<iNumCookies;i++)
		{
			DeleteCookie("text"+i,"/");
		}
	}
	
	// if the text is longer than 1000 chars then we need to split it up and store
	// it in individual cookies
	
	if(sHTML.length > 1000)
	{
		iNumCookies = Math.round((sHTML.length / 1000) + 0.49);
	}
	else
	{
		// text is less than 1000 chars so will fit in one cookie
		iNumCookies = 1;
	}
		
	// write the cookies
	for(i=0;i<iNumCookies;i++)
	{
		sTemp = sHTML.substring(i*1000,(i+1)*1000);
		SetCookie("text"+i,sTemp,null,"/");
	}
	
	// set a cookie to store then total number if cookies that the page text is stored in
	SetCookie("total",iNumCookies,null,"/");
	
	// set a cookie to store the <TITLE> of the document, for re-display on the printable page
	SetCookie("title",window.document.title,null,"/");
	
	// open the pop-up window and call the asp script to pick up and display the cookie text(s)
	displayWindow = window.open();
	displayWindow.location.href = ksSCRIPT_URL;
	
	
}

  
function getText(a_sDiv)
/*
	-----------------------------------------------------------------------------------
	Ver			Date				Author				Comments
	-----------------------------------------------------------------------------------
	d1			30 January 2001		John Downing		Created
	-----------------------------------------------------------------------------------
	Returns the contents of a DIV tag, given its ID as a String.
	
	param: a_sDiv, the ID of the DIV tag we're after.
	
	NB - Explorer 4 & up can use the DOM to get the DIV contents, as the ID identifies an object.
	Netscape has to use java to get the entire page source and then call supplementary functions
	to parse out the equivalent of innerHTML
	
*/
{	
	var sContent = "";
	
    if (isExplorer())
	{	
		sContent = eval(a_sDiv + ".innerHTML");
    }
    else
	{
		// check if the JVM is enabled
	 	if(! navigator.javaEnabled())
		{
        	alert("You need to have Java enabled on your browser for this to work in Navigator !");
        }
		else
        {	
			
			// open the stream for this page
			stream = java.io.DataInputStream(java.net.URL(location).openStream());
			sNextLine = stream.readLine();
			while(sNextLine != null)
			{
				sContent += sNextLine;
				sContent += '\n';
				sNextLine = stream.readLine();
            }

            stream.close();
			
			// we have the whole page at this point, but only want to return the DIV contents
			sContent =  parseContent(a_sDiv,sContent);
		}
	}

    return sContent;
}

function parseContent(a_sDIV,a_sContent)
/*
	-----------------------------------------------------------------------------------
	Ver			Date				Author				Comments
	-----------------------------------------------------------------------------------
	d1			30 January 2001		John Downing		Created
	-----------------------------------------------------------------------------------
	Parses out the contents of a <DIV> tag, given its ID and the text containing it.
	
	param: a_sDIV, the ID of the DIV object whose contents we want 
	param: a_sContent, the text containing in the DIV
*/
{
	var sContent = "";
	sDIV = "<div id=\"" + a_sDIV + "\">";

	sContent = a_sContent.substring((a_sContent.indexOf(sDIV) + sDIV.length),a_sContent.length);
	sContent = sContent.substring(0,sContent.indexOf(ksTEXT_END));
		
	return sContent;
}

function removeTag(a_sText,a_sTagStart,a_sTagEnd)
/*
	-----------------------------------------------------------------------------------
	Ver			Date					Author				Comments
	-----------------------------------------------------------------------------------
	d2			01 February 2001		John Downing		Created
	d6			06 February 2001		John Downing		Updated to be generic
	-----------------------------------------------------------------------------------
	Removes any HTML links from printable page
	Now removes any specified links from printable page

	param: a_sText, the text to be printed
*/
{

	var sText = "";
	var iTagStartPosition = -1;
	var iTagEndPosition = -1;
	
	
	if (a_sText.indexOf(a_sTagStart) > -1)
	{
		while ((iTagStartPosition = a_sText.indexOf(a_sTagStart)) > -1)
		{
			sText += a_sText.substring(0,iTagStartPosition);
			// sText is now the start of the body text to the first tag
			
			a_sText = a_sText.substring(iTagStartPosition + a_sTagStart.length,a_sText.length);
			// a_sText has now had what is in sText chopped off the start
			
			iTagStartPosition = a_sText.indexOf(">");
			a_sText = a_sText.substring(iTagStartPosition +1 ,a_sText.length);
			// a_sText has now had the rest of the tag chopped off the start
			
			if (a_sTagEnd)
			// not all tags will have closing tags, ie <img>
			{
				iTagEndPosition = a_sText.indexOf(a_sTagEnd);
				sText += a_sText.substring(0, iTagEndPosition);
				// sText now has the text inbetween the tags added to it
			
				a_sText = a_sText.substring(iTagEndPosition + a_sTagEnd.length,a_sText.length);
				//a_sText has now had the end tag chopped off the start
			}
		}
		
		// add on anything left at the end of a_sText, after we've reached the last closing tag
		if (a_sText.length > 0)
		{
			sText += a_sText;
		}
	}
	else
	{
		// there are no links in the text
		sText = a_sText;
	}
	
	return sText;
}

function replace(a_sText,a_sFind,a_sReplace)
/*
	------------------------------------------------------------------------------------
	Ver			Date				Author				Comments
	------------------------------------------------------------------------------------
	d3			01 February 2001		John Downing		Created
	d4			01 February 2001		John Downing		Amended method name from
															tagToUpperCase() and allowed
															parameters to be passed
	------------------------------------------------------------------------------------
	Makes sure all <a> and </a> tags are upper case in body of text

	param: a_sText, the text to be parsed
	param: a_sFind, the text to be replaced
	param: a_sReplace. the text to replace it with
*/
{
	var sText = "";
	var sTemp = "";
	iStartPosition = -1;
	iEndPosition = -1;
	
	// replace any lower case <a tags
	while ((iStartPosition = a_sText.indexOf(a_sFind)) > -1)
	{
		sText += a_sText.substring(0,iStartPosition);
		sText += a_sReplace;
		a_sText = a_sText.substring(iStartPosition + a_sFind.length,a_sText.length);
	}
	if (a_sText.length > 0)
	{
		sText += a_sText;
	}
	
	return sText;
}