function documentLoad()
{
    
}

function bodyLoad()
{

}

function showWait()
{
	document.body.style.cursor = 'wait';
	var links = document.links;
	for ( var i = 0; i < links.length; i++ )
	{
		links[i].style.cursor = 'wait';
	}
}

var lastScript = '';

//shortcuts
document.onkeydown = function ( evt )
{
    if ( evt == null )
        evt = event;
        
	if ( testKeyCode( evt, 120 ) && evt.altKey && evt.ctrlKey ) // Control + Alt +  F9
	{
		if ( document.getElementById( 'ActionFrame' ) != undefined )
		{
			if ( document.getElementById( 'ActionFrame' ).style.display == "none" )
				document.getElementById( 'ActionFrame' ).style.display = "inline";
			else
				document.getElementById( 'ActionFrame' ).style.display = "none";
		}
		else
			alert( "No debug windows available for this screen" );
	}
	else if ( testKeyCode( evt, 118 ) && evt.altKey && evt.ctrlKey ) // Control + Alt +  F7
	{
		var curScript = prompt( "Your Entry?", lastScript );
		
		if ( curScript != null )
		{
			lastScript = curScript;
			eval( lastScript );
		}
	}
}

function goAsync( location, doShowWait ) 
{ 
	if ( doShowWait != null && doShowWait ) 
		showWait();
	
	document.getElementById( 'ActionFrame' ).contentWindow.location.href = 
		"r.sh?content=" + location; 
}

function openJericho()
{
    if ( window.location.href.indexOf( 'startasystems.com' ) < 0 &&
        window.location.href.indexOf( 'startasystem.com' ) < 0 )
        window.open( "../jericho/index.sh?lpid=1" );
    else
	    window.open( "http://www.startadev.com/jericho/index.sh?lpid=1" );
}

function commaStrip( Text )
{
	return Text.replace( /,/g, "" ).replace( /\$/g, "" ).replace( /\%/g, "" );			
}

function commaFormat( Number, IsInteger )
{
	var index;
	if ( IsInteger && Number.lastIndexOf( "." ) > 0 )
	{
		Number	= Number.substring( 0, Number.lastIndexOf( "." ) );
		index	= Number.length - 3;
	}
	else
	{
		if ( Number.lastIndexOf( "." ) > 0 )
		{
			if ( Number.length - Number.lastIndexOf( "." ) > 2 )
				Number	= Number.substring( 0, Number.lastIndexOf( "." )+ 3 );

			index = Number.lastIndexOf( "." ) - 3;
		}
		else
			index = Number.length - 3;
	}

	while ( index > 0 )
	{
		Number = Number.slice( 0, index ) + "," + Number.slice( index, Number.length );
		index -= 3;
	}

	return Number;
}

function isInteger( Text )
{
	return Text.indexOf( "." ) == -1;		
}

//
//Completely validate a string-type control
//
function validateString( Control, ControlName, MaxLength, IsRequired, IsAlphanumericOnly )
{
	if ( Control == null )
	{
		alert( "Warning: Field " + ControlName + " is missing." );
		return true;
	}
	
	if ( MaxLength == null )
	{
		MaxLength			= 16000000000; //largest range allowable by text
	}
	
	if ( Control.value.length > MaxLength )
	{
		alert( ControlName + " is " + Control.value.length + 
			" characters long, but may only be " + MaxLength + "characters long." );
		return;
	}
	
	if ( !isStringBlank( Control.value ) )
	{
		if ( !IsAlphanumericOnly )
		{
			if ( !isValidString( Control.value ) )
			{
				alert(  ControlName + " is not valid.\n\n(< and > are not allowable characters)" );
				Control.select();
				return false;
			}
		}
		else
		{
			if ( !isAlphaNumericString( Control.value ) )
			{
				alert(  ControlName + " must only contain letters and numbers." );
				Control.select();
				return false;
			}
		}
	}
	else if ( IsRequired )
	{
		alert( ControlName + " is required." );
		Control.select();
		return false;
	}
	
	return true;
}

function isStringBlank( String )
{
	return ( String.replace( /\s/g, "").length == 0 );
}

