// copyright c 2009 by t. j. marlin
// All rights reserved. No part of this script may be reproduced or used in any
// form or by electronic or mechanical means, including information storage and retrieval 
// systems, without the permission in writing from the author, except in normal use of
// browsing the web page of which this is a part. 

// version 1.2, 30 oct 09
// updated to change the way the menu system is built.
// instead of images to get the red and blue test now use javascript to change the text color
// the maps are gone!

function say( m ){
  m = "" + m + ""; // ensure that m is a string
  if( m != "undefined" ){
    document.write( m );
  } else {
    document.write( "<br>" );
  }
} // function say

// ____________________________________________________________________________
function sayln( m ){  // writes m with a writeln
  m = "" + m + ""; // ensure that m is a string
  if( m != "undefined" ){
    document.writeln( m );
  } else {
    document.writeln( "<br>" );
  }
} // function sayln

// ____________________________________________________________________________
function formatDate( inTime ){
var monthNames = ["Jan.", "Feb.", "March", "April", "May", "June", "July", "Aug.", "Sep.", "Oct.", "Nov.", "Dec."];
var date;
date = inTime.getDate();
date += " " + monthNames[ inTime.getMonth() ]; 
date += " " + inTime.getFullYear();
date += "  " + inTime.getHours();
date += ":" + inTime.getMinutes();
return date;
} //  formatDate ()

// ____________________________________________________________________________
String.prototype.trimL =function(){ // trims leading white space from s and returns the trimmed string
  return this.replace( /^\s*/, "" );
} // function trimL ()


// ____________________________________________________________________________
String.prototype.trimR = function(){ // trims trailing white space from s and returns the trimmed string
  // return this.replace( /\s*$/, "" );  // doesn't work in netscape
  var i = this.length -1;
  while( ( i >= 0 ) && ( this.charAt(i) == " " )){
    i--;
  }
  return this.substr( 0, i+1 );
} // function trimR ()

// ____________________________________________________________________________
String.prototype.trim = function(){ // trims leading and trailing white space from s and returns the trimmed string
//	treating a string as an array and getting chars out of a string with this[i]
// works in firefox. but it is not ecma standard. one must use charAt(). see javascript pgs 28 and 696.
  var i =  this.length -1;
  while( ( i >= 0 ) && ( this.charAt(i) == " " )){
    i--;
  }
  return  this.substr( 0, i+1).replace( /^\s*/, "" ); ;
} // function trim()

// ____________________________________________________________________________
changeColor = "'red'";
changeIndicator = "|";
String.prototype.ci =function(){  // if the first char is the change indicator, returns the string surrounded by font color cmds.
  if( this.substr(0,1) == changeIndicator ){
    return "<font color=" + changeColor + ">" + this.substr( 1, this.length ) + "</font>"
  } else {
    return this
  }
}  //  String.prototype.ci 

// ____________________________________________________________________________
function setMsg( msg ){ // set the message in the status bar
  window.status = msg;
  return true;
}  //  function setMsg( 

// ____________________________________________________________________________
function openLink( target, winName ){  // opens target in a new window
  window.open( target, winName ).focus(); // ensure window is on top
  return false;
}  //  function openLink(

// ____________________________________________________________________________
function show( obj ){ // sets object to visible
  obj.visibility= "visible";
}  //  function show( obj )

// ____________________________________________________________________________
function hide( obj ){ // sets object to invisible
  obj.visibility= "hidden";
}  //  function hide( obj )

// ____________________________________________________________________________
function getObject( obj ){ // convert object name string or obj referenct into a valid obj ref
  var theObj;
  if( typeof obj == "string" ){
    theObj = eval( "document." + coll + obj + styleObj );
  } else {
    theObj = obj;
  }
  return theObj;
} //  function getObject( obj )

// ____________________________________________________________________________
var isNav4Up = false;
var isIe4Up = false;
var coll = "";
var styleObj = "";


if( parseInt( navigator.appVersion ) >= 4 ){
  if( navigator.appName == "Netscape" ){
    isNav4Up = true;
  } else if( navigator.appName.indexOf( "Internet Explorer" ) != -1 ){
    isIe4Up = true;
    coll = "all.";
    styleObj = ".style";
  } 
} //  function getObject( obj )

// ____________________________________________________________________________
function mailto( lhs, rhs, name ){  // from webtechniques, aug 2001
// we use this to foil mail list scavengers.
  say( '<a href="mailto:' + lhs + '@' + rhs + '">' + name + '</a>' );
} //  function mailto( lhs, rhs, name ){

// _____________________________________________________________________
/* test existance of a element in a form

if an element, i. e. a field, does not exist in a form, references that element
causes a run time error. (Note: undefined tests if an extant element has a
value, not whether or not it is declared.)

given a form object and a potential element name, this function determines if
the named element is in fact declared in the form.
returns true if the element exists, false otherwise

inputs: 
  theForm: an object. the form to investigate.
  theEle: a string. the element name to look for.
returns:
  true if the element exists on the form.
  false if not.

examples: 
    exist( document.gurn, "rcName" )
  determines if the element named rcName is an element of form gurn.
  
    function foz(){
      var formObj = document.gurn;
      with( formObj ){
        // references to known objects on the form
	if( exist( formObj, "rcName" ) ){
          // do things with rcName
	}
      }
    }

*/

