/**************************

File Name:         validate_form.js
Description:       contains functions that are used in form validations
Created:           12May2001 (GV)
Modified:           
Called From:      most files
History:           
*/
//*************************
// functions: 
// checkString()
// checkEmail()
// checkEmpty()
// checkZIP()
// checkPhone()
// checkMatch()
// checkYear()
// checkSpecial()
// checkPostingPeriod()
// checkInquiryRange()
// checkInteger()
// checkInRange()
//*************************
var postingPeriodMin = 7;
var postingPeriodMax = 90;

var inquiryNumberMin = 1;
var inquiryNumberMax = 100;

var digits = "0123456789";
var lowercaseLetters = "abcdefghijklmnopqrstuvwxyz"
var uppercaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
var whitespace = " \t\n\r";
var decimalPointDelimiter = ".";
var phoneNumberDelimiters = "()- ";

//expert name
var invalidNameChars = digits+"<>;:~!@#$%^&*()_+{}[]<>?,/\\\"\t\n\r";  // special chars that are allowed: '. (space)
//company name
var invalidCompanyChars = "<>~@#$%^*()_+{}[]<>/\\\t\n\r";   // special chars that are allowed: ';:!?",. (space)
//address
var invalidStreetAddressChars = "<>~!@#$%^&*_+{}[]?\t\n\r"; // special chars that are allowed: ';:()/\",. (space)
//subsidiaries
var invalidSubsChars = "<>:~!@#$%^&*()_+{}[]<>?/\\\"\t\n\r"; // special chars that are allowed: ';,. (space)
//username
var invalidUsernameChars = " <>;:~!#$%^&*()_+{}[]<>?,/\\\"\t\n\r"; // special chars that are allowed: @.'
//other
var invalidSpecialChars = "<@>";

//ZIP code
var validZipChars = digits+lowercaseLetters+uppercaseLetters+" -";
var validPhoneChars = digits+phoneNumberDelimiters+"+";

function isWhitespace (s)
{   var i;
    if ((s == null) || (s.length == 0)) return true;
    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        if (whitespace.indexOf(c) == -1) return false;
    }
    return true;
}

function stripCharsInBag (s, bag)
{   var i;
    var returnString = "";
    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function stripCharsNotInBag (s, bag)
{   var i;
    var returnString = "";
    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        if (bag.indexOf(c) != -1) returnString += c;
    }
    return returnString;
}

function stripWhitespace (s)
{   return stripCharsInBag (s, whitespace)
}

function charInString (c, s)
{   for (i = 0; i < s.length; i++)
    {   if (s.charAt(i) == c) return true;
    }
    return false
}

function stripInitialWhitespace (s)
{   var i = 0;
    while ((i < s.length) && charInString (s.charAt(i), whitespace))
       i++;
    return s.substring (i, s.length);
}

function stripClosingWhitespace (s)
{ var i = s.length
  while ((i > 0) && charInString (s.charAt(i), whitespace))
    i--;
  return s.substring (0, i);
}

function isEmpty(s)
{   
	s = stripInitialWhitespace(s);
	s = stripClosingWhitespace(s);
	return ((s == null) || (s.length == 0))
}

function isLetter (c)
{   return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) )
}

function isDigit (c)
{   return ((c >= "0") && (c <= "9"))
}

function isLetterOrDigit (c)
{   return (isLetter(c) || isDigit(c))
}

function isInteger (s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        if (!isDigit(c)) return false;
    }
    return true;
}

function isAlphanumeric (s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        if (! (isLetter(c) || isDigit(c) ) )
        return false;
    }
    return true;
}

function isAlphanumericDot (s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        if (! (isLetter(c) || isDigit(c) || (c == ".") || (c == "-") || (c == "_")) )
        return false;
    }
    return true;
}

function isZip (s)
{
      sLengthBefore = s.length;
      s = stripCharsNotInBag(s, validZipChars);
      sLengthAfter = s.length;
      if (sLengthBefore != sLengthAfter) return false;
        else return true;
}

