/* - - - - - - - - - - - Edit history
	Purpose:  Common JavaScript functions
	Author:	Chuck Vaughnn
	Copyright 2003 Vaughnn Associates. All rights reserved.
	Modified  2004 for master's degree research project
	Modified  2005 for Ed Web site
	Modified  2006 for database modifications
	Modified  2007 for XHTML 1.0 Strict
	Modified  Oct 2007, added common menu navigation functions
	Modified  Nov 2009, modified for vaughnnDotCom applications
	modified  Sep 2010 new interface design
*/

// - - - - - - - - - - - global variables
var hsCount = 201;				// this counter is used for hideShow objects
var lastHideShowId = "";		// holds id of last selected hideShow <div>
var linkCount = 401;			// this counter is used for help (window) objects
var blockCount = 401;			// this counter is used for help blocks
var lastHelpId = "";			// holds id of last selected help <div>

// - - - - - - - - - - - functions switching between secure and unsecure Web sites
//	Connect to locn using SSL (secure} connection
function goSecure( locn ) {			
	if( location.hostname == 'localhost' ) {		// if using localhost, skip https protocol
		location.href = locn;
		return true;
	}
	location.href = 'https://vaugh2.securesites.com/' + locn;
	return true;
}

//	connect to an un-secure connection
function goUnSecure( locn ) {						
	if( location.hostname == 'localhost' ) {			// local just use locn as URL
		location.href = locn;
		return true;
	}
	else {
		location.href = 'http://vaughnn.com/'+locn;		// use remote URL
		return true;
	}
}

// - - - - - - - - - - - functions for opening a new window

function openNewWin( locn,linkText ) {			//	insert document code to open a new browser window
	with( document ) {
		writeln('<a class="external" href="'+locn+
			'" onclick="window.open(this.href); return false;" '+
			'onkeypress="window.open(this.href); return false;" '+
			'title="Opens a new browser window">'+linkText+'</a>');
	}
}

function openSecureWin( locn,linkText ) {		//	insert document code to open a new browser window
	with( document ) {
		writeln('<a class="external" href="https://vaugh1.sslcert14.com'+locn+
			'" onclick="window.open(this.href); return false;" '+
			'onkeypress="window.open(this.href); return false;" '+
			'title="Opens a new browser window">'+linkText+'</a>');
	}
}

// - - - - - - - - - - - functions supporting hide/show objects

function insertHS( objTitle ) {								// insert a hide/show object into document
	var objID = "obj"+hsCount++;							// hsCount is a qlobal variable
	with( document ) {
		writeln('<p class="hideShowHead"><a class="hideShow" ');
		if( typeof(objTitle) == "undefined" )
			writeln('href="javaScript:hideShow(\''+objID+'\')" title="Click to show, click again to hide">Show hidden information</a></p>');
		else
			writeln('href="javaScript:hideShow(\''+objID+'\')" title="Click to show, click again to hide">'+objTitle+'</a></p>');
			writeln('<div style="margin-top: 0px; margin-left: 1em;" class=objHide id="'+objID+'">');
	}

}

function hideShow( objId ) {								// toggle menu group div between hide and show
	//alert(objId + " ,  " + lastHideShowId );
	var obj;
	if( lastHideShowId != "" && objId != lastHideShowId ) {	// if prior Id is empty or same as current
		obj = getObj( lastHideShowId );						// hide it
		obj.className = "objHide";
	}
	obj = getObj( objId );									// now display current object
	obj.className = ( obj.className == "objHide" ) ? "objShow" : "objHide";
	lastHideShowId = objId;									// save (current) id to lastHideShowId
}


function insertEndDiv() {									// write a closing div for insertHS and insertHSinline functions
	document.writeln('</div>');
}


function getObj( objId ) {						// get an object by its ID
	var obj = document.getElementById( objId );
	if( obj ) return obj;
	// an error occured ...
	alert ( "getObj( id )-> id  \""+objId+"\"  not found");
	return null;
}
/*  
function insertHelpLink() {					// insert help link
	var helpId = "help"+linkCount++;		// linkCount is a qlobal variable
	//alert( helpId );
	with ( document ) {
		writeln('<div style="text-align: right; margin-right: 12px; padding: 4px">');
		writeln('<a href="javaScript:hideShowHelp(\''+helpId+'\')" title="Click to hide/show help">');
		writeln('<img src="images/showHelpUp.png" width="15" height="16" style="vertical-align: middle;" ');
		writeln('onmouseover=src="images/showHelpDn.png" onmouseout=src="images/showHelpUp.png"></a></div>');
	}
}
*/
/* 
function showHelpDiv( helpId ) {							// show object
	//alert( 'showHelpDiv is using '+helpId );
	helpObj = getObj( helpId );
	helpObj.className = "objShow"; 
	lastHelpId = helpId;
}

function hideHelpDiv( helpId ) {							// hide object
	//alert( 'hideHelpDiv is using '+helpId );
	helpObj = getObj( helpId );
	helpObj.className = "objHide";
	lastHelpId = "";	
}
 */
