/*
switchTab( aTab ) ;
MM_preloadImages() ;
MM_swapImage() ;
MM_swapImgRestore() ;
MM_findObj() ;

closeWin() ;
popWin() ;
popMov( aProjectID, aMovieFilename ) ;
popBoard( aProjectID, aBoardFilename ) ;

addToForm( aLinkObj, aFormElement ) ;
chkLength( aFormElement, aLabel, aMinChars, aMaxChars ) ;
trim( aStr ) ;
*/


/****** GENERIC JS FUNCTION *******/



// switches to a new tab, with the current serach string intact.
function switchTab( aTab, aQuery ) {
	var tUrl = aTab ;
	tUrl += (location.search) ? location.search : "?" ;
	if( aQuery && aQuery.indexOf( 'aID' ) != -1 ) {
		var tReg = new RegExp( '&?aID=[0-9]+','g' ) ;
		tUrl = tUrl.replace( tReg, '' ) ;
	}
	if( aQuery && aQuery.indexOf( 'fID' ) != -1 ) {
		var tReg = new RegExp( '&?fID=[0-9]+','g' ) ;
		tUrl = tUrl.replace( tReg, '' ) ;
	}
	if( aQuery && aQuery.indexOf( 'aS' ) != -1 ) {
		var tReg = new RegExp( '&?aS=[0-9]+','g' ) ;
		tUrl = tUrl.replace( tReg, '' ) ;
	}
	if( aQuery ) tUrl += '&' + aQuery ;
	location.href= tUrl ;
}
// preload images
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];}}
}
// image rollover
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];}
}
// restore image rollovers
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;
}
// search for a specific page element by ID
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;
}






/******* WINDOW OPERATIONS ********/
// simply closes a window.
// focuses the windows parent/opener if available.
function closeWin( aWin ) {
	if( !aWin ) { aWin = window ; } ;
	if( aWin.opener ) { aWin.opener.focus() ; }
	aWin.close() ;
}
function popWin( aUrl, aName, aWidth, aHeight, aLocP, aResizeP, aX, aY ) {
	
	var tFeatures = 'width='         + aWidth +
					',height='       + aHeight + 
					',innerwidth='   + aWidth +
					',innerheight='  + aHeight +
					',location='     + aLocP + 
					',toolbar=0'     +
					',directories=0' +
					',menubar=1'     +
					',scrollbars=auto'  +
					',resizable='    + aResizeP ;
	tPopUp = open( aUrl, aName, tFeatures ) ;
	tPopUp.focus() ;
	if( !tPopUp.opener ) tPopUp.opener = this ;
}
// opens a specific window
function popMov( aID, aMovie ) {
	var tUrl = 'movie.php?aID=' + aID + '&aMovie=' + aMovie ;
	popWin( tUrl, 'movietime',400,405,0,0, 300,300  ) ;
}
function popBoard( aID, bID ) {
	var tUrl = 'board.php?aID=' + aID + '&bID=' + bID + '&' ;
	popWin( tUrl, 'boards',700,350,0,0, 200,200  ) ;
}




/***** FROMS ******/



// adds the text of a link object to the form element aFormEle.
// Will remove it again, if called again with the same elements.
//  aEle = link object.
//  aFormEle = form input type text.
function addToForm( aText, aFormEle ) {
	var tStr = aFormEle.value ;
	if(tStr=="ENTER KEYWORDS") tStr='' ;
	
	aText = aText.toLowerCase() ;
	// add a space, unless:
	// - it is the first entry
	// - the user added a space by himself
	if( tStr.length > 0 && tStr.substr( tStr.length-1, 1 ) != ' ' ) {
		tStr += ' ' ;
	}	
	var tPos = tStr.indexOf( aText ) ;
	if( tPos >= 0 ) {
		// we have a match. Remove it.
		var tL = tStr.split( aText ) ;
		tStr = tL.join( '' ) ;
	} else {
		tStr += aText ;
	}
	// reinsert the search string
	aFormEle.value = tStr.toLowerCase() ;
	aFormEle.mod = true ;
	// aFormEle.focus() ; 
	// DO NOT FOCUS. Otherwise you have to click on every keyword link TWICE.
	// ONCE to defocus the form, and a second time to select the keyword.	
}
// checks whether a form element is within a string length range.
//  aEle  = form element to check.
//  aName = is the name to give when generating alert text.
//  aMin  = minimum characters.
//  aMax  = maximum characters.
function chkLength( aEle, aName, aMin, aMax ) {
	aEle.value = trim( aEle.value ) ;
	var tLen = aEle.value.length ;
	if( tLen < aMin ) {
		if( !tErrEle ) tErrEle = aEle ;
		return( 'The "' + aName + '" field is required.\n' ) ;
	} else if( tLen > aMax ) {
		if( !tErrEle ) tErrEle = aEle ;
		return( 'The "' + aName + '" field can only hold ' + aMax +
				' characters {currently ' + tLen + '}.\n' ) ;
	}
	return( '' ) ;
}
// returns the string 'aStr' without leading and trailing spaces.
function trim( aStr ) { 
    // this will get rid of leading spaces 
    while( aStr.substring( 0,1 ) == ' ' ) 
        aStr = aStr.substring( 1, aStr.length ) ;

    // this will get rid of trailing spaces 
    while( aStr.substring( aStr.length-1, aStr.length ) == ' ' )
        aStr = aStr.substring( 0, aStr.length-1 ) ;

   return( aStr ) ;
}
// get the sorting preferences form the bottom pop ups on search page
function getSortPrefs() {
	var tStr = "" ;
	tStr = "&aSortBy=" + document.sort.aSortBy.value ;
	tStr += "&aDirection=" + document.sort.aDirection.value ;
	return( tStr ) ;
}
