/*
 +==========================================================================+
 |  Copyright (c) 2004 Pacific Disaster Center, Maui, Hawaii, USA           |
 |                          All rights reserved.                            |
 +==========================================================================+
 |  FILENAME: pdcscripts.js                                                 |
 +==========================================================================+
 |  DESCRIPTION:                                                            |
 |  Core Javascript for all pdc jsps                                        |
 |  This file MUST be compressed by ANT before deployment.   Remove all     |
 |  whitespaces, comments etc.  DO NOT remove comments from source, please. |
 |  Leave the RCS ID comment intact when doing so.                          |
 +==========================================================================+
 |  HISTORY                                                                 |
 |  [30-DEC-2003, Steve Kunitzer] Initial version                           |
 |  [16-JUN-2004, Uday Kari] Add isPDCNumber method                         |
 |  [24-JUN-2004, Uday Kari] comment out myOKBads offending Mozilla         |
 |  [2005/07/12 01:59:26 $] $Author: dparker $, initial CVS checkin         |
 +==========================================================================+
*/
// $Id: pdcscripts.js,v 1.3 2005/09/30 19:13:12 dparker Exp $

function isEmpty(str) {

  // return true if string null or empty

  if (str == null) return true;

  else if (str.length == 0) return true;

 

  // then trim and see

  var modStr = trimSpcs(str);

 

  if (modStr == "") return true;

  else if (modStr.length == 0) return true;

  else return false;

}
function trimSpcs(str)
{

  // trims leading and trailing spaces

 

  var len = 0;

  if (str != null) len = str.length;

 

  var left = 0;

  var right = len-1;

  // if empty, string return empty string

  if (right < 0) return "";

 

  // trim trailing spaces

  while (hasSpcAt(str, right) ) right--;

  

  // trim leading spaces

  while (hasSpcAt(str, left) )  left++;

 

  if (right < left) return "";

  return str.substring(left, right+1);

}
function hasSpcAt(str, pos)
{
  // helper function for trimSpcs
  // determine if generic whitespace at given position

  if (str == null ) return false;

  else if ( pos < 0 || pos > (str.length-1) ) return false;

  else return hasSpc(str.slice(pos,pos+1) );

}
function hasSpc(str)
{

  // helper function for trimSpcs
  // search for generic whitespace

  if (str == null) return false;

  else if (str.search(/\s/i)  > -1) return true;

  else return false;

}
function checkFileName(filename)
{

  // helper function for file upload...simply checks a string to see if 
  // it is a proper file name.  This is a generic function that will 
  // check if there are weird characters in FileUpload.  It offers one
  // layer of client-side protection to prevent malicious stuff between 
  // script tags for instances being put into the upload field...

  // Usage:
  // returns true if filename is OK
  // false if there are any unexpected patterns observed
  // in your jsp, create one checkMyFileName function for 
  // each fileupload element and call checkFileName function 
  // to do all the work...

  // alert('filename = ' + filename);

  // Preliminary check: filename cannot be empty or null
  if (isEmpty(filename))
  {
     alert('filename is empty or null');
     return false;
  }

  // Function designed to return TRUE if and only if Filename is "clean" as per NT/UNIX

  // regular expression includes 
  // all letters, digits, slash, backslash, colon and the period...thats it
  // filter out any non-word character one or more times globally
  // regular expression for non-word characters
  myRegExp1 = /\W+/g;

  // the only non-word characters allowed 
  // myOKbads means "NOT colon, backslash, forward slash (UNIX pathseparator), 
  // any number of spaces and of course the period(.)
  // myOKbads   = /[^\s+:\\/.]/g;

  // actually better just look at just the bad guys only
  // if all else fails just <> so people do not put scripts in
  // added % as bad guy for jsp form submission
  theBadGuys = /[*?"<>|%]/g;

  // the following just returns the first occurrence of non-word character
  // hence not useful...may be in future versions of Javascript...
  // myArray = myRegExp1.exec(filename);

  // this one works fine...returns all non-words as elements of an array
  myArray = filename.match(myRegExp1);

  // the flag that will be returned after all the filename checks below 
  var flag = true;

  if (myArray == null)
  {
    // alert('Only Word chars found...FileName is Pure...myArray NULL...returning TRUE');
    // do nothing flag is already true
  }
  else
  {
    // found non-word check out the filename further
    // alert('FOUND non-word chars found...further checking...myArray SIZE = ' + myArray.length);
    
    for (var i=0; i < myArray.length; i++)
    {
      var str = myArray[i];
      // alert('myArray[' + i + '] = ' + str);
      if (theBadGuys.test(str))
      {
        flag = false;
        alert('Found Invalid Character in FileName = ' + str);
        break;
      }
    } 
  }

  // do more checks on the filename

  // Check 1: if file seprators (FORWARD SLASH and BACK SLASH) co-exist
  var fpos = filename.indexOf("/");
  var bpos = filename.indexOf("\\");
  if (fpos > -1)
  {
     if (bpos > -1)
     {
        alert('Using both forward slash and backward slash in filename, are we?');
        flag = false;
     }
  }
  else if (bpos > -1)
  {
     if (fpos > -1)
     {
        alert('Using both forward slash and backward slash in filename, are we?');
        flag = false;
     }
  }
  else
  {
     // means no forward slash or backward slash...
     // the only valid non-word character allowed would be period and colon
     // we have already checked...so nothing to do
     // alert('slash mix-up ok');
  }


  // Check 2: cant have leading spaces in fileupload
  var lpos = filename.length - 1;
  var posOne = filename;
  if (lpos > 0) 
  {
    posOne = filename.slice(0,1);
  }
  if (posOne.search(/\s/gi)  > -1)  
  {
    if (flag)
    {
      alert('Filename cannot have leading whitespace\nFile Upload Read Only');
      flag = false;
    }
  }

  // Check 3: last guy cant be / or \ or ...
  var lastChar = filename.slice(lpos);
  if (lastChar.equals("\\") || lastChar.equals("/"))
  {
    if (flag)
    {
      alert('Invalid end character for FileName' + lastChar);
      flag = false;
    }
  }

  // Check 4:  where is the colon?
  // if the colon is anywhere but position = 1 (indices start at zero)

  var colonPos = filename.lastIndexOf(":"); 
  if (colonPos == -1)
  {
    // no colon is OK
    // alert('NO Colon...OK');
  }
  else if (colonPos == 1)
  {
    // lastindex == 1, good
    // alert('Colon...at pos 1...OK');
  }
  else
  {
    // NOT OK!!
    if (flag)
    {
      alert('Improper place for the colon in filename');
      flag = false;  
    }
  }

  if (flag)
  {
    // alert('returning TRUE');
  }
  else
  {
    // alert('returning FALSE');
  }

  return flag;
}
function esq(str)
{
  // replaces single quote with escaped single quote
  qexp = /[']/g;
  return str.replace(qexp,"\'");
}
function isNumbah(str)
{
  // determines if str is a number
  // i.e. "starting at beginning of the string (^), 
  // at least one digit (\d+) followed by decimal (\.)
  // followed by zero or more digits (\d*)"
  var rexp1 = /^\d+\.\d*$/;

  // or it could just be a bunch of digits
  var rexp2 = /^\d+$/;
  
  // trim str
  var nvalue = trimSpcs(str);

  if (rexp1.test(nvalue))
  {
    return true;
  }
  else if (rexp2.test(nvalue))
  {
    return true;
  }
  else
  {
    return false;
  }
}
