var NS4 = (document.layers);    // Which browser?
var IE4 = (document.all);
var win = window;               // window to search.
var n   = 0;

var i=0;

//
//  These variables are shared between the javaScript code and
//  the VB code for graphics.
//
var strGR_ErrorMsg = "",
    strGR_Type     = "",
    numGR_w        = 0,
    numGR_h        = 0,
    numGR_c        = 0;

//
// Asc() returns the numeric value for the single character passed as a string
//
//       This is a javascript version of the VB script asc() function
//
function Asc ( String )
{
	return String.charCodeAt ( 0 );
}

//
// Chr() returns the string equivalent of the numeric value passed in
//
//       This is a javascript version of the VB script chr() function
//
function Chr ( AsciiNum )
{
	return String.fromCharCode ( AsciiNum )
}

function JS_GetBytes ( flnm, offset, bytes )
{
   var objFSO;             // File System Object
   var objFTemp;           // File Object
   var objTextStream;      // Text Stream Object
   var lngSize;
   var strBuffer;          // Return buffer

   objFSO = new ActiveXObject ( "Scripting.FileSystemObject" );

   if ( !objFSO.FileExists ( flnm ) )
      {
      alert ( "[JS_GetBytes] File " + flnm + " doesn't exist" );

      return "";
      }

   //
   // If bytes = -1 then the entire file is read...
   //
   if ( bytes == -1 )
      {
      // ... so we get the filesize
      objFTemp     = objFSO.GetFile ( flnm );
      lngSize      = objFTemp.Size;
      }

   //
   // Open modes...
   //
   fsoForReading      = 1;
   fsoForWriting      = 2;
   fsoForAppending    = 8;

   //
   // Open formats...
   //
   TristateUseDefault = -2;      // Opens the file using the system default
   TristateTrue       = -1;      // Opens the file as Unicode
   TristateFalse      = 0;       // Opens the file as ASCII

   objTextStream = objFSO.OpenTextFile ( flnm, fsoForReading );

   //
   // Skip over what we don't need...
   //
   if ( offset > 0 )
      strBuff = objTextStream.Read ( offset - 1 )

   if ( bytes == -1 )
      strBuffer = objTextStream.Read ( lngSize );
   else
      strBuffer = objTextStream.Read ( bytes   );

   objTextStream.Close();

   return strBuffer;
}

function SF_gfxSpex ( flnm )
{
   var strPNG;
   var strGIF;
   var strBMP;
   var strType;
   var strBuff;

   strType      = "";
   strImageType = "(unknown)";

   strPNG       = Chr ( 137) + Chr ( 80) + Chr ( 78);
   strGIF       = "GIF";
   strBMP       = Chr ( 66)  + Chr ( 77);

   strType      = JS_GetBytes ( flnm, 0, 3 );

   if ( strType == strGIF )
      {
      strGR_Type  = "GIF";
      numGR_w     = lngConvert ( JS_GetBytes ( flnm,  7, 2 ) );
      numGR_h     = lngConvert ( JS_GetBytes ( flnm,  9, 2 ) );

      alert ( "[GIF] we got " + numGR_w  + " " + numGR_h);
      return 1;
      }

   if (  strType.substring ( 0, 2) == strBMP )
      {
      strGR_Type  = "BMP";
      numGR_w     = lngConvert ( JS_GetBytes ( flnm, 19, 2 ) );
      numGR_h     = lngConvert ( JS_GetBytes ( flnm, 23, 2 ) );

      alert ( "[BMP] we got " + numGR_w  + " " + numGR_h);
      return 1;
      }

   if ( strType == strPNG )
      {
      strGR_Type  = "PNG";
      numGR_w     = lngConvert ( JS_GetBytes ( flnm, 19, 2 ) );
      numGR_h     = lngConvert ( JS_GetBytes ( flnm, 23, 2 ) );

      return 1;
      }

   //
   //  If we get to this point, it's probably a jpg file
   //
   flgFound = VB_Determine_JPG_Properties ( flnm );

   alert ( "[JPG] we got " + flgFound + " " + numGR_w  + " " + numGR_h);
   return flgFound;
}

var flagFirstKey = true;

function resetFirstKey()
{
   flagFirstKey = true;
   return;
}