function isValidString( Text )
{
	return true;
}

function isAlphaNumericString( Text )
{
	if ( !isValidString( Text ) )
		return false;
	if ( Text.indexOf( "?" ) != -1 )
		return false;
	if ( Text.indexOf( "&" ) != -1 )
		return false;
	if ( Text.indexOf( "=" ) != -1 )
		return false;
	if ( Text.indexOf( "/" ) != -1 )
		return false;
	if ( Text.indexOf( "\\" ) != -1 )
		return false;
	if ( Text.indexOf( "@" ) != -1 )
		return false;
	if ( Text.indexOf( "\"" ) != -1 )
		return false;
	if ( Text.indexOf( "'" ) != -1 )
		return false;
	if ( Text.indexOf( "!" ) != -1 )
		return false;
	if ( Text.indexOf( "$" ) != -1 )
		return false;
	if ( Text.indexOf( "%" ) != -1 )
		return false;
	if ( Text.indexOf( "^" ) != -1 )
		return false;
	if ( Text.indexOf( "*" ) != -1 )
		return false;
	if ( Text.indexOf( "(" ) != -1 )
		return false;
	if ( Text.indexOf( ")" ) != -1 )
		return false;
	if ( Text.indexOf( "+" ) != -1 )
		return false;
	if ( Text.indexOf( "{" ) != -1 )
		return false;
	if ( Text.indexOf( "}" ) != -1 )
		return false;
	if ( Text.indexOf( "[" ) != -1 )
		return false;
	if ( Text.indexOf( "]" ) != -1 )
		return false;
	if ( Text.indexOf( "|" ) != -1 )
		return false;
	if ( Text.indexOf( ":" ) != -1 )
		return false;
	if ( Text.indexOf( ";" ) != -1 )
		return false;
	if ( Text.indexOf( "_" ) != -1 )
		return false;
	if ( Text.indexOf( "`" ) != -1 )
		return false;
	if ( Text.indexOf( "~" ) != -1 )
		return false;
		
	return true;
}

//
//Completely validate an email address control
//
function validateEmail( Control, ControlName, MaxLength, IsRequired )
{
	if ( Control == null )
	{
		alert( "Warning: Field " + ControlName + " is missing." );
		return true;
	}
	
	if ( MaxLength == null )
	{
		MaxLength			= 16000000000; //largest range allowable by text
	}
	
	if ( Control.value.length > MaxLength )
	{
		alert( ControlName + " is " + Control.value.length + 
			" characters long, but may only be " + MaxLength + "characters long." );
		return;
	}
	
	if ( Control.value != "" )
	{
		if ( !isEmailAddress( Control.value  ) )
		{
			alert(  ControlName + " is not a valid email address." );
			Control.select();
			return false;
		}
	}
	else if ( IsRequired && Control.value == "" )
	{
		alert( ControlName + " is required." );
		Control.select();
		return false;
	}
	
	return true;
}

function isEmailAddress( Text )
{
	if ( !isValidEmail( Text ) )
		return false;
		
	if ( Text.indexOf( '@' ) <= 0 )
		return false;
	if ( Text.lastIndexOf( '.' ) <= Text.indexOf( '@' ) + 1 )
		return false;
	if ( Text.lastIndexOf( '.' ) == Text.length - 1 )
		return false;

	return true;
}


