/*-------------------------------GLOBAL VARIABLES------------------------------------*/

var detect = navigator.userAgent.toLowerCase( );
var OS, browser, version, total, thestring;

/*-----------------------------------------------------------------------------------------------*/

//Browser detect script origionally created by Peter Paul Koch at http://www.quirksmode.org/

function getBrowserInfo( )
{
	if( checkIt( 'konqueror' ) )
	{
		browser = "Konqueror";
		OS = "Linux";
	}
	else if(  checkIt( 'safari'  ) ) browser = "Safari"
	else if(  checkIt( 'omniweb' ) ) browser = "OmniWeb"
	else if(  checkIt( 'opera'   ) ) browser = "Opera"
	else if(  checkIt( 'webtv'   ) ) browser = "WebTV";
	else if(  checkIt( 'icab'    ) ) browser = "iCab"
	else if(  checkIt( 'msie'    ) ) browser = "Internet Explorer"
	else if( !checkIt( 'compatible') )
	{
		browser = "Netscape Navigator"
		version = detect.charAt(8);
	}
	else browser = "An unknown browser";

	if( !version ) version = detect.charAt(place + thestring.length);

	if( !OS )
	{
		if( checkIt( 'linux' ) ) OS	= "Linux";
		else if (checkIt('x11')) OS	= "Unix";
		else if (checkIt('mac')) OS	= "Mac"
		else if (checkIt('win')) OS	= "Windows"
		else OS 					= "an unknown operating system";
	}
}

function checkIt(string) {
	place = detect.indexOf(string) + 1;
	thestring = string;
	return place;
}

/*-----------------------------------------------------------------------------------------------*/

preload_image = new Image( ); 
preload_image.src = WEBPATH + "images/default/core/spinner.gif";
preload_image.src = WEBPATH + "images/default/lightbox/nextlabel.gif";
preload_image.src = WEBPATH + "images/default/lightbox/prevlabel.gif";
	  
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 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/default/core/spinner' + img + '.gif" width="16" height="16" align="absmiddle" border="0" />' + str;
}

function showBigSpinner( text, image )
{
	var str = '';
	var img = '';
	
	if( text  != undefined ) { str = ' <span class="spinnerBigText">' + text + '</span>'; }
	if( image != undefined ) { img = image; }

	return str + ' <img src="' + WEBPATH + 'images/default/core/spinner' + img + '.gif" width="32" height="32" align="absmiddle" border="0" />';
}

function addslashes( str )
{
	str = str.replace( /\'/g, '\\\'');
	str = str.replace( /\"/g, '\\"');
	return str;
}

function stripslashes( str )
{
	str = str.replace( /\\'/g, '\'');
	str = str.replace( /\\"/g, '"');
	return str;
}

function arrayRemove( array, item )
{
	for( i=0; i<array.length; i++ )
	{
		if( item == array[i] ) array.splice( i, 1 );
    }
	
	return array;
}

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";
  	}
}

function scrollBottom( )
{
	dh = document.body.scrollHeight;
	ch = document.body.clientHeight;

	if( dh > ch )
	{
		move = dh-ch
		window.scrollTo( 0, move )
	}
}

// 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 urlencode( str )
{
	var HEX = "0123456789ABCDEF";
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	
	var encoded = "";
	
	for( var i=0; i<str.length; i++ )
	{
		var ch = str.charAt(i);
	    
		if( ch == " " )
		{
		    encoded += "+";				// x-www-urlencoded, rather than %20
		}
		else if( SAFECHARS.indexOf(ch) != -1 )
		{
		    encoded += ch;
		}
		else
		{
		    var charCode = ch.charCodeAt(0);
			
			if( charCode > 255 )
			{
				encoded += "+";
			}
			else
			{
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	}

	return encoded;
}

function urldecode( str )
{
	// Replace + with ' '
    // Replace %xx with equivalent character
   	// Put [ERROR] in output if %xx is invalid.
   	var HEXCHARS = "0123456789ABCDEFabcdef"; 
   	var encoded = str;
   	var plaintext = "";
   	var i = 0;
   
   	while( i < encoded.length )
   	{
		var ch = encoded.charAt(i);
	   
	   	if( ch == "+" )
	   	{
			plaintext += " ";
		   	i++;
	   	}
	   	else if( ch == "%" )
	   	{
			if( i < (encoded.length-2) && HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 && HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 )
		   	{
				plaintext += unescape( encoded.substr(i,3) );
			   	i += 3;
		   	}
		   	else
		   	{
				//alert( 'Bad escape combination near ...' + encoded.substr(i) );
			   	plaintext += "%[ERROR]";
			   	i++;
			}
		}
		else
		{
			plaintext += ch;
		   	i++;
		}
	}
	
   	return plaintext;
}

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.
//
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;
}

function MM_jumpMenu(targ,selObj,restore)
{
	$(targ).contentWindow.location.replace(selObj.options[selObj.selectedIndex].value);
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}