function exist( formObj, varName ){
  var i;
  for( i = 0; i < formObj.length; i++){
    if( varName == formObj.elements[i].name ){
      return true;
    }  
  } 
  return false;
} // exist

// _____________________________________________________________________
/* Menu system

Builds and handles menus. 
   The default menu color is blue. When the mouse is placed over the menu, the text
   turns red. 
   This is known to work in ns 3.5.3 and ie 6.0.2900.2180 running on  a pc with xp sp 2
   
   This system places constraints on the web site structure:
   1. Each page must be named <unique word>.shtml
   2. Each page must be in a subdirectory named <unique word>.
   
   For example, the calendar page is named
   calendar.shtml and lives in the subdirectory calendar.
   
   To add a new page, say gurn, with a corresponding menu:
   1. create the page named gurn.shtml
   2. create the subdirectory gurn. put gurn.shtml in this subdirectory 
   3. put the word "Gurn" in the menus array.
   4 the secon delement of the menus array is dependent on what you want to do: if clicking on the menu item is to open
   4a. the page gurn.shtml, put none in the second element.
   4b. a different page in the page set, put the lower case unique word of the page in
       the second element.
   4c. a completely different page, put the full url in the second element. it must have at least one /
*/ 


// The menus. the second element is what to open:
var menus = [
	[ "AllStar", 'none' ],
	[ "Calendar", 'none' ],
	[ "Coach", 'none' ],
	[ "Courses", 'none' ],
	[ "Extra", 'none' ],
	[ "Home", 'none' ],
	[ "Index", 'none' ],
	[ "Library", 'none' ],
	[ "Links", 'none' ],
	[ "Maps", 'http://www.ayso11k.org/cgi-bin/wsa.pl?funct=maps_req' ],
	[ "Mud Line", 'http://www.ayso11k.org/cgi-bin/wsa.pl?funct=maps_req' ],
	[ "New", 'none' ],
	[ "Plus", 'none' ],
	[ "Regions", '../links/links.shtml' ],
	[ "Registration", 'none' ],
	[ "RefClasses", 'none' ],
	[ "Referee", 'none' ],
	[ "Search", 'none' ],
	[ "Section", 'none' ],
	[ "Spring", 'none' ],
	[ "Staff", 'none' ],
	[ "SuperCamp", 'none' ],
	[ "Tournaments", 'none' ],
	[ "U10-U14", 'none' ],
	[ "U16-U19", 'none' ],
	[ "VIP", 'none' ]
];

function menu_mouse_over( t ){
	//this alert get the text of the line
	//alert( "in mover with " + t.innerHTML );
	t.style.color="red";
}

function menu_mouse_out( t ){
	//alert( "in mout" );
	t.style.color = "blue";
}


// buildMenus - called as the first text in the body of a document
function buildMenus() {
	var slash_re = /\//;
	var lcMenu;
	for( var i = 0; i < menus.length; i++ ){ 
		say( '<tr><td align="center"><a class="menua" href="' );
		lcMenu = menus[ i ][0].toLowerCase();
		if( menus[ i ][1] == "none" ){
			say( "/" + lcMenu + '/' + lcMenu + '.shtml"' );
		}else{
			if( slash_re.test( menus[ i ][1] ) ){
				say( menus[ i ][1] );
			}else{
				say( "/" + menus[ i ][1] + '/' + menus[ i ][1] + '.shtml"' );
			}
		}
		say( '" onmouseover = "menu_mouse_over(this)" onmouseout = "menu_mouse_out(this)">');
		say( menus[i][0] + '</a></td></tr>' );
	} // for( var i
}  // function buildMenus() {

// ___________________________________________________________________________  
  // captilizes first letter of a string. handles Mc. 
  // searches across multiple lines.
  
  // look for zero or more non word chars followed by 1 or more word chars
  var prettyRe = /(\W*)(\w+)/;

  function prettyName( s ){
    var result = "";
    var word;
    //alert( "pretty name with |" + escape( s ) + "|" );
    RegExp.input = s.toLowerCase();
    while( prettyRe.test() ){
      //alert( "found 1 |" + escape( RegExp.$1 ) + "|  2 |" 
      //     + escape( RegExp.$2 ) + "|" );
      // copy the leading non word chars to word followed by the
      // upper case of the first char of the word chars followed by 
      // the remaining word chars.
      word = RegExp.$1 + RegExp.$2.charAt(0).toUpperCase(0) 
                + RegExp.$2.substr( 1 );

      // save the rest of the string for the next loop
      RegExp.input = RegExp.rightContext;

      // look for Mc and capitalize the next character.
      word = word.replace( /(Mc)(\w)(\w*)/, 
            function( s, p1, p2, p3){
              return p1 + p2.toUpperCase() + p3;}
         );

      // add this work to the result and go again.
      result += word;
      //alert( "result |" + escape( result ) + "|" );
      //alert( "next |" + escape( RegExp.input ) + "|" );
    }
    // add any trailing \W chars to the result.
    return (result + RegExp.input);
  }  //  function prettyName( s )
  
// ___________________________________________________________________________  
  // requires that string be non 0 in length and contains only digits 0-9
  // returns true if ok
  // false othewise
  function isAPosNum( n ){
    if( n.length == 0 ){
      return false;
    }
    return !(/[^0-9]/.test( n ));
   }  //  function isAPosNum

