// tools.js
// 
// Utility functions for Thamesdoc pages
// 
// Created on 08-Aug-2005
// @author GS Macfarlane
// @copyright 2005 macfarlane.biz for Thamesdoc
//
 
// ==================================================================================
// Display cookies for diagnostic purposes.

function showAllCookies()
{   var allcookies = document.cookie;

    document.write ('<TABLE BORDER="0">');        
    var eachcookie = allcookies.split(';');
    for (var i=0; i<eachcookie.length; i++)
    {   var values=eachcookie[i].split("=");
        
        if (!values[0].match(/cookies/i) && !values[0].match(/thamesdoc_2/i))
        { 	document.write ('<TR><TD>',values[0],'</TD><TD>&nbsp;<B>',values[1],'</B></TD></TR>');
        }
    }
    document.write ('</TABLE>');
}

// ===================================================================================================
// highlight the "blob" at left of the navigation bar buttons (rollover mouse on)

function rolloverOn(dept)
{   if (is_nav4)
    {   document.layers[dept].visibility 	= "show";
        document.layers[dept].color 		= "gray";
    }
    else
    {   document.getElementById(dept).style.visibility 	= "visible";
        document.getElementById(dept).style.color 		= "gray";
    }
}

// ===================================================================================================
// hide the "blob" at left of the navigation bar buttons (rollover mouse off)

function rolloverOff(dept)
{   if (is_nav4)
    {   document.layers[dept].visibility 	= "hide";
        document.layers[dept].color 		= "gray";
    }
    else
    {   document.getElementById(dept).style.visibility 	= "hidden";
        document.getElementById(dept).style.color 		= "gray";
    }
}

// ===================================================================================================
// show the pop-up department menu from the navigation bar (rollover mouse on)

function menuOn(dept)
{   popup("menu0",1);
}

// ===================================================================================================
// hide the pop-up department menu from the navigation bar (rollover mouse off)

function menuOff(dept)
{   popdn();
}

// ===================================================================================================
// set new values into the navigation bar state cookies (current mode and department)

function changeState(newMode, newDept)
{	document.cookie="thamesdoc_mode="+newMode+"; path=/"; // domain defaults to that of the current page
	document.cookie="thamesdoc_dept="+newDept+"; path=/"; // domain defaults to that of the current page
	// alert ("Change in state to mode="+newMode+" dept="+newDept);
}

// ===================================================================================================
// Set a page variable

function setvar(lbl,v)
{	window.document.tdForm[lbl].value = v;
}

function set2vars(lbl1,lbl2,v)
{	window.document.tdForm[lbl1].value = v;
	window.document.tdForm[lbl2].value = v;
}

// ===================================================================================================
// check a checkbox

function setchk(lbl)
{	window.document.tdForm[lbl].checked=true;
}

// ===================================================================================================
// clear a checkbox

function clrchk(lbl)
{	window.document.tdForm[lbl].checked=false;
}

// ===================================================================================================
// Echo a page variable into the page text

function echoVar(lbl)
{	document.write(window.document.tdForm[lbl].value);
}

// ===================================================================================================
// Set a page variable then submit the form

function setAndSubmit(lbl,v)
{	setvar(lbl,v);
	window.document.tdForm.submit();
}
	
// ===================================================================================================
// Give an "are you sure?" message when the user clicks on a DELETE button

function rusure(url)
{	if (confirm('DELETE... Are you sure?')) 
	{	document.location.href = url;
	}
}

// ===================================================================================================
// Give an "are you sure?" message when the user clicks on a DELETE button, set page variable and submit

function rusureSetAndSubmit(lbl,v)
{	if (confirm('DELETE... Are you sure?')) 
	{	window.document.tdForm[lbl].value = v;
		window.document.tdForm.submit();
	}
}

// ===================================================================================================
// Fetch an object by name (to display/hide editing panels)

function getObject( obj ) 
{	if ( document.getElementById ) 
	{	obj = document.getElementById( obj );
	} 
	else if ( document.all ) 
	{	obj = document.all.item( obj );
	} 
	else
	{
    	obj = null;
  	}
  	return obj;
}

// ===================================================================================================
// Set object visible and move to the current mouse location (to display/hide editing panels)

