
//----------------------------------------------------------------------
//-- Useful Page Functions
//----------------------------------------------------------------------

function JS_Utils_GetParent ( objElement )
{
	if ( objElement )
	{
		if ( objElement.parentElement ) { return objElement.parentElement };
		if ( objElement.parentNode ) { return objElement.parentNode };
		if ( objElement.parent ) { return objElement.parent };
		return false;
	}
	else
	{
		return false;
	}
}

function JS_Utils_GetParentElementByType(eElement, strTargetElement)
{
	/** Is caller the target? **/
	if(eElement.tagName != strTargetElement)
	{
		var eParent = JS_Utils_GetParent(eElement);
		var n = 0;
		
		while( eParent.tagName != strTargetElement)
		{
			/** Kill infiniloop **/
			if(n > 255){alert("No "+ strTargetElement +" found");return false;}
			/** Move up a level **/
			eParent = JS_Utils_GetParent(eParent);
			n++;
		}
		return eParent;
	}
	else
	return eElement;
}

function JS_Utils_ShowHide ( objElement, bShow )
{
	if ( bShow == null )
	{
		if ( objElement.style.display == "none" ) objElement.style.display = "block";
		else objElement.style.display = "none";
	}
	else
	{
		if ( bShow ) 	objElement.style.display = "block";
		else objElement.style.display = "none";
	}
	
}

//----------------------------------------------------------------------
//-- String Functions
//----------------------------------------------------------------------

function JS_Utils_Trim(s)
{
	return s.replace(/^\s+|\s+$/g,"");
}

function JS_Utils_LeftTrim(s)
{
	return s.replace(/^\s+/,"");
}

function JS_Utils_RightTrim(s)
{
	return s.replace(/\s+$/,"");
}

//----------------------------------------------------------------------
//-- Number Functions
//----------------------------------------------------------------------

function JS_Utils_CheckNumber( nNumber )
{
	if ( isNaN( nNumber ) )
		return false;
	else
		return nNumber;
}


function JS_Utils_RoundToHalf( fValue ) 
{
	var fConverted = parseFloat( fValue ); // Make sure we have a number
	var fDecimal = ( fConverted - parseInt( fConverted, 10 ));
   
	fDecimal = Math.round( fDecimal * 10);
   
	if ( fDecimal == 5 ) 
		return ( parseInt( fConverted, 10 )+0.5 ); 
   
	if ( ( fDecimal < 3 ) || ( fDecimal > 7 ) ) 
		return Math.round( fConverted );
	else 
		return ( parseInt( fConverted, 10 )+0.5 );
} 


//----------------------------------------------------------------------
//-- Time Functions
//----------------------------------------------------------------------

function JS_Utils_TimeToDecimal ( tTime )
{
	var fTime;
	var arTime;
		
	// Split string into hours and minutes
	arTime = tTime.split(':');
	
	
	nMinutes = ((arTime[1]/60)*100).toFixed(0);
	
	// Recompile hours and minutes as decimal
	fTime = arTime[0] + '.' + nMinutes;
	
	
	return fTime;
}


function JS_Utils_DecimalToTime ( fTime )
{
	var tTime;
	
	// Split string into hours and minutes
	arTime = fTime.split('.');
	
	nMinutes = (( arTime[1]/100 ) * 60 ).toFixed(0);
	
	if ( nMinutes < 10 )
		nMinutes = ('0' + nMinutes);
	
	// Recompile hours and minutes as time
	tTime = arTime[0] + ':' + nMinutes;	
	
	return tTime;
	
}
//----------------------------------------------------------------------
//-- Open Window
//----------------------------------------------------------------------

function JS_Utils_OpenWindow ( mypage, myname, w, h, scroll )
{
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',noresize'
	win = window.open(mypage, myname, winprops);
}

//----------------------------------------------------------------------
//-- Query String 
//----------------------------------------------------------------------

function JS_Utils_GetParameterByName( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return decodeURIComponent(results[1].replace(/\+/g, " "));
}


//----------------------------------------------------------------------
//-- Font size and cookie
//----------------------------------------------------------------------

function JS_Utils_ChangeFontSize ( nSize )
{
	switch ( nSize )
	{
		case 1 :
			document.body.style.fontSize = "70%";
			break;
			
		case 2 :
			document.body.style.fontSize = "80%";
			break;
			
		case 3 :
			document.body.style.fontSize = "90%";
			break;
			
		default :
			document.body.style.fontSize = "70%";
			break;
	}
	
	JS_Cookie_Create ( "IntrainingFontSize", nSize, 30 );
}

function JS_Utils_LoadFontSize ()
{
	nSize = Number ( JS_Cookie_Read ( "IntrainingFontSize" ) );
	JS_Utils_ChangeFontSize ( nSize );
}

//----------------------------------------------------------------------
//----------------------------------------------------------------------