function resetValue( objValue )
{
   var numValue = objValue.value;

   if ( flagFirstKey == true )
      {
      flagFirstKey   = false;
      objValue.value = "";
      }

   return false;
}

//
//  FileSystemObject functions
//

// var fso     = new ActiveXObject( "Scripting.FileSystemObject" );

// var newpath     = fso.BuildPath    ( "c:\\tmp", "kuku.txt" );
// var fileBool    = fso.FileExists   ("d:\\old.txt");
// var folderBool  = fso.FolderExists ("d:\\old");
// var tmpName     = fso.GetTempName  ();

// alert(newpath);

var strBase          = "";
var strCurrentFolder = "";

// var dirGalleryBase   = strbase        + "\\Activities";
// var dirBayQueen      = dirGalleryBase + "\\BayQueen";

function DOESfolderExists ( strFolderName )
{
   var fso          = new ActiveXObject( "Scripting.FileSystemObject" );
   strCurrentFolder =     strbase + "\\" + strFolderName;

   return ( fso.FolderExists   ( strCurrentFolder ) );
}

function DOESfileExists ( strFileName )
{
   if ( strCurrentFolder == "" )
      return false;

   var fso            = new ActiveXObject( "Scripting.FileSystemObject" );
   var strCurrentFile =     strCurrentFolder + "\\" + strFileName;

   return ( fso.FileExists   ( strCurrentFile ) );
}

function verifyFile ( strFileName )
{
   return ( true );

   var numResults,
       fso = new ActiveXObject ( 'Scripting.FileSystemObject' );

   if ( fso.FileExists( strFileName ) )
      numResults = true;
   else
      numResults = false;

   return numResults;
}

function FullYear_Spawn ( sPageName, sWindowName, nIndex )
{
   var strURL;
   var strYear = "<%=Year(date)%>";

   // alert ( "FullYear_Spawn " + nIndex );
   strURL = sPageName + "?year=" + strYear + "&show_all=yes" + "&index=" + nIndex;

   winFullYearWindow = window.open ( strURL, sWindowName, 'height=600,width=700,left=20,top=5,toolbar=0,location=0,directories=0,status=1,scrollbars=1,resize=0,resizeable=0');
}

var winObj;

var LEFTWARDS_ARROW         = 0;
var UPWARDS_ARROW           = 1;
var RIGHTWARDS_ARROW        = 2;
var DOWNWARDS_ARROW         = 3;
var LEFT_RIGHT_ARROW        = 4;
var LEFTWARDS_DOUBLE_ARROW  = 5;
var UPWARDS_DOUBLE_ARROW    = 6;
var RIGHTWARDS_DOUBLE_ARROW = 7;
var DOWNWARDS_DOUBLE_ARROW  = 8;
var LEFT_RIGHT_DOUBLE_ARROW = 9;

var Arrows = new Array ( 0x2190, 0x2191, 0x2192, 0x2193, 0x2194, 0x21d0, 0x21d1, 0x21d2, 0x21d3, 0x21d4 );

function Chr_Arrow ( numArrowCode )
{
	return String.fromCharCode( Arrows[ numArrowCode ] );
}

function getDivObject ( divName )
{
   return ( document.getElementById ( divName ) );
}

//
// This function will remove and reinsert a file input type such that the contents of the text box will be
// empty.  This is needed to be implemented because for ( some reason, document.getdata.file1.value = "";
// will not work!
//

function BlankOut_FileElement ( strFileObj )
{
   var o = document.getElementById( strFileObj );
   var p = o.parentNode;

   p.removeChild ( o );

   var n = document.createElement ( 'input' );

   with (n)
      {
      type     = 'file';
      id       = strFileObj;
      name     = o.name;
      className= o.className;
      size     = o.size;
      onchange = o.onchange;
      }

   p.appendChild ( n );
}

function testDate ( strDate )
{
   if ( strDate.length == 0 )
      return 1;

   cDateSplit = "/";
   if ( strDate.indexOf( cDateSplit ) == -1 )
      {
      cDateSplit = "-";

      if ( strDate.indexOf( cDateSplit ) == -1 )
         {
         cDateSplit = ".";
         }
      }

   dDate    = strDate.split( cDateSplit );

   intMonth = dDate [0];
   intDay   = dDate [1];
   intYear  = dDate [2];

   return ( Test_the_Date ( intMonth, intDay, intYear ) );
}