function moveObject( obj, e ) 
{	var tempX = 0;
  	var tempY = 0;
  	var offset = 15;
  	var objHolder = obj;

  	obj = getObject( obj );
  	if (obj==null) return;

  	if (document.all) 
  	{ 	tempX = event.clientX + document.body.scrollLeft;
    	tempY = event.clientY + document.body.scrollTop;
  	} 
  	else 
  	{	tempX = e.pageX;
	    tempY = e.pageY;
  	}

	if (tempX < 0){tempX = 0}
  	if (tempY < 0){tempY = 0}

  	obj.style.top  = (tempY + offset) + 'px';
  	// obj.style.left = (tempX + offset) + 'px';
  	obj.style.left = '15%';

  	displayObject( objHolder, true );
}

// ===================================================================================================
// Set object [in]visible (to display/hide editing panels)

function displayObject( obj, show ) 
{	obj = getObject( obj );
  	if (obj==null) return;

  	obj.style.display = show ? 'block' : 'none';
  	obj.style.visibility = show ? 'visible' : 'hidden';
}

// ===================================================================================================
// Dynamically change the HREF= property for an <A> tag

function setHrefTarget(obj, tgt)
{	obj = getObject( obj );
  	if (obj==null) return;
  	obj.href = tgt;
  	
}

// ===================================================================================================
// Calculate the message payload for an SMS text field 

function calcSmsPayload(fldnameBody,fldnameCounter)
{	pl = 160 - (window.document.tdForm[fldnameBody].value.length);
	if (pl > 16) 
	{	rem = '('+pl+' remaining)';
		window.document.tdForm[fldnameBody].style['color']='black';
		window.document.tdForm[fldnameBody].style['backgroundColor']='white';
	}
	else if (pl > 0)
	{	rem = '(Only '+pl+' remaining)';
		window.document.tdForm[fldnameBody].style['color']='black';
		window.document.tdForm[fldnameBody].style['backgroundColor']='mistyrose';
	}
	else
	{	rem = '(None remaining)';
		window.document.tdForm[fldnameBody].style['color']='red';
		window.document.tdForm[fldnameBody].style['backgroundColor']='mistyrose';
	}
	window.document.tdForm[fldnameCounter].value = rem;
}

// ===================================================================================================
// Find scrolling offset in X and Y (cross-browser safe)

function ScrollPositionY ()
{	if (self.pageYOffset) 
		return self.pageYOffset;
	else if (document.documentElement && document.documentElement.scrollTop)
		return document.documentElement.scrollTop;
	else // if (document.body)
		return document.body.scrollTop;
}

function ScrollPositionX ()
{	if (self.pageXOffset) 
		return self.pageXOffset;
	else if (document.documentElement && document.documentElement.scrollLeft)
		return document.documentElement.scrollLeft;
	else // if (document.body)
		return document.body.scrollLeft;
}

// ===================================================================================================
// Find viewport dimensions (cross-browser safe)

function viewportX() 
{	if (self.innerWidth)
		return self.innerWidth;
	else if (document.documentElement && document.documentElement.clientWidth)
		return document.documentElement.clientWidth;
	else // if (document.body)
		return document.body.clientWidth;
}

function viewportY() 
{	if (self.innerHeight)
		return self.innerHeight;
	else if (document.documentElement && document.documentElement.clientHeight)
		return document.documentElement.clientHeight;
	else // if (document.body)
		return document.body.clientHeight;
}

// ===================================================================================================
// stop the background image (z-layer) from scrolling out of sight

function RepositionBackgroundImage() {
	getObject('backgroundDiv').style.top = ScrollPositionY();
}
window.onscroll=RepositionBackgroundImage;

// ===================================================================================================
// Scale the background image to match the display

function ScaleBackgroundImage (bgw,bgh) {
	var wratio = viewportX()/bgw;
	var hratio = viewportY()/bgh;
	
	var ratio = (wratio > hratio) ? wratio : hratio;
	
	var w = Math.round(bgw * ratio);
	var h = Math.round(bgh * ratio);
		
	obj = getObject( 'backgroundImage' );
	obj.height = h;	
	obj.width = w;
}

