// email

function checkEmail (strng) {
  var valid = true;
  if (strng == "") {
     valid = false;
  }
  else if (!isRFC822ValidEmail(strng)) {
     valid = false;
  }
  return valid;
}

function checkURL(argvalue) {

  if (argvalue.length == 0)
    return false;
  if (argvalue.indexOf(" ") != -1)
    return false;
/*
  else if (argvalue.indexOf("http://") == -1)
    return false;
*/
  else if (argvalue == "http://")
    return false;
  else if (argvalue.indexOf("http://") > 0)
    return false;

  argvalue = argvalue.substring(7, argvalue.length);
  if (argvalue.indexOf(".") == -1)
    return false;
  else if (argvalue.indexOf(".") == 0)
    return false;
  else if (argvalue.charAt(argvalue.length - 1) == ".")
    return false;

  if (argvalue.indexOf("/") != -1) {
    argvalue = argvalue.substring(0, argvalue.indexOf("/"));
    if (argvalue.charAt(argvalue.length - 1) == ".")
      return false;
  }

  if (argvalue.indexOf(":") != -1) {
    if (argvalue.indexOf(":") == (argvalue.length - 1))
      return false;
    else if (argvalue.charAt(argvalue.indexOf(":") + 1) == ".")
      return false;
    argvalue = argvalue.substring(0, argvalue.indexOf(":"));
    if (argvalue.charAt(argvalue.length - 1) == ".")
      return false;
  }

  return true;

}

// phone number - strip out delimiters

function checkNumber (strng) {
  var valid = true;
  if (strng == "") {
     valid = false;
  }
  else {
    var stripped = strng.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters
    if (isNaN(parseInt(stripped))) {
       valid = false;

    }
  }
  return valid;
}

function IsNumeric(strString)
{
  //  check for valid numeric strings
  var strValidChars = "0123456789.-+ ";
  var strChar;
  var blnResult = true;

  if (strString.length == 0) return true;

  //  test strString consists of valid characters listed above
  for (i = 0; i < strString.length && blnResult == true; i++)
  {
    strChar = strString.charAt(i);
    if (strValidChars.indexOf(strChar) == -1)
    {
       blnResult = false;
    }
  }
  return blnResult;
}


// password - between 6-8 chars, uppercase, lowercase, and numeral

function checkPassword (strng1, strng2, min, max) {
  var valid = true;
  if (strng1 == "" || strng2 == "") {
     valid = false;
  }

  var illegalChars = /[\W_]/; // allow only letters and numbers
  
  if (strng1 != strng2) {
    valid = false;
  }    
  else if ((strng1.length < min) || (strng1.length > max)) {
    valid = false;
  }
  else if (illegalChars.test(strng1)) {
    valid = false;
  }
  
  return valid;
}    


// username - 4-10 chars, uc, lc, and underscore only.

function checkUsername (strng) {
  var valid = true;
  if (strng == "") {
    valid = false;
  }

  var illegalChars = /\W/; // allow letters, numbers, and underscores
  if ((strng.length < 4) || (strng.length > 50)) {
     valid = false;
  }
  else if (illegalChars.test(strng)) {
    valid = false;
  } 
  return valid;
}       

// numeric Value

function checkValue (val, min, max) {
var error = "";

    if ((val < min) || (val > max)) {
       error = "The "+name+" must be between "+min+" and "+max+".\r\n";
    }
return error;
}

// length.

function checkLength (strng, min, max) {
  var valid = true;

  if ((strng.length < min) || (strng.length > max)) {
    valid = false;
  }
  return valid;
}

// non-empty textbox

function isEmpty(strng) {
  var valid = false;
  if (strng == undefined || strng.length == 0) {
     valid = true;
  }
  return valid;
}

// was textbox altered

function isDifferent(strng) {
var error = ""; 
  if (strng != "Can\'t touch this!") {
     error = "You altered the inviolate text area.\r\n";
  }
return error;
}

// exactly one radio button is chosen

function checkRadio(checkvalue) {
var error = "";
   if (!(checkvalue)) {
       error = "Please check a radio button.\r\n";
    }
return error;
}

// valid selector from dropdown list

function checkDropdown(choice, name) {
var error = "";
    if (choice == 0) {
    error = "You didn't choose an option for the "+name+" from the drop-down list.\r\n";
    }    
return error;
}

function checkSpaces(strng, name) {
var error = "";
    if (strng.indexOf(' ') > -1) {
      error = name+" cannot contain spaces.\r\n";
    }
return error;
}

function checkFilename(strng, name) {
var error = "";
var objRegExp  =  /(^[a-z_0-9A-Z]+$)/;
    if (!objRegExp.test(strng)) {
      error = name+" contains invalid characters, a-z A-Z 0-9 and _ allowed.\r\n";
    }
return error;
}

function extract(what) {
    if (what.indexOf('/') > -1)
        answer = what.substring(what.lastIndexOf('/')+1,what.length);
    else
        answer = what.substring(what.lastIndexOf('\\')+1,what.length);
    return answer;
}

/**
 * JavaScript function to check an email address conforms to RFC822 (http://www.ietf.org/rfc/rfc0822.txt)
 *
 * Version: 0.2
 * Author: Ross Kendall
 * Created: 2006-12-16
 * Updated: 2007-03-22
 *
 * Based on the PHP code by Cal Henderson
 * http://iamcal.com/publish/articles/php/parsing_email/
 */

/*

Portions copyright (C) 2006  Ross Kendall - http://rosskendall.com
Portions copyright (C) 1993-2005 Cal Henderson - http://iamcal.com

Licenced under Creative Commons _or_ GPL according to the terms below...

--

 Licensed under a Creative Commons Attribution-ShareAlike 2.5 License

 You are free:

    * to Share -- to copy, distribute, display, and perform the work
    * to Remix -- to make derivative works

 Under the following conditions:

    * Attribution. You must attribute the work in the manner specified by the author or licensor.
    * Share Alike. If you alter, transform, or build upon this work, you may distribute the resulting work only under a license identical to this one.

    * For any reuse or distribution, you must make clear to others the license terms of this work.
    * Any of these conditions can be waived if you get permission from the copyright holder.

 http://creativecommons.org/licenses/by-sa/2.5/

--

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 http://www.gnu.org/copyleft/gpl.html

*/


function isRFC822ValidEmail(sEmail) {

  var sQtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]';
  var sDtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]';
  var sAtom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+';
  var sQuotedPair = '\\x5c[\\x00-\\x7f]';
  var sDomainLiteral = '\\x5b(' + sDtext + '|' + sQuotedPair + ')*\\x5d';
  var sQuotedString = '\\x22(' + sQtext + '|' + sQuotedPair + ')*\\x22';
  var sDomain_ref = sAtom;
  var sSubDomain = '(' + sDomain_ref + '|' + sDomainLiteral + ')';
  var sWord = '(' + sAtom + '|' + sQuotedString + ')';
  var sDomain = sSubDomain + '(\\x2e' + sSubDomain + ')*';
  var sLocalPart = sWord + '(\\x2e' + sWord + ')*';
  var sAddrSpec = sLocalPart + '\\x40' + sDomain; // complete RFC822 email address spec
  var sValidEmail = '^' + sAddrSpec + '$'; // as whole string

  var reValidEmail = new RegExp(sValidEmail);

  if (reValidEmail.test(sEmail)) {
    return true;
  }

  return false;
}