function formatDate ( dateObject )
{
   replaceSingleQuotes( dateObject );

   var strTemp   = dateObject.value;
   var numLength = strTemp.length;
   var strFinal  = "";
   var flagOK_2_format;

   if ( strTemp == "" )
      return;

   if ( strTemp.charAt(1) == "/" || strTemp.charAt(2) == "/" || strTemp == "" )
      flagOK_2_format = true;
   else
      flagOK_2_format = false;

   if ( flagOK_2_format == false && numLength != 6 && numLength != 8 )
      {
      alert ( "Date Format Error --- Date MUST be in the form mmddyy or mmddyyyy" );

      dateObject.value = "";
      dateObject.select();
      dateObject.focus ();
      }
   else
      {
      //
      //  Check to see if the '/' characters need to be entered
      //
      if ( strTemp.charAt(1) == "/" || strTemp.charAt(2) == "/" || strTemp == "" )
         strFinal = strTemp;
      else
         {
         if ( numLength == 6 )
            strFinal = strTemp.substring(0,2) + "/" + strTemp.substring(2,4) + "/" + strTemp.substring(4,6)
         else
            strFinal = strTemp.substring(0,2) + "/" + strTemp.substring(2,4) + "/" + strTemp.substring(4,8)
         }

      var nResults = testDate ( strFinal );
      if ( nResults == 1 )
         {
         alert ( "Date Format Error: " + strFinal );
         dateObject.value = "";
         dateObject.select ();
         dateObject.focus  ();
         }
      else
         dateObject.value = strFinal;
      }

   return;
}

function showCopyRight ()
{
   document.write ( "<center>" );
   document.write ( "<FONT size=2 color=black>Last updated Oct. 15, 2005" );
   document.write ( "<BR><BR>" );

   document.write ( "This web site is hosted by <a href='http://www.knightsonline.net'><img src='images/Knights_Online.gif' align='absmiddle'></a><br>" );
   document.write ( "<a href='mailto:PaulGScannell@verizon.net' class='blk_links'>Jerry Scannell</a> webmaster" );
   document.write ( "</CENTER>" );

   return true;
}

function floatButton ()
{
   if (document.all)
      {
      document.all.topButton.style.pixelTop = document.body.scrollTop + 20;
      }
   else if (document.layers)
           {
           document.topButton.top = window.pageYOffset;
           }
        else if (document.getElementById)
   	          {
                document.getElementById('topButton').style.top = window.pageYOffset + 'px';
                }
}

function initButton ()
{
   if (document.all)
      {
      document.all.topButton.style.pixelLeft  = 0;
      document.all.topButton.style.visibility = 'visible';
      }
   else if (document.layers)
           {
           document.topButton.left            = 0;
           document.topButton.visibility      = 'show';
           }
        else if (document.getElementById)
               {
               document.getElementById('topButton').style.left       = 0;
               document.getElementById('topButton').style.visibility = 'visible';
               }
}

function findInPage ( str )
{
   var txt, i, found;

   if (str == "")
      return false;             // Dont bother looking for nothing!


   //
   // Netscape
   //
   if (NS4)
      {
      // Look for match starting at the current point. If not found, rewind back to the first match.
      if (!win.find(str))
         while(win.find(str, false, true))
            n++;
      else
            n++;

     if (n == 0)                // If not found in either direction, give message.
        alert("Not found.");
     }


   //
   // Internet Explorer
   //
   if (IE4)
      {
      txt = win.document.body.createTextRange();
      // Find the nth match from the top of the page.
      for (i = 0; i <= n && (found = txt.findText(str)) != false; i++)
         {
         txt.moveStart( "character", 1 );
         txt.moveEnd  ( "textedit" );
         }

      // If found, mark it and scroll it into view.
      if (found)
         {
         txt.moveStart     ( "character", -1 );
         txt.findText      ( str );
         txt.select        ();
         txt.scrollIntoView();
         n++;
         }
      else                      // Otherwise, start over at the top of the page and find first match.
         {
         if (n > 0)
            {
            n = 0;
            findInPage(str);
            }
         else                   // Not found anywhere, give message.
            alert("Not found.");
         }
      }
   return false;
}

