
function visit( linkTo )
{
	window.location = linkTo;
}

function windowOnload( f )
{
    var prev = window.onload;
    window.onload = function( ) { if( prev ) prev( ); f( ); }
}


var editing = false;
var whitespace = " \t\n\r";

// Check whether string s is empty.
function isEmpty( s ) { return ((s == null) || (s.length == 0)) }

function isWhitespace( s )
{
	var i;

	// Is s empty?
    if( isEmpty( s ) ) return true;

    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.

    for( i=0; i<s.length; i++ )
    {
		// Check that current character isn't whitespace.
        var c = s.charAt(i);

        if( whitespace.indexOf(c) == -1 ) return false;
    }

    // All characters are whitespace.
    return true;
}

function showDotted( text, image )
{
	var str = '';
	var img = '';
	
	if( text  != undefined ) { str = ' <span class="spinnerText">' + text + '</span>'; }
	if( image != undefined ) { img = image; }

	return '<img src="' + WEBPATH + 'images/dots_loading' + img + '.gif" width="21" height="5" align="absmiddle" border="0" />' + str;
}


function showSpinner( text, image )
{
	var str = '';
	var img = '';
	
	if( text  != undefined ) { str = ' <span class="spinnerText">' + text + '</span>'; }
	if( image != undefined ) { img = image; }

	return '<img src="' + WEBPATH + 'images/spinner' + img + '.gif" width="16" height="16" align="absmiddle" border="0" />' + str;
}

function highlight( id, currbgcolor )
{
	var tr   = $( 'row' + id );
	checkbox = $( 'chk' + id );
   	
	if( tr.style )
	{
    	if( checkbox.checked )
		{
        	tr.className = "rowHighlight";
      	}
		else
		{
         	tr.className = '';
      	}
   	}
}

/*
   name - name of the cookie
   value - value of the cookie
   [expires] - expiration date of the cookie
     (defaults to end of current session)
   [path] - path for which the cookie is valid
     (defaults to path of calling document)
   [domain] - domain for which the cookie is valid
     (defaults to domain of calling document)
   [secure] - Boolean value indicating if the cookie transmission requires
     a secure transmission
   * an argument defaults when it is assigned null as a placeholder
   * a null placeholder is not required for trailing omitted arguments
*/

function setCookie( name, value, expires, path, domain, secure )
{
	var curCookie = name + "=" + escape( value ) +
      ( (expires) ? "; expires=" + expires.toGMTString( ) : "" ) +
      ( (path)    ? "; path=" + path : "" ) +
      ( (domain)  ? "; domain=" + domain : "" ) +
      ( (secure)  ? "; secure" : "" );
  	document.cookie = curCookie;
}


/*
  name - name of the desired cookie
  return string containing value of specified cookie or null
  if cookie does not exist
*/
function getCookie( name )
{
	var dc = document.cookie;
  	var prefix = name + "=";
  	var begin = dc.indexOf( "; " + prefix );
  
  	if( begin == -1 ) 
	{
    	begin = dc.indexOf(prefix);
    	if( begin != 0 ) return null;
  	}
	else begin += 2;
  	var end = document.cookie.indexOf( ";", begin );
  	if( end == -1 )
    end = dc.length;
  	return unescape( dc.substring( begin + prefix.length, end ) );
}


/*
   name - name of the cookie
   [path] - path of the cookie (must be same as path used to create cookie)
   [domain] - domain of the cookie (must be same as domain used to
     create cookie)
   path and domain default if assigned null or omitted if no explicit
     argument proceeds
*/
function deleteCookie( name, path, domain )
{
  	if( getCookie(name) )
	{
		document.cookie = name + "=" + ((path) ? "; path=" + path : "") +
    	((domain) ? "; domain=" + domain : "") + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  	}
}

// date - any instance of the Date object
// * hand all instances of the Date object to this function for "repairs"

function fixDate( date )
{
	var base = new Date( 0 );
	var skew = base.getTime( );
  	if( skew > 0 ) date.setTime( date.getTime( ) - skew );
}

function checkers( formName )
{
	for( var i=0; i<formName.elements.length; i++ )
	{
		var e = formName.elements[i];
		
		if( e.checked )
		{
			e.checked = false;
		}
		else
		{
			e.checked = true;
		}
	}
}


function gotCheck( formName )
{
	checked=0;
	
	for( var i=0; i<formName.elements.length; i++ )
	{
		var e = formName.elements[i];
		
		if( e.checked )
		{
			checked = 1;
		}
	}
	
	return checked;
}

function toggleRow( id )
{
	 if( $(id).style.display == 'none' )
     {
          $(id).style.display = '';
     }
	 else
	 {
		 $(id).style.display = 'none';
	 }
}

function textCounter( field, countfield, maxlimit )
{
    if( field.value.length > maxlimit ) 
    {
    	field.value = field.value.substring( 0, maxlimit );
    }
    else
    {
    	$(countfield).innerHTML = maxlimit - field.value.length;
    }
}

function bgSwitch( ac, td )
{
	if( ac == 'on' )
	{
		td.style.background = '#ffc';
		td.style.cursor = 'text';
	}
	else if( ac == 'off' )
	{
		td.style.background = '';
	}
}

function showPic( id, whichpic )
{
	$('placeholder' + id).src = whichpic.href;
}

function getTopPos( inputObj )
{
	var returnValue = inputObj.offsetTop;
	
	while( ( inputObj = inputObj.offsetParent ) != null )
	{
		if(inputObj.tagName!='HTML')returnValue += inputObj.offsetTop;
  	}
	
	return returnValue;
}

function getLeftPos( inputObj )
{
	var returnValue = inputObj.offsetLeft;
	
	while( ( inputObj = inputObj.offsetParent ) != null )
	{
		if( inputObj.tagName!='HTML' ) returnValue += inputObj.offsetLeft;
  	}
	
	return returnValue;
}


function logout( )
{
	var objBody = document.getElementsByTagName("body").item(0);
	
	// create overlay div and hardcode some functional styles (aesthetic styles are in CSS file)
	var objOverlay = document.createElement("div");
	objOverlay.setAttribute('id','imgoverlay');
	objOverlay.style.display = '';
	objOverlay.style.position = 'absolute';
	objOverlay.style.top = '0';
	objOverlay.style.left = '0';
	objOverlay.style.zIndex = '90';
 	objOverlay.style.width = '100%';
	objBody.insertBefore(objOverlay, objBody.firstChild);
	
	var arrayPageSize   = getPageSize( );
	var arrayPageScroll = getPageScroll( );
	
	// set height of Overlay to take up whole page and show
	objOverlay.style.height  = (arrayPageSize[1] + 'px');
	objOverlay.style.display = 'block';
	
	var conf = confirm('Are you sure you want to logout?');
	
	if( conf )
	{
		return true;
	}
	else
	{
		objOverlay.style.display = 'none';
		return false;
	}
}

//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize( )
{	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}


//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
//
function getPageScroll( )
{
	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}

