﻿$( document ).ready( function ()
{
	// If we have Tabs, set how the transition is done
	jQuery( "#tabs" ).tabs( { fx: { opacity: "toggle", duration: "500", width: "toggle"} } );

	// Alternate row coloring for our tables
	jQuery( ".striped" ).children( "tr:even" ).addClass( "rowEven" );
	jQuery( ".striped" ).children( "tr:odd" ).addClass( "rowOdd" );

	// Fix up our links
	jQuery( "#content p a, #content ul a, #content ol a" ).each( function ( index )
	{
		if ( this.className == "" )
			this.className = "link";
	} );

	// If we're showing a form, set the default focus
	SetFocusToDefault();
} );

//***********************************************
// Set the focus to the default field if showing a form
function SetFocusToDefault()
{
	// See if we're displaying a form
	if ( document.getElementsByTagName( "form" ).length > 0 ) {
		// We have a hidden form field with the control that should have the focus
		var tag = document.getElementById( "hidFormFocus" );
		if ( tag != null ) {
			// Found it, set the focus there
			var hocus = document.getElementById( tag.value );
			if ( hocus != null )
				hocus.focus();
		}
	}
}

//***********************************************
// Set the focus to a particular type of form field
function SetFocusToType( type )
{
	// Check if we're displaying a form
	var forms = document.forms;
	if ( forms != null ) {
		// Loop through all the forms on the current page
		for ( var i = 0; i < forms.length; ++i ) {
			// Loop through all the elements in the current form
			var form = forms[i];
			for ( var j = 0; j < form.elements.length; ++j ) {
				// If this is the type of field we are looking for
				if ( form.elements[j].type == type ) {
					// Set the focus to the desired element
					form.elements[j].focus();
					break;
				}
			}
		}
	}
}

//***********************************************
// When we're showing the focus for a form item, set the class names for the DIV to show which field we're working on
function FormShowFocus( activeID )
{
	$( "#" + activeID ).removeClass( "formFieldInactive" ).addClass( "formFieldActive" );
}
// When moving off the field, put it back to inactive
function FormShowBlur( inactiveID )
{
	$( "#" + inactiveID ).removeClass( "formFieldActive" ).addClass( "formFieldInactive" );
}

//***********************************************
// Load more help into a control
var this_help_id = 0;
function LoadHelp( help )
{
	this_help_id = help;
	var r = $.ajax( { url: "WebService.asmx/GetHelp", cache: false, data: { "help_id": help }, type: "post", complete: ReturnHelp, error: ErrorHelp, async: true } );
	if ( r == null )
		$( "#helptext" ).html( "Error loading new Help content" );
}

//***********************************************
// When swapping out help text, save original content here
// Can restore the original help content
var help_title = "";
var help_text = "";
function RestoreHelp()
{
	$( "#helptitle" ).html( help_title );
	$( "#helptext" ).html( help_text );
}

//***********************************************
// Ajax call to get help text has returned
function ReturnHelp( r )
{
	// Check if the call has completed
	if ( r.readyState == 4 ) {
		// Check if we received a valid response
		if ( IsResponseValid( r ) ) {
			// Get the JSON return string
			var sJSON = GetResponseValue( r, "string" );
			if ( sJSON.length > 0 ) {
				// Parse the JSON string
				var o = eval( '(' + sJSON + ')' );
				var sOK = o.status;
				if ( sOK == "OK" ) {
					help_title = $( "#helptitle" ).html();
					help_text = $( "#helptext" ).html();
					var sTitle = o.title;
					var sText = o.text;
					var rq = /~q/g;
					var ra = /~a/g;
					var rr = /~r/g;
					var rn = /~n/g;
					sText = sText.replace( rq, "\"" );
					sText = sText.replace( ra, "'" );
					sText = sText.replace( rr, "\r" );
					sText = sText.replace( rn, "\n" );
					if ( this_help_id != help_id )
						sText += "<br /><img alt=\"return\" class=\"link\" title=\"Return to original help content\" onclick=\"RestoreHelp()\" src=\"Images/helpreturn.png\" />";
					$( "#helptitle" ).html( sTitle );
					$( "#helptext" ).html( sText );
				}
				else $( "#helptext" ).html( "The Help server returned: " + decodeURI( o.msg ) );
			}
			else $( "#helptext" ).html( "The Help server returned an empty string" );
		}
		else if ( IsResponseFailed( r ) )
			$( "#helptext" ).html( "The Help server returned status: " + r.status );
	}
}

//***********************************************
// Error trying to get the help content
function ErrorHelp( r )
{
	$( "#helptext" ).html( "I was unable to find the Help you are looking for" );
}

//***********************************************
// Get the Response return values
function GetResponseValue( r, t )
{
	var xml = r.responseXML;
	var x = xml.getElementsByTagName( t )[0];
	if ( ( x != null ) && ( x.firstChild != null ) )
		return ( x.firstChild.nodeValue );
	return ( "" );
}

//***********************************************
// Get the Ajax Response value as XML
function GetResponseXml( xml, t )
{
	var x = xml.getElementsByTagName( t )[0];
	return ( x.firstChild.nodeValue );
}

//***********************************************
// Check if a response is valid
function IsResponseValid( r )
{
	return ( ( ( r.status >= 200 ) && ( r.status <= 299 ) ) || ( r.status == 304 ) );
}

//***********************************************
// Check if a response failed
function IsResponseFailed( r )
{
	return ( ( r.status >= 500 ) && ( r.status <= 599 ) );
}

//***********************************************
// Toggle our help box to show or hide the content
function HelpToggle()
{
	var src = $( "#helpIcon" ).attr( "src" );
	if ( src == "Images/helpOpen.png" )
		$( "#helpContent" ).slideDown( 1000, function () { $( "#helpIcon" ).attr( "src", "Images/helpClose.png" ); } );
	else
		$( "#helpContent" ).slideUp( 1000, function () { $( "#helpIcon" ).attr( "src", "Images/helpOpen.png" ); } );
}

//***********************************************
// Format a boolean to Yes or No
function FormatYesNo( f )
{
	return ( ( f == 0 ) ? "No" : "Yes" );
}

//***********************************************
// Format a number - pass in a value and the number of decimal places desired
function FormatNumber( expr, decplaces )
{
	// Raise incoming value by power of 10 times the number of decimal places
	// Round to an integer and convert to a string
	var str = "" + Math.round( eval( expr ) * Math.pow( 10, decplaces ) );

	// Pad small value strings with zeroes to the left of rounded number
	while ( str.length <= decplaces )
		str = "0" + str;

	// Establish the location of the decimal point
	var decpoint = str.length - decplaces;

	// Assemble final result from
	// (a) the string up to the position of the decimal point
	// (b) the decimal point, and
	// (c) the balance of the string
	return ( str.substring( 0, decpoint ) + "." + str.substring( decpoint, str.length ) );
}

