// whitespace characters
var whitespace = " \t\n\r";

/*
 * Function: isBlank (inString)
 * Purpose: Is the String Blank or empty
 * Arguments: String
 * Returns: boolean
 **/
function isBlank (inString) {
	if (inString == null || inString.length == 0) {
		return true;
	} else {
		return false;
	}
}
//********************************************************************************
//Function: isWithinRange (inString, rangeMin, rangeMax)
//Purpose: Is the String within the range
//Arguments: String, integer, integer
//Returns: boolean
//********************************************************************************
function isWithinRange (inString, rangeMin, rangeMax)  {
	if ((inString == null) || (inString == "")) { 
		return (false);
	}
	if((inString >= rangeMin) && (inString <= rangeMax)) {
		return true;
	} else {
		return false;
	}
}

//********************************************************************************
//Function: trim (string)
//Purpose: Removes the white space at the edges
//Arguments: String
//Returns: String
//********************************************************************************
function trim (string) {
	if (isBlank(string)) {
		return "";
	} else {
		return rightTrim(leftTrim(string));
	}
}

/*
 * Function: leftTrim (inString)
 * Purpose: Removes the beginning white space
 * Arguments: String
 * Returns: String
 **/
function leftTrim (inString)  {
	var outString = inString;
	for (var count = 0; count < inString.length; count++)  {
		var tempChar=inString.substring (count, count + 1);
		if (tempChar != " ") {
			outString = inString.substring (count, inString.length);
			break;
		}
	}
	return outString;
}

/*
 * Function: rightTrim (inString)
 * Purpose: Removes the ending white space
 * Arguments: String
 * Returns: String
 **/
function rightTrim (inString)  {
	var outString = inString;
	for (var count= inString.length; count > 0; count--)  {
		var tempChar = inString.substring (count - 1, count);
		if (tempChar != " ") {
			outString = inString.substring (0, count);
			break;
		}
	}
	return outString;
}

//****************************************************
// returns the amount in the .99 format
//****************************************************
function cent(amount) {
  amount -= 0;
  return (amount == Math.floor(amount)) ? amount + '.00' : (  (amount*10 == Math.floor(amount*10)) ? amount + '0' : amount);
}

//**********************************************************
// returns a number rounded to 2 decimal places
//**********************************************************
function round(number) {
  var X
  X = (!X ? 2 : X);
  return Math.round(number*Math.pow(10,X))/Math.pow(10,X);
}
//****************************************************************************
// Confirm that it is a valid Email
// Tell the user.
// Written By: Keith Valley   Sept 14, 2001
//****************************************************************************
function isEmail(theElement) {
  //var s
  //s = "You have not entered a valid E-mail"
  var str = theElement;
  if (!(result = ((str != "") && (str.indexOf("@") != -1) && (str.indexOf(".") != -1)))) {
    //return warnInvalid(theElementName, s);
		return false;
  }
  return true;
}

//****************************************************************************
// Display the tax depending on whether or not it is an exempt user
// registering.
// Written By: Keith Valley   May 10, 2006
//****************************************************************************
function isExempt(amount) {
  var SubTotal 
	var Total
	var tax
	var gst			= 6.00
	
	SubTotal =  (amount - 0);
	 //alert(SubTotal);
	 if (document.vilgmaReg.exempt.checked)
   {
       document.vilgmaReg.gst.value = "0.00";
       tax = ("0.00" - 0)
   } else {
       // figure out the tax
       tax = SubTotal / 100 * gst;
       tax = Math.floor(tax * 1000)/1000;
       document.vilgmaReg.gst.value = "" + cent(round(tax));
  }
 Total = (cent(round(tax))-0) + SubTotal;
 //alert(Total);
 document.vilgmaReg.Total.value = cent(Total);
}

//****************************************************************************
// Function: trimString
// Purpose: to remove all leading and trailing spaces from a string
// Written By: Keith Valley   Sept 14, 2001
//****************************************************************************
//function trimString (str) {
//  str = this != window? this : str;
//  return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
//}