function Zero_Fill ( strString, numLength )
{
   var strResult = strString;

   while ( strResult.length < numLength )
      strResult = "0" + strResult;

   return strResult;
}

function UpperCase ( String )
{
   var localString = String.toUpperCase();
   return ( localString );
}

function LowerCase ( String )
{
   var localString = String.toLowerCase();
   return ( localString );
}

function jsFloatConvert ( String )
{
   var numRetVal = String * 1.0;

   if ( isNaN ( numRetVal ) )
      numRetVal = 0.0;

   return ( numRetVal );
}

function jsIntegerConvert ( String )
{
   var numRetVal = String * 1;

   if ( isNaN ( numRetVal ) )
      numRetVal = 0;

   return ( numRetVal );
}

function jsStringConvert ( String )
{
   var localString = String;

   if ( String == "null" || String == null || String == "" )
      localString = "";
   else
      localString = String;

   return ( localString );
}

function jsBooleanConvert ( String )
{
   var numRetVal;

   if( String == "true" || String == true )
      numRetVal = true;
   else
      numRetVal = false;

   return ( numRetVal );
}

function jsDateConvert ( dateString )
{
   var strMonth,
       strDay,
       strYear,
       strFinalDate,
       numLength = dateString.length,
       localDate = new Date ( dateString );

   strMonth              = localDate.getMonth () + 1;
   strDay                = localDate.getDate  ();
   // strYear               = localDate.getYear  ();

   if ( isNaN(strMonth) )
      strFinalDate       = "";
   else
      {
      strYear      = dateString.substring ( numLength-4, numLength )
      strFinalDate = strMonth + "/" + strDay + "/" + strYear;
      }

   return ( strFinalDate );
}

function jsTimeConvert ( String )
{
   var strHour, strMinute, strSecond, strFinalTime;

   localTime       = new Date             ( String );

   strHour         = localTime.getHours   () + "";
   strMinute       = localTime.getMinutes () + "";
   strSecond       = localTime.getSeconds () + "";

   if ( isNaN(strHour) )
      strFinalTime = "";
   else
      strFinalTime = Zero_Fill ( strHour, 2 ) + ":" + Zero_Fill ( strMinute, 2 ) + ":" + Zero_Fill ( strSecond, 2 );

   return ( strFinalTime );
}

/*
 * This routine replaces any occurances of a single quote with
 * a null.
 *   NOTE:  In the calling routine, the form merely places the field's name in the call
 *          Take a look at smld\main.asp for an example.
 */

function replaceSingleQuotes ( strField )
{
   var temp = "" + strField.value;
   var add  = "";

   var pos = 0;

   while ( temp.indexOf("'") > -1 )
      {
      pos  = temp.indexOf( "'" );
      temp = "" + (temp.substring(0, pos) + add + temp.substring((pos + 1), temp.length));
      }

   while ( temp.indexOf('"') > -1 )
      {
      pos  = temp.indexOf( '"' );
      temp = "" + (temp.substring(0, pos) + add + temp.substring((pos + 1), temp.length));
      }

   strField.value = temp;
}

function Test_the_String ( String )
{
   var text   = "[" + String + "]"
   if ( text == "[]" )
      return ( 1 );
   else
      return ( 0 );
}

function Test_the_Date ( intMonth, intDay, intYear )
{
   var Bad_Date = 0;

   if ( intMonth != "" && intDay != "" && intYear != "" )
      {
      if ( intMonth < 1    || intMonth > 12 )
         Bad_Date = 1

      if ( intDay   < 1    || intDay   > 31 )
         Bad_Date = 1

      if ( intYear  < 1900 || intYear  > 2199 )
         Bad_Date = 1
      }

   return ( Bad_Date );
}

function Test_the_Integer ( intData )
{
   var intTemp = intData;

   if ( intTemp == "" )
      return ( false );

   if ( parseInt   ( intData, 10 ) != intTemp )
      return ( true  );
   else
      return ( false );

}

function Test_the_Float ( fltData )
{
   var fltTemp = fltData;

   if ( fltTemp == "" )
      return ( false );

   if ( parseFloat ( fltData ) != fltTemp )
      return ( true  );
   else
      return ( false );

}

function Test_Msg ( msg, form )
{
   if ( msg > "" )
      {
      alert      ( msg );
      return     ( false );
      }

   form.submit();
   return     ( true );
}