/* 
function insertHelpBlock() {				// insert help block
	var blockId = "help"+blockCount++;		// blockCount is a qlobal variable
	//alert( blockId );
	with( document ) {
		writeln('<div id="'+blockId+'" class="objHide" style="position: absolute; left: 40px; top: 120px;');
		writeln('background: #F0F8FF; border: medium outset; width: 550px; padding: 6px;">');
		writeln('<div><p style="margin-top: 0px; font-size: 0.96em; text-align: right">');
		writeln('<a href="javascript:hideHelpDiv(\''+blockId+'\')";">Close form help</a>');
		writeln('<hr noshade style="footer" /></p></div>');
	}
}
 */
function hideShowHelp( objId ) {				// toggle menu group div between hide and show
	if( lastHelpId == objId) {					// just close current block
		hideHelpDiv( lastHelpId );
	}
	else if( lastHelpId != "" ) {				// close last, and then open new
		hideHelpDiv( lastHelpId );
		showHelpDiv( objId );
	}
	else
		 showHelpDiv( objId );					// just open new
}


// - - - - - - - - - - - functions supporting common page objects
/* 
function insertFooter() {					// insert page footer
	with( document ) {
		writeln('<div class="botDiv">');
		writeln('<div style="float: right;"><p>&copy; 2007 - 2009 by Vaughnn Associates. Web presence since 1998.</p></div>');
		writeln('<P>Last modified '+ document.lastModified+'</p></div>');
	}
}
 */
/*  
// - - - - - - - - - - - functions supporting navigation menus

//	initialize menu group objects

// menu groups with sub-ordinate menus
function toggleMenuGroup( groupNumber ) {
	lastGroupNumber = groupNumber;
	groupId = getObj( 'menuGroup'+ groupNumber );
	//cName = (groupId.className);
	groupId.className = (groupId.className=='objHide') ? groupId.className = 'objShow' :  groupId.className = 'objHide'
}

// collapse all menu groups
function collapseMenus() {
	for( var i=0; i < menuGroups.length; i++ ) {
		//alert( i+', '+ menuGroups[i] );
		if( menuGroups[i] )
			menuGroups[i].className = 'objHide';
	}
}

*/
// - - - - - - - - - - - functions supporting dynamic page modification

/*	insert HTML code in to a span element...
		<span ID="objectID">"msgText"</span>
*/
function insertHTML( objectID, msgText ) {		// insert text using innerHTML
	//alert( objectID +' , '+msgText );
	var innerObj = getObj( objectID );
	innerObj.innerHTML = msgText;
}

// - - - - - - - - - - - functions supporting timer display
// global variables for timer functions
var timerInterval = 1000;				// in Mill-sec 
var timerIncr = timerInterval / 1000;
var timerMin = 45;
//var timerMin = 1;		testing value
var timerSec = 0;
var si;

function startTimer() {							// start the timer
	si = setInterval ( "timerShow()", timerInterval );
	insertHTML( "sess_time", '45 min : 00 sec' );
}

function stopTimer() {							// stop the timer
	clearTimeout( si );
}

function timerShow() {							// display timerValue
	timerSec = timerSec-timerIncr;
	if( timerSec < 0) {
		timerSec = 60 - timerIncr;
		timerMin--;
	}
	if( timerMin < 0 ) {
		clearTimeout( si );
		goNext("/cousins/logout.php");
	}
	tempSec = ( timerSec < 10 ) ? '0'+timerSec : timerSec;
	tempMin = ( timerMin < 10 ) ? '0'+timerMin : timerMin;
	insertHTML( "sess_time", tempMin+' min : '+tempSec+' sec' );
}


/* - - - - - - - - - - - functions for opening new pages
not sure if these function will be used

function insertOpenNewWin( site_dir_page,linkText ) {			//	insert document code to open a new browser window
	with( document ) {
		writeln('<a class="external" href="'+site_dir_page+
				'" onclick="window.open(this.href); return false;" '+
				'onkeypress="window.open(this.href); return false;" '+
				'title="Opens a new browser window">'+linkText+'</a>');
	}
}

// - - - - - - - - - - - functions miscellaneous
//var winOptions = 'width=900,height=680,status=yes,scrollbars=yes,resizable=yes';
var winOptions = 'status=yes,scrollbars=yes,resizable=yes';
var childWin = null;
function openMailWindow( pageURL,pageTitle) {
	alert( pageURL+' , '+pageTitle);
	if( childWin != null ) {
		childWin.close();
		childWin = null;
	}
	childWin = window.open( pageURL,pageTitle,winOptions );
}
*/