function isPhone (s)
{
      sLengthBefore = s.length;
      s = stripCharsNotInBag(s, validPhoneChars);
      sLengthAfter = s.length;
      if (sLengthBefore != sLengthAfter) return false;
        else return true;
}

//***************************
function isString (s, bag)
{ 
      sLengthBefore = s.length;
      s = stripCharsInBag(s, bag);
      sLengthAfter = s.length;
      if (sLengthBefore != sLengthAfter) return false;
        else return true;
}

function isEmail (s)
{  
  if (isWhitespace(s)) return false;
    var i = 1;
    var sLength = s.length;
    if (!isAlphanumeric(s.charAt(0))) return false; 
    while ((i < sLength) && (s.charAt(i) != "@"))
    {
      if (!isAlphanumericDot(s.charAt(i))) return false; 
      i++
    }
    if ((i >= sLength) || (s.charAt(i) != "@")) return false;
    else i += 2;
    if (!isAlphanumeric(s.charAt(i-1))) return false; 
    while ((i < sLength) && (s.charAt(i) != "."))
    { 
      if (!isAlphanumericDot(s.charAt(i))) return false; 
      i++
    }
    if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
    else i += 2;
    if (!isAlphanumeric(s.charAt(i-1))) return false;
    while (i < sLength - 1)
    {
      if (!isAlphanumericDot(s.charAt(i))) return false;
      i++
    }
  return true;
}

function isYear (s)
{  
    if (!isInteger(s)) return false;
    var num = parseInt (s);
    if (num < 1000) return false;
    return (s.length == 4);
}

function isInRange (s, a, b)
{   
    var num = parseInt (s);
    return ((num >= a) && (num <= b));
}

function isDate (s, style)
{
	delimiter = "." // default
	var daysInMonth = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
	switch (style) {
		case 1: // dd.mm.gggg
			{
				delimiter = "."
				if (s.length > 10) return false 
				regexp = /\d?\d\.\d?\d\.\d{4}/
				if (s.search(regexp) != 0) return false
				break;
			}
		case 2: // dd-mm-gggg
			{
				delimiter = "-"
				if (s.length > 10) return false 
				regexp = /\d?\d\-\d?\d\-\d{4}/
				if (s.search(regexp) != 0) return false 
				break;
			}
		case 3: // dd/mm/yyyy
			{
				delimiter = "/"
				if (s.length > 10) return false // check length
				regexp = /\d?\d\/\d?\d\/\d{4}/
				if (s.search(regexp) != 0) return false // should start at position 0
				break;
			}
	}
	sArray = s.split(delimiter, 3)
	var day = sArray[0]
	var month = sArray[1]
	var year = sArray[2]
	if (!isYear(year)) return false
	if ((month < 1) || (month > 12)) return false
	if ((day < 1) || (day > daysInMonth[month-1])) return false
	return true
}

function warnEmpty(theField, s)
{   
	theField.focus();
    alert(s);
    return false;
}

function warnInvalid (theField, s)
{   theField.focus()
    theField.select()
    alert(s)
    return false
}

function checkEmpty(theField, se)
{
    theField.value = stripInitialWhitespace(theField.value);
    theField.value = stripClosingWhitespace(theField.value);
    if (isEmpty(theField.value)) {
        warnEmpty(theField, se);
        return false;}
    else return true;
}

//****************************************
//***************************************
function checkMatch (field1, field2, s) {
    field1.value = stripInitialWhitespace(field1.value);
    field1.value = stripClosingWhitespace(field1.value);
    field2.value = stripInitialWhitespace(field2.value);
    field2.value = stripClosingWhitespace(field2.value);
  if (field1.value != field2.value) {
    alert(s);
    return(false);
  } else {return(true);}
}

function checkNotMatch (field1, field2, s) {
    field1.value = stripInitialWhitespace(field1.value);
    field1.value = stripClosingWhitespace(field1.value);
    field2.value = stripInitialWhitespace(field2.value);
    field2.value = stripClosingWhitespace(field2.value);
  if (field1.value == field2.value) {
    alert(s);
    return(false);
  } else {return(true);}
}