//****************************************************************************
// Validates that the phone number is in the correct format
// format 250-433-4343
// Written by: Keith Valley
//****************************************************************************
function isPhoneNumber(theElement, theElementName, title) {
  var s
  s = "Please enter your fax number in this format: 555-555-5555."
  if (title == "Phone Number")
  {
    s = "Please enter your phone number in this format: 555-555-5555."
  }
  str = theElement;
  if (str.length != 12) {
    return warnInvalid(theElementName, s);
  }
  len = str.length;
  for (i = 0; i < len; i++) {
    c = str.charAt(i);
    if ((i == 3) || (i == 7)) {
      if (c != "-") {
        return warnInvalid(theElementName, s);
      }
    } else if ((c < "0") || (c > "9")) {
      return warnInvalid(theElementName, s);
    }
  }
  return true;
}
//***************************************************************************
// Verify if the field is empty or not.
// If it is return false and tell the user that is is a required field
// Written By: Keith Valley  - Sept 14, 2001
//**************************************************************************
function isEmpty(val, name, title)
{
  //val = stripWhitespace(val)
  
  if (val == "")
  {
    var s
    s = "You have not entered your "+ title +". Please enter before submitting your registration"
    return warnInvalid(name, s);
  }
  return true;
}
//****************************************************************************
// Notify user that contents of field theField are invalid.
// String s describes expected contents of theField.value.
// Put select theField, put focus in it, and return false.
//****************************************************************************
function warnInvalid (theField, s)
{   theField.focus()
    theField.select()
    alert(s)
    return false
}
//*****************************************************************************
// Function: isMonthAbbrev(str)
// Purpose: is this a valid Month Abbreviation
// Arguments: String
// Returns: boolean
//*****************************************************************************
function isMonthAbbrev(str, name) 
{
  str = str.toLowerCase();
  var months = new Array("jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec");
  for (var i = 0; i < months.length; i++) {
    if (str == months[i]) {
      return true;
    }
  }
  var s
  s = "The Abbreviation '"+ str +"' is not a valid Month Abbreviation.  Please enter the correct abbreviation."
  return warnInvalid(name, s);
}