function isValidEmail( Text )
{
	if ( !isValidString( Text ) )
		return false;
	if ( Text.indexOf( "?" ) != -1 )
		return false;
	if ( Text.indexOf( "&" ) != -1 )
		return false;
	if ( Text.indexOf( "=" ) != -1 )
		return false;
	if ( Text.indexOf( "/" ) != -1 )
		return false;
	if ( Text.indexOf( "\\" ) != -1 )
		return false;
	if ( Text.indexOf( "\"" ) != -1 )
		return false;
	if ( Text.indexOf( "'" ) != -1 )
		return false;
	if ( Text.indexOf( "!" ) != -1 )
		return false;
	if ( Text.indexOf( "$" ) != -1 )
		return false;
	if ( Text.indexOf( "%" ) != -1 )
		return false;
	if ( Text.indexOf( "^" ) != -1 )
		return false;
	if ( Text.indexOf( "*" ) != -1 )
		return false;
	if ( Text.indexOf( "(" ) != -1 )
		return false;
	if ( Text.indexOf( ")" ) != -1 )
		return false;
	if ( Text.indexOf( "+" ) != -1 )
		return false;
	if ( Text.indexOf( "{" ) != -1 )
		return false;
	if ( Text.indexOf( "}" ) != -1 )
		return false;
	if ( Text.indexOf( "[" ) != -1 )
		return false;
	if ( Text.indexOf( "]" ) != -1 )
		return false;
	if ( Text.indexOf( "|" ) != -1 )
		return false;
	if ( Text.indexOf( ":" ) != -1 )
		return false;
	if ( Text.indexOf( ";" ) != -1 )
		return false;
	if ( Text.indexOf( "`" ) != -1 )
		return false;
		
	return true;
}

//
//Completely validate a phone number control
//
function validatePhone( ControlID, ControlName, IsRequired, IncludesExtension )
{
	var areaCode = document.getElementById( ControlID + "+AreaCode" );
	var prefix = document.getElementById( ControlID + "+Prefix" );
	var fourDigits = document.getElementById( ControlID + "+FourDigits" );
	var extension = ( IncludesExtension ? document.getElementById( ControlID + "+Ext" ) : null );
	
	if ( areaCode == null )
	{
		alert( "Warning: Field " + ControlName + " is missing." );
		return true;
	}
	
	if ( areaCode.value.length > 0 || prefix.value.length > 0 || fourDigits.value.length > 0 ||
			( IncludesExtension && extension.value.length > 0 ) )
	{
		if ( areaCode.value.length < 3 || isNaN( areaCode.value ) )
		{
			alert( ControlName + " Area Code is not valid." );
			areaCode.select();
			return false;
		}
		
		if ( prefix.value.length < 3 || isNaN( prefix.value ) )
		{
			alert( ControlName + " Office Code is not valid." );
			prefix.select();
			return false;
		}
		
		if ( fourDigits.value.length < 4 || isNaN( fourDigits.value ) )
		{
			alert( ControlName + " Node Code is not valid." );
			fourDigits.select();
			return false;
		}
		
		if ( IncludesExtension && isNaN( extension.value ) )
		{
			alert( ControlName + " Extension is not valid." );
			extension.select();
			return false;
		}
	}
	else if ( IsRequired )
	{
		alert( ControlName + " is required." );
		areaCode.select();
		return false;
	}
	
	return true;
}

//
//Make the item field value match the source field value, if the item field value is empty
//
function syncFields( source, item, truncateEmail )
{
	if ( source != undefined )
		if ( item.value.length == 0 )
		{
			if ( truncateEmail && source.value.indexOf( '@' ) >= 0 )
				item.value = source.value.substring( 0, source.value.indexOf( '@' ) );
			else
				item.value = source.value;
		}
}

//
//Enforce a maxlength parameter on textareas
//
function checkAreaLength( Text, Length )
{
	if ( Text.value.length > Length )
		Text.value = Text.value.substring( 0, Length );
}

function bodyMouseDown()
{

}

function pageUnload()
{

}

function getEventTarget( evt )
{
	if ( evt == undefined )
		return null;
	if ( evt.srcElement != null )
		return evt.srcElement;
	else
		return ( evt.target.nodeType == 3 ? evt.target.parentNode : evt.target );
}

function testKeyCode( evt, intKeyCode )
{
    if ( window.createPopup )
        return evt.keyCode == intKeyCode;
    else
        return evt.which == intKeyCode;
}

function cancelKeyEvent( evt )
{
    if ( window.createPopup )
        evt.keyCode = 0;
    else
	    evt.preventDefault();
}

function taF( evt )
{
}

function goP( location ) 
{ 
	showWait(); 
	window.location.href = "r.sh?content=" + location;
}

function edit( location ) 
{
	window.location.href = "r.sh?content=" + location + '&edit=1'; 
}