function checkZIP (theField, s)
{ 
    theField.value = stripInitialWhitespace(theField.value);
    theField.value = stripClosingWhitespace(theField.value);
    if (!isZip(theField.value))
       return warnInvalid (theField, s);
    return true;
}

function checkPhone (theField, s)
{  
    theField.value = stripInitialWhitespace(theField.value);
    theField.value = stripClosingWhitespace(theField.value);
    if (!isPhone(theField.value))
       return warnInvalid (theField, s);
    return true;
}

function checkDate (theField, style, s)
{
	theField.value = stripInitialWhitespace(theField.value);
	theField.value = stripClosingWhitespace(theField.value);
	if (!isEmpty(theField.value)) 
    {
	if (!isDate(theField.value, style))
		return warnInvalid(theField, s)
	}
	return true;
}

function checkString (theField, bag, s)
{  
    theField.value = stripInitialWhitespace(theField.value);
    theField.value = stripClosingWhitespace(theField.value);
    switch (bag) {
      case 'name': bag = invalidNameChars;break;
      case 'company': bag = invalidCompanyChars;break;
      case 'street': bag = invalidStreetAddressChars;break;
      case 'subsidiary': bag = invalidSubsChars;break;
      case 'username': bag = invalidUsernameChars;break;
      case 'special': bag = invalidSpecialChars;break;
      default:
    } 
    if (!isString(theField.value, bag))
       return warnInvalid (theField, s);
    return true;
}

//*****************************************
//*****************************************

function checkEmail (theField, s)
{  
    theField.value = stripInitialWhitespace(theField.value);
    theField.value = stripClosingWhitespace(theField.value);
    if (!isEmpty(theField.value)) 
    {
      if (!isEmail(theField.value)) 
         return warnInvalid (theField, s);
    }
    return true;
}

function checkYear (theField, s)
{  
    theField.value = stripInitialWhitespace(theField.value);
    theField.value = stripClosingWhitespace(theField.value);
    if (!isYear(theField.value))
      return warnInvalid(theField, s);
    return true;
}

function checkPostingPeriod (theField, s_acc, s_len)
{
    theField.value = stripInitialWhitespace(theField.value);
    theField.value = stripClosingWhitespace(theField.value);
    if (!isInteger(theField.value)) 
        return warnInvalid(theField, s_acc)
    if (!isInRange(theField.value, postingPeriodMin, postingPeriodMax))
        return warnInvalid(theField, s_len)
    return true;
}

function checkInquiryNumber (theField, s_acc, s_len)
{
    theField.value = stripInitialWhitespace(theField.value);
    theField.value = stripClosingWhitespace(theField.value);
    if (!isInteger(theField.value)) 
        return warnInvalid(theField, s_acc)
    if (!isInRange(theField.value, inquiryNumberMin, inquiryNumberMax))
        return warnInvalid(theField, s_len)
    return true;
}

function checkSpecial (theField, s)
{
  var areaText = theField.innerText;
  if (!isString(areaText, invalidSpecialChars)) 
    return warnInvalid(theField, s)
  return true;
}

function checkInteger (theField, s)
{
    theField.value = stripInitialWhitespace(theField.value);
    theField.value = stripClosingWhitespace(theField.value);
    if (!isInteger(theField.value)) 
        return warnInvalid(theField, s)
    return true;
}

function checkInRange (theField, r_min, r_max, s)
{
    theField.value = stripInitialWhitespace(theField.value);
    theField.value = stripClosingWhitespace(theField.value);
    if (!isInRange(theField.value, r_min, r_max))
        return warnInvalid(theField, s)
    return true;
}

function checkAlphanumeric (theField, s)
{
    theField.value = stripInitialWhitespace(theField.value);
    theField.value = stripClosingWhitespace(theField.value);
    if (isAlphanumeric(theField.value))
        return warnInvalid(theField, s)
    return true;
}