//*****************************************************************************
// Function: isDayOfMonth(day, monthAbbrev)
// Purpose: is the day and the month valid
// Arguments: integer, String
// Returns: boolean
//*****************************************************************************
function isDayOfMonth(day, monthAbbrev, name) 
{
  monthAbbrev = monthAbbrev.toLowerCase();
  if (isNumeric(day) && isMonthAbbrev(monthAbbrev)) {
		
    var daysOfMonths = new Array(12);
    daysOfMonths[0]  = new Array("jan", 31);
    daysOfMonths[1]  = new Array("feb", 29); // leap years are not calculated
    daysOfMonths[2]  = new Array("mar", 31);
    daysOfMonths[3]  = new Array("apr", 30);
    daysOfMonths[4]  = new Array("may", 31);
    daysOfMonths[5]  = new Array("jun", 30);
    daysOfMonths[6]  = new Array("jul", 31);
    daysOfMonths[7]  = new Array("aug", 31);
    daysOfMonths[8]  = new Array("sep", 30);
    daysOfMonths[9]  = new Array("oct", 31);
    daysOfMonths[10] = new Array("nov", 30);
    daysOfMonths[11] = new Array("dec", 31);	

    var monthIndex = -1;
    for (var i = 0; i < daysOfMonths.length; i++) {
      if (monthAbbrev == daysOfMonths[i][0]) {
        monthIndex = i;
	break;
      }
    }
    if (monthIndex >= 0) {
      if (day >= 1 && day <= daysOfMonths[monthIndex][1]) {
	return true;
      }
    }
  }
  var s
  s = "The Day '"+ day +"' is not a valid day of the Month '"+ monthAbbrev  +"'.  Please enter the correct day and try again."
  return warnInvalid(name, s); 
}
//********************************************************************************
// Function: isNumeric(str)
// Purpose: is string a numeric (integer or float)
// Arguments: String
// Returns: boolean
//********************************************************************************
function isNumeric(str) {
  var num;

  /* matches 99.999 */
  if (str.indexOf(".") > 0) {
    num = str.split(".");
    if (num.length == 2) {
      for (var i = 0; i < num.length; i++) {
        if (!isNumberString(num[i])) {
	  return false;
	}
      }
      return true;
    } else if (num.length > 2) {
      return false;
    }
    /* matches .999 */
    } else if (str.indexOf(".") == 0) {
      var temp = str.substring(1, str.length);
      if (isNumberString(temp)) {
        return true;
      } else {
	false;
      }
      
    /* matches 999 */
    } else if (isNumberString(str)) {
      return true;
    } else {
    return false;
  }
}
//*******************************************************************************
// Function: isNumberString(inString)
// Purpose: is the String a number (Integer)
// Arguments: String
// Returns: boolean
//*******************************************************************************
function isNumberString(inString)  {
  if (inString.length == 0) { 
    return false;
  }
  var refString = "1234567890";
  for (var count=0; count < inString.length; count++)  {
    var tempChar = inString.substring (count, count + 1);
    if (refString.indexOf(tempChar, 0) == -1) {  
      return false;
    }
  }
  return true;
}
//*******************************************************************************
// Function: isYear(strYear)
// Purpose: is this a valid Year (between 1000-9999)
// Arguments: String
// Returns: boolean
//*******************************************************************************
function isYear(strYear, name) {
  var regExpObj = /^\d\d\d\d$/;
  if (regExpObj.test(strYear)) {
    return true;
  } else {
    
    var s
    s = "The Year '"+ strYear +"' is not a valid Year.  Please enter the correct year and try again."
    return warnInvalid(name, s);
  }
}
//********************************************************************************
// Function: isValidPostalCodeCanada(strPostalCode)
// Purpose: is the string a valid Canadian Postal Code Example: "V9J 9K3" 
// Arguments: String
// Returns: boolean
//********************************************************************************
//function isValidPostalCodeCanada (strPostalCode, name) {
 // var regExpObj = /^[A-Z]\d[A-Z] \d[A-Z]\d$/i;

 // if (regExpObj.test(strPostalCode)) {
  //  return true;
 // } else {
  //  var s
  //  s = "Please enter your postal code in this format: V0R 1R0."
  //  return warnInvalid(name, s);
  //}
//}
//********************************************************************************
// Function: isLetter(c)
// Purpose: to verify that a character is a letter
// Arguments: a single Character
// Returns: true if character c is an English letter (A .. Z, a..z).
//********************************************************************************
function isLetter (c)
{   
  if (c == "-")
  {
    return true;
    //alert("got here");
  }
  else
  {
   return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) )
  }
}
//********************************************************************************
//
//********************************************************************************
function isAlphabetic (s, name, title) {   
  var i;
  var send
  send = "The Field '" + title +"' is not in a Valid format.  Please include only Letters (A-Z or a-z) in this field."
  
  for (i = 0; i < s.length; i++)
  {   
        // Check that current character is letter.
        var c = s.charAt(i);
        
        if (!isLetter(c))
        return warnInvalid(name, send);
  }
    // All characters are letters.
    return true;
}
//********************************************************************************
// Removes all characters which appear in string bag from string s.
//********************************************************************************
function stripCharsInBag (s, bag)
{   var i;
    var returnString = "";

    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}
//********************************************************************************
// Removes all whitespace characters from s.
// Global variable whitespace (see above)
// defines which characters are considered whitespace.
//********************************************************************************
function stripWhitespace (s)
{   return stripCharsInBag (s, whitespace)
}


